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