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