37f6c285db7f8ff189f504190a46623bbe197330
[reactos.git] / reactos / tools / wpp / ppl.l
1 /*
2 * Wrc preprocessor lexical analysis
3 *
4 * Copyright 1999-2000 Bertho A. Stultiens (BS)
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 * History:
21 * 24-Apr-2000 BS - Started from scratch to restructure everything
22 * and reintegrate the source into the wine-tree.
23 * 04-Jan-2000 BS - Added comments about the lexicographical
24 * grammar to give some insight in the complexity.
25 * 28-Dec-1999 BS - Eliminated backing-up of the flexer by running
26 * `flex -b' on the source. This results in some
27 * weirdo extra rules, but a much faster scanner.
28 * 23-Dec-1999 BS - Started this file
29 *
30 *-------------------------------------------------------------------------
31 * The preprocessor's lexographical grammar (approximately):
32 *
33 * pp := {ws} # {ws} if {ws} {expr} {ws} \n
34 * | {ws} # {ws} ifdef {ws} {id} {ws} \n
35 * | {ws} # {ws} ifndef {ws} {id} {ws} \n
36 * | {ws} # {ws} elif {ws} {expr} {ws} \n
37 * | {ws} # {ws} else {ws} \n
38 * | {ws} # {ws} endif {ws} \n
39 * | {ws} # {ws} include {ws} < {anytext} > \n
40 * | {ws} # {ws} include {ws} " {anytext} " \n
41 * | {ws} # {ws} include_next {ws} < {anytext} > \n
42 * | {ws} # {ws} include_next {ws} " {anytext} " \n
43 * | {ws} # {ws} define {ws} {anytext} \n
44 * | {ws} # {ws} define( {arglist} ) {ws} {expansion} \n
45 * | {ws} # {ws} pragma {ws} {anytext} \n
46 * | {ws} # {ws} ident {ws} {anytext} \n
47 * | {ws} # {ws} error {ws} {anytext} \n
48 * | {ws} # {ws} warning {ws} {anytext} \n
49 * | {ws} # {ws} line {ws} " {anytext} " {number} \n
50 * | {ws} # {ws} {number} " {anytext} " {number} [ {number} [{number}] ] \n
51 * | {ws} # {ws} \n
52 *
53 * ws := [ \t\r\f\v]*
54 *
55 * expr := {expr} [+-*%^/|&] {expr}
56 * | {expr} {logor|logand} {expr}
57 * | [!~+-] {expr}
58 * | {expr} ? {expr} : {expr}
59 *
60 * logor := ||
61 *
62 * logand := &&
63 *
64 * id := [a-zA-Z_][a-zA-Z0-9_]*
65 *
66 * anytext := [^\n]* (see note)
67 *
68 * arglist :=
69 * | {id}
70 * | {arglist} , {id}
71 * | {arglist} , {id} ...
72 *
73 * expansion := {id}
74 * | # {id}
75 * | {anytext}
76 * | {anytext} ## {anytext}
77 *
78 * number := [0-9]+
79 *
80 * Note: "anytext" is not always "[^\n]*". This is because the
81 * trailing context must be considered as well.
82 *
83 * The only certain assumption for the preprocessor to make is that
84 * directives start at the beginning of the line, followed by a '#'
85 * and end with a newline.
86 * Any directive may be suffixed with a line-continuation. Also
87 * classical comment / *...* / (note: no comments within comments,
88 * therefore spaces) is considered to be a line-continuation
89 * (according to gcc and egcs AFAIK, ANSI is a bit vague).
90 * Comments have not been added to the above grammar for simplicity
91 * reasons. However, it is allowed to enter comment anywhere within
92 * the directives as long as they do not interfere with the context.
93 * All comments are considered to be deletable whitespace (both
94 * classical form "/ *...* /" and C++ form "//...\n").
95 *
96 * All recursive scans, except for macro-expansion, are done by the
97 * parser, whereas the simple state transitions of non-recursive
98 * directives are done in the scanner. This results in the many
99 * exclusive start-conditions of the scanner.
100 *
101 * Macro expansions are slightly more difficult because they have to
102 * prescan the arguments. Parameter substitution is literal if the
103 * substitution is # or ## (either side). This enables new identifiers
104 * to be created (see 'info cpp' node Macro|Pitfalls|Prescan for more
105 * information).
106 *
107 * FIXME: Variable macro parameters is recognized, but not yet
108 * expanded. I have to reread the ANSI standard on the subject (yes,
109 * ANSI defines it).
110 *
111 * The following special defines are supported:
112 * __FILE__ -> "thissource.c"
113 * __LINE__ -> 123
114 * __DATE__ -> "May 1 2000"
115 * __TIME__ -> "23:59:59"
116 * These macros expand, as expected, into their ANSI defined values.
117 *
118 * The same include prevention is implemented as gcc and egcs does.
119 * This results in faster processing because we do not read the text
120 * at all. Some wine-sources attempt to include the same file 4 or 5
121 * times. This strategy also saves a lot blank output-lines, which in
122 * its turn improves the real resource scanner/parser.
123 *
124 */
125
126 /*
127 * Special flex options and exclusive scanner start-conditions
128 */
129 %option stack
130 %option never-interactive
131
132 %x pp_pp
133 %x pp_eol
134 %x pp_inc
135 %x pp_dqs
136 %x pp_sqs
137 %x pp_iqs
138 %x pp_comment
139 %x pp_def
140 %x pp_define
141 %x pp_macro
142 %x pp_mbody
143 %x pp_macign
144 %x pp_macscan
145 %x pp_macexp
146 %x pp_if
147 %x pp_ifd
148 %x pp_endif
149 %x pp_line
150 %x pp_defined
151 %x pp_ignore
152 %x RCINCL
153
154 ws [ \v\f\t\r]
155 cident [a-zA-Z_][0-9a-zA-Z_]*
156 ul [uUlL]|[uUlL][lL]|[lL][uU]|[lL][lL][uU]|[uU][lL][lL]|[lL][uU][lL]
157
158 %{
159 #include <stdio.h>
160 #include <stdlib.h>
161 #include <string.h>
162 #include <ctype.h>
163 #include <assert.h>
164
165 #include "wpp_private.h"
166 #include "ppy.tab.h"
167
168 /*
169 * Make sure that we are running an appropriate version of flex.
170 */
171 #if !defined(YY_FLEX_MAJOR_VERSION) || (1000 * YY_FLEX_MAJOR_VERSION + YY_FLEX_MINOR_VERSION < 2005)
172 #error Must use flex version 2.5.1 or higher (yy_scan_* routines are required).
173 #endif
174
175 #define YY_USE_PROTOS
176 #define YY_NO_UNPUT
177 #define YY_READ_BUF_SIZE 65536 /* So we read most of a file at once */
178
179 #define yy_current_state() YY_START
180 #define yy_pp_state(x) yy_pop_state(); yy_push_state(x)
181
182 /*
183 * Always update the current character position within a line
184 */
185 #define YY_USER_ACTION pp_status.char_number+=ppleng;
186
187 /*
188 * Buffer management for includes and expansions
189 */
190 #define MAXBUFFERSTACK 128 /* Nesting more than 128 includes or macro expansion textss is insane */
191
192 typedef struct bufferstackentry {
193 YY_BUFFER_STATE bufferstate; /* Buffer to switch back to */
194 pp_entry_t *define; /* Points to expanding define or NULL if handling includes */
195 int line_number; /* Line that we were handling */
196 int char_number; /* The current position on that line */
197 const char *filename; /* Filename that we were handling */
198 int if_depth; /* How many #if:s deep to check matching #endif:s */
199 int ncontinuations; /* Remember the continuation state */
200 int should_pop; /* Set if we must pop the start-state on EOF */
201 /* Include management */
202 include_state_t incl;
203 char *include_filename;
204 int pass_data;
205 } bufferstackentry_t;
206
207 #define ALLOCBLOCKSIZE (1 << 10) /* Allocate these chunks at a time for string-buffers */
208
209 /*
210 * Macro expansion nesting
211 * We need the stack to handle expansions while scanning
212 * a macro's arguments. The TOS must always be the macro
213 * that receives the current expansion from the scanner.
214 */
215 #define MAXMACEXPSTACK 128 /* Nesting more than 128 macro expansions is insane */
216
217 typedef struct macexpstackentry {
218 pp_entry_t *ppp; /* This macro we are scanning */
219 char **args; /* With these arguments */
220 char **ppargs; /* Resulting in these preprocessed arguments */
221 int *nnls; /* Number of newlines per argument */
222 int nargs; /* And this many arguments scanned */
223 int parentheses; /* Nesting level of () */
224 int curargsize; /* Current scanning argument's size */
225 int curargalloc; /* Current scanning argument's block allocated */
226 char *curarg; /* Current scanning argument's content */
227 } macexpstackentry_t;
228
229 #define MACROPARENTHESES() (top_macro()->parentheses)
230
231 /*
232 * Prototypes
233 */
234 static void newline(int);
235 static int make_number(int radix, YYSTYPE *val, const char *str, int len);
236 static void put_buffer(const char *s, int len);
237 static int is_c_h_include(char *fname, int quoted);
238 /* Buffer management */
239 static void push_buffer(pp_entry_t *ppp, char *filename, char *incname, int pop);
240 static bufferstackentry_t *pop_buffer(void);
241 /* String functions */
242 static void new_string(void);
243 static void add_string(const char *str, int len);
244 static char *get_string(void);
245 static void put_string(void);
246 static int string_start(void);
247 /* Macro functions */
248 static void push_macro(pp_entry_t *ppp);
249 static macexpstackentry_t *top_macro(void);
250 static macexpstackentry_t *pop_macro(void);
251 static void free_macro(macexpstackentry_t *mep);
252 static void add_text_to_macro(const char *text, int len);
253 static void macro_add_arg(int last);
254 static void macro_add_expansion(void);
255 /* Expansion */
256 static void expand_special(pp_entry_t *ppp);
257 static void expand_define(pp_entry_t *ppp);
258 static void expand_macro(macexpstackentry_t *mep);
259
260 /*
261 * Local variables
262 */
263 static int ncontinuations;
264
265 static int strbuf_idx = 0;
266 static int strbuf_alloc = 0;
267 static char *strbuffer = NULL;
268 static int str_startline;
269
270 static macexpstackentry_t *macexpstack[MAXMACEXPSTACK];
271 static int macexpstackidx = 0;
272
273 static bufferstackentry_t bufferstack[MAXBUFFERSTACK];
274 static int bufferstackidx = 0;
275
276 static int pass_data=1;
277
278 /*
279 * Global variables
280 */
281 include_state_t pp_incl_state =
282 {
283 -1, /* state */
284 NULL, /* ppp */
285 0, /* ifdepth */
286 0 /* seen_junk */
287 };
288
289 includelogicentry_t *pp_includelogiclist = NULL;
290
291 %}
292
293 /*
294 **************************************************************************
295 * The scanner starts here
296 **************************************************************************
297 */
298
299 %%
300 /*
301 * Catch line-continuations.
302 * Note: Gcc keeps the line-continuations in, for example, strings
303 * intact. However, I prefer to remove them all so that the next
304 * scanner will not need to reduce the continuation state.
305 *
306 * <*>\\\n newline(0);
307 */
308
309 /*
310 * Detect the leading # of a preprocessor directive.
311 */
312 <INITIAL,pp_ignore>^{ws}*# pp_incl_state.seen_junk++; yy_push_state(pp_pp);
313
314 /*
315 * Scan for the preprocessor directives
316 */
317 <pp_pp>{ws}*include{ws}* if(yy_top_state() != pp_ignore) {yy_pp_state(pp_inc); return tINCLUDE;} else {yy_pp_state(pp_eol);}
318 <pp_pp>{ws}*include_next{ws}* if(yy_top_state() != pp_ignore) {yy_pp_state(pp_inc); return tINCLUDE_NEXT;} else {yy_pp_state(pp_eol);}
319 <pp_pp>{ws}*define{ws}* yy_pp_state(yy_current_state() != pp_ignore ? pp_def : pp_eol);
320 <pp_pp>{ws}*error{ws}* yy_pp_state(pp_eol); if(yy_top_state() != pp_ignore) return tERROR;
321 <pp_pp>{ws}*warning{ws}* yy_pp_state(pp_eol); if(yy_top_state() != pp_ignore) return tWARNING;
322 <pp_pp>{ws}*pragma{ws}* yy_pp_state(pp_eol); if(yy_top_state() != pp_ignore) return tPRAGMA;
323 <pp_pp>{ws}*ident{ws}* yy_pp_state(pp_eol); if(yy_top_state() != pp_ignore) return tPPIDENT;
324 <pp_pp>{ws}*undef{ws}* if(yy_top_state() != pp_ignore) {yy_pp_state(pp_ifd); return tUNDEF;} else {yy_pp_state(pp_eol);}
325 <pp_pp>{ws}*ifdef{ws}* yy_pp_state(pp_ifd); return tIFDEF;
326 <pp_pp>{ws}*ifndef{ws}* pp_incl_state.seen_junk--; yy_pp_state(pp_ifd); return tIFNDEF;
327 <pp_pp>{ws}*if{ws}* yy_pp_state(pp_if); return tIF;
328 <pp_pp>{ws}*elif{ws}* yy_pp_state(pp_if); return tELIF;
329 <pp_pp>{ws}*else{ws}* yy_pp_state(pp_endif); return tELSE;
330 <pp_pp>{ws}*endif{ws}* yy_pp_state(pp_endif); return tENDIF;
331 <pp_pp>{ws}*line{ws}* if(yy_top_state() != pp_ignore) {yy_pp_state(pp_line); return tLINE;} else {yy_pp_state(pp_eol);}
332 <pp_pp>{ws}+ if(yy_top_state() != pp_ignore) {yy_pp_state(pp_line); return tGCCLINE;} else {yy_pp_state(pp_eol);}
333 <pp_pp>{ws}*[a-z]+ pperror("Invalid preprocessor token '%s'", pptext);
334 <pp_pp>\r?\n newline(1); yy_pop_state(); return tNL; /* This could be the null-token */
335 <pp_pp>\\\r?\n newline(0);
336 <pp_pp>\\\r? pperror("Preprocessor junk '%s'", pptext);
337 <pp_pp>. return *pptext;
338
339 /*
340 * Handle #include and #line
341 */
342 <pp_line>[0-9]+ return make_number(10, &pplval, pptext, ppleng);
343 <pp_inc>\< new_string(); add_string(pptext, ppleng); yy_push_state(pp_iqs);
344 <pp_inc,pp_line>\" new_string(); add_string(pptext, ppleng); yy_push_state(pp_dqs);
345 <pp_inc,pp_line>{ws}+ ;
346 <pp_inc,pp_line>\n newline(1); yy_pop_state(); return tNL;
347 <pp_inc,pp_line>\\\r?\n newline(0);
348 <pp_inc,pp_line>(\\\r?)|(.) pperror(yy_current_state() == pp_inc ? "Trailing junk in #include" : "Trailing junk in #line");
349
350 /*
351 * Ignore all input when a false clause is parsed
352 */
353 <pp_ignore>[^#/\\\n]+ ;
354 <pp_ignore>\n newline(1);
355 <pp_ignore>\\\r?\n newline(0);
356 <pp_ignore>(\\\r?)|(.) ;
357
358 /*
359 * Handle #if and #elif.
360 * These require conditionals to be evaluated, but we do not
361 * want to jam the scanner normally when we see these tokens.
362 * Note: tIDENT is handled below.
363 */
364
365 <pp_if>0[0-7]*{ul}? return make_number(8, &pplval, pptext, ppleng);
366 <pp_if>0[0-7]*[8-9]+{ul}? pperror("Invalid octal digit");
367 <pp_if>[1-9][0-9]*{ul}? return make_number(10, &pplval, pptext, ppleng);
368 <pp_if>0[xX][0-9a-fA-F]+{ul}? return make_number(16, &pplval, pptext, ppleng);
369 <pp_if>0[xX] pperror("Invalid hex number");
370 <pp_if>defined yy_push_state(pp_defined); return tDEFINED;
371 <pp_if>"<<" return tLSHIFT;
372 <pp_if>">>" return tRSHIFT;
373 <pp_if>"&&" return tLOGAND;
374 <pp_if>"||" return tLOGOR;
375 <pp_if>"==" return tEQ;
376 <pp_if>"!=" return tNE;
377 <pp_if>"<=" return tLTE;
378 <pp_if>">=" return tGTE;
379 <pp_if>\n newline(1); yy_pop_state(); return tNL;
380 <pp_if>\\\r?\n newline(0);
381 <pp_if>\\\r? pperror("Junk in conditional expression");
382 <pp_if>{ws}+ ;
383 <pp_if>\' new_string(); add_string(pptext, ppleng); yy_push_state(pp_sqs);
384 <pp_if>\" pperror("String constants not allowed in conditionals");
385 <pp_if>. return *pptext;
386
387 /*
388 * Handle #ifdef, #ifndef and #undef
389 * to get only an untranslated/unexpanded identifier
390 */
391 <pp_ifd>{cident} pplval.cptr = pp_xstrdup(pptext); return tIDENT;
392 <pp_ifd>{ws}+ ;
393 <pp_ifd>\n newline(1); yy_pop_state(); return tNL;
394 <pp_ifd>\\\r?\n newline(0);
395 <pp_ifd>(\\\r?)|(.) pperror("Identifier expected");
396
397 /*
398 * Handle #else and #endif.
399 */
400 <pp_endif>{ws}+ ;
401 <pp_endif>\n newline(1); yy_pop_state(); return tNL;
402 <pp_endif>\\\r?\n newline(0);
403 <pp_endif>. pperror("Garbage after #else or #endif.");
404
405 /*
406 * Handle the special 'defined' keyword.
407 * This is necessary to get the identifier prior to any
408 * substitutions.
409 */
410 <pp_defined>{cident} yy_pop_state(); pplval.cptr = pp_xstrdup(pptext); return tIDENT;
411 <pp_defined>{ws}+ ;
412 <pp_defined>(\()|(\)) return *pptext;
413 <pp_defined>\\\r?\n newline(0);
414 <pp_defined>(\\.)|(\n)|(.) pperror("Identifier expected");
415
416 /*
417 * Handle #error, #warning, #pragma and #ident.
418 * Pass everything literally to the parser, which
419 * will act appropriately.
420 * Comments are stripped from the literal text.
421 */
422 <pp_eol>[^/\\\n]+ if(yy_top_state() != pp_ignore) { pplval.cptr = pp_xstrdup(pptext); return tLITERAL; }
423 <pp_eol>\/[^/\\\n*]* if(yy_top_state() != pp_ignore) { pplval.cptr = pp_xstrdup(pptext); return tLITERAL; }
424 <pp_eol>(\\\r?)|(\/[^/*]) if(yy_top_state() != pp_ignore) { pplval.cptr = pp_xstrdup(pptext); return tLITERAL; }
425 <pp_eol>\n newline(1); yy_pop_state(); if(yy_current_state() != pp_ignore) { return tNL; }
426 <pp_eol>\\\r?\n newline(0);
427
428 /*
429 * Handle left side of #define
430 */
431 <pp_def>{cident}\( pplval.cptr = pp_xstrdup(pptext); pplval.cptr[ppleng-1] = '\0'; yy_pp_state(pp_macro); return tMACRO;
432 <pp_def>{cident} pplval.cptr = pp_xstrdup(pptext); yy_pp_state(pp_define); return tDEFINE;
433 <pp_def>{ws}+ ;
434 <pp_def>\\\r?\n newline(0);
435 <pp_def>(\\\r?)|(\n)|(.) perror("Identifier expected");
436
437 /*
438 * Scan the substitution of a define
439 */
440 <pp_define>[^'"/\\\n]+ pplval.cptr = pp_xstrdup(pptext); return tLITERAL;
441 <pp_define>(\\\r?)|(\/[^/*]) pplval.cptr = pp_xstrdup(pptext); return tLITERAL;
442 <pp_define>\\\r?\n{ws}+ newline(0); pplval.cptr = pp_xstrdup(" "); return tLITERAL;
443 <pp_define>\\\r?\n newline(0);
444 <pp_define>\n newline(1); yy_pop_state(); return tNL;
445 <pp_define>\' new_string(); add_string(pptext, ppleng); yy_push_state(pp_sqs);
446 <pp_define>\" new_string(); add_string(pptext, ppleng); yy_push_state(pp_dqs);
447
448 /*
449 * Scan the definition macro arguments
450 */
451 <pp_macro>\){ws}* yy_pp_state(pp_mbody); return tMACROEND;
452 <pp_macro>{ws}+ ;
453 <pp_macro>{cident} pplval.cptr = pp_xstrdup(pptext); return tIDENT;
454 <pp_macro>, return ',';
455 <pp_macro>"..." return tELIPSIS;
456 <pp_macro>(\\\r?)|(\n)|(.)|(\.\.?) pperror("Argument identifier expected");
457 <pp_macro>\\\r?\n newline(0);
458
459 /*
460 * Scan the substitution of a macro
461 */
462 <pp_mbody>[^a-zA-Z0-9'"#/\\\n]+ pplval.cptr = pp_xstrdup(pptext); return tLITERAL;
463 <pp_mbody>{cident} pplval.cptr = pp_xstrdup(pptext); return tIDENT;
464 <pp_mbody>\#\# return tCONCAT;
465 <pp_mbody>\# return tSTRINGIZE;
466 <pp_mbody>[0-9][^'"#/\\\n]* pplval.cptr = pp_xstrdup(pptext); return tLITERAL;
467 <pp_mbody>(\\\r?)|(\/[^/*'"#\\\n]*) pplval.cptr = pp_xstrdup(pptext); return tLITERAL;
468 <pp_mbody>\\\r?\n{ws}+ newline(0); pplval.cptr = pp_xstrdup(" "); return tLITERAL;
469 <pp_mbody>\\\r?\n newline(0);
470 <pp_mbody>\n newline(1); yy_pop_state(); return tNL;
471 <pp_mbody>\' new_string(); add_string(pptext, ppleng); yy_push_state(pp_sqs);
472 <pp_mbody>\" new_string(); add_string(pptext, ppleng); yy_push_state(pp_dqs);
473
474 /*
475 * Macro expansion text scanning.
476 * This state is active just after the identifier is scanned
477 * that triggers an expansion. We *must* delete the leading
478 * whitespace before we can start scanning for arguments.
479 *
480 * If we do not see a '(' as next trailing token, then we have
481 * a false alarm. We just continue with a nose-bleed...
482 */
483 <pp_macign>{ws}*/\( yy_pp_state(pp_macscan);
484 <pp_macign>{ws}*\n {
485 if(yy_top_state() != pp_macscan)
486 newline(0);
487 }
488 <pp_macign>{ws}*\\\r?\n newline(0);
489 <pp_macign>{ws}+|{ws}*\\\r?|. {
490 macexpstackentry_t *mac = pop_macro();
491 yy_pop_state();
492 put_buffer(mac->ppp->ident, strlen(mac->ppp->ident));
493 put_buffer(pptext, ppleng);
494 free_macro(mac);
495 }
496
497 /*
498 * Macro expansion argument text scanning.
499 * This state is active when a macro's arguments are being read for expansion.
500 */
501 <pp_macscan>\( {
502 if(++MACROPARENTHESES() > 1)
503 add_text_to_macro(pptext, ppleng);
504 }
505 <pp_macscan>\) {
506 if(--MACROPARENTHESES() == 0)
507 {
508 yy_pop_state();
509 macro_add_arg(1);
510 }
511 else
512 add_text_to_macro(pptext, ppleng);
513 }
514 <pp_macscan>, {
515 if(MACROPARENTHESES() > 1)
516 add_text_to_macro(pptext, ppleng);
517 else
518 macro_add_arg(0);
519 }
520 <pp_macscan>\" new_string(); add_string(pptext, ppleng); yy_push_state(pp_dqs);
521 <pp_macscan>\' new_string(); add_string(pptext, ppleng); yy_push_state(pp_sqs);
522 <pp_macscan>"/*" yy_push_state(pp_comment); add_text_to_macro(" ", 1);
523 <pp_macscan>\n pp_status.line_number++; pp_status.char_number = 1; add_text_to_macro(pptext, ppleng);
524 <pp_macscan>([^/(),\\\n"']+)|(\/[^/*(),\\\n'"]*)|(\\\r?)|(.) add_text_to_macro(pptext, ppleng);
525 <pp_macscan>\\\r?\n newline(0);
526
527 /*
528 * Comment handling (almost all start-conditions)
529 */
530 <INITIAL,pp_pp,pp_ignore,pp_eol,pp_inc,pp_if,pp_ifd,pp_endif,pp_defined,pp_def,pp_define,pp_macro,pp_mbody,RCINCL>"/*" yy_push_state(pp_comment);
531 <pp_comment>[^*\n]*|"*"+[^*/\n]* ;
532 <pp_comment>\n newline(0);
533 <pp_comment>"*"+"/" yy_pop_state();
534
535 /*
536 * Remove C++ style comment (almost all start-conditions)
537 */
538 <INITIAL,pp_pp,pp_ignore,pp_eol,pp_inc,pp_if,pp_ifd,pp_endif,pp_defined,pp_def,pp_define,pp_macro,pp_mbody,pp_macscan,RCINCL>"//"[^\n]* {
539 if(pptext[ppleng-1] == '\\')
540 ppwarning("C++ style comment ends with an escaped newline (escape ignored)");
541 }
542
543 /*
544 * Single, double and <> quoted constants
545 */
546 <INITIAL,pp_macexp>\" pp_incl_state.seen_junk++; new_string(); add_string(pptext, ppleng); yy_push_state(pp_dqs);
547 <INITIAL,pp_macexp>\' pp_incl_state.seen_junk++; new_string(); add_string(pptext, ppleng); yy_push_state(pp_sqs);
548 <pp_dqs>[^"\\\n]+ add_string(pptext, ppleng);
549 <pp_dqs>\" {
550 add_string(pptext, ppleng);
551 yy_pop_state();
552 switch(yy_current_state())
553 {
554 case pp_pp:
555 case pp_define:
556 case pp_mbody:
557 case pp_inc:
558 case RCINCL:
559 if (yy_current_state()==RCINCL) yy_pop_state();
560 pplval.cptr = get_string();
561 return tDQSTRING;
562 case pp_line:
563 pplval.cptr = get_string();
564 if (is_c_h_include(pplval.cptr, 1)) pass_data=0;
565 else pass_data=1;
566 return tDQSTRING;
567 default:
568 put_string();
569 }
570 }
571 <pp_sqs>[^'\\\n]+ add_string(pptext, ppleng);
572 <pp_sqs>\' {
573 add_string(pptext, ppleng);
574 yy_pop_state();
575 switch(yy_current_state())
576 {
577 case pp_if:
578 case pp_define:
579 case pp_mbody:
580 pplval.cptr = get_string();
581 return tSQSTRING;
582 default:
583 put_string();
584 }
585 }
586 <pp_iqs>[^\>\\\n]+ add_string(pptext, ppleng);
587 <pp_iqs>\> {
588 add_string(pptext, ppleng);
589 yy_pop_state();
590 pplval.cptr = get_string();
591 return tIQSTRING;
592 }
593 <pp_dqs>\\\r?\n {
594 /*
595 * This is tricky; we need to remove the line-continuation
596 * from preprocessor strings, but OTOH retain them in all
597 * other strings. This is because the resource grammar is
598 * even more braindead than initially analysed and line-
599 * continuations in strings introduce, sigh, newlines in
600 * the output. There goes the concept of non-breaking, non-
601 * spacing whitespace.
602 */
603 switch(yy_top_state())
604 {
605 case pp_pp:
606 case pp_define:
607 case pp_mbody:
608 case pp_inc:
609 case pp_line:
610 newline(0);
611 break;
612 default:
613 add_string(pptext, ppleng);
614 newline(-1);
615 }
616 }
617 <pp_iqs,pp_dqs,pp_sqs>\\. add_string(pptext, ppleng);
618 <pp_iqs,pp_dqs,pp_sqs>\n {
619 newline(1);
620 add_string(pptext, ppleng);
621 ppwarning("Newline in string constant encounterd (started line %d)", string_start());
622 }
623
624 /*
625 * Identifier scanning
626 */
627 <INITIAL,pp_if,pp_inc,pp_macexp>{cident} {
628 pp_entry_t *ppp;
629 pp_incl_state.seen_junk++;
630 if(!(ppp = pplookup(pptext)))
631 {
632 if(yy_current_state() == pp_inc)
633 pperror("Expected include filename");
634
635 if(yy_current_state() == pp_if)
636 {
637 pplval.cptr = pp_xstrdup(pptext);
638 return tIDENT;
639 }
640 else {
641 if((yy_current_state()==INITIAL) && (strcasecmp(pptext,"RCINCLUDE")==0)){
642 yy_push_state(RCINCL);
643 return tRCINCLUDE;
644 }
645 else put_buffer(pptext, ppleng);
646 }
647 }
648 else if(!ppp->expanding)
649 {
650 switch(ppp->type)
651 {
652 case def_special:
653 expand_special(ppp);
654 break;
655 case def_define:
656 expand_define(ppp);
657 break;
658 case def_macro:
659 yy_push_state(pp_macign);
660 push_macro(ppp);
661 break;
662 default:
663 pp_internal_error(__FILE__, __LINE__, "Invalid define type %d\n", ppp->type);
664 }
665 }
666 }
667
668 /*
669 * Everything else that needs to be passed and
670 * newline and continuation handling
671 */
672 <INITIAL,pp_macexp>[^a-zA-Z_#'"/\\\n \r\t\f\v]+|(\/|\\)[^a-zA-Z_/*'"\\\n \r\t\v\f]* pp_incl_state.seen_junk++; put_buffer(pptext, ppleng);
673 <INITIAL,pp_macexp>{ws}+ put_buffer(pptext, ppleng);
674 <INITIAL>\n newline(1);
675 <INITIAL>\\\r?\n newline(0);
676 <INITIAL>\\\r? pp_incl_state.seen_junk++; put_buffer(pptext, ppleng);
677
678 /*
679 * Special catcher for macro argmument expansion to prevent
680 * newlines to propagate to the output or admin.
681 */
682 <pp_macexp>(\n)|(.)|(\\\r?(\n|.)) put_buffer(pptext, ppleng);
683
684 <RCINCL>[A-Za-z0-9_\.\\/]+ {
685 pplval.cptr=pp_xstrdup(pptext);
686 yy_pop_state();
687 return tRCINCLUDEPATH;
688 }
689
690 <RCINCL>{ws}+ ;
691
692 <RCINCL>\" {
693 new_string(); add_string(pptext,ppleng);yy_push_state(pp_dqs);
694 }
695
696 /*
697 * This is a 'catch-all' rule to discover errors in the scanner
698 * in an orderly manner.
699 */
700 <*>. pp_incl_state.seen_junk++; ppwarning("Unmatched text '%c' (0x%02x); please report\n", isprint(*pptext & 0xff) ? *pptext : ' ', *pptext);
701
702 <<EOF>> {
703 YY_BUFFER_STATE b = YY_CURRENT_BUFFER;
704 bufferstackentry_t *bep = pop_buffer();
705
706 if((!bep && pp_get_if_depth()) || (bep && pp_get_if_depth() != bep->if_depth))
707 ppwarning("Unmatched #if/#endif at end of file");
708
709 if(!bep)
710 {
711 if(YY_START != INITIAL)
712 pperror("Unexpected end of file during preprocessing");
713 yyterminate();
714 }
715 else if(bep->should_pop == 2)
716 {
717 macexpstackentry_t *mac;
718 mac = pop_macro();
719 expand_macro(mac);
720 }
721 pp_delete_buffer(b);
722 }
723
724 %%
725 /*
726 **************************************************************************
727 * Support functions
728 **************************************************************************
729 */
730
731 #ifndef ppwrap
732 int ppwrap(void)
733 {
734 return 1;
735 }
736 #endif
737
738
739 /*
740 *-------------------------------------------------------------------------
741 * Output newlines or set them as continuations
742 *
743 * Input: -1 - Don't count this one, but update local position (see pp_dqs)
744 * 0 - Line-continuation seen and cache output
745 * 1 - Newline seen and flush output
746 *-------------------------------------------------------------------------
747 */
748 static void newline(int dowrite)
749 {
750 pp_status.line_number++;
751 pp_status.char_number = 1;
752
753 if(dowrite == -1)
754 return;
755
756 ncontinuations++;
757 if(dowrite)
758 {
759 for(;ncontinuations; ncontinuations--)
760 put_buffer("\n", 1);
761 }
762 }
763
764
765 /*
766 *-------------------------------------------------------------------------
767 * Make a number out of an any-base and suffixed string
768 *
769 * Possible number extensions:
770 * - "" int
771 * - "L" long int
772 * - "LL" long long int
773 * - "U" unsigned int
774 * - "UL" unsigned long int
775 * - "ULL" unsigned long long int
776 * - "LU" unsigned long int
777 * - "LLU" unsigned long long int
778 * - "LUL" invalid
779 *
780 * FIXME:
781 * The sizes of resulting 'int' and 'long' are compiler specific.
782 * I depend on sizeof(int) > 2 here (although a relatively safe
783 * assumption).
784 * Long longs are not yet implemented because this is very compiler
785 * specific and I don't want to think too much about the problems.
786 *
787 *-------------------------------------------------------------------------
788 */
789 static int make_number(int radix, YYSTYPE *val, const char *str, int len)
790 {
791 int is_l = 0;
792 int is_ll = 0;
793 int is_u = 0;
794 char ext[4];
795
796 ext[3] = '\0';
797 ext[2] = toupper(str[len-1]);
798 ext[1] = len > 1 ? toupper(str[len-2]) : ' ';
799 ext[0] = len > 2 ? toupper(str[len-3]) : ' ';
800
801 if(!strcmp(ext, "LUL"))
802 pperror("Invalid constant suffix");
803 else if(!strcmp(ext, "LLU") || !strcmp(ext, "ULL"))
804 {
805 is_ll++;
806 is_u++;
807 }
808 else if(!strcmp(ext+1, "LU") || !strcmp(ext+1, "UL"))
809 {
810 is_l++;
811 is_u++;
812 }
813 else if(!strcmp(ext+1, "LL"))
814 {
815 is_ll++;
816 }
817 else if(!strcmp(ext+2, "L"))
818 {
819 is_l++;
820 }
821 else if(!strcmp(ext+2, "U"))
822 {
823 is_u++;
824 }
825
826 if(is_ll)
827 pp_internal_error(__FILE__, __LINE__, "long long constants not implemented yet");
828
829 if(is_u && is_l)
830 {
831 val->ulong = strtoul(str, NULL, radix);
832 return tULONG;
833 }
834 else if(!is_u && is_l)
835 {
836 val->slong = strtol(str, NULL, radix);
837 return tSLONG;
838 }
839 else if(is_u && !is_l)
840 {
841 val->uint = (unsigned int)strtoul(str, NULL, radix);
842 return tUINT;
843 }
844
845 /* Else it must be an int... */
846 val->sint = (int)strtol(str, NULL, radix);
847 return tSINT;
848 }
849
850
851 /*
852 *-------------------------------------------------------------------------
853 * Macro and define expansion support
854 *
855 * FIXME: Variable macro arguments.
856 *-------------------------------------------------------------------------
857 */
858 static void expand_special(pp_entry_t *ppp)
859 {
860 const char *dbgtext = "?";
861 static char *buf = NULL;
862
863 assert(ppp->type == def_special);
864
865 if(!strcmp(ppp->ident, "__LINE__"))
866 {
867 dbgtext = "def_special(__LINE__)";
868 buf = pp_xrealloc(buf, 32);
869 sprintf(buf, "%d", pp_status.line_number);
870 }
871 else if(!strcmp(ppp->ident, "__FILE__"))
872 {
873 dbgtext = "def_special(__FILE__)";
874 buf = pp_xrealloc(buf, strlen(pp_status.input) + 3);
875 sprintf(buf, "\"%s\"", pp_status.input);
876 }
877 else
878 pp_internal_error(__FILE__, __LINE__, "Special macro '%s' not found...\n", ppp->ident);
879
880 if(pp_flex_debug)
881 fprintf(stderr, "expand_special(%d): %s:%d: '%s' -> '%s'\n",
882 macexpstackidx,
883 pp_status.input,
884 pp_status.line_number,
885 ppp->ident,
886 buf ? buf : "");
887
888 if(buf && buf[0])
889 {
890 push_buffer(ppp, NULL, NULL, 0);
891 yy_scan_string(buf);
892 }
893 }
894
895 static void expand_define(pp_entry_t *ppp)
896 {
897 assert(ppp->type == def_define);
898
899 if(pp_flex_debug)
900 fprintf(stderr, "expand_define(%d): %s:%d: '%s' -> '%s'\n",
901 macexpstackidx,
902 pp_status.input,
903 pp_status.line_number,
904 ppp->ident,
905 ppp->subst.text);
906 if(ppp->subst.text && ppp->subst.text[0])
907 {
908 push_buffer(ppp, NULL, NULL, 0);
909 yy_scan_string(ppp->subst.text);
910 }
911 }
912
913 static int curdef_idx = 0;
914 static int curdef_alloc = 0;
915 static char *curdef_text = NULL;
916
917 static void add_text(const char *str, int len)
918 {
919 if(len == 0)
920 return;
921 if(curdef_idx >= curdef_alloc || curdef_alloc - curdef_idx < len)
922 {
923 curdef_alloc += (len + ALLOCBLOCKSIZE-1) & ~(ALLOCBLOCKSIZE-1);
924 curdef_text = pp_xrealloc(curdef_text, curdef_alloc * sizeof(curdef_text[0]));
925 if(curdef_alloc > 65536)
926 ppwarning("Reallocating macro-expansion buffer larger than 64kB");
927 }
928 memcpy(&curdef_text[curdef_idx], str, len);
929 curdef_idx += len;
930 }
931
932 static mtext_t *add_expand_text(mtext_t *mtp, macexpstackentry_t *mep, int *nnl)
933 {
934 char *cptr;
935 char *exp;
936 int tag;
937 int n;
938
939 if(mtp == NULL)
940 return NULL;
941
942 switch(mtp->type)
943 {
944 case exp_text:
945 if(pp_flex_debug)
946 fprintf(stderr, "add_expand_text: exp_text: '%s'\n", mtp->subst.text);
947 add_text(mtp->subst.text, strlen(mtp->subst.text));
948 break;
949
950 case exp_stringize:
951 if(pp_flex_debug)
952 fprintf(stderr, "add_expand_text: exp_stringize(%d): '%s'\n",
953 mtp->subst.argidx,
954 mep->args[mtp->subst.argidx]);
955 cptr = mep->args[mtp->subst.argidx];
956 add_text("\"", 1);
957 while(*cptr)
958 {
959 if(*cptr == '"' || *cptr == '\\')
960 add_text("\\", 1);
961 add_text(cptr, 1);
962 cptr++;
963 }
964 add_text("\"", 1);
965 break;
966
967 case exp_concat:
968 if(pp_flex_debug)
969 fprintf(stderr, "add_expand_text: exp_concat\n");
970 /* Remove trailing whitespace from current expansion text */
971 while(curdef_idx)
972 {
973 if(isspace(curdef_text[curdef_idx-1] & 0xff))
974 curdef_idx--;
975 else
976 break;
977 }
978 /* tag current position and recursively expand the next part */
979 tag = curdef_idx;
980 mtp = add_expand_text(mtp->next, mep, nnl);
981
982 /* Now get rid of the leading space of the expansion */
983 cptr = &curdef_text[tag];
984 n = curdef_idx - tag;
985 while(n)
986 {
987 if(isspace(*cptr & 0xff))
988 {
989 cptr++;
990 n--;
991 }
992 else
993 break;
994 }
995 if(cptr != &curdef_text[tag])
996 {
997 memmove(&curdef_text[tag], cptr, n);
998 curdef_idx -= (curdef_idx - tag) - n;
999 }
1000 break;
1001
1002 case exp_subst:
1003 if((mtp->next && mtp->next->type == exp_concat) || (mtp->prev && mtp->prev->type == exp_concat))
1004 exp = mep->args[mtp->subst.argidx];
1005 else
1006 exp = mep->ppargs[mtp->subst.argidx];
1007 if(exp)
1008 {
1009 add_text(exp, strlen(exp));
1010 *nnl -= mep->nnls[mtp->subst.argidx];
1011 cptr = strchr(exp, '\n');
1012 while(cptr)
1013 {
1014 *cptr = ' ';
1015 cptr = strchr(cptr+1, '\n');
1016 }
1017 mep->nnls[mtp->subst.argidx] = 0;
1018 }
1019 if(pp_flex_debug)
1020 fprintf(stderr, "add_expand_text: exp_subst(%d): '%s'\n", mtp->subst.argidx, exp);
1021 break;
1022
1023 default:
1024 pp_internal_error(__FILE__, __LINE__, "Invalid expansion type (%d) in macro expansion\n", mtp->type);
1025 }
1026 return mtp;
1027 }
1028
1029 static void expand_macro(macexpstackentry_t *mep)
1030 {
1031 mtext_t *mtp;
1032 int n, k;
1033 char *cptr;
1034 int nnl = 0;
1035 pp_entry_t *ppp = mep->ppp;
1036 int nargs = mep->nargs;
1037
1038 assert(ppp->type == def_macro);
1039 assert(ppp->expanding == 0);
1040
1041 if((ppp->nargs >= 0 && nargs != ppp->nargs) || (ppp->nargs < 0 && nargs < -ppp->nargs))
1042 pperror("Too %s macro arguments (%d)", nargs < abs(ppp->nargs) ? "few" : "many", nargs);
1043
1044 for(n = 0; n < nargs; n++)
1045 nnl += mep->nnls[n];
1046
1047 if(pp_flex_debug)
1048 fprintf(stderr, "expand_macro(%d): %s:%d: '%s'(%d,%d) -> ...\n",
1049 macexpstackidx,
1050 pp_status.input,
1051 pp_status.line_number,
1052 ppp->ident,
1053 mep->nargs,
1054 nnl);
1055
1056 curdef_idx = 0;
1057
1058 for(mtp = ppp->subst.mtext; mtp; mtp = mtp->next)
1059 {
1060 if(!(mtp = add_expand_text(mtp, mep, &nnl)))
1061 break;
1062 }
1063
1064 for(n = 0; n < nnl; n++)
1065 add_text("\n", 1);
1066
1067 /* To make sure there is room and termination (see below) */
1068 add_text(" \0", 2);
1069
1070 /* Strip trailing whitespace from expansion */
1071 for(k = curdef_idx, cptr = &curdef_text[curdef_idx-1]; k > 0; k--, cptr--)
1072 {
1073 if(!isspace(*cptr & 0xff))
1074 break;
1075 }
1076
1077 /*
1078 * We must add *one* whitespace to make sure that there
1079 * is a token-separation after the expansion.
1080 */
1081 *(++cptr) = ' ';
1082 *(++cptr) = '\0';
1083 k++;
1084
1085 /* Strip leading whitespace from expansion */
1086 for(n = 0, cptr = curdef_text; n < k; n++, cptr++)
1087 {
1088 if(!isspace(*cptr & 0xff))
1089 break;
1090 }
1091
1092 if(k - n > 0)
1093 {
1094 if(pp_flex_debug)
1095 fprintf(stderr, "expand_text: '%s'\n", curdef_text + n);
1096 push_buffer(ppp, NULL, NULL, 0);
1097 /*yy_scan_bytes(curdef_text + n, k - n);*/
1098 yy_scan_string(curdef_text + n);
1099 }
1100 }
1101
1102 /*
1103 *-------------------------------------------------------------------------
1104 * String collection routines
1105 *-------------------------------------------------------------------------
1106 */
1107 static void new_string(void)
1108 {
1109 #ifdef DEBUG
1110 if(strbuf_idx)
1111 ppwarning("new_string: strbuf_idx != 0");
1112 #endif
1113 strbuf_idx = 0;
1114 str_startline = pp_status.line_number;
1115 }
1116
1117 static void add_string(const char *str, int len)
1118 {
1119 if(len == 0)
1120 return;
1121 if(strbuf_idx >= strbuf_alloc || strbuf_alloc - strbuf_idx < len)
1122 {
1123 strbuf_alloc += (len + ALLOCBLOCKSIZE-1) & ~(ALLOCBLOCKSIZE-1);
1124 strbuffer = pp_xrealloc(strbuffer, strbuf_alloc * sizeof(strbuffer[0]));
1125 if(strbuf_alloc > 65536)
1126 ppwarning("Reallocating string buffer larger than 64kB");
1127 }
1128 memcpy(&strbuffer[strbuf_idx], str, len);
1129 strbuf_idx += len;
1130 }
1131
1132 static char *get_string(void)
1133 {
1134 char *str = pp_xmalloc(strbuf_idx + 1);
1135 memcpy(str, strbuffer, strbuf_idx);
1136 str[strbuf_idx] = '\0';
1137 #ifdef DEBUG
1138 strbuf_idx = 0;
1139 #endif
1140 return str;
1141 }
1142
1143 static void put_string(void)
1144 {
1145 put_buffer(strbuffer, strbuf_idx);
1146 #ifdef DEBUG
1147 strbuf_idx = 0;
1148 #endif
1149 }
1150
1151 static int string_start(void)
1152 {
1153 return str_startline;
1154 }
1155
1156
1157 /*
1158 *-------------------------------------------------------------------------
1159 * Buffer management
1160 *-------------------------------------------------------------------------
1161 */
1162 static void push_buffer(pp_entry_t *ppp, char *filename, char *incname, int pop)
1163 {
1164 if(ppdebug)
1165 printf("push_buffer(%d): %p %p %p %d\n", bufferstackidx, ppp, filename, incname, pop);
1166 if(bufferstackidx >= MAXBUFFERSTACK)
1167 pp_internal_error(__FILE__, __LINE__, "Buffer stack overflow");
1168
1169 memset(&bufferstack[bufferstackidx], 0, sizeof(bufferstack[0]));
1170 bufferstack[bufferstackidx].bufferstate = YY_CURRENT_BUFFER;
1171 bufferstack[bufferstackidx].define = ppp;
1172 bufferstack[bufferstackidx].line_number = pp_status.line_number;
1173 bufferstack[bufferstackidx].char_number = pp_status.char_number;
1174 bufferstack[bufferstackidx].if_depth = pp_get_if_depth();
1175 bufferstack[bufferstackidx].should_pop = pop;
1176 bufferstack[bufferstackidx].filename = pp_status.input;
1177 bufferstack[bufferstackidx].ncontinuations = ncontinuations;
1178 bufferstack[bufferstackidx].incl = pp_incl_state;
1179 bufferstack[bufferstackidx].include_filename = incname;
1180 bufferstack[bufferstackidx].pass_data = pass_data;
1181
1182 if(ppp)
1183 ppp->expanding = 1;
1184 else if(filename)
1185 {
1186 /* These will track the pperror to the correct file and line */
1187 pp_status.line_number = 1;
1188 pp_status.char_number = 1;
1189 pp_status.input = filename;
1190 ncontinuations = 0;
1191 }
1192 else if(!pop)
1193 pp_internal_error(__FILE__, __LINE__, "Pushing buffer without knowing where to go to");
1194 bufferstackidx++;
1195 }
1196
1197 static bufferstackentry_t *pop_buffer(void)
1198 {
1199 if(bufferstackidx < 0)
1200 pp_internal_error(__FILE__, __LINE__, "Bufferstack underflow?");
1201
1202 if(bufferstackidx == 0)
1203 return NULL;
1204
1205 bufferstackidx--;
1206
1207 if(bufferstack[bufferstackidx].define)
1208 bufferstack[bufferstackidx].define->expanding = 0;
1209 else
1210 {
1211 pp_status.line_number = bufferstack[bufferstackidx].line_number;
1212 pp_status.char_number = bufferstack[bufferstackidx].char_number;
1213 pp_status.input = bufferstack[bufferstackidx].filename;
1214 ncontinuations = bufferstack[bufferstackidx].ncontinuations;
1215 if(!bufferstack[bufferstackidx].should_pop)
1216 {
1217 fclose(ppin);
1218 fprintf(ppout, "# %d \"%s\" 2\n", pp_status.line_number, pp_status.input);
1219
1220 /* We have EOF, check the include logic */
1221 if(pp_incl_state.state == 2 && !pp_incl_state.seen_junk && pp_incl_state.ppp)
1222 {
1223 pp_entry_t *ppp = pplookup(pp_incl_state.ppp);
1224 if(ppp)
1225 {
1226 includelogicentry_t *iep = pp_xmalloc(sizeof(includelogicentry_t));
1227 iep->ppp = ppp;
1228 ppp->iep = iep;
1229 iep->filename = bufferstack[bufferstackidx].include_filename;
1230 iep->prev = NULL;
1231 iep->next = pp_includelogiclist;
1232 if(iep->next)
1233 iep->next->prev = iep;
1234 pp_includelogiclist = iep;
1235 if(pp_status.debug)
1236 fprintf(stderr, "pop_buffer: %s:%d: includelogic added, include_ppp='%s', file='%s'\n", pp_status.input, pp_status.line_number, pp_incl_state.ppp, iep->filename);
1237 }
1238 else if(bufferstack[bufferstackidx].include_filename)
1239 free(bufferstack[bufferstackidx].include_filename);
1240 }
1241 if(pp_incl_state.ppp)
1242 free(pp_incl_state.ppp);
1243 pp_incl_state = bufferstack[bufferstackidx].incl;
1244 pass_data = bufferstack[bufferstackidx].pass_data;
1245
1246 }
1247 }
1248
1249 if(ppdebug)
1250 printf("pop_buffer(%d): %p %p (%d, %d, %d) %p %d\n",
1251 bufferstackidx,
1252 bufferstack[bufferstackidx].bufferstate,
1253 bufferstack[bufferstackidx].define,
1254 bufferstack[bufferstackidx].line_number,
1255 bufferstack[bufferstackidx].char_number,
1256 bufferstack[bufferstackidx].if_depth,
1257 bufferstack[bufferstackidx].filename,
1258 bufferstack[bufferstackidx].should_pop);
1259
1260 pp_switch_to_buffer(bufferstack[bufferstackidx].bufferstate);
1261
1262 if(bufferstack[bufferstackidx].should_pop)
1263 {
1264 if(yy_current_state() == pp_macexp)
1265 macro_add_expansion();
1266 else
1267 pp_internal_error(__FILE__, __LINE__, "Pop buffer and state without macro expansion state");
1268 yy_pop_state();
1269 }
1270
1271 return &bufferstack[bufferstackidx];
1272 }
1273
1274
1275 /*
1276 *-------------------------------------------------------------------------
1277 * Macro nestng support
1278 *-------------------------------------------------------------------------
1279 */
1280 static void push_macro(pp_entry_t *ppp)
1281 {
1282 if(macexpstackidx >= MAXMACEXPSTACK)
1283 pperror("Too many nested macros");
1284
1285 macexpstack[macexpstackidx] = pp_xmalloc(sizeof(macexpstack[0][0]));
1286 memset( macexpstack[macexpstackidx], 0, sizeof(macexpstack[0][0]));
1287 macexpstack[macexpstackidx]->ppp = ppp;
1288 macexpstackidx++;
1289 }
1290
1291 static macexpstackentry_t *top_macro(void)
1292 {
1293 return macexpstackidx > 0 ? macexpstack[macexpstackidx-1] : NULL;
1294 }
1295
1296 static macexpstackentry_t *pop_macro(void)
1297 {
1298 if(macexpstackidx <= 0)
1299 pp_internal_error(__FILE__, __LINE__, "Macro expansion stack underflow\n");
1300 return macexpstack[--macexpstackidx];
1301 }
1302
1303 static void free_macro(macexpstackentry_t *mep)
1304 {
1305 int i;
1306
1307 for(i = 0; i < mep->nargs; i++)
1308 free(mep->args[i]);
1309 if(mep->args)
1310 free(mep->args);
1311 if(mep->nnls)
1312 free(mep->nnls);
1313 if(mep->curarg)
1314 free(mep->curarg);
1315 free(mep);
1316 }
1317
1318 static void add_text_to_macro(const char *text, int len)
1319 {
1320 macexpstackentry_t *mep = top_macro();
1321
1322 assert(mep->ppp->expanding == 0);
1323
1324 if(mep->curargalloc - mep->curargsize <= len+1) /* +1 for '\0' */
1325 {
1326 mep->curargalloc += (ALLOCBLOCKSIZE > len+1) ? ALLOCBLOCKSIZE : len+1;
1327 mep->curarg = pp_xrealloc(mep->curarg, mep->curargalloc * sizeof(mep->curarg[0]));
1328 }
1329 memcpy(mep->curarg + mep->curargsize, text, len);
1330 mep->curargsize += len;
1331 mep->curarg[mep->curargsize] = '\0';
1332 }
1333
1334 static void macro_add_arg(int last)
1335 {
1336 int nnl = 0;
1337 char *cptr;
1338 macexpstackentry_t *mep = top_macro();
1339
1340 assert(mep->ppp->expanding == 0);
1341
1342 mep->args = pp_xrealloc(mep->args, (mep->nargs+1) * sizeof(mep->args[0]));
1343 mep->ppargs = pp_xrealloc(mep->ppargs, (mep->nargs+1) * sizeof(mep->ppargs[0]));
1344 mep->nnls = pp_xrealloc(mep->nnls, (mep->nargs+1) * sizeof(mep->nnls[0]));
1345 mep->args[mep->nargs] = pp_xstrdup(mep->curarg ? mep->curarg : "");
1346 cptr = mep->args[mep->nargs]-1;
1347 while((cptr = strchr(cptr+1, '\n')))
1348 {
1349 nnl++;
1350 }
1351 mep->nnls[mep->nargs] = nnl;
1352 mep->nargs++;
1353 free(mep->curarg);
1354 mep->curargalloc = mep->curargsize = 0;
1355 mep->curarg = NULL;
1356
1357 if(pp_flex_debug)
1358 fprintf(stderr, "macro_add_arg: %s:%d: %d -> '%s'\n",
1359 pp_status.input,
1360 pp_status.line_number,
1361 mep->nargs-1,
1362 mep->args[mep->nargs-1]);
1363
1364 /* Each macro argument must be expanded to cope with stingize */
1365 if(last || mep->args[mep->nargs-1][0])
1366 {
1367 yy_push_state(pp_macexp);
1368 push_buffer(NULL, NULL, NULL, last ? 2 : 1);
1369 yy_scan_string(mep->args[mep->nargs-1]);
1370 /*mep->bufferstackidx = bufferstackidx; But not nested! */
1371 }
1372 }
1373
1374 static void macro_add_expansion(void)
1375 {
1376 macexpstackentry_t *mep = top_macro();
1377
1378 assert(mep->ppp->expanding == 0);
1379
1380 mep->ppargs[mep->nargs-1] = pp_xstrdup(mep->curarg ? mep->curarg : "");
1381 free(mep->curarg);
1382 mep->curargalloc = mep->curargsize = 0;
1383 mep->curarg = NULL;
1384
1385 if(pp_flex_debug)
1386 fprintf(stderr, "macro_add_expansion: %s:%d: %d -> '%s'\n",
1387 pp_status.input,
1388 pp_status.line_number,
1389 mep->nargs-1,
1390 mep->ppargs[mep->nargs-1]);
1391 }
1392
1393
1394 /*
1395 *-------------------------------------------------------------------------
1396 * Output management
1397 *-------------------------------------------------------------------------
1398 */
1399 static void put_buffer(const char *s, int len)
1400 {
1401 if(top_macro())
1402 add_text_to_macro(s, len);
1403 else {
1404 if(pass_data)
1405 fwrite(s, 1, len, ppout);
1406 }
1407 }
1408
1409
1410 /*
1411 *-------------------------------------------------------------------------
1412 * Include management
1413 *-------------------------------------------------------------------------
1414 */
1415 static int is_c_h_include(char *fname, int quoted)
1416 {
1417 int sl=strlen(fname);
1418 if (sl < 2 + 2 * quoted) return 0;
1419 if ((toupper(fname[sl-1-quoted])!='H') && (toupper(fname[sl-1-quoted])!='C')) return 0;
1420 if (fname[sl-2-quoted]!='.') return 0;
1421 return 1;
1422 }
1423
1424 void pp_do_include(char *fname, int type)
1425 {
1426 char *newpath;
1427 int n;
1428 includelogicentry_t *iep;
1429
1430 for(iep = pp_includelogiclist; iep; iep = iep->next)
1431 {
1432 if(!strcmp(iep->filename, fname))
1433 {
1434 /*
1435 * We are done. The file was included before.
1436 * If the define was deleted, then this entry would have
1437 * been deleted too.
1438 */
1439 return;
1440 }
1441 }
1442
1443 n = strlen(fname);
1444
1445 if(n <= 2)
1446 pperror("Empty include filename");
1447
1448 /* Undo the effect of the quotation */
1449 fname[n-1] = '\0';
1450
1451 if((ppin = pp_open_include(fname+1, type ? pp_status.input : NULL, &newpath, type)) == NULL)
1452 pperror("Unable to open include file %s", fname+1);
1453
1454 fname[n-1] = *fname; /* Redo the quotes */
1455 push_buffer(NULL, newpath, fname, 0);
1456 pp_incl_state.seen_junk = 0;
1457 pp_incl_state.state = 0;
1458 pp_incl_state.ppp = NULL;
1459 if (is_c_h_include(newpath, 0)) pass_data=0;
1460 else pass_data=1;
1461
1462 if(pp_status.debug)
1463 fprintf(stderr, "pp_do_include: %s:%d: include_state=%d, include_ppp='%s', include_ifdepth=%d ,pass_data=%d\n",
1464 pp_status.input, pp_status.line_number, pp_incl_state.state, pp_incl_state.ppp, pp_incl_state.ifdepth, pass_data);
1465 pp_switch_to_buffer(pp_create_buffer(ppin, YY_BUF_SIZE));
1466
1467 fprintf(ppout, "# 1 \"%s\" 1%s\n", newpath, type ? "" : " 3");
1468 }
1469
1470 /*
1471 *-------------------------------------------------------------------------
1472 * Push/pop preprocessor ignore state when processing conditionals
1473 * which are false.
1474 *-------------------------------------------------------------------------
1475 */
1476 void pp_push_ignore_state(void)
1477 {
1478 yy_push_state(pp_ignore);
1479 }
1480
1481 void pp_pop_ignore_state(void)
1482 {
1483 yy_pop_state();
1484 }