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