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