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