PR c++/3637
[official-gcc.git] / gcc / tradcpp.c
blob730dfaa27cd380347a6d37195ca0cb2ce7c9babb
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
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.
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.
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. */
22 #include "config.h"
23 #include "system.h"
24 #include "version.h"
25 #include "cppdefault.h"
26 #include "tradcpp.h"
27 #include "mkdeps.h"
28 #include "intl.h"
30 typedef unsigned char U_CHAR;
32 /* Name under which this program was invoked. */
34 static const char *progname;
36 /* Current maximum length of directory names in the search path
37 for include files. (Altered as we get more of them.) */
39 size_t max_include_len;
41 /* Nonzero means copy comments into the output file. */
43 int put_out_comments = 0;
45 /* mkdeps.h opaque structure that encapsulates dependency information. */
46 struct deps *deps;
48 /* Nonzero means print the names of included files rather than
49 the preprocessed output. 1 means just the #include "...",
50 2 means #include <...> as well. */
52 int print_deps = 0;
54 /* Nonzero means print dummy targets for each header file. */
56 int print_deps_phony_targets = 0;
58 /* If true, fopen (deps_file, "a") else fopen (deps_file, "w"). */
60 int deps_append = 0;
62 /* File name which deps are being written to. This is 0 if deps are
63 being written to stdout. */
65 const char *deps_file = 0;
67 /* Nonzero if missing .h files in -M output are assumed to be
68 generated files and not errors. */
70 int deps_missing_files = 0;
72 /* Nonzero means don't output line number information. */
74 int no_line_commands;
76 /* Nonzero means inhibit output of the preprocessed text
77 and instead output the definitions of all user-defined macros
78 in a form suitable for use as input to cccp. */
80 int dump_macros;
82 /* Nonzero means don't print warning messages. -w. */
84 int inhibit_warnings = 0;
86 /* Non-0 means don't output the preprocessed program. */
87 int inhibit_output = 0;
89 /* Nonzero means warn if slash-star appears in a comment. */
91 int warn_comments;
93 /* Nonzero causes output not to be done,
94 but directives such as #define that have side effects
95 are still obeyed. */
97 int no_output;
99 /* Value of __USER_LABEL_PREFIX__. Target-dependent, also controlled
100 by -f(no-)leading-underscore. */
101 static const char *user_label_prefix;
103 /* I/O buffer structure.
104 The `fname' field is nonzero for source files and #include files
105 and for the dummy text used for -D and -U.
106 It is zero for rescanning results of macro expansion
107 and for expanding macro arguments. */
108 #define INPUT_STACK_MAX 200
109 struct file_name_list;
110 struct file_buf {
111 const char *fname;
112 int lineno;
113 int length;
114 U_CHAR *buf;
115 U_CHAR *bufp;
116 /* Macro that this level is the expansion of.
117 Included so that we can reenable the macro
118 at the end of this level. */
119 struct hashnode *macro;
120 /* Value of if_stack at start of this file.
121 Used to prohibit unmatched #endif (etc) in an include file. */
122 struct if_stack *if_stack;
123 /* Object to be freed at end of input at this level. */
124 U_CHAR *free_ptr;
125 /* Position to start scanning for #include_next in this file. */
126 struct file_name_list *next_header_dir;
127 } instack[INPUT_STACK_MAX];
129 typedef struct file_buf FILE_BUF;
131 /* Current nesting level of input sources.
132 `instack[indepth]' is the level currently being read. */
133 int indepth = -1;
134 #define CHECK_DEPTH(code) \
135 if (indepth >= (INPUT_STACK_MAX - 1)) \
137 error_with_line (line_for_error (instack[indepth].lineno), \
138 "macro or #include recursion too deep"); \
139 code; \
142 /* Current depth in #include directives that use <...>. */
143 int system_include_depth = 0;
145 /* The output buffer. Its LENGTH field is the amount of room allocated
146 for the buffer, not the number of chars actually present. To get
147 that, subtract outbuf.buf from outbuf.bufp. */
149 #define OUTBUF_SIZE 10 /* initial size of output buffer */
150 FILE_BUF outbuf;
152 /* Grow output buffer OBUF points at
153 so it can hold at least NEEDED more chars. */
155 #define check_expand(OBUF, NEEDED) do { \
156 if ((OBUF)->length - ((OBUF)->bufp - (OBUF)->buf) <= (NEEDED)) \
157 grow_outbuf ((OBUF), (NEEDED)); \
158 } while (0)
160 struct file_name_list
162 struct file_name_list *next;
163 const char *fname;
166 struct file_name_list *include = 0; /* First dir to search */
167 /* First dir to search for <file> */
168 struct file_name_list *first_bracket_include = 0;
169 struct file_name_list *last_include = 0; /* Last in chain */
171 /* List of included files that contained #once. */
172 struct file_name_list *dont_repeat_files = 0;
174 /* List of other included files. */
175 struct file_name_list *all_include_files = 0;
177 /* Structure allocated for every #define. For a simple replacement
178 such as
179 #define foo bar ,
180 nargs = -1, the `pattern' list is null, and the expansion is just
181 the replacement text. Nargs = 0 means a functionlike macro with no args,
182 e.g.,
183 #define getchar() getc (stdin) .
184 When there are args, the expansion is the replacement text with the
185 args squashed out, and the reflist is a list describing how to
186 build the output from the input: e.g., "3 chars, then the 1st arg,
187 then 9 chars, then the 3rd arg, then 0 chars, then the 2nd arg".
188 The chars here come from the expansion. Whatever is left of the
189 expansion after the last arg-occurrence is copied after that arg.
190 Note that the reflist can be arbitrarily long---
191 its length depends on the number of times the arguments appear in
192 the replacement text, not how many args there are. Example:
193 #define f(x) x+x+x+x+x+x+x would have replacement text "++++++" and
194 pattern list
195 { (0, 1), (1, 1), (1, 1), ..., (1, 1), NULL }
196 where (x, y) means (nchars, argno). */
198 typedef struct definition DEFINITION;
199 struct definition {
200 int nargs;
201 int length; /* length of expansion string */
202 U_CHAR *expansion;
203 struct reflist {
204 struct reflist *next;
205 char stringify; /* nonzero if this arg was preceded by a
206 # operator. */
207 char raw_before; /* Nonzero if a ## operator before arg. */
208 char raw_after; /* Nonzero if a ## operator after arg. */
209 int nchars; /* Number of literal chars to copy before
210 this arg occurrence. */
211 int argno; /* Number of arg to substitute (origin-0) */
212 } *pattern;
213 /* Names of macro args, concatenated in reverse order
214 with comma-space between them.
215 The only use of this is that we warn on redefinition
216 if this differs between the old and new definitions. */
217 const U_CHAR *argnames;
220 /* Chained list of answers to an assertion. */
221 struct answer
223 struct answer *next;
224 const unsigned char *answer;
225 size_t len;
228 /* different kinds of things that can appear in the value field
229 of a hash node. Actually, this may be useless now. */
230 union hashval {
231 const char *cpval;
232 DEFINITION *defn;
233 struct answer *answers;
236 /* The structure of a node in the hash table. The hash table
237 has entries for all tokens defined by #define commands (type T_MACRO),
238 plus some special tokens like __LINE__ (these each have their own
239 type, and the appropriate code is run when that type of node is seen.
240 It does not contain control words like "#define", which are recognized
241 by a separate piece of code. */
243 /* different flavors of hash nodes --- also used in keyword table */
244 enum node_type {
245 T_DEFINE = 1, /* `#define' */
246 T_INCLUDE, /* `#include' */
247 T_INCLUDE_NEXT,/* `#include_next' */
248 T_IFDEF, /* `#ifdef' */
249 T_IFNDEF, /* `#ifndef' */
250 T_IF, /* `#if' */
251 T_ELSE, /* `#else' */
252 T_ELIF, /* `#elif' */
253 T_UNDEF, /* `#undef' */
254 T_LINE, /* `#line' */
255 T_ENDIF, /* `#endif' */
256 T_ERROR, /* `#error' */
257 T_WARNING, /* `#warning' */
258 T_ASSERT, /* `#assert' */
259 T_UNASSERT, /* `#unassert' */
260 T_SPECLINE, /* special symbol `__LINE__' */
261 T_DATE, /* `__DATE__' */
262 T_FILE, /* `__FILE__' */
263 T_BASE_FILE, /* `__BASE_FILE__' */
264 T_INCLUDE_LEVEL, /* `__INCLUDE_LEVEL__' */
265 T_VERSION, /* `__VERSION__' */
266 T_TIME, /* `__TIME__' */
267 T_CONST, /* Constant value, used by `__STDC__' */
268 T_MACRO, /* macro defined by `#define' */
269 T_SPEC_DEFINED, /* special `defined' macro for use in #if statements */
270 T_UNUSED /* Used for something not defined. */
273 struct hashnode {
274 struct hashnode *next; /* double links for easy deletion */
275 struct hashnode *prev;
276 struct hashnode **bucket_hdr; /* also, a back pointer to this node's hash
277 chain is kept, in case the node is the head
278 of the chain and gets deleted. */
279 enum node_type type; /* type of special token */
280 int length; /* length of token, for quick comparison */
281 U_CHAR *name; /* the actual name */
282 union hashval value; /* pointer to expansion, or whatever */
285 typedef struct hashnode HASHNODE;
287 static HASHNODE *parse_assertion PARAMS ((const unsigned char *,
288 const unsigned char *,
289 struct answer **, int));
290 static struct answer **find_answer PARAMS ((HASHNODE *,
291 const struct answer *));
292 static int parse_answer PARAMS ((const unsigned char *, const unsigned char *,
293 struct answer **, int));
294 static unsigned char *canonicalize_text PARAMS ((const unsigned char *,
295 const unsigned char *,
296 const unsigned char **));
298 /* Some definitions for the hash table. The hash function MUST be
299 computed as shown in hashf () below. That is because the rescan
300 loop computes the hash value `on the fly' for most tokens,
301 in order to avoid the overhead of a lot of procedure calls to
302 the hashf () function. Hashf () only exists for the sake of
303 politeness, for use when speed isn't so important. */
305 #define HASHSIZE 1403
306 HASHNODE *hashtab[HASHSIZE];
307 #define HASHSTEP(old, c) ((old << 2) + c)
308 #define MAKE_POS(v) (v & 0x7fffffff) /* make number positive */
310 /* `struct directive' defines one #-directive, including how to handle it. */
312 struct directive {
313 int length; /* Length of name */
314 void (*func) PARAMS ((U_CHAR *, U_CHAR *, FILE_BUF *));
315 /* Function to handle directive */
316 const char *name; /* Name of directive */
317 enum node_type type; /* Code which describes which directive. */
320 /* Last arg to output_line_command. */
321 enum file_change_code {same_file, enter_file, leave_file};
323 /* This structure represents one parsed argument in a macro call.
324 `raw' points to the argument text as written (`raw_length' is its length).
325 `expanded' points to the argument's macro-expansion
326 (its length is `expand_length').
327 `stringified_length' is the length the argument would have
328 if stringified.
329 `free1' and `free2', if nonzero, point to blocks to be freed
330 when the macro argument data is no longer needed. */
332 struct argdata {
333 U_CHAR *raw, *expanded;
334 int raw_length, expand_length;
335 int stringified_length;
336 U_CHAR *free1, *free2;
337 char newlines;
338 char comments;
341 /* The arglist structure is built by do_define to tell
342 collect_definition where the argument names begin. That
343 is, for a define like "#define f(x,y,z) foo+x-bar*y", the arglist
344 would contain pointers to the strings x, y, and z.
345 Collect_definition would then build a DEFINITION node,
346 with reflist nodes pointing to the places x, y, and z had
347 appeared. So the arglist is just convenience data passed
348 between these two routines. It is not kept around after
349 the current #define has been processed and entered into the
350 hash table. */
352 struct arglist {
353 struct arglist *next;
354 U_CHAR *name;
355 int length;
356 int argno;
359 /* Function prototypes. */
361 static void do_define PARAMS ((U_CHAR *, U_CHAR *, FILE_BUF *));
362 static void do_error PARAMS ((U_CHAR *, U_CHAR *, FILE_BUF *));
363 static void do_warning PARAMS ((U_CHAR *, U_CHAR *, FILE_BUF *));
364 static void do_line PARAMS ((U_CHAR *, U_CHAR *, FILE_BUF *));
365 static void do_include PARAMS ((U_CHAR *, U_CHAR *, FILE_BUF *));
366 static void do_include_next PARAMS ((U_CHAR *, U_CHAR *, FILE_BUF *));
367 static void do_undef PARAMS ((U_CHAR *, U_CHAR *, FILE_BUF *));
368 static void do_if PARAMS ((U_CHAR *, U_CHAR *, FILE_BUF *));
369 static void do_ifdef PARAMS ((U_CHAR *, U_CHAR *, FILE_BUF *));
370 static void do_ifndef PARAMS ((U_CHAR *, U_CHAR *, FILE_BUF *));
371 static void do_else PARAMS ((U_CHAR *, U_CHAR *, FILE_BUF *));
372 static void do_elif PARAMS ((U_CHAR *, U_CHAR *, FILE_BUF *));
373 static void do_endif PARAMS ((U_CHAR *, U_CHAR *, FILE_BUF *));
374 static void do_assert PARAMS ((U_CHAR *, U_CHAR *, FILE_BUF *));
375 static void do_unassert PARAMS ((U_CHAR *, U_CHAR *, FILE_BUF *));
376 static void do_xifdef PARAMS ((U_CHAR *, U_CHAR *, enum node_type));
378 static struct hashnode *install PARAMS ((const U_CHAR *, int, enum node_type, int));
379 static int hashf PARAMS ((const U_CHAR *, int, int));
380 static int compare_defs PARAMS ((DEFINITION *, DEFINITION *));
381 static int comp_def_part PARAMS ((int, const U_CHAR *, int,
382 const U_CHAR *, int, int));
383 static void delete_macro PARAMS ((HASHNODE *));
385 /* First arg to v_message. */
386 enum msgtype { MT_WARNING = 0, MT_ERROR, MT_FATAL };
387 static void v_message PARAMS ((enum msgtype mtype, int line,
388 const char *msgid, va_list ap))
389 ATTRIBUTE_PRINTF (3, 0);
391 static int line_for_error PARAMS ((int));
393 /* We know perfectly well which file this is, so we don't need to
394 use __FILE__. */
395 #undef abort
396 #if (GCC_VERSION >= 2007)
397 #define abort() fancy_abort(__LINE__, __FUNCTION__)
398 #else
399 #define abort() fancy_abort(__LINE__, 0);
400 #endif
402 static void macroexpand PARAMS ((HASHNODE *, FILE_BUF *));
403 static void special_symbol PARAMS ((HASHNODE *, FILE_BUF *));
404 static void dump_all_macros PARAMS ((void));
405 static void dump_defn_1 PARAMS ((const U_CHAR *, int, int));
406 static void dump_arg_n PARAMS ((DEFINITION *, int));
407 static void conditional_skip PARAMS ((FILE_BUF *, int, enum node_type));
408 static void skip_if_group PARAMS ((FILE_BUF *, int));
409 static void output_line_command PARAMS ((FILE_BUF *, FILE_BUF *,
410 int, enum file_change_code));
412 static int eval_if_expression PARAMS ((const U_CHAR *, int));
414 static void output_deps PARAMS ((void));
415 static void initialize_builtins PARAMS ((void));
416 static void run_directive PARAMS ((const char *, size_t,
417 enum node_type));
418 static void make_definition PARAMS ((const char *));
419 static void make_undef PARAMS ((const char *));
420 static void make_assertion PARAMS ((const char *));
422 static void grow_outbuf PARAMS ((FILE_BUF *, int));
423 static int handle_directive PARAMS ((FILE_BUF *, FILE_BUF *));
424 static void process_include PARAMS ((struct file_name_list *,
425 const U_CHAR *, int, int, FILE_BUF *));
426 static void finclude PARAMS ((int, const char *,
427 struct file_name_list *, FILE_BUF *));
428 static void init_dependency_output PARAMS ((void));
429 static void rescan PARAMS ((FILE_BUF *, int));
430 static void newline_fix PARAMS ((U_CHAR *));
431 static void name_newline_fix PARAMS ((U_CHAR *));
432 static U_CHAR *macarg1 PARAMS ((U_CHAR *, const U_CHAR *, int *,
433 int *, int *));
434 static const char *macarg PARAMS ((struct argdata *));
435 static int discard_comments PARAMS ((U_CHAR *, int, int));
436 static int file_size_and_mode PARAMS ((int, int *, long *));
438 static U_CHAR *skip_to_end_of_comment PARAMS ((FILE_BUF *, int *));
439 static U_CHAR *skip_quoted_string PARAMS ((const U_CHAR *, const U_CHAR *,
440 int, int *, int *, int *));
442 int main PARAMS ((int, char **));
444 /* Convenience. Write U"string" to get an unsigned string constant. */
445 #define U (const unsigned char *)
447 /* Here is the actual list of #-directives, most-often-used first. */
449 struct directive directive_table[] = {
450 { 6, do_define, "define", T_DEFINE },
451 { 7, do_include, "include", T_INCLUDE },
452 { 5, do_endif, "endif", T_ENDIF },
453 { 5, do_ifdef, "ifdef", T_IFDEF },
454 { 2, do_if, "if", T_IF, },
455 { 4, do_else, "else", T_ELSE },
456 { 6, do_ifndef, "ifndef", T_IFNDEF },
457 { 5, do_undef, "undef", T_UNDEF },
458 { 4, do_line, "line", T_LINE },
459 { 4, do_elif, "elif", T_ELIF },
460 { 5, do_error, "error", T_ERROR },
461 { 7, do_warning, "warning", T_WARNING },
462 { 12, do_include_next, "include_next", T_INCLUDE_NEXT },
463 { 6, do_assert, "assert", T_ASSERT },
464 { 8, do_unassert,"unassert",T_UNASSERT},
465 { -1, 0, "", T_UNUSED},
468 #define SKIP_WHITE_SPACE(p) do { while (is_nvspace(*p)) p++; } while (0)
469 #define SKIP_ALL_WHITE_SPACE(p) do { while (is_space(*p)) p++; } while (0)
471 int errors = 0; /* Error counter for exit code */
473 static FILE_BUF expand_to_temp_buffer PARAMS ((const U_CHAR *, const U_CHAR *, int));
474 static DEFINITION *collect_expansion PARAMS ((U_CHAR *, U_CHAR *, int,
475 struct arglist *));
477 /* Stack of conditionals currently in progress
478 (including both successful and failing conditionals). */
480 struct if_stack {
481 struct if_stack *next; /* for chaining to the next stack frame */
482 const char *fname; /* copied from input when frame is made */
483 int lineno; /* similarly */
484 int if_succeeded; /* true if a leg of this if-group
485 has been passed through rescan */
486 enum node_type type; /* type of last directive seen in this group */
488 typedef struct if_stack IF_STACK_FRAME;
489 IF_STACK_FRAME *if_stack = NULL;
491 /* Nonzero means -I- has been seen,
492 so don't look for #include "foo" the source-file directory. */
493 int ignore_srcdir;
495 /* Pending directives. */
496 enum pending_dir_t {PD_NONE = 0, PD_DEFINE, PD_UNDEF, PD_ASSERTION, PD_FILE};
498 typedef struct pending_dir pending_dir;
499 struct pending_dir
501 const char *arg;
502 enum pending_dir_t type;
506 main (argc, argv)
507 int argc;
508 char **argv;
510 int st_mode;
511 long st_size;
512 const char *in_fname, *out_fname;
513 int f, i;
514 FILE_BUF *fp;
515 pending_dir *pend = (pending_dir *) xcalloc (argc, sizeof (pending_dir));
516 int no_standard_includes = 0;
518 #ifdef RLIMIT_STACK
519 /* Get rid of any avoidable limit on stack size. */
521 struct rlimit rlim;
523 /* Set the stack limit huge so that alloca (particularly stringtab
524 * in dbxread.c) does not fail. */
525 getrlimit (RLIMIT_STACK, &rlim);
526 rlim.rlim_cur = rlim.rlim_max;
527 setrlimit (RLIMIT_STACK, &rlim);
529 #endif /* RLIMIT_STACK defined */
531 progname = argv[0];
533 in_fname = NULL;
534 out_fname = NULL;
536 no_line_commands = 0;
537 dump_macros = 0;
538 no_output = 0;
540 max_include_len = cpp_GCC_INCLUDE_DIR_len + 7; /* ??? */
542 gcc_init_libintl ();
544 /* It's simplest to just create this struct whether or not it will
545 be needed. */
546 deps = deps_init ();
548 /* Process switches and find input file name. */
550 for (i = 1; i < argc; i++) {
551 if (argv[i][0] != '-') {
552 if (out_fname != NULL)
553 fatal ("Usage: %s [switches] input output", argv[0]);
554 else if (in_fname != NULL)
555 out_fname = argv[i];
556 else
557 in_fname = argv[i];
558 } else {
559 int c = argv[i][1];
561 switch (c) {
562 case 'E':
563 case '$':
564 break; /* Ignore for compatibility with ISO/extended cpp. */
566 case 'l':
567 if (!strcmp (argv[i], "-lang-c++")
568 || !strcmp (argv[i], "-lang-objc++"))
569 fatal ("-traditional is not supported in C++");
570 else if (!strcmp (argv[i], "-lang-c89"))
571 fatal ("-traditional and -ansi are mutually exclusive");
572 else if (!strcmp (argv[i], "-lang-objc"))
573 pend[i].type = PD_DEFINE, pend[i].arg = "__OBJC__";
574 else if (!strcmp (argv[i], "-lang-asm"))
575 pend[i].type = PD_DEFINE, pend[i].arg = "__ASSEMBLER__";
576 else if (!strcmp (argv[i], "-lang-fortran"))
577 pend[i].type = PD_DEFINE, pend[i].arg = "_LANGUAGE_FORTRAN";
578 /* All other possibilities ignored. */
579 break;
581 case 'i':
582 if (!strcmp (argv[i], "-include"))
584 if (i + 1 == argc)
585 fatal ("Filename missing after -i option");
586 else
587 pend[i].type = PD_FILE, pend[i].arg = argv[i + 1], i++;
589 else if (!strcmp (argv[i], "-iprefix"))
590 i++; /* Ignore for compatibility */
591 else if (!strcmp (argv[i], "-isystem")
592 || !strcmp (argv[i], "-iwithprefix")
593 || !strcmp (argv[i], "-iwithprefixbefore")
594 || !strcmp (argv[i], "-idirafter"))
595 goto add_include; /* best we can do */
597 break;
599 case 'o':
600 if (out_fname != NULL)
601 fatal ("Output filename specified twice");
602 if (i + 1 == argc)
603 fatal ("Filename missing after -o option");
604 out_fname = argv[++i];
605 if (!strcmp (out_fname, "-"))
606 out_fname = "";
607 break;
609 case 'w':
610 inhibit_warnings = 1;
611 break;
613 case 'W':
614 if (!strcmp (argv[i], "-Wcomments"))
615 warn_comments = 1;
616 else if (!strcmp (argv[i], "-Wcomment"))
617 warn_comments = 1;
618 else if (!strcmp (argv[i], "-Wall")) {
619 warn_comments = 1;
621 break;
623 case 'f':
624 if (!strcmp (argv[i], "-fleading-underscore"))
625 user_label_prefix = "_";
626 else if (!strcmp (argv[i], "-fno-leading-underscore"))
627 user_label_prefix = "";
628 break;
630 case 'M':
632 char *p = NULL;
634 /* -MD and -MMD for tradcpp are deprecated and undocumented
635 (use -M or -MM with -MF instead), and probably should be
636 removed with the next major GCC version. For the moment
637 we allow these for the benefit of Automake 1.4, which
638 uses these when dependency tracking is enabled. Automake
639 1.5 will fix this. */
640 if (!strncmp (argv[i], "-MD", 3)) {
641 p = argv[i] + 3;
642 print_deps = 2;
643 } else if (!strncmp (argv[i], "-MMD", 4)) {
644 p = argv[i] + 4;
645 print_deps = 1;
646 } else if (!strcmp (argv[i], "-M")) {
647 print_deps = 2;
648 } else if (!strcmp (argv[i], "-MM")) {
649 print_deps = 1;
650 } else if (!strcmp (argv[i], "-MG")) {
651 deps_missing_files = 1;
652 } else if (!strcmp (argv[i], "-MF")) {
653 p = argv[i] + 3;
654 } else if (!strcmp (argv[i], "-MP")) {
655 print_deps_phony_targets = 1;
656 } else if (!strcmp (argv[i], "-MQ") || !strcmp (argv[i], "-MT")) {
657 /* Add a target. -MQ quotes for Make. */
658 const char *tgt = argv[i] + 3;
659 int quoted = argv[i][2] == 'Q';
661 if (*tgt == '\0' && i + 1 == argc)
662 fatal ("Target missing after %s option", argv[i]);
663 else
665 if (*tgt == '\0')
666 tgt = argv[++i];
668 deps_add_target (deps, tgt, quoted);
672 if (p) {
673 if (*p)
674 deps_file = p;
675 else if (i + 1 == argc)
676 fatal ("Filename missing after %s option", argv[i]);
677 else
678 deps_file = argv[++i];
681 break;
683 case 'd':
684 dump_macros = 1;
685 no_output = 1;
686 break;
688 case 'v':
689 fprintf (stderr, "GNU traditional CPP version %s\n", version_string);
690 break;
692 case 'D':
693 case 'U':
694 case 'A':
696 char *p;
698 if (argv[i][2] != 0)
699 p = argv[i] + 2;
700 else if (i + 1 == argc)
701 fatal ("Macro name missing after -%c option", c);
702 else
703 p = argv[++i];
705 if (c == 'D')
706 pend[i].type = PD_DEFINE;
707 else if (c == 'U')
708 pend[i].type = PD_UNDEF;
709 else
710 pend[i].type = PD_ASSERTION;
711 pend[i].arg = p;
713 break;
715 case 'C':
716 put_out_comments = 1;
717 break;
719 case 'p':
720 if (!strcmp (argv[i], "-pedantic"))
721 fatal ("-pedantic and -traditional are mutually exclusive");
722 break;
724 case 't':
725 if (!strcmp (argv[i], "-trigraphs"))
726 fatal ("-trigraphs and -traditional are mutually exclusive");
727 break;
729 case 'P':
730 no_line_commands = 1;
731 break;
733 case 'I': /* Add directory to path for includes. */
734 add_include:
736 struct file_name_list *dirtmp;
738 if (! ignore_srcdir && !strcmp (argv[i] + 2, "-"))
739 ignore_srcdir = 1;
740 else {
741 dirtmp = (struct file_name_list *)
742 xmalloc (sizeof (struct file_name_list));
743 dirtmp->next = 0; /* New one goes on the end */
744 if (include == 0)
745 include = dirtmp;
746 else
747 last_include->next = dirtmp;
748 last_include = dirtmp; /* Tail follows the last one */
749 if (argv[i][1] == 'I' && argv[i][2] != 0)
750 dirtmp->fname = argv[i] + 2;
751 else if (i + 1 == argc)
752 fatal ("Directory name missing after -I option");
753 else
754 dirtmp->fname = argv[++i];
755 if (strlen (dirtmp->fname) > max_include_len)
756 max_include_len = strlen (dirtmp->fname);
757 if (ignore_srcdir && first_bracket_include == 0)
758 first_bracket_include = dirtmp;
761 break;
763 case 'n':
764 /* -nostdinc causes no default include directories.
765 You must specify all include-file directories with -I. */
766 no_standard_includes = 1;
767 break;
769 case '\0': /* JF handle '-' as file name meaning stdin or stdout */
770 if (in_fname == NULL) {
771 in_fname = "";
772 break;
773 } else if (out_fname == NULL) {
774 out_fname = "";
775 break;
776 } /* else fall through into error */
778 default:
779 fatal ("Invalid option `%s'", argv[i]);
784 init_dependency_output ();
786 /* After checking the environment variables, check if -M or -MM has
787 not been specified, but other -M options have. */
788 if (print_deps == 0
789 && (deps_missing_files || deps_file || print_deps_phony_targets))
790 fatal ("you must additionally specify either -M or -MM");
792 if (user_label_prefix == 0)
793 user_label_prefix = USER_LABEL_PREFIX;
795 if (print_deps)
797 /* Set the default target (if there is none already), and
798 the dependency on the main file. */
799 deps_add_default_target (deps, in_fname);
801 deps_add_dep (deps, in_fname);
804 /* Install __LINE__, etc. Must follow option processing. */
805 initialize_builtins ();
807 /* Do defines specified with -D and undefines specified with -U. */
808 for (i = 1; i < argc; i++)
809 if (pend[i].type == PD_DEFINE)
810 make_definition (pend[i].arg);
811 else if (pend[i].type == PD_UNDEF)
812 make_undef (pend[i].arg);
813 else if (pend[i].type == PD_ASSERTION)
814 make_assertion (pend[i].arg);
816 /* Unless -fnostdinc,
817 tack on the standard include file dirs to the specified list */
818 if (!no_standard_includes) {
819 const struct default_include *di;
820 struct file_name_list *old_last_include = last_include;
821 struct file_name_list *dirtmp;
822 for (di = cpp_include_defaults; di->fname; di++) {
823 if (di->cplusplus)
824 continue;
825 dirtmp = (struct file_name_list *)
826 xmalloc (sizeof (struct file_name_list));
827 dirtmp->next = 0; /* New one goes on the end */
828 if (include == 0)
829 include = dirtmp;
830 else
831 last_include->next = dirtmp;
832 last_include = dirtmp; /* Tail follows the last one */
833 dirtmp->fname = di->fname;
834 if (strlen (dirtmp->fname) > max_include_len)
835 max_include_len = strlen (dirtmp->fname);
838 if (ignore_srcdir && first_bracket_include == 0)
839 first_bracket_include = old_last_include->next;
842 /* Initialize output buffer */
844 outbuf.buf = (U_CHAR *) xmalloc (OUTBUF_SIZE);
845 outbuf.bufp = outbuf.buf;
846 outbuf.length = OUTBUF_SIZE;
848 /* Scan the -i files before the main input.
849 Much like #including them, but with no_output set
850 so that only their macro definitions matter. */
852 no_output++;
853 indepth++;
854 for (i = 1; i < argc; i++)
855 if (pend[i].type == PD_FILE)
857 int fd = open (pend[i].arg, O_RDONLY, 0666);
858 if (fd < 0)
860 perror_with_name (pend[i].arg);
861 return FATAL_EXIT_CODE;
864 /* For -M, add this file to the dependencies. */
865 if (print_deps)
866 deps_add_dep (deps, pend[i].arg);
868 finclude (fd, pend[i].arg, 0, &outbuf);
870 indepth--;
871 no_output--;
873 /* Pending directives no longer needed. */
874 free ((PTR) pend);
876 /* Create an input stack level for the main input file
877 and copy the entire contents of the file into it. */
879 fp = &instack[++indepth];
881 /* JF check for stdin */
882 if (in_fname == NULL || *in_fname == 0) {
883 in_fname = "";
884 f = 0;
885 } else if ((f = open (in_fname, O_RDONLY, 0666)) < 0)
886 goto sys_error;
888 if (file_size_and_mode (f, &st_mode, &st_size))
889 goto sys_error;
890 fp->fname = in_fname;
891 fp->lineno = 1;
892 /* JF all this is mine about reading pipes and ttys */
893 if (!S_ISREG (st_mode)) {
894 /* Read input from a file that is not a normal disk file.
895 We cannot preallocate a buffer with the correct size,
896 so we must read in the file a piece at the time and make it bigger. */
897 int size;
898 int bsize;
899 int cnt;
900 U_CHAR *bufp;
902 bsize = 2000;
903 size = 0;
904 fp->buf = (U_CHAR *) xmalloc (bsize + 2);
905 bufp = fp->buf;
906 for (;;) {
907 cnt = read (f, bufp, bsize - size);
908 if (cnt < 0) goto sys_error; /* error! */
909 if (cnt == 0) break; /* End of file */
910 size += cnt;
911 bufp += cnt;
912 if (bsize == size) { /* Buffer is full! */
913 bsize *= 2;
914 fp->buf = (U_CHAR *) xrealloc (fp->buf, bsize + 2);
915 bufp = fp->buf + size; /* May have moved */
918 fp->length = size;
919 } else {
920 /* Read a file whose size we can determine in advance.
921 For the sake of VMS, st_size is just an upper bound. */
922 long i;
923 fp->length = 0;
924 fp->buf = (U_CHAR *) xmalloc (st_size + 2);
926 while (st_size > 0) {
927 i = read (f, fp->buf + fp->length, st_size);
928 if (i <= 0) {
929 if (i == 0) break;
930 goto sys_error;
932 fp->length += i;
933 st_size -= i;
936 fp->bufp = fp->buf;
937 fp->if_stack = if_stack;
939 /* Make sure data ends with a newline. And put a null after it. */
941 if (fp->length > 0 && fp->buf[fp->length-1] != '\n')
942 fp->buf[fp->length++] = '\n';
943 fp->buf[fp->length] = '\0';
945 /* Now that we know the input file is valid, open the output. */
947 if (!out_fname || !strcmp (out_fname, ""))
948 out_fname = "stdout";
949 else if (! freopen (out_fname, "w", stdout))
950 pfatal_with_name (out_fname);
952 output_line_command (fp, &outbuf, 0, same_file);
954 /* Scan the input, processing macros and directives. */
956 rescan (&outbuf, 0);
958 /* Now we have processed the entire input
959 Write whichever kind of output has been requested. */
962 if (dump_macros)
963 dump_all_macros ();
964 else if (! inhibit_output)
965 if (write (fileno (stdout), outbuf.buf, outbuf.bufp - outbuf.buf) < 0)
966 fatal ("I/O error on output");
968 /* Don't write the deps file if preprocessing has failed. */
969 if (print_deps && errors == 0)
970 output_deps ();
972 /* Destruct the deps object. */
973 deps_free (deps);
975 if (ferror (stdout))
976 fatal ("I/O error on output");
978 if (errors)
979 exit (FATAL_EXIT_CODE);
980 exit (SUCCESS_EXIT_CODE);
982 sys_error:
983 pfatal_with_name (in_fname);
986 /* Set up dependency-file output. */
987 static void
988 init_dependency_output ()
990 char *spec, *s, *output_file;
992 /* Either of two environment variables can specify output of deps.
993 Its value is either "OUTPUT_FILE" or "OUTPUT_FILE DEPS_TARGET",
994 where OUTPUT_FILE is the file to write deps info to
995 and DEPS_TARGET is the target to mention in the deps. */
997 if (print_deps == 0)
999 spec = getenv ("DEPENDENCIES_OUTPUT");
1000 if (spec)
1001 print_deps = 1;
1002 else
1004 spec = getenv ("SUNPRO_DEPENDENCIES");
1005 if (spec)
1006 print_deps = 2;
1007 else
1008 return;
1011 /* Find the space before the DEPS_TARGET, if there is one. */
1012 s = strchr (spec, ' ');
1013 if (s)
1015 /* Let the caller perform MAKE quoting. */
1016 deps_add_target (deps, s + 1, 0);
1017 output_file = (char *) xmalloc (s - spec + 1);
1018 memcpy (output_file, spec, s - spec);
1019 output_file[s - spec] = 0;
1021 else
1022 output_file = spec;
1024 /* Command line overrides environment variables. */
1025 if (deps_file == 0)
1026 deps_file = output_file;
1027 deps_append = 1;
1030 /* If dependencies go to standard output, or -MG is used, we should
1031 suppress output. The user may be requesting other stuff to
1032 stdout, with -dM, -v etc. We let them shoot themselves in the
1033 foot. */
1034 if (deps_file == 0 || deps_missing_files)
1035 inhibit_output = 1;
1038 /* Use mkdeps.c to output dependency information. */
1039 static void
1040 output_deps ()
1042 /* Stream on which to print the dependency information. */
1043 FILE *deps_stream = 0;
1044 const char *const deps_mode = deps_append ? "a" : "w";
1046 if (deps_file == 0)
1047 deps_stream = stdout;
1048 else
1050 deps_stream = fopen (deps_file, deps_mode);
1051 if (deps_stream == 0)
1053 error_from_errno (deps_file);
1054 return;
1058 deps_write (deps, deps_stream, 72);
1060 if (print_deps_phony_targets)
1061 deps_phony_targets (deps, deps_stream);
1063 /* Don't close stdout. */
1064 if (deps_file)
1066 if (ferror (deps_stream) || fclose (deps_stream) != 0)
1067 fatal ("I/O error on output");
1071 /* Move all backslash-newline pairs out of embarrassing places.
1072 Exchange all such pairs following BP
1073 with any potentially-embarrasing characters that follow them.
1074 Potentially-embarrassing characters are / and *
1075 (because a backslash-newline inside a comment delimiter
1076 would cause it not to be recognized). */
1077 static void
1078 newline_fix (bp)
1079 U_CHAR *bp;
1081 U_CHAR *p = bp;
1082 int count = 0;
1084 /* First count the backslash-newline pairs here. */
1086 while (*p++ == '\\' && *p++ == '\n')
1087 count++;
1089 p = bp + count * 2;
1091 /* Exit if what follows the backslash-newlines is not embarrassing. */
1093 if (count == 0 || (*p != '/' && *p != '*'))
1094 return;
1096 /* Copy all potentially embarrassing characters
1097 that follow the backslash-newline pairs
1098 down to where the pairs originally started. */
1100 while (*p == '*' || *p == '/')
1101 *bp++ = *p++;
1103 /* Now write the same number of pairs after the embarrassing chars. */
1104 while (count-- > 0) {
1105 *bp++ = '\\';
1106 *bp++ = '\n';
1110 /* Like newline_fix but for use within a directive-name.
1111 Move any backslash-newlines up past any following symbol constituents. */
1112 static void
1113 name_newline_fix (bp)
1114 U_CHAR *bp;
1116 U_CHAR *p = bp;
1117 int count = 0;
1119 /* First count the backslash-newline pairs here. */
1121 while (*p++ == '\\' && *p++ == '\n')
1122 count++;
1124 p = bp + count * 2;
1126 /* What follows the backslash-newlines is not embarrassing. */
1128 if (count == 0 || !is_idchar (*p))
1129 return;
1131 /* Copy all potentially embarrassing characters
1132 that follow the backslash-newline pairs
1133 down to where the pairs originally started. */
1135 while (is_idchar (*p))
1136 *bp++ = *p++;
1138 /* Now write the same number of pairs after the embarrassing chars. */
1139 while (count-- > 0) {
1140 *bp++ = '\\';
1141 *bp++ = '\n';
1146 * The main loop of the program.
1148 * Read characters from the input stack, transferring them to the
1149 * output buffer OP.
1151 * Macros are expanded and push levels on the input stack.
1152 * At the end of such a level it is popped off and we keep reading.
1153 * At the end of any other kind of level, we return.
1154 * #-directives are handled, except within macros.
1156 * If OUTPUT_MARKS is nonzero, keep Newline markers found in the input
1157 * and insert them when appropriate. This is set while scanning macro
1158 * arguments before substitution. It is zero when scanning for final output.
1159 * There are three types of Newline markers:
1160 * * Newline - follows a macro name that was not expanded
1161 * because it appeared inside an expansion of the same macro.
1162 * This marker prevents future expansion of that identifier.
1163 * When the input is rescanned into the final output, these are deleted.
1164 * These are also deleted by ## concatenation.
1165 * * Newline Space (or Newline and any other whitespace character)
1166 * stands for a place that tokens must be separated or whitespace
1167 * is otherwise desirable, but where the ANSI standard specifies there
1168 * is no whitespace. This marker turns into a Space (or whichever other
1169 * whitespace char appears in the marker) in the final output,
1170 * but it turns into nothing in an argument that is stringified with #.
1171 * Such stringified arguments are the only place where the ANSI standard
1172 * specifies with precision that whitespace may not appear.
1174 * During this function, IP->bufp is kept cached in IBP for speed of access.
1175 * Likewise, OP->bufp is kept in OBP. Before calling a subroutine
1176 * IBP, IP and OBP must be copied back to memory. IP and IBP are
1177 * copied back with the RECACHE macro. OBP must be copied back from OP->bufp
1178 * explicitly, and before RECACHE, since RECACHE uses OBP.
1181 static void
1182 rescan (op, output_marks)
1183 FILE_BUF *op;
1184 int output_marks;
1186 /* Character being scanned in main loop. */
1187 U_CHAR c;
1189 /* Length of pending accumulated identifier. */
1190 int ident_length = 0;
1192 /* Hash code of pending accumulated identifier. */
1193 int hash = 0;
1195 /* Current input level (&instack[indepth]). */
1196 FILE_BUF *ip;
1198 /* Pointer for scanning input. */
1199 U_CHAR *ibp;
1201 /* Pointer to end of input. End of scan is controlled by LIMIT. */
1202 U_CHAR *limit;
1204 /* Pointer for storing output. */
1205 U_CHAR *obp;
1207 /* REDO_CHAR is nonzero if we are processing an identifier
1208 after backing up over the terminating character.
1209 Sometimes we process an identifier without backing up over
1210 the terminating character, if the terminating character
1211 is not special. Backing up is done so that the terminating character
1212 will be dispatched on again once the identifier is dealt with. */
1213 int redo_char = 0;
1215 /* 1 if within an identifier inside of which a concatenation
1216 marker (Newline -) has been seen. */
1217 int concatenated = 0;
1219 /* While scanning a comment or a string constant,
1220 this records the line it started on, for error messages. */
1221 int start_line;
1223 /* Record position of last `real' newline. */
1224 U_CHAR *beg_of_line;
1226 /* This has to be a global bacause of RECACHE. */
1227 U_CHAR *obufp_before_macroname = NULL;
1229 /* Pop the innermost input stack level, assuming it is a macro expansion. */
1231 #define POPMACRO \
1232 do { ip->macro->type = T_MACRO; \
1233 if (ip->free_ptr) free (ip->free_ptr); \
1234 --indepth; } while (0)
1236 /* Reload `rescan's local variables that describe the current
1237 level of the input stack. */
1239 #define RECACHE \
1240 do { ip = &instack[indepth]; \
1241 ibp = ip->bufp; \
1242 limit = ip->buf + ip->length; \
1243 op->bufp = obp; \
1244 check_expand (op, limit - ibp); \
1245 beg_of_line = 0; \
1246 obufp_before_macroname += op->bufp - obp; \
1247 obp = op->bufp; } while (0)
1249 if (no_output && instack[indepth].fname != 0)
1250 skip_if_group (&instack[indepth], 1);
1252 obp = op->bufp;
1253 RECACHE;
1254 beg_of_line = ibp;
1256 /* Our caller must always put a null after the end of
1257 the input at each input stack level. */
1258 if (*limit != 0)
1259 abort ();
1261 while (1) {
1262 c = *ibp++;
1263 *obp++ = c;
1265 switch (c) {
1266 case '\\':
1267 if (ibp >= limit)
1268 break;
1269 if (*ibp == '\n') {
1270 /* Always merge lines ending with backslash-newline,
1271 even in middle of identifier. */
1272 ++ibp;
1273 ++ip->lineno;
1274 --obp; /* remove backslash from obuf */
1275 break;
1277 /* Otherwise, backslash suppresses specialness of following char,
1278 so copy it here to prevent the switch from seeing it.
1279 But first get any pending identifier processed. */
1280 if (ident_length > 0)
1281 goto specialchar;
1282 *obp++ = *ibp++;
1283 break;
1285 case '#':
1286 /* If this is expanding a macro definition, don't recognize
1287 preprocessor directives. */
1288 if (ip->macro != 0)
1289 goto randomchar;
1290 if (ident_length)
1291 goto specialchar;
1293 /* # keyword: a # must be the first char on the line */
1294 if (beg_of_line == 0)
1295 goto randomchar;
1296 if (beg_of_line + 1 != ibp)
1297 goto randomchar;
1299 /* This # can start a directive. */
1301 --obp; /* Don't copy the '#' */
1303 ip->bufp = ibp;
1304 op->bufp = obp;
1305 if (! handle_directive (ip, op)) {
1306 #ifdef USE_C_ALLOCA
1307 alloca (0);
1308 #endif
1309 /* Not a known directive: treat it as ordinary text.
1310 IP, OP, IBP, etc. have not been changed. */
1311 if (no_output && instack[indepth].fname) {
1312 /* If not generating expanded output,
1313 what we do with ordinary text is skip it.
1314 Discard everything until next # directive. */
1315 skip_if_group (&instack[indepth], 1);
1316 RECACHE;
1317 beg_of_line = ibp;
1318 break;
1320 ++obp; /* Copy the '#' after all */
1321 goto randomchar;
1323 #ifdef USE_C_ALLOCA
1324 alloca (0);
1325 #endif
1326 /* A # directive has been successfully processed. */
1327 /* If not generating expanded output, ignore everything until
1328 next # directive. */
1329 if (no_output && instack[indepth].fname)
1330 skip_if_group (&instack[indepth], 1);
1331 obp = op->bufp;
1332 RECACHE;
1333 beg_of_line = ibp;
1334 break;
1336 case '\"': /* skip quoted string */
1337 case '\'':
1338 /* A single quoted string is treated like a double -- some
1339 programs (e.g., troff) are perverse this way */
1341 if (ident_length)
1342 goto specialchar;
1344 start_line = ip->lineno;
1346 /* Skip ahead to a matching quote. */
1348 while (1) {
1349 if (ibp >= limit) {
1350 if (ip->macro != 0) {
1351 /* try harder: this string crosses a macro expansion boundary */
1352 POPMACRO;
1353 RECACHE;
1354 continue;
1356 break;
1358 *obp++ = *ibp;
1359 switch (*ibp++) {
1360 case '\n':
1361 ++ip->lineno;
1362 ++op->lineno;
1363 /* Traditionally, end of line ends a string constant with no error.
1364 So exit the loop and record the new line. */
1365 beg_of_line = ibp;
1366 goto while2end;
1368 case '\\':
1369 if (ibp >= limit)
1370 break;
1371 if (*ibp == '\n') {
1372 /* Backslash newline is replaced by nothing at all,
1373 but keep the line counts correct. */
1374 --obp;
1375 ++ibp;
1376 ++ip->lineno;
1377 } else {
1378 /* ANSI stupidly requires that in \\ the second \
1379 is *not* prevented from combining with a newline. */
1380 while (*ibp == '\\' && ibp[1] == '\n') {
1381 ibp += 2;
1382 ++ip->lineno;
1384 *obp++ = *ibp++;
1386 break;
1388 case '\"':
1389 case '\'':
1390 if (ibp[-1] == c)
1391 goto while2end;
1392 break;
1395 while2end:
1396 break;
1398 case '/':
1399 if (*ibp == '\\' && ibp[1] == '\n')
1400 newline_fix (ibp);
1401 /* Don't look for comments inside a macro definition. */
1402 if (ip->macro != 0)
1403 goto randomchar;
1404 /* A comment constitutes white space, so it can terminate an identifier.
1405 Process the identifier, if any. */
1406 if (ident_length)
1407 goto specialchar;
1409 if (*ibp != '*')
1410 goto randomchar;
1412 /* We have a comment. Skip it, optionally copying it to output. */
1414 start_line = ip->lineno;
1416 ++ibp; /* Skip the star. */
1418 /* In K+R C, a comment is equivalent to nothing. Note that we
1419 already output the slash; we might not want it. */
1420 if (! put_out_comments)
1421 obp--;
1422 else
1423 *obp++ = '*';
1426 U_CHAR *before_bp = ibp;
1428 while (ibp < limit) {
1429 switch (*ibp++) {
1430 case '/':
1431 if (warn_comments && ibp < limit && *ibp == '*')
1432 warning("`/*' within comment");
1433 break;
1434 case '*':
1435 if (*ibp == '\\' && ibp[1] == '\n')
1436 newline_fix (ibp);
1437 if (ibp >= limit || *ibp == '/')
1438 goto comment_end;
1439 break;
1440 case '\n':
1441 ++ip->lineno;
1442 /* Copy the newline into the output buffer, in order to
1443 avoid the pain of a #line every time a multiline comment
1444 is seen. */
1445 if (!put_out_comments)
1446 *obp++ = '\n';
1447 ++op->lineno;
1450 comment_end:
1452 if (ibp >= limit)
1453 error_with_line (line_for_error (start_line),
1454 "unterminated comment");
1455 else {
1456 ibp++;
1457 if (put_out_comments) {
1458 memcpy (obp, before_bp, ibp - before_bp);
1459 obp += ibp - before_bp;
1463 break;
1465 case '0': case '1': case '2': case '3': case '4':
1466 case '5': case '6': case '7': case '8': case '9':
1467 /* If digit is not part of identifier, it starts a number,
1468 which means that following letters are not an identifier.
1469 "0x5" does not refer to an identifier "x5".
1470 So copy all alphanumerics that follow without accumulating
1471 as an identifier. Periods also, for sake of "3.e7". */
1473 if (ident_length == 0) {
1474 while (ibp < limit) {
1475 while (ibp < limit && ibp[0] == '\\' && ibp[1] == '\n') {
1476 ++ip->lineno;
1477 ibp += 2;
1479 c = *ibp++;
1480 if (! ISIDNUM (c) && c != '.') {
1481 --ibp;
1482 break;
1484 *obp++ = c;
1485 /* A sign can be part of a preprocessing number
1486 if it follows an e. */
1487 if (c == 'e' || c == 'E') {
1488 while (ibp < limit && ibp[0] == '\\' && ibp[1] == '\n') {
1489 ++ip->lineno;
1490 ibp += 2;
1492 if (ibp < limit && (*ibp == '+' || *ibp == '-')) {
1493 *obp++ = *ibp++;
1494 /* Traditional C does not let the token go past the sign. */
1495 break;
1499 break;
1501 /* fall through */
1503 case '_':
1504 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
1505 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
1506 case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
1507 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
1508 case 'y': case 'z':
1509 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
1510 case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
1511 case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
1512 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
1513 case 'Y': case 'Z':
1514 ident_length++;
1515 /* Compute step of hash function, to avoid a proc call on every token */
1516 hash = HASHSTEP (hash, c);
1517 break;
1519 case '\n':
1520 /* If reprocessing a macro expansion, newline is a special marker. */
1521 if (ip->macro != 0) {
1522 /* Newline White is a "funny space" to separate tokens that are
1523 supposed to be separate but without space between.
1524 Here White means any horizontal whitespace character.
1525 Newline - marks a recursive macro use that is not
1526 supposed to be expandable. */
1528 if (*ibp == '-') {
1529 /* Newline - inhibits expansion of preceding token.
1530 If expanding a macro arg, we keep the newline -.
1531 In final output, it is deleted. */
1532 if (! concatenated) {
1533 ident_length = 0;
1534 hash = 0;
1536 ibp++;
1537 if (!output_marks) {
1538 obp--;
1539 } else {
1540 /* If expanding a macro arg, keep the newline -. */
1541 *obp++ = '-';
1543 } else if (is_space (*ibp)) {
1544 /* Newline Space does not prevent expansion of preceding token
1545 so expand the preceding token and then come back. */
1546 if (ident_length > 0)
1547 goto specialchar;
1549 /* If generating final output, newline space makes a space. */
1550 if (!output_marks) {
1551 obp[-1] = *ibp++;
1552 /* And Newline Newline makes a newline, so count it. */
1553 if (obp[-1] == '\n')
1554 op->lineno++;
1555 } else {
1556 /* If expanding a macro arg, keep the newline space.
1557 If the arg gets stringified, newline space makes nothing. */
1558 *obp++ = *ibp++;
1560 } else abort (); /* Newline followed by something random? */
1561 break;
1564 /* If there is a pending identifier, handle it and come back here. */
1565 if (ident_length > 0)
1566 goto specialchar;
1568 beg_of_line = ibp;
1570 /* Update the line counts and output a #line if necessary. */
1571 ++ip->lineno;
1572 ++op->lineno;
1573 if (ip->lineno != op->lineno) {
1574 op->bufp = obp;
1575 output_line_command (ip, op, 1, same_file);
1576 check_expand (op, ip->length - (ip->bufp - ip->buf));
1577 obp = op->bufp;
1579 break;
1581 /* Come here either after (1) a null character that is part of the input
1582 or (2) at the end of the input, because there is a null there. */
1583 case 0:
1584 if (ibp <= limit)
1585 /* Our input really contains a null character. */
1586 goto randomchar;
1588 /* At end of a macro-expansion level, pop it and read next level. */
1589 if (ip->macro != 0) {
1590 obp--;
1591 ibp--;
1592 /* If we have an identifier that ends here, process it now, so
1593 we get the right error for recursion. */
1594 if (ident_length && ! is_idchar (*instack[indepth - 1].bufp)) {
1595 redo_char = 1;
1596 goto randomchar;
1598 POPMACRO;
1599 RECACHE;
1600 break;
1603 /* If we don't have a pending identifier,
1604 return at end of input. */
1605 if (ident_length == 0) {
1606 obp--;
1607 ibp--;
1608 op->bufp = obp;
1609 ip->bufp = ibp;
1610 goto ending;
1613 /* If we do have a pending identifier, just consider this null
1614 a special character and arrange to dispatch on it again.
1615 The second time, IDENT_LENGTH will be zero so we will return. */
1617 /* Fall through */
1619 specialchar:
1621 /* Handle the case of a character such as /, ', " or null
1622 seen following an identifier. Back over it so that
1623 after the identifier is processed the special char
1624 will be dispatched on again. */
1626 ibp--;
1627 obp--;
1628 redo_char = 1;
1630 default:
1632 randomchar:
1634 if (ident_length > 0) {
1635 HASHNODE *hp;
1637 /* We have just seen an identifier end. If it's a macro, expand it.
1639 IDENT_LENGTH is the length of the identifier
1640 and HASH is its hash code.
1642 The identifier has already been copied to the output,
1643 so if it is a macro we must remove it.
1645 If REDO_CHAR is 0, the char that terminated the identifier
1646 has been skipped in the output and the input.
1647 OBP-IDENT_LENGTH-1 points to the identifier.
1648 If the identifier is a macro, we must back over the terminator.
1650 If REDO_CHAR is 1, the terminating char has already been
1651 backed over. OBP-IDENT_LENGTH points to the identifier. */
1653 for (hp = hashtab[MAKE_POS (hash) % HASHSIZE]; hp != NULL;
1654 hp = hp->next) {
1656 if (hp->length == ident_length) {
1657 /* obufp_before_macroname is used only in this block,
1658 but it has to be global because of RECACHE. */
1659 int op_lineno_before_macroname;
1660 int i = ident_length;
1661 U_CHAR *p = hp->name;
1662 U_CHAR *q = obp - i;
1664 if (! redo_char)
1665 q--;
1667 do { /* All this to avoid a strncmp () */
1668 if (*p++ != *q++)
1669 goto hashcollision;
1670 } while (--i);
1672 /* We found a use of a macro name.
1673 see if the context shows it is a macro call. */
1675 /* Back up over terminating character if not already done. */
1676 if (! redo_char) {
1677 ibp--;
1678 obp--;
1681 obufp_before_macroname = obp - ident_length;
1682 op_lineno_before_macroname = op->lineno;
1684 /* If macro wants an arglist, verify that a '(' follows.
1685 first skip all whitespace, copying it to the output
1686 after the macro name. Then, if there is no '(',
1687 decide this is not a macro call and leave things that way. */
1688 if (hp->type == T_MACRO && hp->value.defn->nargs >= 0)
1690 while (1) {
1691 /* Scan forward over whitespace, copying it to the output. */
1692 if (ibp == limit && ip->macro != 0) {
1693 POPMACRO;
1694 RECACHE;
1696 /* A comment: copy it unchanged or discard it. */
1697 else if (*ibp == '/' && ibp+1 != limit && ibp[1] == '*') {
1698 if (put_out_comments) {
1699 *obp++ = '/';
1700 *obp++ = '*';
1702 ibp += 2;
1703 while (ibp + 1 != limit
1704 && !(ibp[0] == '*' && ibp[1] == '/')) {
1705 /* We need not worry about newline-marks,
1706 since they are never found in comments. */
1707 if (*ibp == '\n') {
1708 /* Newline in a file. Count it. */
1709 ++ip->lineno;
1710 ++op->lineno;
1712 if (put_out_comments)
1713 *obp++ = *ibp++;
1714 else
1715 ibp++;
1717 ibp += 2;
1718 if (put_out_comments) {
1719 *obp++ = '*';
1720 *obp++ = '/';
1723 else if (is_space (*ibp)) {
1724 *obp++ = *ibp++;
1725 if (ibp[-1] == '\n') {
1726 if (ip->macro == 0) {
1727 /* Newline in a file. Count it. */
1728 ++ip->lineno;
1729 ++op->lineno;
1730 } else if (!output_marks) {
1731 /* A newline mark, and we don't want marks
1732 in the output. If it is newline-hyphen,
1733 discard it entirely. Otherwise, it is
1734 newline-whitechar, so keep the whitechar. */
1735 obp--;
1736 if (*ibp == '-')
1737 ibp++;
1738 else {
1739 if (*ibp == '\n')
1740 ++op->lineno;
1741 *obp++ = *ibp++;
1743 } else {
1744 /* A newline mark; copy both chars to the output. */
1745 *obp++ = *ibp++;
1749 else break;
1751 if (*ibp != '(')
1752 break;
1755 /* This is now known to be a macro call.
1756 Discard the macro name from the output,
1757 along with any following whitespace just copied. */
1758 obp = obufp_before_macroname;
1759 op->lineno = op_lineno_before_macroname;
1761 /* Expand the macro, reading arguments as needed,
1762 and push the expansion on the input stack. */
1763 ip->bufp = ibp;
1764 op->bufp = obp;
1765 macroexpand (hp, op);
1767 /* Reexamine input stack, since macroexpand has pushed
1768 a new level on it. */
1769 obp = op->bufp;
1770 RECACHE;
1771 break;
1773 hashcollision:
1775 } /* End hash-table-search loop */
1776 ident_length = hash = 0; /* Stop collecting identifier */
1777 redo_char = 0;
1778 concatenated = 0;
1779 } /* End if (ident_length > 0) */
1780 } /* End switch */
1781 } /* End per-char loop */
1783 /* Come here to return -- but first give an error message
1784 if there was an unterminated successful conditional. */
1785 ending:
1786 if (if_stack != ip->if_stack) {
1787 const char *str;
1788 switch (if_stack->type) {
1789 case T_IF:
1790 str = "if";
1791 break;
1792 case T_IFDEF:
1793 str = "ifdef";
1794 break;
1795 case T_IFNDEF:
1796 str = "ifndef";
1797 break;
1798 case T_ELSE:
1799 str = "else";
1800 break;
1801 case T_ELIF:
1802 str = "elif";
1803 break;
1804 default:
1805 abort ();
1807 error_with_line (line_for_error (if_stack->lineno),
1808 "unterminated #%s conditional", str);
1810 if_stack = ip->if_stack;
1814 * Rescan a string into a temporary buffer and return the result
1815 * as a FILE_BUF. Note this function returns a struct, not a pointer.
1817 * OUTPUT_MARKS nonzero means keep Newline markers found in the input
1818 * and insert such markers when appropriate. See `rescan' for details.
1819 * OUTPUT_MARKS is 1 for macroexpanding a macro argument separately
1820 * before substitution; it is 0 for other uses.
1822 static FILE_BUF
1823 expand_to_temp_buffer (buf, limit, output_marks)
1824 const U_CHAR *buf, *limit;
1825 int output_marks;
1827 FILE_BUF *ip;
1828 FILE_BUF obuf;
1829 int length = limit - buf;
1830 U_CHAR *buf1;
1831 int odepth = indepth;
1833 if (length < 0)
1834 abort ();
1836 /* Set up the input on the input stack. */
1838 buf1 = (U_CHAR *) alloca (length + 1);
1840 const U_CHAR *p1 = buf;
1841 U_CHAR *p2 = buf1;
1843 while (p1 != limit)
1844 *p2++ = *p1++;
1846 buf1[length] = 0;
1848 /* Set up to receive the output. */
1850 obuf.length = length * 2 + 100; /* Usually enough. Why be stingy? */
1851 obuf.bufp = obuf.buf = (U_CHAR *) xmalloc (obuf.length);
1852 obuf.fname = 0;
1853 obuf.macro = 0;
1854 obuf.free_ptr = 0;
1856 CHECK_DEPTH ({return obuf;});
1858 ++indepth;
1860 ip = &instack[indepth];
1861 ip->fname = 0;
1862 ip->macro = 0;
1863 ip->free_ptr = 0;
1864 ip->length = length;
1865 ip->buf = ip->bufp = buf1;
1866 ip->if_stack = if_stack;
1868 ip->lineno = obuf.lineno = 1;
1870 /* Scan the input, create the output. */
1872 rescan (&obuf, output_marks);
1874 /* Pop input stack to original state. */
1875 --indepth;
1877 if (indepth != odepth)
1878 abort ();
1880 /* Record the output. */
1881 obuf.length = obuf.bufp - obuf.buf;
1883 return obuf;
1887 * Process a # directive. Expects IP->bufp to point to the '#', as in
1888 * `#define foo bar'. Passes to the command handler
1889 * (do_define, do_include, etc.): the addresses of the 1st and
1890 * last chars of the command (starting immediately after the #
1891 * keyword), plus op and the keyword table pointer. If the command
1892 * contains comments it is copied into a temporary buffer sans comments
1893 * and the temporary buffer is passed to the command handler instead.
1894 * Likewise for backslash-newlines.
1896 * Returns nonzero if this was a known # directive.
1897 * Otherwise, returns zero, without advancing the input pointer.
1900 static int
1901 handle_directive (ip, op)
1902 FILE_BUF *ip, *op;
1904 U_CHAR *bp, *cp;
1905 struct directive *kt;
1906 int ident_length;
1907 U_CHAR *resume_p;
1909 /* Nonzero means we must copy the entire command
1910 to get rid of comments or backslash-newlines. */
1911 int copy_command = 0;
1913 U_CHAR *ident, *after_ident;
1915 bp = ip->bufp;
1916 /* Skip whitespace and \-newline. */
1917 while (1) {
1918 if (is_nvspace (*bp))
1919 bp++;
1920 else if (*bp == '/' && (newline_fix (bp + 1), bp[1]) == '*') {
1921 ip->bufp = bp;
1922 skip_to_end_of_comment (ip, &ip->lineno);
1923 bp = ip->bufp;
1924 } else if (*bp == '\\' && bp[1] == '\n') {
1925 bp += 2; ip->lineno++;
1926 } else break;
1929 /* Now find end of directive name.
1930 If we encounter a backslash-newline, exchange it with any following
1931 symbol-constituents so that we end up with a contiguous name. */
1933 cp = bp;
1934 while (1) {
1935 if (is_idchar (*cp))
1936 cp++;
1937 else {
1938 if (*cp == '\\' && cp[1] == '\n')
1939 name_newline_fix (cp);
1940 if (is_idchar (*cp))
1941 cp++;
1942 else break;
1945 ident_length = cp - bp;
1946 ident = bp;
1947 after_ident = cp;
1949 /* A line of just `#' becomes blank. */
1951 if (ident_length == 0 && *after_ident == '\n') {
1952 ip->bufp = after_ident;
1953 return 1;
1957 * Decode the keyword and call the appropriate expansion
1958 * routine, after moving the input pointer up to the next line.
1960 for (kt = directive_table; kt->length > 0; kt++) {
1961 if (kt->length == ident_length
1962 && !strncmp (kt->name, (const char *)ident, ident_length)) {
1963 U_CHAR *buf;
1964 U_CHAR *limit = ip->buf + ip->length;
1965 int unterminated = 0;
1967 /* Nonzero means do not delete comments within the directive.
1968 #define needs this to detect traditional token paste. */
1969 int keep_comments = kt->type == T_DEFINE;
1971 /* Find the end of this command (first newline not backslashed
1972 and not in a string or comment).
1973 Set COPY_COMMAND if the command must be copied
1974 (it contains a backslash-newline or a comment). */
1976 buf = bp = after_ident;
1977 while (bp < limit) {
1978 U_CHAR c = *bp++;
1979 switch (c) {
1980 case '\\':
1981 if (bp < limit) {
1982 if (*bp == '\n') {
1983 ip->lineno++;
1984 copy_command = 1;
1986 bp++;
1988 break;
1990 case '\'':
1991 case '\"':
1992 bp = skip_quoted_string (bp - 1, limit, ip->lineno, &ip->lineno, &copy_command, &unterminated);
1993 if (unterminated) {
1994 /* Traditional preprocessing permits unterminated strings. */
1995 ip->bufp = bp;
1996 goto endloop1;
1998 break;
2000 /* <...> is special for #include. */
2001 case '<':
2002 if (kt->type != T_INCLUDE)
2003 break;
2004 while (*bp && *bp != '>') bp++;
2005 break;
2007 case '/':
2008 if (*bp == '\\' && bp[1] == '\n')
2009 newline_fix (bp);
2010 if (*bp == '*') {
2011 U_CHAR *obp = bp - 1;
2012 ip->bufp = bp + 1;
2013 skip_to_end_of_comment (ip, &ip->lineno);
2014 bp = ip->bufp;
2015 /* No need to copy the command because of a comment at the end;
2016 just don't include the comment in the directive. */
2017 if (bp == limit || *bp == '\n') {
2018 bp = obp;
2019 goto endloop1;
2021 /* Don't remove the comments if this is #define. */
2022 if (! keep_comments)
2023 copy_command++;
2025 break;
2027 case '\n':
2028 --bp; /* Point to the newline */
2029 ip->bufp = bp;
2030 goto endloop1;
2033 ip->bufp = bp;
2035 endloop1:
2036 resume_p = ip->bufp;
2037 /* BP is the end of the directive.
2038 RESUME_P is the next interesting data after the directive.
2039 A comment may come between. */
2041 if (copy_command) {
2042 U_CHAR *xp = buf;
2043 /* Need to copy entire command into temp buffer before dispatching */
2045 cp = (U_CHAR *) alloca (bp - buf + 5); /* room for cmd plus
2046 some slop */
2047 buf = cp;
2049 /* Copy to the new buffer, deleting comments
2050 and backslash-newlines (and whitespace surrounding the latter). */
2052 while (xp < bp) {
2053 U_CHAR c = *xp++;
2054 *cp++ = c;
2056 switch (c) {
2057 case '\n':
2058 break;
2060 /* <...> is special for #include. */
2061 case '<':
2062 if (kt->type != T_INCLUDE)
2063 break;
2064 while (xp < bp && c != '>') {
2065 c = *xp++;
2066 if (c == '\\' && xp < bp && *xp == '\n')
2067 xp++, ip->lineno++;
2068 else
2069 *cp++ = c;
2071 break;
2073 case '\\':
2074 if (*xp == '\n') {
2075 xp++;
2076 cp--;
2077 if (cp != buf && is_space (cp[-1])) {
2078 while (cp != buf && is_space(cp[-1])) cp--;
2079 cp++;
2080 SKIP_WHITE_SPACE (xp);
2081 } else if (is_nvspace (*xp)) {
2082 *cp++ = *xp++;
2083 SKIP_WHITE_SPACE (xp);
2085 } else {
2086 *cp++ = *xp++;
2088 break;
2090 case '\'':
2091 case '\"':
2093 const U_CHAR *bp1
2094 = skip_quoted_string (xp - 1, limit, ip->lineno, 0, 0, 0);
2095 while (xp != bp1)
2096 *cp++ = *xp++;
2098 break;
2100 case '/':
2101 if (*xp == '*') {
2102 ip->bufp = xp + 1;
2103 skip_to_end_of_comment (ip, 0);
2104 if (keep_comments)
2105 while (xp != ip->bufp)
2106 *cp++ = *xp++;
2107 /* Delete the slash. */
2108 else
2109 cp--;
2110 xp = ip->bufp;
2115 /* Null-terminate the copy. */
2117 *cp = 0;
2119 else
2120 cp = bp;
2122 ip->bufp = resume_p;
2124 /* Call the appropriate command handler. buf now points to
2125 either the appropriate place in the input buffer, or to
2126 the temp buffer if it was necessary to make one. cp
2127 points to the first char after the contents of the (possibly
2128 copied) command, in either case. */
2129 (*kt->func) (buf, cp, op);
2130 check_expand (op, ip->length - (ip->bufp - ip->buf));
2132 return 1;
2136 return 0;
2139 static const char *const
2140 monthnames[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
2141 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
2144 * expand things like __FILE__. Place the expansion into the output
2145 * buffer *without* rescanning.
2147 static void
2148 special_symbol (hp, op)
2149 HASHNODE *hp;
2150 FILE_BUF *op;
2152 const char *buf;
2153 time_t t;
2154 int i, len;
2155 int true_indepth;
2156 FILE_BUF *ip = NULL;
2157 static struct tm *timebuf = NULL;
2159 int paren = 0; /* For special `defined' keyword */
2161 for (i = indepth; i >= 0; i--)
2162 if (instack[i].fname != NULL) {
2163 ip = &instack[i];
2164 break;
2166 if (ip == NULL)
2167 fatal ("not in any file?!");
2169 switch (hp->type) {
2170 case T_FILE:
2171 case T_BASE_FILE:
2173 const char *string;
2174 if (hp->type == T_FILE)
2175 string = ip->fname;
2176 else
2177 string = instack[0].fname;
2179 if (string)
2181 char *tmp = (char *) alloca (3 + strlen (string));
2182 sprintf (tmp, "\"%s\"", string);
2183 buf = tmp;
2185 else
2186 buf = "";
2188 break;
2191 case T_INCLUDE_LEVEL:
2193 char *tmp = (char *) alloca (8); /* Eigth bytes ought to be more than enough */
2194 true_indepth = 0;
2195 for (i = indepth; i >= 0; i--)
2196 if (instack[i].fname != NULL)
2197 true_indepth++;
2199 sprintf (tmp, "%d", true_indepth - 1);
2200 buf = tmp;
2201 break;
2204 case T_VERSION:
2206 char *tmp = (char *) alloca (3 + strlen (version_string));
2207 sprintf (tmp, "\"%s\"", version_string);
2208 buf = tmp;
2209 break;
2212 case T_CONST:
2213 buf = hp->value.cpval;
2214 break;
2216 case T_SPECLINE:
2218 char *tmp = (char *) alloca (10);
2219 sprintf (tmp, "%d", ip->lineno);
2220 buf = tmp;
2221 break;
2224 case T_DATE:
2225 case T_TIME:
2227 char *tmp = (char *) alloca (20);
2229 if (timebuf == NULL) {
2230 t = time (0);
2231 timebuf = localtime (&t);
2233 if (hp->type == T_DATE)
2234 sprintf (tmp, "\"%s %2d %4d\"", monthnames[timebuf->tm_mon],
2235 timebuf->tm_mday, timebuf->tm_year + 1900);
2236 else
2237 sprintf (tmp, "\"%02d:%02d:%02d\"", timebuf->tm_hour, timebuf->tm_min,
2238 timebuf->tm_sec);
2239 buf = tmp;
2240 break;
2243 case T_SPEC_DEFINED:
2244 buf = " 0 "; /* Assume symbol is not defined */
2245 ip = &instack[indepth];
2246 SKIP_WHITE_SPACE (ip->bufp);
2247 if (*ip->bufp == '(') {
2248 paren++;
2249 ip->bufp++; /* Skip over the paren */
2250 SKIP_WHITE_SPACE (ip->bufp);
2253 if (!is_idstart (*ip->bufp))
2254 goto oops;
2256 HASHNODE *hp = lookup (ip->bufp, -1, -1);
2258 if (hp && hp->type != T_UNUSED && hp->type != T_SPEC_DEFINED)
2259 buf = " 1 ";
2261 while (is_idchar (*ip->bufp))
2262 ++ip->bufp;
2263 SKIP_WHITE_SPACE (ip->bufp);
2264 if (paren) {
2265 if (*ip->bufp != ')')
2266 goto oops;
2267 ++ip->bufp;
2269 break;
2271 oops:
2273 error ("`defined' must be followed by ident or (ident)");
2274 break;
2276 default:
2277 error ("cccp error: invalid special hash type"); /* time for gdb */
2278 abort ();
2280 len = strlen (buf);
2281 check_expand (op, len);
2282 memcpy (op->bufp, buf, len);
2283 op->bufp += len;
2287 /* Routines to handle #directives */
2290 * Process include file by reading it in and calling rescan.
2291 * Expects to see "fname" or <fname> on the input.
2293 static void
2294 do_include (buf, limit, op)
2295 U_CHAR *buf, *limit;
2296 FILE_BUF *op;
2298 U_CHAR *fbeg, *fend; /* Beginning and end of fname */
2300 struct file_name_list *stackp = include; /* Chain of dirs to search */
2301 struct file_name_list dsp[1]; /* First in chain, if #include "..." */
2302 int flen;
2304 int retried = 0; /* Have already tried macro
2305 expanding the include line*/
2306 FILE_BUF trybuf; /* It got expanded into here */
2307 int system_header_p = 0; /* 0 for "...", 1 for <...> */
2309 get_filename:
2311 fbeg = buf;
2312 SKIP_WHITE_SPACE (fbeg);
2313 /* Discard trailing whitespace so we can easily see
2314 if we have parsed all the significant chars we were given. */
2315 while (limit != fbeg && is_nvspace (limit[-1])) limit--;
2317 switch (*fbeg++) {
2318 case '\"':
2319 fend = fbeg;
2320 while (fend != limit && *fend != '\"')
2321 fend++;
2322 if (*fend == '\"' && fend + 1 == limit) {
2323 FILE_BUF *fp;
2325 /* We have "filename". Figure out directory this source
2326 file is coming from and put it on the front of the list. */
2328 /* If -I- was specified, don't search current dir, only spec'd ones. */
2329 if (ignore_srcdir) break;
2331 for (fp = &instack[indepth]; fp >= instack; fp--)
2333 size_t n;
2334 const char *ep, *nam;
2336 if ((nam = fp->fname) != NULL) {
2337 /* Found a named file. Figure out dir of the file,
2338 and put it in front of the search list. */
2339 dsp[0].next = stackp;
2340 stackp = dsp;
2341 ep = strrchr (nam, '/');
2342 if (ep != NULL) {
2343 char *f;
2344 n = ep - nam;
2345 f = (char *) alloca (n + 1);
2346 strncpy (f, nam, n);
2347 f[n] = '\0';
2348 dsp[0].fname = f;
2349 if (n > max_include_len) max_include_len = n;
2350 } else {
2351 dsp[0].fname = 0; /* Current directory */
2353 break;
2356 break;
2358 goto fail;
2360 case '<':
2361 fend = fbeg;
2362 while (fend != limit && *fend != '>') fend++;
2363 if (*fend == '>' && fend + 1 == limit) {
2364 system_header_p = 1;
2365 /* If -I-, start with the first -I dir after the -I-. */
2366 if (first_bracket_include)
2367 stackp = first_bracket_include;
2368 break;
2370 goto fail;
2372 default:
2373 fail:
2374 if (retried) {
2375 error ("#include expects \"fname\" or <fname>");
2376 return;
2377 } else {
2378 trybuf = expand_to_temp_buffer (buf, limit, 0);
2379 buf = (U_CHAR *) alloca (trybuf.bufp - trybuf.buf + 1);
2380 memcpy (buf, trybuf.buf, trybuf.bufp - trybuf.buf);
2381 limit = buf + (trybuf.bufp - trybuf.buf);
2382 free (trybuf.buf);
2383 retried++;
2384 goto get_filename;
2388 flen = fend - fbeg;
2389 process_include (stackp, fbeg, flen, system_header_p, op);
2392 static void
2393 do_include_next (buf, limit, op)
2394 U_CHAR *buf, *limit;
2395 FILE_BUF *op;
2397 U_CHAR *fbeg, *fend; /* Beginning and end of fname */
2399 struct file_name_list *stackp; /* Chain of dirs to search */
2400 int flen;
2402 int retried = 0; /* Have already tried macro
2403 expanding the include line*/
2404 FILE_BUF trybuf; /* It got expanded into here */
2405 int system_header_p = 0; /* 0 for "...", 1 for <...> */
2407 /* Treat as plain #include if we don't know where to start
2408 looking. */
2409 stackp = instack[indepth].next_header_dir;
2410 if (stackp == 0)
2412 do_include (buf, limit, op);
2413 return;
2416 get_filename:
2418 fbeg = buf;
2419 SKIP_WHITE_SPACE (fbeg);
2420 /* Discard trailing whitespace so we can easily see
2421 if we have parsed all the significant chars we were given. */
2422 while (limit != fbeg && is_nvspace (limit[-1])) limit--;
2424 switch (*fbeg++) {
2425 case '\"':
2426 fend = fbeg;
2427 while (fend != limit && *fend != '\"')
2428 fend++;
2429 if (*fend == '\"' && fend + 1 == limit)
2430 break;
2431 goto fail;
2433 case '<':
2434 fend = fbeg;
2435 while (fend != limit && *fend != '>') fend++;
2436 if (*fend == '>' && fend + 1 == limit) {
2437 system_header_p = 1;
2438 break;
2440 goto fail;
2442 default:
2443 fail:
2444 if (retried) {
2445 error ("#include expects \"fname\" or <fname>");
2446 return;
2447 } else {
2448 trybuf = expand_to_temp_buffer (buf, limit, 0);
2449 buf = (U_CHAR *) alloca (trybuf.bufp - trybuf.buf + 1);
2450 memcpy (buf, trybuf.buf, trybuf.bufp - trybuf.buf);
2451 limit = buf + (trybuf.bufp - trybuf.buf);
2452 free (trybuf.buf);
2453 retried++;
2454 goto get_filename;
2458 flen = fend - fbeg;
2459 process_include (stackp, fbeg, flen, system_header_p, op);
2462 static void
2463 process_include (stackp, fbeg, flen, system_header_p, op)
2464 struct file_name_list *stackp;
2465 const U_CHAR *fbeg;
2466 int flen;
2467 int system_header_p;
2468 FILE_BUF *op;
2470 char *fname;
2471 int f = -1; /* file number */
2473 fname = (char *) alloca (max_include_len + flen + 2);
2474 /* + 2 above for slash and terminating null. */
2476 /* If specified file name is absolute, just open it. */
2478 if (IS_ABSOLUTE_PATHNAME (fbeg)) {
2479 strncpy (fname, (const char *)fbeg, flen);
2480 fname[flen] = 0;
2481 f = open (fname, O_RDONLY, 0666);
2482 } else {
2483 /* Search directory path, trying to open the file.
2484 Copy each filename tried into FNAME. */
2486 for (; stackp; stackp = stackp->next) {
2487 if (stackp->fname) {
2488 strcpy (fname, stackp->fname);
2489 strcat (fname, "/");
2490 fname[strlen (fname) + flen] = 0;
2491 } else {
2492 fname[0] = 0;
2494 strncat (fname, (const char *)fbeg, flen);
2495 if ((f = open (fname, O_RDONLY, 0666)) >= 0)
2496 break;
2500 if (f < 0) {
2501 strncpy (fname, (const char *)fbeg, flen);
2502 fname[flen] = 0;
2503 if (deps_missing_files
2504 && print_deps > (system_header_p || (system_include_depth > 0))) {
2506 /* If requested as a system header, assume it belongs in
2507 the first system header directory. */
2508 if (first_bracket_include)
2509 stackp = first_bracket_include;
2510 else
2511 stackp = include;
2513 if (!system_header_p || IS_ABSOLUTE_PATHNAME (fbeg) || !stackp->fname)
2514 deps_add_dep (deps, fname);
2515 else {
2516 char *p;
2517 int len = strlen(stackp->fname);
2519 p = (char *) alloca (len + flen + 2);
2520 memcpy (p, stackp->fname, len);
2521 p[len++] = '/';
2522 memcpy (p + len, fbeg, flen);
2523 len += flen;
2524 p[len] = '\0';
2525 deps_add_dep (deps, p);
2527 } else if (print_deps
2528 && print_deps <= (system_header_p
2529 || (system_include_depth > 0)))
2530 warning ("No include path in which to find %.*s", flen, fbeg);
2531 else
2532 error_from_errno (fname);
2534 } else {
2536 /* Check to see if this include file is a once-only include file.
2537 If so, give up. */
2539 struct file_name_list* ptr;
2541 for (ptr = dont_repeat_files; ptr; ptr = ptr->next) {
2542 if (!strcmp (ptr->fname, fname)) {
2543 close (f);
2544 return; /* This file was once'd. */
2548 for (ptr = all_include_files; ptr; ptr = ptr->next) {
2549 if (!strcmp (ptr->fname, fname))
2550 break; /* This file was included before. */
2553 if (ptr == 0) {
2554 /* This is the first time for this file. */
2555 /* Add it to list of files included. */
2557 ptr = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
2558 ptr->next = all_include_files;
2559 all_include_files = ptr;
2560 ptr->fname = xstrdup (fname);
2562 /* For -M, add this file to the dependencies. */
2563 if (print_deps > (system_header_p || (system_include_depth > 0)))
2564 deps_add_dep (deps, fname);
2567 if (system_header_p)
2568 system_include_depth++;
2570 /* Actually process the file. */
2571 finclude (f, fname, stackp->next, op);
2573 if (system_header_p)
2574 system_include_depth--;
2576 close (f);
2580 /* Process the contents of include file FNAME, already open on descriptor F,
2581 with output to OP. */
2583 static void
2584 finclude (f, fname, nhd, op)
2585 int f;
2586 const char *fname;
2587 struct file_name_list *nhd;
2588 FILE_BUF *op;
2590 int st_mode;
2591 long st_size;
2592 long i;
2593 FILE_BUF *fp; /* For input stack frame */
2595 CHECK_DEPTH (return;);
2597 if (file_size_and_mode (f, &st_mode, &st_size))
2598 goto nope;
2600 fp = &instack[indepth + 1];
2601 memset (fp, 0, sizeof (FILE_BUF));
2602 fp->fname = fname;
2603 fp->length = 0;
2604 fp->lineno = 1;
2605 fp->if_stack = if_stack;
2606 fp->next_header_dir = nhd;
2608 if (S_ISREG (st_mode)) {
2609 fp->buf = (U_CHAR *) xmalloc (st_size + 2);
2610 fp->bufp = fp->buf;
2612 /* Read the file contents, knowing that st_size is an upper bound
2613 on the number of bytes we can read. */
2614 while (st_size > 0) {
2615 i = read (f, fp->buf + fp->length, st_size);
2616 if (i <= 0) {
2617 if (i == 0) break;
2618 goto nope;
2620 fp->length += i;
2621 st_size -= i;
2624 else {
2625 /* Cannot count its file size before reading. */
2627 U_CHAR *bufp;
2628 U_CHAR *basep;
2629 int bsize = 2000;
2631 st_size = 0;
2632 basep = (U_CHAR *) xmalloc (bsize + 2);
2633 bufp = basep;
2635 for (;;) {
2636 i = read (f, bufp, bsize - st_size);
2637 if (i < 0)
2638 goto nope; /* error! */
2639 if (i == 0)
2640 break; /* End of file */
2641 st_size += i;
2642 bufp += i;
2643 if (bsize == st_size) { /* Buffer is full! */
2644 bsize *= 2;
2645 basep = (U_CHAR *) xrealloc (basep, bsize + 2);
2646 bufp = basep + st_size; /* May have moved */
2649 fp->buf = basep;
2650 fp->bufp = fp->buf;
2651 fp->length = st_size;
2653 close (f);
2655 /* Make sure data ends with a newline. And put a null after it. */
2657 if (fp->length > 0 && fp->buf[fp->length-1] != '\n')
2658 fp->buf[fp->length++] = '\n';
2659 fp->buf[fp->length] = '\0';
2661 indepth++;
2662 output_line_command (fp, op, 0, enter_file);
2663 rescan (op, 0);
2664 indepth--;
2665 instack[indepth].lineno++;
2666 instack[indepth].bufp++; /* Skip the new line. */
2667 output_line_command (&instack[indepth], op, 0, leave_file);
2668 free (fp->buf);
2669 return;
2671 nope:
2672 perror_with_name (fname);
2673 close (f);
2677 /* Process a #define command.
2678 BUF points to the contents of the #define command, as a continguous string.
2679 LIMIT points to the first character past the end of the definition.
2680 KEYWORD is the keyword-table entry for #define. */
2682 static void
2683 do_define (buf, limit, op)
2684 U_CHAR *buf, *limit;
2685 FILE_BUF *op ATTRIBUTE_UNUSED;
2687 U_CHAR *bp; /* temp ptr into input buffer */
2688 U_CHAR *symname; /* remember where symbol name starts */
2689 int sym_length; /* and how long it is */
2691 DEFINITION *defn;
2692 int arglengths = 0; /* Accumulate lengths of arg names
2693 plus number of args. */
2694 int hashcode;
2696 bp = buf;
2698 while (is_nvspace (*bp))
2699 bp++;
2701 symname = bp; /* remember where it starts */
2702 while (is_idchar (*bp) && bp < limit) {
2703 bp++;
2705 sym_length = bp - symname;
2706 if (sym_length == 0)
2708 error ("invalid macro name");
2709 return;
2711 else if (!is_idstart (*symname)) {
2712 U_CHAR *msg; /* what pain... */
2713 msg = (U_CHAR *) alloca (sym_length + 1);
2714 memcpy (msg, symname, sym_length);
2715 msg[sym_length] = 0;
2716 error ("invalid macro name `%s'", msg);
2717 return;
2718 } else {
2719 if (! strncmp ((const char *)symname, "defined", 7) && sym_length == 7)
2721 error ("\"defined\" cannot be used as a macro name");
2722 return;
2726 /* lossage will occur if identifiers or control keywords are broken
2727 across lines using backslash. This is not the right place to take
2728 care of that. */
2730 if (*bp == '(') {
2731 struct arglist *arg_ptrs = NULL;
2732 int argno = 0;
2734 bp++; /* skip '(' */
2735 SKIP_WHITE_SPACE (bp);
2737 /* Loop over macro argument names. */
2738 while (*bp != ')') {
2739 struct arglist *temp;
2741 temp = (struct arglist *) alloca (sizeof (struct arglist));
2742 temp->name = bp;
2743 temp->next = arg_ptrs;
2744 temp->argno = argno++;
2745 arg_ptrs = temp;
2747 if (!is_idstart (*bp))
2748 warning ("parameter name starts with a digit in #define");
2750 /* Find the end of the arg name. */
2751 while (is_idchar (*bp)) {
2752 bp++;
2754 temp->length = bp - temp->name;
2755 arglengths += temp->length + 2;
2756 SKIP_WHITE_SPACE (bp);
2757 if (temp->length == 0 || (*bp != ',' && *bp != ')')) {
2758 error ("badly punctuated parameter list in #define");
2759 return;
2761 if (*bp == ',') {
2762 bp++;
2763 SKIP_WHITE_SPACE (bp);
2765 if (bp >= limit) {
2766 error ("unterminated parameter list in #define");
2767 return;
2771 ++bp; /* skip paren */
2772 while (is_nvspace (*bp) && bp < limit) /* and leading whitespace */
2773 ++bp;
2774 /* now everything from bp before limit is the definition. */
2775 defn = collect_expansion (bp, limit, argno, arg_ptrs);
2777 /* Now set defn->argnames to the result of concatenating
2778 the argument names in reverse order
2779 with comma-space between them. */
2781 struct arglist *temp;
2782 int i = 0;
2783 U_CHAR *tmp = (U_CHAR *) xmalloc (arglengths + 1);
2785 for (temp = arg_ptrs; temp; temp = temp->next) {
2786 memcpy (&tmp[i], temp->name, temp->length);
2787 i += temp->length;
2788 if (temp->next != 0) {
2789 tmp[i++] = ',';
2790 tmp[i++] = ' ';
2793 tmp[i] = 0;
2794 defn->argnames = tmp;
2797 } else {
2798 /* simple expansion or empty definition; skip leading whitespace */
2799 while (is_nvspace (*bp) && bp < limit)
2800 ++bp;
2801 /* now everything from bp before limit is the definition. */
2802 defn = collect_expansion (bp, limit, -1, 0);
2803 defn->argnames = (const U_CHAR *) "";
2806 hashcode = hashf (symname, sym_length, HASHSIZE);
2809 HASHNODE *hp;
2810 if ((hp = lookup (symname, sym_length, hashcode)) == NULL)
2811 hp = install (symname, sym_length, T_MACRO, hashcode);
2812 else {
2813 if (hp->type != T_MACRO || compare_defs (defn, hp->value.defn))
2814 warning ("\"%.*s\" redefined", sym_length, symname);
2816 /* Replace the old definition. */
2817 hp->type = T_MACRO;
2820 hp->value.defn = defn;
2825 * return zero if two DEFINITIONs are isomorphic
2827 static int
2828 compare_defs (d1, d2)
2829 DEFINITION *d1, *d2;
2831 struct reflist *a1, *a2;
2832 U_CHAR *p1 = d1->expansion;
2833 U_CHAR *p2 = d2->expansion;
2834 int first = 1;
2836 if (d1->nargs != d2->nargs)
2837 return 1;
2838 if (strcmp ((const char *)d1->argnames, (const char *)d2->argnames))
2839 return 1;
2840 for (a1 = d1->pattern, a2 = d2->pattern; a1 && a2;
2841 a1 = a1->next, a2 = a2->next) {
2842 if (!((a1->nchars == a2->nchars
2843 && ! strncmp ((const char *)p1, (const char *)p2, a1->nchars))
2844 || ! comp_def_part (first, p1, a1->nchars, p2, a2->nchars, 0))
2845 || a1->argno != a2->argno
2846 || a1->stringify != a2->stringify
2847 || a1->raw_before != a2->raw_before
2848 || a1->raw_after != a2->raw_after)
2849 return 1;
2850 first = 0;
2851 p1 += a1->nchars;
2852 p2 += a2->nchars;
2854 if (a1 != a2)
2855 return 1;
2856 if (comp_def_part (first, p1, d1->length - (p1 - d1->expansion),
2857 p2, d2->length - (p2 - d2->expansion), 1))
2858 return 1;
2859 return 0;
2862 /* Return 1 if two parts of two macro definitions are effectively different.
2863 One of the parts starts at BEG1 and has LEN1 chars;
2864 the other has LEN2 chars at BEG2.
2865 Any sequence of whitespace matches any other sequence of whitespace.
2866 FIRST means these parts are the first of a macro definition;
2867 so ignore leading whitespace entirely.
2868 LAST means these parts are the last of a macro definition;
2869 so ignore trailing whitespace entirely. */
2870 static int
2871 comp_def_part (first, beg1, len1, beg2, len2, last)
2872 int first;
2873 const U_CHAR *beg1, *beg2;
2874 int len1, len2;
2875 int last;
2877 const U_CHAR *end1 = beg1 + len1;
2878 const U_CHAR *end2 = beg2 + len2;
2879 if (first) {
2880 while (beg1 != end1 && is_space (*beg1)) beg1++;
2881 while (beg2 != end2 && is_space (*beg2)) beg2++;
2883 if (last) {
2884 while (beg1 != end1 && is_space (end1[-1])) end1--;
2885 while (beg2 != end2 && is_space (end2[-1])) end2--;
2887 while (beg1 != end1 && beg2 != end2) {
2888 if (is_space (*beg1) && is_space (*beg2)) {
2889 while (beg1 != end1 && is_space (*beg1)) beg1++;
2890 while (beg2 != end2 && is_space (*beg2)) beg2++;
2891 } else if (*beg1 == *beg2) {
2892 beg1++; beg2++;
2893 } else break;
2895 return (beg1 != end1) || (beg2 != end2);
2898 /* Read a replacement list for a macro with parameters.
2899 Build the DEFINITION structure.
2900 Reads characters of text starting at BUF until LIMIT.
2901 ARGLIST specifies the formal parameters to look for
2902 in the text of the definition; NARGS is the number of args
2903 in that list, or -1 for a macro name that wants no argument list.
2904 MACRONAME is the macro name itself (so we can avoid recursive expansion)
2905 and NAMELEN is its length in characters.
2907 Note that comments and backslash-newlines have already been deleted
2908 from the argument. */
2910 /* Leading and trailing Space, Tab, etc. are converted to markers
2911 Newline Space, Newline Tab, etc.
2912 Newline Space makes a space in the final output
2913 but is discarded if stringified. (Newline Tab is similar but
2914 makes a Tab instead.)
2916 If there is no trailing whitespace, a Newline Space is added at the end
2917 to prevent concatenation that would be contrary to the standard. */
2919 static DEFINITION *
2920 collect_expansion (buf, end, nargs, arglist)
2921 U_CHAR *buf, *end;
2922 int nargs;
2923 struct arglist *arglist;
2925 DEFINITION *defn;
2926 U_CHAR *p, *limit, *lastp, *exp_p;
2927 struct reflist *endpat = NULL;
2928 /* Pointer to first nonspace after last ## seen. */
2929 U_CHAR *concat = 0;
2930 /* Pointer to first nonspace after last single-# seen. */
2931 U_CHAR *stringify = 0;
2932 int maxsize;
2933 int expected_delimiter = '\0';
2935 /* Scan thru the replacement list, ignoring comments and quoted
2936 strings, picking up on the macro calls. It does a linear search
2937 thru the arg list on every potential symbol. Profiling might say
2938 that something smarter should happen. */
2940 if (end < buf)
2941 abort ();
2943 /* Find the beginning of the trailing whitespace. */
2944 /* Find end of leading whitespace. */
2945 limit = end;
2946 p = buf;
2947 while (p < limit && is_space (limit[-1])) limit--;
2948 while (p < limit && is_space (*p)) p++;
2950 /* Allocate space for the text in the macro definition.
2951 Leading and trailing whitespace chars need 2 bytes each.
2952 Each other input char may or may not need 1 byte,
2953 so this is an upper bound.
2954 The extra 2 are for invented trailing newline-marker and final null. */
2955 maxsize = (sizeof (DEFINITION)
2956 + 2 * (end - limit) + 2 * (p - buf)
2957 + (limit - p) + 3);
2958 defn = (DEFINITION *) xcalloc (1, maxsize);
2960 defn->nargs = nargs;
2961 exp_p = defn->expansion = (U_CHAR *) defn + sizeof (DEFINITION);
2962 lastp = exp_p;
2964 p = buf;
2966 /* Convert leading whitespace to Newline-markers. */
2967 while (p < limit && is_space (*p)) {
2968 *exp_p++ = '\n';
2969 *exp_p++ = *p++;
2972 /* Process the main body of the definition. */
2973 while (p < limit) {
2974 int skipped_arg = 0;
2975 U_CHAR c = *p++;
2977 *exp_p++ = c;
2979 /* In -traditional mode, recognize arguments inside strings and
2980 and character constants, and ignore special properties of #.
2981 Arguments inside strings are considered "stringified", but no
2982 extra quote marks are supplied. */
2983 switch (c) {
2984 case '\'':
2985 case '\"':
2986 if (expected_delimiter != '\0') {
2987 if (c == expected_delimiter)
2988 expected_delimiter = '\0';
2989 } else
2990 expected_delimiter = c;
2991 break;
2993 case '\\':
2994 /* Backslash quotes delimiters and itself, but not macro args. */
2995 if (expected_delimiter != 0 && p < limit
2996 && (*p == expected_delimiter || *p == '\\')) {
2997 *exp_p++ = *p++;
2998 continue;
3000 break;
3002 case '/':
3003 if (expected_delimiter != '\0') /* No comments inside strings. */
3004 break;
3005 if (*p == '*') {
3006 /* If we find a comment that wasn't removed by handle_directive,
3007 this must be -traditional. So replace the comment with
3008 nothing at all. */
3009 exp_p--;
3010 p += 1;
3011 while (p < limit && !(p[-2] == '*' && p[-1] == '/'))
3012 p++;
3014 break;
3017 if (is_idchar (c) && nargs > 0) {
3018 U_CHAR *id_beg = p - 1;
3019 int id_len;
3021 --exp_p;
3022 while (p != limit && is_idchar (*p)) p++;
3023 id_len = p - id_beg;
3025 if (is_idstart (c)) {
3026 struct arglist *arg;
3028 for (arg = arglist; arg != NULL; arg = arg->next) {
3029 struct reflist *tpat;
3031 if (arg->name[0] == c
3032 && arg->length == id_len
3033 && strncmp ((const char *)arg->name,
3034 (const char *)id_beg, id_len) == 0) {
3035 /* make a pat node for this arg and append it to the end of
3036 the pat list */
3037 tpat = (struct reflist *) xmalloc (sizeof (struct reflist));
3038 tpat->next = NULL;
3039 tpat->raw_before = concat == id_beg;
3040 tpat->raw_after = 0;
3041 tpat->stringify = expected_delimiter != '\0';
3043 if (endpat == NULL)
3044 defn->pattern = tpat;
3045 else
3046 endpat->next = tpat;
3047 endpat = tpat;
3049 tpat->argno = arg->argno;
3050 tpat->nchars = exp_p - lastp;
3052 U_CHAR *p1 = p;
3053 SKIP_WHITE_SPACE (p1);
3054 if (p1 + 2 <= limit && p1[0] == '#' && p1[1] == '#')
3055 tpat->raw_after = 1;
3057 lastp = exp_p; /* place to start copying from next time */
3058 skipped_arg = 1;
3059 break;
3064 /* If this was not a macro arg, copy it into the expansion. */
3065 if (! skipped_arg) {
3066 U_CHAR *lim1 = p;
3067 p = id_beg;
3068 while (p != lim1)
3069 *exp_p++ = *p++;
3070 if (stringify == id_beg)
3071 error ("# operator should be followed by a macro argument name");
3076 if (limit < end) {
3077 /* Convert trailing whitespace to Newline-markers. */
3078 while (limit < end && is_space (*limit)) {
3079 *exp_p++ = '\n';
3080 *exp_p++ = *limit++;
3083 *exp_p = '\0';
3085 defn->length = exp_p - defn->expansion;
3087 /* Crash now if we overrun the allocated size. */
3088 if (defn->length + 1 > maxsize)
3089 abort ();
3091 return defn;
3095 * interpret #line command. Remembers previously seen fnames
3096 * in its very own hash table.
3098 #define FNAME_HASHSIZE 37
3099 static void
3100 do_line (buf, limit, op)
3101 U_CHAR *buf, *limit;
3102 FILE_BUF *op;
3104 U_CHAR *bp;
3105 FILE_BUF *ip = &instack[indepth];
3106 FILE_BUF tem;
3107 int new_lineno;
3108 enum file_change_code file_change = same_file;
3110 /* Expand any macros. */
3111 tem = expand_to_temp_buffer (buf, limit, 0);
3113 /* Point to macroexpanded line, which is null-terminated now. */
3114 bp = tem.buf;
3115 SKIP_WHITE_SPACE (bp);
3117 if (!ISDIGIT (*bp)) {
3118 error ("invalid format #line command");
3119 return;
3122 /* The Newline at the end of this line remains to be processed.
3123 To put the next line at the specified line number,
3124 we must store a line number now that is one less. */
3125 new_lineno = atoi ((const char *)bp);
3127 /* skip over the line number. */
3128 while (ISDIGIT (*bp))
3129 bp++;
3131 SKIP_WHITE_SPACE (bp);
3133 if (*bp == '\"') {
3134 static HASHNODE *fname_table[FNAME_HASHSIZE];
3135 HASHNODE *hp, **hash_bucket;
3136 U_CHAR *fname;
3137 int fname_length;
3139 fname = ++bp;
3141 while (*bp && *bp != '\"')
3142 bp++;
3143 if (*bp != '\"') {
3144 error ("invalid format #line command");
3145 return;
3148 fname_length = bp - fname;
3150 bp++;
3151 SKIP_WHITE_SPACE (bp);
3152 if (*bp) {
3153 if (*bp == '1')
3154 file_change = enter_file;
3155 else if (*bp == '2')
3156 file_change = leave_file;
3157 else {
3158 error ("invalid format #line command");
3159 return;
3162 bp++;
3163 SKIP_WHITE_SPACE (bp);
3164 if (*bp) {
3165 error ("invalid format #line command");
3166 return;
3170 hash_bucket =
3171 &fname_table[hashf (fname, fname_length, FNAME_HASHSIZE)];
3172 for (hp = *hash_bucket; hp != NULL; hp = hp->next)
3173 if (hp->length == fname_length &&
3174 strncmp (hp->value.cpval, (const char *)fname, fname_length) == 0) {
3175 ip->fname = hp->value.cpval;
3176 break;
3178 if (hp == 0) {
3179 char *q;
3180 /* Didn't find it; cons up a new one. */
3181 hp = (HASHNODE *) xcalloc (1, sizeof (HASHNODE) + fname_length + 1);
3182 hp->next = *hash_bucket;
3183 *hash_bucket = hp;
3185 hp->length = fname_length;
3186 ip->fname = hp->value.cpval = q = ((char *) hp) + sizeof (HASHNODE);
3187 memcpy (q, fname, fname_length);
3189 } else if (*bp) {
3190 error ("invalid format #line command");
3191 return;
3194 ip->lineno = new_lineno;
3195 output_line_command (ip, op, 0, file_change);
3196 ip->bufp++; /* Skip the new line. */
3197 check_expand (op, ip->length - (ip->bufp - ip->buf));
3201 * remove all definitions of symbol from symbol table.
3202 * according to un*x /lib/cpp, it is not an error to undef
3203 * something that has no definitions, so it isn't one here either.
3205 static void
3206 do_undef (buf, limit, op)
3207 U_CHAR *buf;
3208 U_CHAR *limit ATTRIBUTE_UNUSED;
3209 FILE_BUF *op ATTRIBUTE_UNUSED;
3211 HASHNODE *hp;
3213 SKIP_WHITE_SPACE (buf);
3215 if (! strncmp ((const char *)buf, "defined", 7) && ! is_idchar (buf[7]))
3216 warning ("undefining `defined'");
3218 while ((hp = lookup (buf, -1, -1)) != NULL) {
3219 if (hp->type != T_MACRO)
3220 warning ("undefining `%s'", hp->name);
3221 delete_macro (hp);
3225 /* Read the tokens of the answer into the macro pool. Only commit the
3226 memory if we intend it as permanent storage, i.e. the #assert case.
3227 Returns 0 on success. */
3229 static int
3230 parse_answer (buf, limit, answerp, type)
3231 const unsigned char *buf, *limit;
3232 struct answer **answerp;
3233 int type;
3235 const unsigned char *start;
3237 /* Skip leading whitespace. */
3238 if (buf < limit && *buf == ' ')
3239 buf++;
3241 /* Parentheses are optional here. */
3242 if (buf == limit && type == T_UNASSERT)
3243 return 0;
3245 if (buf == limit || *buf++ != '(')
3247 if (type == T_IF)
3248 return 0;
3250 error ("missing '(' after predicate");
3251 return 1;
3254 /* Drop whitespace at start. */
3255 while (buf < limit && *buf == ' ')
3256 buf++;
3258 start = buf;
3259 while (buf < limit && *buf != ')')
3260 buf++;
3262 if (buf == limit)
3264 error ("missing ')' to complete answer");
3265 return 1;
3268 if (buf == start)
3270 error ("predicate's answer is empty");
3271 return 1;
3274 if ((type == T_ASSERT || type == T_UNASSERT) && buf + 1 != limit)
3276 error ("extra text at end of directive");
3277 return 1;
3280 /* Lose trailing whitespace. */
3281 if (buf[-1] == ' ')
3282 buf--;
3284 *answerp = (struct answer *) xmalloc (sizeof (struct answer));
3285 (*answerp)->answer = start;
3286 (*answerp)->len = buf - start;
3288 return 0;
3291 /* Parses an assertion, returning a pointer to the hash node of the
3292 predicate, or 0 on error. If an answer was supplied, it is placed
3293 in ANSWERP, otherwise it is set to 0. */
3294 static HASHNODE *
3295 parse_assertion (buf, limit, answerp, type)
3296 const unsigned char *buf, *limit;
3297 struct answer **answerp;
3298 int type;
3300 HASHNODE *result = 0;
3301 const unsigned char *climit;
3302 unsigned char *bp, *symname = canonicalize_text (buf, limit, &climit);
3303 unsigned int len;
3305 bp = symname;
3306 if (bp < climit && is_idstart (*bp))
3309 bp++;
3310 while (bp < climit && is_idchar (*bp));
3312 len = bp - symname;
3314 *answerp = 0;
3315 if (len == 0)
3317 if (symname == climit)
3318 error ("assertion without predicate");
3319 else
3320 error ("predicate must be an identifier");
3322 /* Unfortunately, because of the way we handle #if, we don't avoid
3323 macro expansion in answers. This is not easy to fix. */
3324 else if (parse_answer (bp, climit, answerp, type) == 0)
3326 unsigned char *sym = alloca (len + 1);
3327 int hashcode;
3329 /* Prefix '#' to get it out of macro namespace. */
3330 sym[0] = '#';
3331 memcpy (sym + 1, symname, len);
3333 hashcode = hashf (sym, len + 1, HASHSIZE);
3334 result = lookup (sym, len + 1, hashcode);
3335 if (result == 0)
3336 result = install (sym, len + 1, T_UNUSED, hashcode);
3339 return result;
3342 /* Test an assertion within a preprocessor conditional. Returns zero
3343 on error or failure, one on success. */
3345 test_assertion (pbuf)
3346 unsigned char **pbuf; /* NUL-terminated. */
3348 unsigned char *buf = *pbuf;
3349 unsigned char *limit = buf + strlen ((char *) buf);
3350 struct answer *answer;
3351 HASHNODE *node;
3352 int result = 0;
3354 node = parse_assertion (buf, limit, &answer, T_IF);
3355 if (node)
3357 result = (node->type == T_ASSERT &&
3358 (answer == 0 || *find_answer (node, answer) != 0));
3360 /* Yuk. We update pbuf to point after the assertion test.
3361 First, move past the identifier. */
3362 if (is_space (*buf))
3363 buf++;
3364 while (is_idchar (*buf))
3365 buf++;
3366 /* If we have an answer, we need to move past the parentheses. */
3367 if (answer)
3368 while (*buf++ != ')')
3370 *pbuf = buf;
3373 return result;
3376 /* Handle a #error directive. */
3377 static void
3378 do_error (buf, limit, op)
3379 U_CHAR *buf;
3380 U_CHAR *limit;
3381 FILE_BUF *op ATTRIBUTE_UNUSED;
3383 error ("#error%.*s", (int) (limit - buf), buf);
3386 /* Handle a #warning directive. */
3387 static void
3388 do_warning (buf, limit, op)
3389 U_CHAR *buf;
3390 U_CHAR *limit;
3391 FILE_BUF *op ATTRIBUTE_UNUSED;
3393 warning ("#warning%.*s", (int) (limit - buf), buf);
3396 /* Handle a #assert directive. */
3397 static void
3398 do_assert (buf, limit, op)
3399 U_CHAR *buf;
3400 U_CHAR *limit;
3401 FILE_BUF *op ATTRIBUTE_UNUSED;
3403 struct answer *new_answer;
3404 HASHNODE *node;
3406 node = parse_assertion (buf, limit, &new_answer, T_ASSERT);
3407 if (node)
3409 /* Place the new answer in the answer list. First check there
3410 is not a duplicate. */
3411 new_answer->next = 0;
3412 if (node->type == T_ASSERT)
3414 if (*find_answer (node, new_answer))
3416 free (new_answer);
3417 warning ("\"%s\" re-asserted", node->name + 1);
3418 return;
3420 new_answer->next = node->value.answers;
3422 node->type = T_ASSERT;
3423 node->value.answers = new_answer;
3427 /* Function body to be provided later. */
3428 static void
3429 do_unassert (buf, limit, op)
3430 U_CHAR *buf;
3431 U_CHAR *limit;
3432 FILE_BUF *op ATTRIBUTE_UNUSED;
3434 HASHNODE *node;
3435 struct answer *answer;
3437 node = parse_assertion (buf, limit, &answer, T_UNASSERT);
3438 /* It isn't an error to #unassert something that isn't asserted. */
3439 if (node)
3441 if (node->type == T_ASSERT)
3443 if (answer)
3445 struct answer **p = find_answer (node, answer), *temp;
3447 /* Remove the answer from the list. */
3448 temp = *p;
3449 if (temp)
3450 *p = temp->next;
3452 /* Did we free the last answer? */
3453 if (node->value.answers == 0)
3454 delete_macro (node);
3456 else
3457 delete_macro (node);
3460 free (answer);
3464 /* Returns a pointer to the pointer to the answer in the answer chain,
3465 or a pointer to NULL if the answer is not in the chain. */
3466 static struct answer **
3467 find_answer (node, candidate)
3468 HASHNODE *node;
3469 const struct answer *candidate;
3471 struct answer **result;
3473 for (result = &node->value.answers; *result; result = &(*result)->next)
3475 struct answer *answer = *result;
3477 if (answer->len == candidate->len
3478 && !memcmp (answer->answer, candidate->answer, answer->len))
3479 break;
3482 return result;
3485 /* Return a malloced buffer with leading and trailing whitespace
3486 removed, and all instances of internal whitespace reduced to a
3487 single space. */
3488 static unsigned char *
3489 canonicalize_text (buf, limit, climit)
3490 const unsigned char *buf, *limit, **climit;
3492 unsigned int len = limit - buf;
3493 unsigned char *result = (unsigned char *) xmalloc (len), *dest;
3495 for (dest = result; buf < limit;)
3497 if (! is_space (*buf))
3498 *dest++ = *buf++;
3499 else
3501 while (++buf < limit && is_space (*buf))
3503 if (dest != result && buf != limit)
3504 *dest++ = ' ';
3508 *climit = dest;
3509 return result;
3513 * handle #if command by
3514 * 1) inserting special `defined' keyword into the hash table
3515 * that gets turned into 0 or 1 by special_symbol (thus,
3516 * if the luser has a symbol called `defined' already, it won't
3517 * work inside the #if command)
3518 * 2) rescan the input into a temporary output buffer
3519 * 3) pass the output buffer to the yacc parser and collect a value
3520 * 4) clean up the mess left from steps 1 and 2.
3521 * 5) call conditional_skip to skip til the next #endif (etc.),
3522 * or not, depending on the value from step 3.
3524 static void
3525 do_if (buf, limit, op)
3526 U_CHAR *buf, *limit;
3527 FILE_BUF *op ATTRIBUTE_UNUSED;
3529 int value;
3530 FILE_BUF *ip = &instack[indepth];
3532 value = eval_if_expression (buf, limit - buf);
3533 conditional_skip (ip, value == 0, T_IF);
3537 * handle a #elif directive by not changing if_stack either.
3538 * see the comment above do_else.
3540 static void
3541 do_elif (buf, limit, op)
3542 U_CHAR *buf, *limit;
3543 FILE_BUF *op;
3545 int value;
3546 FILE_BUF *ip = &instack[indepth];
3548 if (if_stack == instack[indepth].if_stack) {
3549 error ("#elif not within a conditional");
3550 return;
3551 } else {
3552 if (if_stack->type != T_IF && if_stack->type != T_ELIF) {
3553 error ("#elif after #else");
3554 fprintf (stderr, " (matches line %d", if_stack->lineno);
3555 if (if_stack->fname != NULL && ip->fname != NULL &&
3556 strcmp (if_stack->fname, ip->fname) != 0)
3557 fprintf (stderr, ", file %s", if_stack->fname);
3558 fprintf (stderr, ")\n");
3560 if_stack->type = T_ELIF;
3563 if (if_stack->if_succeeded)
3564 skip_if_group (ip, 0);
3565 else {
3566 value = eval_if_expression (buf, limit - buf);
3567 if (value == 0)
3568 skip_if_group (ip, 0);
3569 else {
3570 ++if_stack->if_succeeded; /* continue processing input */
3571 output_line_command (ip, op, 1, same_file);
3577 * evaluate a #if expression in BUF, of length LENGTH,
3578 * then parse the result as a C expression and return the value as an int.
3580 static int
3581 eval_if_expression (buf, length)
3582 const U_CHAR *buf;
3583 int length;
3585 FILE_BUF temp_obuf;
3586 HASHNODE *save_defined;
3587 int value;
3589 save_defined = install (U"defined", -1, T_SPEC_DEFINED, -1);
3590 temp_obuf = expand_to_temp_buffer (buf, buf + length, 0);
3591 delete_macro (save_defined); /* clean up special symbol */
3593 value = parse_c_expression ((const char *)temp_obuf.buf);
3595 free (temp_obuf.buf);
3597 return value;
3601 * routine to handle ifdef/ifndef. Try to look up the symbol,
3602 * then do or don't skip to the #endif/#else/#elif depending
3603 * on what directive is actually being processed.
3605 static void
3606 do_xifdef (buf, limit, type)
3607 U_CHAR *buf, *limit;
3608 enum node_type type;
3610 int skip;
3611 FILE_BUF *ip = &instack[indepth];
3612 U_CHAR *end;
3614 /* Discard leading and trailing whitespace. */
3615 SKIP_WHITE_SPACE (buf);
3616 while (limit != buf && is_nvspace (limit[-1])) limit--;
3618 /* Find the end of the identifier at the beginning. */
3619 for (end = buf; is_idchar (*end); end++);
3621 if (end == buf)
3622 skip = (type == T_IFDEF);
3623 else
3624 skip = (lookup (buf, end-buf, -1) == NULL) ^ (type == T_IFNDEF);
3626 conditional_skip (ip, skip, T_IF);
3629 static void
3630 do_ifdef (buf, limit, op)
3631 U_CHAR *buf, *limit;
3632 FILE_BUF *op ATTRIBUTE_UNUSED;
3634 do_xifdef (buf, limit, T_IFDEF);
3637 static void
3638 do_ifndef (buf, limit, op)
3639 U_CHAR *buf, *limit;
3640 FILE_BUF *op ATTRIBUTE_UNUSED;
3642 do_xifdef (buf, limit, T_IFNDEF);
3646 * push TYPE on stack; then, if SKIP is nonzero, skip ahead.
3648 static void
3649 conditional_skip (ip, skip, type)
3650 FILE_BUF *ip;
3651 int skip;
3652 enum node_type type;
3654 IF_STACK_FRAME *temp;
3656 temp = (IF_STACK_FRAME *) xcalloc (1, sizeof (IF_STACK_FRAME));
3657 temp->fname = ip->fname;
3658 temp->lineno = ip->lineno;
3659 temp->next = if_stack;
3660 if_stack = temp;
3662 if_stack->type = type;
3664 if (skip != 0) {
3665 skip_if_group (ip, 0);
3666 return;
3667 } else {
3668 ++if_stack->if_succeeded;
3669 output_line_command (ip, &outbuf, 1, same_file);
3674 * skip to #endif, #else, or #elif. adjust line numbers, etc.
3675 * leaves input ptr at the sharp sign found.
3676 * If ANY is nonzero, return at next directive of any sort.
3678 static void
3679 skip_if_group (ip, any)
3680 FILE_BUF *ip;
3681 int any;
3683 U_CHAR *bp = ip->bufp, *cp;
3684 U_CHAR *endb = ip->buf + ip->length;
3685 struct directive *kt;
3686 IF_STACK_FRAME *save_if_stack = if_stack; /* don't pop past here */
3687 U_CHAR *beg_of_line = bp;
3689 while (bp < endb) {
3690 switch (*bp++) {
3691 case '/': /* possible comment */
3692 if (*bp == '\\' && bp[1] == '\n')
3693 newline_fix (bp);
3694 if (*bp == '*') {
3695 ip->bufp = ++bp;
3696 bp = skip_to_end_of_comment (ip, &ip->lineno);
3698 break;
3699 case '\"':
3700 case '\'':
3701 bp = skip_quoted_string (bp - 1, endb, ip->lineno, &ip->lineno, 0, 0);
3702 break;
3703 case '\\':
3704 /* Char after backslash loses its special meaning. */
3705 if (bp < endb) {
3706 if (*bp == '\n')
3707 ++ip->lineno; /* But do update the line-count. */
3708 bp++;
3710 break;
3711 case '\n':
3712 ++ip->lineno;
3713 beg_of_line = bp;
3714 break;
3715 case '#':
3716 ip->bufp = bp - 1;
3718 /* # keyword: a # must be first nonblank char on the line */
3719 if (beg_of_line == 0)
3720 break;
3721 /* Scan from start of line, skipping whitespace, comments
3722 and backslash-newlines, and see if we reach this #.
3723 If not, this # is not special. */
3724 bp = beg_of_line;
3725 while (1) {
3726 if (is_nvspace (*bp))
3727 bp++;
3728 else if (*bp == '\\' && bp[1] == '\n')
3729 bp += 2;
3730 else if (*bp == '/' && bp[1] == '*') {
3731 bp += 2;
3732 while (!(*bp == '*' && bp[1] == '/')) {
3733 if (*bp == '\n')
3734 ip->lineno++;
3735 bp++;
3737 bp += 2;
3739 else break;
3741 if (bp != ip->bufp) {
3742 bp = ip->bufp + 1; /* Reset bp to after the #. */
3743 break;
3746 bp = ip->bufp + 1; /* Point after '#'. */
3748 /* Skip whitespace and \-newline. */
3749 while (1) {
3750 if (is_nvspace (*bp))
3751 bp++;
3752 else if (*bp == '\\' && bp[1] == '\n')
3753 bp += 2;
3754 else if (*bp == '/' && bp[1] == '*') {
3755 bp += 2;
3756 while (!(*bp == '*' && bp[1] == '/'))
3757 bp++;
3758 bp += 2;
3760 else break;
3763 cp = bp;
3765 /* Now find end of directive name.
3766 If we encounter a backslash-newline, exchange it with any following
3767 symbol-constituents so that we end up with a contiguous name. */
3769 while (1) {
3770 if (is_idchar (*bp))
3771 bp++;
3772 else {
3773 if (*bp == '\\' && bp[1] == '\n')
3774 name_newline_fix (bp);
3775 if (is_idchar (*bp))
3776 bp++;
3777 else break;
3781 for (kt = directive_table; kt->length >= 0; kt++) {
3782 IF_STACK_FRAME *temp;
3783 if (strncmp ((const char *)cp, kt->name, kt->length) == 0
3784 && !is_idchar (cp[kt->length])) {
3786 /* If we are asked to return on next directive,
3787 do so now. */
3788 if (any)
3789 return;
3791 switch (kt->type) {
3792 case T_IF:
3793 case T_IFDEF:
3794 case T_IFNDEF:
3795 temp = (IF_STACK_FRAME *) xcalloc (1, sizeof (IF_STACK_FRAME));
3796 temp->next = if_stack;
3797 if_stack = temp;
3798 temp->lineno = ip->lineno;
3799 temp->fname = ip->fname;
3800 temp->type = kt->type;
3801 break;
3802 case T_ELSE:
3803 case T_ENDIF:
3804 case T_ELIF:
3805 if (if_stack == instack[indepth].if_stack) {
3806 error ("#%s not within a conditional", kt->name);
3807 break;
3809 else if (if_stack == save_if_stack)
3810 return; /* found what we came for */
3812 if (kt->type != T_ENDIF) {
3813 if (if_stack->type == T_ELSE)
3814 error ("#else or #elif after #else");
3815 if_stack->type = kt->type;
3816 break;
3819 temp = if_stack;
3820 if_stack = if_stack->next;
3821 free (temp);
3822 break;
3824 default:
3825 /* Anything else is ignored. */
3826 break;
3828 break;
3833 ip->bufp = bp;
3834 /* after this returns, rescan will exit because ip->bufp
3835 now points to the end of the buffer.
3836 rescan is responsible for the error message also. */
3840 * handle a #else directive. Do this by just continuing processing
3841 * without changing if_stack ; this is so that the error message
3842 * for missing #endif's etc. will point to the original #if. It
3843 * is possible that something different would be better.
3845 static void
3846 do_else (buf, limit, op)
3847 U_CHAR *buf ATTRIBUTE_UNUSED;
3848 U_CHAR *limit ATTRIBUTE_UNUSED;
3849 FILE_BUF *op;
3851 FILE_BUF *ip = &instack[indepth];
3853 if (if_stack == instack[indepth].if_stack) {
3854 error ("#else not within a conditional");
3855 return;
3856 } else {
3857 if (if_stack->type != T_IF && if_stack->type != T_ELIF) {
3858 error ("#else after #else");
3859 fprintf (stderr, " (matches line %d", if_stack->lineno);
3860 if (strcmp (if_stack->fname, ip->fname) != 0)
3861 fprintf (stderr, ", file %s", if_stack->fname);
3862 fprintf (stderr, ")\n");
3864 if_stack->type = T_ELSE;
3867 if (if_stack->if_succeeded)
3868 skip_if_group (ip, 0);
3869 else {
3870 ++if_stack->if_succeeded; /* continue processing input */
3871 output_line_command (ip, op, 1, same_file);
3876 * unstack after #endif command
3878 static void
3879 do_endif (buf, limit, op)
3880 U_CHAR *buf ATTRIBUTE_UNUSED;
3881 U_CHAR *limit ATTRIBUTE_UNUSED;
3882 FILE_BUF *op;
3884 if (if_stack == instack[indepth].if_stack)
3885 error ("unbalanced #endif");
3886 else {
3887 IF_STACK_FRAME *temp = if_stack;
3888 if_stack = if_stack->next;
3889 free (temp);
3890 output_line_command (&instack[indepth], op, 1, same_file);
3895 * Skip a comment, assuming the input ptr immediately follows the
3896 * initial slash-star. Bump line counter as necessary.
3897 * (The canonical line counter is &ip->lineno).
3898 * Don't use this routine (or the next one) if bumping the line
3899 * counter is not sufficient to deal with newlines in the string.
3901 static U_CHAR *
3902 skip_to_end_of_comment (ip, line_counter)
3903 FILE_BUF *ip;
3904 int *line_counter; /* place to remember newlines, or NULL */
3906 U_CHAR *limit = ip->buf + ip->length;
3907 U_CHAR *bp = ip->bufp;
3908 FILE_BUF *op = &outbuf; /* JF */
3909 int output = put_out_comments && !line_counter;
3911 /* JF this line_counter stuff is a crock to make sure the
3912 comment is only put out once, no matter how many times
3913 the comment is skipped. It almost works */
3914 if (output) {
3915 *op->bufp++ = '/';
3916 *op->bufp++ = '*';
3918 while (bp < limit) {
3919 if (output)
3920 *op->bufp++ = *bp;
3921 switch (*bp++) {
3922 case '/':
3923 if (warn_comments && bp < limit && *bp == '*')
3924 warning("`/*' within comment");
3925 break;
3926 case '\n':
3927 if (line_counter != NULL)
3928 ++*line_counter;
3929 if (output)
3930 ++op->lineno;
3931 break;
3932 case '*':
3933 if (*bp == '\\' && bp[1] == '\n')
3934 newline_fix (bp);
3935 if (*bp == '/') {
3936 if (output)
3937 *op->bufp++ = '/';
3938 ip->bufp = ++bp;
3939 return bp;
3941 break;
3944 ip->bufp = bp;
3945 return bp;
3949 * Skip over a quoted string. BP points to the opening quote.
3950 * Returns a pointer after the closing quote. Don't go past LIMIT.
3951 * START_LINE is the line number of the starting point (but it need
3952 * not be valid if the starting point is inside a macro expansion).
3954 * The input stack state is not changed.
3956 * If COUNT_NEWLINES is nonzero, it points to an int to increment
3957 * for each newline passed.
3959 * If BACKSLASH_NEWLINES_P is nonzero, store 1 thru it
3960 * if we pass a backslash-newline.
3962 * If EOFP is nonzero, set *EOFP to 1 if the string is unterminated.
3964 static U_CHAR *
3965 skip_quoted_string (bp, limit, start_line, count_newlines, backslash_newlines_p, eofp)
3966 const U_CHAR *bp;
3967 const U_CHAR *limit;
3968 int start_line;
3969 int *count_newlines;
3970 int *backslash_newlines_p;
3971 int *eofp;
3973 U_CHAR c, match;
3975 match = *bp++;
3976 while (1) {
3977 if (bp >= limit) {
3978 error_with_line (line_for_error (start_line),
3979 "unterminated string or character constant");
3980 if (eofp)
3981 *eofp = 1;
3982 break;
3984 c = *bp++;
3985 if (c == '\\') {
3986 while (*bp == '\\' && bp[1] == '\n') {
3987 if (backslash_newlines_p)
3988 *backslash_newlines_p = 1;
3989 if (count_newlines)
3990 ++*count_newlines;
3991 bp += 2;
3993 if (*bp == '\n' && count_newlines) {
3994 if (backslash_newlines_p)
3995 *backslash_newlines_p = 1;
3996 ++*count_newlines;
3998 bp++;
3999 } else if (c == '\n') {
4000 /* Unterminated strings and character constants are 'legal'. */
4001 bp--; /* Don't consume the newline. */
4002 if (eofp)
4003 *eofp = 1;
4004 break;
4005 } else if (c == match)
4006 break;
4008 return (U_CHAR *) bp;
4012 * write out a #line command, for instance, after an #include file.
4013 * If CONDITIONAL is nonzero, we can omit the #line if it would
4014 * appear to be a no-op, and we can output a few newlines instead
4015 * if we want to increase the line number by a small amount.
4016 * FILE_CHANGE says whether we are entering a file, leaving, or neither.
4019 static void
4020 output_line_command (ip, op, conditional, file_change)
4021 FILE_BUF *ip, *op;
4022 int conditional;
4023 enum file_change_code file_change;
4025 int len;
4026 char line_cmd_buf[500];
4028 if (no_line_commands
4029 || ip->fname == NULL
4030 || no_output) {
4031 op->lineno = ip->lineno;
4032 return;
4035 if (conditional) {
4036 if (ip->lineno == op->lineno)
4037 return;
4039 /* If the inherited line number is a little too small,
4040 output some newlines instead of a #line command. */
4041 if (ip->lineno > op->lineno && ip->lineno < op->lineno + 8) {
4042 check_expand (op, 10);
4043 while (ip->lineno > op->lineno) {
4044 *op->bufp++ = '\n';
4045 op->lineno++;
4047 return;
4051 sprintf (line_cmd_buf, "# %d \"%s\"", ip->lineno, ip->fname);
4052 if (file_change != same_file)
4053 strcat (line_cmd_buf, file_change == enter_file ? " 1" : " 2");
4054 if (system_include_depth > 0)
4055 strcat (line_cmd_buf, " 3");
4056 len = strlen (line_cmd_buf);
4057 line_cmd_buf[len++] = '\n';
4058 check_expand (op, len + 1);
4059 if (op->bufp > op->buf && op->bufp[-1] != '\n')
4060 *op->bufp++ = '\n';
4061 memcpy (op->bufp, line_cmd_buf, len);
4062 op->bufp += len;
4063 op->lineno = ip->lineno;
4067 /* Expand a macro call.
4068 HP points to the symbol that is the macro being called.
4069 Put the result of expansion onto the input stack
4070 so that subsequent input by our caller will use it.
4072 If macro wants arguments, caller has already verified that
4073 an argument list follows; arguments come from the input stack. */
4075 static void
4076 macroexpand (hp, op)
4077 HASHNODE *hp;
4078 FILE_BUF *op;
4080 int nargs;
4081 DEFINITION *defn = hp->value.defn;
4082 U_CHAR *xbuf;
4083 int xbuf_len;
4084 int start_line = instack[indepth].lineno;
4086 CHECK_DEPTH (return;);
4088 /* it might not actually be a macro. */
4089 if (hp->type != T_MACRO) {
4090 special_symbol (hp, op);
4091 return;
4094 nargs = defn->nargs;
4096 if (nargs >= 0) {
4097 int i;
4098 struct argdata *args;
4099 const char *parse_error = 0;
4101 args = (struct argdata *) alloca ((nargs + 1) * sizeof (struct argdata));
4103 for (i = 0; i < nargs; i++) {
4104 args[i].raw = args[i].expanded = (U_CHAR *) "";
4105 args[i].raw_length = args[i].expand_length
4106 = args[i].stringified_length = 0;
4107 args[i].free1 = args[i].free2 = 0;
4110 /* Parse all the macro args that are supplied. I counts them.
4111 The first NARGS args are stored in ARGS.
4112 The rest are discarded. */
4113 i = 0;
4114 do {
4115 /* Discard the open-parenthesis or comma before the next arg. */
4116 ++instack[indepth].bufp;
4117 parse_error
4118 = macarg ((i < nargs || (nargs == 0 && i == 0)) ? &args[i] : 0);
4119 if (parse_error)
4121 error_with_line (line_for_error (start_line), "%s", parse_error);
4122 break;
4124 i++;
4125 } while (*instack[indepth].bufp != ')');
4127 /* If we got one arg but it was just whitespace, call that 0 args. */
4128 if (i == 1) {
4129 const U_CHAR *bp = args[0].raw;
4130 const U_CHAR *lim = bp + args[0].raw_length;
4131 while (bp != lim && is_space (*bp)) bp++;
4132 if (bp == lim)
4133 i = 0;
4136 if (nargs == 0 && i > 0)
4137 error ("arguments given to macro `%s'", hp->name);
4138 else if (i < nargs) {
4139 /* traditional C allows foo() if foo wants one argument. */
4140 if (nargs == 1 && i == 0)
4142 else if (i == 0)
4143 error ("no args to macro `%s'", hp->name);
4144 else if (i == 1)
4145 error ("only 1 arg to macro `%s'", hp->name);
4146 else
4147 error ("only %d args to macro `%s'", i, hp->name);
4148 } else if (i > nargs)
4149 error ("too many (%d) args to macro `%s'", i, hp->name);
4151 /* Swallow the closeparen. */
4152 ++instack[indepth].bufp;
4154 /* If macro wants zero args, we parsed the arglist for checking only.
4155 Read directly from the macro definition. */
4156 if (nargs == 0) {
4157 xbuf = defn->expansion;
4158 xbuf_len = defn->length;
4159 } else {
4160 U_CHAR *exp = defn->expansion;
4161 int offset; /* offset in expansion,
4162 copied a piece at a time */
4163 int totlen; /* total amount of exp buffer filled so far */
4165 struct reflist *ap;
4167 /* Macro really takes args. Compute the expansion of this call. */
4169 /* Compute length in characters of the macro's expansion. */
4170 xbuf_len = defn->length;
4171 for (ap = defn->pattern; ap != NULL; ap = ap->next) {
4172 if (ap->stringify)
4173 xbuf_len += args[ap->argno].stringified_length;
4174 else
4175 xbuf_len += args[ap->argno].raw_length;
4178 xbuf = (U_CHAR *) xmalloc (xbuf_len + 1);
4180 /* Generate in XBUF the complete expansion
4181 with arguments substituted in.
4182 TOTLEN is the total size generated so far.
4183 OFFSET is the index in the definition
4184 of where we are copying from. */
4185 offset = totlen = 0;
4186 for (ap = defn->pattern; ap != NULL; ap = ap->next) {
4187 struct argdata *arg = &args[ap->argno];
4189 for (i = 0; i < ap->nchars; i++)
4190 xbuf[totlen++] = exp[offset++];
4192 if (ap->stringify != 0) {
4193 int arglen = arg->raw_length;
4194 int escaped = 0;
4195 int in_string = 0;
4196 int c;
4197 i = 0;
4198 while (i < arglen
4199 && (c = arg->raw[i], is_space (c)))
4200 i++;
4201 while (i < arglen
4202 && (c = arg->raw[arglen - 1], is_space (c)))
4203 arglen--;
4204 for (; i < arglen; i++) {
4205 c = arg->raw[i];
4207 /* Special markers Newline Space
4208 generate nothing for a stringified argument. */
4209 if (c == '\n' && arg->raw[i+1] != '\n') {
4210 i++;
4211 continue;
4214 /* Internal sequences of whitespace are replaced by one space
4215 except within an string or char token. */
4216 if (! in_string
4217 && (c == '\n' ? arg->raw[i+1] == '\n' : is_space (c))) {
4218 while (1) {
4219 /* Note that Newline Space does occur within whitespace
4220 sequences; consider it part of the sequence. */
4221 if (c == '\n' && is_space (arg->raw[i+1]))
4222 i += 2;
4223 else if (c != '\n' && is_space (c))
4224 i++;
4225 else break;
4226 c = arg->raw[i];
4228 i--;
4229 c = ' ';
4232 if (escaped)
4233 escaped = 0;
4234 else {
4235 if (c == '\\')
4236 escaped = 1;
4237 if (in_string) {
4238 if (c == in_string)
4239 in_string = 0;
4240 } else if (c == '\"' || c == '\'')
4241 in_string = c;
4244 /* Escape these chars */
4245 if (c == '\"' || (in_string && c == '\\'))
4246 xbuf[totlen++] = '\\';
4247 if (ISPRINT (c))
4248 xbuf[totlen++] = c;
4249 else {
4250 sprintf ((char *) &xbuf[totlen], "\\%03o", (unsigned int) c);
4251 totlen += 4;
4254 } else {
4255 const U_CHAR *p1 = arg->raw;
4256 const U_CHAR *l1 = p1 + arg->raw_length;
4258 if (ap->raw_before) {
4259 while (p1 != l1 && is_space (*p1)) p1++;
4260 while (p1 != l1 && is_idchar (*p1))
4261 xbuf[totlen++] = *p1++;
4262 /* Delete any no-reexpansion marker that follows
4263 an identifier at the beginning of the argument
4264 if the argument is concatenated with what precedes it. */
4265 if (p1[0] == '\n' && p1[1] == '-')
4266 p1 += 2;
4268 if (ap->raw_after) {
4269 /* Arg is concatenated after: delete trailing whitespace,
4270 whitespace markers, and no-reexpansion markers. */
4271 while (p1 != l1) {
4272 if (is_space (l1[-1])) l1--;
4273 else if (l1[-1] == '-') {
4274 const U_CHAR *p2 = l1 - 1;
4275 /* If a `-' is preceded by an odd number of newlines then it
4276 and the last newline are a no-reexpansion marker. */
4277 while (p2 != p1 && p2[-1] == '\n') p2--;
4278 if ((l1 - 1 - p2) & 1) {
4279 l1 -= 2;
4281 else break;
4283 else break;
4286 memmove (xbuf + totlen, p1, l1 - p1);
4287 totlen += l1 - p1;
4290 if (totlen > xbuf_len)
4291 abort ();
4294 /* if there is anything left of the definition
4295 after handling the arg list, copy that in too. */
4297 for (i = offset; i < defn->length; i++)
4298 xbuf[totlen++] = exp[i];
4300 xbuf[totlen] = 0;
4301 xbuf_len = totlen;
4303 for (i = 0; i < nargs; i++) {
4304 if (args[i].free1 != 0)
4305 free (args[i].free1);
4306 if (args[i].free2 != 0)
4307 free (args[i].free2);
4310 } else {
4311 xbuf = defn->expansion;
4312 xbuf_len = defn->length;
4315 /* Now put the expansion on the input stack
4316 so our caller will commence reading from it. */
4318 FILE_BUF *ip2;
4320 ip2 = &instack[++indepth];
4322 ip2->fname = 0;
4323 ip2->lineno = 0;
4324 ip2->buf = xbuf;
4325 ip2->length = xbuf_len;
4326 ip2->bufp = xbuf;
4327 ip2->free_ptr = (nargs > 0) ? xbuf : 0;
4328 ip2->macro = hp;
4329 ip2->if_stack = if_stack;
4334 * Parse a macro argument and store the info on it into *ARGPTR.
4335 * Return nonzero to indicate a syntax error.
4338 static const char *
4339 macarg (argptr)
4340 struct argdata *argptr;
4342 FILE_BUF *ip = &instack[indepth];
4343 int paren = 0;
4344 int newlines = 0;
4345 int comments = 0;
4347 /* Try to parse as much of the argument as exists at this
4348 input stack level. */
4349 U_CHAR *bp = macarg1 (ip->bufp, ip->buf + ip->length,
4350 &paren, &newlines, &comments);
4352 /* If we find the end of the argument at this level,
4353 set up *ARGPTR to point at it in the input stack. */
4354 if (!(ip->fname != 0 && (newlines != 0 || comments != 0))
4355 && bp != ip->buf + ip->length) {
4356 if (argptr != 0) {
4357 argptr->raw = ip->bufp;
4358 argptr->raw_length = bp - ip->bufp;
4360 ip->bufp = bp;
4361 } else {
4362 /* This input stack level ends before the macro argument does.
4363 We must pop levels and keep parsing.
4364 Therefore, we must allocate a temporary buffer and copy
4365 the macro argument into it. */
4366 int bufsize = bp - ip->bufp;
4367 int extra = newlines;
4368 U_CHAR *buffer = (U_CHAR *) xmalloc (bufsize + extra + 1);
4369 int final_start = 0;
4371 memcpy (buffer, ip->bufp, bufsize);
4372 ip->bufp = bp;
4373 ip->lineno += newlines;
4375 while (bp == ip->buf + ip->length) {
4376 if (instack[indepth].macro == 0) {
4377 free (buffer);
4378 return "unterminated macro call";
4380 ip->macro->type = T_MACRO;
4381 if (ip->free_ptr)
4382 free (ip->free_ptr);
4383 ip = &instack[--indepth];
4384 newlines = 0;
4385 comments = 0;
4386 bp = macarg1 (ip->bufp, ip->buf + ip->length, &paren,
4387 &newlines, &comments);
4388 final_start = bufsize;
4389 bufsize += bp - ip->bufp;
4390 extra += newlines;
4391 buffer = (U_CHAR *) xrealloc (buffer, bufsize + extra + 1);
4392 memcpy (buffer + bufsize - (bp - ip->bufp), ip->bufp, bp - ip->bufp);
4393 ip->bufp = bp;
4394 ip->lineno += newlines;
4397 /* Now, if arg is actually wanted, record its raw form,
4398 discarding comments and duplicating newlines in whatever
4399 part of it did not come from a macro expansion.
4400 EXTRA space has been preallocated for duplicating the newlines.
4401 FINAL_START is the index of the start of that part. */
4402 if (argptr != 0) {
4403 argptr->raw = buffer;
4404 argptr->raw_length = bufsize;
4405 argptr->free1 = buffer;
4406 argptr->newlines = newlines;
4407 argptr->comments = comments;
4408 if ((newlines || comments) && ip->fname != 0)
4409 argptr->raw_length
4410 = final_start +
4411 discard_comments (argptr->raw + final_start,
4412 argptr->raw_length - final_start,
4413 newlines);
4414 argptr->raw[argptr->raw_length] = 0;
4415 if (argptr->raw_length > bufsize + extra)
4416 abort ();
4420 /* If we are not discarding this argument,
4421 macroexpand it and compute its length as stringified.
4422 All this info goes into *ARGPTR. */
4424 if (argptr != 0) {
4425 FILE_BUF obuf;
4426 const U_CHAR *buf, *lim;
4427 int totlen;
4429 obuf = expand_to_temp_buffer (argptr->raw,
4430 argptr->raw + argptr->raw_length,
4433 argptr->expanded = obuf.buf;
4434 argptr->expand_length = obuf.length;
4435 argptr->free2 = obuf.buf;
4437 buf = argptr->raw;
4438 lim = buf + argptr->raw_length;
4440 totlen = 0;
4441 while (buf != lim) {
4442 U_CHAR c = *buf++;
4443 totlen++;
4444 /* Internal sequences of whitespace are replaced by one space
4445 in most cases, but not always. So count all the whitespace
4446 in case we need to keep it all. */
4447 if (c == '\"' || c == '\\') /* escape these chars */
4448 totlen++;
4449 else if (!ISPRINT (c))
4450 totlen += 3;
4452 argptr->stringified_length = totlen;
4454 return 0;
4457 /* Scan text from START (inclusive) up to LIMIT (exclusive),
4458 counting parens in *DEPTHPTR,
4459 and return if reach LIMIT
4460 or before a `)' that would make *DEPTHPTR negative
4461 or before a comma when *DEPTHPTR is zero.
4462 Single and double quotes are matched and termination
4463 is inhibited within them. Comments also inhibit it.
4464 Value returned is pointer to stopping place.
4466 Increment *NEWLINES each time a newline is passed.
4467 Set *COMMENTS to 1 if a comment is seen. */
4469 static U_CHAR *
4470 macarg1 (start, limit, depthptr, newlines, comments)
4471 U_CHAR *start;
4472 const U_CHAR *limit;
4473 int *depthptr, *newlines, *comments;
4475 U_CHAR *bp = start;
4477 while (bp < limit) {
4478 switch (*bp) {
4479 case '(':
4480 (*depthptr)++;
4481 break;
4482 case ')':
4483 if (--(*depthptr) < 0)
4484 return bp;
4485 break;
4486 case '\\':
4487 /* Traditionally, backslash makes following char not special. */
4488 if (bp + 1 < limit)
4490 bp++;
4491 /* But count source lines anyway. */
4492 if (*bp == '\n')
4493 ++*newlines;
4495 break;
4496 case '\n':
4497 ++*newlines;
4498 break;
4499 case '/':
4500 if (bp[1] == '\\' && bp[2] == '\n')
4501 newline_fix (bp + 1);
4502 if (bp[1] != '*' || bp + 1 >= limit)
4503 break;
4504 *comments = 1;
4505 bp += 2;
4506 while (bp + 1 < limit) {
4507 if (bp[0] == '*'
4508 && bp[1] == '\\' && bp[2] == '\n')
4509 newline_fix (bp + 1);
4510 if (bp[0] == '*' && bp[1] == '/')
4511 break;
4512 if (*bp == '\n') ++*newlines;
4513 bp++;
4515 bp += 1;
4516 break;
4517 case '\'':
4518 case '\"':
4520 int quotec;
4521 for (quotec = *bp++; bp + 1 < limit && *bp != quotec; bp++) {
4522 if (*bp == '\\') {
4523 bp++;
4524 if (*bp == '\n')
4525 ++*newlines;
4526 while (*bp == '\\' && bp[1] == '\n') {
4527 bp += 2;
4529 } else if (*bp == '\n') {
4530 ++*newlines;
4531 if (quotec == '\'')
4532 break;
4536 break;
4537 case ',':
4538 if ((*depthptr) == 0)
4539 return bp;
4540 break;
4542 bp++;
4545 return bp;
4548 /* Discard comments and duplicate newlines
4549 in the string of length LENGTH at START,
4550 except inside of string constants.
4551 The string is copied into itself with its beginning staying fixed.
4553 NEWLINES is the number of newlines that must be duplicated.
4554 We assume that that much extra space is available past the end
4555 of the string. */
4557 static int
4558 discard_comments (start, length, newlines)
4559 U_CHAR *start;
4560 int length;
4561 int newlines;
4563 U_CHAR *ibp;
4564 U_CHAR *obp;
4565 const U_CHAR *limit;
4566 int c;
4568 /* If we have newlines to duplicate, copy everything
4569 that many characters up. Then, in the second part,
4570 we will have room to insert the newlines
4571 while copying down.
4572 NEWLINES may actually be too large, because it counts
4573 newlines in string constants, and we don't duplicate those.
4574 But that does no harm. */
4575 if (newlines > 0) {
4576 ibp = start + length;
4577 obp = ibp + newlines;
4578 limit = start;
4579 while (limit != ibp)
4580 *--obp = *--ibp;
4583 ibp = start + newlines;
4584 limit = start + length + newlines;
4585 obp = start;
4587 while (ibp < limit) {
4588 *obp++ = c = *ibp++;
4589 switch (c) {
4590 case '\n':
4591 /* Duplicate the newline. */
4592 *obp++ = '\n';
4593 break;
4595 case '\\':
4596 if (*ibp == '\n') {
4597 obp--;
4598 ibp++;
4600 break;
4602 case '/':
4603 if (*ibp == '\\' && ibp[1] == '\n')
4604 newline_fix (ibp);
4605 /* Delete any comment. */
4606 if (ibp[0] != '*' || ibp + 1 >= limit)
4607 break;
4608 obp--;
4609 ibp++;
4610 while (ibp + 1 < limit) {
4611 if (ibp[0] == '*'
4612 && ibp[1] == '\\' && ibp[2] == '\n')
4613 newline_fix (ibp + 1);
4614 if (ibp[0] == '*' && ibp[1] == '/')
4615 break;
4616 ibp++;
4618 ibp += 2;
4619 break;
4621 case '\'':
4622 case '\"':
4623 /* Notice and skip strings, so that we don't
4624 think that comments start inside them,
4625 and so we don't duplicate newlines in them. */
4627 int quotec = c;
4628 while (ibp < limit) {
4629 *obp++ = c = *ibp++;
4630 if (c == quotec)
4631 break;
4632 if (c == '\n' && quotec == '\'')
4633 break;
4634 if (c == '\\' && ibp < limit) {
4635 while (*ibp == '\\' && ibp[1] == '\n')
4636 ibp += 2;
4637 *obp++ = *ibp++;
4641 break;
4645 return obp - start;
4649 /* Core error handling routine. */
4650 static void
4651 v_message (mtype, line, msgid, ap)
4652 enum msgtype mtype;
4653 int line;
4654 const char *msgid;
4655 va_list ap;
4657 const char *fname = 0;
4658 int i;
4660 if (mtype == MT_WARNING && inhibit_warnings)
4661 return;
4663 for (i = indepth; i >= 0; i--)
4664 if (instack[i].fname != NULL) {
4665 if (line == 0)
4666 line = instack[i].lineno;
4667 fname = instack[i].fname;
4668 break;
4671 if (fname)
4672 fprintf (stderr, "%s:%d: ", fname, line);
4673 else
4674 fprintf (stderr, "%s: ", progname);
4676 if (mtype == MT_WARNING)
4677 fputs (_("warning: "), stderr);
4679 vfprintf (stderr, _(msgid), ap);
4680 putc ('\n', stderr);
4682 if (mtype == MT_ERROR)
4683 errors++;
4687 * error - print error message and increment count of errors.
4689 void
4690 error VPARAMS ((const char *msgid, ...))
4692 VA_OPEN(ap, msgid);
4693 VA_FIXEDARG (ap, const char *, msgid);
4695 v_message (MT_ERROR, 0, msgid, ap);
4696 VA_CLOSE (ap);
4699 void
4700 error_with_line VPARAMS ((int line, const char *msgid, ...))
4702 VA_OPEN(ap, msgid);
4703 VA_FIXEDARG (ap, int, line);
4704 VA_FIXEDARG (ap, const char *, msgid);
4706 v_message (MT_ERROR, line, msgid, ap);
4707 VA_CLOSE (ap);
4710 /* Error including a message from `errno'. */
4711 void
4712 error_from_errno (name)
4713 const char *name;
4715 error ("%s: %s", name, strerror (errno));
4718 /* Print error message but don't count it. */
4719 void
4720 warning VPARAMS ((const char *msgid, ...))
4722 VA_OPEN(ap, msgid);
4723 VA_FIXEDARG (ap, const char *, msgid);
4725 v_message (MT_WARNING, 0, msgid, ap);
4726 VA_CLOSE (ap);
4729 void
4730 fatal VPARAMS ((const char *msgid, ...))
4732 VA_OPEN(ap, msgid);
4733 VA_FIXEDARG (ap, const char *, msgid);
4735 v_message (MT_FATAL, 0, msgid, ap);
4736 VA_CLOSE (ap);
4737 exit (FATAL_EXIT_CODE);
4740 /* More 'friendly' abort that prints the location at which we died. */
4741 void
4742 fancy_abort (line, func)
4743 int line;
4744 const char *func;
4746 fatal ("Internal error in %s, at tradcpp.c:%d\n\
4747 Please submit a full bug report.\n\
4748 See %s for instructions.", func, line, GCCBUGURL);
4751 void
4752 perror_with_name (name)
4753 const char *name;
4755 fprintf (stderr, "%s: %s: %s\n", progname, name, strerror (errno));
4756 errors++;
4759 void
4760 pfatal_with_name (name)
4761 const char *name;
4763 perror_with_name (name);
4764 exit (FATAL_EXIT_CODE);
4767 /* Return the line at which an error occurred.
4768 The error is not necessarily associated with the current spot
4769 in the input stack, so LINE says where. LINE will have been
4770 copied from ip->lineno for the current input level.
4771 If the current level is for a file, we return LINE.
4772 But if the current level is not for a file, LINE is meaningless.
4773 In that case, we return the lineno of the innermost file. */
4774 static int
4775 line_for_error (line)
4776 int line;
4778 int i;
4779 int line1 = line;
4781 for (i = indepth; i >= 0; ) {
4782 if (instack[i].fname != 0)
4783 return line1;
4784 i--;
4785 if (i < 0)
4786 return 0;
4787 line1 = instack[i].lineno;
4789 return 0;
4793 * If OBUF doesn't have NEEDED bytes after OPTR, make it bigger.
4795 * As things stand, nothing is ever placed in the output buffer to be
4796 * removed again except when it's KNOWN to be part of an identifier,
4797 * so flushing and moving down everything left, instead of expanding,
4798 * should work ok.
4801 static void
4802 grow_outbuf (obuf, needed)
4803 FILE_BUF *obuf;
4804 int needed;
4806 U_CHAR *p;
4807 int minsize;
4809 if (obuf->length - (obuf->bufp - obuf->buf) > needed)
4810 return;
4812 /* Make it at least twice as big as it is now. */
4813 obuf->length *= 2;
4814 /* Make it have at least 150% of the free space we will need. */
4815 minsize = (3 * needed) / 2 + (obuf->bufp - obuf->buf);
4816 if (minsize > obuf->length)
4817 obuf->length = minsize;
4819 p = (U_CHAR *) xrealloc (obuf->buf, obuf->length);
4820 obuf->bufp = p + (obuf->bufp - obuf->buf);
4821 obuf->buf = p;
4824 /* Symbol table for macro names and special symbols */
4827 * install a name in the main hash table, even if it is already there.
4828 * name stops with first non alphanumeric, except leading '#'.
4829 * caller must check against redefinition if that is desired.
4830 * delete_macro () removes things installed by install () in fifo order.
4831 * this is important because of the `defined' special symbol used
4832 * in #if, and also if pushdef/popdef directives are ever implemented.
4834 * If LEN is >= 0, it is the length of the name.
4835 * Otherwise, compute the length by scanning the entire name.
4837 * If HASH is >= 0, it is the precomputed hash code.
4838 * Otherwise, compute the hash code.
4840 * caller must set the value, if any is desired.
4842 static HASHNODE *
4843 install (name, len, type, hash)
4844 const U_CHAR *name;
4845 int len;
4846 enum node_type type;
4847 int hash;
4848 /* watch out here if sizeof (U_CHAR *) != sizeof (int) */
4850 HASHNODE *hp;
4851 int bucket;
4852 const U_CHAR *p;
4853 U_CHAR *q;
4855 if (len < 0) {
4856 p = name;
4857 while (is_idchar (*p))
4858 p++;
4859 len = p - name;
4862 if (hash < 0)
4863 hash = hashf (name, len, HASHSIZE);
4865 hp = (HASHNODE *) xmalloc (sizeof (HASHNODE) + len + 1);
4866 bucket = hash;
4867 hp->bucket_hdr = &hashtab[bucket];
4868 hp->next = hashtab[bucket];
4869 hashtab[bucket] = hp;
4870 hp->prev = NULL;
4871 if (hp->next != NULL)
4872 hp->next->prev = hp;
4873 hp->type = type;
4874 hp->length = len;
4875 hp->name = q = ((U_CHAR *) hp) + sizeof (HASHNODE);
4876 memcpy (q, name, len);
4877 q[len] = 0;
4878 return hp;
4882 * find the most recent hash node for name name (ending with first
4883 * non-identifier char) installed by install
4885 * If LEN is >= 0, it is the length of the name.
4886 * Otherwise, compute the length by scanning the entire name.
4888 * If HASH is >= 0, it is the precomputed hash code.
4889 * Otherwise, compute the hash code.
4891 HASHNODE *
4892 lookup (name, len, hash)
4893 const U_CHAR *name;
4894 int len;
4895 int hash;
4897 const U_CHAR *bp;
4898 HASHNODE *bucket;
4900 if (len < 0) {
4901 for (bp = name; is_idchar (*bp); bp++) ;
4902 len = bp - name;
4905 if (hash < 0)
4906 hash = hashf (name, len, HASHSIZE);
4908 bucket = hashtab[hash];
4909 while (bucket) {
4910 if (bucket->length == len
4911 && strncmp ((const char *)bucket->name, (const char *)name, len) == 0)
4912 return bucket;
4913 bucket = bucket->next;
4915 return NULL;
4919 * Delete a hash node. Some weirdness to free junk from macros.
4920 * More such weirdness will have to be added if you define more hash
4921 * types that need it.
4924 /* Note that the DEFINITION of a macro is removed from the hash table
4925 but its storage is not freed. This would be a storage leak
4926 except that it is not reasonable to keep undefining and redefining
4927 large numbers of macros many times.
4928 In any case, this is necessary, because a macro can be #undef'd
4929 in the middle of reading the arguments to a call to it.
4930 If #undef freed the DEFINITION, that would crash. */
4931 static void
4932 delete_macro (hp)
4933 HASHNODE *hp;
4936 if (hp->prev != NULL)
4937 hp->prev->next = hp->next;
4938 if (hp->next != NULL)
4939 hp->next->prev = hp->prev;
4941 /* make sure that the bucket chain header that
4942 the deleted guy was on points to the right thing afterwards. */
4943 if (hp == *hp->bucket_hdr)
4944 *hp->bucket_hdr = hp->next;
4946 free (hp);
4950 * return hash function on name. must be compatible with the one
4951 * computed a step at a time, elsewhere
4953 static int
4954 hashf (name, len, hashsize)
4955 const U_CHAR *name;
4956 int len;
4957 int hashsize;
4959 int r = 0;
4961 while (len--)
4962 r = HASHSTEP (r, *name++);
4964 return MAKE_POS (r) % hashsize;
4967 /* Dump all macro definitions as #defines to stdout. */
4969 static void
4970 dump_all_macros ()
4972 int bucket;
4974 for (bucket = 0; bucket < HASHSIZE; bucket++) {
4975 HASHNODE *hp;
4977 for (hp = hashtab[bucket]; hp; hp= hp->next) {
4978 if (hp->type == T_MACRO) {
4979 DEFINITION *defn = hp->value.defn;
4980 struct reflist *ap;
4981 int offset;
4982 int concat;
4985 /* Print the definition of the macro HP. */
4987 printf ("#define %s", hp->name);
4988 if (defn->nargs >= 0) {
4989 int i;
4991 printf ("(");
4992 for (i = 0; i < defn->nargs; i++) {
4993 dump_arg_n (defn, i);
4994 if (i + 1 < defn->nargs)
4995 printf (", ");
4997 printf (")");
5000 printf (" ");
5002 offset = 0;
5003 concat = 0;
5004 for (ap = defn->pattern; ap != NULL; ap = ap->next) {
5005 dump_defn_1 (defn->expansion, offset, ap->nchars);
5006 if (ap->nchars != 0)
5007 concat = 0;
5008 offset += ap->nchars;
5009 if (ap->stringify)
5010 printf (" #");
5011 if (ap->raw_before && !concat)
5012 printf (" ## ");
5013 concat = 0;
5014 dump_arg_n (defn, ap->argno);
5015 if (ap->raw_after) {
5016 printf (" ## ");
5017 concat = 1;
5020 dump_defn_1 (defn->expansion, offset, defn->length - offset);
5021 printf ("\n");
5027 /* Output to stdout a substring of a macro definition.
5028 BASE is the beginning of the definition.
5029 Output characters START thru LENGTH.
5030 Discard newlines outside of strings, thus
5031 converting funny-space markers to ordinary spaces. */
5032 static void
5033 dump_defn_1 (base, start, length)
5034 const U_CHAR *base;
5035 int start;
5036 int length;
5038 const U_CHAR *p = base + start;
5039 const U_CHAR *limit = base + start + length;
5041 while (p < limit) {
5042 if (*p != '\n')
5043 putchar (*p);
5044 else if (*p == '\"' || *p =='\'') {
5045 const U_CHAR *p1 = skip_quoted_string (p, limit, 0, 0, 0, 0);
5046 fwrite (p, p1 - p, 1, stdout);
5047 p = p1 - 1;
5049 p++;
5053 /* Print the name of argument number ARGNUM of macro definition DEFN.
5054 Recall that DEFN->argnames contains all the arg names
5055 concatenated in reverse order with comma-space in between. */
5056 static void
5057 dump_arg_n (defn, argnum)
5058 DEFINITION *defn;
5059 int argnum;
5061 const U_CHAR *p = defn->argnames;
5062 while (argnum + 1 < defn->nargs) {
5063 p = (const U_CHAR *) strchr ((const char *)p, ' ') + 1;
5064 argnum++;
5067 while (*p && *p != ',') {
5068 putchar (*p);
5069 p++;
5073 /* Initialize the built-in macros. */
5074 #define DSC(x) U x, sizeof x - 1
5075 #define install_spec(name, type) \
5076 install(DSC(name), type, -1);
5077 #define install_value(name, val) \
5078 hp = install(DSC(name), T_CONST, -1); hp->value.cpval = val;
5079 static void
5080 initialize_builtins ()
5082 HASHNODE *hp;
5084 install_spec ("__BASE_FILE__", T_BASE_FILE);
5085 install_spec ("__DATE__", T_DATE);
5086 install_spec ("__FILE__", T_FILE);
5087 install_spec ("__TIME__", T_TIME);
5088 install_spec ("__VERSION__", T_VERSION);
5089 install_spec ("__INCLUDE_LEVEL__", T_INCLUDE_LEVEL);
5090 install_spec ("__LINE__", T_SPECLINE);
5092 #ifndef NO_BUILTIN_SIZE_TYPE
5093 install_value ("__SIZE_TYPE__", SIZE_TYPE);
5094 #endif
5095 #ifndef NO_BUILTIN_PTRDIFF_TYPE
5096 install_value ("__PTRDIFF_TYPE__", PTRDIFF_TYPE);
5097 #endif
5098 #ifndef NO_BUILTIN_WCHAR_TYPE
5099 install_value ("__WCHAR_TYPE__", WCHAR_TYPE);
5100 #endif
5101 #ifndef NO_BUILTIN_WINT_TYPE
5102 install_value ("__WINT_TYPE__", WINT_TYPE);
5103 #endif
5104 install_value ("__REGISTER_PREFIX__", REGISTER_PREFIX);
5105 install_value ("__USER_LABEL_PREFIX__", user_label_prefix);
5107 #undef DSC
5108 #undef install_spec
5109 #undef install_value
5111 /* Common handler of command line directives -U, -D and -A. */
5112 static void
5113 run_directive (str, len, type)
5114 const char *str;
5115 size_t len;
5116 enum node_type type;
5118 struct directive *kt;
5119 FILE_BUF *ip = &instack[++indepth];
5120 ip->fname = "*command line*";
5122 ip->buf = ip->bufp = (U_CHAR *) str;
5123 ip->length = len;
5124 ip->lineno = 1;
5125 ip->macro = 0;
5126 ip->free_ptr = 0;
5127 ip->if_stack = if_stack;
5129 for (kt = directive_table; kt->type != type; kt++)
5132 (*kt->func) ((U_CHAR *) str, (U_CHAR *) str + len, NULL);
5133 --indepth;
5136 /* Handle the -D option. If STR is just an identifier, define it with
5137 * value 1. If STR has anything after the identifier, then it should
5138 * be identifier-space-definition. */
5139 static void
5140 make_definition (str)
5141 const char *str;
5143 char *buf, *p;
5144 size_t count;
5146 /* Copy the entire option so we can modify it.
5147 Change the first "=" in the string to a space. If there is none,
5148 tack " 1" on the end. */
5150 /* Length including the null. */
5151 count = strlen (str);
5152 buf = (char *) alloca (count + 2);
5153 memcpy (buf, str, count);
5155 p = strchr (str, '=');
5156 if (p)
5157 buf[p - str] = ' ';
5158 else
5160 buf[count++] = ' ';
5161 buf[count++] = '1';
5164 run_directive (buf, count, T_DEFINE);
5167 /* Handle the -U option. */
5168 static void
5169 make_undef (str)
5170 const char *str;
5172 run_directive (str, strlen (str), T_UNDEF);
5175 /* Handles the #assert (-A) and #unassert (-A-) command line options. */
5176 static void
5177 make_assertion (str)
5178 const char *str;
5180 enum node_type type = T_ASSERT;
5181 size_t count;
5182 const char *p;
5184 if (*str == '-')
5186 str++;
5187 type = T_UNASSERT;
5190 count = strlen (str);
5191 p = strchr (str, '=');
5192 if (p)
5194 /* Copy the entire option so we can modify it. Change the first
5195 "=" in the string to a '(', and tack a ')' on the end. */
5196 char *buf = (char *) alloca (count + 1);
5198 memcpy (buf, str, count);
5199 buf[p - str] = '(';
5200 buf[count++] = ')';
5201 str = buf;
5204 run_directive (str, count, type);
5207 /* Get the file-mode and data size of the file open on FD
5208 and store them in *MODE_POINTER and *SIZE_POINTER. */
5210 static int
5211 file_size_and_mode (fd, mode_pointer, size_pointer)
5212 int fd;
5213 int *mode_pointer;
5214 long *size_pointer;
5216 struct stat sbuf;
5218 if (fstat (fd, &sbuf) < 0) return -1;
5219 if (mode_pointer) *mode_pointer = sbuf.st_mode;
5220 if (size_pointer) *size_pointer = sbuf.st_size;
5221 return 0;