add debug info
[indent.git] / indent.c
CommitLineData
b811a28a
LM
1/* depend: */
2/* cflags: */
3/* linker: debug.o */
4
5#include <assert.h>
6#include <getopt.h>
7#include <malloc.h>
8#include <stdio.h>
9#include <stdlib.h>
10#include <string.h>
11
12#include "debug.h"
13
14/* macros */
15
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))
19
20//#define BUFFERSIZE 4096
21#define BUFFERSIZE 256
222cc715
LM
22#define TABSIZE 4
23
24/* type definition */
25
26typedef enum {
27 e_unknown = 0,
28 e_ansi,
29 e_kr
30} cmode_t;
b811a28a
LM
31
32/* gobal variables */
33
34char *progname = NULL;
35
36/* help function */
37
38void usage (int ret)
39{
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);
47
48 exit (ret);
49}
50
51/* indent function */
52
222cc715
LM
53int indent (FILE *fin, FILE *fout, cmode_t cmode) {
54 char bufin[BUFFERSIZE + 1] = {0};
55 char bufout[BUFFERSIZE * TABSIZE + 1] = {0};
56 size_t i, nb;
b811a28a 57
b811a28a 58 while (!feof (fin)) {
222cc715
LM
59
60 /* read file */
61 nb = fread (bufin, 1, BUFFERSIZE, fin);
943fa96e 62 VERBOSE (DEBUG, fprintf (stdout, "buffer in: %d\n", nb));
222cc715 63 if (errno != 0) {
943fa96e 64 VERBOSE (ERROR, fprintf (stderr, "can't read file (%d)\n", errno));
222cc715
LM
65 exit (1);
66 }
67
68 /* process line */
69 char *ptin = bufin;
70 char *ptout = bufout;
71 while (*ptin != '\0') {
72 switch (*ptin) {
73 case '\t':
74 for (i = 0; i < TABSIZE; i++) {
75 *ptout++ = ' ';
76 }
77 break;
78 case '{':
79 case '}':
80 case ';':
81 *ptout++ = *ptin;
82 *ptout++ = '\n';
83 break;
84 default:
85 *ptout++ = *ptin;
b811a28a 86 }
222cc715 87 ptin++;
b811a28a 88 }
222cc715
LM
89 ptout = '\0';
90
91 /* write file */
943fa96e 92 VERBOSE (DEBUG, fprintf (stdout, "buffer out: %d\n", strlen (bufout)));
222cc715
LM
93 ptout = bufout;
94 while ((nb = fread (ptout, 1, strlen (ptout), fout)) != strlen (ptout)) {
95 if (errno != 0) {
943fa96e 96 VERBOSE (ERROR, fprintf (stderr, "can't write file (%d)\n", errno));
222cc715 97 exit (1);
b811a28a 98 }
222cc715
LM
99 ptout += nb;
100 }
b811a28a
LM
101 }
102
103 /* close all */
104 fclose (fin);
105 fclose (fout);
106
107 return 0;
108}
109
110/* main function */
111
112int main (int argc, char *argv[])
113{
222cc715 114 cmode_t cmode = e_unknown;
b811a28a
LM
115 char *input = NULL;
116 char *mode = "ansi";
117 char *output = NULL;
118
119 /* get basename */
120 char *pt = progname = argv[0];
121 while (*pt) {
122 if ((*pt == '/') || (*pt == '\\')) {
123 progname = pt + 1;
124 }
125 pt++;
126 }
127
128 int c;
129 while ((c = getopt(argc, argv, "i:hm:o:v:")) != EOF) {
130 switch (c) {
131 case 'i':
132 input = optarg;
133 break;
134 case 'm':
135 mode = optarg;
136 break;
137 case 'o':
138 output = optarg;
139 break;
140 case 'v':
141 verbose = atoi (optarg);
142 break;
143 case 'h':
144 default:
145 usage (c != 'h');
146 }
147 }
148 if (argc - optind != 0) {
149 fprintf (stderr, "%s: invalid option -- %s\n", progname, argv[optind]);
150 usage (1);
151 }
152
153 /* check input */
154 FILE *fin = NULL;
155 if (input) {
156 fin = fopen (input, "rb");
157 if (!fin) {
158 VERBOSE (ERROR, fprintf (stderr, "error: can't open file '%s'\n", input));
159 }
160 } else {
161 fin = stdin;
162 }
163
164 /* check output */
165 FILE *fout = NULL;
166 if (output) {
167 fout = fopen (input, "wb");
168 if (!fout) {
169 VERBOSE (ERROR, fprintf (stderr, "error: can't open file '%s'\n", output));
170 fclose (fin);
171 }
172 } else {
173 fout = stdout;
174 }
175
222cc715
LM
176 /* check mode */
177 if (strcmp (mode, "ansi") == 0) {
178 cmode = e_ansi;
179 } else if (strcmp (mode, "k&r") == 0) {
180 cmode = e_kr;
181 } else {
182 VERBOSE (ERROR, fprintf (stderr, "error: mode '%s' unknown\n", mode));
183 }
184
185 return indent (fin, fout, cmode);
b811a28a
LM
186}
187
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) }'
192
193/* vim: set ts=4 sw=4 et: */