145661ba7950afed98f7a0c292e6e9f04ee87ffc
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};
61 nb
= fread (bufin
, 1, BUFFERSIZE
, fin
);
62 VERBOSE (DEBUG
, fprintf (stdout
, "buffer in: %d\n", nb
));
64 VERBOSE (ERROR
, fprintf (stderr
, "can't read file (%d)\n", errno
));
71 while (*ptin
!= '\0') {
74 for (i
= 0; i
< TABSIZE
; i
++) {
92 VERBOSE (DEBUG
, fprintf (stdout
, "buffer out: %d\n", strlen (bufout
)));
94 while ((nb
= fread (ptout
, 1, strlen (ptout
), fout
)) != strlen (ptout
)) {
96 VERBOSE (ERROR
, fprintf (stderr
, "can't write file (%d)\n", errno
));
112 int main (int argc
, char *argv
[])
114 cmode_t cmode
= e_unknown
;
120 char *pt
= progname
= argv
[0];
122 if ((*pt
== '/') || (*pt
== '\\')) {
129 while ((c
= getopt(argc
, argv
, "i:hm:o:v:")) != EOF
) {
141 verbose
= atoi (optarg
);
148 if (argc
- optind
!= 0) {
149 fprintf (stderr
, "%s: invalid option -- %s\n", progname
, argv
[optind
]);
156 fin
= fopen (input
, "rb");
158 VERBOSE (ERROR
, fprintf (stderr
, "error: can't open file '%s'\n", input
));
167 fout
= fopen (input
, "wb");
169 VERBOSE (ERROR
, fprintf (stderr
, "error: can't open file '%s'\n", output
));
177 if (strcmp (mode
, "ansi") == 0) {
179 } else if (strcmp (mode
, "k&r") == 0) {
182 VERBOSE (ERROR
, fprintf (stderr
, "error: mode '%s' unknown\n", mode
));
185 return indent (fin
, fout
, cmode
);
188 // test: indent.exe -h
189 // test: indent.exe -h | awk '/usage:/ { rc=1 } END { exit (1-rc) }'
190 // test: indent.exe -_ 2> /dev/null | awk 'END { if (NR == 0) { exit(0) } else exit (1) }'
191 // test: indent.exe -_ 2>&1 | awk '/usage:/ { rc=1 } END { exit (1-rc) }'
193 /* vim: set ts=4 sw=4 et: */