first process
[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);
b811a28a 62 VERBOSE (DEBUG, fprintf (stdout, "buffer: %d\n", nb));
222cc715
LM
63 if (errno != 0) {
64 VERBOSE (ERROR, fprintf (stderr, "can't read file\n"));
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 */
92 ptout = bufout;
93 while ((nb = fread (ptout, 1, strlen (ptout), fout)) != strlen (ptout)) {
94 if (errno != 0) {
95 VERBOSE (ERROR, fprintf (stderr, "can't write file\n"));
96 exit (1);
b811a28a 97 }
222cc715
LM
98 ptout += nb;
99 }
b811a28a
LM
100 }
101
102 /* close all */
103 fclose (fin);
104 fclose (fout);
105
106 return 0;
107}
108
109/* main function */
110
111int main (int argc, char *argv[])
112{
222cc715 113 cmode_t cmode = e_unknown;
b811a28a
LM
114 char *input = NULL;
115 char *mode = "ansi";
116 char *output = NULL;
117
118 /* get basename */
119 char *pt = progname = argv[0];
120 while (*pt) {
121 if ((*pt == '/') || (*pt == '\\')) {
122 progname = pt + 1;
123 }
124 pt++;
125 }
126
127 int c;
128 while ((c = getopt(argc, argv, "i:hm:o:v:")) != EOF) {
129 switch (c) {
130 case 'i':
131 input = optarg;
132 break;
133 case 'm':
134 mode = optarg;
135 break;
136 case 'o':
137 output = optarg;
138 break;
139 case 'v':
140 verbose = atoi (optarg);
141 break;
142 case 'h':
143 default:
144 usage (c != 'h');
145 }
146 }
147 if (argc - optind != 0) {
148 fprintf (stderr, "%s: invalid option -- %s\n", progname, argv[optind]);
149 usage (1);
150 }
151
152 /* check input */
153 FILE *fin = NULL;
154 if (input) {
155 fin = fopen (input, "rb");
156 if (!fin) {
157 VERBOSE (ERROR, fprintf (stderr, "error: can't open file '%s'\n", input));
158 }
159 } else {
160 fin = stdin;
161 }
162
163 /* check output */
164 FILE *fout = NULL;
165 if (output) {
166 fout = fopen (input, "wb");
167 if (!fout) {
168 VERBOSE (ERROR, fprintf (stderr, "error: can't open file '%s'\n", output));
169 fclose (fin);
170 }
171 } else {
172 fout = stdout;
173 }
174
222cc715
LM
175 /* check mode */
176 if (strcmp (mode, "ansi") == 0) {
177 cmode = e_ansi;
178 } else if (strcmp (mode, "k&r") == 0) {
179 cmode = e_kr;
180 } else {
181 VERBOSE (ERROR, fprintf (stderr, "error: mode '%s' unknown\n", mode));
182 }
183
184 return indent (fin, fout, cmode);
b811a28a
LM
185}
186
187// test: indent.exe -h
188// test: indent.exe -h | awk '/usage:/ { rc=1 } END { exit (1-rc) }'
189// test: indent.exe -_ 2> /dev/null | awk 'END { if (NR == 0) { exit(0) } else exit (1) }'
190// test: indent.exe -_ 2>&1 | awk '/usage:/ { rc=1 } END { exit (1-rc) }'
191
192/* vim: set ts=4 sw=4 et: */