a37dad77691017571e161d42ddd78d38ad9a029c
16 #define CEIL(x, y) (((x) + (y) - 1) / (y))
17 #define MIN(x, y) (((x) < (y)) ? (x) : (y))
18 #define MAX(x, y) (((x) > (y)) ? (x) : (y))
20 //#define BUFFERSIZE 4096
21 #define BUFFERSIZE 256
34 char *progname
= NULL
;
40 FILE *fd
= ret
? stderr
: stdout
;
41 fprintf (fd
, "usage: %s [-i file] [-h] [-m k&r|ansi|c99] [-o file] [-v]\n", progname
);
42 fprintf (fd
, " -i : input file\n");
43 fprintf (fd
, " -h : help message\n");
44 fprintf (fd
, " -m : indent mode\n");
45 fprintf (fd
, " -o : output file\n");
46 fprintf (fd
, " -v : verbose level (%d)\n", verbose
);
53 int indent (FILE *fin
, FILE *fout
, cmode_t cmode
) {
54 char bufin
[BUFFERSIZE
+ 1] = {0};
55 char bufout
[BUFFERSIZE
* TABSIZE
+ 1] = {0};
67 memset (bufin
, 0, sizeof (bufin
));
68 memset (bufout
, 0, sizeof (bufout
));
71 nb
= fread (bufin
, 1, BUFFERSIZE
, fin
);
72 VERBOSE (DEBUG
, fprintf (stdout
, "buffer in: %d\n", nb
));
74 VERBOSE (ERROR
, fprintf (stderr
, "can't read file (%d)\n", errno
));
81 while (*ptin
!= '\0') {
82 VERBOSE (DEBUG
, fprintf (stdout
, "caracter: %c\n", *ptin
));
86 if (((comment
== 1) && (*ptin
== '\n')) ||
87 ((comment
== 2) && ((*ptin
== '*') && (ptin
[1] == '/')))) {
98 comment
= (ptin
[1] == '/') ? 1 : (ptin
[1] == '*') ? 2 : 0;
100 for (i
= 0; i
< nbindent
* TABSIZE
; i
++) {
115 for (i
= 0; i
< nbindent
* TABSIZE
; i
++) {
127 for (i
= 0; i
< nbindent
* TABSIZE
; i
++) {
131 if (ptin
[1] != ';') {
157 if ((*ptin
== '"') && (!character
) && (!special
)) {
160 if ((*ptin
== '\'') && (!string
) && (!special
)) {
164 for (i
= 0; i
< nbindent
* TABSIZE
; i
++) {
171 special
= (*ptin
== '\\');
172 parent
+= (*ptin
== '(') ? +1 : (*ptin
== ')') ? -1 : 0;
178 VERBOSE (DEBUG
, fprintf (stdout
, "buffer out: %d\n", strlen (bufout
)));
180 while ((nb
= fwrite (ptout
, 1, strlen (ptout
), fout
)) != strlen (ptout
)) {
181 VERBOSE (DEBUG
, fprintf (stdout
, "buffer out: %d/%d\n", nb
, strlen (ptout
)));
183 VERBOSE (ERROR
, fprintf (stderr
, "can't write file (%d)\n", errno
));
199 int main (int argc
, char *argv
[])
201 cmode_t cmode
= e_unknown
;
207 char *pt
= progname
= argv
[0];
209 if ((*pt
== '/') || (*pt
== '\\')) {
216 while ((c
= getopt(argc
, argv
, "i:hm:o:v:")) != EOF
) {
228 verbose
= atoi (optarg
);
235 if (argc
- optind
!= 0) {
236 fprintf (stderr
, "%s: invalid option -- %s\n", progname
, argv
[optind
]);
243 fin
= fopen (input
, "rb");
245 VERBOSE (ERROR
, fprintf (stderr
, "error: can't open file '%s'\n", input
));
254 fout
= fopen (output
, "wb");
256 VERBOSE (ERROR
, fprintf (stderr
, "error: can't open file '%s'\n", output
));
264 if (strcmp (mode
, "ansi") == 0) {
266 } else if (strcmp (mode
, "k&r") == 0) {
269 VERBOSE (ERROR
, fprintf (stderr
, "error: mode '%s' unknown\n", mode
));
272 return indent (fin
, fout
, cmode
);
275 // test: indent.exe -h
276 // test: indent.exe -h | awk '/usage:/ { rc=1 } END { exit (1-rc) }'
277 // test: indent.exe -_ 2> /dev/null | awk 'END { if (NR == 0) { exit(0) } else exit (1) }'
278 // test: indent.exe -_ 2>&1 | awk '/usage:/ { rc=1 } END { exit (1-rc) }'
280 /* vim: set ts=4 sw=4 et: */