]>
Commit | Line | Data |
---|---|---|
24c3c71a | 1 | /* C Compatible Compiler Preprocessor (CCCP) |
fa5db828 | 2 | Copyright (C) 1986, 1987, 1989, 2000, 2001 Free Software Foundation, Inc. |
24c3c71a ZW |
3 | Written by Paul Rubin, June 1986 |
4 | Adapted to ANSI C, Richard Stallman, Jan 1987 | |
5 | Dusted off, polished, and adapted for use as traditional | |
6 | preprocessor only, Zack Weinberg, Jul 2000 | |
7 | ||
8 | This program is free software; you can redistribute it and/or modify it | |
9 | under the terms of the GNU General Public License as published by the | |
ef90743a | 10 | Free Software Foundation; either version 2, or (at your option) any |
24c3c71a ZW |
11 | later version. |
12 | ||
13 | This program is distributed in the hope that it will be useful, | |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
19 | along with this program; if not, write to the Free Software | |
ef90743a | 20 | Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ |
24c3c71a ZW |
21 | |
22 | #include "config.h" | |
23 | #include "system.h" | |
24 | #include "version.h" | |
25 | #include "cppdefault.h" | |
0a8ad417 | 26 | #include "tradcpp.h" |
2f638f96 | 27 | #include "mkdeps.h" |
79e2e160 | 28 | #include "intl.h" |
24c3c71a | 29 | |
24c3c71a ZW |
30 | typedef unsigned char U_CHAR; |
31 | ||
32 | /* Name under which this program was invoked. */ | |
33 | ||
0a8ad417 | 34 | static const char *progname; |
24c3c71a ZW |
35 | |
36 | /* Current maximum length of directory names in the search path | |
37 | for include files. (Altered as we get more of them.) */ | |
38 | ||
39 | size_t max_include_len; | |
40 | ||
41 | /* Nonzero means copy comments into the output file. */ | |
42 | ||
43 | int put_out_comments = 0; | |
44 | ||
2f638f96 NB |
45 | /* mkdeps.h opaque structure that encapsulates dependency information. */ |
46 | struct deps *deps; | |
47 | ||
24c3c71a ZW |
48 | /* Nonzero means print the names of included files rather than |
49 | the preprocessed output. 1 means just the #include "...", | |
50 | 2 means #include <...> as well. */ | |
51 | ||
52 | int print_deps = 0; | |
53 | ||
2f638f96 NB |
54 | /* Nonzero means print dummy targets for each header file. */ |
55 | ||
56 | int print_deps_phony_targets = 0; | |
57 | ||
58 | /* If true, fopen (deps_file, "a") else fopen (deps_file, "w"). */ | |
59 | ||
60 | int deps_append = 0; | |
61 | ||
c3843cea JJ |
62 | /* File name which deps are being written to. This is 0 if deps are |
63 | being written to stdout. */ | |
64 | ||
65 | const char *deps_file = 0; | |
66 | ||
67 | /* Nonzero if missing .h files in -M output are assumed to be | |
68 | generated files and not errors. */ | |
69 | ||
2f638f96 | 70 | int deps_missing_files = 0; |
c3843cea | 71 | |
24c3c71a ZW |
72 | /* Nonzero means don't output line number information. */ |
73 | ||
74 | int no_line_commands; | |
75 | ||
76 | /* Nonzero means inhibit output of the preprocessed text | |
77 | and instead output the definitions of all user-defined macros | |
78 | in a form suitable for use as input to cccp. */ | |
79 | ||
80 | int dump_macros; | |
81 | ||
82 | /* Nonzero means don't print warning messages. -w. */ | |
83 | ||
84 | int inhibit_warnings = 0; | |
85 | ||
2f638f96 NB |
86 | /* Non-0 means don't output the preprocessed program. */ |
87 | int inhibit_output = 0; | |
88 | ||
0fef3fd0 NB |
89 | /* Nonzero means chars are signed. */ |
90 | #if DEFAULT_SIGNED_CHAR | |
91 | int flag_signed_char = 1; | |
92 | #else | |
93 | int flag_signed_char = 0; | |
94 | #endif | |
95 | ||
24c3c71a ZW |
96 | /* Nonzero means warn if slash-star appears in a comment. */ |
97 | ||
98 | int warn_comments; | |
99 | ||
100 | /* Nonzero causes output not to be done, | |
101 | but directives such as #define that have side effects | |
102 | are still obeyed. */ | |
103 | ||
104 | int no_output; | |
105 | ||
24c3c71a ZW |
106 | /* I/O buffer structure. |
107 | The `fname' field is nonzero for source files and #include files | |
108 | and for the dummy text used for -D and -U. | |
109 | It is zero for rescanning results of macro expansion | |
110 | and for expanding macro arguments. */ | |
111 | #define INPUT_STACK_MAX 200 | |
6d4587f7 | 112 | struct file_name_list; |
24c3c71a ZW |
113 | struct file_buf { |
114 | const char *fname; | |
115 | int lineno; | |
116 | int length; | |
117 | U_CHAR *buf; | |
118 | U_CHAR *bufp; | |
119 | /* Macro that this level is the expansion of. | |
120 | Included so that we can reenable the macro | |
121 | at the end of this level. */ | |
122 | struct hashnode *macro; | |
123 | /* Value of if_stack at start of this file. | |
124 | Used to prohibit unmatched #endif (etc) in an include file. */ | |
125 | struct if_stack *if_stack; | |
126 | /* Object to be freed at end of input at this level. */ | |
127 | U_CHAR *free_ptr; | |
6d4587f7 ZW |
128 | /* Position to start scanning for #include_next in this file. */ |
129 | struct file_name_list *next_header_dir; | |
24c3c71a ZW |
130 | } instack[INPUT_STACK_MAX]; |
131 | ||
132 | typedef struct file_buf FILE_BUF; | |
133 | ||
134 | /* Current nesting level of input sources. | |
135 | `instack[indepth]' is the level currently being read. */ | |
136 | int indepth = -1; | |
137 | #define CHECK_DEPTH(code) \ | |
138 | if (indepth >= (INPUT_STACK_MAX - 1)) \ | |
139 | { \ | |
140 | error_with_line (line_for_error (instack[indepth].lineno), \ | |
141 | "macro or #include recursion too deep"); \ | |
142 | code; \ | |
143 | } | |
144 | ||
145 | /* Current depth in #include directives that use <...>. */ | |
146 | int system_include_depth = 0; | |
147 | ||
148 | /* The output buffer. Its LENGTH field is the amount of room allocated | |
149 | for the buffer, not the number of chars actually present. To get | |
150 | that, subtract outbuf.buf from outbuf.bufp. */ | |
151 | ||
152 | #define OUTBUF_SIZE 10 /* initial size of output buffer */ | |
153 | FILE_BUF outbuf; | |
154 | ||
155 | /* Grow output buffer OBUF points at | |
156 | so it can hold at least NEEDED more chars. */ | |
157 | ||
158 | #define check_expand(OBUF, NEEDED) do { \ | |
159 | if ((OBUF)->length - ((OBUF)->bufp - (OBUF)->buf) <= (NEEDED)) \ | |
160 | grow_outbuf ((OBUF), (NEEDED)); \ | |
161 | } while (0) | |
162 | ||
163 | struct file_name_list | |
164 | { | |
165 | struct file_name_list *next; | |
166 | const char *fname; | |
167 | }; | |
168 | ||
169 | struct file_name_list *include = 0; /* First dir to search */ | |
170 | /* First dir to search for <file> */ | |
171 | struct file_name_list *first_bracket_include = 0; | |
172 | struct file_name_list *last_include = 0; /* Last in chain */ | |
173 | ||
174 | /* List of included files that contained #once. */ | |
175 | struct file_name_list *dont_repeat_files = 0; | |
176 | ||
177 | /* List of other included files. */ | |
178 | struct file_name_list *all_include_files = 0; | |
179 | \f | |
180 | /* Structure allocated for every #define. For a simple replacement | |
181 | such as | |
182 | #define foo bar , | |
183 | nargs = -1, the `pattern' list is null, and the expansion is just | |
184 | the replacement text. Nargs = 0 means a functionlike macro with no args, | |
185 | e.g., | |
186 | #define getchar() getc (stdin) . | |
187 | When there are args, the expansion is the replacement text with the | |
188 | args squashed out, and the reflist is a list describing how to | |
189 | build the output from the input: e.g., "3 chars, then the 1st arg, | |
190 | then 9 chars, then the 3rd arg, then 0 chars, then the 2nd arg". | |
191 | The chars here come from the expansion. Whatever is left of the | |
192 | expansion after the last arg-occurrence is copied after that arg. | |
193 | Note that the reflist can be arbitrarily long--- | |
194 | its length depends on the number of times the arguments appear in | |
195 | the replacement text, not how many args there are. Example: | |
196 | #define f(x) x+x+x+x+x+x+x would have replacement text "++++++" and | |
197 | pattern list | |
198 | { (0, 1), (1, 1), (1, 1), ..., (1, 1), NULL } | |
199 | where (x, y) means (nchars, argno). */ | |
200 | ||
201 | typedef struct definition DEFINITION; | |
202 | struct definition { | |
203 | int nargs; | |
204 | int length; /* length of expansion string */ | |
205 | U_CHAR *expansion; | |
206 | struct reflist { | |
207 | struct reflist *next; | |
208 | char stringify; /* nonzero if this arg was preceded by a | |
209 | # operator. */ | |
210 | char raw_before; /* Nonzero if a ## operator before arg. */ | |
211 | char raw_after; /* Nonzero if a ## operator after arg. */ | |
212 | int nchars; /* Number of literal chars to copy before | |
213 | this arg occurrence. */ | |
214 | int argno; /* Number of arg to substitute (origin-0) */ | |
215 | } *pattern; | |
216 | /* Names of macro args, concatenated in reverse order | |
217 | with comma-space between them. | |
218 | The only use of this is that we warn on redefinition | |
219 | if this differs between the old and new definitions. */ | |
0a8ad417 | 220 | const U_CHAR *argnames; |
24c3c71a ZW |
221 | }; |
222 | ||
4eb191f3 NB |
223 | /* Chained list of answers to an assertion. */ |
224 | struct answer | |
225 | { | |
226 | struct answer *next; | |
227 | const unsigned char *answer; | |
228 | size_t len; | |
229 | }; | |
230 | ||
24c3c71a ZW |
231 | /* different kinds of things that can appear in the value field |
232 | of a hash node. Actually, this may be useless now. */ | |
233 | union hashval { | |
234 | const char *cpval; | |
235 | DEFINITION *defn; | |
4eb191f3 | 236 | struct answer *answers; |
24c3c71a ZW |
237 | }; |
238 | ||
24c3c71a ZW |
239 | /* The structure of a node in the hash table. The hash table |
240 | has entries for all tokens defined by #define commands (type T_MACRO), | |
241 | plus some special tokens like __LINE__ (these each have their own | |
242 | type, and the appropriate code is run when that type of node is seen. | |
243 | It does not contain control words like "#define", which are recognized | |
244 | by a separate piece of code. */ | |
245 | ||
246 | /* different flavors of hash nodes --- also used in keyword table */ | |
247 | enum node_type { | |
248 | T_DEFINE = 1, /* `#define' */ | |
249 | T_INCLUDE, /* `#include' */ | |
6d4587f7 | 250 | T_INCLUDE_NEXT,/* `#include_next' */ |
24c3c71a ZW |
251 | T_IFDEF, /* `#ifdef' */ |
252 | T_IFNDEF, /* `#ifndef' */ | |
253 | T_IF, /* `#if' */ | |
254 | T_ELSE, /* `#else' */ | |
255 | T_ELIF, /* `#elif' */ | |
256 | T_UNDEF, /* `#undef' */ | |
257 | T_LINE, /* `#line' */ | |
258 | T_ENDIF, /* `#endif' */ | |
3244472d NB |
259 | T_ERROR, /* `#error' */ |
260 | T_WARNING, /* `#warning' */ | |
40bd4395 NB |
261 | T_ASSERT, /* `#assert' */ |
262 | T_UNASSERT, /* `#unassert' */ | |
24c3c71a ZW |
263 | T_SPECLINE, /* special symbol `__LINE__' */ |
264 | T_DATE, /* `__DATE__' */ | |
265 | T_FILE, /* `__FILE__' */ | |
266 | T_BASE_FILE, /* `__BASE_FILE__' */ | |
267 | T_INCLUDE_LEVEL, /* `__INCLUDE_LEVEL__' */ | |
268 | T_VERSION, /* `__VERSION__' */ | |
269 | T_TIME, /* `__TIME__' */ | |
270 | T_CONST, /* Constant value, used by `__STDC__' */ | |
271 | T_MACRO, /* macro defined by `#define' */ | |
272 | T_SPEC_DEFINED, /* special `defined' macro for use in #if statements */ | |
273 | T_UNUSED /* Used for something not defined. */ | |
274 | }; | |
275 | ||
276 | struct hashnode { | |
277 | struct hashnode *next; /* double links for easy deletion */ | |
278 | struct hashnode *prev; | |
279 | struct hashnode **bucket_hdr; /* also, a back pointer to this node's hash | |
280 | chain is kept, in case the node is the head | |
281 | of the chain and gets deleted. */ | |
282 | enum node_type type; /* type of special token */ | |
283 | int length; /* length of token, for quick comparison */ | |
284 | U_CHAR *name; /* the actual name */ | |
285 | union hashval value; /* pointer to expansion, or whatever */ | |
286 | }; | |
287 | ||
288 | typedef struct hashnode HASHNODE; | |
289 | ||
4eb191f3 NB |
290 | static HASHNODE *parse_assertion PARAMS ((const unsigned char *, |
291 | const unsigned char *, | |
292 | struct answer **, int)); | |
293 | static struct answer **find_answer PARAMS ((HASHNODE *, | |
294 | const struct answer *)); | |
295 | static int parse_answer PARAMS ((const unsigned char *, const unsigned char *, | |
296 | struct answer **, int)); | |
297 | static unsigned char *canonicalize_text PARAMS ((const unsigned char *, | |
298 | const unsigned char *, | |
299 | const unsigned char **)); | |
300 | ||
24c3c71a ZW |
301 | /* Some definitions for the hash table. The hash function MUST be |
302 | computed as shown in hashf () below. That is because the rescan | |
303 | loop computes the hash value `on the fly' for most tokens, | |
304 | in order to avoid the overhead of a lot of procedure calls to | |
305 | the hashf () function. Hashf () only exists for the sake of | |
306 | politeness, for use when speed isn't so important. */ | |
307 | ||
308 | #define HASHSIZE 1403 | |
309 | HASHNODE *hashtab[HASHSIZE]; | |
310 | #define HASHSTEP(old, c) ((old << 2) + c) | |
311 | #define MAKE_POS(v) (v & 0x7fffffff) /* make number positive */ | |
312 | ||
313 | /* `struct directive' defines one #-directive, including how to handle it. */ | |
314 | ||
315 | struct directive { | |
0b5826ac KG |
316 | const int length; /* Length of name */ |
317 | void (*const func) PARAMS ((U_CHAR *, U_CHAR *, FILE_BUF *)); | |
24c3c71a | 318 | /* Function to handle directive */ |
0b5826ac KG |
319 | const char *const name; /* Name of directive */ |
320 | const enum node_type type; /* Code which describes which directive. */ | |
24c3c71a ZW |
321 | }; |
322 | ||
323 | /* Last arg to output_line_command. */ | |
324 | enum file_change_code {same_file, enter_file, leave_file}; | |
325 | ||
326 | /* This structure represents one parsed argument in a macro call. | |
327 | `raw' points to the argument text as written (`raw_length' is its length). | |
328 | `expanded' points to the argument's macro-expansion | |
329 | (its length is `expand_length'). | |
330 | `stringified_length' is the length the argument would have | |
331 | if stringified. | |
332 | `free1' and `free2', if nonzero, point to blocks to be freed | |
333 | when the macro argument data is no longer needed. */ | |
334 | ||
335 | struct argdata { | |
336 | U_CHAR *raw, *expanded; | |
337 | int raw_length, expand_length; | |
338 | int stringified_length; | |
339 | U_CHAR *free1, *free2; | |
340 | char newlines; | |
341 | char comments; | |
342 | }; | |
343 | ||
344 | /* The arglist structure is built by do_define to tell | |
345 | collect_definition where the argument names begin. That | |
346 | is, for a define like "#define f(x,y,z) foo+x-bar*y", the arglist | |
347 | would contain pointers to the strings x, y, and z. | |
348 | Collect_definition would then build a DEFINITION node, | |
349 | with reflist nodes pointing to the places x, y, and z had | |
350 | appeared. So the arglist is just convenience data passed | |
351 | between these two routines. It is not kept around after | |
352 | the current #define has been processed and entered into the | |
353 | hash table. */ | |
354 | ||
355 | struct arglist { | |
356 | struct arglist *next; | |
357 | U_CHAR *name; | |
358 | int length; | |
359 | int argno; | |
360 | }; | |
361 | ||
362 | /* Function prototypes. */ | |
363 | ||
40bd4395 | 364 | static void do_define PARAMS ((U_CHAR *, U_CHAR *, FILE_BUF *)); |
e793c609 | 365 | static void do_error PARAMS ((U_CHAR *, U_CHAR *, FILE_BUF *)); |
3244472d | 366 | static void do_warning PARAMS ((U_CHAR *, U_CHAR *, FILE_BUF *)); |
40bd4395 NB |
367 | static void do_line PARAMS ((U_CHAR *, U_CHAR *, FILE_BUF *)); |
368 | static void do_include PARAMS ((U_CHAR *, U_CHAR *, FILE_BUF *)); | |
6d4587f7 | 369 | static void do_include_next PARAMS ((U_CHAR *, U_CHAR *, FILE_BUF *)); |
40bd4395 NB |
370 | static void do_undef PARAMS ((U_CHAR *, U_CHAR *, FILE_BUF *)); |
371 | static void do_if PARAMS ((U_CHAR *, U_CHAR *, FILE_BUF *)); | |
372 | static void do_ifdef PARAMS ((U_CHAR *, U_CHAR *, FILE_BUF *)); | |
373 | static void do_ifndef PARAMS ((U_CHAR *, U_CHAR *, FILE_BUF *)); | |
374 | static void do_else PARAMS ((U_CHAR *, U_CHAR *, FILE_BUF *)); | |
375 | static void do_elif PARAMS ((U_CHAR *, U_CHAR *, FILE_BUF *)); | |
376 | static void do_endif PARAMS ((U_CHAR *, U_CHAR *, FILE_BUF *)); | |
377 | static void do_assert PARAMS ((U_CHAR *, U_CHAR *, FILE_BUF *)); | |
378 | static void do_unassert PARAMS ((U_CHAR *, U_CHAR *, FILE_BUF *)); | |
379 | static void do_xifdef PARAMS ((U_CHAR *, U_CHAR *, enum node_type)); | |
0a8ad417 KG |
380 | |
381 | static struct hashnode *install PARAMS ((const U_CHAR *, int, enum node_type, int)); | |
382 | static int hashf PARAMS ((const U_CHAR *, int, int)); | |
383 | static int compare_defs PARAMS ((DEFINITION *, DEFINITION *)); | |
384 | static int comp_def_part PARAMS ((int, const U_CHAR *, int, | |
385 | const U_CHAR *, int, int)); | |
386 | static void delete_macro PARAMS ((HASHNODE *)); | |
24c3c71a ZW |
387 | |
388 | /* First arg to v_message. */ | |
79e2e160 | 389 | enum msgtype { MT_WARNING = 0, MT_ERROR, MT_FATAL }; |
0a8ad417 KG |
390 | static void v_message PARAMS ((enum msgtype mtype, int line, |
391 | const char *msgid, va_list ap)) | |
88f3c477 JM |
392 | ATTRIBUTE_PRINTF (3, 0); |
393 | ||
0a8ad417 | 394 | static int line_for_error PARAMS ((int)); |
24c3c71a ZW |
395 | |
396 | /* We know perfectly well which file this is, so we don't need to | |
397 | use __FILE__. */ | |
398 | #undef abort | |
399 | #if (GCC_VERSION >= 2007) | |
400 | #define abort() fancy_abort(__LINE__, __FUNCTION__) | |
401 | #else | |
402 | #define abort() fancy_abort(__LINE__, 0); | |
403 | #endif | |
404 | ||
0a8ad417 KG |
405 | static void macroexpand PARAMS ((HASHNODE *, FILE_BUF *)); |
406 | static void special_symbol PARAMS ((HASHNODE *, FILE_BUF *)); | |
407 | static void dump_all_macros PARAMS ((void)); | |
408 | static void dump_defn_1 PARAMS ((const U_CHAR *, int, int)); | |
409 | static void dump_arg_n PARAMS ((DEFINITION *, int)); | |
410 | static void conditional_skip PARAMS ((FILE_BUF *, int, enum node_type)); | |
411 | static void skip_if_group PARAMS ((FILE_BUF *, int)); | |
412 | static void output_line_command PARAMS ((FILE_BUF *, FILE_BUF *, | |
24c3c71a ZW |
413 | int, enum file_change_code)); |
414 | ||
0a8ad417 KG |
415 | static int eval_if_expression PARAMS ((const U_CHAR *, int)); |
416 | ||
2f638f96 | 417 | static void output_deps PARAMS ((void)); |
0a8ad417 | 418 | static void initialize_builtins PARAMS ((void)); |
40bd4395 NB |
419 | static void run_directive PARAMS ((const char *, size_t, |
420 | enum node_type)); | |
421 | static void make_definition PARAMS ((const char *)); | |
422 | static void make_undef PARAMS ((const char *)); | |
423 | static void make_assertion PARAMS ((const char *)); | |
0a8ad417 KG |
424 | |
425 | static void grow_outbuf PARAMS ((FILE_BUF *, int)); | |
426 | static int handle_directive PARAMS ((FILE_BUF *, FILE_BUF *)); | |
6d4587f7 ZW |
427 | static void process_include PARAMS ((struct file_name_list *, |
428 | const U_CHAR *, int, int, FILE_BUF *)); | |
8157303b | 429 | static void fixup_newlines PARAMS ((FILE_BUF *)); |
6d4587f7 ZW |
430 | static void finclude PARAMS ((int, const char *, |
431 | struct file_name_list *, FILE_BUF *)); | |
2f638f96 | 432 | static void init_dependency_output PARAMS ((void)); |
0a8ad417 KG |
433 | static void rescan PARAMS ((FILE_BUF *, int)); |
434 | static void newline_fix PARAMS ((U_CHAR *)); | |
435 | static void name_newline_fix PARAMS ((U_CHAR *)); | |
436 | static U_CHAR *macarg1 PARAMS ((U_CHAR *, const U_CHAR *, int *, | |
437 | int *, int *)); | |
438 | static const char *macarg PARAMS ((struct argdata *)); | |
439 | static int discard_comments PARAMS ((U_CHAR *, int, int)); | |
440 | static int file_size_and_mode PARAMS ((int, int *, long *)); | |
441 | ||
442 | static U_CHAR *skip_to_end_of_comment PARAMS ((FILE_BUF *, int *)); | |
443 | static U_CHAR *skip_quoted_string PARAMS ((const U_CHAR *, const U_CHAR *, | |
444 | int, int *, int *, int *)); | |
24c3c71a | 445 | |
24c3c71a ZW |
446 | int main PARAMS ((int, char **)); |
447 | ||
448 | /* Convenience. Write U"string" to get an unsigned string constant. */ | |
449 | #define U (const unsigned char *) | |
450 | ||
451 | /* Here is the actual list of #-directives, most-often-used first. */ | |
452 | ||
0b5826ac | 453 | static const struct directive directive_table[] = { |
24c3c71a ZW |
454 | { 6, do_define, "define", T_DEFINE }, |
455 | { 7, do_include, "include", T_INCLUDE }, | |
456 | { 5, do_endif, "endif", T_ENDIF }, | |
40bd4395 | 457 | { 5, do_ifdef, "ifdef", T_IFDEF }, |
24c3c71a ZW |
458 | { 2, do_if, "if", T_IF, }, |
459 | { 4, do_else, "else", T_ELSE }, | |
40bd4395 | 460 | { 6, do_ifndef, "ifndef", T_IFNDEF }, |
24c3c71a ZW |
461 | { 5, do_undef, "undef", T_UNDEF }, |
462 | { 4, do_line, "line", T_LINE }, | |
463 | { 4, do_elif, "elif", T_ELIF }, | |
3244472d NB |
464 | { 5, do_error, "error", T_ERROR }, |
465 | { 7, do_warning, "warning", T_WARNING }, | |
6d4587f7 | 466 | { 12, do_include_next, "include_next", T_INCLUDE_NEXT }, |
40bd4395 NB |
467 | { 6, do_assert, "assert", T_ASSERT }, |
468 | { 8, do_unassert,"unassert",T_UNASSERT}, | |
24c3c71a ZW |
469 | { -1, 0, "", T_UNUSED}, |
470 | }; | |
471 | ||
f6bbde28 ZW |
472 | #define SKIP_WHITE_SPACE(p) do { while (is_nvspace(*p)) p++; } while (0) |
473 | #define SKIP_ALL_WHITE_SPACE(p) do { while (is_space(*p)) p++; } while (0) | |
24c3c71a ZW |
474 | |
475 | int errors = 0; /* Error counter for exit code */ | |
476 | ||
0a8ad417 KG |
477 | static FILE_BUF expand_to_temp_buffer PARAMS ((const U_CHAR *, const U_CHAR *, int)); |
478 | static DEFINITION *collect_expansion PARAMS ((U_CHAR *, U_CHAR *, int, | |
479 | struct arglist *)); | |
24c3c71a ZW |
480 | |
481 | /* Stack of conditionals currently in progress | |
482 | (including both successful and failing conditionals). */ | |
483 | ||
484 | struct if_stack { | |
485 | struct if_stack *next; /* for chaining to the next stack frame */ | |
486 | const char *fname; /* copied from input when frame is made */ | |
487 | int lineno; /* similarly */ | |
488 | int if_succeeded; /* true if a leg of this if-group | |
489 | has been passed through rescan */ | |
490 | enum node_type type; /* type of last directive seen in this group */ | |
491 | }; | |
492 | typedef struct if_stack IF_STACK_FRAME; | |
493 | IF_STACK_FRAME *if_stack = NULL; | |
494 | ||
24c3c71a ZW |
495 | /* Nonzero means -I- has been seen, |
496 | so don't look for #include "foo" the source-file directory. */ | |
497 | int ignore_srcdir; | |
24c3c71a | 498 | |
fb6a7b08 NB |
499 | /* Pending directives. */ |
500 | enum pending_dir_t {PD_NONE = 0, PD_DEFINE, PD_UNDEF, PD_ASSERTION, PD_FILE}; | |
501 | ||
502 | typedef struct pending_dir pending_dir; | |
503 | struct pending_dir | |
504 | { | |
505 | const char *arg; | |
506 | enum pending_dir_t type; | |
507 | }; | |
508 | ||
24c3c71a ZW |
509 | int |
510 | main (argc, argv) | |
511 | int argc; | |
512 | char **argv; | |
513 | { | |
514 | int st_mode; | |
515 | long st_size; | |
516 | const char *in_fname, *out_fname; | |
517 | int f, i; | |
518 | FILE_BUF *fp; | |
fb6a7b08 | 519 | pending_dir *pend = (pending_dir *) xcalloc (argc, sizeof (pending_dir)); |
24c3c71a ZW |
520 | int no_standard_includes = 0; |
521 | ||
faf31866 KG |
522 | hex_init (); |
523 | ||
24c3c71a ZW |
524 | #ifdef RLIMIT_STACK |
525 | /* Get rid of any avoidable limit on stack size. */ | |
526 | { | |
527 | struct rlimit rlim; | |
528 | ||
529 | /* Set the stack limit huge so that alloca (particularly stringtab | |
530 | * in dbxread.c) does not fail. */ | |
531 | getrlimit (RLIMIT_STACK, &rlim); | |
532 | rlim.rlim_cur = rlim.rlim_max; | |
533 | setrlimit (RLIMIT_STACK, &rlim); | |
534 | } | |
535 | #endif /* RLIMIT_STACK defined */ | |
536 | ||
537 | progname = argv[0]; | |
538 | ||
539 | in_fname = NULL; | |
540 | out_fname = NULL; | |
541 | ||
24c3c71a ZW |
542 | no_line_commands = 0; |
543 | dump_macros = 0; | |
544 | no_output = 0; | |
545 | ||
24c3c71a ZW |
546 | max_include_len = cpp_GCC_INCLUDE_DIR_len + 7; /* ??? */ |
547 | ||
79e2e160 ZW |
548 | gcc_init_libintl (); |
549 | ||
2f638f96 NB |
550 | /* It's simplest to just create this struct whether or not it will |
551 | be needed. */ | |
552 | deps = deps_init (); | |
553 | ||
24c3c71a ZW |
554 | /* Process switches and find input file name. */ |
555 | ||
556 | for (i = 1; i < argc; i++) { | |
557 | if (argv[i][0] != '-') { | |
558 | if (out_fname != NULL) | |
1f978f5f | 559 | fatal ("usage: %s [switches] input output", argv[0]); |
24c3c71a ZW |
560 | else if (in_fname != NULL) |
561 | out_fname = argv[i]; | |
562 | else | |
563 | in_fname = argv[i]; | |
564 | } else { | |
fb6a7b08 NB |
565 | int c = argv[i][1]; |
566 | ||
567 | switch (c) { | |
24c3c71a ZW |
568 | case 'E': |
569 | case '$': | |
24c3c71a ZW |
570 | break; /* Ignore for compatibility with ISO/extended cpp. */ |
571 | ||
572 | case 'l': | |
573 | if (!strcmp (argv[i], "-lang-c++") | |
574 | || !strcmp (argv[i], "-lang-objc++")) | |
575 | fatal ("-traditional is not supported in C++"); | |
576 | else if (!strcmp (argv[i], "-lang-c89")) | |
577 | fatal ("-traditional and -ansi are mutually exclusive"); | |
578 | else if (!strcmp (argv[i], "-lang-objc")) | |
fb6a7b08 | 579 | pend[i].type = PD_DEFINE, pend[i].arg = "__OBJC__"; |
24c3c71a | 580 | else if (!strcmp (argv[i], "-lang-asm")) |
fb6a7b08 | 581 | pend[i].type = PD_DEFINE, pend[i].arg = "__ASSEMBLER__"; |
24c3c71a | 582 | else if (!strcmp (argv[i], "-lang-fortran")) |
fb6a7b08 | 583 | pend[i].type = PD_DEFINE, pend[i].arg = "_LANGUAGE_FORTRAN"; |
24c3c71a ZW |
584 | /* All other possibilities ignored. */ |
585 | break; | |
586 | ||
587 | case 'i': | |
588 | if (!strcmp (argv[i], "-include")) | |
589 | { | |
590 | if (i + 1 == argc) | |
1f978f5f | 591 | fatal ("filename missing after -i option"); |
24c3c71a | 592 | else |
fb6a7b08 | 593 | pend[i].type = PD_FILE, pend[i].arg = argv[i + 1], i++; |
24c3c71a ZW |
594 | } |
595 | else if (!strcmp (argv[i], "-iprefix")) | |
596 | i++; /* Ignore for compatibility */ | |
597 | else if (!strcmp (argv[i], "-isystem") | |
598 | || !strcmp (argv[i], "-iwithprefix") | |
599 | || !strcmp (argv[i], "-iwithprefixbefore") | |
600 | || !strcmp (argv[i], "-idirafter")) | |
5c7525ac | 601 | goto add_include; /* best we can do */ |
24c3c71a ZW |
602 | |
603 | break; | |
604 | ||
605 | case 'o': | |
606 | if (out_fname != NULL) | |
1f978f5f | 607 | fatal ("output filename specified twice"); |
24c3c71a | 608 | if (i + 1 == argc) |
1f978f5f | 609 | fatal ("filename missing after -o option"); |
24c3c71a ZW |
610 | out_fname = argv[++i]; |
611 | if (!strcmp (out_fname, "-")) | |
612 | out_fname = ""; | |
613 | break; | |
614 | ||
615 | case 'w': | |
616 | inhibit_warnings = 1; | |
617 | break; | |
618 | ||
619 | case 'W': | |
620 | if (!strcmp (argv[i], "-Wcomments")) | |
621 | warn_comments = 1; | |
622 | else if (!strcmp (argv[i], "-Wcomment")) | |
623 | warn_comments = 1; | |
624 | else if (!strcmp (argv[i], "-Wall")) { | |
625 | warn_comments = 1; | |
626 | } | |
627 | break; | |
628 | ||
629 | case 'f': | |
5279d739 | 630 | if (!strcmp (argv[i], "-fsigned-char")) |
0fef3fd0 NB |
631 | flag_signed_char = 1; |
632 | else if (!strcmp (argv[i], "-funsigned-char")) | |
633 | flag_signed_char = 0; | |
24c3c71a ZW |
634 | break; |
635 | ||
8c883bff | 636 | /* Ignore target-specific and optimization flags. */ |
e5f5feea | 637 | case 'm': |
8c883bff | 638 | case 'O': |
e5f5feea NB |
639 | break; |
640 | ||
24c3c71a | 641 | case 'M': |
c3843cea JJ |
642 | { |
643 | char *p = NULL; | |
644 | ||
2f638f96 NB |
645 | /* -MD and -MMD for tradcpp are deprecated and undocumented |
646 | (use -M or -MM with -MF instead), and probably should be | |
647 | removed with the next major GCC version. For the moment | |
648 | we allow these for the benefit of Automake 1.4, which | |
649 | uses these when dependency tracking is enabled. Automake | |
650 | 1.5 will fix this. */ | |
c3843cea JJ |
651 | if (!strncmp (argv[i], "-MD", 3)) { |
652 | p = argv[i] + 3; | |
653 | print_deps = 2; | |
654 | } else if (!strncmp (argv[i], "-MMD", 4)) { | |
655 | p = argv[i] + 4; | |
656 | print_deps = 1; | |
657 | } else if (!strcmp (argv[i], "-M")) { | |
658 | print_deps = 2; | |
c3843cea JJ |
659 | } else if (!strcmp (argv[i], "-MM")) { |
660 | print_deps = 1; | |
2f638f96 NB |
661 | } else if (!strcmp (argv[i], "-MG")) { |
662 | deps_missing_files = 1; | |
663 | } else if (!strcmp (argv[i], "-MF")) { | |
664 | p = argv[i] + 3; | |
665 | } else if (!strcmp (argv[i], "-MP")) { | |
666 | print_deps_phony_targets = 1; | |
667 | } else if (!strcmp (argv[i], "-MQ") || !strcmp (argv[i], "-MT")) { | |
668 | /* Add a target. -MQ quotes for Make. */ | |
669 | const char *tgt = argv[i] + 3; | |
670 | int quoted = argv[i][2] == 'Q'; | |
671 | ||
672 | if (*tgt == '\0' && i + 1 == argc) | |
1f978f5f | 673 | fatal ("target missing after %s option", argv[i]); |
2f638f96 NB |
674 | else |
675 | { | |
676 | if (*tgt == '\0') | |
677 | tgt = argv[++i]; | |
678 | ||
679 | deps_add_target (deps, tgt, quoted); | |
680 | } | |
681 | } | |
682 | ||
c3843cea JJ |
683 | if (p) { |
684 | if (*p) | |
685 | deps_file = p; | |
686 | else if (i + 1 == argc) | |
1f978f5f | 687 | fatal ("filename missing after %s option", argv[i]); |
c3843cea JJ |
688 | else |
689 | deps_file = argv[++i]; | |
690 | } | |
691 | } | |
24c3c71a ZW |
692 | break; |
693 | ||
694 | case 'd': | |
695 | dump_macros = 1; | |
696 | no_output = 1; | |
697 | break; | |
698 | ||
699 | case 'v': | |
700 | fprintf (stderr, "GNU traditional CPP version %s\n", version_string); | |
701 | break; | |
702 | ||
703 | case 'D': | |
fb6a7b08 | 704 | case 'U': |
40bd4395 | 705 | case 'A': |
24c3c71a | 706 | { |
1e18a243 | 707 | char *p; |
24c3c71a ZW |
708 | |
709 | if (argv[i][2] != 0) | |
710 | p = argv[i] + 2; | |
711 | else if (i + 1 == argc) | |
1f978f5f | 712 | fatal ("macro name missing after -%c option", c); |
24c3c71a ZW |
713 | else |
714 | p = argv[++i]; | |
715 | ||
40bd4395 NB |
716 | if (c == 'D') |
717 | pend[i].type = PD_DEFINE; | |
718 | else if (c == 'U') | |
719 | pend[i].type = PD_UNDEF; | |
720 | else | |
721 | pend[i].type = PD_ASSERTION; | |
fb6a7b08 | 722 | pend[i].arg = p; |
24c3c71a ZW |
723 | } |
724 | break; | |
725 | ||
24c3c71a ZW |
726 | case 'C': |
727 | put_out_comments = 1; | |
728 | break; | |
729 | ||
730 | case 'p': | |
731 | if (!strcmp (argv[i], "-pedantic")) | |
732 | fatal ("-pedantic and -traditional are mutually exclusive"); | |
733 | break; | |
734 | ||
735 | case 't': | |
736 | if (!strcmp (argv[i], "-trigraphs")) | |
737 | fatal ("-trigraphs and -traditional are mutually exclusive"); | |
738 | break; | |
739 | ||
740 | case 'P': | |
741 | no_line_commands = 1; | |
742 | break; | |
743 | ||
744 | case 'I': /* Add directory to path for includes. */ | |
5c7525ac | 745 | add_include: |
24c3c71a ZW |
746 | { |
747 | struct file_name_list *dirtmp; | |
748 | ||
749 | if (! ignore_srcdir && !strcmp (argv[i] + 2, "-")) | |
750 | ignore_srcdir = 1; | |
751 | else { | |
752 | dirtmp = (struct file_name_list *) | |
753 | xmalloc (sizeof (struct file_name_list)); | |
754 | dirtmp->next = 0; /* New one goes on the end */ | |
755 | if (include == 0) | |
756 | include = dirtmp; | |
757 | else | |
758 | last_include->next = dirtmp; | |
759 | last_include = dirtmp; /* Tail follows the last one */ | |
760 | if (argv[i][1] == 'I' && argv[i][2] != 0) | |
761 | dirtmp->fname = argv[i] + 2; | |
762 | else if (i + 1 == argc) | |
1f978f5f | 763 | fatal ("directory name missing after -I option"); |
24c3c71a ZW |
764 | else |
765 | dirtmp->fname = argv[++i]; | |
766 | if (strlen (dirtmp->fname) > max_include_len) | |
767 | max_include_len = strlen (dirtmp->fname); | |
768 | if (ignore_srcdir && first_bracket_include == 0) | |
769 | first_bracket_include = dirtmp; | |
770 | } | |
771 | } | |
772 | break; | |
773 | ||
774 | case 'n': | |
775 | /* -nostdinc causes no default include directories. | |
776 | You must specify all include-file directories with -I. */ | |
777 | no_standard_includes = 1; | |
778 | break; | |
779 | ||
aaf93206 NB |
780 | case 'q': |
781 | /* Accept -quiet silently. */ | |
782 | break; | |
783 | ||
24c3c71a ZW |
784 | case '\0': /* JF handle '-' as file name meaning stdin or stdout */ |
785 | if (in_fname == NULL) { | |
786 | in_fname = ""; | |
787 | break; | |
788 | } else if (out_fname == NULL) { | |
789 | out_fname = ""; | |
790 | break; | |
791 | } /* else fall through into error */ | |
792 | ||
793 | default: | |
1f978f5f | 794 | fatal ("invalid option `%s'", argv[i]); |
24c3c71a ZW |
795 | } |
796 | } | |
797 | } | |
798 | ||
2f638f96 NB |
799 | init_dependency_output (); |
800 | ||
801 | /* After checking the environment variables, check if -M or -MM has | |
802 | not been specified, but other -M options have. */ | |
803 | if (print_deps == 0 | |
804 | && (deps_missing_files || deps_file || print_deps_phony_targets)) | |
805 | fatal ("you must additionally specify either -M or -MM"); | |
c3843cea | 806 | |
2f638f96 NB |
807 | if (print_deps) |
808 | { | |
809 | /* Set the default target (if there is none already), and | |
810 | the dependency on the main file. */ | |
811 | deps_add_default_target (deps, in_fname); | |
812 | ||
813 | deps_add_dep (deps, in_fname); | |
814 | } | |
815 | ||
f6bbde28 | 816 | /* Install __LINE__, etc. Must follow option processing. */ |
24c3c71a ZW |
817 | initialize_builtins (); |
818 | ||
2c8f0515 | 819 | /* Do defines specified with -D and undefines specified with -U. */ |
24c3c71a | 820 | for (i = 1; i < argc; i++) |
fb6a7b08 | 821 | if (pend[i].type == PD_DEFINE) |
40bd4395 | 822 | make_definition (pend[i].arg); |
fb6a7b08 | 823 | else if (pend[i].type == PD_UNDEF) |
40bd4395 NB |
824 | make_undef (pend[i].arg); |
825 | else if (pend[i].type == PD_ASSERTION) | |
826 | make_assertion (pend[i].arg); | |
24c3c71a ZW |
827 | |
828 | /* Unless -fnostdinc, | |
829 | tack on the standard include file dirs to the specified list */ | |
830 | if (!no_standard_includes) { | |
831 | const struct default_include *di; | |
832 | struct file_name_list *old_last_include = last_include; | |
833 | struct file_name_list *dirtmp; | |
834 | for (di = cpp_include_defaults; di->fname; di++) { | |
835 | if (di->cplusplus) | |
836 | continue; | |
837 | dirtmp = (struct file_name_list *) | |
838 | xmalloc (sizeof (struct file_name_list)); | |
839 | dirtmp->next = 0; /* New one goes on the end */ | |
840 | if (include == 0) | |
841 | include = dirtmp; | |
842 | else | |
843 | last_include->next = dirtmp; | |
844 | last_include = dirtmp; /* Tail follows the last one */ | |
845 | dirtmp->fname = di->fname; | |
9512cb3f JJ |
846 | if (strlen (dirtmp->fname) > max_include_len) |
847 | max_include_len = strlen (dirtmp->fname); | |
24c3c71a ZW |
848 | } |
849 | ||
850 | if (ignore_srcdir && first_bracket_include == 0) | |
851 | first_bracket_include = old_last_include->next; | |
852 | } | |
853 | ||
854 | /* Initialize output buffer */ | |
855 | ||
856 | outbuf.buf = (U_CHAR *) xmalloc (OUTBUF_SIZE); | |
857 | outbuf.bufp = outbuf.buf; | |
858 | outbuf.length = OUTBUF_SIZE; | |
859 | ||
860 | /* Scan the -i files before the main input. | |
861 | Much like #including them, but with no_output set | |
862 | so that only their macro definitions matter. */ | |
863 | ||
864 | no_output++; | |
fa5db828 | 865 | indepth++; |
24c3c71a | 866 | for (i = 1; i < argc; i++) |
fb6a7b08 NB |
867 | if (pend[i].type == PD_FILE) |
868 | { | |
869 | int fd = open (pend[i].arg, O_RDONLY, 0666); | |
870 | if (fd < 0) | |
871 | { | |
872 | perror_with_name (pend[i].arg); | |
873 | return FATAL_EXIT_CODE; | |
874 | } | |
7855db7c JJ |
875 | |
876 | /* For -M, add this file to the dependencies. */ | |
877 | if (print_deps) | |
878 | deps_add_dep (deps, pend[i].arg); | |
879 | ||
6d4587f7 | 880 | finclude (fd, pend[i].arg, 0, &outbuf); |
24c3c71a | 881 | } |
fa5db828 | 882 | indepth--; |
24c3c71a ZW |
883 | no_output--; |
884 | ||
fb6a7b08 NB |
885 | /* Pending directives no longer needed. */ |
886 | free ((PTR) pend); | |
887 | ||
24c3c71a ZW |
888 | /* Create an input stack level for the main input file |
889 | and copy the entire contents of the file into it. */ | |
890 | ||
891 | fp = &instack[++indepth]; | |
892 | ||
893 | /* JF check for stdin */ | |
894 | if (in_fname == NULL || *in_fname == 0) { | |
895 | in_fname = ""; | |
896 | f = 0; | |
897 | } else if ((f = open (in_fname, O_RDONLY, 0666)) < 0) | |
6e993bdb | 898 | goto sys_error; |
24c3c71a | 899 | |
24c3c71a | 900 | if (file_size_and_mode (f, &st_mode, &st_size)) |
6e993bdb | 901 | goto sys_error; |
24c3c71a ZW |
902 | fp->fname = in_fname; |
903 | fp->lineno = 1; | |
904 | /* JF all this is mine about reading pipes and ttys */ | |
905 | if (!S_ISREG (st_mode)) { | |
906 | /* Read input from a file that is not a normal disk file. | |
907 | We cannot preallocate a buffer with the correct size, | |
908 | so we must read in the file a piece at the time and make it bigger. */ | |
909 | int size; | |
910 | int bsize; | |
911 | int cnt; | |
912 | U_CHAR *bufp; | |
913 | ||
914 | bsize = 2000; | |
915 | size = 0; | |
916 | fp->buf = (U_CHAR *) xmalloc (bsize + 2); | |
917 | bufp = fp->buf; | |
918 | for (;;) { | |
919 | cnt = read (f, bufp, bsize - size); | |
6e993bdb ZW |
920 | if (cnt < 0) goto sys_error; /* error! */ |
921 | if (cnt == 0) break; /* End of file */ | |
24c3c71a ZW |
922 | size += cnt; |
923 | bufp += cnt; | |
6e993bdb | 924 | if (bsize == size) { /* Buffer is full! */ |
24c3c71a ZW |
925 | bsize *= 2; |
926 | fp->buf = (U_CHAR *) xrealloc (fp->buf, bsize + 2); | |
927 | bufp = fp->buf + size; /* May have moved */ | |
928 | } | |
929 | } | |
930 | fp->length = size; | |
931 | } else { | |
932 | /* Read a file whose size we can determine in advance. | |
933 | For the sake of VMS, st_size is just an upper bound. */ | |
934 | long i; | |
935 | fp->length = 0; | |
936 | fp->buf = (U_CHAR *) xmalloc (st_size + 2); | |
937 | ||
938 | while (st_size > 0) { | |
939 | i = read (f, fp->buf + fp->length, st_size); | |
940 | if (i <= 0) { | |
941 | if (i == 0) break; | |
6e993bdb | 942 | goto sys_error; |
24c3c71a ZW |
943 | } |
944 | fp->length += i; | |
945 | st_size -= i; | |
946 | } | |
947 | } | |
948 | fp->bufp = fp->buf; | |
949 | fp->if_stack = if_stack; | |
8157303b | 950 | fixup_newlines (fp); |
24c3c71a ZW |
951 | |
952 | /* Make sure data ends with a newline. And put a null after it. */ | |
953 | ||
954 | if (fp->length > 0 && fp->buf[fp->length-1] != '\n') | |
955 | fp->buf[fp->length++] = '\n'; | |
956 | fp->buf[fp->length] = '\0'; | |
957 | ||
958 | /* Now that we know the input file is valid, open the output. */ | |
959 | ||
960 | if (!out_fname || !strcmp (out_fname, "")) | |
961 | out_fname = "stdout"; | |
962 | else if (! freopen (out_fname, "w", stdout)) | |
963 | pfatal_with_name (out_fname); | |
964 | ||
965 | output_line_command (fp, &outbuf, 0, same_file); | |
966 | ||
967 | /* Scan the input, processing macros and directives. */ | |
968 | ||
969 | rescan (&outbuf, 0); | |
970 | ||
971 | /* Now we have processed the entire input | |
972 | Write whichever kind of output has been requested. */ | |
973 | ||
974 | ||
975 | if (dump_macros) | |
976 | dump_all_macros (); | |
2f638f96 | 977 | else if (! inhibit_output) |
24c3c71a ZW |
978 | if (write (fileno (stdout), outbuf.buf, outbuf.bufp - outbuf.buf) < 0) |
979 | fatal ("I/O error on output"); | |
24c3c71a | 980 | |
2f638f96 NB |
981 | /* Don't write the deps file if preprocessing has failed. */ |
982 | if (print_deps && errors == 0) | |
983 | output_deps (); | |
984 | ||
985 | /* Destruct the deps object. */ | |
986 | deps_free (deps); | |
24c3c71a ZW |
987 | |
988 | if (ferror (stdout)) | |
989 | fatal ("I/O error on output"); | |
990 | ||
991 | if (errors) | |
992 | exit (FATAL_EXIT_CODE); | |
993 | exit (SUCCESS_EXIT_CODE); | |
994 | ||
6e993bdb | 995 | sys_error: |
24c3c71a ZW |
996 | pfatal_with_name (in_fname); |
997 | } | |
998 | ||
2f638f96 NB |
999 | /* Set up dependency-file output. */ |
1000 | static void | |
1001 | init_dependency_output () | |
1002 | { | |
1003 | char *spec, *s, *output_file; | |
1004 | ||
1005 | /* Either of two environment variables can specify output of deps. | |
1006 | Its value is either "OUTPUT_FILE" or "OUTPUT_FILE DEPS_TARGET", | |
1007 | where OUTPUT_FILE is the file to write deps info to | |
1008 | and DEPS_TARGET is the target to mention in the deps. */ | |
1009 | ||
1010 | if (print_deps == 0) | |
1011 | { | |
1012 | spec = getenv ("DEPENDENCIES_OUTPUT"); | |
1013 | if (spec) | |
1014 | print_deps = 1; | |
1015 | else | |
1016 | { | |
1017 | spec = getenv ("SUNPRO_DEPENDENCIES"); | |
1018 | if (spec) | |
1019 | print_deps = 2; | |
1020 | else | |
1021 | return; | |
1022 | } | |
1023 | ||
1024 | /* Find the space before the DEPS_TARGET, if there is one. */ | |
1025 | s = strchr (spec, ' '); | |
1026 | if (s) | |
1027 | { | |
1028 | /* Let the caller perform MAKE quoting. */ | |
1029 | deps_add_target (deps, s + 1, 0); | |
1030 | output_file = (char *) xmalloc (s - spec + 1); | |
1031 | memcpy (output_file, spec, s - spec); | |
1032 | output_file[s - spec] = 0; | |
1033 | } | |
1034 | else | |
1035 | output_file = spec; | |
1036 | ||
1037 | /* Command line overrides environment variables. */ | |
1038 | if (deps_file == 0) | |
1039 | deps_file = output_file; | |
1040 | deps_append = 1; | |
1041 | } | |
1042 | ||
1043 | /* If dependencies go to standard output, or -MG is used, we should | |
1044 | suppress output. The user may be requesting other stuff to | |
1045 | stdout, with -dM, -v etc. We let them shoot themselves in the | |
1046 | foot. */ | |
1047 | if (deps_file == 0 || deps_missing_files) | |
1048 | inhibit_output = 1; | |
1049 | } | |
1050 | ||
1051 | /* Use mkdeps.c to output dependency information. */ | |
1052 | static void | |
1053 | output_deps () | |
1054 | { | |
1055 | /* Stream on which to print the dependency information. */ | |
1056 | FILE *deps_stream = 0; | |
27c38fbe | 1057 | const char *const deps_mode = deps_append ? "a" : "w"; |
2f638f96 NB |
1058 | |
1059 | if (deps_file == 0) | |
1060 | deps_stream = stdout; | |
1061 | else | |
1062 | { | |
1063 | deps_stream = fopen (deps_file, deps_mode); | |
1064 | if (deps_stream == 0) | |
1065 | { | |
1066 | error_from_errno (deps_file); | |
1067 | return; | |
1068 | } | |
1069 | } | |
1070 | ||
1071 | deps_write (deps, deps_stream, 72); | |
1072 | ||
1073 | if (print_deps_phony_targets) | |
1074 | deps_phony_targets (deps, deps_stream); | |
1075 | ||
1076 | /* Don't close stdout. */ | |
1077 | if (deps_file) | |
1078 | { | |
1079 | if (ferror (deps_stream) || fclose (deps_stream) != 0) | |
1080 | fatal ("I/O error on output"); | |
1081 | } | |
1082 | } | |
1083 | ||
24c3c71a ZW |
1084 | /* Move all backslash-newline pairs out of embarrassing places. |
1085 | Exchange all such pairs following BP | |
1086 | with any potentially-embarrasing characters that follow them. | |
1087 | Potentially-embarrassing characters are / and * | |
1088 | (because a backslash-newline inside a comment delimiter | |
1089 | would cause it not to be recognized). */ | |
0a8ad417 | 1090 | static void |
24c3c71a ZW |
1091 | newline_fix (bp) |
1092 | U_CHAR *bp; | |
1093 | { | |
b3694847 SS |
1094 | U_CHAR *p = bp; |
1095 | int count = 0; | |
24c3c71a ZW |
1096 | |
1097 | /* First count the backslash-newline pairs here. */ | |
1098 | ||
1099 | while (*p++ == '\\' && *p++ == '\n') | |
1100 | count++; | |
1101 | ||
1102 | p = bp + count * 2; | |
1103 | ||
1104 | /* Exit if what follows the backslash-newlines is not embarrassing. */ | |
1105 | ||
1106 | if (count == 0 || (*p != '/' && *p != '*')) | |
1107 | return; | |
1108 | ||
1109 | /* Copy all potentially embarrassing characters | |
1110 | that follow the backslash-newline pairs | |
1111 | down to where the pairs originally started. */ | |
1112 | ||
1113 | while (*p == '*' || *p == '/') | |
1114 | *bp++ = *p++; | |
1115 | ||
1116 | /* Now write the same number of pairs after the embarrassing chars. */ | |
1117 | while (count-- > 0) { | |
1118 | *bp++ = '\\'; | |
1119 | *bp++ = '\n'; | |
1120 | } | |
1121 | } | |
1122 | ||
1123 | /* Like newline_fix but for use within a directive-name. | |
1124 | Move any backslash-newlines up past any following symbol constituents. */ | |
0a8ad417 | 1125 | static void |
24c3c71a ZW |
1126 | name_newline_fix (bp) |
1127 | U_CHAR *bp; | |
1128 | { | |
b3694847 SS |
1129 | U_CHAR *p = bp; |
1130 | int count = 0; | |
24c3c71a ZW |
1131 | |
1132 | /* First count the backslash-newline pairs here. */ | |
1133 | ||
1134 | while (*p++ == '\\' && *p++ == '\n') | |
1135 | count++; | |
1136 | ||
1137 | p = bp + count * 2; | |
1138 | ||
1139 | /* What follows the backslash-newlines is not embarrassing. */ | |
1140 | ||
f6bbde28 | 1141 | if (count == 0 || !is_idchar (*p)) |
24c3c71a ZW |
1142 | return; |
1143 | ||
1144 | /* Copy all potentially embarrassing characters | |
1145 | that follow the backslash-newline pairs | |
1146 | down to where the pairs originally started. */ | |
1147 | ||
f6bbde28 | 1148 | while (is_idchar (*p)) |
24c3c71a ZW |
1149 | *bp++ = *p++; |
1150 | ||
1151 | /* Now write the same number of pairs after the embarrassing chars. */ | |
1152 | while (count-- > 0) { | |
1153 | *bp++ = '\\'; | |
1154 | *bp++ = '\n'; | |
1155 | } | |
1156 | } | |
1157 | \f | |
1158 | /* | |
1159 | * The main loop of the program. | |
1160 | * | |
1161 | * Read characters from the input stack, transferring them to the | |
1162 | * output buffer OP. | |
1163 | * | |
1164 | * Macros are expanded and push levels on the input stack. | |
1165 | * At the end of such a level it is popped off and we keep reading. | |
1166 | * At the end of any other kind of level, we return. | |
1167 | * #-directives are handled, except within macros. | |
1168 | * | |
1169 | * If OUTPUT_MARKS is nonzero, keep Newline markers found in the input | |
1170 | * and insert them when appropriate. This is set while scanning macro | |
1171 | * arguments before substitution. It is zero when scanning for final output. | |
1172 | * There are three types of Newline markers: | |
1173 | * * Newline - follows a macro name that was not expanded | |
1174 | * because it appeared inside an expansion of the same macro. | |
1175 | * This marker prevents future expansion of that identifier. | |
1176 | * When the input is rescanned into the final output, these are deleted. | |
1177 | * These are also deleted by ## concatenation. | |
1178 | * * Newline Space (or Newline and any other whitespace character) | |
1179 | * stands for a place that tokens must be separated or whitespace | |
1180 | * is otherwise desirable, but where the ANSI standard specifies there | |
1181 | * is no whitespace. This marker turns into a Space (or whichever other | |
1182 | * whitespace char appears in the marker) in the final output, | |
1183 | * but it turns into nothing in an argument that is stringified with #. | |
1184 | * Such stringified arguments are the only place where the ANSI standard | |
1185 | * specifies with precision that whitespace may not appear. | |
1186 | * | |
1187 | * During this function, IP->bufp is kept cached in IBP for speed of access. | |
1188 | * Likewise, OP->bufp is kept in OBP. Before calling a subroutine | |
1189 | * IBP, IP and OBP must be copied back to memory. IP and IBP are | |
1190 | * copied back with the RECACHE macro. OBP must be copied back from OP->bufp | |
1191 | * explicitly, and before RECACHE, since RECACHE uses OBP. | |
1192 | */ | |
1193 | ||
0a8ad417 | 1194 | static void |
24c3c71a ZW |
1195 | rescan (op, output_marks) |
1196 | FILE_BUF *op; | |
1197 | int output_marks; | |
1198 | { | |
1199 | /* Character being scanned in main loop. */ | |
b3694847 | 1200 | U_CHAR c; |
24c3c71a ZW |
1201 | |
1202 | /* Length of pending accumulated identifier. */ | |
b3694847 | 1203 | int ident_length = 0; |
24c3c71a ZW |
1204 | |
1205 | /* Hash code of pending accumulated identifier. */ | |
b3694847 | 1206 | int hash = 0; |
24c3c71a ZW |
1207 | |
1208 | /* Current input level (&instack[indepth]). */ | |
1209 | FILE_BUF *ip; | |
1210 | ||
1211 | /* Pointer for scanning input. */ | |
b3694847 | 1212 | U_CHAR *ibp; |
24c3c71a ZW |
1213 | |
1214 | /* Pointer to end of input. End of scan is controlled by LIMIT. */ | |
b3694847 | 1215 | U_CHAR *limit; |
24c3c71a ZW |
1216 | |
1217 | /* Pointer for storing output. */ | |
b3694847 | 1218 | U_CHAR *obp; |
24c3c71a ZW |
1219 | |
1220 | /* REDO_CHAR is nonzero if we are processing an identifier | |
1221 | after backing up over the terminating character. | |
1222 | Sometimes we process an identifier without backing up over | |
1223 | the terminating character, if the terminating character | |
1224 | is not special. Backing up is done so that the terminating character | |
1225 | will be dispatched on again once the identifier is dealt with. */ | |
1226 | int redo_char = 0; | |
1227 | ||
1228 | /* 1 if within an identifier inside of which a concatenation | |
1229 | marker (Newline -) has been seen. */ | |
1230 | int concatenated = 0; | |
1231 | ||
1232 | /* While scanning a comment or a string constant, | |
1233 | this records the line it started on, for error messages. */ | |
1234 | int start_line; | |
1235 | ||
1236 | /* Record position of last `real' newline. */ | |
1237 | U_CHAR *beg_of_line; | |
1238 | ||
c1a6a8dc SS |
1239 | /* This has to be a global bacause of RECACHE. */ |
1240 | U_CHAR *obufp_before_macroname = NULL; | |
1241 | ||
24c3c71a ZW |
1242 | /* Pop the innermost input stack level, assuming it is a macro expansion. */ |
1243 | ||
1244 | #define POPMACRO \ | |
1245 | do { ip->macro->type = T_MACRO; \ | |
1246 | if (ip->free_ptr) free (ip->free_ptr); \ | |
1247 | --indepth; } while (0) | |
1248 | ||
1249 | /* Reload `rescan's local variables that describe the current | |
1250 | level of the input stack. */ | |
1251 | ||
1252 | #define RECACHE \ | |
1253 | do { ip = &instack[indepth]; \ | |
1254 | ibp = ip->bufp; \ | |
1255 | limit = ip->buf + ip->length; \ | |
1256 | op->bufp = obp; \ | |
1257 | check_expand (op, limit - ibp); \ | |
1258 | beg_of_line = 0; \ | |
c1a6a8dc | 1259 | obufp_before_macroname += op->bufp - obp; \ |
24c3c71a ZW |
1260 | obp = op->bufp; } while (0) |
1261 | ||
1262 | if (no_output && instack[indepth].fname != 0) | |
1263 | skip_if_group (&instack[indepth], 1); | |
1264 | ||
1265 | obp = op->bufp; | |
1266 | RECACHE; | |
1267 | beg_of_line = ibp; | |
1268 | ||
1269 | /* Our caller must always put a null after the end of | |
1270 | the input at each input stack level. */ | |
1271 | if (*limit != 0) | |
1272 | abort (); | |
1273 | ||
1274 | while (1) { | |
1275 | c = *ibp++; | |
1276 | *obp++ = c; | |
1277 | ||
1278 | switch (c) { | |
1279 | case '\\': | |
1280 | if (ibp >= limit) | |
1281 | break; | |
1282 | if (*ibp == '\n') { | |
1283 | /* Always merge lines ending with backslash-newline, | |
1284 | even in middle of identifier. */ | |
1285 | ++ibp; | |
1286 | ++ip->lineno; | |
1287 | --obp; /* remove backslash from obuf */ | |
1288 | break; | |
1289 | } | |
1290 | /* Otherwise, backslash suppresses specialness of following char, | |
1291 | so copy it here to prevent the switch from seeing it. | |
1292 | But first get any pending identifier processed. */ | |
1293 | if (ident_length > 0) | |
1294 | goto specialchar; | |
1295 | *obp++ = *ibp++; | |
1296 | break; | |
1297 | ||
1298 | case '#': | |
1299 | /* If this is expanding a macro definition, don't recognize | |
1300 | preprocessor directives. */ | |
1301 | if (ip->macro != 0) | |
1302 | goto randomchar; | |
1303 | if (ident_length) | |
1304 | goto specialchar; | |
1305 | ||
53fdf0be | 1306 | /* # keyword: a # must be the first char on the line */ |
24c3c71a ZW |
1307 | if (beg_of_line == 0) |
1308 | goto randomchar; | |
53fdf0be ZW |
1309 | if (beg_of_line + 1 != ibp) |
1310 | goto randomchar; | |
24c3c71a ZW |
1311 | |
1312 | /* This # can start a directive. */ | |
1313 | ||
1314 | --obp; /* Don't copy the '#' */ | |
1315 | ||
1316 | ip->bufp = ibp; | |
1317 | op->bufp = obp; | |
1318 | if (! handle_directive (ip, op)) { | |
1319 | #ifdef USE_C_ALLOCA | |
1320 | alloca (0); | |
1321 | #endif | |
1322 | /* Not a known directive: treat it as ordinary text. | |
1323 | IP, OP, IBP, etc. have not been changed. */ | |
1324 | if (no_output && instack[indepth].fname) { | |
1325 | /* If not generating expanded output, | |
1326 | what we do with ordinary text is skip it. | |
1327 | Discard everything until next # directive. */ | |
1328 | skip_if_group (&instack[indepth], 1); | |
1329 | RECACHE; | |
1330 | beg_of_line = ibp; | |
1331 | break; | |
1332 | } | |
1333 | ++obp; /* Copy the '#' after all */ | |
1334 | goto randomchar; | |
1335 | } | |
1336 | #ifdef USE_C_ALLOCA | |
1337 | alloca (0); | |
1338 | #endif | |
1339 | /* A # directive has been successfully processed. */ | |
1340 | /* If not generating expanded output, ignore everything until | |
1341 | next # directive. */ | |
1342 | if (no_output && instack[indepth].fname) | |
1343 | skip_if_group (&instack[indepth], 1); | |
1344 | obp = op->bufp; | |
1345 | RECACHE; | |
1346 | beg_of_line = ibp; | |
1347 | break; | |
1348 | ||
1349 | case '\"': /* skip quoted string */ | |
1350 | case '\'': | |
1351 | /* A single quoted string is treated like a double -- some | |
1352 | programs (e.g., troff) are perverse this way */ | |
1353 | ||
1354 | if (ident_length) | |
1355 | goto specialchar; | |
1356 | ||
1357 | start_line = ip->lineno; | |
1358 | ||
1359 | /* Skip ahead to a matching quote. */ | |
1360 | ||
1361 | while (1) { | |
1362 | if (ibp >= limit) { | |
1363 | if (ip->macro != 0) { | |
1364 | /* try harder: this string crosses a macro expansion boundary */ | |
1365 | POPMACRO; | |
1366 | RECACHE; | |
1367 | continue; | |
1368 | } | |
1369 | break; | |
1370 | } | |
1371 | *obp++ = *ibp; | |
1372 | switch (*ibp++) { | |
1373 | case '\n': | |
1374 | ++ip->lineno; | |
1375 | ++op->lineno; | |
1376 | /* Traditionally, end of line ends a string constant with no error. | |
1377 | So exit the loop and record the new line. */ | |
1378 | beg_of_line = ibp; | |
1379 | goto while2end; | |
1380 | ||
1381 | case '\\': | |
1382 | if (ibp >= limit) | |
1383 | break; | |
1384 | if (*ibp == '\n') { | |
1385 | /* Backslash newline is replaced by nothing at all, | |
1386 | but keep the line counts correct. */ | |
1387 | --obp; | |
1388 | ++ibp; | |
1389 | ++ip->lineno; | |
1390 | } else { | |
1391 | /* ANSI stupidly requires that in \\ the second \ | |
1392 | is *not* prevented from combining with a newline. */ | |
1393 | while (*ibp == '\\' && ibp[1] == '\n') { | |
1394 | ibp += 2; | |
1395 | ++ip->lineno; | |
1396 | } | |
1397 | *obp++ = *ibp++; | |
1398 | } | |
1399 | break; | |
1400 | ||
1401 | case '\"': | |
1402 | case '\'': | |
1403 | if (ibp[-1] == c) | |
1404 | goto while2end; | |
1405 | break; | |
1406 | } | |
1407 | } | |
1408 | while2end: | |
1409 | break; | |
1410 | ||
1411 | case '/': | |
1412 | if (*ibp == '\\' && ibp[1] == '\n') | |
1413 | newline_fix (ibp); | |
1414 | /* Don't look for comments inside a macro definition. */ | |
1415 | if (ip->macro != 0) | |
1416 | goto randomchar; | |
1417 | /* A comment constitutes white space, so it can terminate an identifier. | |
1418 | Process the identifier, if any. */ | |
1419 | if (ident_length) | |
1420 | goto specialchar; | |
1421 | ||
1422 | if (*ibp != '*') | |
1423 | goto randomchar; | |
1424 | ||
1425 | /* We have a comment. Skip it, optionally copying it to output. */ | |
1426 | ||
1427 | start_line = ip->lineno; | |
1428 | ||
1429 | ++ibp; /* Skip the star. */ | |
1430 | ||
1431 | /* In K+R C, a comment is equivalent to nothing. Note that we | |
1432 | already output the slash; we might not want it. */ | |
1433 | if (! put_out_comments) | |
1434 | obp--; | |
1435 | else | |
1436 | *obp++ = '*'; | |
1437 | ||
1438 | { | |
1439 | U_CHAR *before_bp = ibp; | |
1440 | ||
1441 | while (ibp < limit) { | |
1442 | switch (*ibp++) { | |
1443 | case '/': | |
1444 | if (warn_comments && ibp < limit && *ibp == '*') | |
1445 | warning("`/*' within comment"); | |
1446 | break; | |
1447 | case '*': | |
1448 | if (*ibp == '\\' && ibp[1] == '\n') | |
1449 | newline_fix (ibp); | |
1450 | if (ibp >= limit || *ibp == '/') | |
1451 | goto comment_end; | |
1452 | break; | |
1453 | case '\n': | |
1454 | ++ip->lineno; | |
1455 | /* Copy the newline into the output buffer, in order to | |
1456 | avoid the pain of a #line every time a multiline comment | |
1457 | is seen. */ | |
1458 | if (!put_out_comments) | |
1459 | *obp++ = '\n'; | |
1460 | ++op->lineno; | |
1461 | } | |
1462 | } | |
1463 | comment_end: | |
1464 | ||
1465 | if (ibp >= limit) | |
1466 | error_with_line (line_for_error (start_line), | |
1467 | "unterminated comment"); | |
1468 | else { | |
1469 | ibp++; | |
1470 | if (put_out_comments) { | |
1471 | memcpy (obp, before_bp, ibp - before_bp); | |
1472 | obp += ibp - before_bp; | |
1473 | } | |
1474 | } | |
1475 | } | |
1476 | break; | |
1477 | ||
1478 | case '0': case '1': case '2': case '3': case '4': | |
1479 | case '5': case '6': case '7': case '8': case '9': | |
1480 | /* If digit is not part of identifier, it starts a number, | |
1481 | which means that following letters are not an identifier. | |
1482 | "0x5" does not refer to an identifier "x5". | |
1483 | So copy all alphanumerics that follow without accumulating | |
1484 | as an identifier. Periods also, for sake of "3.e7". */ | |
1485 | ||
1486 | if (ident_length == 0) { | |
1487 | while (ibp < limit) { | |
1488 | while (ibp < limit && ibp[0] == '\\' && ibp[1] == '\n') { | |
1489 | ++ip->lineno; | |
1490 | ibp += 2; | |
1491 | } | |
1492 | c = *ibp++; | |
0df6c2c7 | 1493 | if (! ISIDNUM (c) && c != '.') { |
24c3c71a ZW |
1494 | --ibp; |
1495 | break; | |
1496 | } | |
1497 | *obp++ = c; | |
1498 | /* A sign can be part of a preprocessing number | |
1499 | if it follows an e. */ | |
1500 | if (c == 'e' || c == 'E') { | |
1501 | while (ibp < limit && ibp[0] == '\\' && ibp[1] == '\n') { | |
1502 | ++ip->lineno; | |
1503 | ibp += 2; | |
1504 | } | |
1505 | if (ibp < limit && (*ibp == '+' || *ibp == '-')) { | |
1506 | *obp++ = *ibp++; | |
1507 | /* Traditional C does not let the token go past the sign. */ | |
1508 | break; | |
1509 | } | |
1510 | } | |
1511 | } | |
1512 | break; | |
1513 | } | |
1514 | /* fall through */ | |
1515 | ||
1516 | case '_': | |
1517 | case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': | |
1518 | case 'g': case 'h': case 'i': case 'j': case 'k': case 'l': | |
1519 | case 'm': case 'n': case 'o': case 'p': case 'q': case 'r': | |
1520 | case 's': case 't': case 'u': case 'v': case 'w': case 'x': | |
1521 | case 'y': case 'z': | |
1522 | case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': | |
1523 | case 'G': case 'H': case 'I': case 'J': case 'K': case 'L': | |
1524 | case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R': | |
1525 | case 'S': case 'T': case 'U': case 'V': case 'W': case 'X': | |
1526 | case 'Y': case 'Z': | |
1527 | ident_length++; | |
1528 | /* Compute step of hash function, to avoid a proc call on every token */ | |
1529 | hash = HASHSTEP (hash, c); | |
1530 | break; | |
1531 | ||
1532 | case '\n': | |
1533 | /* If reprocessing a macro expansion, newline is a special marker. */ | |
1534 | if (ip->macro != 0) { | |
1535 | /* Newline White is a "funny space" to separate tokens that are | |
1536 | supposed to be separate but without space between. | |
1537 | Here White means any horizontal whitespace character. | |
1538 | Newline - marks a recursive macro use that is not | |
1539 | supposed to be expandable. */ | |
1540 | ||
1541 | if (*ibp == '-') { | |
1542 | /* Newline - inhibits expansion of preceding token. | |
1543 | If expanding a macro arg, we keep the newline -. | |
1544 | In final output, it is deleted. */ | |
1545 | if (! concatenated) { | |
1546 | ident_length = 0; | |
1547 | hash = 0; | |
1548 | } | |
1549 | ibp++; | |
1550 | if (!output_marks) { | |
1551 | obp--; | |
1552 | } else { | |
1553 | /* If expanding a macro arg, keep the newline -. */ | |
1554 | *obp++ = '-'; | |
1555 | } | |
f6bbde28 | 1556 | } else if (is_space (*ibp)) { |
24c3c71a ZW |
1557 | /* Newline Space does not prevent expansion of preceding token |
1558 | so expand the preceding token and then come back. */ | |
1559 | if (ident_length > 0) | |
1560 | goto specialchar; | |
1561 | ||
1562 | /* If generating final output, newline space makes a space. */ | |
1563 | if (!output_marks) { | |
1564 | obp[-1] = *ibp++; | |
1565 | /* And Newline Newline makes a newline, so count it. */ | |
1566 | if (obp[-1] == '\n') | |
1567 | op->lineno++; | |
1568 | } else { | |
1569 | /* If expanding a macro arg, keep the newline space. | |
1570 | If the arg gets stringified, newline space makes nothing. */ | |
1571 | *obp++ = *ibp++; | |
1572 | } | |
1573 | } else abort (); /* Newline followed by something random? */ | |
1574 | break; | |
1575 | } | |
1576 | ||
1577 | /* If there is a pending identifier, handle it and come back here. */ | |
1578 | if (ident_length > 0) | |
1579 | goto specialchar; | |
1580 | ||
1581 | beg_of_line = ibp; | |
1582 | ||
1583 | /* Update the line counts and output a #line if necessary. */ | |
1584 | ++ip->lineno; | |
1585 | ++op->lineno; | |
1586 | if (ip->lineno != op->lineno) { | |
1587 | op->bufp = obp; | |
1588 | output_line_command (ip, op, 1, same_file); | |
1589 | check_expand (op, ip->length - (ip->bufp - ip->buf)); | |
1590 | obp = op->bufp; | |
1591 | } | |
1592 | break; | |
1593 | ||
1594 | /* Come here either after (1) a null character that is part of the input | |
1595 | or (2) at the end of the input, because there is a null there. */ | |
1596 | case 0: | |
1597 | if (ibp <= limit) | |
1598 | /* Our input really contains a null character. */ | |
1599 | goto randomchar; | |
1600 | ||
1601 | /* At end of a macro-expansion level, pop it and read next level. */ | |
1602 | if (ip->macro != 0) { | |
1603 | obp--; | |
1604 | ibp--; | |
1605 | /* If we have an identifier that ends here, process it now, so | |
1606 | we get the right error for recursion. */ | |
f6bbde28 | 1607 | if (ident_length && ! is_idchar (*instack[indepth - 1].bufp)) { |
24c3c71a ZW |
1608 | redo_char = 1; |
1609 | goto randomchar; | |
1610 | } | |
1611 | POPMACRO; | |
1612 | RECACHE; | |
1613 | break; | |
1614 | } | |
1615 | ||
1616 | /* If we don't have a pending identifier, | |
1617 | return at end of input. */ | |
1618 | if (ident_length == 0) { | |
1619 | obp--; | |
1620 | ibp--; | |
1621 | op->bufp = obp; | |
1622 | ip->bufp = ibp; | |
1623 | goto ending; | |
1624 | } | |
1625 | ||
1626 | /* If we do have a pending identifier, just consider this null | |
1627 | a special character and arrange to dispatch on it again. | |
1628 | The second time, IDENT_LENGTH will be zero so we will return. */ | |
1629 | ||
1630 | /* Fall through */ | |
1631 | ||
1632 | specialchar: | |
1633 | ||
1634 | /* Handle the case of a character such as /, ', " or null | |
1635 | seen following an identifier. Back over it so that | |
1636 | after the identifier is processed the special char | |
1637 | will be dispatched on again. */ | |
1638 | ||
1639 | ibp--; | |
1640 | obp--; | |
1641 | redo_char = 1; | |
1642 | ||
1643 | default: | |
1644 | ||
1645 | randomchar: | |
1646 | ||
1647 | if (ident_length > 0) { | |
b3694847 | 1648 | HASHNODE *hp; |
24c3c71a ZW |
1649 | |
1650 | /* We have just seen an identifier end. If it's a macro, expand it. | |
1651 | ||
1652 | IDENT_LENGTH is the length of the identifier | |
1653 | and HASH is its hash code. | |
1654 | ||
1655 | The identifier has already been copied to the output, | |
1656 | so if it is a macro we must remove it. | |
1657 | ||
1658 | If REDO_CHAR is 0, the char that terminated the identifier | |
1659 | has been skipped in the output and the input. | |
1660 | OBP-IDENT_LENGTH-1 points to the identifier. | |
1661 | If the identifier is a macro, we must back over the terminator. | |
1662 | ||
1663 | If REDO_CHAR is 1, the terminating char has already been | |
1664 | backed over. OBP-IDENT_LENGTH points to the identifier. */ | |
1665 | ||
1666 | for (hp = hashtab[MAKE_POS (hash) % HASHSIZE]; hp != NULL; | |
1667 | hp = hp->next) { | |
1668 | ||
1669 | if (hp->length == ident_length) { | |
c1a6a8dc SS |
1670 | /* obufp_before_macroname is used only in this block, |
1671 | but it has to be global because of RECACHE. */ | |
24c3c71a | 1672 | int op_lineno_before_macroname; |
b3694847 SS |
1673 | int i = ident_length; |
1674 | U_CHAR *p = hp->name; | |
1675 | U_CHAR *q = obp - i; | |
24c3c71a ZW |
1676 | |
1677 | if (! redo_char) | |
1678 | q--; | |
1679 | ||
1680 | do { /* All this to avoid a strncmp () */ | |
1681 | if (*p++ != *q++) | |
1682 | goto hashcollision; | |
1683 | } while (--i); | |
1684 | ||
1685 | /* We found a use of a macro name. | |
1686 | see if the context shows it is a macro call. */ | |
1687 | ||
1688 | /* Back up over terminating character if not already done. */ | |
1689 | if (! redo_char) { | |
1690 | ibp--; | |
1691 | obp--; | |
1692 | } | |
1693 | ||
1694 | obufp_before_macroname = obp - ident_length; | |
1695 | op_lineno_before_macroname = op->lineno; | |
1696 | ||
1697 | /* If macro wants an arglist, verify that a '(' follows. | |
1698 | first skip all whitespace, copying it to the output | |
1699 | after the macro name. Then, if there is no '(', | |
1700 | decide this is not a macro call and leave things that way. */ | |
1701 | if (hp->type == T_MACRO && hp->value.defn->nargs >= 0) | |
1702 | { | |
1703 | while (1) { | |
1704 | /* Scan forward over whitespace, copying it to the output. */ | |
1705 | if (ibp == limit && ip->macro != 0) { | |
1706 | POPMACRO; | |
1707 | RECACHE; | |
1708 | } | |
1709 | /* A comment: copy it unchanged or discard it. */ | |
1710 | else if (*ibp == '/' && ibp+1 != limit && ibp[1] == '*') { | |
1711 | if (put_out_comments) { | |
1712 | *obp++ = '/'; | |
1713 | *obp++ = '*'; | |
1714 | } | |
1715 | ibp += 2; | |
1716 | while (ibp + 1 != limit | |
1717 | && !(ibp[0] == '*' && ibp[1] == '/')) { | |
1718 | /* We need not worry about newline-marks, | |
1719 | since they are never found in comments. */ | |
1720 | if (*ibp == '\n') { | |
1721 | /* Newline in a file. Count it. */ | |
1722 | ++ip->lineno; | |
1723 | ++op->lineno; | |
1724 | } | |
1725 | if (put_out_comments) | |
1726 | *obp++ = *ibp++; | |
1727 | else | |
1728 | ibp++; | |
1729 | } | |
1730 | ibp += 2; | |
1731 | if (put_out_comments) { | |
1732 | *obp++ = '*'; | |
1733 | *obp++ = '/'; | |
1734 | } | |
1735 | } | |
f6bbde28 | 1736 | else if (is_space (*ibp)) { |
24c3c71a ZW |
1737 | *obp++ = *ibp++; |
1738 | if (ibp[-1] == '\n') { | |
1739 | if (ip->macro == 0) { | |
1740 | /* Newline in a file. Count it. */ | |
1741 | ++ip->lineno; | |
1742 | ++op->lineno; | |
1743 | } else if (!output_marks) { | |
1744 | /* A newline mark, and we don't want marks | |
1745 | in the output. If it is newline-hyphen, | |
1746 | discard it entirely. Otherwise, it is | |
1747 | newline-whitechar, so keep the whitechar. */ | |
1748 | obp--; | |
1749 | if (*ibp == '-') | |
1750 | ibp++; | |
1751 | else { | |
1752 | if (*ibp == '\n') | |
1753 | ++op->lineno; | |
1754 | *obp++ = *ibp++; | |
1755 | } | |
1756 | } else { | |
1757 | /* A newline mark; copy both chars to the output. */ | |
1758 | *obp++ = *ibp++; | |
1759 | } | |
1760 | } | |
1761 | } | |
1762 | else break; | |
1763 | } | |
1764 | if (*ibp != '(') | |
1765 | break; | |
1766 | } | |
1767 | ||
1768 | /* This is now known to be a macro call. | |
1769 | Discard the macro name from the output, | |
1770 | along with any following whitespace just copied. */ | |
1771 | obp = obufp_before_macroname; | |
1772 | op->lineno = op_lineno_before_macroname; | |
1773 | ||
1774 | /* Expand the macro, reading arguments as needed, | |
1775 | and push the expansion on the input stack. */ | |
1776 | ip->bufp = ibp; | |
1777 | op->bufp = obp; | |
1778 | macroexpand (hp, op); | |
1779 | ||
1780 | /* Reexamine input stack, since macroexpand has pushed | |
1781 | a new level on it. */ | |
1782 | obp = op->bufp; | |
1783 | RECACHE; | |
1784 | break; | |
1785 | } | |
1786 | hashcollision: | |
1787 | ; | |
1788 | } /* End hash-table-search loop */ | |
1789 | ident_length = hash = 0; /* Stop collecting identifier */ | |
1790 | redo_char = 0; | |
1791 | concatenated = 0; | |
1792 | } /* End if (ident_length > 0) */ | |
1793 | } /* End switch */ | |
1794 | } /* End per-char loop */ | |
1795 | ||
1796 | /* Come here to return -- but first give an error message | |
1797 | if there was an unterminated successful conditional. */ | |
1798 | ending: | |
1799 | if (if_stack != ip->if_stack) { | |
1800 | const char *str; | |
1801 | switch (if_stack->type) { | |
1802 | case T_IF: | |
1803 | str = "if"; | |
1804 | break; | |
1805 | case T_IFDEF: | |
1806 | str = "ifdef"; | |
1807 | break; | |
1808 | case T_IFNDEF: | |
1809 | str = "ifndef"; | |
1810 | break; | |
1811 | case T_ELSE: | |
1812 | str = "else"; | |
1813 | break; | |
1814 | case T_ELIF: | |
1815 | str = "elif"; | |
1816 | break; | |
1817 | default: | |
1818 | abort (); | |
1819 | } | |
1820 | error_with_line (line_for_error (if_stack->lineno), | |
1821 | "unterminated #%s conditional", str); | |
1822 | } | |
1823 | if_stack = ip->if_stack; | |
1824 | } | |
1825 | \f | |
1826 | /* | |
1827 | * Rescan a string into a temporary buffer and return the result | |
1828 | * as a FILE_BUF. Note this function returns a struct, not a pointer. | |
1829 | * | |
1830 | * OUTPUT_MARKS nonzero means keep Newline markers found in the input | |
1831 | * and insert such markers when appropriate. See `rescan' for details. | |
1832 | * OUTPUT_MARKS is 1 for macroexpanding a macro argument separately | |
1833 | * before substitution; it is 0 for other uses. | |
1834 | */ | |
0a8ad417 | 1835 | static FILE_BUF |
24c3c71a | 1836 | expand_to_temp_buffer (buf, limit, output_marks) |
0a8ad417 | 1837 | const U_CHAR *buf, *limit; |
24c3c71a ZW |
1838 | int output_marks; |
1839 | { | |
b3694847 | 1840 | FILE_BUF *ip; |
24c3c71a ZW |
1841 | FILE_BUF obuf; |
1842 | int length = limit - buf; | |
1843 | U_CHAR *buf1; | |
1844 | int odepth = indepth; | |
1845 | ||
1846 | if (length < 0) | |
1847 | abort (); | |
1848 | ||
1849 | /* Set up the input on the input stack. */ | |
1850 | ||
1851 | buf1 = (U_CHAR *) alloca (length + 1); | |
1852 | { | |
b3694847 SS |
1853 | const U_CHAR *p1 = buf; |
1854 | U_CHAR *p2 = buf1; | |
24c3c71a ZW |
1855 | |
1856 | while (p1 != limit) | |
1857 | *p2++ = *p1++; | |
1858 | } | |
1859 | buf1[length] = 0; | |
1860 | ||
1861 | /* Set up to receive the output. */ | |
1862 | ||
1863 | obuf.length = length * 2 + 100; /* Usually enough. Why be stingy? */ | |
1864 | obuf.bufp = obuf.buf = (U_CHAR *) xmalloc (obuf.length); | |
1865 | obuf.fname = 0; | |
1866 | obuf.macro = 0; | |
1867 | obuf.free_ptr = 0; | |
1868 | ||
1869 | CHECK_DEPTH ({return obuf;}); | |
1870 | ||
1871 | ++indepth; | |
1872 | ||
1873 | ip = &instack[indepth]; | |
1874 | ip->fname = 0; | |
1875 | ip->macro = 0; | |
1876 | ip->free_ptr = 0; | |
1877 | ip->length = length; | |
1878 | ip->buf = ip->bufp = buf1; | |
1879 | ip->if_stack = if_stack; | |
1880 | ||
1881 | ip->lineno = obuf.lineno = 1; | |
1882 | ||
1883 | /* Scan the input, create the output. */ | |
1884 | ||
1885 | rescan (&obuf, output_marks); | |
1886 | ||
1887 | /* Pop input stack to original state. */ | |
1888 | --indepth; | |
1889 | ||
1890 | if (indepth != odepth) | |
1891 | abort (); | |
1892 | ||
1893 | /* Record the output. */ | |
1894 | obuf.length = obuf.bufp - obuf.buf; | |
1895 | ||
1896 | return obuf; | |
1897 | } | |
1898 | \f | |
1899 | /* | |
1900 | * Process a # directive. Expects IP->bufp to point to the '#', as in | |
1901 | * `#define foo bar'. Passes to the command handler | |
1902 | * (do_define, do_include, etc.): the addresses of the 1st and | |
1903 | * last chars of the command (starting immediately after the # | |
1904 | * keyword), plus op and the keyword table pointer. If the command | |
1905 | * contains comments it is copied into a temporary buffer sans comments | |
1906 | * and the temporary buffer is passed to the command handler instead. | |
1907 | * Likewise for backslash-newlines. | |
1908 | * | |
1909 | * Returns nonzero if this was a known # directive. | |
1910 | * Otherwise, returns zero, without advancing the input pointer. | |
1911 | */ | |
1912 | ||
0a8ad417 | 1913 | static int |
24c3c71a ZW |
1914 | handle_directive (ip, op) |
1915 | FILE_BUF *ip, *op; | |
1916 | { | |
b3694847 | 1917 | U_CHAR *bp, *cp; |
0b5826ac | 1918 | const struct directive *kt; |
b3694847 | 1919 | int ident_length; |
24c3c71a ZW |
1920 | U_CHAR *resume_p; |
1921 | ||
1922 | /* Nonzero means we must copy the entire command | |
1923 | to get rid of comments or backslash-newlines. */ | |
1924 | int copy_command = 0; | |
1925 | ||
1926 | U_CHAR *ident, *after_ident; | |
1927 | ||
1928 | bp = ip->bufp; | |
1929 | /* Skip whitespace and \-newline. */ | |
1930 | while (1) { | |
f6bbde28 | 1931 | if (is_nvspace (*bp)) |
24c3c71a ZW |
1932 | bp++; |
1933 | else if (*bp == '/' && (newline_fix (bp + 1), bp[1]) == '*') { | |
1934 | ip->bufp = bp; | |
1935 | skip_to_end_of_comment (ip, &ip->lineno); | |
1936 | bp = ip->bufp; | |
1937 | } else if (*bp == '\\' && bp[1] == '\n') { | |
1938 | bp += 2; ip->lineno++; | |
1939 | } else break; | |
1940 | } | |
1941 | ||
1942 | /* Now find end of directive name. | |
1943 | If we encounter a backslash-newline, exchange it with any following | |
1944 | symbol-constituents so that we end up with a contiguous name. */ | |
1945 | ||
1946 | cp = bp; | |
1947 | while (1) { | |
f6bbde28 | 1948 | if (is_idchar (*cp)) |
24c3c71a ZW |
1949 | cp++; |
1950 | else { | |
1951 | if (*cp == '\\' && cp[1] == '\n') | |
1952 | name_newline_fix (cp); | |
f6bbde28 | 1953 | if (is_idchar (*cp)) |
24c3c71a ZW |
1954 | cp++; |
1955 | else break; | |
1956 | } | |
1957 | } | |
1958 | ident_length = cp - bp; | |
1959 | ident = bp; | |
1960 | after_ident = cp; | |
1961 | ||
1962 | /* A line of just `#' becomes blank. */ | |
1963 | ||
1964 | if (ident_length == 0 && *after_ident == '\n') { | |
1965 | ip->bufp = after_ident; | |
1966 | return 1; | |
1967 | } | |
1968 | ||
1969 | /* | |
1970 | * Decode the keyword and call the appropriate expansion | |
1971 | * routine, after moving the input pointer up to the next line. | |
1972 | */ | |
1973 | for (kt = directive_table; kt->length > 0; kt++) { | |
1974 | if (kt->length == ident_length | |
0a8ad417 | 1975 | && !strncmp (kt->name, (const char *)ident, ident_length)) { |
b3694847 SS |
1976 | U_CHAR *buf; |
1977 | U_CHAR *limit = ip->buf + ip->length; | |
24c3c71a ZW |
1978 | int unterminated = 0; |
1979 | ||
1980 | /* Nonzero means do not delete comments within the directive. | |
1981 | #define needs this to detect traditional token paste. */ | |
1982 | int keep_comments = kt->type == T_DEFINE; | |
1983 | ||
1984 | /* Find the end of this command (first newline not backslashed | |
1985 | and not in a string or comment). | |
1986 | Set COPY_COMMAND if the command must be copied | |
1987 | (it contains a backslash-newline or a comment). */ | |
1988 | ||
1989 | buf = bp = after_ident; | |
1990 | while (bp < limit) { | |
b3694847 | 1991 | U_CHAR c = *bp++; |
24c3c71a ZW |
1992 | switch (c) { |
1993 | case '\\': | |
1994 | if (bp < limit) { | |
1995 | if (*bp == '\n') { | |
1996 | ip->lineno++; | |
1997 | copy_command = 1; | |
1998 | } | |
1999 | bp++; | |
2000 | } | |
2001 | break; | |
2002 | ||
2003 | case '\'': | |
2004 | case '\"': | |
2005 | bp = skip_quoted_string (bp - 1, limit, ip->lineno, &ip->lineno, ©_command, &unterminated); | |
2006 | if (unterminated) { | |
2007 | /* Traditional preprocessing permits unterminated strings. */ | |
2008 | ip->bufp = bp; | |
2009 | goto endloop1; | |
2010 | } | |
2011 | break; | |
2012 | ||
2013 | /* <...> is special for #include. */ | |
2014 | case '<': | |
2015 | if (kt->type != T_INCLUDE) | |
2016 | break; | |
2017 | while (*bp && *bp != '>') bp++; | |
2018 | break; | |
2019 | ||
2020 | case '/': | |
2021 | if (*bp == '\\' && bp[1] == '\n') | |
2022 | newline_fix (bp); | |
2023 | if (*bp == '*') { | |
2024 | U_CHAR *obp = bp - 1; | |
2025 | ip->bufp = bp + 1; | |
2026 | skip_to_end_of_comment (ip, &ip->lineno); | |
2027 | bp = ip->bufp; | |
2028 | /* No need to copy the command because of a comment at the end; | |
2029 | just don't include the comment in the directive. */ | |
2030 | if (bp == limit || *bp == '\n') { | |
2031 | bp = obp; | |
2032 | goto endloop1; | |
2033 | } | |
2034 | /* Don't remove the comments if this is #define. */ | |
2035 | if (! keep_comments) | |
2036 | copy_command++; | |
2037 | } | |
2038 | break; | |
2039 | ||
2040 | case '\n': | |
2041 | --bp; /* Point to the newline */ | |
2042 | ip->bufp = bp; | |
2043 | goto endloop1; | |
2044 | } | |
2045 | } | |
2046 | ip->bufp = bp; | |
2047 | ||
2048 | endloop1: | |
2049 | resume_p = ip->bufp; | |
2050 | /* BP is the end of the directive. | |
2051 | RESUME_P is the next interesting data after the directive. | |
2052 | A comment may come between. */ | |
2053 | ||
2054 | if (copy_command) { | |
b3694847 | 2055 | U_CHAR *xp = buf; |
24c3c71a ZW |
2056 | /* Need to copy entire command into temp buffer before dispatching */ |
2057 | ||
2058 | cp = (U_CHAR *) alloca (bp - buf + 5); /* room for cmd plus | |
2059 | some slop */ | |
2060 | buf = cp; | |
2061 | ||
2062 | /* Copy to the new buffer, deleting comments | |
2063 | and backslash-newlines (and whitespace surrounding the latter). */ | |
2064 | ||
2065 | while (xp < bp) { | |
b3694847 | 2066 | U_CHAR c = *xp++; |
24c3c71a ZW |
2067 | *cp++ = c; |
2068 | ||
2069 | switch (c) { | |
2070 | case '\n': | |
2071 | break; | |
2072 | ||
2073 | /* <...> is special for #include. */ | |
2074 | case '<': | |
2075 | if (kt->type != T_INCLUDE) | |
2076 | break; | |
2077 | while (xp < bp && c != '>') { | |
2078 | c = *xp++; | |
2079 | if (c == '\\' && xp < bp && *xp == '\n') | |
2080 | xp++, ip->lineno++; | |
2081 | else | |
2082 | *cp++ = c; | |
2083 | } | |
2084 | break; | |
2085 | ||
2086 | case '\\': | |
2087 | if (*xp == '\n') { | |
2088 | xp++; | |
2089 | cp--; | |
f6bbde28 ZW |
2090 | if (cp != buf && is_space (cp[-1])) { |
2091 | while (cp != buf && is_space(cp[-1])) cp--; | |
24c3c71a ZW |
2092 | cp++; |
2093 | SKIP_WHITE_SPACE (xp); | |
69e47210 | 2094 | } else if (is_nvspace (*xp)) { |
24c3c71a ZW |
2095 | *cp++ = *xp++; |
2096 | SKIP_WHITE_SPACE (xp); | |
2097 | } | |
2098 | } else { | |
2099 | *cp++ = *xp++; | |
2100 | } | |
2101 | break; | |
2102 | ||
2103 | case '\'': | |
2104 | case '\"': | |
2105 | { | |
b3694847 | 2106 | const U_CHAR *bp1 |
24c3c71a ZW |
2107 | = skip_quoted_string (xp - 1, limit, ip->lineno, 0, 0, 0); |
2108 | while (xp != bp1) | |
2109 | *cp++ = *xp++; | |
2110 | } | |
2111 | break; | |
2112 | ||
2113 | case '/': | |
2114 | if (*xp == '*') { | |
2115 | ip->bufp = xp + 1; | |
2116 | skip_to_end_of_comment (ip, 0); | |
2117 | if (keep_comments) | |
2118 | while (xp != ip->bufp) | |
2119 | *cp++ = *xp++; | |
2120 | /* Delete the slash. */ | |
2121 | else | |
2122 | cp--; | |
2123 | xp = ip->bufp; | |
2124 | } | |
2125 | } | |
2126 | } | |
2127 | ||
2128 | /* Null-terminate the copy. */ | |
2129 | ||
2130 | *cp = 0; | |
2131 | } | |
2132 | else | |
2133 | cp = bp; | |
2134 | ||
2135 | ip->bufp = resume_p; | |
2136 | ||
2137 | /* Call the appropriate command handler. buf now points to | |
2138 | either the appropriate place in the input buffer, or to | |
2139 | the temp buffer if it was necessary to make one. cp | |
2140 | points to the first char after the contents of the (possibly | |
2141 | copied) command, in either case. */ | |
40bd4395 | 2142 | (*kt->func) (buf, cp, op); |
24c3c71a ZW |
2143 | check_expand (op, ip->length - (ip->bufp - ip->buf)); |
2144 | ||
2145 | return 1; | |
2146 | } | |
2147 | } | |
2148 | ||
2149 | return 0; | |
2150 | } | |
2151 | \f | |
2152 | static const char *const | |
2153 | monthnames[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", | |
2154 | "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; | |
2155 | ||
2156 | /* | |
2157 | * expand things like __FILE__. Place the expansion into the output | |
2158 | * buffer *without* rescanning. | |
2159 | */ | |
0a8ad417 | 2160 | static void |
24c3c71a ZW |
2161 | special_symbol (hp, op) |
2162 | HASHNODE *hp; | |
2163 | FILE_BUF *op; | |
2164 | { | |
0a8ad417 | 2165 | const char *buf; |
24c3c71a ZW |
2166 | time_t t; |
2167 | int i, len; | |
2168 | int true_indepth; | |
2169 | FILE_BUF *ip = NULL; | |
2170 | static struct tm *timebuf = NULL; | |
2171 | ||
2172 | int paren = 0; /* For special `defined' keyword */ | |
2173 | ||
2174 | for (i = indepth; i >= 0; i--) | |
2175 | if (instack[i].fname != NULL) { | |
2176 | ip = &instack[i]; | |
2177 | break; | |
2178 | } | |
2179 | if (ip == NULL) | |
2180 | fatal ("not in any file?!"); | |
2181 | ||
2182 | switch (hp->type) { | |
2183 | case T_FILE: | |
2184 | case T_BASE_FILE: | |
2185 | { | |
2186 | const char *string; | |
2187 | if (hp->type == T_FILE) | |
2188 | string = ip->fname; | |
2189 | else | |
2190 | string = instack[0].fname; | |
2191 | ||
2192 | if (string) | |
2193 | { | |
0a8ad417 KG |
2194 | char *tmp = (char *) alloca (3 + strlen (string)); |
2195 | sprintf (tmp, "\"%s\"", string); | |
2196 | buf = tmp; | |
24c3c71a ZW |
2197 | } |
2198 | else | |
0a8ad417 | 2199 | buf = ""; |
24c3c71a ZW |
2200 | |
2201 | break; | |
2202 | } | |
2203 | ||
2204 | case T_INCLUDE_LEVEL: | |
0a8ad417 KG |
2205 | { |
2206 | char *tmp = (char *) alloca (8); /* Eigth bytes ought to be more than enough */ | |
2207 | true_indepth = 0; | |
2208 | for (i = indepth; i >= 0; i--) | |
2209 | if (instack[i].fname != NULL) | |
2210 | true_indepth++; | |
2211 | ||
2212 | sprintf (tmp, "%d", true_indepth - 1); | |
2213 | buf = tmp; | |
24c3c71a | 2214 | break; |
0a8ad417 | 2215 | } |
24c3c71a ZW |
2216 | |
2217 | case T_VERSION: | |
0a8ad417 KG |
2218 | { |
2219 | char *tmp = (char *) alloca (3 + strlen (version_string)); | |
2220 | sprintf (tmp, "\"%s\"", version_string); | |
2221 | buf = tmp; | |
2222 | break; | |
2223 | } | |
24c3c71a ZW |
2224 | |
2225 | case T_CONST: | |
0a8ad417 | 2226 | buf = hp->value.cpval; |
24c3c71a ZW |
2227 | break; |
2228 | ||
2229 | case T_SPECLINE: | |
0a8ad417 KG |
2230 | { |
2231 | char *tmp = (char *) alloca (10); | |
2232 | sprintf (tmp, "%d", ip->lineno); | |
2233 | buf = tmp; | |
2234 | break; | |
2235 | } | |
24c3c71a ZW |
2236 | |
2237 | case T_DATE: | |
2238 | case T_TIME: | |
0a8ad417 KG |
2239 | { |
2240 | char *tmp = (char *) alloca (20); | |
2241 | ||
2242 | if (timebuf == NULL) { | |
2243 | t = time (0); | |
2244 | timebuf = localtime (&t); | |
2245 | } | |
2246 | if (hp->type == T_DATE) | |
2247 | sprintf (tmp, "\"%s %2d %4d\"", monthnames[timebuf->tm_mon], | |
2248 | timebuf->tm_mday, timebuf->tm_year + 1900); | |
2249 | else | |
2250 | sprintf (tmp, "\"%02d:%02d:%02d\"", timebuf->tm_hour, timebuf->tm_min, | |
2251 | timebuf->tm_sec); | |
2252 | buf = tmp; | |
2253 | break; | |
24c3c71a | 2254 | } |
24c3c71a ZW |
2255 | |
2256 | case T_SPEC_DEFINED: | |
0a8ad417 | 2257 | buf = " 0 "; /* Assume symbol is not defined */ |
24c3c71a ZW |
2258 | ip = &instack[indepth]; |
2259 | SKIP_WHITE_SPACE (ip->bufp); | |
2260 | if (*ip->bufp == '(') { | |
2261 | paren++; | |
2262 | ip->bufp++; /* Skip over the paren */ | |
2263 | SKIP_WHITE_SPACE (ip->bufp); | |
2264 | } | |
2265 | ||
f6bbde28 | 2266 | if (!is_idstart (*ip->bufp)) |
24c3c71a | 2267 | goto oops; |
c36ee164 NB |
2268 | { |
2269 | HASHNODE *hp = lookup (ip->bufp, -1, -1); | |
2270 | ||
2271 | if (hp && hp->type != T_UNUSED && hp->type != T_SPEC_DEFINED) | |
2272 | buf = " 1 "; | |
2273 | } | |
f6bbde28 | 2274 | while (is_idchar (*ip->bufp)) |
24c3c71a ZW |
2275 | ++ip->bufp; |
2276 | SKIP_WHITE_SPACE (ip->bufp); | |
2277 | if (paren) { | |
2278 | if (*ip->bufp != ')') | |
2279 | goto oops; | |
2280 | ++ip->bufp; | |
2281 | } | |
2282 | break; | |
2283 | ||
2284 | oops: | |
2285 | ||
2286 | error ("`defined' must be followed by ident or (ident)"); | |
2287 | break; | |
2288 | ||
2289 | default: | |
2290 | error ("cccp error: invalid special hash type"); /* time for gdb */ | |
2291 | abort (); | |
2292 | } | |
2293 | len = strlen (buf); | |
2294 | check_expand (op, len); | |
2295 | memcpy (op->bufp, buf, len); | |
2296 | op->bufp += len; | |
2297 | } | |
2298 | ||
2299 | \f | |
2300 | /* Routines to handle #directives */ | |
2301 | ||
2302 | /* | |
2303 | * Process include file by reading it in and calling rescan. | |
2304 | * Expects to see "fname" or <fname> on the input. | |
2305 | */ | |
0a8ad417 | 2306 | static void |
40bd4395 | 2307 | do_include (buf, limit, op) |
24c3c71a ZW |
2308 | U_CHAR *buf, *limit; |
2309 | FILE_BUF *op; | |
24c3c71a | 2310 | { |
24c3c71a ZW |
2311 | U_CHAR *fbeg, *fend; /* Beginning and end of fname */ |
2312 | ||
2313 | struct file_name_list *stackp = include; /* Chain of dirs to search */ | |
2314 | struct file_name_list dsp[1]; /* First in chain, if #include "..." */ | |
2315 | int flen; | |
2316 | ||
24c3c71a ZW |
2317 | int retried = 0; /* Have already tried macro |
2318 | expanding the include line*/ | |
2319 | FILE_BUF trybuf; /* It got expanded into here */ | |
2320 | int system_header_p = 0; /* 0 for "...", 1 for <...> */ | |
2321 | ||
24c3c71a ZW |
2322 | get_filename: |
2323 | ||
2324 | fbeg = buf; | |
2325 | SKIP_WHITE_SPACE (fbeg); | |
2326 | /* Discard trailing whitespace so we can easily see | |
2327 | if we have parsed all the significant chars we were given. */ | |
f6bbde28 | 2328 | while (limit != fbeg && is_nvspace (limit[-1])) limit--; |
24c3c71a ZW |
2329 | |
2330 | switch (*fbeg++) { | |
2331 | case '\"': | |
2332 | fend = fbeg; | |
2333 | while (fend != limit && *fend != '\"') | |
2334 | fend++; | |
2335 | if (*fend == '\"' && fend + 1 == limit) { | |
2336 | FILE_BUF *fp; | |
2337 | ||
2338 | /* We have "filename". Figure out directory this source | |
2339 | file is coming from and put it on the front of the list. */ | |
2340 | ||
2341 | /* If -I- was specified, don't search current dir, only spec'd ones. */ | |
2342 | if (ignore_srcdir) break; | |
2343 | ||
2344 | for (fp = &instack[indepth]; fp >= instack; fp--) | |
2345 | { | |
2346 | size_t n; | |
2347 | const char *ep, *nam; | |
2348 | ||
2349 | if ((nam = fp->fname) != NULL) { | |
2350 | /* Found a named file. Figure out dir of the file, | |
2351 | and put it in front of the search list. */ | |
2352 | dsp[0].next = stackp; | |
2353 | stackp = dsp; | |
2354 | ep = strrchr (nam, '/'); | |
2355 | if (ep != NULL) { | |
2356 | char *f; | |
2357 | n = ep - nam; | |
2358 | f = (char *) alloca (n + 1); | |
2359 | strncpy (f, nam, n); | |
2360 | f[n] = '\0'; | |
2361 | dsp[0].fname = f; | |
2362 | if (n > max_include_len) max_include_len = n; | |
2363 | } else { | |
2364 | dsp[0].fname = 0; /* Current directory */ | |
2365 | } | |
2366 | break; | |
2367 | } | |
2368 | } | |
2369 | break; | |
2370 | } | |
2371 | goto fail; | |
2372 | ||
2373 | case '<': | |
2374 | fend = fbeg; | |
2375 | while (fend != limit && *fend != '>') fend++; | |
2376 | if (*fend == '>' && fend + 1 == limit) { | |
2377 | system_header_p = 1; | |
2378 | /* If -I-, start with the first -I dir after the -I-. */ | |
2379 | if (first_bracket_include) | |
2380 | stackp = first_bracket_include; | |
2381 | break; | |
2382 | } | |
2383 | goto fail; | |
2384 | ||
2385 | default: | |
2386 | fail: | |
2387 | if (retried) { | |
2388 | error ("#include expects \"fname\" or <fname>"); | |
2389 | return; | |
2390 | } else { | |
2391 | trybuf = expand_to_temp_buffer (buf, limit, 0); | |
2392 | buf = (U_CHAR *) alloca (trybuf.bufp - trybuf.buf + 1); | |
2393 | memcpy (buf, trybuf.buf, trybuf.bufp - trybuf.buf); | |
2394 | limit = buf + (trybuf.bufp - trybuf.buf); | |
2395 | free (trybuf.buf); | |
2396 | retried++; | |
2397 | goto get_filename; | |
2398 | } | |
2399 | } | |
2400 | ||
2401 | flen = fend - fbeg; | |
6d4587f7 ZW |
2402 | process_include (stackp, fbeg, flen, system_header_p, op); |
2403 | } | |
2404 | ||
2405 | static void | |
2406 | do_include_next (buf, limit, op) | |
2407 | U_CHAR *buf, *limit; | |
2408 | FILE_BUF *op; | |
2409 | { | |
2410 | U_CHAR *fbeg, *fend; /* Beginning and end of fname */ | |
2411 | ||
2412 | struct file_name_list *stackp; /* Chain of dirs to search */ | |
2413 | int flen; | |
2414 | ||
2415 | int retried = 0; /* Have already tried macro | |
2416 | expanding the include line*/ | |
2417 | FILE_BUF trybuf; /* It got expanded into here */ | |
2418 | int system_header_p = 0; /* 0 for "...", 1 for <...> */ | |
2419 | ||
2420 | /* Treat as plain #include if we don't know where to start | |
2421 | looking. */ | |
2422 | stackp = instack[indepth].next_header_dir; | |
2423 | if (stackp == 0) | |
2424 | { | |
2425 | do_include (buf, limit, op); | |
2426 | return; | |
2427 | } | |
2428 | ||
2429 | get_filename: | |
2430 | ||
2431 | fbeg = buf; | |
2432 | SKIP_WHITE_SPACE (fbeg); | |
2433 | /* Discard trailing whitespace so we can easily see | |
2434 | if we have parsed all the significant chars we were given. */ | |
2435 | while (limit != fbeg && is_nvspace (limit[-1])) limit--; | |
2436 | ||
2437 | switch (*fbeg++) { | |
2438 | case '\"': | |
2439 | fend = fbeg; | |
2440 | while (fend != limit && *fend != '\"') | |
2441 | fend++; | |
2442 | if (*fend == '\"' && fend + 1 == limit) | |
2443 | break; | |
2444 | goto fail; | |
2445 | ||
2446 | case '<': | |
2447 | fend = fbeg; | |
2448 | while (fend != limit && *fend != '>') fend++; | |
2449 | if (*fend == '>' && fend + 1 == limit) { | |
2450 | system_header_p = 1; | |
2451 | break; | |
2452 | } | |
2453 | goto fail; | |
2454 | ||
2455 | default: | |
2456 | fail: | |
2457 | if (retried) { | |
2458 | error ("#include expects \"fname\" or <fname>"); | |
2459 | return; | |
2460 | } else { | |
2461 | trybuf = expand_to_temp_buffer (buf, limit, 0); | |
2462 | buf = (U_CHAR *) alloca (trybuf.bufp - trybuf.buf + 1); | |
2463 | memcpy (buf, trybuf.buf, trybuf.bufp - trybuf.buf); | |
2464 | limit = buf + (trybuf.bufp - trybuf.buf); | |
2465 | free (trybuf.buf); | |
2466 | retried++; | |
2467 | goto get_filename; | |
2468 | } | |
2469 | } | |
2470 | ||
2471 | flen = fend - fbeg; | |
2472 | process_include (stackp, fbeg, flen, system_header_p, op); | |
2473 | } | |
2474 | ||
2475 | static void | |
2476 | process_include (stackp, fbeg, flen, system_header_p, op) | |
2477 | struct file_name_list *stackp; | |
2478 | const U_CHAR *fbeg; | |
2479 | int flen; | |
2480 | int system_header_p; | |
2481 | FILE_BUF *op; | |
2482 | { | |
2483 | char *fname; | |
2484 | int f = -1; /* file number */ | |
2485 | ||
24c3c71a ZW |
2486 | fname = (char *) alloca (max_include_len + flen + 2); |
2487 | /* + 2 above for slash and terminating null. */ | |
2488 | ||
2489 | /* If specified file name is absolute, just open it. */ | |
2490 | ||
05e81724 | 2491 | if (IS_ABSOLUTE_PATHNAME (fbeg)) { |
0a8ad417 | 2492 | strncpy (fname, (const char *)fbeg, flen); |
24c3c71a ZW |
2493 | fname[flen] = 0; |
2494 | f = open (fname, O_RDONLY, 0666); | |
2495 | } else { | |
2496 | /* Search directory path, trying to open the file. | |
2497 | Copy each filename tried into FNAME. */ | |
2498 | ||
2499 | for (; stackp; stackp = stackp->next) { | |
2500 | if (stackp->fname) { | |
2501 | strcpy (fname, stackp->fname); | |
2502 | strcat (fname, "/"); | |
2503 | fname[strlen (fname) + flen] = 0; | |
2504 | } else { | |
2505 | fname[0] = 0; | |
2506 | } | |
0a8ad417 | 2507 | strncat (fname, (const char *)fbeg, flen); |
24c3c71a ZW |
2508 | if ((f = open (fname, O_RDONLY, 0666)) >= 0) |
2509 | break; | |
2510 | } | |
2511 | } | |
2512 | ||
2513 | if (f < 0) { | |
0a8ad417 | 2514 | strncpy (fname, (const char *)fbeg, flen); |
24c3c71a | 2515 | fname[flen] = 0; |
2f638f96 | 2516 | if (deps_missing_files |
c3843cea | 2517 | && print_deps > (system_header_p || (system_include_depth > 0))) { |
24c3c71a | 2518 | |
c3843cea JJ |
2519 | /* If requested as a system header, assume it belongs in |
2520 | the first system header directory. */ | |
2521 | if (first_bracket_include) | |
2522 | stackp = first_bracket_include; | |
24c3c71a | 2523 | else |
c3843cea JJ |
2524 | stackp = include; |
2525 | ||
05e81724 | 2526 | if (!system_header_p || IS_ABSOLUTE_PATHNAME (fbeg) || !stackp->fname) |
2f638f96 | 2527 | deps_add_dep (deps, fname); |
c3843cea JJ |
2528 | else { |
2529 | char *p; | |
2530 | int len = strlen(stackp->fname); | |
2531 | ||
2532 | p = (char *) alloca (len + flen + 2); | |
2533 | memcpy (p, stackp->fname, len); | |
2534 | p[len++] = '/'; | |
2535 | memcpy (p + len, fbeg, flen); | |
2536 | len += flen; | |
2f638f96 NB |
2537 | p[len] = '\0'; |
2538 | deps_add_dep (deps, p); | |
c3843cea | 2539 | } |
c3843cea JJ |
2540 | } else if (print_deps |
2541 | && print_deps <= (system_header_p | |
2542 | || (system_include_depth > 0))) | |
c725bd79 | 2543 | warning ("no include path in which to find %.*s", flen, fbeg); |
c3843cea JJ |
2544 | else |
2545 | error_from_errno (fname); | |
2546 | ||
24c3c71a ZW |
2547 | } else { |
2548 | ||
2549 | /* Check to see if this include file is a once-only include file. | |
2550 | If so, give up. */ | |
2551 | ||
2552 | struct file_name_list* ptr; | |
2553 | ||
2554 | for (ptr = dont_repeat_files; ptr; ptr = ptr->next) { | |
2555 | if (!strcmp (ptr->fname, fname)) { | |
2556 | close (f); | |
2557 | return; /* This file was once'd. */ | |
2558 | } | |
2559 | } | |
2560 | ||
2561 | for (ptr = all_include_files; ptr; ptr = ptr->next) { | |
2562 | if (!strcmp (ptr->fname, fname)) | |
2563 | break; /* This file was included before. */ | |
2564 | } | |
2565 | ||
2566 | if (ptr == 0) { | |
2567 | /* This is the first time for this file. */ | |
2568 | /* Add it to list of files included. */ | |
2569 | ||
2570 | ptr = (struct file_name_list *) xmalloc (sizeof (struct file_name_list)); | |
2571 | ptr->next = all_include_files; | |
2572 | all_include_files = ptr; | |
2573 | ptr->fname = xstrdup (fname); | |
2574 | ||
2575 | /* For -M, add this file to the dependencies. */ | |
2f638f96 NB |
2576 | if (print_deps > (system_header_p || (system_include_depth > 0))) |
2577 | deps_add_dep (deps, fname); | |
24c3c71a ZW |
2578 | } |
2579 | ||
2580 | if (system_header_p) | |
2581 | system_include_depth++; | |
2582 | ||
2583 | /* Actually process the file. */ | |
6d4587f7 | 2584 | finclude (f, fname, stackp->next, op); |
24c3c71a ZW |
2585 | |
2586 | if (system_header_p) | |
2587 | system_include_depth--; | |
2588 | ||
2589 | close (f); | |
2590 | } | |
2591 | } | |
2592 | ||
8157303b JJ |
2593 | /* Replace all CR NL, NL CR and CR sequences with NL. */ |
2594 | ||
2595 | static void | |
29d83d60 DM |
2596 | fixup_newlines (fp) |
2597 | FILE_BUF *fp; | |
8157303b JJ |
2598 | { |
2599 | U_CHAR *p, *q, *end; | |
2600 | ||
2601 | if (fp->length <= 0) | |
2602 | return; | |
2603 | ||
2604 | end = fp->buf + fp->length; | |
2605 | *end = '\r'; | |
2606 | p = (U_CHAR *) strchr ((const char *) fp->buf, '\r'); | |
2607 | *end = '\0'; | |
2608 | if (p == end) | |
2609 | return; | |
2610 | ||
2611 | if (p > fp->buf && p[-1] == '\n') | |
2612 | p--; | |
2613 | q = p; | |
2614 | while (p < end) | |
2615 | switch (*p) | |
2616 | { | |
2617 | default: | |
2618 | *q++ = *p++; | |
2619 | break; | |
2620 | case '\n': | |
2621 | case '\r': | |
2622 | p += 1 + (p[0] + p[1] == '\n' + '\r'); | |
2623 | *q++ = '\n'; | |
2624 | break; | |
2625 | } | |
2626 | ||
2627 | fp->length = q - fp->buf; | |
2628 | } | |
2629 | ||
24c3c71a ZW |
2630 | /* Process the contents of include file FNAME, already open on descriptor F, |
2631 | with output to OP. */ | |
2632 | ||
0a8ad417 | 2633 | static void |
6d4587f7 | 2634 | finclude (f, fname, nhd, op) |
24c3c71a ZW |
2635 | int f; |
2636 | const char *fname; | |
6d4587f7 | 2637 | struct file_name_list *nhd; |
24c3c71a ZW |
2638 | FILE_BUF *op; |
2639 | { | |
2640 | int st_mode; | |
2641 | long st_size; | |
2642 | long i; | |
2643 | FILE_BUF *fp; /* For input stack frame */ | |
2644 | ||
2645 | CHECK_DEPTH (return;); | |
2646 | ||
2647 | if (file_size_and_mode (f, &st_mode, &st_size)) | |
2648 | goto nope; | |
2649 | ||
2650 | fp = &instack[indepth + 1]; | |
2651 | memset (fp, 0, sizeof (FILE_BUF)); | |
2652 | fp->fname = fname; | |
2653 | fp->length = 0; | |
2654 | fp->lineno = 1; | |
2655 | fp->if_stack = if_stack; | |
6d4587f7 | 2656 | fp->next_header_dir = nhd; |
24c3c71a ZW |
2657 | |
2658 | if (S_ISREG (st_mode)) { | |
2659 | fp->buf = (U_CHAR *) xmalloc (st_size + 2); | |
2660 | fp->bufp = fp->buf; | |
2661 | ||
2662 | /* Read the file contents, knowing that st_size is an upper bound | |
2663 | on the number of bytes we can read. */ | |
2664 | while (st_size > 0) { | |
2665 | i = read (f, fp->buf + fp->length, st_size); | |
2666 | if (i <= 0) { | |
2667 | if (i == 0) break; | |
2668 | goto nope; | |
2669 | } | |
2670 | fp->length += i; | |
2671 | st_size -= i; | |
2672 | } | |
2673 | } | |
2674 | else { | |
2675 | /* Cannot count its file size before reading. */ | |
2676 | ||
2677 | U_CHAR *bufp; | |
2678 | U_CHAR *basep; | |
2679 | int bsize = 2000; | |
2680 | ||
2681 | st_size = 0; | |
2682 | basep = (U_CHAR *) xmalloc (bsize + 2); | |
2683 | bufp = basep; | |
2684 | ||
2685 | for (;;) { | |
2686 | i = read (f, bufp, bsize - st_size); | |
2687 | if (i < 0) | |
2688 | goto nope; /* error! */ | |
2689 | if (i == 0) | |
2690 | break; /* End of file */ | |
2691 | st_size += i; | |
2692 | bufp += i; | |
2693 | if (bsize == st_size) { /* Buffer is full! */ | |
2694 | bsize *= 2; | |
2695 | basep = (U_CHAR *) xrealloc (basep, bsize + 2); | |
2696 | bufp = basep + st_size; /* May have moved */ | |
2697 | } | |
2698 | } | |
2699 | fp->buf = basep; | |
2700 | fp->bufp = fp->buf; | |
2701 | fp->length = st_size; | |
2702 | } | |
2703 | close (f); | |
8157303b | 2704 | fixup_newlines (fp); |
24c3c71a ZW |
2705 | |
2706 | /* Make sure data ends with a newline. And put a null after it. */ | |
2707 | ||
2708 | if (fp->length > 0 && fp->buf[fp->length-1] != '\n') | |
2709 | fp->buf[fp->length++] = '\n'; | |
2710 | fp->buf[fp->length] = '\0'; | |
2711 | ||
2712 | indepth++; | |
2713 | output_line_command (fp, op, 0, enter_file); | |
2714 | rescan (op, 0); | |
2715 | indepth--; | |
3244472d NB |
2716 | instack[indepth].lineno++; |
2717 | instack[indepth].bufp++; /* Skip the new line. */ | |
24c3c71a ZW |
2718 | output_line_command (&instack[indepth], op, 0, leave_file); |
2719 | free (fp->buf); | |
2720 | return; | |
2721 | ||
2722 | nope: | |
2723 | perror_with_name (fname); | |
2724 | close (f); | |
2725 | } | |
2726 | \f | |
2727 | ||
2728 | /* Process a #define command. | |
2729 | BUF points to the contents of the #define command, as a continguous string. | |
2730 | LIMIT points to the first character past the end of the definition. | |
2731 | KEYWORD is the keyword-table entry for #define. */ | |
2732 | ||
0a8ad417 | 2733 | static void |
40bd4395 | 2734 | do_define (buf, limit, op) |
24c3c71a ZW |
2735 | U_CHAR *buf, *limit; |
2736 | FILE_BUF *op ATTRIBUTE_UNUSED; | |
24c3c71a ZW |
2737 | { |
2738 | U_CHAR *bp; /* temp ptr into input buffer */ | |
2739 | U_CHAR *symname; /* remember where symbol name starts */ | |
2740 | int sym_length; /* and how long it is */ | |
2741 | ||
2742 | DEFINITION *defn; | |
2743 | int arglengths = 0; /* Accumulate lengths of arg names | |
2744 | plus number of args. */ | |
2745 | int hashcode; | |
2746 | ||
2747 | bp = buf; | |
2748 | ||
f6bbde28 | 2749 | while (is_nvspace (*bp)) |
24c3c71a ZW |
2750 | bp++; |
2751 | ||
2752 | symname = bp; /* remember where it starts */ | |
f6bbde28 | 2753 | while (is_idchar (*bp) && bp < limit) { |
24c3c71a ZW |
2754 | bp++; |
2755 | } | |
2756 | sym_length = bp - symname; | |
2757 | if (sym_length == 0) | |
c36ee164 NB |
2758 | { |
2759 | error ("invalid macro name"); | |
2760 | return; | |
2761 | } | |
f6bbde28 | 2762 | else if (!is_idstart (*symname)) { |
24c3c71a ZW |
2763 | U_CHAR *msg; /* what pain... */ |
2764 | msg = (U_CHAR *) alloca (sym_length + 1); | |
2765 | memcpy (msg, symname, sym_length); | |
2766 | msg[sym_length] = 0; | |
2767 | error ("invalid macro name `%s'", msg); | |
c36ee164 | 2768 | return; |
24c3c71a | 2769 | } else { |
0a8ad417 | 2770 | if (! strncmp ((const char *)symname, "defined", 7) && sym_length == 7) |
c36ee164 NB |
2771 | { |
2772 | error ("\"defined\" cannot be used as a macro name"); | |
2773 | return; | |
2774 | } | |
24c3c71a ZW |
2775 | } |
2776 | ||
2777 | /* lossage will occur if identifiers or control keywords are broken | |
2778 | across lines using backslash. This is not the right place to take | |
2779 | care of that. */ | |
2780 | ||
2781 | if (*bp == '(') { | |
2782 | struct arglist *arg_ptrs = NULL; | |
2783 | int argno = 0; | |
2784 | ||
2785 | bp++; /* skip '(' */ | |
2786 | SKIP_WHITE_SPACE (bp); | |
2787 | ||
2788 | /* Loop over macro argument names. */ | |
2789 | while (*bp != ')') { | |
2790 | struct arglist *temp; | |
2791 | ||
2792 | temp = (struct arglist *) alloca (sizeof (struct arglist)); | |
2793 | temp->name = bp; | |
2794 | temp->next = arg_ptrs; | |
2795 | temp->argno = argno++; | |
2796 | arg_ptrs = temp; | |
2797 | ||
f6bbde28 | 2798 | if (!is_idstart (*bp)) |
24c3c71a ZW |
2799 | warning ("parameter name starts with a digit in #define"); |
2800 | ||
2801 | /* Find the end of the arg name. */ | |
f6bbde28 | 2802 | while (is_idchar (*bp)) { |
24c3c71a ZW |
2803 | bp++; |
2804 | } | |
2805 | temp->length = bp - temp->name; | |
2806 | arglengths += temp->length + 2; | |
2807 | SKIP_WHITE_SPACE (bp); | |
2808 | if (temp->length == 0 || (*bp != ',' && *bp != ')')) { | |
2809 | error ("badly punctuated parameter list in #define"); | |
2810 | return; | |
2811 | } | |
2812 | if (*bp == ',') { | |
2813 | bp++; | |
2814 | SKIP_WHITE_SPACE (bp); | |
2815 | } | |
2816 | if (bp >= limit) { | |
2817 | error ("unterminated parameter list in #define"); | |
2818 | return; | |
2819 | } | |
2820 | } | |
2821 | ||
2d2a86ae JJ |
2822 | ++bp; /* skip paren */ |
2823 | while (is_nvspace (*bp) && bp < limit) /* and leading whitespace */ | |
1e18a243 | 2824 | ++bp; |
24c3c71a ZW |
2825 | /* now everything from bp before limit is the definition. */ |
2826 | defn = collect_expansion (bp, limit, argno, arg_ptrs); | |
2827 | ||
2828 | /* Now set defn->argnames to the result of concatenating | |
2829 | the argument names in reverse order | |
2830 | with comma-space between them. */ | |
24c3c71a ZW |
2831 | { |
2832 | struct arglist *temp; | |
2833 | int i = 0; | |
0a8ad417 KG |
2834 | U_CHAR *tmp = (U_CHAR *) xmalloc (arglengths + 1); |
2835 | ||
24c3c71a | 2836 | for (temp = arg_ptrs; temp; temp = temp->next) { |
0a8ad417 | 2837 | memcpy (&tmp[i], temp->name, temp->length); |
24c3c71a ZW |
2838 | i += temp->length; |
2839 | if (temp->next != 0) { | |
0a8ad417 KG |
2840 | tmp[i++] = ','; |
2841 | tmp[i++] = ' '; | |
24c3c71a ZW |
2842 | } |
2843 | } | |
0a8ad417 KG |
2844 | tmp[i] = 0; |
2845 | defn->argnames = tmp; | |
2846 | ||
24c3c71a ZW |
2847 | } |
2848 | } else { | |
1e18a243 | 2849 | /* simple expansion or empty definition; skip leading whitespace */ |
2d2a86ae | 2850 | while (is_nvspace (*bp) && bp < limit) |
1e18a243 | 2851 | ++bp; |
24c3c71a ZW |
2852 | /* now everything from bp before limit is the definition. */ |
2853 | defn = collect_expansion (bp, limit, -1, 0); | |
0a8ad417 | 2854 | defn->argnames = (const U_CHAR *) ""; |
24c3c71a ZW |
2855 | } |
2856 | ||
2857 | hashcode = hashf (symname, sym_length, HASHSIZE); | |
2858 | ||
2859 | { | |
2860 | HASHNODE *hp; | |
2861 | if ((hp = lookup (symname, sym_length, hashcode)) == NULL) | |
2862 | hp = install (symname, sym_length, T_MACRO, hashcode); | |
2863 | else { | |
2864 | if (hp->type != T_MACRO || compare_defs (defn, hp->value.defn)) | |
2865 | warning ("\"%.*s\" redefined", sym_length, symname); | |
2866 | ||
2867 | /* Replace the old definition. */ | |
2868 | hp->type = T_MACRO; | |
2869 | } | |
2870 | ||
2871 | hp->value.defn = defn; | |
2872 | } | |
2873 | } | |
2874 | ||
2875 | /* | |
2876 | * return zero if two DEFINITIONs are isomorphic | |
2877 | */ | |
0a8ad417 | 2878 | static int |
24c3c71a ZW |
2879 | compare_defs (d1, d2) |
2880 | DEFINITION *d1, *d2; | |
2881 | { | |
b3694847 SS |
2882 | struct reflist *a1, *a2; |
2883 | U_CHAR *p1 = d1->expansion; | |
2884 | U_CHAR *p2 = d2->expansion; | |
24c3c71a ZW |
2885 | int first = 1; |
2886 | ||
2887 | if (d1->nargs != d2->nargs) | |
2888 | return 1; | |
0a8ad417 | 2889 | if (strcmp ((const char *)d1->argnames, (const char *)d2->argnames)) |
24c3c71a ZW |
2890 | return 1; |
2891 | for (a1 = d1->pattern, a2 = d2->pattern; a1 && a2; | |
2892 | a1 = a1->next, a2 = a2->next) { | |
2893 | if (!((a1->nchars == a2->nchars | |
0a8ad417 | 2894 | && ! strncmp ((const char *)p1, (const char *)p2, a1->nchars)) |
24c3c71a ZW |
2895 | || ! comp_def_part (first, p1, a1->nchars, p2, a2->nchars, 0)) |
2896 | || a1->argno != a2->argno | |
2897 | || a1->stringify != a2->stringify | |
2898 | || a1->raw_before != a2->raw_before | |
2899 | || a1->raw_after != a2->raw_after) | |
2900 | return 1; | |
2901 | first = 0; | |
2902 | p1 += a1->nchars; | |
2903 | p2 += a2->nchars; | |
2904 | } | |
2905 | if (a1 != a2) | |
2906 | return 1; | |
2907 | if (comp_def_part (first, p1, d1->length - (p1 - d1->expansion), | |
2908 | p2, d2->length - (p2 - d2->expansion), 1)) | |
2909 | return 1; | |
2910 | return 0; | |
2911 | } | |
2912 | ||
2913 | /* Return 1 if two parts of two macro definitions are effectively different. | |
2914 | One of the parts starts at BEG1 and has LEN1 chars; | |
2915 | the other has LEN2 chars at BEG2. | |
2916 | Any sequence of whitespace matches any other sequence of whitespace. | |
2917 | FIRST means these parts are the first of a macro definition; | |
2918 | so ignore leading whitespace entirely. | |
2919 | LAST means these parts are the last of a macro definition; | |
2920 | so ignore trailing whitespace entirely. */ | |
0a8ad417 | 2921 | static int |
24c3c71a ZW |
2922 | comp_def_part (first, beg1, len1, beg2, len2, last) |
2923 | int first; | |
0a8ad417 | 2924 | const U_CHAR *beg1, *beg2; |
24c3c71a ZW |
2925 | int len1, len2; |
2926 | int last; | |
2927 | { | |
b3694847 SS |
2928 | const U_CHAR *end1 = beg1 + len1; |
2929 | const U_CHAR *end2 = beg2 + len2; | |
24c3c71a | 2930 | if (first) { |
f6bbde28 ZW |
2931 | while (beg1 != end1 && is_space (*beg1)) beg1++; |
2932 | while (beg2 != end2 && is_space (*beg2)) beg2++; | |
24c3c71a ZW |
2933 | } |
2934 | if (last) { | |
f6bbde28 ZW |
2935 | while (beg1 != end1 && is_space (end1[-1])) end1--; |
2936 | while (beg2 != end2 && is_space (end2[-1])) end2--; | |
24c3c71a ZW |
2937 | } |
2938 | while (beg1 != end1 && beg2 != end2) { | |
f6bbde28 ZW |
2939 | if (is_space (*beg1) && is_space (*beg2)) { |
2940 | while (beg1 != end1 && is_space (*beg1)) beg1++; | |
2941 | while (beg2 != end2 && is_space (*beg2)) beg2++; | |
24c3c71a ZW |
2942 | } else if (*beg1 == *beg2) { |
2943 | beg1++; beg2++; | |
2944 | } else break; | |
2945 | } | |
2946 | return (beg1 != end1) || (beg2 != end2); | |
2947 | } | |
2948 | ||
2949 | /* Read a replacement list for a macro with parameters. | |
2950 | Build the DEFINITION structure. | |
2951 | Reads characters of text starting at BUF until LIMIT. | |
2952 | ARGLIST specifies the formal parameters to look for | |
2953 | in the text of the definition; NARGS is the number of args | |
2954 | in that list, or -1 for a macro name that wants no argument list. | |
2955 | MACRONAME is the macro name itself (so we can avoid recursive expansion) | |
2956 | and NAMELEN is its length in characters. | |
2957 | ||
2958 | Note that comments and backslash-newlines have already been deleted | |
2959 | from the argument. */ | |
2960 | ||
2961 | /* Leading and trailing Space, Tab, etc. are converted to markers | |
2962 | Newline Space, Newline Tab, etc. | |
2963 | Newline Space makes a space in the final output | |
2964 | but is discarded if stringified. (Newline Tab is similar but | |
2965 | makes a Tab instead.) | |
2966 | ||
2967 | If there is no trailing whitespace, a Newline Space is added at the end | |
2968 | to prevent concatenation that would be contrary to the standard. */ | |
2969 | ||
0a8ad417 | 2970 | static DEFINITION * |
24c3c71a ZW |
2971 | collect_expansion (buf, end, nargs, arglist) |
2972 | U_CHAR *buf, *end; | |
2973 | int nargs; | |
2974 | struct arglist *arglist; | |
2975 | { | |
2976 | DEFINITION *defn; | |
b3694847 | 2977 | U_CHAR *p, *limit, *lastp, *exp_p; |
24c3c71a ZW |
2978 | struct reflist *endpat = NULL; |
2979 | /* Pointer to first nonspace after last ## seen. */ | |
2980 | U_CHAR *concat = 0; | |
2981 | /* Pointer to first nonspace after last single-# seen. */ | |
2982 | U_CHAR *stringify = 0; | |
2983 | int maxsize; | |
2984 | int expected_delimiter = '\0'; | |
2985 | ||
2986 | /* Scan thru the replacement list, ignoring comments and quoted | |
2987 | strings, picking up on the macro calls. It does a linear search | |
2988 | thru the arg list on every potential symbol. Profiling might say | |
2989 | that something smarter should happen. */ | |
2990 | ||
2991 | if (end < buf) | |
2992 | abort (); | |
2993 | ||
2994 | /* Find the beginning of the trailing whitespace. */ | |
2995 | /* Find end of leading whitespace. */ | |
2996 | limit = end; | |
2997 | p = buf; | |
f6bbde28 ZW |
2998 | while (p < limit && is_space (limit[-1])) limit--; |
2999 | while (p < limit && is_space (*p)) p++; | |
24c3c71a ZW |
3000 | |
3001 | /* Allocate space for the text in the macro definition. | |
3002 | Leading and trailing whitespace chars need 2 bytes each. | |
3003 | Each other input char may or may not need 1 byte, | |
3004 | so this is an upper bound. | |
3005 | The extra 2 are for invented trailing newline-marker and final null. */ | |
3006 | maxsize = (sizeof (DEFINITION) | |
3007 | + 2 * (end - limit) + 2 * (p - buf) | |
3008 | + (limit - p) + 3); | |
3009 | defn = (DEFINITION *) xcalloc (1, maxsize); | |
3010 | ||
3011 | defn->nargs = nargs; | |
3012 | exp_p = defn->expansion = (U_CHAR *) defn + sizeof (DEFINITION); | |
3013 | lastp = exp_p; | |
3014 | ||
3015 | p = buf; | |
3016 | ||
3017 | /* Convert leading whitespace to Newline-markers. */ | |
f6bbde28 | 3018 | while (p < limit && is_space (*p)) { |
24c3c71a ZW |
3019 | *exp_p++ = '\n'; |
3020 | *exp_p++ = *p++; | |
3021 | } | |
3022 | ||
3023 | /* Process the main body of the definition. */ | |
3024 | while (p < limit) { | |
3025 | int skipped_arg = 0; | |
b3694847 | 3026 | U_CHAR c = *p++; |
24c3c71a ZW |
3027 | |
3028 | *exp_p++ = c; | |
3029 | ||
3030 | /* In -traditional mode, recognize arguments inside strings and | |
3031 | and character constants, and ignore special properties of #. | |
3032 | Arguments inside strings are considered "stringified", but no | |
3033 | extra quote marks are supplied. */ | |
3034 | switch (c) { | |
3035 | case '\'': | |
3036 | case '\"': | |
3037 | if (expected_delimiter != '\0') { | |
3038 | if (c == expected_delimiter) | |
3039 | expected_delimiter = '\0'; | |
3040 | } else | |
3041 | expected_delimiter = c; | |
3042 | break; | |
3043 | ||
3044 | case '\\': | |
3045 | /* Backslash quotes delimiters and itself, but not macro args. */ | |
3046 | if (expected_delimiter != 0 && p < limit | |
3047 | && (*p == expected_delimiter || *p == '\\')) { | |
3048 | *exp_p++ = *p++; | |
3049 | continue; | |
3050 | } | |
3051 | break; | |
3052 | ||
3053 | case '/': | |
3054 | if (expected_delimiter != '\0') /* No comments inside strings. */ | |
3055 | break; | |
3056 | if (*p == '*') { | |
3057 | /* If we find a comment that wasn't removed by handle_directive, | |
3058 | this must be -traditional. So replace the comment with | |
3059 | nothing at all. */ | |
3060 | exp_p--; | |
3061 | p += 1; | |
3062 | while (p < limit && !(p[-2] == '*' && p[-1] == '/')) | |
3063 | p++; | |
3064 | } | |
3065 | break; | |
3066 | } | |
3067 | ||
f6bbde28 | 3068 | if (is_idchar (c) && nargs > 0) { |
24c3c71a ZW |
3069 | U_CHAR *id_beg = p - 1; |
3070 | int id_len; | |
3071 | ||
3072 | --exp_p; | |
f6bbde28 | 3073 | while (p != limit && is_idchar (*p)) p++; |
24c3c71a ZW |
3074 | id_len = p - id_beg; |
3075 | ||
f6bbde28 | 3076 | if (is_idstart (c)) { |
b3694847 | 3077 | struct arglist *arg; |
24c3c71a ZW |
3078 | |
3079 | for (arg = arglist; arg != NULL; arg = arg->next) { | |
3080 | struct reflist *tpat; | |
3081 | ||
3082 | if (arg->name[0] == c | |
3083 | && arg->length == id_len | |
0a8ad417 KG |
3084 | && strncmp ((const char *)arg->name, |
3085 | (const char *)id_beg, id_len) == 0) { | |
24c3c71a ZW |
3086 | /* make a pat node for this arg and append it to the end of |
3087 | the pat list */ | |
3088 | tpat = (struct reflist *) xmalloc (sizeof (struct reflist)); | |
3089 | tpat->next = NULL; | |
3090 | tpat->raw_before = concat == id_beg; | |
3091 | tpat->raw_after = 0; | |
3092 | tpat->stringify = expected_delimiter != '\0'; | |
3093 | ||
3094 | if (endpat == NULL) | |
3095 | defn->pattern = tpat; | |
3096 | else | |
3097 | endpat->next = tpat; | |
3098 | endpat = tpat; | |
3099 | ||
3100 | tpat->argno = arg->argno; | |
3101 | tpat->nchars = exp_p - lastp; | |
3102 | { | |
b3694847 | 3103 | U_CHAR *p1 = p; |
24c3c71a ZW |
3104 | SKIP_WHITE_SPACE (p1); |
3105 | if (p1 + 2 <= limit && p1[0] == '#' && p1[1] == '#') | |
3106 | tpat->raw_after = 1; | |
3107 | } | |
3108 | lastp = exp_p; /* place to start copying from next time */ | |
3109 | skipped_arg = 1; | |
3110 | break; | |
3111 | } | |
3112 | } | |
3113 | } | |
3114 | ||
3115 | /* If this was not a macro arg, copy it into the expansion. */ | |
3116 | if (! skipped_arg) { | |
b3694847 | 3117 | U_CHAR *lim1 = p; |
24c3c71a ZW |
3118 | p = id_beg; |
3119 | while (p != lim1) | |
3120 | *exp_p++ = *p++; | |
3121 | if (stringify == id_beg) | |
3122 | error ("# operator should be followed by a macro argument name"); | |
3123 | } | |
3124 | } | |
3125 | } | |
3126 | ||
3127 | if (limit < end) { | |
3128 | /* Convert trailing whitespace to Newline-markers. */ | |
f6bbde28 | 3129 | while (limit < end && is_space (*limit)) { |
24c3c71a ZW |
3130 | *exp_p++ = '\n'; |
3131 | *exp_p++ = *limit++; | |
3132 | } | |
3133 | } | |
3134 | *exp_p = '\0'; | |
3135 | ||
3136 | defn->length = exp_p - defn->expansion; | |
3137 | ||
3138 | /* Crash now if we overrun the allocated size. */ | |
3139 | if (defn->length + 1 > maxsize) | |
3140 | abort (); | |
3141 | ||
3142 | return defn; | |
3143 | } | |
3144 | \f | |
3145 | /* | |
3146 | * interpret #line command. Remembers previously seen fnames | |
3147 | * in its very own hash table. | |
3148 | */ | |
3149 | #define FNAME_HASHSIZE 37 | |
0a8ad417 | 3150 | static void |
40bd4395 | 3151 | do_line (buf, limit, op) |
24c3c71a ZW |
3152 | U_CHAR *buf, *limit; |
3153 | FILE_BUF *op; | |
24c3c71a | 3154 | { |
b3694847 | 3155 | U_CHAR *bp; |
24c3c71a ZW |
3156 | FILE_BUF *ip = &instack[indepth]; |
3157 | FILE_BUF tem; | |
3158 | int new_lineno; | |
3159 | enum file_change_code file_change = same_file; | |
3160 | ||
3161 | /* Expand any macros. */ | |
3162 | tem = expand_to_temp_buffer (buf, limit, 0); | |
3163 | ||
3164 | /* Point to macroexpanded line, which is null-terminated now. */ | |
3165 | bp = tem.buf; | |
3166 | SKIP_WHITE_SPACE (bp); | |
3167 | ||
ce1cc601 | 3168 | if (!ISDIGIT (*bp)) { |
24c3c71a ZW |
3169 | error ("invalid format #line command"); |
3170 | return; | |
3171 | } | |
3172 | ||
3173 | /* The Newline at the end of this line remains to be processed. | |
3174 | To put the next line at the specified line number, | |
3175 | we must store a line number now that is one less. */ | |
3244472d | 3176 | new_lineno = atoi ((const char *)bp); |
24c3c71a ZW |
3177 | |
3178 | /* skip over the line number. */ | |
ce1cc601 | 3179 | while (ISDIGIT (*bp)) |
24c3c71a ZW |
3180 | bp++; |
3181 | ||
24c3c71a ZW |
3182 | SKIP_WHITE_SPACE (bp); |
3183 | ||
3184 | if (*bp == '\"') { | |
3185 | static HASHNODE *fname_table[FNAME_HASHSIZE]; | |
3186 | HASHNODE *hp, **hash_bucket; | |
3187 | U_CHAR *fname; | |
3188 | int fname_length; | |
3189 | ||
3190 | fname = ++bp; | |
3191 | ||
3192 | while (*bp && *bp != '\"') | |
3193 | bp++; | |
3194 | if (*bp != '\"') { | |
3195 | error ("invalid format #line command"); | |
3196 | return; | |
3197 | } | |
3198 | ||
3199 | fname_length = bp - fname; | |
3200 | ||
3201 | bp++; | |
3202 | SKIP_WHITE_SPACE (bp); | |
3203 | if (*bp) { | |
3204 | if (*bp == '1') | |
3205 | file_change = enter_file; | |
3206 | else if (*bp == '2') | |
3207 | file_change = leave_file; | |
3208 | else { | |
3209 | error ("invalid format #line command"); | |
3210 | return; | |
3211 | } | |
3212 | ||
3213 | bp++; | |
3214 | SKIP_WHITE_SPACE (bp); | |
3215 | if (*bp) { | |
3216 | error ("invalid format #line command"); | |
3217 | return; | |
3218 | } | |
3219 | } | |
3220 | ||
3221 | hash_bucket = | |
3222 | &fname_table[hashf (fname, fname_length, FNAME_HASHSIZE)]; | |
3223 | for (hp = *hash_bucket; hp != NULL; hp = hp->next) | |
3224 | if (hp->length == fname_length && | |
0a8ad417 | 3225 | strncmp (hp->value.cpval, (const char *)fname, fname_length) == 0) { |
24c3c71a ZW |
3226 | ip->fname = hp->value.cpval; |
3227 | break; | |
3228 | } | |
3229 | if (hp == 0) { | |
3230 | char *q; | |
3231 | /* Didn't find it; cons up a new one. */ | |
3232 | hp = (HASHNODE *) xcalloc (1, sizeof (HASHNODE) + fname_length + 1); | |
3233 | hp->next = *hash_bucket; | |
3234 | *hash_bucket = hp; | |
3235 | ||
3236 | hp->length = fname_length; | |
3237 | ip->fname = hp->value.cpval = q = ((char *) hp) + sizeof (HASHNODE); | |
3238 | memcpy (q, fname, fname_length); | |
3239 | } | |
3240 | } else if (*bp) { | |
3241 | error ("invalid format #line command"); | |
3242 | return; | |
3243 | } | |
3244 | ||
3245 | ip->lineno = new_lineno; | |
3246 | output_line_command (ip, op, 0, file_change); | |
3244472d | 3247 | ip->bufp++; /* Skip the new line. */ |
24c3c71a ZW |
3248 | check_expand (op, ip->length - (ip->bufp - ip->buf)); |
3249 | } | |
3250 | ||
3251 | /* | |
3252 | * remove all definitions of symbol from symbol table. | |
3253 | * according to un*x /lib/cpp, it is not an error to undef | |
3254 | * something that has no definitions, so it isn't one here either. | |
3255 | */ | |
0a8ad417 | 3256 | static void |
40bd4395 | 3257 | do_undef (buf, limit, op) |
24c3c71a ZW |
3258 | U_CHAR *buf; |
3259 | U_CHAR *limit ATTRIBUTE_UNUSED; | |
3260 | FILE_BUF *op ATTRIBUTE_UNUSED; | |
24c3c71a ZW |
3261 | { |
3262 | HASHNODE *hp; | |
3263 | ||
3264 | SKIP_WHITE_SPACE (buf); | |
3265 | ||
f6bbde28 | 3266 | if (! strncmp ((const char *)buf, "defined", 7) && ! is_idchar (buf[7])) |
24c3c71a ZW |
3267 | warning ("undefining `defined'"); |
3268 | ||
3269 | while ((hp = lookup (buf, -1, -1)) != NULL) { | |
3270 | if (hp->type != T_MACRO) | |
3271 | warning ("undefining `%s'", hp->name); | |
3272 | delete_macro (hp); | |
3273 | } | |
3274 | } | |
3275 | ||
4eb191f3 NB |
3276 | /* Read the tokens of the answer into the macro pool. Only commit the |
3277 | memory if we intend it as permanent storage, i.e. the #assert case. | |
3278 | Returns 0 on success. */ | |
3279 | ||
3280 | static int | |
3281 | parse_answer (buf, limit, answerp, type) | |
3282 | const unsigned char *buf, *limit; | |
3283 | struct answer **answerp; | |
3284 | int type; | |
3285 | { | |
3286 | const unsigned char *start; | |
3287 | ||
3288 | /* Skip leading whitespace. */ | |
3289 | if (buf < limit && *buf == ' ') | |
3290 | buf++; | |
3291 | ||
3292 | /* Parentheses are optional here. */ | |
7682e7bc | 3293 | if (buf == limit && type == T_UNASSERT) |
4eb191f3 NB |
3294 | return 0; |
3295 | ||
3296 | if (buf == limit || *buf++ != '(') | |
3297 | { | |
7682e7bc NB |
3298 | if (type == T_IF) |
3299 | return 0; | |
3300 | ||
4eb191f3 NB |
3301 | error ("missing '(' after predicate"); |
3302 | return 1; | |
3303 | } | |
3304 | ||
3305 | /* Drop whitespace at start. */ | |
3306 | while (buf < limit && *buf == ' ') | |
3307 | buf++; | |
3308 | ||
3309 | start = buf; | |
3310 | while (buf < limit && *buf != ')') | |
3311 | buf++; | |
3312 | ||
3313 | if (buf == limit) | |
3314 | { | |
3315 | error ("missing ')' to complete answer"); | |
3316 | return 1; | |
3317 | } | |
3318 | ||
3319 | if (buf == start) | |
3320 | { | |
3321 | error ("predicate's answer is empty"); | |
3322 | return 1; | |
3323 | } | |
3324 | ||
3325 | if ((type == T_ASSERT || type == T_UNASSERT) && buf + 1 != limit) | |
3326 | { | |
3327 | error ("extra text at end of directive"); | |
3328 | return 1; | |
3329 | } | |
3330 | ||
3331 | /* Lose trailing whitespace. */ | |
3332 | if (buf[-1] == ' ') | |
3333 | buf--; | |
3334 | ||
3335 | *answerp = (struct answer *) xmalloc (sizeof (struct answer)); | |
3336 | (*answerp)->answer = start; | |
3337 | (*answerp)->len = buf - start; | |
3338 | ||
3339 | return 0; | |
3340 | } | |
3341 | ||
3342 | /* Parses an assertion, returning a pointer to the hash node of the | |
3343 | predicate, or 0 on error. If an answer was supplied, it is placed | |
3344 | in ANSWERP, otherwise it is set to 0. */ | |
3345 | static HASHNODE * | |
3346 | parse_assertion (buf, limit, answerp, type) | |
3347 | const unsigned char *buf, *limit; | |
3348 | struct answer **answerp; | |
3349 | int type; | |
3350 | { | |
3351 | HASHNODE *result = 0; | |
3352 | const unsigned char *climit; | |
3353 | unsigned char *bp, *symname = canonicalize_text (buf, limit, &climit); | |
3354 | unsigned int len; | |
3355 | ||
3356 | bp = symname; | |
f6bbde28 | 3357 | if (bp < climit && is_idstart (*bp)) |
7682e7bc NB |
3358 | { |
3359 | do | |
3360 | bp++; | |
f6bbde28 | 3361 | while (bp < climit && is_idchar (*bp)); |
7682e7bc | 3362 | } |
4eb191f3 NB |
3363 | len = bp - symname; |
3364 | ||
3365 | *answerp = 0; | |
3366 | if (len == 0) | |
3367 | { | |
3368 | if (symname == climit) | |
3369 | error ("assertion without predicate"); | |
3370 | else | |
3371 | error ("predicate must be an identifier"); | |
3372 | } | |
7682e7bc NB |
3373 | /* Unfortunately, because of the way we handle #if, we don't avoid |
3374 | macro expansion in answers. This is not easy to fix. */ | |
4eb191f3 NB |
3375 | else if (parse_answer (bp, climit, answerp, type) == 0) |
3376 | { | |
3377 | unsigned char *sym = alloca (len + 1); | |
3378 | int hashcode; | |
3379 | ||
3380 | /* Prefix '#' to get it out of macro namespace. */ | |
3381 | sym[0] = '#'; | |
3382 | memcpy (sym + 1, symname, len); | |
3383 | ||
3384 | hashcode = hashf (sym, len + 1, HASHSIZE); | |
3385 | result = lookup (sym, len + 1, hashcode); | |
3386 | if (result == 0) | |
3387 | result = install (sym, len + 1, T_UNUSED, hashcode); | |
3388 | } | |
3389 | ||
3390 | return result; | |
3391 | } | |
3392 | ||
7682e7bc NB |
3393 | /* Test an assertion within a preprocessor conditional. Returns zero |
3394 | on error or failure, one on success. */ | |
3395 | int | |
3396 | test_assertion (pbuf) | |
3397 | unsigned char **pbuf; /* NUL-terminated. */ | |
3398 | { | |
3399 | unsigned char *buf = *pbuf; | |
3400 | unsigned char *limit = buf + strlen ((char *) buf); | |
3401 | struct answer *answer; | |
3402 | HASHNODE *node; | |
3403 | int result = 0; | |
3404 | ||
3405 | node = parse_assertion (buf, limit, &answer, T_IF); | |
3406 | if (node) | |
3407 | { | |
3408 | result = (node->type == T_ASSERT && | |
3409 | (answer == 0 || *find_answer (node, answer) != 0)); | |
3410 | ||
3411 | /* Yuk. We update pbuf to point after the assertion test. | |
3412 | First, move past the identifier. */ | |
f6bbde28 | 3413 | if (is_space (*buf)) |
7682e7bc | 3414 | buf++; |
f6bbde28 | 3415 | while (is_idchar (*buf)) |
7682e7bc NB |
3416 | buf++; |
3417 | /* If we have an answer, we need to move past the parentheses. */ | |
3418 | if (answer) | |
3419 | while (*buf++ != ')') | |
3420 | ; | |
3421 | *pbuf = buf; | |
3422 | } | |
3423 | ||
3424 | return result; | |
3425 | } | |
3426 | ||
e793c609 NB |
3427 | /* Handle a #error directive. */ |
3428 | static void | |
3429 | do_error (buf, limit, op) | |
3430 | U_CHAR *buf; | |
3431 | U_CHAR *limit; | |
3432 | FILE_BUF *op ATTRIBUTE_UNUSED; | |
3433 | { | |
1df97aa4 | 3434 | error ("#error%.*s", (int) (limit - buf), buf); |
e793c609 NB |
3435 | } |
3436 | ||
3244472d NB |
3437 | /* Handle a #warning directive. */ |
3438 | static void | |
3439 | do_warning (buf, limit, op) | |
3440 | U_CHAR *buf; | |
3441 | U_CHAR *limit; | |
3442 | FILE_BUF *op ATTRIBUTE_UNUSED; | |
3443 | { | |
3444 | warning ("#warning%.*s", (int) (limit - buf), buf); | |
3445 | } | |
3446 | ||
4eb191f3 | 3447 | /* Handle a #assert directive. */ |
40bd4395 NB |
3448 | static void |
3449 | do_assert (buf, limit, op) | |
4eb191f3 NB |
3450 | U_CHAR *buf; |
3451 | U_CHAR *limit; | |
40bd4395 NB |
3452 | FILE_BUF *op ATTRIBUTE_UNUSED; |
3453 | { | |
4eb191f3 NB |
3454 | struct answer *new_answer; |
3455 | HASHNODE *node; | |
3456 | ||
3457 | node = parse_assertion (buf, limit, &new_answer, T_ASSERT); | |
3458 | if (node) | |
3459 | { | |
3460 | /* Place the new answer in the answer list. First check there | |
3461 | is not a duplicate. */ | |
3462 | new_answer->next = 0; | |
3463 | if (node->type == T_ASSERT) | |
3464 | { | |
3465 | if (*find_answer (node, new_answer)) | |
3466 | { | |
3467 | free (new_answer); | |
3468 | warning ("\"%s\" re-asserted", node->name + 1); | |
3469 | return; | |
3470 | } | |
3471 | new_answer->next = node->value.answers; | |
3472 | } | |
3473 | node->type = T_ASSERT; | |
3474 | node->value.answers = new_answer; | |
3475 | } | |
40bd4395 NB |
3476 | } |
3477 | ||
3478 | /* Function body to be provided later. */ | |
3479 | static void | |
3480 | do_unassert (buf, limit, op) | |
4eb191f3 NB |
3481 | U_CHAR *buf; |
3482 | U_CHAR *limit; | |
40bd4395 NB |
3483 | FILE_BUF *op ATTRIBUTE_UNUSED; |
3484 | { | |
4eb191f3 NB |
3485 | HASHNODE *node; |
3486 | struct answer *answer; | |
3487 | ||
3488 | node = parse_assertion (buf, limit, &answer, T_UNASSERT); | |
3489 | /* It isn't an error to #unassert something that isn't asserted. */ | |
3490 | if (node) | |
3491 | { | |
3492 | if (node->type == T_ASSERT) | |
3493 | { | |
3494 | if (answer) | |
3495 | { | |
3496 | struct answer **p = find_answer (node, answer), *temp; | |
3497 | ||
3498 | /* Remove the answer from the list. */ | |
3499 | temp = *p; | |
3500 | if (temp) | |
3501 | *p = temp->next; | |
3502 | ||
3503 | /* Did we free the last answer? */ | |
3504 | if (node->value.answers == 0) | |
3505 | delete_macro (node); | |
3506 | } | |
3507 | else | |
3508 | delete_macro (node); | |
3509 | } | |
3510 | ||
3511 | free (answer); | |
3512 | } | |
3513 | } | |
3514 | ||
3515 | /* Returns a pointer to the pointer to the answer in the answer chain, | |
3516 | or a pointer to NULL if the answer is not in the chain. */ | |
3517 | static struct answer ** | |
3518 | find_answer (node, candidate) | |
3519 | HASHNODE *node; | |
3520 | const struct answer *candidate; | |
3521 | { | |
3522 | struct answer **result; | |
3523 | ||
3524 | for (result = &node->value.answers; *result; result = &(*result)->next) | |
3525 | { | |
3526 | struct answer *answer = *result; | |
3527 | ||
3528 | if (answer->len == candidate->len | |
3529 | && !memcmp (answer->answer, candidate->answer, answer->len)) | |
3530 | break; | |
3531 | } | |
3532 | ||
3533 | return result; | |
3534 | } | |
3535 | ||
3536 | /* Return a malloced buffer with leading and trailing whitespace | |
3537 | removed, and all instances of internal whitespace reduced to a | |
3538 | single space. */ | |
3539 | static unsigned char * | |
3540 | canonicalize_text (buf, limit, climit) | |
3541 | const unsigned char *buf, *limit, **climit; | |
3542 | { | |
3543 | unsigned int len = limit - buf; | |
3544 | unsigned char *result = (unsigned char *) xmalloc (len), *dest; | |
3545 | ||
3546 | for (dest = result; buf < limit;) | |
3547 | { | |
f6bbde28 | 3548 | if (! is_space (*buf)) |
4eb191f3 NB |
3549 | *dest++ = *buf++; |
3550 | else | |
3551 | { | |
f6bbde28 | 3552 | while (++buf < limit && is_space (*buf)) |
4eb191f3 NB |
3553 | ; |
3554 | if (dest != result && buf != limit) | |
3555 | *dest++ = ' '; | |
3556 | } | |
3557 | } | |
3558 | ||
3559 | *climit = dest; | |
3560 | return result; | |
40bd4395 NB |
3561 | } |
3562 | ||
24c3c71a ZW |
3563 | /* |
3564 | * handle #if command by | |
3565 | * 1) inserting special `defined' keyword into the hash table | |
3566 | * that gets turned into 0 or 1 by special_symbol (thus, | |
3567 | * if the luser has a symbol called `defined' already, it won't | |
3568 | * work inside the #if command) | |
3569 | * 2) rescan the input into a temporary output buffer | |
3570 | * 3) pass the output buffer to the yacc parser and collect a value | |
3571 | * 4) clean up the mess left from steps 1 and 2. | |
3572 | * 5) call conditional_skip to skip til the next #endif (etc.), | |
3573 | * or not, depending on the value from step 3. | |
3574 | */ | |
0a8ad417 | 3575 | static void |
40bd4395 | 3576 | do_if (buf, limit, op) |
24c3c71a ZW |
3577 | U_CHAR *buf, *limit; |
3578 | FILE_BUF *op ATTRIBUTE_UNUSED; | |
24c3c71a ZW |
3579 | { |
3580 | int value; | |
3581 | FILE_BUF *ip = &instack[indepth]; | |
3582 | ||
3583 | value = eval_if_expression (buf, limit - buf); | |
3584 | conditional_skip (ip, value == 0, T_IF); | |
3585 | } | |
3586 | ||
3587 | /* | |
3588 | * handle a #elif directive by not changing if_stack either. | |
3589 | * see the comment above do_else. | |
3590 | */ | |
0a8ad417 | 3591 | static void |
40bd4395 | 3592 | do_elif (buf, limit, op) |
24c3c71a ZW |
3593 | U_CHAR *buf, *limit; |
3594 | FILE_BUF *op; | |
24c3c71a ZW |
3595 | { |
3596 | int value; | |
3597 | FILE_BUF *ip = &instack[indepth]; | |
3598 | ||
3599 | if (if_stack == instack[indepth].if_stack) { | |
3600 | error ("#elif not within a conditional"); | |
3601 | return; | |
3602 | } else { | |
3603 | if (if_stack->type != T_IF && if_stack->type != T_ELIF) { | |
3604 | error ("#elif after #else"); | |
3605 | fprintf (stderr, " (matches line %d", if_stack->lineno); | |
3606 | if (if_stack->fname != NULL && ip->fname != NULL && | |
3607 | strcmp (if_stack->fname, ip->fname) != 0) | |
3608 | fprintf (stderr, ", file %s", if_stack->fname); | |
3609 | fprintf (stderr, ")\n"); | |
3610 | } | |
3611 | if_stack->type = T_ELIF; | |
3612 | } | |
3613 | ||
3614 | if (if_stack->if_succeeded) | |
3615 | skip_if_group (ip, 0); | |
3616 | else { | |
3617 | value = eval_if_expression (buf, limit - buf); | |
3618 | if (value == 0) | |
3619 | skip_if_group (ip, 0); | |
3620 | else { | |
3621 | ++if_stack->if_succeeded; /* continue processing input */ | |
3622 | output_line_command (ip, op, 1, same_file); | |
3623 | } | |
3624 | } | |
3625 | } | |
3626 | ||
3627 | /* | |
3628 | * evaluate a #if expression in BUF, of length LENGTH, | |
3629 | * then parse the result as a C expression and return the value as an int. | |
3630 | */ | |
0a8ad417 | 3631 | static int |
24c3c71a | 3632 | eval_if_expression (buf, length) |
0a8ad417 | 3633 | const U_CHAR *buf; |
24c3c71a ZW |
3634 | int length; |
3635 | { | |
3636 | FILE_BUF temp_obuf; | |
3637 | HASHNODE *save_defined; | |
3638 | int value; | |
3639 | ||
3640 | save_defined = install (U"defined", -1, T_SPEC_DEFINED, -1); | |
3641 | temp_obuf = expand_to_temp_buffer (buf, buf + length, 0); | |
3642 | delete_macro (save_defined); /* clean up special symbol */ | |
3643 | ||
0a8ad417 | 3644 | value = parse_c_expression ((const char *)temp_obuf.buf); |
24c3c71a ZW |
3645 | |
3646 | free (temp_obuf.buf); | |
3647 | ||
3648 | return value; | |
3649 | } | |
3650 | ||
3651 | /* | |
3652 | * routine to handle ifdef/ifndef. Try to look up the symbol, | |
3653 | * then do or don't skip to the #endif/#else/#elif depending | |
3654 | * on what directive is actually being processed. | |
3655 | */ | |
0a8ad417 | 3656 | static void |
40bd4395 | 3657 | do_xifdef (buf, limit, type) |
24c3c71a | 3658 | U_CHAR *buf, *limit; |
40bd4395 | 3659 | enum node_type type; |
24c3c71a ZW |
3660 | { |
3661 | int skip; | |
3662 | FILE_BUF *ip = &instack[indepth]; | |
3663 | U_CHAR *end; | |
3664 | ||
3665 | /* Discard leading and trailing whitespace. */ | |
3666 | SKIP_WHITE_SPACE (buf); | |
f6bbde28 | 3667 | while (limit != buf && is_nvspace (limit[-1])) limit--; |
24c3c71a ZW |
3668 | |
3669 | /* Find the end of the identifier at the beginning. */ | |
f6bbde28 | 3670 | for (end = buf; is_idchar (*end); end++); |
24c3c71a | 3671 | |
40bd4395 NB |
3672 | if (end == buf) |
3673 | skip = (type == T_IFDEF); | |
3674 | else | |
3675 | skip = (lookup (buf, end-buf, -1) == NULL) ^ (type == T_IFNDEF); | |
24c3c71a ZW |
3676 | |
3677 | conditional_skip (ip, skip, T_IF); | |
3678 | } | |
3679 | ||
40bd4395 NB |
3680 | static void |
3681 | do_ifdef (buf, limit, op) | |
3682 | U_CHAR *buf, *limit; | |
3683 | FILE_BUF *op ATTRIBUTE_UNUSED; | |
3684 | { | |
3685 | do_xifdef (buf, limit, T_IFDEF); | |
3686 | } | |
3687 | ||
3688 | static void | |
3689 | do_ifndef (buf, limit, op) | |
3690 | U_CHAR *buf, *limit; | |
3691 | FILE_BUF *op ATTRIBUTE_UNUSED; | |
3692 | { | |
3693 | do_xifdef (buf, limit, T_IFNDEF); | |
3694 | } | |
3695 | ||
24c3c71a ZW |
3696 | /* |
3697 | * push TYPE on stack; then, if SKIP is nonzero, skip ahead. | |
3698 | */ | |
0a8ad417 | 3699 | static void |
24c3c71a ZW |
3700 | conditional_skip (ip, skip, type) |
3701 | FILE_BUF *ip; | |
3702 | int skip; | |
3703 | enum node_type type; | |
3704 | { | |
3705 | IF_STACK_FRAME *temp; | |
3706 | ||
3707 | temp = (IF_STACK_FRAME *) xcalloc (1, sizeof (IF_STACK_FRAME)); | |
3708 | temp->fname = ip->fname; | |
3709 | temp->lineno = ip->lineno; | |
3710 | temp->next = if_stack; | |
3711 | if_stack = temp; | |
3712 | ||
3713 | if_stack->type = type; | |
3714 | ||
3715 | if (skip != 0) { | |
3716 | skip_if_group (ip, 0); | |
3717 | return; | |
3718 | } else { | |
3719 | ++if_stack->if_succeeded; | |
3720 | output_line_command (ip, &outbuf, 1, same_file); | |
3721 | } | |
3722 | } | |
3723 | ||
3724 | /* | |
3725 | * skip to #endif, #else, or #elif. adjust line numbers, etc. | |
3726 | * leaves input ptr at the sharp sign found. | |
3727 | * If ANY is nonzero, return at next directive of any sort. | |
3728 | */ | |
0a8ad417 | 3729 | static void |
24c3c71a ZW |
3730 | skip_if_group (ip, any) |
3731 | FILE_BUF *ip; | |
3732 | int any; | |
3733 | { | |
b3694847 SS |
3734 | U_CHAR *bp = ip->bufp, *cp; |
3735 | U_CHAR *endb = ip->buf + ip->length; | |
0b5826ac | 3736 | const struct directive *kt; |
24c3c71a ZW |
3737 | IF_STACK_FRAME *save_if_stack = if_stack; /* don't pop past here */ |
3738 | U_CHAR *beg_of_line = bp; | |
3739 | ||
3740 | while (bp < endb) { | |
3741 | switch (*bp++) { | |
3742 | case '/': /* possible comment */ | |
3743 | if (*bp == '\\' && bp[1] == '\n') | |
3744 | newline_fix (bp); | |
3745 | if (*bp == '*') { | |
3746 | ip->bufp = ++bp; | |
3747 | bp = skip_to_end_of_comment (ip, &ip->lineno); | |
3748 | } | |
3749 | break; | |
3750 | case '\"': | |
3751 | case '\'': | |
3752 | bp = skip_quoted_string (bp - 1, endb, ip->lineno, &ip->lineno, 0, 0); | |
3753 | break; | |
3754 | case '\\': | |
3755 | /* Char after backslash loses its special meaning. */ | |
3756 | if (bp < endb) { | |
3757 | if (*bp == '\n') | |
3758 | ++ip->lineno; /* But do update the line-count. */ | |
3759 | bp++; | |
3760 | } | |
3761 | break; | |
3762 | case '\n': | |
3763 | ++ip->lineno; | |
3764 | beg_of_line = bp; | |
3765 | break; | |
3766 | case '#': | |
3767 | ip->bufp = bp - 1; | |
3768 | ||
3769 | /* # keyword: a # must be first nonblank char on the line */ | |
3770 | if (beg_of_line == 0) | |
3771 | break; | |
3772 | /* Scan from start of line, skipping whitespace, comments | |
3773 | and backslash-newlines, and see if we reach this #. | |
3774 | If not, this # is not special. */ | |
3775 | bp = beg_of_line; | |
3776 | while (1) { | |
f6bbde28 | 3777 | if (is_nvspace (*bp)) |
24c3c71a ZW |
3778 | bp++; |
3779 | else if (*bp == '\\' && bp[1] == '\n') | |
3780 | bp += 2; | |
3781 | else if (*bp == '/' && bp[1] == '*') { | |
3782 | bp += 2; | |
3783 | while (!(*bp == '*' && bp[1] == '/')) { | |
3784 | if (*bp == '\n') | |
3785 | ip->lineno++; | |
3786 | bp++; | |
3787 | } | |
3788 | bp += 2; | |
3789 | } | |
3790 | else break; | |
3791 | } | |
3792 | if (bp != ip->bufp) { | |
3793 | bp = ip->bufp + 1; /* Reset bp to after the #. */ | |
3794 | break; | |
3795 | } | |
3796 | ||
3797 | bp = ip->bufp + 1; /* Point after '#'. */ | |
3798 | ||
3799 | /* Skip whitespace and \-newline. */ | |
3800 | while (1) { | |
f6bbde28 | 3801 | if (is_nvspace (*bp)) |
24c3c71a ZW |
3802 | bp++; |
3803 | else if (*bp == '\\' && bp[1] == '\n') | |
3804 | bp += 2; | |
3805 | else if (*bp == '/' && bp[1] == '*') { | |
3806 | bp += 2; | |
3807 | while (!(*bp == '*' && bp[1] == '/')) | |
3808 | bp++; | |
3809 | bp += 2; | |
3810 | } | |
3811 | else break; | |
3812 | } | |
3813 | ||
3814 | cp = bp; | |
3815 | ||
3816 | /* Now find end of directive name. | |
3817 | If we encounter a backslash-newline, exchange it with any following | |
3818 | symbol-constituents so that we end up with a contiguous name. */ | |
3819 | ||
3820 | while (1) { | |
f6bbde28 | 3821 | if (is_idchar (*bp)) |
24c3c71a ZW |
3822 | bp++; |
3823 | else { | |
3824 | if (*bp == '\\' && bp[1] == '\n') | |
3825 | name_newline_fix (bp); | |
f6bbde28 | 3826 | if (is_idchar (*bp)) |
24c3c71a ZW |
3827 | bp++; |
3828 | else break; | |
3829 | } | |
3830 | } | |
3831 | ||
3832 | for (kt = directive_table; kt->length >= 0; kt++) { | |
3833 | IF_STACK_FRAME *temp; | |
0a8ad417 | 3834 | if (strncmp ((const char *)cp, kt->name, kt->length) == 0 |
f6bbde28 | 3835 | && !is_idchar (cp[kt->length])) { |
24c3c71a ZW |
3836 | |
3837 | /* If we are asked to return on next directive, | |
3838 | do so now. */ | |
3839 | if (any) | |
3840 | return; | |
3841 | ||
3842 | switch (kt->type) { | |
3843 | case T_IF: | |
3844 | case T_IFDEF: | |
3845 | case T_IFNDEF: | |
3846 | temp = (IF_STACK_FRAME *) xcalloc (1, sizeof (IF_STACK_FRAME)); | |
3847 | temp->next = if_stack; | |
3848 | if_stack = temp; | |
3849 | temp->lineno = ip->lineno; | |
3850 | temp->fname = ip->fname; | |
3851 | temp->type = kt->type; | |
3852 | break; | |
3853 | case T_ELSE: | |
3854 | case T_ENDIF: | |
3855 | case T_ELIF: | |
3856 | if (if_stack == instack[indepth].if_stack) { | |
3857 | error ("#%s not within a conditional", kt->name); | |
3858 | break; | |
3859 | } | |
3860 | else if (if_stack == save_if_stack) | |
3861 | return; /* found what we came for */ | |
3862 | ||
3863 | if (kt->type != T_ENDIF) { | |
3864 | if (if_stack->type == T_ELSE) | |
3865 | error ("#else or #elif after #else"); | |
3866 | if_stack->type = kt->type; | |
3867 | break; | |
3868 | } | |
3869 | ||
3870 | temp = if_stack; | |
3871 | if_stack = if_stack->next; | |
3872 | free (temp); | |
3873 | break; | |
3874 | ||
3875 | default: | |
3876 | /* Anything else is ignored. */ | |
3877 | break; | |
3878 | } | |
3879 | break; | |
3880 | } | |
3881 | } | |
3882 | } | |
3883 | } | |
3884 | ip->bufp = bp; | |
3885 | /* after this returns, rescan will exit because ip->bufp | |
3886 | now points to the end of the buffer. | |
3887 | rescan is responsible for the error message also. */ | |
3888 | } | |
3889 | ||
3890 | /* | |
3891 | * handle a #else directive. Do this by just continuing processing | |
3892 | * without changing if_stack ; this is so that the error message | |
3893 | * for missing #endif's etc. will point to the original #if. It | |
3894 | * is possible that something different would be better. | |
3895 | */ | |
0a8ad417 | 3896 | static void |
40bd4395 | 3897 | do_else (buf, limit, op) |
24c3c71a ZW |
3898 | U_CHAR *buf ATTRIBUTE_UNUSED; |
3899 | U_CHAR *limit ATTRIBUTE_UNUSED; | |
3900 | FILE_BUF *op; | |
24c3c71a ZW |
3901 | { |
3902 | FILE_BUF *ip = &instack[indepth]; | |
3903 | ||
3904 | if (if_stack == instack[indepth].if_stack) { | |
3905 | error ("#else not within a conditional"); | |
3906 | return; | |
3907 | } else { | |
3908 | if (if_stack->type != T_IF && if_stack->type != T_ELIF) { | |
3909 | error ("#else after #else"); | |
3910 | fprintf (stderr, " (matches line %d", if_stack->lineno); | |
3911 | if (strcmp (if_stack->fname, ip->fname) != 0) | |
3912 | fprintf (stderr, ", file %s", if_stack->fname); | |
3913 | fprintf (stderr, ")\n"); | |
3914 | } | |
3915 | if_stack->type = T_ELSE; | |
3916 | } | |
3917 | ||
3918 | if (if_stack->if_succeeded) | |
3919 | skip_if_group (ip, 0); | |
3920 | else { | |
3921 | ++if_stack->if_succeeded; /* continue processing input */ | |
3922 | output_line_command (ip, op, 1, same_file); | |
3923 | } | |
3924 | } | |
3925 | ||
3926 | /* | |
3927 | * unstack after #endif command | |
3928 | */ | |
0a8ad417 | 3929 | static void |
40bd4395 | 3930 | do_endif (buf, limit, op) |
24c3c71a ZW |
3931 | U_CHAR *buf ATTRIBUTE_UNUSED; |
3932 | U_CHAR *limit ATTRIBUTE_UNUSED; | |
3933 | FILE_BUF *op; | |
24c3c71a ZW |
3934 | { |
3935 | if (if_stack == instack[indepth].if_stack) | |
3936 | error ("unbalanced #endif"); | |
3937 | else { | |
3938 | IF_STACK_FRAME *temp = if_stack; | |
3939 | if_stack = if_stack->next; | |
3940 | free (temp); | |
3941 | output_line_command (&instack[indepth], op, 1, same_file); | |
3942 | } | |
3943 | } | |
3944 | ||
3945 | /* | |
3946 | * Skip a comment, assuming the input ptr immediately follows the | |
3947 | * initial slash-star. Bump line counter as necessary. | |
3948 | * (The canonical line counter is &ip->lineno). | |
3949 | * Don't use this routine (or the next one) if bumping the line | |
3950 | * counter is not sufficient to deal with newlines in the string. | |
3951 | */ | |
0a8ad417 | 3952 | static U_CHAR * |
24c3c71a | 3953 | skip_to_end_of_comment (ip, line_counter) |
b3694847 | 3954 | FILE_BUF *ip; |
24c3c71a ZW |
3955 | int *line_counter; /* place to remember newlines, or NULL */ |
3956 | { | |
b3694847 SS |
3957 | U_CHAR *limit = ip->buf + ip->length; |
3958 | U_CHAR *bp = ip->bufp; | |
24c3c71a ZW |
3959 | FILE_BUF *op = &outbuf; /* JF */ |
3960 | int output = put_out_comments && !line_counter; | |
3961 | ||
3962 | /* JF this line_counter stuff is a crock to make sure the | |
3963 | comment is only put out once, no matter how many times | |
3964 | the comment is skipped. It almost works */ | |
3965 | if (output) { | |
3966 | *op->bufp++ = '/'; | |
3967 | *op->bufp++ = '*'; | |
3968 | } | |
3969 | while (bp < limit) { | |
3970 | if (output) | |
3971 | *op->bufp++ = *bp; | |
3972 | switch (*bp++) { | |
3973 | case '/': | |
3974 | if (warn_comments && bp < limit && *bp == '*') | |
3975 | warning("`/*' within comment"); | |
3976 | break; | |
3977 | case '\n': | |
3978 | if (line_counter != NULL) | |
3979 | ++*line_counter; | |
3980 | if (output) | |
3981 | ++op->lineno; | |
3982 | break; | |
3983 | case '*': | |
3984 | if (*bp == '\\' && bp[1] == '\n') | |
3985 | newline_fix (bp); | |
3986 | if (*bp == '/') { | |
3987 | if (output) | |
3988 | *op->bufp++ = '/'; | |
3989 | ip->bufp = ++bp; | |
3990 | return bp; | |
3991 | } | |
3992 | break; | |
3993 | } | |
3994 | } | |
3995 | ip->bufp = bp; | |
3996 | return bp; | |
3997 | } | |
3998 | ||
3999 | /* | |
4000 | * Skip over a quoted string. BP points to the opening quote. | |
4001 | * Returns a pointer after the closing quote. Don't go past LIMIT. | |
4002 | * START_LINE is the line number of the starting point (but it need | |
4003 | * not be valid if the starting point is inside a macro expansion). | |
4004 | * | |
4005 | * The input stack state is not changed. | |
4006 | * | |
4007 | * If COUNT_NEWLINES is nonzero, it points to an int to increment | |
4008 | * for each newline passed. | |
4009 | * | |
4010 | * If BACKSLASH_NEWLINES_P is nonzero, store 1 thru it | |
4011 | * if we pass a backslash-newline. | |
4012 | * | |
4013 | * If EOFP is nonzero, set *EOFP to 1 if the string is unterminated. | |
4014 | */ | |
0a8ad417 | 4015 | static U_CHAR * |
24c3c71a | 4016 | skip_quoted_string (bp, limit, start_line, count_newlines, backslash_newlines_p, eofp) |
b3694847 SS |
4017 | const U_CHAR *bp; |
4018 | const U_CHAR *limit; | |
24c3c71a ZW |
4019 | int start_line; |
4020 | int *count_newlines; | |
4021 | int *backslash_newlines_p; | |
4022 | int *eofp; | |
4023 | { | |
b3694847 | 4024 | U_CHAR c, match; |
24c3c71a ZW |
4025 | |
4026 | match = *bp++; | |
4027 | while (1) { | |
4028 | if (bp >= limit) { | |
4029 | error_with_line (line_for_error (start_line), | |
4030 | "unterminated string or character constant"); | |
4031 | if (eofp) | |
4032 | *eofp = 1; | |
4033 | break; | |
4034 | } | |
4035 | c = *bp++; | |
4036 | if (c == '\\') { | |
4037 | while (*bp == '\\' && bp[1] == '\n') { | |
4038 | if (backslash_newlines_p) | |
4039 | *backslash_newlines_p = 1; | |
4040 | if (count_newlines) | |
4041 | ++*count_newlines; | |
4042 | bp += 2; | |
4043 | } | |
4044 | if (*bp == '\n' && count_newlines) { | |
4045 | if (backslash_newlines_p) | |
4046 | *backslash_newlines_p = 1; | |
4047 | ++*count_newlines; | |
4048 | } | |
4049 | bp++; | |
4050 | } else if (c == '\n') { | |
4051 | /* Unterminated strings and character constants are 'legal'. */ | |
4052 | bp--; /* Don't consume the newline. */ | |
4053 | if (eofp) | |
4054 | *eofp = 1; | |
4055 | break; | |
4056 | } else if (c == match) | |
4057 | break; | |
4058 | } | |
0a8ad417 | 4059 | return (U_CHAR *) bp; |
24c3c71a ZW |
4060 | } |
4061 | \f | |
4062 | /* | |
4063 | * write out a #line command, for instance, after an #include file. | |
4064 | * If CONDITIONAL is nonzero, we can omit the #line if it would | |
4065 | * appear to be a no-op, and we can output a few newlines instead | |
4066 | * if we want to increase the line number by a small amount. | |
4067 | * FILE_CHANGE says whether we are entering a file, leaving, or neither. | |
4068 | */ | |
4069 | ||
0a8ad417 | 4070 | static void |
24c3c71a ZW |
4071 | output_line_command (ip, op, conditional, file_change) |
4072 | FILE_BUF *ip, *op; | |
4073 | int conditional; | |
4074 | enum file_change_code file_change; | |
4075 | { | |
4076 | int len; | |
4077 | char line_cmd_buf[500]; | |
4078 | ||
4079 | if (no_line_commands | |
4080 | || ip->fname == NULL | |
4081 | || no_output) { | |
4082 | op->lineno = ip->lineno; | |
4083 | return; | |
4084 | } | |
4085 | ||
4086 | if (conditional) { | |
4087 | if (ip->lineno == op->lineno) | |
4088 | return; | |
4089 | ||
4090 | /* If the inherited line number is a little too small, | |
4091 | output some newlines instead of a #line command. */ | |
4092 | if (ip->lineno > op->lineno && ip->lineno < op->lineno + 8) { | |
4093 | check_expand (op, 10); | |
4094 | while (ip->lineno > op->lineno) { | |
4095 | *op->bufp++ = '\n'; | |
4096 | op->lineno++; | |
4097 | } | |
4098 | return; | |
4099 | } | |
4100 | } | |
4101 | ||
4102 | sprintf (line_cmd_buf, "# %d \"%s\"", ip->lineno, ip->fname); | |
4103 | if (file_change != same_file) | |
4104 | strcat (line_cmd_buf, file_change == enter_file ? " 1" : " 2"); | |
b3e2d1eb KG |
4105 | if (system_include_depth > 0) |
4106 | strcat (line_cmd_buf, " 3"); | |
24c3c71a ZW |
4107 | len = strlen (line_cmd_buf); |
4108 | line_cmd_buf[len++] = '\n'; | |
4109 | check_expand (op, len + 1); | |
4110 | if (op->bufp > op->buf && op->bufp[-1] != '\n') | |
4111 | *op->bufp++ = '\n'; | |
4112 | memcpy (op->bufp, line_cmd_buf, len); | |
4113 | op->bufp += len; | |
4114 | op->lineno = ip->lineno; | |
4115 | } | |
4116 | \f | |
4117 | ||
4118 | /* Expand a macro call. | |
4119 | HP points to the symbol that is the macro being called. | |
4120 | Put the result of expansion onto the input stack | |
4121 | so that subsequent input by our caller will use it. | |
4122 | ||
4123 | If macro wants arguments, caller has already verified that | |
4124 | an argument list follows; arguments come from the input stack. */ | |
4125 | ||
0a8ad417 | 4126 | static void |
24c3c71a ZW |
4127 | macroexpand (hp, op) |
4128 | HASHNODE *hp; | |
4129 | FILE_BUF *op; | |
4130 | { | |
4131 | int nargs; | |
4132 | DEFINITION *defn = hp->value.defn; | |
b3694847 | 4133 | U_CHAR *xbuf; |
24c3c71a ZW |
4134 | int xbuf_len; |
4135 | int start_line = instack[indepth].lineno; | |
4136 | ||
4137 | CHECK_DEPTH (return;); | |
4138 | ||
4139 | /* it might not actually be a macro. */ | |
4140 | if (hp->type != T_MACRO) { | |
4141 | special_symbol (hp, op); | |
4142 | return; | |
4143 | } | |
4144 | ||
4145 | nargs = defn->nargs; | |
4146 | ||
4147 | if (nargs >= 0) { | |
b3694847 | 4148 | int i; |
24c3c71a ZW |
4149 | struct argdata *args; |
4150 | const char *parse_error = 0; | |
4151 | ||
4152 | args = (struct argdata *) alloca ((nargs + 1) * sizeof (struct argdata)); | |
4153 | ||
4154 | for (i = 0; i < nargs; i++) { | |
4155 | args[i].raw = args[i].expanded = (U_CHAR *) ""; | |
4156 | args[i].raw_length = args[i].expand_length | |
4157 | = args[i].stringified_length = 0; | |
4158 | args[i].free1 = args[i].free2 = 0; | |
4159 | } | |
4160 | ||
4161 | /* Parse all the macro args that are supplied. I counts them. | |
4162 | The first NARGS args are stored in ARGS. | |
4163 | The rest are discarded. */ | |
4164 | i = 0; | |
4165 | do { | |
4166 | /* Discard the open-parenthesis or comma before the next arg. */ | |
4167 | ++instack[indepth].bufp; | |
4168 | parse_error | |
4169 | = macarg ((i < nargs || (nargs == 0 && i == 0)) ? &args[i] : 0); | |
4170 | if (parse_error) | |
4171 | { | |
3fdefbcc | 4172 | error_with_line (line_for_error (start_line), "%s", parse_error); |
24c3c71a ZW |
4173 | break; |
4174 | } | |
4175 | i++; | |
4176 | } while (*instack[indepth].bufp != ')'); | |
4177 | ||
4178 | /* If we got one arg but it was just whitespace, call that 0 args. */ | |
4179 | if (i == 1) { | |
b3694847 SS |
4180 | const U_CHAR *bp = args[0].raw; |
4181 | const U_CHAR *lim = bp + args[0].raw_length; | |
f6bbde28 | 4182 | while (bp != lim && is_space (*bp)) bp++; |
24c3c71a ZW |
4183 | if (bp == lim) |
4184 | i = 0; | |
4185 | } | |
4186 | ||
4187 | if (nargs == 0 && i > 0) | |
4188 | error ("arguments given to macro `%s'", hp->name); | |
4189 | else if (i < nargs) { | |
4190 | /* traditional C allows foo() if foo wants one argument. */ | |
4191 | if (nargs == 1 && i == 0) | |
4192 | ; | |
4193 | else if (i == 0) | |
4194 | error ("no args to macro `%s'", hp->name); | |
4195 | else if (i == 1) | |
4196 | error ("only 1 arg to macro `%s'", hp->name); | |
4197 | else | |
4198 | error ("only %d args to macro `%s'", i, hp->name); | |
4199 | } else if (i > nargs) | |
4200 | error ("too many (%d) args to macro `%s'", i, hp->name); | |
4201 | ||
4202 | /* Swallow the closeparen. */ | |
4203 | ++instack[indepth].bufp; | |
4204 | ||
4205 | /* If macro wants zero args, we parsed the arglist for checking only. | |
4206 | Read directly from the macro definition. */ | |
4207 | if (nargs == 0) { | |
4208 | xbuf = defn->expansion; | |
4209 | xbuf_len = defn->length; | |
4210 | } else { | |
b3694847 SS |
4211 | U_CHAR *exp = defn->expansion; |
4212 | int offset; /* offset in expansion, | |
24c3c71a | 4213 | copied a piece at a time */ |
b3694847 | 4214 | int totlen; /* total amount of exp buffer filled so far */ |
24c3c71a | 4215 | |
b3694847 | 4216 | struct reflist *ap; |
24c3c71a ZW |
4217 | |
4218 | /* Macro really takes args. Compute the expansion of this call. */ | |
4219 | ||
4220 | /* Compute length in characters of the macro's expansion. */ | |
4221 | xbuf_len = defn->length; | |
4222 | for (ap = defn->pattern; ap != NULL; ap = ap->next) { | |
4223 | if (ap->stringify) | |
4224 | xbuf_len += args[ap->argno].stringified_length; | |
4225 | else | |
4226 | xbuf_len += args[ap->argno].raw_length; | |
4227 | } | |
4228 | ||
4229 | xbuf = (U_CHAR *) xmalloc (xbuf_len + 1); | |
4230 | ||
4231 | /* Generate in XBUF the complete expansion | |
4232 | with arguments substituted in. | |
4233 | TOTLEN is the total size generated so far. | |
4234 | OFFSET is the index in the definition | |
4235 | of where we are copying from. */ | |
4236 | offset = totlen = 0; | |
4237 | for (ap = defn->pattern; ap != NULL; ap = ap->next) { | |
b3694847 | 4238 | struct argdata *arg = &args[ap->argno]; |
24c3c71a ZW |
4239 | |
4240 | for (i = 0; i < ap->nchars; i++) | |
4241 | xbuf[totlen++] = exp[offset++]; | |
4242 | ||
4243 | if (ap->stringify != 0) { | |
4244 | int arglen = arg->raw_length; | |
4245 | int escaped = 0; | |
4246 | int in_string = 0; | |
4247 | int c; | |
4248 | i = 0; | |
4249 | while (i < arglen | |
f6bbde28 | 4250 | && (c = arg->raw[i], is_space (c))) |
24c3c71a ZW |
4251 | i++; |
4252 | while (i < arglen | |
f6bbde28 | 4253 | && (c = arg->raw[arglen - 1], is_space (c))) |
24c3c71a ZW |
4254 | arglen--; |
4255 | for (; i < arglen; i++) { | |
4256 | c = arg->raw[i]; | |
4257 | ||
4258 | /* Special markers Newline Space | |
4259 | generate nothing for a stringified argument. */ | |
4260 | if (c == '\n' && arg->raw[i+1] != '\n') { | |
4261 | i++; | |
4262 | continue; | |
4263 | } | |
4264 | ||
4265 | /* Internal sequences of whitespace are replaced by one space | |
4266 | except within an string or char token. */ | |
4267 | if (! in_string | |
f6bbde28 | 4268 | && (c == '\n' ? arg->raw[i+1] == '\n' : is_space (c))) { |
24c3c71a ZW |
4269 | while (1) { |
4270 | /* Note that Newline Space does occur within whitespace | |
4271 | sequences; consider it part of the sequence. */ | |
f6bbde28 | 4272 | if (c == '\n' && is_space (arg->raw[i+1])) |
24c3c71a | 4273 | i += 2; |
f6bbde28 | 4274 | else if (c != '\n' && is_space (c)) |
24c3c71a ZW |
4275 | i++; |
4276 | else break; | |
4277 | c = arg->raw[i]; | |
4278 | } | |
4279 | i--; | |
4280 | c = ' '; | |
4281 | } | |
4282 | ||
4283 | if (escaped) | |
4284 | escaped = 0; | |
4285 | else { | |
4286 | if (c == '\\') | |
4287 | escaped = 1; | |
4288 | if (in_string) { | |
4289 | if (c == in_string) | |
4290 | in_string = 0; | |
4291 | } else if (c == '\"' || c == '\'') | |
4292 | in_string = c; | |
4293 | } | |
4294 | ||
4295 | /* Escape these chars */ | |
4296 | if (c == '\"' || (in_string && c == '\\')) | |
4297 | xbuf[totlen++] = '\\'; | |
ce1cc601 | 4298 | if (ISPRINT (c)) |
24c3c71a ZW |
4299 | xbuf[totlen++] = c; |
4300 | else { | |
4301 | sprintf ((char *) &xbuf[totlen], "\\%03o", (unsigned int) c); | |
4302 | totlen += 4; | |
4303 | } | |
4304 | } | |
4305 | } else { | |
0a8ad417 KG |
4306 | const U_CHAR *p1 = arg->raw; |
4307 | const U_CHAR *l1 = p1 + arg->raw_length; | |
24c3c71a ZW |
4308 | |
4309 | if (ap->raw_before) { | |
f6bbde28 ZW |
4310 | while (p1 != l1 && is_space (*p1)) p1++; |
4311 | while (p1 != l1 && is_idchar (*p1)) | |
24c3c71a ZW |
4312 | xbuf[totlen++] = *p1++; |
4313 | /* Delete any no-reexpansion marker that follows | |
4314 | an identifier at the beginning of the argument | |
4315 | if the argument is concatenated with what precedes it. */ | |
4316 | if (p1[0] == '\n' && p1[1] == '-') | |
4317 | p1 += 2; | |
4318 | } | |
4319 | if (ap->raw_after) { | |
4320 | /* Arg is concatenated after: delete trailing whitespace, | |
4321 | whitespace markers, and no-reexpansion markers. */ | |
4322 | while (p1 != l1) { | |
f6bbde28 | 4323 | if (is_space (l1[-1])) l1--; |
24c3c71a | 4324 | else if (l1[-1] == '-') { |
0a8ad417 | 4325 | const U_CHAR *p2 = l1 - 1; |
24c3c71a ZW |
4326 | /* If a `-' is preceded by an odd number of newlines then it |
4327 | and the last newline are a no-reexpansion marker. */ | |
4328 | while (p2 != p1 && p2[-1] == '\n') p2--; | |
4329 | if ((l1 - 1 - p2) & 1) { | |
4330 | l1 -= 2; | |
4331 | } | |
4332 | else break; | |
4333 | } | |
4334 | else break; | |
4335 | } | |
4336 | } | |
4337 | memmove (xbuf + totlen, p1, l1 - p1); | |
4338 | totlen += l1 - p1; | |
4339 | } | |
4340 | ||
4341 | if (totlen > xbuf_len) | |
4342 | abort (); | |
4343 | } | |
4344 | ||
4345 | /* if there is anything left of the definition | |
4346 | after handling the arg list, copy that in too. */ | |
4347 | ||
4348 | for (i = offset; i < defn->length; i++) | |
4349 | xbuf[totlen++] = exp[i]; | |
4350 | ||
4351 | xbuf[totlen] = 0; | |
4352 | xbuf_len = totlen; | |
4353 | ||
4354 | for (i = 0; i < nargs; i++) { | |
4355 | if (args[i].free1 != 0) | |
4356 | free (args[i].free1); | |
4357 | if (args[i].free2 != 0) | |
4358 | free (args[i].free2); | |
4359 | } | |
4360 | } | |
4361 | } else { | |
4362 | xbuf = defn->expansion; | |
4363 | xbuf_len = defn->length; | |
4364 | } | |
4365 | ||
4366 | /* Now put the expansion on the input stack | |
4367 | so our caller will commence reading from it. */ | |
4368 | { | |
b3694847 | 4369 | FILE_BUF *ip2; |
24c3c71a ZW |
4370 | |
4371 | ip2 = &instack[++indepth]; | |
4372 | ||
4373 | ip2->fname = 0; | |
4374 | ip2->lineno = 0; | |
4375 | ip2->buf = xbuf; | |
4376 | ip2->length = xbuf_len; | |
4377 | ip2->bufp = xbuf; | |
4378 | ip2->free_ptr = (nargs > 0) ? xbuf : 0; | |
4379 | ip2->macro = hp; | |
4380 | ip2->if_stack = if_stack; | |
4381 | } | |
4382 | } | |
4383 | \f | |
4384 | /* | |
4385 | * Parse a macro argument and store the info on it into *ARGPTR. | |
4386 | * Return nonzero to indicate a syntax error. | |
4387 | */ | |
4388 | ||
0a8ad417 | 4389 | static const char * |
24c3c71a | 4390 | macarg (argptr) |
b3694847 | 4391 | struct argdata *argptr; |
24c3c71a ZW |
4392 | { |
4393 | FILE_BUF *ip = &instack[indepth]; | |
4394 | int paren = 0; | |
4395 | int newlines = 0; | |
4396 | int comments = 0; | |
4397 | ||
4398 | /* Try to parse as much of the argument as exists at this | |
4399 | input stack level. */ | |
4400 | U_CHAR *bp = macarg1 (ip->bufp, ip->buf + ip->length, | |
4401 | &paren, &newlines, &comments); | |
4402 | ||
4403 | /* If we find the end of the argument at this level, | |
4404 | set up *ARGPTR to point at it in the input stack. */ | |
4405 | if (!(ip->fname != 0 && (newlines != 0 || comments != 0)) | |
4406 | && bp != ip->buf + ip->length) { | |
4407 | if (argptr != 0) { | |
4408 | argptr->raw = ip->bufp; | |
4409 | argptr->raw_length = bp - ip->bufp; | |
4410 | } | |
4411 | ip->bufp = bp; | |
4412 | } else { | |
4413 | /* This input stack level ends before the macro argument does. | |
4414 | We must pop levels and keep parsing. | |
4415 | Therefore, we must allocate a temporary buffer and copy | |
4416 | the macro argument into it. */ | |
4417 | int bufsize = bp - ip->bufp; | |
4418 | int extra = newlines; | |
4419 | U_CHAR *buffer = (U_CHAR *) xmalloc (bufsize + extra + 1); | |
4420 | int final_start = 0; | |
4421 | ||
4422 | memcpy (buffer, ip->bufp, bufsize); | |
4423 | ip->bufp = bp; | |
4424 | ip->lineno += newlines; | |
4425 | ||
4426 | while (bp == ip->buf + ip->length) { | |
4427 | if (instack[indepth].macro == 0) { | |
4428 | free (buffer); | |
4429 | return "unterminated macro call"; | |
4430 | } | |
4431 | ip->macro->type = T_MACRO; | |
4432 | if (ip->free_ptr) | |
4433 | free (ip->free_ptr); | |
4434 | ip = &instack[--indepth]; | |
4435 | newlines = 0; | |
4436 | comments = 0; | |
4437 | bp = macarg1 (ip->bufp, ip->buf + ip->length, &paren, | |
4438 | &newlines, &comments); | |
4439 | final_start = bufsize; | |
4440 | bufsize += bp - ip->bufp; | |
4441 | extra += newlines; | |
4442 | buffer = (U_CHAR *) xrealloc (buffer, bufsize + extra + 1); | |
4443 | memcpy (buffer + bufsize - (bp - ip->bufp), ip->bufp, bp - ip->bufp); | |
4444 | ip->bufp = bp; | |
4445 | ip->lineno += newlines; | |
4446 | } | |
4447 | ||
4448 | /* Now, if arg is actually wanted, record its raw form, | |
4449 | discarding comments and duplicating newlines in whatever | |
4450 | part of it did not come from a macro expansion. | |
4451 | EXTRA space has been preallocated for duplicating the newlines. | |
4452 | FINAL_START is the index of the start of that part. */ | |
4453 | if (argptr != 0) { | |
4454 | argptr->raw = buffer; | |
4455 | argptr->raw_length = bufsize; | |
4456 | argptr->free1 = buffer; | |
4457 | argptr->newlines = newlines; | |
4458 | argptr->comments = comments; | |
4459 | if ((newlines || comments) && ip->fname != 0) | |
4460 | argptr->raw_length | |
4461 | = final_start + | |
4462 | discard_comments (argptr->raw + final_start, | |
4463 | argptr->raw_length - final_start, | |
4464 | newlines); | |
4465 | argptr->raw[argptr->raw_length] = 0; | |
4466 | if (argptr->raw_length > bufsize + extra) | |
4467 | abort (); | |
4468 | } | |
4469 | } | |
4470 | ||
4471 | /* If we are not discarding this argument, | |
4472 | macroexpand it and compute its length as stringified. | |
4473 | All this info goes into *ARGPTR. */ | |
4474 | ||
4475 | if (argptr != 0) { | |
4476 | FILE_BUF obuf; | |
b3694847 SS |
4477 | const U_CHAR *buf, *lim; |
4478 | int totlen; | |
24c3c71a ZW |
4479 | |
4480 | obuf = expand_to_temp_buffer (argptr->raw, | |
4481 | argptr->raw + argptr->raw_length, | |
4482 | 1); | |
4483 | ||
4484 | argptr->expanded = obuf.buf; | |
4485 | argptr->expand_length = obuf.length; | |
4486 | argptr->free2 = obuf.buf; | |
4487 | ||
4488 | buf = argptr->raw; | |
4489 | lim = buf + argptr->raw_length; | |
4490 | ||
4491 | totlen = 0; | |
4492 | while (buf != lim) { | |
b3694847 | 4493 | U_CHAR c = *buf++; |
24c3c71a ZW |
4494 | totlen++; |
4495 | /* Internal sequences of whitespace are replaced by one space | |
4496 | in most cases, but not always. So count all the whitespace | |
4497 | in case we need to keep it all. */ | |
4498 | if (c == '\"' || c == '\\') /* escape these chars */ | |
4499 | totlen++; | |
ce1cc601 | 4500 | else if (!ISPRINT (c)) |
24c3c71a ZW |
4501 | totlen += 3; |
4502 | } | |
4503 | argptr->stringified_length = totlen; | |
4504 | } | |
4505 | return 0; | |
4506 | } | |
4507 | ||
4508 | /* Scan text from START (inclusive) up to LIMIT (exclusive), | |
4509 | counting parens in *DEPTHPTR, | |
4510 | and return if reach LIMIT | |
4511 | or before a `)' that would make *DEPTHPTR negative | |
4512 | or before a comma when *DEPTHPTR is zero. | |
4513 | Single and double quotes are matched and termination | |
4514 | is inhibited within them. Comments also inhibit it. | |
4515 | Value returned is pointer to stopping place. | |
4516 | ||
4517 | Increment *NEWLINES each time a newline is passed. | |
4518 | Set *COMMENTS to 1 if a comment is seen. */ | |
4519 | ||
0a8ad417 | 4520 | static U_CHAR * |
24c3c71a ZW |
4521 | macarg1 (start, limit, depthptr, newlines, comments) |
4522 | U_CHAR *start; | |
b3694847 | 4523 | const U_CHAR *limit; |
24c3c71a ZW |
4524 | int *depthptr, *newlines, *comments; |
4525 | { | |
b3694847 | 4526 | U_CHAR *bp = start; |
24c3c71a ZW |
4527 | |
4528 | while (bp < limit) { | |
4529 | switch (*bp) { | |
4530 | case '(': | |
4531 | (*depthptr)++; | |
4532 | break; | |
4533 | case ')': | |
4534 | if (--(*depthptr) < 0) | |
4535 | return bp; | |
4536 | break; | |
4537 | case '\\': | |
4538 | /* Traditionally, backslash makes following char not special. */ | |
4539 | if (bp + 1 < limit) | |
4540 | { | |
4541 | bp++; | |
4542 | /* But count source lines anyway. */ | |
4543 | if (*bp == '\n') | |
4544 | ++*newlines; | |
4545 | } | |
4546 | break; | |
4547 | case '\n': | |
4548 | ++*newlines; | |
4549 | break; | |
4550 | case '/': | |
4551 | if (bp[1] == '\\' && bp[2] == '\n') | |
4552 | newline_fix (bp + 1); | |
4553 | if (bp[1] != '*' || bp + 1 >= limit) | |
4554 | break; | |
4555 | *comments = 1; | |
4556 | bp += 2; | |
4557 | while (bp + 1 < limit) { | |
4558 | if (bp[0] == '*' | |
4559 | && bp[1] == '\\' && bp[2] == '\n') | |
4560 | newline_fix (bp + 1); | |
4561 | if (bp[0] == '*' && bp[1] == '/') | |
4562 | break; | |
4563 | if (*bp == '\n') ++*newlines; | |
4564 | bp++; | |
4565 | } | |
4566 | bp += 1; | |
4567 | break; | |
4568 | case '\'': | |
4569 | case '\"': | |
4570 | { | |
4571 | int quotec; | |
4572 | for (quotec = *bp++; bp + 1 < limit && *bp != quotec; bp++) { | |
4573 | if (*bp == '\\') { | |
4574 | bp++; | |
4575 | if (*bp == '\n') | |
4576 | ++*newlines; | |
4577 | while (*bp == '\\' && bp[1] == '\n') { | |
4578 | bp += 2; | |
4579 | } | |
4580 | } else if (*bp == '\n') { | |
4581 | ++*newlines; | |
4582 | if (quotec == '\'') | |
4583 | break; | |
4584 | } | |
4585 | } | |
4586 | } | |
4587 | break; | |
4588 | case ',': | |
4589 | if ((*depthptr) == 0) | |
4590 | return bp; | |
4591 | break; | |
4592 | } | |
4593 | bp++; | |
4594 | } | |
4595 | ||
4596 | return bp; | |
4597 | } | |
4598 | ||
4599 | /* Discard comments and duplicate newlines | |
4600 | in the string of length LENGTH at START, | |
4601 | except inside of string constants. | |
4602 | The string is copied into itself with its beginning staying fixed. | |
4603 | ||
4604 | NEWLINES is the number of newlines that must be duplicated. | |
4605 | We assume that that much extra space is available past the end | |
4606 | of the string. */ | |
4607 | ||
0a8ad417 | 4608 | static int |
24c3c71a ZW |
4609 | discard_comments (start, length, newlines) |
4610 | U_CHAR *start; | |
4611 | int length; | |
4612 | int newlines; | |
4613 | { | |
b3694847 SS |
4614 | U_CHAR *ibp; |
4615 | U_CHAR *obp; | |
4616 | const U_CHAR *limit; | |
4617 | int c; | |
24c3c71a ZW |
4618 | |
4619 | /* If we have newlines to duplicate, copy everything | |
4620 | that many characters up. Then, in the second part, | |
4621 | we will have room to insert the newlines | |
4622 | while copying down. | |
4623 | NEWLINES may actually be too large, because it counts | |
4624 | newlines in string constants, and we don't duplicate those. | |
4625 | But that does no harm. */ | |
4626 | if (newlines > 0) { | |
4627 | ibp = start + length; | |
4628 | obp = ibp + newlines; | |
4629 | limit = start; | |
4630 | while (limit != ibp) | |
4631 | *--obp = *--ibp; | |
4632 | } | |
4633 | ||
4634 | ibp = start + newlines; | |
4635 | limit = start + length + newlines; | |
4636 | obp = start; | |
4637 | ||
4638 | while (ibp < limit) { | |
4639 | *obp++ = c = *ibp++; | |
4640 | switch (c) { | |
4641 | case '\n': | |
4642 | /* Duplicate the newline. */ | |
4643 | *obp++ = '\n'; | |
4644 | break; | |
4645 | ||
4646 | case '\\': | |
4647 | if (*ibp == '\n') { | |
4648 | obp--; | |
4649 | ibp++; | |
4650 | } | |
4651 | break; | |
4652 | ||
4653 | case '/': | |
4654 | if (*ibp == '\\' && ibp[1] == '\n') | |
4655 | newline_fix (ibp); | |
4656 | /* Delete any comment. */ | |
4657 | if (ibp[0] != '*' || ibp + 1 >= limit) | |
4658 | break; | |
4659 | obp--; | |
4660 | ibp++; | |
4661 | while (ibp + 1 < limit) { | |
4662 | if (ibp[0] == '*' | |
4663 | && ibp[1] == '\\' && ibp[2] == '\n') | |
4664 | newline_fix (ibp + 1); | |
4665 | if (ibp[0] == '*' && ibp[1] == '/') | |
4666 | break; | |
4667 | ibp++; | |
4668 | } | |
4669 | ibp += 2; | |
4670 | break; | |
4671 | ||
4672 | case '\'': | |
4673 | case '\"': | |
4674 | /* Notice and skip strings, so that we don't | |
4675 | think that comments start inside them, | |
4676 | and so we don't duplicate newlines in them. */ | |
4677 | { | |
4678 | int quotec = c; | |
4679 | while (ibp < limit) { | |
4680 | *obp++ = c = *ibp++; | |
4681 | if (c == quotec) | |
4682 | break; | |
4683 | if (c == '\n' && quotec == '\'') | |
4684 | break; | |
4685 | if (c == '\\' && ibp < limit) { | |
4686 | while (*ibp == '\\' && ibp[1] == '\n') | |
4687 | ibp += 2; | |
4688 | *obp++ = *ibp++; | |
4689 | } | |
4690 | } | |
4691 | } | |
4692 | break; | |
4693 | } | |
4694 | } | |
4695 | ||
4696 | return obp - start; | |
4697 | } | |
4698 | \f | |
4699 | ||
4700 | /* Core error handling routine. */ | |
0a8ad417 | 4701 | static void |
24c3c71a ZW |
4702 | v_message (mtype, line, msgid, ap) |
4703 | enum msgtype mtype; | |
4704 | int line; | |
4705 | const char *msgid; | |
4706 | va_list ap; | |
4707 | { | |
4708 | const char *fname = 0; | |
4709 | int i; | |
4710 | ||
79e2e160 | 4711 | if (mtype == MT_WARNING && inhibit_warnings) |
24c3c71a ZW |
4712 | return; |
4713 | ||
4714 | for (i = indepth; i >= 0; i--) | |
4715 | if (instack[i].fname != NULL) { | |
4716 | if (line == 0) | |
4717 | line = instack[i].lineno; | |
4718 | fname = instack[i].fname; | |
4719 | break; | |
4720 | } | |
4721 | ||
4722 | if (fname) | |
4723 | fprintf (stderr, "%s:%d: ", fname, line); | |
4724 | else | |
4725 | fprintf (stderr, "%s: ", progname); | |
4726 | ||
79e2e160 ZW |
4727 | if (mtype == MT_WARNING) |
4728 | fputs (_("warning: "), stderr); | |
24c3c71a | 4729 | |
79e2e160 | 4730 | vfprintf (stderr, _(msgid), ap); |
24c3c71a ZW |
4731 | putc ('\n', stderr); |
4732 | ||
79e2e160 | 4733 | if (mtype == MT_ERROR) |
24c3c71a ZW |
4734 | errors++; |
4735 | } | |
4736 | ||
4737 | /* | |
4738 | * error - print error message and increment count of errors. | |
4739 | */ | |
4740 | void | |
4741 | error VPARAMS ((const char *msgid, ...)) | |
4742 | { | |
7a75edb7 AJ |
4743 | VA_OPEN(ap, msgid); |
4744 | VA_FIXEDARG (ap, const char *, msgid); | |
24c3c71a | 4745 | |
79e2e160 | 4746 | v_message (MT_ERROR, 0, msgid, ap); |
7a75edb7 | 4747 | VA_CLOSE (ap); |
24c3c71a ZW |
4748 | } |
4749 | ||
4750 | void | |
4751 | error_with_line VPARAMS ((int line, const char *msgid, ...)) | |
4752 | { | |
7a75edb7 AJ |
4753 | VA_OPEN(ap, msgid); |
4754 | VA_FIXEDARG (ap, int, line); | |
4755 | VA_FIXEDARG (ap, const char *, msgid); | |
24c3c71a | 4756 | |
79e2e160 | 4757 | v_message (MT_ERROR, line, msgid, ap); |
7a75edb7 | 4758 | VA_CLOSE (ap); |
24c3c71a ZW |
4759 | } |
4760 | ||
4761 | /* Error including a message from `errno'. */ | |
4762 | void | |
4763 | error_from_errno (name) | |
4764 | const char *name; | |
4765 | { | |
4766 | error ("%s: %s", name, strerror (errno)); | |
4767 | } | |
4768 | ||
4769 | /* Print error message but don't count it. */ | |
4770 | void | |
4771 | warning VPARAMS ((const char *msgid, ...)) | |
4772 | { | |
7a75edb7 AJ |
4773 | VA_OPEN(ap, msgid); |
4774 | VA_FIXEDARG (ap, const char *, msgid); | |
24c3c71a | 4775 | |
79e2e160 | 4776 | v_message (MT_WARNING, 0, msgid, ap); |
7a75edb7 | 4777 | VA_CLOSE (ap); |
24c3c71a ZW |
4778 | } |
4779 | ||
4780 | void | |
4781 | fatal VPARAMS ((const char *msgid, ...)) | |
4782 | { | |
7a75edb7 AJ |
4783 | VA_OPEN(ap, msgid); |
4784 | VA_FIXEDARG (ap, const char *, msgid); | |
24c3c71a | 4785 | |
79e2e160 | 4786 | v_message (MT_FATAL, 0, msgid, ap); |
7a75edb7 | 4787 | VA_CLOSE (ap); |
24c3c71a ZW |
4788 | exit (FATAL_EXIT_CODE); |
4789 | } | |
4790 | ||
4791 | /* More 'friendly' abort that prints the location at which we died. */ | |
4792 | void | |
4793 | fancy_abort (line, func) | |
4794 | int line; | |
4795 | const char *func; | |
4796 | { | |
1f978f5f | 4797 | fatal ("internal error in %s, at tradcpp.c:%d\n\ |
24c3c71a ZW |
4798 | Please submit a full bug report.\n\ |
4799 | See %s for instructions.", func, line, GCCBUGURL); | |
4800 | } | |
4801 | ||
4802 | void | |
4803 | perror_with_name (name) | |
4804 | const char *name; | |
4805 | { | |
4806 | fprintf (stderr, "%s: %s: %s\n", progname, name, strerror (errno)); | |
4807 | errors++; | |
4808 | } | |
4809 | ||
4810 | void | |
4811 | pfatal_with_name (name) | |
4812 | const char *name; | |
4813 | { | |
4814 | perror_with_name (name); | |
4815 | exit (FATAL_EXIT_CODE); | |
4816 | } | |
4817 | ||
4818 | /* Return the line at which an error occurred. | |
4819 | The error is not necessarily associated with the current spot | |
4820 | in the input stack, so LINE says where. LINE will have been | |
4821 | copied from ip->lineno for the current input level. | |
4822 | If the current level is for a file, we return LINE. | |
4823 | But if the current level is not for a file, LINE is meaningless. | |
4824 | In that case, we return the lineno of the innermost file. */ | |
0a8ad417 | 4825 | static int |
24c3c71a ZW |
4826 | line_for_error (line) |
4827 | int line; | |
4828 | { | |
4829 | int i; | |
4830 | int line1 = line; | |
4831 | ||
4832 | for (i = indepth; i >= 0; ) { | |
4833 | if (instack[i].fname != 0) | |
4834 | return line1; | |
4835 | i--; | |
4836 | if (i < 0) | |
4837 | return 0; | |
4838 | line1 = instack[i].lineno; | |
4839 | } | |
4840 | return 0; | |
4841 | } | |
4842 | ||
4843 | /* | |
4844 | * If OBUF doesn't have NEEDED bytes after OPTR, make it bigger. | |
4845 | * | |
4846 | * As things stand, nothing is ever placed in the output buffer to be | |
4847 | * removed again except when it's KNOWN to be part of an identifier, | |
4848 | * so flushing and moving down everything left, instead of expanding, | |
4849 | * should work ok. | |
4850 | */ | |
4851 | ||
0a8ad417 | 4852 | static void |
24c3c71a | 4853 | grow_outbuf (obuf, needed) |
b3694847 SS |
4854 | FILE_BUF *obuf; |
4855 | int needed; | |
24c3c71a | 4856 | { |
b3694847 | 4857 | U_CHAR *p; |
24c3c71a ZW |
4858 | int minsize; |
4859 | ||
4860 | if (obuf->length - (obuf->bufp - obuf->buf) > needed) | |
4861 | return; | |
4862 | ||
4863 | /* Make it at least twice as big as it is now. */ | |
4864 | obuf->length *= 2; | |
4865 | /* Make it have at least 150% of the free space we will need. */ | |
4866 | minsize = (3 * needed) / 2 + (obuf->bufp - obuf->buf); | |
4867 | if (minsize > obuf->length) | |
4868 | obuf->length = minsize; | |
4869 | ||
4870 | p = (U_CHAR *) xrealloc (obuf->buf, obuf->length); | |
4871 | obuf->bufp = p + (obuf->bufp - obuf->buf); | |
4872 | obuf->buf = p; | |
4873 | } | |
4874 | \f | |
4875 | /* Symbol table for macro names and special symbols */ | |
4876 | ||
4877 | /* | |
4878 | * install a name in the main hash table, even if it is already there. | |
4879 | * name stops with first non alphanumeric, except leading '#'. | |
4880 | * caller must check against redefinition if that is desired. | |
4881 | * delete_macro () removes things installed by install () in fifo order. | |
4882 | * this is important because of the `defined' special symbol used | |
4883 | * in #if, and also if pushdef/popdef directives are ever implemented. | |
4884 | * | |
4885 | * If LEN is >= 0, it is the length of the name. | |
4886 | * Otherwise, compute the length by scanning the entire name. | |
4887 | * | |
4888 | * If HASH is >= 0, it is the precomputed hash code. | |
4889 | * Otherwise, compute the hash code. | |
4890 | * | |
4891 | * caller must set the value, if any is desired. | |
4892 | */ | |
0a8ad417 | 4893 | static HASHNODE * |
24c3c71a ZW |
4894 | install (name, len, type, hash) |
4895 | const U_CHAR *name; | |
4896 | int len; | |
4897 | enum node_type type; | |
4898 | int hash; | |
4899 | /* watch out here if sizeof (U_CHAR *) != sizeof (int) */ | |
4900 | { | |
b3694847 SS |
4901 | HASHNODE *hp; |
4902 | int bucket; | |
4903 | const U_CHAR *p; | |
24c3c71a ZW |
4904 | U_CHAR *q; |
4905 | ||
4906 | if (len < 0) { | |
4907 | p = name; | |
f6bbde28 | 4908 | while (is_idchar (*p)) |
24c3c71a ZW |
4909 | p++; |
4910 | len = p - name; | |
4911 | } | |
4912 | ||
4913 | if (hash < 0) | |
4914 | hash = hashf (name, len, HASHSIZE); | |
4915 | ||
4916 | hp = (HASHNODE *) xmalloc (sizeof (HASHNODE) + len + 1); | |
4917 | bucket = hash; | |
4918 | hp->bucket_hdr = &hashtab[bucket]; | |
4919 | hp->next = hashtab[bucket]; | |
4920 | hashtab[bucket] = hp; | |
4921 | hp->prev = NULL; | |
4922 | if (hp->next != NULL) | |
4923 | hp->next->prev = hp; | |
4924 | hp->type = type; | |
4925 | hp->length = len; | |
4926 | hp->name = q = ((U_CHAR *) hp) + sizeof (HASHNODE); | |
4927 | memcpy (q, name, len); | |
4928 | q[len] = 0; | |
4929 | return hp; | |
4930 | } | |
4931 | ||
4932 | /* | |
4933 | * find the most recent hash node for name name (ending with first | |
4934 | * non-identifier char) installed by install | |
4935 | * | |
4936 | * If LEN is >= 0, it is the length of the name. | |
4937 | * Otherwise, compute the length by scanning the entire name. | |
4938 | * | |
4939 | * If HASH is >= 0, it is the precomputed hash code. | |
4940 | * Otherwise, compute the hash code. | |
4941 | */ | |
4942 | HASHNODE * | |
4943 | lookup (name, len, hash) | |
4944 | const U_CHAR *name; | |
4945 | int len; | |
4946 | int hash; | |
4947 | { | |
b3694847 SS |
4948 | const U_CHAR *bp; |
4949 | HASHNODE *bucket; | |
24c3c71a ZW |
4950 | |
4951 | if (len < 0) { | |
f6bbde28 | 4952 | for (bp = name; is_idchar (*bp); bp++) ; |
24c3c71a ZW |
4953 | len = bp - name; |
4954 | } | |
4955 | ||
4956 | if (hash < 0) | |
4957 | hash = hashf (name, len, HASHSIZE); | |
4958 | ||
4959 | bucket = hashtab[hash]; | |
4960 | while (bucket) { | |
4961 | if (bucket->length == len | |
0a8ad417 | 4962 | && strncmp ((const char *)bucket->name, (const char *)name, len) == 0) |
24c3c71a ZW |
4963 | return bucket; |
4964 | bucket = bucket->next; | |
4965 | } | |
4966 | return NULL; | |
4967 | } | |
4968 | ||
4969 | /* | |
4970 | * Delete a hash node. Some weirdness to free junk from macros. | |
4971 | * More such weirdness will have to be added if you define more hash | |
4972 | * types that need it. | |
4973 | */ | |
4974 | ||
4975 | /* Note that the DEFINITION of a macro is removed from the hash table | |
4976 | but its storage is not freed. This would be a storage leak | |
4977 | except that it is not reasonable to keep undefining and redefining | |
4978 | large numbers of macros many times. | |
4979 | In any case, this is necessary, because a macro can be #undef'd | |
4980 | in the middle of reading the arguments to a call to it. | |
4981 | If #undef freed the DEFINITION, that would crash. */ | |
0a8ad417 | 4982 | static void |
24c3c71a ZW |
4983 | delete_macro (hp) |
4984 | HASHNODE *hp; | |
4985 | { | |
4986 | ||
4987 | if (hp->prev != NULL) | |
4988 | hp->prev->next = hp->next; | |
4989 | if (hp->next != NULL) | |
4990 | hp->next->prev = hp->prev; | |
4991 | ||
4992 | /* make sure that the bucket chain header that | |
4993 | the deleted guy was on points to the right thing afterwards. */ | |
4994 | if (hp == *hp->bucket_hdr) | |
4995 | *hp->bucket_hdr = hp->next; | |
4996 | ||
4997 | free (hp); | |
4998 | } | |
4999 | ||
5000 | /* | |
5001 | * return hash function on name. must be compatible with the one | |
5002 | * computed a step at a time, elsewhere | |
5003 | */ | |
0a8ad417 | 5004 | static int |
24c3c71a | 5005 | hashf (name, len, hashsize) |
b3694847 SS |
5006 | const U_CHAR *name; |
5007 | int len; | |
24c3c71a ZW |
5008 | int hashsize; |
5009 | { | |
b3694847 | 5010 | int r = 0; |
24c3c71a ZW |
5011 | |
5012 | while (len--) | |
5013 | r = HASHSTEP (r, *name++); | |
5014 | ||
5015 | return MAKE_POS (r) % hashsize; | |
5016 | } | |
5017 | \f | |
5018 | /* Dump all macro definitions as #defines to stdout. */ | |
5019 | ||
0a8ad417 | 5020 | static void |
24c3c71a ZW |
5021 | dump_all_macros () |
5022 | { | |
5023 | int bucket; | |
5024 | ||
5025 | for (bucket = 0; bucket < HASHSIZE; bucket++) { | |
b3694847 | 5026 | HASHNODE *hp; |
24c3c71a ZW |
5027 | |
5028 | for (hp = hashtab[bucket]; hp; hp= hp->next) { | |
5029 | if (hp->type == T_MACRO) { | |
b3694847 | 5030 | DEFINITION *defn = hp->value.defn; |
24c3c71a ZW |
5031 | struct reflist *ap; |
5032 | int offset; | |
5033 | int concat; | |
5034 | ||
5035 | ||
5036 | /* Print the definition of the macro HP. */ | |
5037 | ||
5038 | printf ("#define %s", hp->name); | |
5039 | if (defn->nargs >= 0) { | |
5040 | int i; | |
5041 | ||
5042 | printf ("("); | |
5043 | for (i = 0; i < defn->nargs; i++) { | |
5044 | dump_arg_n (defn, i); | |
5045 | if (i + 1 < defn->nargs) | |
5046 | printf (", "); | |
5047 | } | |
5048 | printf (")"); | |
5049 | } | |
5050 | ||
5051 | printf (" "); | |
5052 | ||
5053 | offset = 0; | |
5054 | concat = 0; | |
5055 | for (ap = defn->pattern; ap != NULL; ap = ap->next) { | |
5056 | dump_defn_1 (defn->expansion, offset, ap->nchars); | |
5057 | if (ap->nchars != 0) | |
5058 | concat = 0; | |
5059 | offset += ap->nchars; | |
5060 | if (ap->stringify) | |
5061 | printf (" #"); | |
5062 | if (ap->raw_before && !concat) | |
5063 | printf (" ## "); | |
5064 | concat = 0; | |
5065 | dump_arg_n (defn, ap->argno); | |
5066 | if (ap->raw_after) { | |
5067 | printf (" ## "); | |
5068 | concat = 1; | |
5069 | } | |
5070 | } | |
5071 | dump_defn_1 (defn->expansion, offset, defn->length - offset); | |
5072 | printf ("\n"); | |
5073 | } | |
5074 | } | |
5075 | } | |
5076 | } | |
5077 | ||
5078 | /* Output to stdout a substring of a macro definition. | |
5079 | BASE is the beginning of the definition. | |
5080 | Output characters START thru LENGTH. | |
5081 | Discard newlines outside of strings, thus | |
5082 | converting funny-space markers to ordinary spaces. */ | |
0a8ad417 | 5083 | static void |
24c3c71a | 5084 | dump_defn_1 (base, start, length) |
0a8ad417 | 5085 | const U_CHAR *base; |
24c3c71a ZW |
5086 | int start; |
5087 | int length; | |
5088 | { | |
0a8ad417 KG |
5089 | const U_CHAR *p = base + start; |
5090 | const U_CHAR *limit = base + start + length; | |
24c3c71a ZW |
5091 | |
5092 | while (p < limit) { | |
5093 | if (*p != '\n') | |
5094 | putchar (*p); | |
5095 | else if (*p == '\"' || *p =='\'') { | |
0a8ad417 | 5096 | const U_CHAR *p1 = skip_quoted_string (p, limit, 0, 0, 0, 0); |
24c3c71a ZW |
5097 | fwrite (p, p1 - p, 1, stdout); |
5098 | p = p1 - 1; | |
5099 | } | |
5100 | p++; | |
5101 | } | |
5102 | } | |
5103 | ||
5104 | /* Print the name of argument number ARGNUM of macro definition DEFN. | |
5105 | Recall that DEFN->argnames contains all the arg names | |
5106 | concatenated in reverse order with comma-space in between. */ | |
0a8ad417 | 5107 | static void |
24c3c71a ZW |
5108 | dump_arg_n (defn, argnum) |
5109 | DEFINITION *defn; | |
5110 | int argnum; | |
5111 | { | |
b3694847 | 5112 | const U_CHAR *p = defn->argnames; |
24c3c71a | 5113 | while (argnum + 1 < defn->nargs) { |
0a8ad417 | 5114 | p = (const U_CHAR *) strchr ((const char *)p, ' ') + 1; |
24c3c71a ZW |
5115 | argnum++; |
5116 | } | |
5117 | ||
5118 | while (*p && *p != ',') { | |
5119 | putchar (*p); | |
5120 | p++; | |
5121 | } | |
5122 | } | |
24c3c71a ZW |
5123 | |
5124 | /* Initialize the built-in macros. */ | |
5125 | #define DSC(x) U x, sizeof x - 1 | |
5126 | #define install_spec(name, type) \ | |
5127 | install(DSC(name), type, -1); | |
5279d739 ZW |
5128 | #define install_value(name, val) do { \ |
5129 | hp = install(DSC(name), T_CONST, -1); hp->value.cpval = val; \ | |
5130 | } while (0) | |
0a8ad417 | 5131 | static void |
24c3c71a ZW |
5132 | initialize_builtins () |
5133 | { | |
5134 | HASHNODE *hp; | |
5135 | ||
5136 | install_spec ("__BASE_FILE__", T_BASE_FILE); | |
5137 | install_spec ("__DATE__", T_DATE); | |
5138 | install_spec ("__FILE__", T_FILE); | |
5139 | install_spec ("__TIME__", T_TIME); | |
5140 | install_spec ("__VERSION__", T_VERSION); | |
5141 | install_spec ("__INCLUDE_LEVEL__", T_INCLUDE_LEVEL); | |
5142 | install_spec ("__LINE__", T_SPECLINE); | |
5143 | ||
0fef3fd0 NB |
5144 | if (flag_signed_char == 0) |
5145 | install_value ("__CHAR_UNSIGNED__", "1"); | |
24c3c71a ZW |
5146 | } |
5147 | #undef DSC | |
5148 | #undef install_spec | |
5149 | #undef install_value | |
5150 | \f | |
40bd4395 | 5151 | /* Common handler of command line directives -U, -D and -A. */ |
0a8ad417 | 5152 | static void |
40bd4395 NB |
5153 | run_directive (str, len, type) |
5154 | const char *str; | |
5155 | size_t len; | |
5156 | enum node_type type; | |
24c3c71a | 5157 | { |
0b5826ac | 5158 | const struct directive *kt; |
40bd4395 NB |
5159 | FILE_BUF *ip = &instack[++indepth]; |
5160 | ip->fname = "*command line*"; | |
24c3c71a | 5161 | |
40bd4395 | 5162 | ip->buf = ip->bufp = (U_CHAR *) str; |
1e18a243 | 5163 | ip->length = len; |
24c3c71a ZW |
5164 | ip->lineno = 1; |
5165 | ip->macro = 0; | |
5166 | ip->free_ptr = 0; | |
5167 | ip->if_stack = if_stack; | |
5168 | ||
40bd4395 | 5169 | for (kt = directive_table; kt->type != type; kt++) |
24c3c71a ZW |
5170 | ; |
5171 | ||
40bd4395 | 5172 | (*kt->func) ((U_CHAR *) str, (U_CHAR *) str + len, NULL); |
24c3c71a ZW |
5173 | --indepth; |
5174 | } | |
5175 | ||
40bd4395 NB |
5176 | /* Handle the -D option. If STR is just an identifier, define it with |
5177 | * value 1. If STR has anything after the identifier, then it should | |
5178 | * be identifier-space-definition. */ | |
0a8ad417 | 5179 | static void |
40bd4395 NB |
5180 | make_definition (str) |
5181 | const char *str; | |
24c3c71a | 5182 | { |
40bd4395 NB |
5183 | char *buf, *p; |
5184 | size_t count; | |
24c3c71a | 5185 | |
40bd4395 NB |
5186 | /* Copy the entire option so we can modify it. |
5187 | Change the first "=" in the string to a space. If there is none, | |
5188 | tack " 1" on the end. */ | |
24c3c71a | 5189 | |
40bd4395 NB |
5190 | /* Length including the null. */ |
5191 | count = strlen (str); | |
5192 | buf = (char *) alloca (count + 2); | |
5193 | memcpy (buf, str, count); | |
24c3c71a | 5194 | |
40bd4395 NB |
5195 | p = strchr (str, '='); |
5196 | if (p) | |
5197 | buf[p - str] = ' '; | |
5198 | else | |
5199 | { | |
5200 | buf[count++] = ' '; | |
5201 | buf[count++] = '1'; | |
5202 | } | |
24c3c71a | 5203 | |
40bd4395 NB |
5204 | run_directive (buf, count, T_DEFINE); |
5205 | } | |
5206 | ||
5207 | /* Handle the -U option. */ | |
5208 | static void | |
5209 | make_undef (str) | |
5210 | const char *str; | |
5211 | { | |
5212 | run_directive (str, strlen (str), T_UNDEF); | |
5213 | } | |
5214 | ||
5215 | /* Handles the #assert (-A) and #unassert (-A-) command line options. */ | |
5216 | static void | |
5217 | make_assertion (str) | |
5218 | const char *str; | |
5219 | { | |
5220 | enum node_type type = T_ASSERT; | |
5221 | size_t count; | |
5222 | const char *p; | |
5223 | ||
5224 | if (*str == '-') | |
5225 | { | |
5226 | str++; | |
5227 | type = T_UNASSERT; | |
5228 | } | |
5229 | ||
5230 | count = strlen (str); | |
5231 | p = strchr (str, '='); | |
5232 | if (p) | |
5233 | { | |
5234 | /* Copy the entire option so we can modify it. Change the first | |
5235 | "=" in the string to a '(', and tack a ')' on the end. */ | |
5236 | char *buf = (char *) alloca (count + 1); | |
5237 | ||
5238 | memcpy (buf, str, count); | |
5239 | buf[p - str] = '('; | |
5240 | buf[count++] = ')'; | |
5241 | str = buf; | |
5242 | } | |
5243 | ||
5244 | run_directive (str, count, type); | |
24c3c71a ZW |
5245 | } |
5246 | \f | |
24c3c71a ZW |
5247 | /* Get the file-mode and data size of the file open on FD |
5248 | and store them in *MODE_POINTER and *SIZE_POINTER. */ | |
5249 | ||
0a8ad417 | 5250 | static int |
24c3c71a ZW |
5251 | file_size_and_mode (fd, mode_pointer, size_pointer) |
5252 | int fd; | |
5253 | int *mode_pointer; | |
5254 | long *size_pointer; | |
5255 | { | |
5256 | struct stat sbuf; | |
5257 | ||
5258 | if (fstat (fd, &sbuf) < 0) return -1; | |
5259 | if (mode_pointer) *mode_pointer = sbuf.st_mode; | |
5260 | if (size_pointer) *size_pointer = sbuf.st_size; | |
5261 | return 0; | |
5262 | } |