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