17 #define CEIL(x, y) (((x) + (y) - 1) / (y))
18 #define MIN(x, y) (((x) < (y)) ? (x) : (y))
19 #define MAX(x, y) (((x) > (y)) ? (x) : (y))
21 //#define BUFFERSIZE 4096
22 #define BUFFERSIZE 256
35 char *progname
= NULL
;
41 FILE *fd
= ret
? stderr
: stdout
;
42 fprintf (fd
, "usage: %s [-i file] [-h] [-m k&r|ansi|c99] [-o file] [-v]\n", progname
);
43 fprintf (fd
, " -i : input file\n");
44 fprintf (fd
, " -h : help message\n");
45 fprintf (fd
, " -m : indent mode\n");
46 fprintf (fd
, " -o : output file\n");
47 fprintf (fd
, " -v : verbose level (%d)\n", verbose
);
54 int indent (FILE *fin
, FILE *fout
, cmode_t cmode
) {
55 char bufin
[BUFFERSIZE
+ 1] = {0};
56 char bufout
[BUFFERSIZE
* TABSIZE
+ 1] = {0};
69 memset (bufin
, 0, sizeof (bufin
));
70 memset (bufout
, 0, sizeof (bufout
));
73 nb
= fread (bufin
, 1, BUFFERSIZE
, fin
);
74 VERBOSE (DEBUG
, fprintf (stdout
, "buffer in: %d\n", nb
));
76 VERBOSE (ERROR
, fprintf (stderr
, "can't read file (%d)\n", errno
));
83 while (*ptin
!= '\0') {
84 VERBOSE (DEBUG
, fprintf (stdout
, "caracter: %c\n", *ptin
));
88 if (((comment
== 1) && (*ptin
== '\n')) ||
89 ((comment
== 2) && ((*ptin
== '*') && (ptin
[1] == '/')))) {
100 comment
= (ptin
[1] == '/') ? 1 : (ptin
[1] == '*') ? 2 : 0;
102 for (i
= 0; i
< nbindent
* TABSIZE
; i
++) {
112 if ((string
) || (!space
)) {
119 for (i
= 0; i
< nbindent
* TABSIZE
; i
++) {
131 for (i
= 0; i
< nbindent
* TABSIZE
; i
++) {
135 if (ptin
[1] != ';') {
161 if ((*ptin
== '"') && (!character
) && (!special
)) {
164 if ((*ptin
== '\'') && (!string
) && (!special
)) {
168 for (i
= 0; i
< nbindent
* TABSIZE
; i
++) {
175 space
= ((*ptin
== ' ') || (*ptin
== '\t'));
176 special
= (*ptin
== '\\');
177 parent
+= (*ptin
== '(') ? +1 : (*ptin
== ')') ? -1 : 0;
183 VERBOSE (DEBUG
, fprintf (stdout
, "buffer out: %d\n", strlen (bufout
)));
185 while ((nb
= fwrite (ptout
, 1, strlen (ptout
), fout
)) != strlen (ptout
)) {
186 VERBOSE (DEBUG
, fprintf (stdout
, "buffer out: %d/%d\n", nb
, strlen (ptout
)));
188 VERBOSE (ERROR
, fprintf (stderr
, "can't write file (%d)\n", errno
));
204 int main (int argc
, char *argv
[])
206 cmode_t cmode
= e_unknown
;
212 char *pt
= progname
= argv
[0];
214 if ((*pt
== '/') || (*pt
== '\\')) {
221 while ((c
= getopt(argc
, argv
, "i:hm:o:v:")) != EOF
) {
233 verbose
= atoi (optarg
);
240 if (argc
- optind
!= 0) {
241 fprintf (stderr
, "%s: invalid option -- %s\n", progname
, argv
[optind
]);
248 fin
= fopen (input
, "rb");
250 VERBOSE (ERROR
, fprintf (stderr
, "error: can't open file '%s'\n", input
));
259 fout
= fopen (output
, "wb");
261 VERBOSE (ERROR
, fprintf (stderr
, "error: can't open file '%s'\n", output
));
269 if (strcmp (mode
, "ansi") == 0) {
271 } else if (strcmp (mode
, "k&r") == 0) {
274 VERBOSE (ERROR
, fprintf (stderr
, "error: mode '%s' unknown\n", mode
));
277 return indent (fin
, fout
, cmode
);
280 // test: indent.exe -h
281 // test: indent.exe -h | awk '/usage:/ { rc=1 } END { exit (1-rc) }'
282 // test: indent.exe -_ 2> /dev/null | awk 'END { if (NR == 0) { exit(0) } else exit (1) }'
283 // test: indent.exe -_ 2>&1 | awk '/usage:/ { rc=1 } END { exit (1-rc) }'
285 /* vim: set ts=4 sw=4 et: */