2b8834583edf60f530378a8cd1a0a7a01b4a66c6
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
25 char *progname
= NULL
;
31 FILE *fd
= ret
? stderr
: stdout
;
32 fprintf (fd
, "usage: %s [-i file] [-h] [-m k&r|ansi|c99] [-o file] [-v]\n", progname
);
33 fprintf (fd
, " -i : input file\n");
34 fprintf (fd
, " -h : help message\n");
35 fprintf (fd
, " -m : indent mode\n");
36 fprintf (fd
, " -o : output file\n");
37 fprintf (fd
, " -v : verbose level (%d)\n", verbose
);
44 int indent (FILE *fin
, FILE *fout
) {
45 char buffer
[BUFFERSIZE
+ 1] = {0};
49 int nb
= fread (pt
, 1, BUFFERSIZE
- (pt
- buffer
), fin
);
50 VERBOSE (DEBUG
, fprintf (stdout
, "buffer: %d\n", nb
));
53 while (pt
[i
] != '\0') {
54 if (pt
[i
++] == '\n') {
59 VERBOSE (DEBUG
, fprintf (stdout
, "line: %d\n", strlen (line
)));
60 VERBOSE (DEBUG
, fprintf (stdout
, "out: %s\n", line
));
63 while (line
[k
] != '\0') {
67 if (begin
) trailing
++;
78 if (pt
- buffer
< BUFFERSIZE
) {
79 for (i
= pt
- buffer
; i
< BUFFERSIZE
; i
++) {
95 int main (int argc
, char *argv
[])
102 char *pt
= progname
= argv
[0];
104 if ((*pt
== '/') || (*pt
== '\\')) {
111 while ((c
= getopt(argc
, argv
, "i:hm:o:v:")) != EOF
) {
123 verbose
= atoi (optarg
);
130 if (argc
- optind
!= 0) {
131 fprintf (stderr
, "%s: invalid option -- %s\n", progname
, argv
[optind
]);
138 fin
= fopen (input
, "rb");
140 VERBOSE (ERROR
, fprintf (stderr
, "error: can't open file '%s'\n", input
));
149 fout
= fopen (input
, "wb");
151 VERBOSE (ERROR
, fprintf (stderr
, "error: can't open file '%s'\n", output
));
158 return indent (fin
, fout
);
161 // test: indent.exe -h
162 // test: indent.exe -h | awk '/usage:/ { rc=1 } END { exit (1-rc) }'
163 // test: indent.exe -_ 2> /dev/null | awk 'END { if (NR == 0) { exit(0) } else exit (1) }'
164 // test: indent.exe -_ 2>&1 | awk '/usage:/ { rc=1 } END { exit (1-rc) }'
166 /* vim: set ts=4 sw=4 et: */