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