ae2f_docs
Loading...
Searching...
No Matches
main.c
1#include "./main.auto.h"
2
3#if NDEBUG
4#define DEBUG 0
5#else
6#define DEBUG 1
7#endif
8
9#include <stdio.h>
10#include <string.h>
11#include <assert.h>
12
13enum STATE_t {
14 STATE_GOOD,
15 STATE_EOF,
16 STATE_OPENING_UNKNOWN,
17 STATE_STACK_SMASHED,
18 STATE_ARGC_UNEXPECTED,
19 STATE_FOPEN_BAD,
20 STATE_UNEXPECTED
21};
22
23struct STACK_T {
24 FILE* m_inp;
25 char m_dir[DIRLEN + 1];
26 char m_current[DIRLEN + PATHLEN + 1];
27} STACK[STACKLEN + 1];
28size_t STACK_IDX = 0;
29size_t STACK_IDX_BUFF;
30
31#define STACK_LAST STACK[STACK_IDX]
32#define STACK_NXT STACK[STACK_IDX + 1]
33
34int Ch;
35int i;
36
37char inc_key[sizeof("#include")] = {0, };
38char inc_path_buff[PATHLEN + 1] = {0, };
39
40typedef unsigned char flag_t;
41struct FLAGS_t {
42 /** is a key of include */
43 flag_t m_Key : 1;
44 flag_t m_path_nclosed : 1;
45 flag_t m_ManyCmtOutMaybe : 1;
46 flag_t m_Key_Idx : 4;
47} FLAGS;
48
49char path_closing;
50char* path_cursor;
51
52int main(int argc, char** argv) {
53 assert(argc > 0);
54 if(argc < 1) {
55 return -STATE_ARGC_UNEXPECTED;
56 }
57 for(i = argc; i-- != 1;) {
58 if(strlen(argv[i]) > DIRLEN)
59 return -1;
60 }
61
62 STACK_LAST.m_inp = stdin;
63 STACK_LAST.m_dir[0] = 0;
64
65KEYGET:
66
67 Ch = 0;
68
69 memset(inc_key, 0, sizeof(inc_key));
70 inc_key[0] = '#';
71
72 assert(STACK_LAST.m_inp);
73
74 while((Ch = fgetc(STACK_LAST.m_inp)) != EOF) {
75 switch(Ch) {
76 case '\'':
77 {
78 fputc('\'', stdout);
79
80 while((Ch = fgetc(STACK_LAST.m_inp)) != EOF) {
81 fputc(Ch, stdout);
82 if(Ch == '\'') {
83 goto KEYGET;
84 }
85 }
86
87 } return -STATE_EOF;
88
89#if 1
90 case '"':
91 {
92 fputc('"', stdout);
93
94 while((Ch = fgetc(STACK_LAST.m_inp)) != EOF) {
95 fputc(Ch, stdout);
96 if(Ch == '"') {
97 goto KEYGET;
98 }
99 }
100
101 } return -STATE_EOF;
102#endif
103
104 case '#':
105 {
106 while((Ch = fgetc(STACK_LAST.m_inp)) != EOF
107 && (Ch == ' '
108 || Ch == '\t')
109 );
110 if(Ch == EOF) return -STATE_EOF;
111 inc_key[1] = Ch;
112
113 FLAGS.m_Key_Idx = 2;
114
115 if(Ch == 'i')
116 for(
117 ;
118 FLAGS.m_Key_Idx < sizeof("#include") - 1
119 && (inc_key[FLAGS.m_Key_Idx] = fgetc(STACK_LAST.m_inp))
120 == "#include"[FLAGS.m_Key_Idx];
121 FLAGS.m_Key_Idx++
122 );
123
124 if(FLAGS.m_Key_Idx == sizeof("#include") - 1) {
125 goto STACK_SUB;
126 } else {
127 fputs(inc_key, stdout);
128 }
129 }
130 goto KEYGET;
131
132 case '/':
133 switch(Ch = fgetc(STACK_LAST.m_inp)) {
134 case '/':
135 {
136#if DEBUG
137 fputs("//", stdout);
138#endif
139
140 while((Ch = fgetc(STACK_LAST.m_inp)) != EOF && Ch != '\n' && Ch != '\r') {
141#if DEBUG
142 fputc(Ch, stdout);
143#endif
144 }
145
146 if(Ch == EOF) return -STATE_EOF;
147 puts("");
148 goto KEYGET;
149 }
150 default:
151 fputc('/', stdout);
152 fputc(Ch, stdout);
153 goto KEYGET;
154#if 1
155 case '*':
156 fputs("/*", stdout);
157
158 while((Ch = fgetc(STACK_LAST.m_inp)) != EOF) {
159 if(Ch == '*') {
160 if((Ch = fgetc(STACK_LAST.m_inp)) == '/') {
161 fputs("*/", stdout);
162 goto KEYGET;
163 }
164 fputc('*', stdout);
165 fputc(Ch, stdout);
166 }
167 fputc(Ch, stdout);
168 }
169
170 goto KEYGET;
171#endif
172 } return -STATE_UNEXPECTED;
173
174 default:
175 {
176 fputc(Ch, stdout);
177 goto KEYGET;
178 }
179 }
180 }
181
182 goto STACK_ADD;
183STACK_SUB:
184 while((Ch = fgetc(STACK_LAST.m_inp)) != EOF && (Ch == ' ' || Ch == '\t'));
185 switch(Ch) {
186 case EOF:
187#if DEBUG
188 puts("/** Unknown EOF bug has appeared. */");
189#endif
190 return -STATE_EOF;
191 case '\"': path_closing = '\"'; break;
192 case '<': path_closing = '>'; break;
193 default:
194 fputc(Ch, stdout);
195 goto KEYGET;
196 }
197
198#if DEBUG
199 puts("/** include detected */");
200#endif
201
202 path_cursor = inc_path_buff;
203 FLAGS.m_path_nclosed = 1;
204#if DEBUG
205 puts("/** Path cursor */");
206#endif
207 if(STACK_IDX == STACKLEN) {
208#if DEBUG
209 puts("/** Stack has smashed. shame. */");
210 fputs("/*", stdout);
211#endif
212
213 while((Ch = fgetc(STACK_LAST.m_inp)) != EOF && Ch != '\n' && Ch != '\r') {
214#if DEBUG
215 fputc(Ch, stdout);
216#endif
217 }
218
219#if DEBUG
220 puts("*/");
221#endif
222
223
225
226
227 goto KEYGET;
228#endif
229 return -STATE_STACK_SMASHED;
230 }
231
232 while(path_cursor - inc_path_buff < PATHLEN) {
233 if((Ch = fgetc(STACK_LAST.m_inp)) == EOF)
234 return -STATE_EOF;
235 if(Ch == path_closing) {
236 *path_cursor = 0;
237 FLAGS.m_path_nclosed = 0;
238 break;
239 }
240 *path_cursor = Ch;
241 path_cursor++;
242 }
243 if(FLAGS.m_path_nclosed) return -STATE_OPENING_UNKNOWN;
244
245 strcpy(STACK_NXT.m_current, STACK_LAST.m_dir);
246 strcat(STACK_NXT.m_current, inc_path_buff);
247
248#if DEBUG
249 fputs("/* Opening: ", stdout);
250 fputs(STACK_NXT.m_current, stdout);
251 fputs(" */\n", stdout);
252#endif
253 STACK_NXT.m_inp = fopen(STACK_NXT.m_current, "r");
254
255#if DEBUG
256 printf("/** first fp: %p */\n", STACK_NXT.m_inp);
257#endif
258
259
260 for(i = argc; !STACK_NXT.m_inp && i-- != 1;) {
261 strcpy( STACK_NXT.m_current, argv[i]);
262 strcat(STACK_NXT.m_current, inc_path_buff);
263
264#if DEBUG
265 fputs("/* Opening: ", stdout);
266 fputs(STACK_NXT.m_current, stdout);
267 fputs(" */ \n", stdout);
268#endif
269 STACK_NXT.m_inp = fopen(STACK_NXT.m_current, "r");
270 }
271 if(!STACK_NXT.m_inp)
273 {
274#if DEBUG
275 puts("/** Failed but ignoring it. */");
276#endif
278 fputs("#include <", stdout);
279 fputs(inc_path_buff, stdout);
280 puts(">");
281#endif
282
283 goto KEYGET;
284 }
285#else
286 {
287#if DEBUG
288 puts("/** Failed. */");
289#endif
290 return -STATE_FOPEN_BAD;
291 }
292#endif
293
294 for(i = strlen(STACK_NXT.m_current); i--;) {
295 if(STACK_NXT.m_current[i] == '/') {
296 Ch = STACK_NXT.m_current[i + 1];
297 STACK_NXT.m_current[i + 1] = 0;
298 strcpy(STACK_NXT.m_dir, STACK_NXT.m_current);
299 STACK_NXT.m_current[i + 1] = Ch;
300
301#if DEBUG
302 printf("/** Current %s */\n", STACK_NXT.m_current);
303 printf("/** Dir %s */\n", STACK_NXT.m_dir);
304#endif
305 break;
306 }
307 }
308
309
311#if DEBUG
312 puts("/** Found something. Now let's validate. */");
313#endif
314
315 for(STACK_IDX_BUFF = STACK_IDX; STACK_IDX_BUFF--;) {
316 if(!strcmp(STACK[STACK_IDX_BUFF].m_current, STACK_NXT.m_current)) {
317#if DEBUG
318 puts("/** It's repeating himself for some reason. */");
319
320#endif
321 goto KEYGET;
322 }
323 }
324
325#if DEBUG
326 puts("/** It's not repeating */");
327#endif
328#endif
329
330
331#if DEBUG
332 fputs(
333 "/******************** */\n"
334 "/* Now the path will be: */ \n /*"
335 , stdout
336 );
337
338 fputs(STACK_NXT.m_current, stdout);
339
340 puts(" */");
341#endif
342 ++STACK_IDX;
343 goto KEYGET;
344
345STACK_ADD:
346 if(STACK_IDX) {
347 fclose(STACK_LAST.m_inp);
348 --STACK_IDX;
349
350#if DEBUG
351 puts("/** One eof gracefully */");
352#endif
353
354 goto KEYGET;
355 }
356
357#if DEBUG
358 puts("/** All done. */");
359#endif
360 puts("");
361
362 return -STATE_GOOD;
363}
#define INC_IGNORE_NFOUND
Definition main.auto.h:6
#define PATHLEN
Definition main.auto.h:4
#define INC_REPT_CHECK
Definition main.auto.h:8
#define NDEBUG
Definition main.auto.h:10
#define DIRLEN
Definition main.auto.h:3
#define INC_IGNORE_SMASH
Definition main.auto.h:7
#define INC_LEAVE_NFOUND
Definition main.auto.h:9
#define STACKLEN
Definition main.auto.h:5
#define DEBUG
Definition main.c:4
#define STACK_NXT
Definition main.c:32
#define STACK_LAST
Definition main.c:31
Definition main.c:41
flag_t m_Key
Definition main.c:43
Definition main.c:23