d3dx9_36: Improve some stubs.
[wine/multimedia.git] / libs / wpp / ppl.l
blob06a4f18df2b82435f2129241bb4dc1f73b9fbb3b
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_endif
154 %x pp_line
155 %x pp_defined
156 %x pp_ignore
157 %x RCINCL
159 ws      [ \v\f\t\r]
160 cident  [a-zA-Z_][0-9a-zA-Z_]*
161 ul      [uUlL]|[uUlL][lL]|[lL][uU]|[lL][lL][uU]|[uU][lL][lL]|[lL][uU][lL]
164 #include <stdio.h>
165 #include <stdlib.h>
166 #include <string.h>
167 #include <ctype.h>
168 #include <assert.h>
169 #include <errno.h>
170 #include <limits.h>
172 #ifndef LLONG_MAX
173 # define LLONG_MAX  ((long long)0x7fffffff << 32 | 0xffffffff)
174 # define LLONG_MIN  (-LLONG_MAX - 1)
175 #endif
176 #ifndef ULLONG_MAX
177 # define ULLONG_MAX ((long long)0xffffffff << 32 | 0xffffffff)
178 #endif
180 #ifndef HAVE_UNISTD_H
181 #define YY_NO_UNISTD_H
182 #endif
184 #include "wine/wpp.h"
185 #include "wpp_private.h"
186 #include "ppy.tab.h"
189  * Make sure that we are running an appropriate version of flex.
190  */
191 #if !defined(YY_FLEX_MAJOR_VERSION) || (1000 * YY_FLEX_MAJOR_VERSION + YY_FLEX_MINOR_VERSION < 2005)
192 #error Must use flex version 2.5.1 or higher (yy_scan_* routines are required).
193 #endif
195 #define YY_READ_BUF_SIZE        65536           /* So we read most of a file at once */
197 #define yy_current_state()      YY_START
198 #define yy_pp_state(x)          yy_pop_state(); yy_push_state(x)
201  * Always update the current character position within a line
202  */
203 #define YY_USER_ACTION  pp_status.char_number+=ppy_leng;
206  * Buffer management for includes and expansions
207  */
208 #define MAXBUFFERSTACK  128     /* Nesting more than 128 includes or macro expansion textss is insane */
210 typedef struct bufferstackentry {
211         YY_BUFFER_STATE bufferstate;    /* Buffer to switch back to */
212         void            *filehandle;    /* Handle to be used with wpp_callbacks->read */
213         pp_entry_t      *define;        /* Points to expanding define or NULL if handling includes */
214         int             line_number;    /* Line that we were handling */
215         int             char_number;    /* The current position on that line */
216         const char      *filename;      /* Filename that we were handling */
217         int             if_depth;       /* How many #if:s deep to check matching #endif:s */
218         int             ncontinuations; /* Remember the continuation state */
219         int             should_pop;     /* Set if we must pop the start-state on EOF */
220         /* Include management */
221         include_state_t incl;
222         char            *include_filename;
223 } bufferstackentry_t;
225 #define ALLOCBLOCKSIZE  (1 << 10)       /* Allocate these chunks at a time for string-buffers */
228  * Macro expansion nesting
229  * We need the stack to handle expansions while scanning
230  * a macro's arguments. The TOS must always be the macro
231  * that receives the current expansion from the scanner.
232  */
233 #define MAXMACEXPSTACK  128     /* Nesting more than 128 macro expansions is insane */
235 typedef struct macexpstackentry {
236         pp_entry_t      *ppp;           /* This macro we are scanning */
237         char            **args;         /* With these arguments */
238         char            **ppargs;       /* Resulting in these preprocessed arguments */
239         int             *nnls;          /* Number of newlines per argument */
240         int             nargs;          /* And this many arguments scanned */
241         int             parentheses;    /* Nesting level of () */
242         int             curargsize;     /* Current scanning argument's size */
243         int             curargalloc;    /* Current scanning argument's block allocated */
244         char            *curarg;        /* Current scanning argument's content */
245 } macexpstackentry_t;
247 #define MACROPARENTHESES()      (top_macro()->parentheses)
250  * Prototypes
251  */
252 static void newline(int);
253 static int make_number(int radix, YYSTYPE *val, const char *str, int len);
254 static void put_buffer(const char *s, int len);
255 /* Buffer management */
256 static void push_buffer(pp_entry_t *ppp, char *filename, char *incname, int pop);
257 static bufferstackentry_t *pop_buffer(void);
258 /* String functions */
259 static void new_string(void);
260 static void add_string(const char *str, int len);
261 static char *get_string(void);
262 static void put_string(void);
263 static int string_start(void);
264 /* Macro functions */
265 static void push_macro(pp_entry_t *ppp);
266 static macexpstackentry_t *top_macro(void);
267 static macexpstackentry_t *pop_macro(void);
268 static void free_macro(macexpstackentry_t *mep);
269 static void add_text_to_macro(const char *text, int len);
270 static void macro_add_arg(int last);
271 static void macro_add_expansion(void);
272 /* Expansion */
273 static void expand_special(pp_entry_t *ppp);
274 static void expand_define(pp_entry_t *ppp);
275 static void expand_macro(macexpstackentry_t *mep);
278  * Local variables
279  */
280 static int ncontinuations;
282 static int strbuf_idx = 0;
283 static int strbuf_alloc = 0;
284 static char *strbuffer = NULL;
285 static int str_startline;
287 static macexpstackentry_t *macexpstack[MAXMACEXPSTACK];
288 static int macexpstackidx = 0;
290 static bufferstackentry_t bufferstack[MAXBUFFERSTACK];
291 static int bufferstackidx = 0;
294  * Global variables
295  */
296 include_state_t pp_incl_state =
298     -1,    /* state */
299     NULL,  /* ppp */
300     0,     /* ifdepth */
301     0      /* seen_junk */
304 includelogicentry_t *pp_includelogiclist = NULL;
306 #define YY_INPUT(buf,result,max_size)                                        \
307         {                                                                    \
308                 result = wpp_callbacks->read(pp_status.file, buf, max_size); \
309         }
311 #define BUFFERINITIALCAPACITY 256
313 void pp_writestring(const char *format, ...)
315         va_list valist;
316         int len;
317         static char *buffer;
318         static int buffercapacity;
319         char *new_buffer;
321         if(buffercapacity == 0)
322         {
323                 buffer = pp_xmalloc(BUFFERINITIALCAPACITY);
324                 if(buffer == NULL)
325                         return;
326                 buffercapacity = BUFFERINITIALCAPACITY;
327         }
329         va_start(valist, format);
330         len = vsnprintf(buffer, buffercapacity,
331                         format, 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                 len = vsnprintf(buffer, buffercapacity,
349                                 format, valist);
350         }
351         va_end(valist);
353         wpp_callbacks->write(buffer, len);
359  **************************************************************************
360  * The scanner starts here
361  **************************************************************************
362  */
365         /*
366          * Catch line-continuations.
367          * Note: Gcc keeps the line-continuations in, for example, strings
368          * intact. However, I prefer to remove them all so that the next
369          * scanner will not need to reduce the continuation state.
370          *
371          * <*>\\\n              newline(0);
372          */
374         /*
375          * Detect the leading # of a preprocessor directive.
376          */
377 <INITIAL,pp_ignore>^{ws}*#      pp_incl_state.seen_junk++; yy_push_state(pp_pp);
379         /*
380          * Scan for the preprocessor directives
381          */
382 <pp_pp>{ws}*include{ws}*        if(yy_top_state() != pp_ignore) {yy_pp_state(pp_inc); return tINCLUDE;} else {yy_pp_state(pp_eol);}
383 <pp_pp>{ws}*define{ws}*         yy_pp_state(yy_current_state() != pp_ignore ? pp_def : pp_eol);
384 <pp_pp>{ws}*error{ws}*          yy_pp_state(pp_eol);    if(yy_top_state() != pp_ignore) return tERROR;
385 <pp_pp>{ws}*warning{ws}*        yy_pp_state(pp_eol);    if(yy_top_state() != pp_ignore) return tWARNING;
386 <pp_pp>{ws}*pragma{ws}*         yy_pp_state(pp_eol);    if(yy_top_state() != pp_ignore) return tPRAGMA;
387 <pp_pp>{ws}*ident{ws}*          yy_pp_state(pp_eol);    if(yy_top_state() != pp_ignore) return tPPIDENT;
388 <pp_pp>{ws}*undef{ws}*          if(yy_top_state() != pp_ignore) {yy_pp_state(pp_ifd); return tUNDEF;} else {yy_pp_state(pp_eol);}
389 <pp_pp>{ws}*ifdef{ws}*          yy_pp_state(pp_ifd);    return tIFDEF;
390 <pp_pp>{ws}*ifndef{ws}*         pp_incl_state.seen_junk--; yy_pp_state(pp_ifd); return tIFNDEF;
391 <pp_pp>{ws}*if{ws}*             yy_pp_state(pp_if);     return tIF;
392 <pp_pp>{ws}*elif{ws}*           yy_pp_state(pp_if);     return tELIF;
393 <pp_pp>{ws}*else{ws}*           yy_pp_state(pp_endif);  return tELSE;
394 <pp_pp>{ws}*endif{ws}*          yy_pp_state(pp_endif);  return tENDIF;
395 <pp_pp>{ws}*line{ws}*           if(yy_top_state() != pp_ignore) {yy_pp_state(pp_line); return tLINE;} else {yy_pp_state(pp_eol);}
396 <pp_pp>{ws}+                    if(yy_top_state() != pp_ignore) {yy_pp_state(pp_line); return tGCCLINE;} else {yy_pp_state(pp_eol);}
397 <pp_pp>{ws}*[a-z]+              ppy_error("Invalid preprocessor token '%s'", ppy_text);
398 <pp_pp>\r?\n                    newline(1); yy_pop_state(); return tNL; /* This could be the null-token */
399 <pp_pp>\\\r?\n                  newline(0);
400 <pp_pp>\\\r?                    ppy_error("Preprocessor junk '%s'", ppy_text);
401 <pp_pp>.                        return *ppy_text;
403         /*
404          * Handle #include and #line
405          */
406 <pp_line>[0-9]+                 return make_number(10, &ppy_lval, ppy_text, ppy_leng);
407 <pp_inc>\<                      new_string(); add_string(ppy_text, ppy_leng); yy_push_state(pp_iqs);
408 <pp_inc,pp_line>\"              new_string(); add_string(ppy_text, ppy_leng); yy_push_state(pp_dqs);
409 <pp_inc,pp_line>{ws}+           ;
410 <pp_inc,pp_line>\n              newline(1); yy_pop_state(); return tNL;
411 <pp_inc,pp_line>\\\r?\n         newline(0);
412 <pp_inc,pp_line>(\\\r?)|(.)     ppy_error(yy_current_state() == pp_inc ? "Trailing junk in #include" : "Trailing junk in #line");
414         /*
415          * Ignore all input when a false clause is parsed
416          */
417 <pp_ignore>[^#/\\\n]+           ;
418 <pp_ignore>\n                   newline(1);
419 <pp_ignore>\\\r?\n              newline(0);
420 <pp_ignore>(\\\r?)|(.)          ;
422         /*
423          * Handle #if and #elif.
424          * These require conditionals to be evaluated, but we do not
425          * want to jam the scanner normally when we see these tokens.
426          * Note: tIDENT is handled below.
427          */
429 <pp_if>0[0-7]*{ul}?             return make_number(8, &ppy_lval, ppy_text, ppy_leng);
430 <pp_if>0[0-7]*[8-9]+{ul}?       ppy_error("Invalid octal digit");
431 <pp_if>[1-9][0-9]*{ul}?         return make_number(10, &ppy_lval, ppy_text, ppy_leng);
432 <pp_if>0[xX][0-9a-fA-F]+{ul}?   return make_number(16, &ppy_lval, ppy_text, ppy_leng);
433 <pp_if>0[xX]                    ppy_error("Invalid hex number");
434 <pp_if>defined                  yy_push_state(pp_defined); return tDEFINED;
435 <pp_if>"<<"                     return tLSHIFT;
436 <pp_if>">>"                     return tRSHIFT;
437 <pp_if>"&&"                     return tLOGAND;
438 <pp_if>"||"                     return tLOGOR;
439 <pp_if>"=="                     return tEQ;
440 <pp_if>"!="                     return tNE;
441 <pp_if>"<="                     return tLTE;
442 <pp_if>">="                     return tGTE;
443 <pp_if>\n                       newline(1); yy_pop_state(); return tNL;
444 <pp_if>\\\r?\n                  newline(0);
445 <pp_if>\\\r?                    ppy_error("Junk in conditional expression");
446 <pp_if>{ws}+                    ;
447 <pp_if>\'                       new_string(); add_string(ppy_text, ppy_leng); yy_push_state(pp_sqs);
448 <pp_if>\"                       ppy_error("String constants not allowed in conditionals");
449 <pp_if>.                        return *ppy_text;
451         /*
452          * Handle #ifdef, #ifndef and #undef
453          * to get only an untranslated/unexpanded identifier
454          */
455 <pp_ifd>{cident}        ppy_lval.cptr = pp_xstrdup(ppy_text); return tIDENT;
456 <pp_ifd>{ws}+           ;
457 <pp_ifd>\n              newline(1); yy_pop_state(); return tNL;
458 <pp_ifd>\\\r?\n         newline(0);
459 <pp_ifd>(\\\r?)|(.)     ppy_error("Identifier expected");
461         /*
462          * Handle #else and #endif.
463          */
464 <pp_endif>{ws}+         ;
465 <pp_endif>\n            newline(1); yy_pop_state(); return tNL;
466 <pp_endif>\\\r?\n       newline(0);
467 <pp_endif>.             ppy_error("Garbage after #else or #endif.");
469         /*
470          * Handle the special 'defined' keyword.
471          * This is necessary to get the identifier prior to any
472          * substitutions.
473          */
474 <pp_defined>{cident}            yy_pop_state(); ppy_lval.cptr = pp_xstrdup(ppy_text); return tIDENT;
475 <pp_defined>{ws}+               ;
476 <pp_defined>(\()|(\))           return *ppy_text;
477 <pp_defined>\\\r?\n             newline(0);
478 <pp_defined>(\\.)|(\n)|(.)      ppy_error("Identifier expected");
480         /*
481          * Handle #error, #warning, #pragma and #ident.
482          * Pass everything literally to the parser, which
483          * will act appropriately.
484          * Comments are stripped from the literal text.
485          */
486 <pp_eol>[^/\\\n]+               if(yy_top_state() != pp_ignore) { ppy_lval.cptr = pp_xstrdup(ppy_text); return tLITERAL; }
487 <pp_eol>\/[^/\\\n*]*            if(yy_top_state() != pp_ignore) { ppy_lval.cptr = pp_xstrdup(ppy_text); return tLITERAL; }
488 <pp_eol>(\\\r?)|(\/[^/*])       if(yy_top_state() != pp_ignore) { ppy_lval.cptr = pp_xstrdup(ppy_text); return tLITERAL; }
489 <pp_eol>\n                      newline(1); yy_pop_state(); if(yy_current_state() != pp_ignore) { return tNL; }
490 <pp_eol>\\\r?\n                 newline(0);
492         /*
493          * Handle left side of #define
494          */
495 <pp_def>{cident}\(              ppy_lval.cptr = pp_xstrdup(ppy_text); if(ppy_lval.cptr) ppy_lval.cptr[ppy_leng-1] = '\0'; yy_pp_state(pp_macro);  return tMACRO;
496 <pp_def>{cident}                ppy_lval.cptr = pp_xstrdup(ppy_text); yy_pp_state(pp_define); return tDEFINE;
497 <pp_def>{ws}+                   ;
498 <pp_def>\\\r?\n                 newline(0);
499 <pp_def>(\\\r?)|(\n)|(.)        perror("Identifier expected");
501         /*
502          * Scan the substitution of a define
503          */
504 <pp_define>[^'"/\\\n]+          ppy_lval.cptr = pp_xstrdup(ppy_text); return tLITERAL;
505 <pp_define>(\\\r?)|(\/[^/*])    ppy_lval.cptr = pp_xstrdup(ppy_text); return tLITERAL;
506 <pp_define>\\\r?\n{ws}+         newline(0); ppy_lval.cptr = pp_xstrdup(" "); return tLITERAL;
507 <pp_define>\\\r?\n              newline(0);
508 <pp_define>\n                   newline(1); yy_pop_state(); return tNL;
509 <pp_define>\'                   new_string(); add_string(ppy_text, ppy_leng); yy_push_state(pp_sqs);
510 <pp_define>\"                   new_string(); add_string(ppy_text, ppy_leng); yy_push_state(pp_dqs);
512         /*
513          * Scan the definition macro arguments
514          */
515 <pp_macro>\){ws}*               yy_pp_state(pp_mbody); return tMACROEND;
516 <pp_macro>{ws}+                 ;
517 <pp_macro>{cident}              ppy_lval.cptr = pp_xstrdup(ppy_text); return tIDENT;
518 <pp_macro>,                     return ',';
519 <pp_macro>"..."                 return tELIPSIS;
520 <pp_macro>(\\\r?)|(\n)|(.)|(\.\.?)      ppy_error("Argument identifier expected");
521 <pp_macro>\\\r?\n               newline(0);
523         /*
524          * Scan the substitution of a macro
525          */
526 <pp_mbody>[^a-zA-Z0-9'"#/\\\n]+ ppy_lval.cptr = pp_xstrdup(ppy_text); return tLITERAL;
527 <pp_mbody>{cident}              ppy_lval.cptr = pp_xstrdup(ppy_text); return tIDENT;
528 <pp_mbody>\#\#                  return tCONCAT;
529 <pp_mbody>\#                    return tSTRINGIZE;
530 <pp_mbody>[0-9][a-zA-Z0-9]*[^a-zA-Z0-9'"#/\\\n]*        ppy_lval.cptr = pp_xstrdup(ppy_text); return tLITERAL;
531 <pp_mbody>(\\\r?)|(\/[^/*'"#\\\n]*)     ppy_lval.cptr = pp_xstrdup(ppy_text); return tLITERAL;
532 <pp_mbody>\\\r?\n{ws}+          newline(0); ppy_lval.cptr = pp_xstrdup(" "); return tLITERAL;
533 <pp_mbody>\\\r?\n               newline(0);
534 <pp_mbody>\n                    newline(1); yy_pop_state(); return tNL;
535 <pp_mbody>\'                    new_string(); add_string(ppy_text, ppy_leng); yy_push_state(pp_sqs);
536 <pp_mbody>\"                    new_string(); add_string(ppy_text, ppy_leng); yy_push_state(pp_dqs);
538         /*
539          * Macro expansion text scanning.
540          * This state is active just after the identifier is scanned
541          * that triggers an expansion. We *must* delete the leading
542          * whitespace before we can start scanning for arguments.
543          *
544          * If we do not see a '(' as next trailing token, then we have
545          * a false alarm. We just continue with a nose-bleed...
546          */
547 <pp_macign>{ws}*/\(     yy_pp_state(pp_macscan);
548 <pp_macign>{ws}*\n      {
549                 if(yy_top_state() != pp_macscan)
550                         newline(0);
551         }
552 <pp_macign>{ws}*\\\r?\n newline(0);
553 <pp_macign>{ws}+|{ws}*\\\r?|.   {
554                 macexpstackentry_t *mac = pop_macro();
555                 yy_pop_state();
556                 put_buffer(mac->ppp->ident, strlen(mac->ppp->ident));
557                 put_buffer(ppy_text, ppy_leng);
558                 free_macro(mac);
559         }
561         /*
562          * Macro expansion argument text scanning.
563          * This state is active when a macro's arguments are being read for expansion.
564          */
565 <pp_macscan>\(  {
566                 if(++MACROPARENTHESES() > 1)
567                         add_text_to_macro(ppy_text, ppy_leng);
568         }
569 <pp_macscan>\)  {
570                 if(--MACROPARENTHESES() == 0)
571                 {
572                         yy_pop_state();
573                         macro_add_arg(1);
574                 }
575                 else
576                         add_text_to_macro(ppy_text, ppy_leng);
577         }
578 <pp_macscan>,           {
579                 if(MACROPARENTHESES() > 1)
580                         add_text_to_macro(ppy_text, ppy_leng);
581                 else
582                         macro_add_arg(0);
583         }
584 <pp_macscan>\"          new_string(); add_string(ppy_text, ppy_leng); yy_push_state(pp_dqs);
585 <pp_macscan>\'          new_string(); add_string(ppy_text, ppy_leng); yy_push_state(pp_sqs);
586 <pp_macscan>"/*"        yy_push_state(pp_comment); add_text_to_macro(" ", 1);
587 <pp_macscan>\n          pp_status.line_number++; pp_status.char_number = 1; add_text_to_macro(ppy_text, ppy_leng);
588 <pp_macscan>([^/(),\\\n"']+)|(\/[^/*(),\\\n'"]*)|(\\\r?)|(.)    add_text_to_macro(ppy_text, ppy_leng);
589 <pp_macscan>\\\r?\n     newline(0);
591         /*
592          * Comment handling (almost all start-conditions)
593          */
594 <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);
595 <pp_comment>[^*\n]*|"*"+[^*/\n]*        ;
596 <pp_comment>\n                          newline(0);
597 <pp_comment>"*"+"/"                     yy_pop_state();
599         /*
600          * Remove C++ style comment (almost all start-conditions)
601          */
602 <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]* {
603                 if(ppy_text[ppy_leng-1] == '\\')
604                         ppy_warning("C++ style comment ends with an escaped newline (escape ignored)");
605         }
607         /*
608          * Single, double and <> quoted constants
609          */
610 <INITIAL,pp_macexp>\"           pp_incl_state.seen_junk++; new_string(); add_string(ppy_text, ppy_leng); yy_push_state(pp_dqs);
611 <INITIAL,pp_macexp>\'           pp_incl_state.seen_junk++; new_string(); add_string(ppy_text, ppy_leng); yy_push_state(pp_sqs);
612 <pp_dqs>[^"\\\n]+               add_string(ppy_text, ppy_leng);
613 <pp_dqs>\"                      {
614                 add_string(ppy_text, ppy_leng);
615                 yy_pop_state();
616                 switch(yy_current_state())
617                 {
618                 case pp_pp:
619                 case pp_define:
620                 case pp_mbody:
621                 case pp_inc:
622                 case RCINCL:
623                         if (yy_current_state()==RCINCL) yy_pop_state();
624                         ppy_lval.cptr = get_string();
625                         return tDQSTRING;
626                 case pp_line:
627                         ppy_lval.cptr = get_string();
628                         return tDQSTRING;
629                 default:
630                         put_string();
631                 }
632         }
633 <pp_sqs>[^'\\\n]+               add_string(ppy_text, ppy_leng);
634 <pp_sqs>\'                      {
635                 add_string(ppy_text, ppy_leng);
636                 yy_pop_state();
637                 switch(yy_current_state())
638                 {
639                 case pp_if:
640                 case pp_define:
641                 case pp_mbody:
642                         ppy_lval.cptr = get_string();
643                         return tSQSTRING;
644                 default:
645                         put_string();
646                 }
647         }
648 <pp_iqs>[^\>\\\n]+              add_string(ppy_text, ppy_leng);
649 <pp_iqs>\>                      {
650                 add_string(ppy_text, ppy_leng);
651                 yy_pop_state();
652                 ppy_lval.cptr = get_string();
653                 return tIQSTRING;
654         }
655 <pp_dqs>\\\r?\n         {
656                 /*
657                  * This is tricky; we need to remove the line-continuation
658                  * from preprocessor strings, but OTOH retain them in all
659                  * other strings. This is because the resource grammar is
660                  * even more braindead than initially analysed and line-
661                  * continuations in strings introduce, sigh, newlines in
662                  * the output. There goes the concept of non-breaking, non-
663                  * spacing whitespace.
664                  */
665                 switch(yy_top_state())
666                 {
667                 case pp_pp:
668                 case pp_define:
669                 case pp_mbody:
670                 case pp_inc:
671                 case pp_line:
672                         newline(0);
673                         break;
674                 default:
675                         add_string(ppy_text, ppy_leng);
676                         newline(-1);
677                 }
678         }
679 <pp_iqs,pp_dqs,pp_sqs>\\.       add_string(ppy_text, ppy_leng);
680 <pp_iqs,pp_dqs,pp_sqs>\n        {
681                 newline(1);
682                 add_string(ppy_text, ppy_leng);
683                 ppy_warning("Newline in string constant encounterd (started line %d)", string_start());
684         }
686         /*
687          * Identifier scanning
688          */
689 <INITIAL,pp_if,pp_inc,pp_macexp>{cident}        {
690                 pp_entry_t *ppp;
691                 pp_incl_state.seen_junk++;
692                 if(!(ppp = pplookup(ppy_text)))
693                 {
694                         if(yy_current_state() == pp_inc)
695                                 ppy_error("Expected include filename");
697                         else if(yy_current_state() == pp_if)
698                         {
699                                 ppy_lval.cptr = pp_xstrdup(ppy_text);
700                                 return tIDENT;
701                         }
702                         else {
703                                 if((yy_current_state()==INITIAL) && (strcasecmp(ppy_text,"RCINCLUDE")==0)){
704                                         yy_push_state(RCINCL);
705                                         return tRCINCLUDE;
706                                 }
707                                 else put_buffer(ppy_text, ppy_leng);
708                         }
709                 }
710                 else if(!ppp->expanding)
711                 {
712                         switch(ppp->type)
713                         {
714                         case def_special:
715                                 expand_special(ppp);
716                                 break;
717                         case def_define:
718                                 expand_define(ppp);
719                                 break;
720                         case def_macro:
721                                 yy_push_state(pp_macign);
722                                 push_macro(ppp);
723                                 break;
724                         default:
725                                 pp_internal_error(__FILE__, __LINE__, "Invalid define type %d\n", ppp->type);
726                         }
727                 }
728                 else put_buffer(ppy_text, ppy_leng);
729         }
731         /*
732          * Everything else that needs to be passed and
733          * newline and continuation handling
734          */
735 <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);
736 <INITIAL,pp_macexp>{ws}+        put_buffer(ppy_text, ppy_leng);
737 <INITIAL>\n                     newline(1);
738 <INITIAL>\\\r?\n                newline(0);
739 <INITIAL>\\\r?                  pp_incl_state.seen_junk++; put_buffer(ppy_text, ppy_leng);
741         /*
742          * Special catcher for macro argmument expansion to prevent
743          * newlines to propagate to the output or admin.
744          */
745 <pp_macexp>(\n)|(.)|(\\\r?(\n|.))       put_buffer(ppy_text, ppy_leng);
747 <RCINCL>[A-Za-z0-9_\.\\/]+ {
748                 ppy_lval.cptr=pp_xstrdup(ppy_text);
749                 yy_pop_state();
750                 return tRCINCLUDEPATH;
751         }
753 <RCINCL>{ws}+ ;
755 <RCINCL>\"              {
756                 new_string(); add_string(ppy_text,ppy_leng);yy_push_state(pp_dqs);
757         }
759         /*
760          * This is a 'catch-all' rule to discover errors in the scanner
761          * in an orderly manner.
762          */
763 <*>.            pp_incl_state.seen_junk++; ppy_warning("Unmatched text '%c' (0x%02x); please report\n", isprint(*ppy_text & 0xff) ? *ppy_text : ' ', *ppy_text);
765 <<EOF>> {
766                 YY_BUFFER_STATE b = YY_CURRENT_BUFFER;
767                 bufferstackentry_t *bep = pop_buffer();
769                 if((!bep && pp_get_if_depth()) || (bep && pp_get_if_depth() != bep->if_depth))
770                         ppy_warning("Unmatched #if/#endif at end of file");
772                 if(!bep)
773                 {
774                         if(YY_START != INITIAL)
775                                 ppy_error("Unexpected end of file during preprocessing");
776                         yyterminate();
777                 }
778                 else if(bep->should_pop == 2)
779                 {
780                         macexpstackentry_t *mac;
781                         mac = pop_macro();
782                         expand_macro(mac);
783                 }
784                 ppy__delete_buffer(b);
785         }
789  **************************************************************************
790  * Support functions
791  **************************************************************************
792  */
794 #ifndef ppy_wrap
795 int ppy_wrap(void)
797         return 1;
799 #endif
803  *-------------------------------------------------------------------------
804  * Output newlines or set them as continuations
806  * Input: -1 - Don't count this one, but update local position (see pp_dqs)
807  *         0 - Line-continuation seen and cache output
808  *         1 - Newline seen and flush output
809  *-------------------------------------------------------------------------
810  */
811 static void newline(int dowrite)
813         pp_status.line_number++;
814         pp_status.char_number = 1;
816         if(dowrite == -1)
817                 return;
819         ncontinuations++;
820         if(dowrite)
821         {
822                 for(;ncontinuations; ncontinuations--)
823                         put_buffer("\n", 1);
824         }
829  *-------------------------------------------------------------------------
830  * Make a number out of an any-base and suffixed string
832  * Possible number extensions:
833  * - ""         int
834  * - "L"        long int
835  * - "LL"       long long int
836  * - "U"        unsigned int
837  * - "UL"       unsigned long int
838  * - "ULL"      unsigned long long int
839  * - "LU"       unsigned long int
840  * - "LLU"      unsigned long long int
841  * - "LUL"      invalid
843  * FIXME:
844  * The sizes of resulting 'int' and 'long' are compiler specific.
845  * I depend on sizeof(int) > 2 here (although a relatively safe
846  * assumption).
847  * Long longs are not yet implemented because this is very compiler
848  * specific and I don't want to think too much about the problems.
850  *-------------------------------------------------------------------------
851  */
852 static int make_number(int radix, YYSTYPE *val, const char *str, int len)
854         int is_l  = 0;
855         int is_ll = 0;
856         int is_u  = 0;
857         char ext[4];
858         long l;
860         ext[3] = '\0';
861         ext[2] = toupper(str[len-1]);
862         ext[1] = len > 1 ? toupper(str[len-2]) : ' ';
863         ext[0] = len > 2 ? toupper(str[len-3]) : ' ';
865         if(!strcmp(ext, "LUL"))
866         {
867                 ppy_error("Invalid constant suffix");
868                 return 0;
869         }
870         else if(!strcmp(ext, "LLU") || !strcmp(ext, "ULL"))
871         {
872                 is_ll++;
873                 is_u++;
874         }
875         else if(!strcmp(ext+1, "LU") || !strcmp(ext+1, "UL"))
876         {
877                 is_l++;
878                 is_u++;
879         }
880         else if(!strcmp(ext+1, "LL"))
881         {
882                 is_ll++;
883         }
884         else if(!strcmp(ext+2, "L"))
885         {
886                 is_l++;
887         }
888         else if(!strcmp(ext+2, "U"))
889         {
890                 is_u++;
891         }
893         if(is_ll)
894         {
895 /* Assume as in the declaration of wrc_ull_t and wrc_sll_t */
896 #ifdef HAVE_LONG_LONG
897                 if (is_u)
898                 {
899                         errno = 0;
900                         val->ull = strtoull(str, NULL, radix);
901                         if (val->ull == ULLONG_MAX && errno == ERANGE)
902                                 ppy_error("integer constant %s is too large\n", str);
903                         return tULONGLONG;
904                 }
905                 else
906                 {
907                         errno = 0;
908                         val->sll = strtoll(str, NULL, radix);
909                         if ((val->sll == LLONG_MIN || val->sll == LLONG_MAX) && errno == ERANGE)
910                                 ppy_error("integer constant %s is too large\n", str);
911                         return tSLONGLONG;
912                 }
913 #else
914                 pp_internal_error(__FILE__, __LINE__, "long long constants not supported on this platform");
915 #endif
916         }
917         else if(is_u && is_l)
918         {
919                 errno = 0;
920                 val->ulong = strtoul(str, NULL, radix);
921                 if (val->ulong == ULONG_MAX && errno == ERANGE)
922                         ppy_error("integer constant %s is too large\n", str);
923                 return tULONG;
924         }
925         else if(!is_u && is_l)
926         {
927                 errno = 0;
928                 val->slong = strtol(str, NULL, radix);
929                 if ((val->slong == LONG_MIN || val->slong == LONG_MAX) && errno == ERANGE)
930                         ppy_error("integer constant %s is too large\n", str);
931                 return tSLONG;
932         }
933         else if(is_u && !is_l)
934         {
935                 unsigned long ul;
936                 errno = 0;
937                 ul = strtoul(str, NULL, radix);
938                 if ((ul == ULONG_MAX && errno == ERANGE) || (ul > UINT_MAX))
939                         ppy_error("integer constant %s is too large\n", str);
940                 val->uint = (unsigned int)ul;
941                 return tUINT;
942         }
944         /* Else it must be an int... */
945         errno = 0;
946         l = strtol(str, NULL, radix);
947         if (((l == LONG_MIN || l == LONG_MAX) && errno == ERANGE) ||
948                 (l > INT_MAX) || (l < INT_MIN))
949                 ppy_error("integer constant %s is too large\n", str);
950         val->sint = (int)l;
951         return tSINT;
956  *-------------------------------------------------------------------------
957  * Macro and define expansion support
959  * FIXME: Variable macro arguments.
960  *-------------------------------------------------------------------------
961  */
962 static void expand_special(pp_entry_t *ppp)
964         const char *dbgtext = "?";
965         static char *buf = NULL;
966         char *new_buf;
968         assert(ppp->type == def_special);
970         if(!strcmp(ppp->ident, "__LINE__"))
971         {
972                 dbgtext = "def_special(__LINE__)";
973                 new_buf = pp_xrealloc(buf, 32);
974                 if(!new_buf)
975                         return;
976                 buf = new_buf;
977                 sprintf(buf, "%d", pp_status.line_number);
978         }
979         else if(!strcmp(ppp->ident, "__FILE__"))
980         {
981                 dbgtext = "def_special(__FILE__)";
982                 new_buf = pp_xrealloc(buf, strlen(pp_status.input) + 3);
983                 if(!new_buf)
984                         return;
985                 buf = new_buf;
986                 sprintf(buf, "\"%s\"", pp_status.input);
987         }
988         else
989                 pp_internal_error(__FILE__, __LINE__, "Special macro '%s' not found...\n", ppp->ident);
991         if(pp_flex_debug)
992                 fprintf(stderr, "expand_special(%d): %s:%d: '%s' -> '%s'\n",
993                         macexpstackidx,
994                         pp_status.input,
995                         pp_status.line_number,
996                         ppp->ident,
997                         buf ? buf : "");
999         if(buf && buf[0])
1000         {
1001                 push_buffer(ppp, NULL, NULL, 0);
1002                 yy_scan_string(buf);
1003         }
1006 static void expand_define(pp_entry_t *ppp)
1008         assert(ppp->type == def_define);
1010         if(pp_flex_debug)
1011                 fprintf(stderr, "expand_define(%d): %s:%d: '%s' -> '%s'\n",
1012                         macexpstackidx,
1013                         pp_status.input,
1014                         pp_status.line_number,
1015                         ppp->ident,
1016                         ppp->subst.text);
1017         if(ppp->subst.text && ppp->subst.text[0])
1018         {
1019                 push_buffer(ppp, NULL, NULL, 0);
1020                 yy_scan_string(ppp->subst.text);
1021         }
1024 static int curdef_idx = 0;
1025 static int curdef_alloc = 0;
1026 static char *curdef_text = NULL;
1028 static void add_text(const char *str, int len)
1030         int new_alloc;
1031         char *new_text;
1033         if(len == 0)
1034                 return;
1035         if(curdef_idx >= curdef_alloc || curdef_alloc - curdef_idx < len)
1036         {
1037                 new_alloc = curdef_alloc + ((len + ALLOCBLOCKSIZE-1) & ~(ALLOCBLOCKSIZE-1));
1038                 new_text = pp_xrealloc(curdef_text, new_alloc * sizeof(curdef_text[0]));
1039                 if(!new_text)
1040                         return;
1041                 curdef_text = new_text;
1042                 curdef_alloc = new_alloc;
1043                 if(curdef_alloc > 65536)
1044                         ppy_warning("Reallocating macro-expansion buffer larger than 64kB");
1045         }
1046         memcpy(&curdef_text[curdef_idx], str, len);
1047         curdef_idx += len;
1050 static mtext_t *add_expand_text(mtext_t *mtp, macexpstackentry_t *mep, int *nnl)
1052         char *cptr;
1053         char *exp;
1054         int tag;
1055         int n;
1057         if(mtp == NULL)
1058                 return NULL;
1060         switch(mtp->type)
1061         {
1062         case exp_text:
1063                 if(pp_flex_debug)
1064                         fprintf(stderr, "add_expand_text: exp_text: '%s'\n", mtp->subst.text);
1065                 add_text(mtp->subst.text, strlen(mtp->subst.text));
1066                 break;
1068         case exp_stringize:
1069                 if(pp_flex_debug)
1070                         fprintf(stderr, "add_expand_text: exp_stringize(%d): '%s'\n",
1071                                 mtp->subst.argidx,
1072                                 mep->args[mtp->subst.argidx]);
1073                 cptr = mep->args[mtp->subst.argidx];
1074                 add_text("\"", 1);
1075                 while(*cptr)
1076                 {
1077                         if(*cptr == '"' || *cptr == '\\')
1078                                 add_text("\\", 1);
1079                         add_text(cptr, 1);
1080                         cptr++;
1081                 }
1082                 add_text("\"", 1);
1083                 break;
1085         case exp_concat:
1086                 if(pp_flex_debug)
1087                         fprintf(stderr, "add_expand_text: exp_concat\n");
1088                 /* Remove trailing whitespace from current expansion text */
1089                 while(curdef_idx)
1090                 {
1091                         if(isspace(curdef_text[curdef_idx-1] & 0xff))
1092                                 curdef_idx--;
1093                         else
1094                                 break;
1095                 }
1096                 /* tag current position and recursively expand the next part */
1097                 tag = curdef_idx;
1098                 mtp = add_expand_text(mtp->next, mep, nnl);
1100                 /* Now get rid of the leading space of the expansion */
1101                 cptr = &curdef_text[tag];
1102                 n = curdef_idx - tag;
1103                 while(n)
1104                 {
1105                         if(isspace(*cptr & 0xff))
1106                         {
1107                                 cptr++;
1108                                 n--;
1109                         }
1110                         else
1111                                 break;
1112                 }
1113                 if(cptr != &curdef_text[tag])
1114                 {
1115                         memmove(&curdef_text[tag], cptr, n);
1116                         curdef_idx -= (curdef_idx - tag) - n;
1117                 }
1118                 break;
1120         case exp_subst:
1121                 if((mtp->next && mtp->next->type == exp_concat) || (mtp->prev && mtp->prev->type == exp_concat))
1122                         exp = mep->args[mtp->subst.argidx];
1123                 else
1124                         exp = mep->ppargs[mtp->subst.argidx];
1125                 if(exp)
1126                 {
1127                         add_text(exp, strlen(exp));
1128                         *nnl -= mep->nnls[mtp->subst.argidx];
1129                         cptr = strchr(exp, '\n');
1130                         while(cptr)
1131                         {
1132                                 *cptr = ' ';
1133                                 cptr = strchr(cptr+1, '\n');
1134                         }
1135                         mep->nnls[mtp->subst.argidx] = 0;
1136                 }
1137                 if(pp_flex_debug)
1138                         fprintf(stderr, "add_expand_text: exp_subst(%d): '%s'\n", mtp->subst.argidx, exp);
1139                 break;
1141         default:
1142                 pp_internal_error(__FILE__, __LINE__, "Invalid expansion type (%d) in macro expansion\n", mtp->type);
1143         }
1144         return mtp;
1147 static void expand_macro(macexpstackentry_t *mep)
1149         mtext_t *mtp;
1150         int n, k;
1151         char *cptr;
1152         int nnl = 0;
1153         pp_entry_t *ppp = mep->ppp;
1154         int nargs = mep->nargs;
1156         assert(ppp->type == def_macro);
1157         assert(ppp->expanding == 0);
1159         if((ppp->nargs >= 0 && nargs != ppp->nargs) || (ppp->nargs < 0 && nargs < -ppp->nargs))
1160         {
1161                 ppy_error("Too %s macro arguments (%d)", nargs < abs(ppp->nargs) ? "few" : "many", nargs);
1162                 return;
1163         }
1165         for(n = 0; n < nargs; n++)
1166                 nnl += mep->nnls[n];
1168         if(pp_flex_debug)
1169                 fprintf(stderr, "expand_macro(%d): %s:%d: '%s'(%d,%d) -> ...\n",
1170                         macexpstackidx,
1171                         pp_status.input,
1172                         pp_status.line_number,
1173                         ppp->ident,
1174                         mep->nargs,
1175                         nnl);
1177         curdef_idx = 0;
1179         for(mtp = ppp->subst.mtext; mtp; mtp = mtp->next)
1180         {
1181                 if(!(mtp = add_expand_text(mtp, mep, &nnl)))
1182                         break;
1183         }
1185         for(n = 0; n < nnl; n++)
1186                 add_text("\n", 1);
1188         /* To make sure there is room and termination (see below) */
1189         add_text(" \0", 2);
1191         /* Strip trailing whitespace from expansion */
1192         for(k = curdef_idx, cptr = &curdef_text[curdef_idx-1]; k > 0; k--, cptr--)
1193         {
1194                 if(!isspace(*cptr & 0xff))
1195                         break;
1196         }
1198         /*
1199          * We must add *one* whitespace to make sure that there
1200          * is a token-separation after the expansion.
1201          */
1202         *(++cptr) = ' ';
1203         *(++cptr) = '\0';
1204         k++;
1206         /* Strip leading whitespace from expansion */
1207         for(n = 0, cptr = curdef_text; n < k; n++, cptr++)
1208         {
1209                 if(!isspace(*cptr & 0xff))
1210                         break;
1211         }
1213         if(k - n > 0)
1214         {
1215                 if(pp_flex_debug)
1216                         fprintf(stderr, "expand_text: '%s'\n", curdef_text + n);
1217                 push_buffer(ppp, NULL, NULL, 0);
1218                 /*yy_scan_bytes(curdef_text + n, k - n);*/
1219                 yy_scan_string(curdef_text + n);
1220         }
1224  *-------------------------------------------------------------------------
1225  * String collection routines
1226  *-------------------------------------------------------------------------
1227  */
1228 static void new_string(void)
1230 #ifdef DEBUG
1231         if(strbuf_idx)
1232                 ppy_warning("new_string: strbuf_idx != 0");
1233 #endif
1234         strbuf_idx = 0;
1235         str_startline = pp_status.line_number;
1238 static void add_string(const char *str, int len)
1240         int new_alloc;
1241         char *new_buffer;
1243         if(len == 0)
1244                 return;
1245         if(strbuf_idx >= strbuf_alloc || strbuf_alloc - strbuf_idx < len)
1246         {
1247                 new_alloc = strbuf_alloc + ((len + ALLOCBLOCKSIZE-1) & ~(ALLOCBLOCKSIZE-1));
1248                 new_buffer = pp_xrealloc(strbuffer, new_alloc * sizeof(strbuffer[0]));
1249                 if(!new_buffer)
1250                         return;
1251                 strbuffer = new_buffer;
1252                 strbuf_alloc = new_alloc;
1253                 if(strbuf_alloc > 65536)
1254                         ppy_warning("Reallocating string buffer larger than 64kB");
1255         }
1256         memcpy(&strbuffer[strbuf_idx], str, len);
1257         strbuf_idx += len;
1260 static char *get_string(void)
1262         char *str = pp_xmalloc(strbuf_idx + 1);
1263         if(!str)
1264                 return NULL;
1265         memcpy(str, strbuffer, strbuf_idx);
1266         str[strbuf_idx] = '\0';
1267 #ifdef DEBUG
1268         strbuf_idx = 0;
1269 #endif
1270         return str;
1273 static void put_string(void)
1275         put_buffer(strbuffer, strbuf_idx);
1276 #ifdef DEBUG
1277         strbuf_idx = 0;
1278 #endif
1281 static int string_start(void)
1283         return str_startline;
1288  *-------------------------------------------------------------------------
1289  * Buffer management
1290  *-------------------------------------------------------------------------
1291  */
1292 static void push_buffer(pp_entry_t *ppp, char *filename, char *incname, int pop)
1294         if(ppy_debug)
1295                 printf("push_buffer(%d): %p %p %p %d\n", bufferstackidx, ppp, filename, incname, pop);
1296         if(bufferstackidx >= MAXBUFFERSTACK)
1297                 pp_internal_error(__FILE__, __LINE__, "Buffer stack overflow");
1299         memset(&bufferstack[bufferstackidx], 0, sizeof(bufferstack[0]));
1300         bufferstack[bufferstackidx].bufferstate = YY_CURRENT_BUFFER;
1301         bufferstack[bufferstackidx].filehandle  = pp_status.file;
1302         bufferstack[bufferstackidx].define      = ppp;
1303         bufferstack[bufferstackidx].line_number = pp_status.line_number;
1304         bufferstack[bufferstackidx].char_number = pp_status.char_number;
1305         bufferstack[bufferstackidx].if_depth    = pp_get_if_depth();
1306         bufferstack[bufferstackidx].should_pop  = pop;
1307         bufferstack[bufferstackidx].filename    = pp_status.input;
1308         bufferstack[bufferstackidx].ncontinuations      = ncontinuations;
1309         bufferstack[bufferstackidx].incl                = pp_incl_state;
1310         bufferstack[bufferstackidx].include_filename    = incname;
1312         if(ppp)
1313                 ppp->expanding = 1;
1314         else if(filename)
1315         {
1316                 /* These will track the ppy_error to the correct file and line */
1317                 pp_status.line_number = 1;
1318                 pp_status.char_number = 1;
1319                 pp_status.input  = filename;
1320                 ncontinuations = 0;
1321         }
1322         else if(!pop)
1323                 pp_internal_error(__FILE__, __LINE__, "Pushing buffer without knowing where to go to");
1324         bufferstackidx++;
1327 static bufferstackentry_t *pop_buffer(void)
1329         if(bufferstackidx < 0)
1330                 pp_internal_error(__FILE__, __LINE__, "Bufferstack underflow?");
1332         if(bufferstackidx == 0)
1333                 return NULL;
1335         bufferstackidx--;
1337         if(bufferstack[bufferstackidx].define)
1338                 bufferstack[bufferstackidx].define->expanding = 0;
1339         else
1340         {
1341                 if(!bufferstack[bufferstackidx].should_pop)
1342                 {
1343                         wpp_callbacks->close(pp_status.file);
1344                         pp_writestring("# %d \"%s\" 2\n", bufferstack[bufferstackidx].line_number, bufferstack[bufferstackidx].filename);
1346                         /* We have EOF, check the include logic */
1347                         if(pp_incl_state.state == 2 && !pp_incl_state.seen_junk && pp_incl_state.ppp)
1348                         {
1349                                 pp_entry_t *ppp = pplookup(pp_incl_state.ppp);
1350                                 if(ppp)
1351                                 {
1352                                         includelogicentry_t *iep = pp_xmalloc(sizeof(includelogicentry_t));
1353                                         if(!iep)
1354                                                 return NULL;
1356                                         iep->ppp = ppp;
1357                                         ppp->iep = iep;
1358                                         iep->filename = bufferstack[bufferstackidx].include_filename;
1359                                         iep->prev = NULL;
1360                                         iep->next = pp_includelogiclist;
1361                                         if(iep->next)
1362                                                 iep->next->prev = iep;
1363                                         pp_includelogiclist = iep;
1364                                         if(pp_status.debug)
1365                                                 fprintf(stderr, "pop_buffer: %s:%d: includelogic added, include_ppp='%s', file='%s'\n", bufferstack[bufferstackidx].filename, bufferstack[bufferstackidx].line_number, pp_incl_state.ppp, iep->filename);
1366                                 }
1367                                 else
1368                                         free(bufferstack[bufferstackidx].include_filename);
1369                         }
1370                         free(pp_incl_state.ppp);
1371                         pp_incl_state   = bufferstack[bufferstackidx].incl;
1373                 }
1374                 pp_status.line_number = bufferstack[bufferstackidx].line_number;
1375                 pp_status.char_number = bufferstack[bufferstackidx].char_number;
1376                 pp_status.input  = bufferstack[bufferstackidx].filename;
1377                 ncontinuations = bufferstack[bufferstackidx].ncontinuations;
1378         }
1380         if(ppy_debug)
1381                 printf("pop_buffer(%d): %p %p (%d, %d, %d) %p %d\n",
1382                         bufferstackidx,
1383                         bufferstack[bufferstackidx].bufferstate,
1384                         bufferstack[bufferstackidx].define,
1385                         bufferstack[bufferstackidx].line_number,
1386                         bufferstack[bufferstackidx].char_number,
1387                         bufferstack[bufferstackidx].if_depth,
1388                         bufferstack[bufferstackidx].filename,
1389                         bufferstack[bufferstackidx].should_pop);
1391         pp_status.file = bufferstack[bufferstackidx].filehandle;
1392         ppy__switch_to_buffer(bufferstack[bufferstackidx].bufferstate);
1394         if(bufferstack[bufferstackidx].should_pop)
1395         {
1396                 if(yy_current_state() == pp_macexp)
1397                         macro_add_expansion();
1398                 else
1399                         pp_internal_error(__FILE__, __LINE__, "Pop buffer and state without macro expansion state");
1400                 yy_pop_state();
1401         }
1403         return &bufferstack[bufferstackidx];
1408  *-------------------------------------------------------------------------
1409  * Macro nestng support
1410  *-------------------------------------------------------------------------
1411  */
1412 static void push_macro(pp_entry_t *ppp)
1414         if(macexpstackidx >= MAXMACEXPSTACK)
1415         {
1416                 ppy_error("Too many nested macros");
1417                 return;
1418         }
1420         macexpstack[macexpstackidx] = pp_xmalloc(sizeof(macexpstack[0][0]));
1421         if(!macexpstack[macexpstackidx])
1422                 return;
1423         memset( macexpstack[macexpstackidx], 0, sizeof(macexpstack[0][0]));
1424         macexpstack[macexpstackidx]->ppp = ppp;
1425         macexpstackidx++;
1428 static macexpstackentry_t *top_macro(void)
1430         return macexpstackidx > 0 ? macexpstack[macexpstackidx-1] : NULL;
1433 static macexpstackentry_t *pop_macro(void)
1435         if(macexpstackidx <= 0)
1436                 pp_internal_error(__FILE__, __LINE__, "Macro expansion stack underflow\n");
1437         return macexpstack[--macexpstackidx];
1440 static void free_macro(macexpstackentry_t *mep)
1442         int i;
1444         for(i = 0; i < mep->nargs; i++)
1445                 free(mep->args[i]);
1446         free(mep->args);
1447         free(mep->nnls);
1448         free(mep->curarg);
1449         free(mep);
1452 static void add_text_to_macro(const char *text, int len)
1454         macexpstackentry_t *mep = top_macro();
1456         assert(mep->ppp->expanding == 0);
1458         if(mep->curargalloc - mep->curargsize <= len+1) /* +1 for '\0' */
1459         {
1460                 char *new_curarg;
1461                 int new_alloc = mep->curargalloc + (ALLOCBLOCKSIZE > len+1) ? ALLOCBLOCKSIZE : len+1;
1462                 new_curarg = pp_xrealloc(mep->curarg, new_alloc * sizeof(mep->curarg[0]));
1463                 if(!new_curarg)
1464                         return;
1465                 mep->curarg = new_curarg;
1466                 mep->curargalloc = new_alloc;
1467         }
1468         memcpy(mep->curarg + mep->curargsize, text, len);
1469         mep->curargsize += len;
1470         mep->curarg[mep->curargsize] = '\0';
1473 static void macro_add_arg(int last)
1475         int nnl = 0;
1476         char *cptr;
1477         char **new_args, **new_ppargs;
1478         int *new_nnls;
1479         macexpstackentry_t *mep = top_macro();
1481         assert(mep->ppp->expanding == 0);
1483         new_args = pp_xrealloc(mep->args, (mep->nargs+1) * sizeof(mep->args[0]));
1484         if(!new_args)
1485                 return;
1486         mep->args = new_args;
1488         new_ppargs = pp_xrealloc(mep->ppargs, (mep->nargs+1) * sizeof(mep->ppargs[0]));
1489         if(!new_ppargs)
1490                 return;
1491         mep->ppargs = new_ppargs;
1493         new_nnls = pp_xrealloc(mep->nnls, (mep->nargs+1) * sizeof(mep->nnls[0]));
1494         if(!new_nnls)
1495                 return;
1496         mep->nnls = new_nnls;
1498         mep->args[mep->nargs] = pp_xstrdup(mep->curarg ? mep->curarg : "");
1499         if(!mep->args[mep->nargs])
1500                 return;
1501         cptr = mep->args[mep->nargs]-1;
1502         while((cptr = strchr(cptr+1, '\n')))
1503         {
1504                 nnl++;
1505         }
1506         mep->nnls[mep->nargs] = nnl;
1507         mep->nargs++;
1508         free(mep->curarg);
1509         mep->curargalloc = mep->curargsize = 0;
1510         mep->curarg = NULL;
1512         if(pp_flex_debug)
1513                 fprintf(stderr, "macro_add_arg: %s:%d: %d -> '%s'\n",
1514                         pp_status.input,
1515                         pp_status.line_number,
1516                         mep->nargs-1,
1517                         mep->args[mep->nargs-1]);
1519         /* Each macro argument must be expanded to cope with stingize */
1520         if(last || mep->args[mep->nargs-1][0])
1521         {
1522                 yy_push_state(pp_macexp);
1523                 push_buffer(NULL, NULL, NULL, last ? 2 : 1);
1524                 yy_scan_string(mep->args[mep->nargs-1]);
1525                 /*mep->bufferstackidx = bufferstackidx;  But not nested! */
1526         }
1529 static void macro_add_expansion(void)
1531         macexpstackentry_t *mep = top_macro();
1533         assert(mep->ppp->expanding == 0);
1535         mep->ppargs[mep->nargs-1] = pp_xstrdup(mep->curarg ? mep->curarg : "");
1536         free(mep->curarg);
1537         mep->curargalloc = mep->curargsize = 0;
1538         mep->curarg = NULL;
1540         if(pp_flex_debug)
1541                 fprintf(stderr, "macro_add_expansion: %s:%d: %d -> '%s'\n",
1542                         pp_status.input,
1543                         pp_status.line_number,
1544                         mep->nargs-1,
1545                         mep->ppargs[mep->nargs-1] ? mep->ppargs[mep->nargs-1] : "");
1550  *-------------------------------------------------------------------------
1551  * Output management
1552  *-------------------------------------------------------------------------
1553  */
1554 static void put_buffer(const char *s, int len)
1556         if(top_macro())
1557                 add_text_to_macro(s, len);
1558         else
1559                 wpp_callbacks->write(s, len);
1564  *-------------------------------------------------------------------------
1565  * Include management
1566  *-------------------------------------------------------------------------
1567  */
1568 void pp_do_include(char *fname, int type)
1570         char *newpath;
1571         int n;
1572         includelogicentry_t *iep;
1573         void *fp;
1575         if(!fname)
1576                 return;
1578         for(iep = pp_includelogiclist; iep; iep = iep->next)
1579         {
1580                 if(!strcmp(iep->filename, fname))
1581                 {
1582                         /*
1583                          * We are done. The file was included before.
1584                          * If the define was deleted, then this entry would have
1585                          * been deleted too.
1586                          */
1587                         return;
1588                 }
1589         }
1591         n = strlen(fname);
1593         if(n <= 2)
1594         {
1595                 ppy_error("Empty include filename");
1596                 return;
1597         }
1599         /* Undo the effect of the quotation */
1600         fname[n-1] = '\0';
1602         if((fp = pp_open_include(fname+1, type ? pp_status.input : NULL, &newpath)) == NULL)
1603         {
1604                 ppy_error("Unable to open include file %s", fname+1);
1605                 return;
1606         }
1608         fname[n-1] = *fname;    /* Redo the quotes */
1609         push_buffer(NULL, newpath, fname, 0);
1610         pp_incl_state.seen_junk = 0;
1611         pp_incl_state.state = 0;
1612         pp_incl_state.ppp = NULL;
1614         if(pp_status.debug)
1615                 fprintf(stderr, "pp_do_include: %s:%d: include_state=%d, include_ppp='%s', include_ifdepth=%d\n",
1616                         pp_status.input, pp_status.line_number, pp_incl_state.state, pp_incl_state.ppp, pp_incl_state.ifdepth);
1617         pp_status.file = fp;
1618         ppy__switch_to_buffer(ppy__create_buffer(NULL, YY_BUF_SIZE));
1620         pp_writestring("# 1 \"%s\" 1%s\n", newpath, type ? "" : " 3");
1624  *-------------------------------------------------------------------------
1625  * Push/pop preprocessor ignore state when processing conditionals
1626  * which are false.
1627  *-------------------------------------------------------------------------
1628  */
1629 void pp_push_ignore_state(void)
1631         yy_push_state(pp_ignore);
1634 void pp_pop_ignore_state(void)
1636         yy_pop_state();