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