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