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