ae2f_docs
main.c
Go to the documentation of this file.
1#include "./main.auto.h"
2
3#if defined(NDEBUG) && 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
22
23struct STACK_T {
24 FILE* m_inp;
25 char m_dir[DIRLEN + 1];
27} STACK[STACKLEN + 1];
28size_t STACK_IDX = 0;
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 */
47} FLAGS;
48
51
52int main(int argc, char** argv) {
53 assert(argc > 0);
54 if(argc < 1) {
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#if DEBUG
157 fputs("/*", stdout);
158#endif
159
160 while((Ch = fgetc(STACK_LAST.m_inp)) != EOF) {
161 if(Ch == '*') {
162 if((Ch = fgetc(STACK_LAST.m_inp)) == '/') {
163#if DEBUG
164 fputs("*/", stdout);
165#endif
166 goto KEYGET;
167 }
168#if DEBUG
169 fputc('*', stdout);
170 fputc(Ch, stdout);
171#endif
172 }
173#if DEBUG
174 fputc(Ch, stdout);
175#endif
176 }
177
178 goto KEYGET;
179#endif
180 } return -STATE_UNEXPECTED;
181
182 default:
183 {
184 fputc(Ch, stdout);
185 goto KEYGET;
186 }
187 }
188 }
189
190 goto STACK_ADD;
191STACK_SUB:
192 while((Ch = fgetc(STACK_LAST.m_inp)) != EOF && (Ch == ' ' || Ch == '\t'));
193 switch(Ch) {
194 case EOF:
195#if DEBUG
196 puts("/** Unknown EOF bug has appeared. */ \\");
197#endif
198 return -STATE_EOF;
199 case '\"': path_closing = '\"'; break;
200 case '<': path_closing = '>'; break;
201 default:
202 fputc(Ch, stdout);
203 goto KEYGET;
204 }
205
206#if DEBUG
207 puts("/** include detected */ \\");
208#endif
209
211 FLAGS.m_path_nclosed = 1;
212#if DEBUG
213 puts("/** Path cursor */ \\");
214#endif
215 if(STACK_IDX == STACKLEN) {
216#if DEBUG
217 puts("/** Stack has smashed. shame. */ \\");
218 fputs("/*", stdout);
219
220 while((Ch = fgetc(STACK_LAST.m_inp)) != EOF && Ch != '\n' && Ch != '\r') {
221#if DEBUG
222 fputc(Ch, stdout);
223#endif
224 }
225
226 puts("*/ \\");
227#endif
228
230
231
232 goto KEYGET;
233#endif
234 return -STATE_STACK_SMASHED;
235 }
236
238 if((Ch = fgetc(STACK_LAST.m_inp)) == EOF)
239 return -STATE_EOF;
240 if(Ch == path_closing) {
241 *path_cursor = 0;
242 FLAGS.m_path_nclosed = 0;
243 break;
244 }
245 *path_cursor = Ch;
246 path_cursor++;
247 }
248 if(FLAGS.m_path_nclosed) return -STATE_OPENING_UNKNOWN;
249
252
253#if DEBUG
254 fputs("/* Opening: ", stdout);
255 fputs(STACK_NXT.m_current, stdout);
256 fputs(" */ \\\n", stdout);
257#endif
259
260#if DEBUG
261 printf("/** first fp: %p */ \\\n", STACK_NXT.m_inp);
262#endif
263
264
265 for(i = argc; !STACK_NXT.m_inp && i-- != 1;) {
266 strcpy( STACK_NXT.m_current, argv[i]);
268
269#if DEBUG
270 fputs("/* Opening: ", stdout);
271 fputs(STACK_NXT.m_current, stdout);
272 fputs(" */ \\\n", stdout);
273#endif
275 }
276 if(!STACK_NXT.m_inp)
278 {
279#if DEBUG
280 puts("/** Failed but ignoring it. */ \\");
281#endif
283 fputs("#include <", stdout);
284 fputs(inc_path_buff, stdout);
285 puts(">");
286#endif
287
288 goto KEYGET;
289 }
290#else
291 {
292#if DEBUG
293 puts("/** Failed. */ \\");
294#endif
295 return -STATE_FOPEN_BAD;
296 }
297#endif
298
299 for(i = strlen(STACK_NXT.m_current); i--;) {
300 if(STACK_NXT.m_current[i] == '/') {
301 Ch = STACK_NXT.m_current[i + 1];
302 STACK_NXT.m_current[i + 1] = 0;
304 STACK_NXT.m_current[i + 1] = Ch;
305
306#if DEBUG
307 printf("/** Current %s */ \\\n", STACK_NXT.m_current);
308 printf("/** Dir %s */ \\\n", STACK_NXT.m_dir);
309#endif
310 break;
311 }
312 }
313
314
316#if DEBUG
317 puts("/** Found something. Now let's validate. */ \\");
318#endif
319
321 if(!strcmp(STACK[STACK_IDX_BUFF].m_current, STACK_NXT.m_current)) {
322#if DEBUG
323 puts("/** It's repeating himself for some reason. */ \\");
324 goto KEYGET;
325#endif
326 }
327 }
328
329#if DEBUG
330 puts("/** It's not repeating */ \\");
331#endif
332#endif
333
334
335#if DEBUG
336 fputs(
337 "/******************** */\\\n"
338 "/* Now the path will be: */ \\\n /*"
339 , stdout
340 );
341
342 fputs(STACK_NXT.m_current, stdout);
343
344 puts(" */ \\");
345#endif
346 ++STACK_IDX;
347 goto KEYGET;
348
349STACK_ADD:
350 if(STACK_IDX) {
351 fclose(STACK_LAST.m_inp);
352 --STACK_IDX;
353
354#if DEBUG
355 puts("/** One eof gracefully */ \\");
356#endif
357
358 goto KEYGET;
359 }
360
361#if DEBUG
362 puts("/** All done. */");
363#endif
364
365 return -STATE_GOOD;
366}
#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 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
int Ch
Definition main.c:6
char inc_key[sizeof("#include")]
Definition main.c:37
unsigned char flag_t
Definition main.c:40
size_t STACK_IDX_BUFF
Definition main.c:29
STATE_t
Definition main.c:13
@ STATE_UNEXPECTED
Definition main.c:20
@ STATE_ARGC_UNEXPECTED
Definition main.c:18
@ STATE_OPENING_UNKNOWN
Definition main.c:16
@ STATE_GOOD
Definition main.c:14
@ STATE_EOF
Definition main.c:15
@ STATE_STACK_SMASHED
Definition main.c:17
@ STATE_FOPEN_BAD
Definition main.c:19
size_t STACK_IDX
Definition main.c:28
char path_closing
Definition main.c:49
char inc_path_buff[PATHLEN+1]
Definition main.c:38
char * path_cursor
Definition main.c:50
#define DEBUG
Definition main.c:6
#define STACK_NXT
Definition main.c:32
#define STACK_LAST
Definition main.c:31
Definition main.c:41
flag_t m_path_nclosed
Definition main.c:44
flag_t m_Key_Idx
Definition main.c:46
flag_t m_ManyCmtOutMaybe
Definition main.c:45
flag_t m_Key
Definition main.c:43
Definition main.c:23
char m_dir[DIRLEN+1]
Definition main.c:25
char m_current[DIRLEN+PATHLEN+1]
Definition main.c:26
FILE * m_inp
Definition main.c:24