* gcc.dg/cpp/sysmac1.c,sysmac2.c: Return to original file.
[official-gcc.git] / gcc / tradcpp.c
blobbfb12027b83dead805a23a3663758e8bf97f5492
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;
852 /* For -M, add this file to the dependencies. */
853 if (print_deps)
854 deps_add_dep (deps, pend[i].arg);
856 finclude (fd, pend[i].arg, &outbuf);
858 indepth--;
859 no_output--;
861 /* Pending directives no longer needed. */
862 free ((PTR) pend);
864 /* Create an input stack level for the main input file
865 and copy the entire contents of the file into it. */
867 fp = &instack[++indepth];
869 /* JF check for stdin */
870 if (in_fname == NULL || *in_fname == 0) {
871 in_fname = "";
872 f = 0;
873 } else if ((f = open (in_fname, O_RDONLY, 0666)) < 0)
874 goto sys_error;
876 if (file_size_and_mode (f, &st_mode, &st_size))
877 goto sys_error;
878 fp->fname = in_fname;
879 fp->lineno = 1;
880 /* JF all this is mine about reading pipes and ttys */
881 if (!S_ISREG (st_mode)) {
882 /* Read input from a file that is not a normal disk file.
883 We cannot preallocate a buffer with the correct size,
884 so we must read in the file a piece at the time and make it bigger. */
885 int size;
886 int bsize;
887 int cnt;
888 U_CHAR *bufp;
890 bsize = 2000;
891 size = 0;
892 fp->buf = (U_CHAR *) xmalloc (bsize + 2);
893 bufp = fp->buf;
894 for (;;) {
895 cnt = read (f, bufp, bsize - size);
896 if (cnt < 0) goto sys_error; /* error! */
897 if (cnt == 0) break; /* End of file */
898 size += cnt;
899 bufp += cnt;
900 if (bsize == size) { /* Buffer is full! */
901 bsize *= 2;
902 fp->buf = (U_CHAR *) xrealloc (fp->buf, bsize + 2);
903 bufp = fp->buf + size; /* May have moved */
906 fp->length = size;
907 } else {
908 /* Read a file whose size we can determine in advance.
909 For the sake of VMS, st_size is just an upper bound. */
910 long i;
911 fp->length = 0;
912 fp->buf = (U_CHAR *) xmalloc (st_size + 2);
914 while (st_size > 0) {
915 i = read (f, fp->buf + fp->length, st_size);
916 if (i <= 0) {
917 if (i == 0) break;
918 goto sys_error;
920 fp->length += i;
921 st_size -= i;
924 fp->bufp = fp->buf;
925 fp->if_stack = if_stack;
927 /* Make sure data ends with a newline. And put a null after it. */
929 if (fp->length > 0 && fp->buf[fp->length-1] != '\n')
930 fp->buf[fp->length++] = '\n';
931 fp->buf[fp->length] = '\0';
933 /* Now that we know the input file is valid, open the output. */
935 if (!out_fname || !strcmp (out_fname, ""))
936 out_fname = "stdout";
937 else if (! freopen (out_fname, "w", stdout))
938 pfatal_with_name (out_fname);
940 output_line_command (fp, &outbuf, 0, same_file);
942 /* Scan the input, processing macros and directives. */
944 rescan (&outbuf, 0);
946 /* Now we have processed the entire input
947 Write whichever kind of output has been requested. */
950 if (dump_macros)
951 dump_all_macros ();
952 else if (! inhibit_output)
953 if (write (fileno (stdout), outbuf.buf, outbuf.bufp - outbuf.buf) < 0)
954 fatal ("I/O error on output");
956 /* Don't write the deps file if preprocessing has failed. */
957 if (print_deps && errors == 0)
958 output_deps ();
960 /* Destruct the deps object. */
961 deps_free (deps);
963 if (ferror (stdout))
964 fatal ("I/O error on output");
966 if (errors)
967 exit (FATAL_EXIT_CODE);
968 exit (SUCCESS_EXIT_CODE);
970 sys_error:
971 pfatal_with_name (in_fname);
974 /* Set up dependency-file output. */
975 static void
976 init_dependency_output ()
978 char *spec, *s, *output_file;
980 /* Either of two environment variables can specify output of deps.
981 Its value is either "OUTPUT_FILE" or "OUTPUT_FILE DEPS_TARGET",
982 where OUTPUT_FILE is the file to write deps info to
983 and DEPS_TARGET is the target to mention in the deps. */
985 if (print_deps == 0)
987 spec = getenv ("DEPENDENCIES_OUTPUT");
988 if (spec)
989 print_deps = 1;
990 else
992 spec = getenv ("SUNPRO_DEPENDENCIES");
993 if (spec)
994 print_deps = 2;
995 else
996 return;
999 /* Find the space before the DEPS_TARGET, if there is one. */
1000 s = strchr (spec, ' ');
1001 if (s)
1003 /* Let the caller perform MAKE quoting. */
1004 deps_add_target (deps, s + 1, 0);
1005 output_file = (char *) xmalloc (s - spec + 1);
1006 memcpy (output_file, spec, s - spec);
1007 output_file[s - spec] = 0;
1009 else
1010 output_file = spec;
1012 /* Command line overrides environment variables. */
1013 if (deps_file == 0)
1014 deps_file = output_file;
1015 deps_append = 1;
1018 /* If dependencies go to standard output, or -MG is used, we should
1019 suppress output. The user may be requesting other stuff to
1020 stdout, with -dM, -v etc. We let them shoot themselves in the
1021 foot. */
1022 if (deps_file == 0 || deps_missing_files)
1023 inhibit_output = 1;
1026 /* Use mkdeps.c to output dependency information. */
1027 static void
1028 output_deps ()
1030 /* Stream on which to print the dependency information. */
1031 FILE *deps_stream = 0;
1032 const char *deps_mode = deps_append ? "a" : "w";
1034 if (deps_file == 0)
1035 deps_stream = stdout;
1036 else
1038 deps_stream = fopen (deps_file, deps_mode);
1039 if (deps_stream == 0)
1041 error_from_errno (deps_file);
1042 return;
1046 deps_write (deps, deps_stream, 72);
1048 if (print_deps_phony_targets)
1049 deps_phony_targets (deps, deps_stream);
1051 /* Don't close stdout. */
1052 if (deps_file)
1054 if (ferror (deps_stream) || fclose (deps_stream) != 0)
1055 fatal ("I/O error on output");
1059 /* Move all backslash-newline pairs out of embarrassing places.
1060 Exchange all such pairs following BP
1061 with any potentially-embarrasing characters that follow them.
1062 Potentially-embarrassing characters are / and *
1063 (because a backslash-newline inside a comment delimiter
1064 would cause it not to be recognized). */
1065 static void
1066 newline_fix (bp)
1067 U_CHAR *bp;
1069 register U_CHAR *p = bp;
1070 register int count = 0;
1072 /* First count the backslash-newline pairs here. */
1074 while (*p++ == '\\' && *p++ == '\n')
1075 count++;
1077 p = bp + count * 2;
1079 /* Exit if what follows the backslash-newlines is not embarrassing. */
1081 if (count == 0 || (*p != '/' && *p != '*'))
1082 return;
1084 /* Copy all potentially embarrassing characters
1085 that follow the backslash-newline pairs
1086 down to where the pairs originally started. */
1088 while (*p == '*' || *p == '/')
1089 *bp++ = *p++;
1091 /* Now write the same number of pairs after the embarrassing chars. */
1092 while (count-- > 0) {
1093 *bp++ = '\\';
1094 *bp++ = '\n';
1098 /* Like newline_fix but for use within a directive-name.
1099 Move any backslash-newlines up past any following symbol constituents. */
1100 static void
1101 name_newline_fix (bp)
1102 U_CHAR *bp;
1104 register U_CHAR *p = bp;
1105 register int count = 0;
1107 /* First count the backslash-newline pairs here. */
1109 while (*p++ == '\\' && *p++ == '\n')
1110 count++;
1112 p = bp + count * 2;
1114 /* What follows the backslash-newlines is not embarrassing. */
1116 if (count == 0 || !is_idchar (*p))
1117 return;
1119 /* Copy all potentially embarrassing characters
1120 that follow the backslash-newline pairs
1121 down to where the pairs originally started. */
1123 while (is_idchar (*p))
1124 *bp++ = *p++;
1126 /* Now write the same number of pairs after the embarrassing chars. */
1127 while (count-- > 0) {
1128 *bp++ = '\\';
1129 *bp++ = '\n';
1134 * The main loop of the program.
1136 * Read characters from the input stack, transferring them to the
1137 * output buffer OP.
1139 * Macros are expanded and push levels on the input stack.
1140 * At the end of such a level it is popped off and we keep reading.
1141 * At the end of any other kind of level, we return.
1142 * #-directives are handled, except within macros.
1144 * If OUTPUT_MARKS is nonzero, keep Newline markers found in the input
1145 * and insert them when appropriate. This is set while scanning macro
1146 * arguments before substitution. It is zero when scanning for final output.
1147 * There are three types of Newline markers:
1148 * * Newline - follows a macro name that was not expanded
1149 * because it appeared inside an expansion of the same macro.
1150 * This marker prevents future expansion of that identifier.
1151 * When the input is rescanned into the final output, these are deleted.
1152 * These are also deleted by ## concatenation.
1153 * * Newline Space (or Newline and any other whitespace character)
1154 * stands for a place that tokens must be separated or whitespace
1155 * is otherwise desirable, but where the ANSI standard specifies there
1156 * is no whitespace. This marker turns into a Space (or whichever other
1157 * whitespace char appears in the marker) in the final output,
1158 * but it turns into nothing in an argument that is stringified with #.
1159 * Such stringified arguments are the only place where the ANSI standard
1160 * specifies with precision that whitespace may not appear.
1162 * During this function, IP->bufp is kept cached in IBP for speed of access.
1163 * Likewise, OP->bufp is kept in OBP. Before calling a subroutine
1164 * IBP, IP and OBP must be copied back to memory. IP and IBP are
1165 * copied back with the RECACHE macro. OBP must be copied back from OP->bufp
1166 * explicitly, and before RECACHE, since RECACHE uses OBP.
1169 static void
1170 rescan (op, output_marks)
1171 FILE_BUF *op;
1172 int output_marks;
1174 /* Character being scanned in main loop. */
1175 register U_CHAR c;
1177 /* Length of pending accumulated identifier. */
1178 register int ident_length = 0;
1180 /* Hash code of pending accumulated identifier. */
1181 register int hash = 0;
1183 /* Current input level (&instack[indepth]). */
1184 FILE_BUF *ip;
1186 /* Pointer for scanning input. */
1187 register U_CHAR *ibp;
1189 /* Pointer to end of input. End of scan is controlled by LIMIT. */
1190 register U_CHAR *limit;
1192 /* Pointer for storing output. */
1193 register U_CHAR *obp;
1195 /* REDO_CHAR is nonzero if we are processing an identifier
1196 after backing up over the terminating character.
1197 Sometimes we process an identifier without backing up over
1198 the terminating character, if the terminating character
1199 is not special. Backing up is done so that the terminating character
1200 will be dispatched on again once the identifier is dealt with. */
1201 int redo_char = 0;
1203 /* 1 if within an identifier inside of which a concatenation
1204 marker (Newline -) has been seen. */
1205 int concatenated = 0;
1207 /* While scanning a comment or a string constant,
1208 this records the line it started on, for error messages. */
1209 int start_line;
1211 /* Record position of last `real' newline. */
1212 U_CHAR *beg_of_line;
1214 /* Pop the innermost input stack level, assuming it is a macro expansion. */
1216 #define POPMACRO \
1217 do { ip->macro->type = T_MACRO; \
1218 if (ip->free_ptr) free (ip->free_ptr); \
1219 --indepth; } while (0)
1221 /* Reload `rescan's local variables that describe the current
1222 level of the input stack. */
1224 #define RECACHE \
1225 do { ip = &instack[indepth]; \
1226 ibp = ip->bufp; \
1227 limit = ip->buf + ip->length; \
1228 op->bufp = obp; \
1229 check_expand (op, limit - ibp); \
1230 beg_of_line = 0; \
1231 obp = op->bufp; } while (0)
1233 if (no_output && instack[indepth].fname != 0)
1234 skip_if_group (&instack[indepth], 1);
1236 obp = op->bufp;
1237 RECACHE;
1238 beg_of_line = ibp;
1240 /* Our caller must always put a null after the end of
1241 the input at each input stack level. */
1242 if (*limit != 0)
1243 abort ();
1245 while (1) {
1246 c = *ibp++;
1247 *obp++ = c;
1249 switch (c) {
1250 case '\\':
1251 if (ibp >= limit)
1252 break;
1253 if (*ibp == '\n') {
1254 /* Always merge lines ending with backslash-newline,
1255 even in middle of identifier. */
1256 ++ibp;
1257 ++ip->lineno;
1258 --obp; /* remove backslash from obuf */
1259 break;
1261 /* Otherwise, backslash suppresses specialness of following char,
1262 so copy it here to prevent the switch from seeing it.
1263 But first get any pending identifier processed. */
1264 if (ident_length > 0)
1265 goto specialchar;
1266 *obp++ = *ibp++;
1267 break;
1269 case '#':
1270 /* If this is expanding a macro definition, don't recognize
1271 preprocessor directives. */
1272 if (ip->macro != 0)
1273 goto randomchar;
1274 if (ident_length)
1275 goto specialchar;
1277 /* # keyword: a # must be the first char on the line */
1278 if (beg_of_line == 0)
1279 goto randomchar;
1280 if (beg_of_line + 1 != ibp)
1281 goto randomchar;
1283 /* This # can start a directive. */
1285 --obp; /* Don't copy the '#' */
1287 ip->bufp = ibp;
1288 op->bufp = obp;
1289 if (! handle_directive (ip, op)) {
1290 #ifdef USE_C_ALLOCA
1291 alloca (0);
1292 #endif
1293 /* Not a known directive: treat it as ordinary text.
1294 IP, OP, IBP, etc. have not been changed. */
1295 if (no_output && instack[indepth].fname) {
1296 /* If not generating expanded output,
1297 what we do with ordinary text is skip it.
1298 Discard everything until next # directive. */
1299 skip_if_group (&instack[indepth], 1);
1300 RECACHE;
1301 beg_of_line = ibp;
1302 break;
1304 ++obp; /* Copy the '#' after all */
1305 goto randomchar;
1307 #ifdef USE_C_ALLOCA
1308 alloca (0);
1309 #endif
1310 /* A # directive has been successfully processed. */
1311 /* If not generating expanded output, ignore everything until
1312 next # directive. */
1313 if (no_output && instack[indepth].fname)
1314 skip_if_group (&instack[indepth], 1);
1315 obp = op->bufp;
1316 RECACHE;
1317 beg_of_line = ibp;
1318 break;
1320 case '\"': /* skip quoted string */
1321 case '\'':
1322 /* A single quoted string is treated like a double -- some
1323 programs (e.g., troff) are perverse this way */
1325 if (ident_length)
1326 goto specialchar;
1328 start_line = ip->lineno;
1330 /* Skip ahead to a matching quote. */
1332 while (1) {
1333 if (ibp >= limit) {
1334 if (ip->macro != 0) {
1335 /* try harder: this string crosses a macro expansion boundary */
1336 POPMACRO;
1337 RECACHE;
1338 continue;
1340 break;
1342 *obp++ = *ibp;
1343 switch (*ibp++) {
1344 case '\n':
1345 ++ip->lineno;
1346 ++op->lineno;
1347 /* Traditionally, end of line ends a string constant with no error.
1348 So exit the loop and record the new line. */
1349 beg_of_line = ibp;
1350 goto while2end;
1352 case '\\':
1353 if (ibp >= limit)
1354 break;
1355 if (*ibp == '\n') {
1356 /* Backslash newline is replaced by nothing at all,
1357 but keep the line counts correct. */
1358 --obp;
1359 ++ibp;
1360 ++ip->lineno;
1361 } else {
1362 /* ANSI stupidly requires that in \\ the second \
1363 is *not* prevented from combining with a newline. */
1364 while (*ibp == '\\' && ibp[1] == '\n') {
1365 ibp += 2;
1366 ++ip->lineno;
1368 *obp++ = *ibp++;
1370 break;
1372 case '\"':
1373 case '\'':
1374 if (ibp[-1] == c)
1375 goto while2end;
1376 break;
1379 while2end:
1380 break;
1382 case '/':
1383 if (*ibp == '\\' && ibp[1] == '\n')
1384 newline_fix (ibp);
1385 /* Don't look for comments inside a macro definition. */
1386 if (ip->macro != 0)
1387 goto randomchar;
1388 /* A comment constitutes white space, so it can terminate an identifier.
1389 Process the identifier, if any. */
1390 if (ident_length)
1391 goto specialchar;
1393 if (*ibp != '*')
1394 goto randomchar;
1396 /* We have a comment. Skip it, optionally copying it to output. */
1398 start_line = ip->lineno;
1400 ++ibp; /* Skip the star. */
1402 /* In K+R C, a comment is equivalent to nothing. Note that we
1403 already output the slash; we might not want it. */
1404 if (! put_out_comments)
1405 obp--;
1406 else
1407 *obp++ = '*';
1410 U_CHAR *before_bp = ibp;
1412 while (ibp < limit) {
1413 switch (*ibp++) {
1414 case '/':
1415 if (warn_comments && ibp < limit && *ibp == '*')
1416 warning("`/*' within comment");
1417 break;
1418 case '*':
1419 if (*ibp == '\\' && ibp[1] == '\n')
1420 newline_fix (ibp);
1421 if (ibp >= limit || *ibp == '/')
1422 goto comment_end;
1423 break;
1424 case '\n':
1425 ++ip->lineno;
1426 /* Copy the newline into the output buffer, in order to
1427 avoid the pain of a #line every time a multiline comment
1428 is seen. */
1429 if (!put_out_comments)
1430 *obp++ = '\n';
1431 ++op->lineno;
1434 comment_end:
1436 if (ibp >= limit)
1437 error_with_line (line_for_error (start_line),
1438 "unterminated comment");
1439 else {
1440 ibp++;
1441 if (put_out_comments) {
1442 memcpy (obp, before_bp, ibp - before_bp);
1443 obp += ibp - before_bp;
1447 break;
1449 case '0': case '1': case '2': case '3': case '4':
1450 case '5': case '6': case '7': case '8': case '9':
1451 /* If digit is not part of identifier, it starts a number,
1452 which means that following letters are not an identifier.
1453 "0x5" does not refer to an identifier "x5".
1454 So copy all alphanumerics that follow without accumulating
1455 as an identifier. Periods also, for sake of "3.e7". */
1457 if (ident_length == 0) {
1458 while (ibp < limit) {
1459 while (ibp < limit && ibp[0] == '\\' && ibp[1] == '\n') {
1460 ++ip->lineno;
1461 ibp += 2;
1463 c = *ibp++;
1464 if (!ISALNUM (c) && c != '.' && c != '_') {
1465 --ibp;
1466 break;
1468 *obp++ = c;
1469 /* A sign can be part of a preprocessing number
1470 if it follows an e. */
1471 if (c == 'e' || c == 'E') {
1472 while (ibp < limit && ibp[0] == '\\' && ibp[1] == '\n') {
1473 ++ip->lineno;
1474 ibp += 2;
1476 if (ibp < limit && (*ibp == '+' || *ibp == '-')) {
1477 *obp++ = *ibp++;
1478 /* Traditional C does not let the token go past the sign. */
1479 break;
1483 break;
1485 /* fall through */
1487 case '_':
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 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
1494 case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
1495 case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
1496 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
1497 case 'Y': case 'Z':
1498 ident_length++;
1499 /* Compute step of hash function, to avoid a proc call on every token */
1500 hash = HASHSTEP (hash, c);
1501 break;
1503 case '\n':
1504 /* If reprocessing a macro expansion, newline is a special marker. */
1505 if (ip->macro != 0) {
1506 /* Newline White is a "funny space" to separate tokens that are
1507 supposed to be separate but without space between.
1508 Here White means any horizontal whitespace character.
1509 Newline - marks a recursive macro use that is not
1510 supposed to be expandable. */
1512 if (*ibp == '-') {
1513 /* Newline - inhibits expansion of preceding token.
1514 If expanding a macro arg, we keep the newline -.
1515 In final output, it is deleted. */
1516 if (! concatenated) {
1517 ident_length = 0;
1518 hash = 0;
1520 ibp++;
1521 if (!output_marks) {
1522 obp--;
1523 } else {
1524 /* If expanding a macro arg, keep the newline -. */
1525 *obp++ = '-';
1527 } else if (is_space (*ibp)) {
1528 /* Newline Space does not prevent expansion of preceding token
1529 so expand the preceding token and then come back. */
1530 if (ident_length > 0)
1531 goto specialchar;
1533 /* If generating final output, newline space makes a space. */
1534 if (!output_marks) {
1535 obp[-1] = *ibp++;
1536 /* And Newline Newline makes a newline, so count it. */
1537 if (obp[-1] == '\n')
1538 op->lineno++;
1539 } else {
1540 /* If expanding a macro arg, keep the newline space.
1541 If the arg gets stringified, newline space makes nothing. */
1542 *obp++ = *ibp++;
1544 } else abort (); /* Newline followed by something random? */
1545 break;
1548 /* If there is a pending identifier, handle it and come back here. */
1549 if (ident_length > 0)
1550 goto specialchar;
1552 beg_of_line = ibp;
1554 /* Update the line counts and output a #line if necessary. */
1555 ++ip->lineno;
1556 ++op->lineno;
1557 if (ip->lineno != op->lineno) {
1558 op->bufp = obp;
1559 output_line_command (ip, op, 1, same_file);
1560 check_expand (op, ip->length - (ip->bufp - ip->buf));
1561 obp = op->bufp;
1563 break;
1565 /* Come here either after (1) a null character that is part of the input
1566 or (2) at the end of the input, because there is a null there. */
1567 case 0:
1568 if (ibp <= limit)
1569 /* Our input really contains a null character. */
1570 goto randomchar;
1572 /* At end of a macro-expansion level, pop it and read next level. */
1573 if (ip->macro != 0) {
1574 obp--;
1575 ibp--;
1576 /* If we have an identifier that ends here, process it now, so
1577 we get the right error for recursion. */
1578 if (ident_length && ! is_idchar (*instack[indepth - 1].bufp)) {
1579 redo_char = 1;
1580 goto randomchar;
1582 POPMACRO;
1583 RECACHE;
1584 break;
1587 /* If we don't have a pending identifier,
1588 return at end of input. */
1589 if (ident_length == 0) {
1590 obp--;
1591 ibp--;
1592 op->bufp = obp;
1593 ip->bufp = ibp;
1594 goto ending;
1597 /* If we do have a pending identifier, just consider this null
1598 a special character and arrange to dispatch on it again.
1599 The second time, IDENT_LENGTH will be zero so we will return. */
1601 /* Fall through */
1603 specialchar:
1605 /* Handle the case of a character such as /, ', " or null
1606 seen following an identifier. Back over it so that
1607 after the identifier is processed the special char
1608 will be dispatched on again. */
1610 ibp--;
1611 obp--;
1612 redo_char = 1;
1614 default:
1616 randomchar:
1618 if (ident_length > 0) {
1619 register HASHNODE *hp;
1621 /* We have just seen an identifier end. If it's a macro, expand it.
1623 IDENT_LENGTH is the length of the identifier
1624 and HASH is its hash code.
1626 The identifier has already been copied to the output,
1627 so if it is a macro we must remove it.
1629 If REDO_CHAR is 0, the char that terminated the identifier
1630 has been skipped in the output and the input.
1631 OBP-IDENT_LENGTH-1 points to the identifier.
1632 If the identifier is a macro, we must back over the terminator.
1634 If REDO_CHAR is 1, the terminating char has already been
1635 backed over. OBP-IDENT_LENGTH points to the identifier. */
1637 for (hp = hashtab[MAKE_POS (hash) % HASHSIZE]; hp != NULL;
1638 hp = hp->next) {
1640 if (hp->length == ident_length) {
1641 U_CHAR *obufp_before_macroname;
1642 int op_lineno_before_macroname;
1643 register int i = ident_length;
1644 register U_CHAR *p = hp->name;
1645 register U_CHAR *q = obp - i;
1647 if (! redo_char)
1648 q--;
1650 do { /* All this to avoid a strncmp () */
1651 if (*p++ != *q++)
1652 goto hashcollision;
1653 } while (--i);
1655 /* We found a use of a macro name.
1656 see if the context shows it is a macro call. */
1658 /* Back up over terminating character if not already done. */
1659 if (! redo_char) {
1660 ibp--;
1661 obp--;
1664 obufp_before_macroname = obp - ident_length;
1665 op_lineno_before_macroname = op->lineno;
1667 /* If macro wants an arglist, verify that a '(' follows.
1668 first skip all whitespace, copying it to the output
1669 after the macro name. Then, if there is no '(',
1670 decide this is not a macro call and leave things that way. */
1671 if (hp->type == T_MACRO && hp->value.defn->nargs >= 0)
1673 while (1) {
1674 /* Scan forward over whitespace, copying it to the output. */
1675 if (ibp == limit && ip->macro != 0) {
1676 POPMACRO;
1677 RECACHE;
1679 /* A comment: copy it unchanged or discard it. */
1680 else if (*ibp == '/' && ibp+1 != limit && ibp[1] == '*') {
1681 if (put_out_comments) {
1682 *obp++ = '/';
1683 *obp++ = '*';
1685 ibp += 2;
1686 while (ibp + 1 != limit
1687 && !(ibp[0] == '*' && ibp[1] == '/')) {
1688 /* We need not worry about newline-marks,
1689 since they are never found in comments. */
1690 if (*ibp == '\n') {
1691 /* Newline in a file. Count it. */
1692 ++ip->lineno;
1693 ++op->lineno;
1695 if (put_out_comments)
1696 *obp++ = *ibp++;
1697 else
1698 ibp++;
1700 ibp += 2;
1701 if (put_out_comments) {
1702 *obp++ = '*';
1703 *obp++ = '/';
1706 else if (is_space (*ibp)) {
1707 *obp++ = *ibp++;
1708 if (ibp[-1] == '\n') {
1709 if (ip->macro == 0) {
1710 /* Newline in a file. Count it. */
1711 ++ip->lineno;
1712 ++op->lineno;
1713 } else if (!output_marks) {
1714 /* A newline mark, and we don't want marks
1715 in the output. If it is newline-hyphen,
1716 discard it entirely. Otherwise, it is
1717 newline-whitechar, so keep the whitechar. */
1718 obp--;
1719 if (*ibp == '-')
1720 ibp++;
1721 else {
1722 if (*ibp == '\n')
1723 ++op->lineno;
1724 *obp++ = *ibp++;
1726 } else {
1727 /* A newline mark; copy both chars to the output. */
1728 *obp++ = *ibp++;
1732 else break;
1734 if (*ibp != '(')
1735 break;
1738 /* This is now known to be a macro call.
1739 Discard the macro name from the output,
1740 along with any following whitespace just copied. */
1741 obp = obufp_before_macroname;
1742 op->lineno = op_lineno_before_macroname;
1744 /* Expand the macro, reading arguments as needed,
1745 and push the expansion on the input stack. */
1746 ip->bufp = ibp;
1747 op->bufp = obp;
1748 macroexpand (hp, op);
1750 /* Reexamine input stack, since macroexpand has pushed
1751 a new level on it. */
1752 obp = op->bufp;
1753 RECACHE;
1754 break;
1756 hashcollision:
1758 } /* End hash-table-search loop */
1759 ident_length = hash = 0; /* Stop collecting identifier */
1760 redo_char = 0;
1761 concatenated = 0;
1762 } /* End if (ident_length > 0) */
1763 } /* End switch */
1764 } /* End per-char loop */
1766 /* Come here to return -- but first give an error message
1767 if there was an unterminated successful conditional. */
1768 ending:
1769 if (if_stack != ip->if_stack) {
1770 const char *str;
1771 switch (if_stack->type) {
1772 case T_IF:
1773 str = "if";
1774 break;
1775 case T_IFDEF:
1776 str = "ifdef";
1777 break;
1778 case T_IFNDEF:
1779 str = "ifndef";
1780 break;
1781 case T_ELSE:
1782 str = "else";
1783 break;
1784 case T_ELIF:
1785 str = "elif";
1786 break;
1787 default:
1788 abort ();
1790 error_with_line (line_for_error (if_stack->lineno),
1791 "unterminated #%s conditional", str);
1793 if_stack = ip->if_stack;
1797 * Rescan a string into a temporary buffer and return the result
1798 * as a FILE_BUF. Note this function returns a struct, not a pointer.
1800 * OUTPUT_MARKS nonzero means keep Newline markers found in the input
1801 * and insert such markers when appropriate. See `rescan' for details.
1802 * OUTPUT_MARKS is 1 for macroexpanding a macro argument separately
1803 * before substitution; it is 0 for other uses.
1805 static FILE_BUF
1806 expand_to_temp_buffer (buf, limit, output_marks)
1807 const U_CHAR *buf, *limit;
1808 int output_marks;
1810 register FILE_BUF *ip;
1811 FILE_BUF obuf;
1812 int length = limit - buf;
1813 U_CHAR *buf1;
1814 int odepth = indepth;
1816 if (length < 0)
1817 abort ();
1819 /* Set up the input on the input stack. */
1821 buf1 = (U_CHAR *) alloca (length + 1);
1823 register const U_CHAR *p1 = buf;
1824 register U_CHAR *p2 = buf1;
1826 while (p1 != limit)
1827 *p2++ = *p1++;
1829 buf1[length] = 0;
1831 /* Set up to receive the output. */
1833 obuf.length = length * 2 + 100; /* Usually enough. Why be stingy? */
1834 obuf.bufp = obuf.buf = (U_CHAR *) xmalloc (obuf.length);
1835 obuf.fname = 0;
1836 obuf.macro = 0;
1837 obuf.free_ptr = 0;
1839 CHECK_DEPTH ({return obuf;});
1841 ++indepth;
1843 ip = &instack[indepth];
1844 ip->fname = 0;
1845 ip->macro = 0;
1846 ip->free_ptr = 0;
1847 ip->length = length;
1848 ip->buf = ip->bufp = buf1;
1849 ip->if_stack = if_stack;
1851 ip->lineno = obuf.lineno = 1;
1853 /* Scan the input, create the output. */
1855 rescan (&obuf, output_marks);
1857 /* Pop input stack to original state. */
1858 --indepth;
1860 if (indepth != odepth)
1861 abort ();
1863 /* Record the output. */
1864 obuf.length = obuf.bufp - obuf.buf;
1866 return obuf;
1870 * Process a # directive. Expects IP->bufp to point to the '#', as in
1871 * `#define foo bar'. Passes to the command handler
1872 * (do_define, do_include, etc.): the addresses of the 1st and
1873 * last chars of the command (starting immediately after the #
1874 * keyword), plus op and the keyword table pointer. If the command
1875 * contains comments it is copied into a temporary buffer sans comments
1876 * and the temporary buffer is passed to the command handler instead.
1877 * Likewise for backslash-newlines.
1879 * Returns nonzero if this was a known # directive.
1880 * Otherwise, returns zero, without advancing the input pointer.
1883 static int
1884 handle_directive (ip, op)
1885 FILE_BUF *ip, *op;
1887 register U_CHAR *bp, *cp;
1888 register struct directive *kt;
1889 register int ident_length;
1890 U_CHAR *resume_p;
1892 /* Nonzero means we must copy the entire command
1893 to get rid of comments or backslash-newlines. */
1894 int copy_command = 0;
1896 U_CHAR *ident, *after_ident;
1898 bp = ip->bufp;
1899 /* Skip whitespace and \-newline. */
1900 while (1) {
1901 if (is_nvspace (*bp))
1902 bp++;
1903 else if (*bp == '/' && (newline_fix (bp + 1), bp[1]) == '*') {
1904 ip->bufp = bp;
1905 skip_to_end_of_comment (ip, &ip->lineno);
1906 bp = ip->bufp;
1907 } else if (*bp == '\\' && bp[1] == '\n') {
1908 bp += 2; ip->lineno++;
1909 } else break;
1912 /* Now find end of directive name.
1913 If we encounter a backslash-newline, exchange it with any following
1914 symbol-constituents so that we end up with a contiguous name. */
1916 cp = bp;
1917 while (1) {
1918 if (is_idchar (*cp))
1919 cp++;
1920 else {
1921 if (*cp == '\\' && cp[1] == '\n')
1922 name_newline_fix (cp);
1923 if (is_idchar (*cp))
1924 cp++;
1925 else break;
1928 ident_length = cp - bp;
1929 ident = bp;
1930 after_ident = cp;
1932 /* A line of just `#' becomes blank. */
1934 if (ident_length == 0 && *after_ident == '\n') {
1935 ip->bufp = after_ident;
1936 return 1;
1940 * Decode the keyword and call the appropriate expansion
1941 * routine, after moving the input pointer up to the next line.
1943 for (kt = directive_table; kt->length > 0; kt++) {
1944 if (kt->length == ident_length
1945 && !strncmp (kt->name, (const char *)ident, ident_length)) {
1946 register U_CHAR *buf;
1947 register U_CHAR *limit = ip->buf + ip->length;
1948 int unterminated = 0;
1950 /* Nonzero means do not delete comments within the directive.
1951 #define needs this to detect traditional token paste. */
1952 int keep_comments = kt->type == T_DEFINE;
1954 /* Find the end of this command (first newline not backslashed
1955 and not in a string or comment).
1956 Set COPY_COMMAND if the command must be copied
1957 (it contains a backslash-newline or a comment). */
1959 buf = bp = after_ident;
1960 while (bp < limit) {
1961 register U_CHAR c = *bp++;
1962 switch (c) {
1963 case '\\':
1964 if (bp < limit) {
1965 if (*bp == '\n') {
1966 ip->lineno++;
1967 copy_command = 1;
1969 bp++;
1971 break;
1973 case '\'':
1974 case '\"':
1975 bp = skip_quoted_string (bp - 1, limit, ip->lineno, &ip->lineno, &copy_command, &unterminated);
1976 if (unterminated) {
1977 /* Traditional preprocessing permits unterminated strings. */
1978 ip->bufp = bp;
1979 goto endloop1;
1981 break;
1983 /* <...> is special for #include. */
1984 case '<':
1985 if (kt->type != T_INCLUDE)
1986 break;
1987 while (*bp && *bp != '>') bp++;
1988 break;
1990 case '/':
1991 if (*bp == '\\' && bp[1] == '\n')
1992 newline_fix (bp);
1993 if (*bp == '*') {
1994 U_CHAR *obp = bp - 1;
1995 ip->bufp = bp + 1;
1996 skip_to_end_of_comment (ip, &ip->lineno);
1997 bp = ip->bufp;
1998 /* No need to copy the command because of a comment at the end;
1999 just don't include the comment in the directive. */
2000 if (bp == limit || *bp == '\n') {
2001 bp = obp;
2002 goto endloop1;
2004 /* Don't remove the comments if this is #define. */
2005 if (! keep_comments)
2006 copy_command++;
2008 break;
2010 case '\n':
2011 --bp; /* Point to the newline */
2012 ip->bufp = bp;
2013 goto endloop1;
2016 ip->bufp = bp;
2018 endloop1:
2019 resume_p = ip->bufp;
2020 /* BP is the end of the directive.
2021 RESUME_P is the next interesting data after the directive.
2022 A comment may come between. */
2024 if (copy_command) {
2025 register U_CHAR *xp = buf;
2026 /* Need to copy entire command into temp buffer before dispatching */
2028 cp = (U_CHAR *) alloca (bp - buf + 5); /* room for cmd plus
2029 some slop */
2030 buf = cp;
2032 /* Copy to the new buffer, deleting comments
2033 and backslash-newlines (and whitespace surrounding the latter). */
2035 while (xp < bp) {
2036 register U_CHAR c = *xp++;
2037 *cp++ = c;
2039 switch (c) {
2040 case '\n':
2041 break;
2043 /* <...> is special for #include. */
2044 case '<':
2045 if (kt->type != T_INCLUDE)
2046 break;
2047 while (xp < bp && c != '>') {
2048 c = *xp++;
2049 if (c == '\\' && xp < bp && *xp == '\n')
2050 xp++, ip->lineno++;
2051 else
2052 *cp++ = c;
2054 break;
2056 case '\\':
2057 if (*xp == '\n') {
2058 xp++;
2059 cp--;
2060 if (cp != buf && is_space (cp[-1])) {
2061 while (cp != buf && is_space(cp[-1])) cp--;
2062 cp++;
2063 SKIP_WHITE_SPACE (xp);
2064 } else if (is_space (*xp)) {
2065 *cp++ = *xp++;
2066 SKIP_WHITE_SPACE (xp);
2068 } else {
2069 *cp++ = *xp++;
2071 break;
2073 case '\'':
2074 case '\"':
2076 register const U_CHAR *bp1
2077 = skip_quoted_string (xp - 1, limit, ip->lineno, 0, 0, 0);
2078 while (xp != bp1)
2079 *cp++ = *xp++;
2081 break;
2083 case '/':
2084 if (*xp == '*') {
2085 ip->bufp = xp + 1;
2086 skip_to_end_of_comment (ip, 0);
2087 if (keep_comments)
2088 while (xp != ip->bufp)
2089 *cp++ = *xp++;
2090 /* Delete the slash. */
2091 else
2092 cp--;
2093 xp = ip->bufp;
2098 /* Null-terminate the copy. */
2100 *cp = 0;
2102 else
2103 cp = bp;
2105 ip->bufp = resume_p;
2107 /* Call the appropriate command handler. buf now points to
2108 either the appropriate place in the input buffer, or to
2109 the temp buffer if it was necessary to make one. cp
2110 points to the first char after the contents of the (possibly
2111 copied) command, in either case. */
2112 (*kt->func) (buf, cp, op);
2113 check_expand (op, ip->length - (ip->bufp - ip->buf));
2115 return 1;
2119 return 0;
2122 static const char *const
2123 monthnames[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
2124 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
2127 * expand things like __FILE__. Place the expansion into the output
2128 * buffer *without* rescanning.
2130 static void
2131 special_symbol (hp, op)
2132 HASHNODE *hp;
2133 FILE_BUF *op;
2135 const char *buf;
2136 time_t t;
2137 int i, len;
2138 int true_indepth;
2139 FILE_BUF *ip = NULL;
2140 static struct tm *timebuf = NULL;
2142 int paren = 0; /* For special `defined' keyword */
2144 for (i = indepth; i >= 0; i--)
2145 if (instack[i].fname != NULL) {
2146 ip = &instack[i];
2147 break;
2149 if (ip == NULL)
2150 fatal ("not in any file?!");
2152 switch (hp->type) {
2153 case T_FILE:
2154 case T_BASE_FILE:
2156 const char *string;
2157 if (hp->type == T_FILE)
2158 string = ip->fname;
2159 else
2160 string = instack[0].fname;
2162 if (string)
2164 char *tmp = (char *) alloca (3 + strlen (string));
2165 sprintf (tmp, "\"%s\"", string);
2166 buf = tmp;
2168 else
2169 buf = "";
2171 break;
2174 case T_INCLUDE_LEVEL:
2176 char *tmp = (char *) alloca (8); /* Eigth bytes ought to be more than enough */
2177 true_indepth = 0;
2178 for (i = indepth; i >= 0; i--)
2179 if (instack[i].fname != NULL)
2180 true_indepth++;
2182 sprintf (tmp, "%d", true_indepth - 1);
2183 buf = tmp;
2184 break;
2187 case T_VERSION:
2189 char *tmp = (char *) alloca (3 + strlen (version_string));
2190 sprintf (tmp, "\"%s\"", version_string);
2191 buf = tmp;
2192 break;
2195 case T_CONST:
2196 buf = hp->value.cpval;
2197 break;
2199 case T_SPECLINE:
2201 char *tmp = (char *) alloca (10);
2202 sprintf (tmp, "%d", ip->lineno);
2203 buf = tmp;
2204 break;
2207 case T_DATE:
2208 case T_TIME:
2210 char *tmp = (char *) alloca (20);
2212 if (timebuf == NULL) {
2213 t = time (0);
2214 timebuf = localtime (&t);
2216 if (hp->type == T_DATE)
2217 sprintf (tmp, "\"%s %2d %4d\"", monthnames[timebuf->tm_mon],
2218 timebuf->tm_mday, timebuf->tm_year + 1900);
2219 else
2220 sprintf (tmp, "\"%02d:%02d:%02d\"", timebuf->tm_hour, timebuf->tm_min,
2221 timebuf->tm_sec);
2222 buf = tmp;
2223 break;
2226 case T_SPEC_DEFINED:
2227 buf = " 0 "; /* Assume symbol is not defined */
2228 ip = &instack[indepth];
2229 SKIP_WHITE_SPACE (ip->bufp);
2230 if (*ip->bufp == '(') {
2231 paren++;
2232 ip->bufp++; /* Skip over the paren */
2233 SKIP_WHITE_SPACE (ip->bufp);
2236 if (!is_idstart (*ip->bufp))
2237 goto oops;
2239 HASHNODE *hp = lookup (ip->bufp, -1, -1);
2241 if (hp && hp->type != T_UNUSED && hp->type != T_SPEC_DEFINED)
2242 buf = " 1 ";
2244 while (is_idchar (*ip->bufp))
2245 ++ip->bufp;
2246 SKIP_WHITE_SPACE (ip->bufp);
2247 if (paren) {
2248 if (*ip->bufp != ')')
2249 goto oops;
2250 ++ip->bufp;
2252 break;
2254 oops:
2256 error ("`defined' must be followed by ident or (ident)");
2257 break;
2259 default:
2260 error ("cccp error: invalid special hash type"); /* time for gdb */
2261 abort ();
2263 len = strlen (buf);
2264 check_expand (op, len);
2265 memcpy (op->bufp, buf, len);
2266 op->bufp += len;
2270 /* Routines to handle #directives */
2273 * Process include file by reading it in and calling rescan.
2274 * Expects to see "fname" or <fname> on the input.
2276 static void
2277 do_include (buf, limit, op)
2278 U_CHAR *buf, *limit;
2279 FILE_BUF *op;
2281 char *fname; /* Dynamically allocated fname buffer */
2282 U_CHAR *fbeg, *fend; /* Beginning and end of fname */
2284 struct file_name_list *stackp = include; /* Chain of dirs to search */
2285 struct file_name_list dsp[1]; /* First in chain, if #include "..." */
2286 int flen;
2288 int f; /* file number */
2290 int retried = 0; /* Have already tried macro
2291 expanding the include line*/
2292 FILE_BUF trybuf; /* It got expanded into here */
2293 int system_header_p = 0; /* 0 for "...", 1 for <...> */
2295 f= -1; /* JF we iz paranoid! */
2297 get_filename:
2299 fbeg = buf;
2300 SKIP_WHITE_SPACE (fbeg);
2301 /* Discard trailing whitespace so we can easily see
2302 if we have parsed all the significant chars we were given. */
2303 while (limit != fbeg && is_nvspace (limit[-1])) limit--;
2305 switch (*fbeg++) {
2306 case '\"':
2307 fend = fbeg;
2308 while (fend != limit && *fend != '\"')
2309 fend++;
2310 if (*fend == '\"' && fend + 1 == limit) {
2311 FILE_BUF *fp;
2313 /* We have "filename". Figure out directory this source
2314 file is coming from and put it on the front of the list. */
2316 /* If -I- was specified, don't search current dir, only spec'd ones. */
2317 if (ignore_srcdir) break;
2319 for (fp = &instack[indepth]; fp >= instack; fp--)
2321 size_t n;
2322 const char *ep, *nam;
2324 if ((nam = fp->fname) != NULL) {
2325 /* Found a named file. Figure out dir of the file,
2326 and put it in front of the search list. */
2327 dsp[0].next = stackp;
2328 stackp = dsp;
2329 ep = strrchr (nam, '/');
2330 if (ep != NULL) {
2331 char *f;
2332 n = ep - nam;
2333 f = (char *) alloca (n + 1);
2334 strncpy (f, nam, n);
2335 f[n] = '\0';
2336 dsp[0].fname = f;
2337 if (n > max_include_len) max_include_len = n;
2338 } else {
2339 dsp[0].fname = 0; /* Current directory */
2341 break;
2344 break;
2346 goto fail;
2348 case '<':
2349 fend = fbeg;
2350 while (fend != limit && *fend != '>') fend++;
2351 if (*fend == '>' && fend + 1 == limit) {
2352 system_header_p = 1;
2353 /* If -I-, start with the first -I dir after the -I-. */
2354 if (first_bracket_include)
2355 stackp = first_bracket_include;
2356 break;
2358 goto fail;
2360 default:
2361 fail:
2362 if (retried) {
2363 error ("#include expects \"fname\" or <fname>");
2364 return;
2365 } else {
2366 trybuf = expand_to_temp_buffer (buf, limit, 0);
2367 buf = (U_CHAR *) alloca (trybuf.bufp - trybuf.buf + 1);
2368 memcpy (buf, trybuf.buf, trybuf.bufp - trybuf.buf);
2369 limit = buf + (trybuf.bufp - trybuf.buf);
2370 free (trybuf.buf);
2371 retried++;
2372 goto get_filename;
2376 flen = fend - fbeg;
2377 fname = (char *) alloca (max_include_len + flen + 2);
2378 /* + 2 above for slash and terminating null. */
2380 /* If specified file name is absolute, just open it. */
2382 if (IS_ABSOLUTE_PATHNAME (fbeg)) {
2383 strncpy (fname, (const char *)fbeg, flen);
2384 fname[flen] = 0;
2385 f = open (fname, O_RDONLY, 0666);
2386 } else {
2387 /* Search directory path, trying to open the file.
2388 Copy each filename tried into FNAME. */
2390 for (; stackp; stackp = stackp->next) {
2391 if (stackp->fname) {
2392 strcpy (fname, stackp->fname);
2393 strcat (fname, "/");
2394 fname[strlen (fname) + flen] = 0;
2395 } else {
2396 fname[0] = 0;
2398 strncat (fname, (const char *)fbeg, flen);
2399 if ((f = open (fname, O_RDONLY, 0666)) >= 0)
2400 break;
2404 if (f < 0) {
2405 strncpy (fname, (const char *)fbeg, flen);
2406 fname[flen] = 0;
2407 if (deps_missing_files
2408 && print_deps > (system_header_p || (system_include_depth > 0))) {
2410 /* If requested as a system header, assume it belongs in
2411 the first system header directory. */
2412 if (first_bracket_include)
2413 stackp = first_bracket_include;
2414 else
2415 stackp = include;
2417 if (!system_header_p || IS_ABSOLUTE_PATHNAME (fbeg) || !stackp->fname)
2418 deps_add_dep (deps, fname);
2419 else {
2420 char *p;
2421 int len = strlen(stackp->fname);
2423 p = (char *) alloca (len + flen + 2);
2424 memcpy (p, stackp->fname, len);
2425 p[len++] = '/';
2426 memcpy (p + len, fbeg, flen);
2427 len += flen;
2428 p[len] = '\0';
2429 deps_add_dep (deps, p);
2431 } else if (print_deps
2432 && print_deps <= (system_header_p
2433 || (system_include_depth > 0)))
2434 warning ("No include path in which to find %.*s", flen, fbeg);
2435 else
2436 error_from_errno (fname);
2438 } else {
2440 /* Check to see if this include file is a once-only include file.
2441 If so, give up. */
2443 struct file_name_list* ptr;
2445 for (ptr = dont_repeat_files; ptr; ptr = ptr->next) {
2446 if (!strcmp (ptr->fname, fname)) {
2447 close (f);
2448 return; /* This file was once'd. */
2452 for (ptr = all_include_files; ptr; ptr = ptr->next) {
2453 if (!strcmp (ptr->fname, fname))
2454 break; /* This file was included before. */
2457 if (ptr == 0) {
2458 /* This is the first time for this file. */
2459 /* Add it to list of files included. */
2461 ptr = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
2462 ptr->next = all_include_files;
2463 all_include_files = ptr;
2464 ptr->fname = xstrdup (fname);
2466 /* For -M, add this file to the dependencies. */
2467 if (print_deps > (system_header_p || (system_include_depth > 0)))
2468 deps_add_dep (deps, fname);
2471 if (system_header_p)
2472 system_include_depth++;
2474 /* Actually process the file. */
2475 finclude (f, fname, op);
2477 if (system_header_p)
2478 system_include_depth--;
2480 close (f);
2484 /* Process the contents of include file FNAME, already open on descriptor F,
2485 with output to OP. */
2487 static void
2488 finclude (f, fname, op)
2489 int f;
2490 const char *fname;
2491 FILE_BUF *op;
2493 int st_mode;
2494 long st_size;
2495 long i;
2496 FILE_BUF *fp; /* For input stack frame */
2498 CHECK_DEPTH (return;);
2500 if (file_size_and_mode (f, &st_mode, &st_size))
2501 goto nope;
2503 fp = &instack[indepth + 1];
2504 memset (fp, 0, sizeof (FILE_BUF));
2505 fp->fname = fname;
2506 fp->length = 0;
2507 fp->lineno = 1;
2508 fp->if_stack = if_stack;
2510 if (S_ISREG (st_mode)) {
2511 fp->buf = (U_CHAR *) xmalloc (st_size + 2);
2512 fp->bufp = fp->buf;
2514 /* Read the file contents, knowing that st_size is an upper bound
2515 on the number of bytes we can read. */
2516 while (st_size > 0) {
2517 i = read (f, fp->buf + fp->length, st_size);
2518 if (i <= 0) {
2519 if (i == 0) break;
2520 goto nope;
2522 fp->length += i;
2523 st_size -= i;
2526 else {
2527 /* Cannot count its file size before reading. */
2529 U_CHAR *bufp;
2530 U_CHAR *basep;
2531 int bsize = 2000;
2533 st_size = 0;
2534 basep = (U_CHAR *) xmalloc (bsize + 2);
2535 bufp = basep;
2537 for (;;) {
2538 i = read (f, bufp, bsize - st_size);
2539 if (i < 0)
2540 goto nope; /* error! */
2541 if (i == 0)
2542 break; /* End of file */
2543 st_size += i;
2544 bufp += i;
2545 if (bsize == st_size) { /* Buffer is full! */
2546 bsize *= 2;
2547 basep = (U_CHAR *) xrealloc (basep, bsize + 2);
2548 bufp = basep + st_size; /* May have moved */
2551 fp->buf = basep;
2552 fp->bufp = fp->buf;
2553 fp->length = st_size;
2555 close (f);
2557 /* Make sure data ends with a newline. And put a null after it. */
2559 if (fp->length > 0 && fp->buf[fp->length-1] != '\n')
2560 fp->buf[fp->length++] = '\n';
2561 fp->buf[fp->length] = '\0';
2563 indepth++;
2564 output_line_command (fp, op, 0, enter_file);
2565 rescan (op, 0);
2566 indepth--;
2567 instack[indepth].lineno++;
2568 instack[indepth].bufp++; /* Skip the new line. */
2569 output_line_command (&instack[indepth], op, 0, leave_file);
2570 free (fp->buf);
2571 return;
2573 nope:
2574 perror_with_name (fname);
2575 close (f);
2579 /* Process a #define command.
2580 BUF points to the contents of the #define command, as a continguous string.
2581 LIMIT points to the first character past the end of the definition.
2582 KEYWORD is the keyword-table entry for #define. */
2584 static void
2585 do_define (buf, limit, op)
2586 U_CHAR *buf, *limit;
2587 FILE_BUF *op ATTRIBUTE_UNUSED;
2589 U_CHAR *bp; /* temp ptr into input buffer */
2590 U_CHAR *symname; /* remember where symbol name starts */
2591 int sym_length; /* and how long it is */
2593 DEFINITION *defn;
2594 int arglengths = 0; /* Accumulate lengths of arg names
2595 plus number of args. */
2596 int hashcode;
2598 bp = buf;
2600 while (is_nvspace (*bp))
2601 bp++;
2603 symname = bp; /* remember where it starts */
2604 while (is_idchar (*bp) && bp < limit) {
2605 bp++;
2607 sym_length = bp - symname;
2608 if (sym_length == 0)
2610 error ("invalid macro name");
2611 return;
2613 else if (!is_idstart (*symname)) {
2614 U_CHAR *msg; /* what pain... */
2615 msg = (U_CHAR *) alloca (sym_length + 1);
2616 memcpy (msg, symname, sym_length);
2617 msg[sym_length] = 0;
2618 error ("invalid macro name `%s'", msg);
2619 return;
2620 } else {
2621 if (! strncmp ((const char *)symname, "defined", 7) && sym_length == 7)
2623 error ("\"defined\" cannot be used as a macro name");
2624 return;
2628 /* lossage will occur if identifiers or control keywords are broken
2629 across lines using backslash. This is not the right place to take
2630 care of that. */
2632 if (*bp == '(') {
2633 struct arglist *arg_ptrs = NULL;
2634 int argno = 0;
2636 bp++; /* skip '(' */
2637 SKIP_WHITE_SPACE (bp);
2639 /* Loop over macro argument names. */
2640 while (*bp != ')') {
2641 struct arglist *temp;
2643 temp = (struct arglist *) alloca (sizeof (struct arglist));
2644 temp->name = bp;
2645 temp->next = arg_ptrs;
2646 temp->argno = argno++;
2647 arg_ptrs = temp;
2649 if (!is_idstart (*bp))
2650 warning ("parameter name starts with a digit in #define");
2652 /* Find the end of the arg name. */
2653 while (is_idchar (*bp)) {
2654 bp++;
2656 temp->length = bp - temp->name;
2657 arglengths += temp->length + 2;
2658 SKIP_WHITE_SPACE (bp);
2659 if (temp->length == 0 || (*bp != ',' && *bp != ')')) {
2660 error ("badly punctuated parameter list in #define");
2661 return;
2663 if (*bp == ',') {
2664 bp++;
2665 SKIP_WHITE_SPACE (bp);
2667 if (bp >= limit) {
2668 error ("unterminated parameter list in #define");
2669 return;
2673 ++bp; /* skip paren */
2674 while (is_nvspace (*bp) && bp < limit) /* and leading whitespace */
2675 ++bp;
2676 /* now everything from bp before limit is the definition. */
2677 defn = collect_expansion (bp, limit, argno, arg_ptrs);
2679 /* Now set defn->argnames to the result of concatenating
2680 the argument names in reverse order
2681 with comma-space between them. */
2683 struct arglist *temp;
2684 int i = 0;
2685 U_CHAR *tmp = (U_CHAR *) xmalloc (arglengths + 1);
2687 for (temp = arg_ptrs; temp; temp = temp->next) {
2688 memcpy (&tmp[i], temp->name, temp->length);
2689 i += temp->length;
2690 if (temp->next != 0) {
2691 tmp[i++] = ',';
2692 tmp[i++] = ' ';
2695 tmp[i] = 0;
2696 defn->argnames = tmp;
2699 } else {
2700 /* simple expansion or empty definition; skip leading whitespace */
2701 while (is_nvspace (*bp) && bp < limit)
2702 ++bp;
2703 /* now everything from bp before limit is the definition. */
2704 defn = collect_expansion (bp, limit, -1, 0);
2705 defn->argnames = (const U_CHAR *) "";
2708 hashcode = hashf (symname, sym_length, HASHSIZE);
2711 HASHNODE *hp;
2712 if ((hp = lookup (symname, sym_length, hashcode)) == NULL)
2713 hp = install (symname, sym_length, T_MACRO, hashcode);
2714 else {
2715 if (hp->type != T_MACRO || compare_defs (defn, hp->value.defn))
2716 warning ("\"%.*s\" redefined", sym_length, symname);
2718 /* Replace the old definition. */
2719 hp->type = T_MACRO;
2722 hp->value.defn = defn;
2727 * return zero if two DEFINITIONs are isomorphic
2729 static int
2730 compare_defs (d1, d2)
2731 DEFINITION *d1, *d2;
2733 register struct reflist *a1, *a2;
2734 register U_CHAR *p1 = d1->expansion;
2735 register U_CHAR *p2 = d2->expansion;
2736 int first = 1;
2738 if (d1->nargs != d2->nargs)
2739 return 1;
2740 if (strcmp ((const char *)d1->argnames, (const char *)d2->argnames))
2741 return 1;
2742 for (a1 = d1->pattern, a2 = d2->pattern; a1 && a2;
2743 a1 = a1->next, a2 = a2->next) {
2744 if (!((a1->nchars == a2->nchars
2745 && ! strncmp ((const char *)p1, (const char *)p2, a1->nchars))
2746 || ! comp_def_part (first, p1, a1->nchars, p2, a2->nchars, 0))
2747 || a1->argno != a2->argno
2748 || a1->stringify != a2->stringify
2749 || a1->raw_before != a2->raw_before
2750 || a1->raw_after != a2->raw_after)
2751 return 1;
2752 first = 0;
2753 p1 += a1->nchars;
2754 p2 += a2->nchars;
2756 if (a1 != a2)
2757 return 1;
2758 if (comp_def_part (first, p1, d1->length - (p1 - d1->expansion),
2759 p2, d2->length - (p2 - d2->expansion), 1))
2760 return 1;
2761 return 0;
2764 /* Return 1 if two parts of two macro definitions are effectively different.
2765 One of the parts starts at BEG1 and has LEN1 chars;
2766 the other has LEN2 chars at BEG2.
2767 Any sequence of whitespace matches any other sequence of whitespace.
2768 FIRST means these parts are the first of a macro definition;
2769 so ignore leading whitespace entirely.
2770 LAST means these parts are the last of a macro definition;
2771 so ignore trailing whitespace entirely. */
2772 static int
2773 comp_def_part (first, beg1, len1, beg2, len2, last)
2774 int first;
2775 const U_CHAR *beg1, *beg2;
2776 int len1, len2;
2777 int last;
2779 register const U_CHAR *end1 = beg1 + len1;
2780 register const U_CHAR *end2 = beg2 + len2;
2781 if (first) {
2782 while (beg1 != end1 && is_space (*beg1)) beg1++;
2783 while (beg2 != end2 && is_space (*beg2)) beg2++;
2785 if (last) {
2786 while (beg1 != end1 && is_space (end1[-1])) end1--;
2787 while (beg2 != end2 && is_space (end2[-1])) end2--;
2789 while (beg1 != end1 && beg2 != end2) {
2790 if (is_space (*beg1) && is_space (*beg2)) {
2791 while (beg1 != end1 && is_space (*beg1)) beg1++;
2792 while (beg2 != end2 && is_space (*beg2)) beg2++;
2793 } else if (*beg1 == *beg2) {
2794 beg1++; beg2++;
2795 } else break;
2797 return (beg1 != end1) || (beg2 != end2);
2800 /* Read a replacement list for a macro with parameters.
2801 Build the DEFINITION structure.
2802 Reads characters of text starting at BUF until LIMIT.
2803 ARGLIST specifies the formal parameters to look for
2804 in the text of the definition; NARGS is the number of args
2805 in that list, or -1 for a macro name that wants no argument list.
2806 MACRONAME is the macro name itself (so we can avoid recursive expansion)
2807 and NAMELEN is its length in characters.
2809 Note that comments and backslash-newlines have already been deleted
2810 from the argument. */
2812 /* Leading and trailing Space, Tab, etc. are converted to markers
2813 Newline Space, Newline Tab, etc.
2814 Newline Space makes a space in the final output
2815 but is discarded if stringified. (Newline Tab is similar but
2816 makes a Tab instead.)
2818 If there is no trailing whitespace, a Newline Space is added at the end
2819 to prevent concatenation that would be contrary to the standard. */
2821 static DEFINITION *
2822 collect_expansion (buf, end, nargs, arglist)
2823 U_CHAR *buf, *end;
2824 int nargs;
2825 struct arglist *arglist;
2827 DEFINITION *defn;
2828 register U_CHAR *p, *limit, *lastp, *exp_p;
2829 struct reflist *endpat = NULL;
2830 /* Pointer to first nonspace after last ## seen. */
2831 U_CHAR *concat = 0;
2832 /* Pointer to first nonspace after last single-# seen. */
2833 U_CHAR *stringify = 0;
2834 int maxsize;
2835 int expected_delimiter = '\0';
2837 /* Scan thru the replacement list, ignoring comments and quoted
2838 strings, picking up on the macro calls. It does a linear search
2839 thru the arg list on every potential symbol. Profiling might say
2840 that something smarter should happen. */
2842 if (end < buf)
2843 abort ();
2845 /* Find the beginning of the trailing whitespace. */
2846 /* Find end of leading whitespace. */
2847 limit = end;
2848 p = buf;
2849 while (p < limit && is_space (limit[-1])) limit--;
2850 while (p < limit && is_space (*p)) p++;
2852 /* Allocate space for the text in the macro definition.
2853 Leading and trailing whitespace chars need 2 bytes each.
2854 Each other input char may or may not need 1 byte,
2855 so this is an upper bound.
2856 The extra 2 are for invented trailing newline-marker and final null. */
2857 maxsize = (sizeof (DEFINITION)
2858 + 2 * (end - limit) + 2 * (p - buf)
2859 + (limit - p) + 3);
2860 defn = (DEFINITION *) xcalloc (1, maxsize);
2862 defn->nargs = nargs;
2863 exp_p = defn->expansion = (U_CHAR *) defn + sizeof (DEFINITION);
2864 lastp = exp_p;
2866 p = buf;
2868 /* Convert leading whitespace to Newline-markers. */
2869 while (p < limit && is_space (*p)) {
2870 *exp_p++ = '\n';
2871 *exp_p++ = *p++;
2874 /* Process the main body of the definition. */
2875 while (p < limit) {
2876 int skipped_arg = 0;
2877 register U_CHAR c = *p++;
2879 *exp_p++ = c;
2881 /* In -traditional mode, recognize arguments inside strings and
2882 and character constants, and ignore special properties of #.
2883 Arguments inside strings are considered "stringified", but no
2884 extra quote marks are supplied. */
2885 switch (c) {
2886 case '\'':
2887 case '\"':
2888 if (expected_delimiter != '\0') {
2889 if (c == expected_delimiter)
2890 expected_delimiter = '\0';
2891 } else
2892 expected_delimiter = c;
2893 break;
2895 case '\\':
2896 /* Backslash quotes delimiters and itself, but not macro args. */
2897 if (expected_delimiter != 0 && p < limit
2898 && (*p == expected_delimiter || *p == '\\')) {
2899 *exp_p++ = *p++;
2900 continue;
2902 break;
2904 case '/':
2905 if (expected_delimiter != '\0') /* No comments inside strings. */
2906 break;
2907 if (*p == '*') {
2908 /* If we find a comment that wasn't removed by handle_directive,
2909 this must be -traditional. So replace the comment with
2910 nothing at all. */
2911 exp_p--;
2912 p += 1;
2913 while (p < limit && !(p[-2] == '*' && p[-1] == '/'))
2914 p++;
2916 break;
2919 if (is_idchar (c) && nargs > 0) {
2920 U_CHAR *id_beg = p - 1;
2921 int id_len;
2923 --exp_p;
2924 while (p != limit && is_idchar (*p)) p++;
2925 id_len = p - id_beg;
2927 if (is_idstart (c)) {
2928 register struct arglist *arg;
2930 for (arg = arglist; arg != NULL; arg = arg->next) {
2931 struct reflist *tpat;
2933 if (arg->name[0] == c
2934 && arg->length == id_len
2935 && strncmp ((const char *)arg->name,
2936 (const char *)id_beg, id_len) == 0) {
2937 /* make a pat node for this arg and append it to the end of
2938 the pat list */
2939 tpat = (struct reflist *) xmalloc (sizeof (struct reflist));
2940 tpat->next = NULL;
2941 tpat->raw_before = concat == id_beg;
2942 tpat->raw_after = 0;
2943 tpat->stringify = expected_delimiter != '\0';
2945 if (endpat == NULL)
2946 defn->pattern = tpat;
2947 else
2948 endpat->next = tpat;
2949 endpat = tpat;
2951 tpat->argno = arg->argno;
2952 tpat->nchars = exp_p - lastp;
2954 register U_CHAR *p1 = p;
2955 SKIP_WHITE_SPACE (p1);
2956 if (p1 + 2 <= limit && p1[0] == '#' && p1[1] == '#')
2957 tpat->raw_after = 1;
2959 lastp = exp_p; /* place to start copying from next time */
2960 skipped_arg = 1;
2961 break;
2966 /* If this was not a macro arg, copy it into the expansion. */
2967 if (! skipped_arg) {
2968 register U_CHAR *lim1 = p;
2969 p = id_beg;
2970 while (p != lim1)
2971 *exp_p++ = *p++;
2972 if (stringify == id_beg)
2973 error ("# operator should be followed by a macro argument name");
2978 if (limit < end) {
2979 /* Convert trailing whitespace to Newline-markers. */
2980 while (limit < end && is_space (*limit)) {
2981 *exp_p++ = '\n';
2982 *exp_p++ = *limit++;
2985 *exp_p = '\0';
2987 defn->length = exp_p - defn->expansion;
2989 /* Crash now if we overrun the allocated size. */
2990 if (defn->length + 1 > maxsize)
2991 abort ();
2993 return defn;
2997 * interpret #line command. Remembers previously seen fnames
2998 * in its very own hash table.
3000 #define FNAME_HASHSIZE 37
3001 static void
3002 do_line (buf, limit, op)
3003 U_CHAR *buf, *limit;
3004 FILE_BUF *op;
3006 register U_CHAR *bp;
3007 FILE_BUF *ip = &instack[indepth];
3008 FILE_BUF tem;
3009 int new_lineno;
3010 enum file_change_code file_change = same_file;
3012 /* Expand any macros. */
3013 tem = expand_to_temp_buffer (buf, limit, 0);
3015 /* Point to macroexpanded line, which is null-terminated now. */
3016 bp = tem.buf;
3017 SKIP_WHITE_SPACE (bp);
3019 if (!ISDIGIT (*bp)) {
3020 error ("invalid format #line command");
3021 return;
3024 /* The Newline at the end of this line remains to be processed.
3025 To put the next line at the specified line number,
3026 we must store a line number now that is one less. */
3027 new_lineno = atoi ((const char *)bp);
3029 /* skip over the line number. */
3030 while (ISDIGIT (*bp))
3031 bp++;
3033 SKIP_WHITE_SPACE (bp);
3035 if (*bp == '\"') {
3036 static HASHNODE *fname_table[FNAME_HASHSIZE];
3037 HASHNODE *hp, **hash_bucket;
3038 U_CHAR *fname;
3039 int fname_length;
3041 fname = ++bp;
3043 while (*bp && *bp != '\"')
3044 bp++;
3045 if (*bp != '\"') {
3046 error ("invalid format #line command");
3047 return;
3050 fname_length = bp - fname;
3052 bp++;
3053 SKIP_WHITE_SPACE (bp);
3054 if (*bp) {
3055 if (*bp == '1')
3056 file_change = enter_file;
3057 else if (*bp == '2')
3058 file_change = leave_file;
3059 else {
3060 error ("invalid format #line command");
3061 return;
3064 bp++;
3065 SKIP_WHITE_SPACE (bp);
3066 if (*bp) {
3067 error ("invalid format #line command");
3068 return;
3072 hash_bucket =
3073 &fname_table[hashf (fname, fname_length, FNAME_HASHSIZE)];
3074 for (hp = *hash_bucket; hp != NULL; hp = hp->next)
3075 if (hp->length == fname_length &&
3076 strncmp (hp->value.cpval, (const char *)fname, fname_length) == 0) {
3077 ip->fname = hp->value.cpval;
3078 break;
3080 if (hp == 0) {
3081 char *q;
3082 /* Didn't find it; cons up a new one. */
3083 hp = (HASHNODE *) xcalloc (1, sizeof (HASHNODE) + fname_length + 1);
3084 hp->next = *hash_bucket;
3085 *hash_bucket = hp;
3087 hp->length = fname_length;
3088 ip->fname = hp->value.cpval = q = ((char *) hp) + sizeof (HASHNODE);
3089 memcpy (q, fname, fname_length);
3091 } else if (*bp) {
3092 error ("invalid format #line command");
3093 return;
3096 ip->lineno = new_lineno;
3097 output_line_command (ip, op, 0, file_change);
3098 ip->bufp++; /* Skip the new line. */
3099 check_expand (op, ip->length - (ip->bufp - ip->buf));
3103 * remove all definitions of symbol from symbol table.
3104 * according to un*x /lib/cpp, it is not an error to undef
3105 * something that has no definitions, so it isn't one here either.
3107 static void
3108 do_undef (buf, limit, op)
3109 U_CHAR *buf;
3110 U_CHAR *limit ATTRIBUTE_UNUSED;
3111 FILE_BUF *op ATTRIBUTE_UNUSED;
3113 HASHNODE *hp;
3115 SKIP_WHITE_SPACE (buf);
3117 if (! strncmp ((const char *)buf, "defined", 7) && ! is_idchar (buf[7]))
3118 warning ("undefining `defined'");
3120 while ((hp = lookup (buf, -1, -1)) != NULL) {
3121 if (hp->type != T_MACRO)
3122 warning ("undefining `%s'", hp->name);
3123 delete_macro (hp);
3127 /* Read the tokens of the answer into the macro pool. Only commit the
3128 memory if we intend it as permanent storage, i.e. the #assert case.
3129 Returns 0 on success. */
3131 static int
3132 parse_answer (buf, limit, answerp, type)
3133 const unsigned char *buf, *limit;
3134 struct answer **answerp;
3135 int type;
3137 const unsigned char *start;
3139 /* Skip leading whitespace. */
3140 if (buf < limit && *buf == ' ')
3141 buf++;
3143 /* Parentheses are optional here. */
3144 if (buf == limit && type == T_UNASSERT)
3145 return 0;
3147 if (buf == limit || *buf++ != '(')
3149 if (type == T_IF)
3150 return 0;
3152 error ("missing '(' after predicate");
3153 return 1;
3156 /* Drop whitespace at start. */
3157 while (buf < limit && *buf == ' ')
3158 buf++;
3160 start = buf;
3161 while (buf < limit && *buf != ')')
3162 buf++;
3164 if (buf == limit)
3166 error ("missing ')' to complete answer");
3167 return 1;
3170 if (buf == start)
3172 error ("predicate's answer is empty");
3173 return 1;
3176 if ((type == T_ASSERT || type == T_UNASSERT) && buf + 1 != limit)
3178 error ("extra text at end of directive");
3179 return 1;
3182 /* Lose trailing whitespace. */
3183 if (buf[-1] == ' ')
3184 buf--;
3186 *answerp = (struct answer *) xmalloc (sizeof (struct answer));
3187 (*answerp)->answer = start;
3188 (*answerp)->len = buf - start;
3190 return 0;
3193 /* Parses an assertion, returning a pointer to the hash node of the
3194 predicate, or 0 on error. If an answer was supplied, it is placed
3195 in ANSWERP, otherwise it is set to 0. */
3196 static HASHNODE *
3197 parse_assertion (buf, limit, answerp, type)
3198 const unsigned char *buf, *limit;
3199 struct answer **answerp;
3200 int type;
3202 HASHNODE *result = 0;
3203 const unsigned char *climit;
3204 unsigned char *bp, *symname = canonicalize_text (buf, limit, &climit);
3205 unsigned int len;
3207 bp = symname;
3208 if (bp < climit && is_idstart (*bp))
3211 bp++;
3212 while (bp < climit && is_idchar (*bp));
3214 len = bp - symname;
3216 *answerp = 0;
3217 if (len == 0)
3219 if (symname == climit)
3220 error ("assertion without predicate");
3221 else
3222 error ("predicate must be an identifier");
3224 /* Unfortunately, because of the way we handle #if, we don't avoid
3225 macro expansion in answers. This is not easy to fix. */
3226 else if (parse_answer (bp, climit, answerp, type) == 0)
3228 unsigned char *sym = alloca (len + 1);
3229 int hashcode;
3231 /* Prefix '#' to get it out of macro namespace. */
3232 sym[0] = '#';
3233 memcpy (sym + 1, symname, len);
3235 hashcode = hashf (sym, len + 1, HASHSIZE);
3236 result = lookup (sym, len + 1, hashcode);
3237 if (result == 0)
3238 result = install (sym, len + 1, T_UNUSED, hashcode);
3241 return result;
3244 /* Test an assertion within a preprocessor conditional. Returns zero
3245 on error or failure, one on success. */
3247 test_assertion (pbuf)
3248 unsigned char **pbuf; /* NUL-terminated. */
3250 unsigned char *buf = *pbuf;
3251 unsigned char *limit = buf + strlen ((char *) buf);
3252 struct answer *answer;
3253 HASHNODE *node;
3254 int result = 0;
3256 node = parse_assertion (buf, limit, &answer, T_IF);
3257 if (node)
3259 result = (node->type == T_ASSERT &&
3260 (answer == 0 || *find_answer (node, answer) != 0));
3262 /* Yuk. We update pbuf to point after the assertion test.
3263 First, move past the identifier. */
3264 if (is_space (*buf))
3265 buf++;
3266 while (is_idchar (*buf))
3267 buf++;
3268 /* If we have an answer, we need to move past the parentheses. */
3269 if (answer)
3270 while (*buf++ != ')')
3272 *pbuf = buf;
3275 return result;
3278 /* Handle a #error directive. */
3279 static void
3280 do_error (buf, limit, op)
3281 U_CHAR *buf;
3282 U_CHAR *limit;
3283 FILE_BUF *op ATTRIBUTE_UNUSED;
3285 error ("#error%.*s", (int) (limit - buf), buf);
3288 /* Handle a #warning directive. */
3289 static void
3290 do_warning (buf, limit, op)
3291 U_CHAR *buf;
3292 U_CHAR *limit;
3293 FILE_BUF *op ATTRIBUTE_UNUSED;
3295 warning ("#warning%.*s", (int) (limit - buf), buf);
3298 /* Handle a #assert directive. */
3299 static void
3300 do_assert (buf, limit, op)
3301 U_CHAR *buf;
3302 U_CHAR *limit;
3303 FILE_BUF *op ATTRIBUTE_UNUSED;
3305 struct answer *new_answer;
3306 HASHNODE *node;
3308 node = parse_assertion (buf, limit, &new_answer, T_ASSERT);
3309 if (node)
3311 /* Place the new answer in the answer list. First check there
3312 is not a duplicate. */
3313 new_answer->next = 0;
3314 if (node->type == T_ASSERT)
3316 if (*find_answer (node, new_answer))
3318 free (new_answer);
3319 warning ("\"%s\" re-asserted", node->name + 1);
3320 return;
3322 new_answer->next = node->value.answers;
3324 node->type = T_ASSERT;
3325 node->value.answers = new_answer;
3329 /* Function body to be provided later. */
3330 static void
3331 do_unassert (buf, limit, op)
3332 U_CHAR *buf;
3333 U_CHAR *limit;
3334 FILE_BUF *op ATTRIBUTE_UNUSED;
3336 HASHNODE *node;
3337 struct answer *answer;
3339 node = parse_assertion (buf, limit, &answer, T_UNASSERT);
3340 /* It isn't an error to #unassert something that isn't asserted. */
3341 if (node)
3343 if (node->type == T_ASSERT)
3345 if (answer)
3347 struct answer **p = find_answer (node, answer), *temp;
3349 /* Remove the answer from the list. */
3350 temp = *p;
3351 if (temp)
3352 *p = temp->next;
3354 /* Did we free the last answer? */
3355 if (node->value.answers == 0)
3356 delete_macro (node);
3358 else
3359 delete_macro (node);
3362 free (answer);
3366 /* Returns a pointer to the pointer to the answer in the answer chain,
3367 or a pointer to NULL if the answer is not in the chain. */
3368 static struct answer **
3369 find_answer (node, candidate)
3370 HASHNODE *node;
3371 const struct answer *candidate;
3373 struct answer **result;
3375 for (result = &node->value.answers; *result; result = &(*result)->next)
3377 struct answer *answer = *result;
3379 if (answer->len == candidate->len
3380 && !memcmp (answer->answer, candidate->answer, answer->len))
3381 break;
3384 return result;
3387 /* Return a malloced buffer with leading and trailing whitespace
3388 removed, and all instances of internal whitespace reduced to a
3389 single space. */
3390 static unsigned char *
3391 canonicalize_text (buf, limit, climit)
3392 const unsigned char *buf, *limit, **climit;
3394 unsigned int len = limit - buf;
3395 unsigned char *result = (unsigned char *) xmalloc (len), *dest;
3397 for (dest = result; buf < limit;)
3399 if (! is_space (*buf))
3400 *dest++ = *buf++;
3401 else
3403 while (++buf < limit && is_space (*buf))
3405 if (dest != result && buf != limit)
3406 *dest++ = ' ';
3410 *climit = dest;
3411 return result;
3415 * handle #if command by
3416 * 1) inserting special `defined' keyword into the hash table
3417 * that gets turned into 0 or 1 by special_symbol (thus,
3418 * if the luser has a symbol called `defined' already, it won't
3419 * work inside the #if command)
3420 * 2) rescan the input into a temporary output buffer
3421 * 3) pass the output buffer to the yacc parser and collect a value
3422 * 4) clean up the mess left from steps 1 and 2.
3423 * 5) call conditional_skip to skip til the next #endif (etc.),
3424 * or not, depending on the value from step 3.
3426 static void
3427 do_if (buf, limit, op)
3428 U_CHAR *buf, *limit;
3429 FILE_BUF *op ATTRIBUTE_UNUSED;
3431 int value;
3432 FILE_BUF *ip = &instack[indepth];
3434 value = eval_if_expression (buf, limit - buf);
3435 conditional_skip (ip, value == 0, T_IF);
3439 * handle a #elif directive by not changing if_stack either.
3440 * see the comment above do_else.
3442 static void
3443 do_elif (buf, limit, op)
3444 U_CHAR *buf, *limit;
3445 FILE_BUF *op;
3447 int value;
3448 FILE_BUF *ip = &instack[indepth];
3450 if (if_stack == instack[indepth].if_stack) {
3451 error ("#elif not within a conditional");
3452 return;
3453 } else {
3454 if (if_stack->type != T_IF && if_stack->type != T_ELIF) {
3455 error ("#elif after #else");
3456 fprintf (stderr, " (matches line %d", if_stack->lineno);
3457 if (if_stack->fname != NULL && ip->fname != NULL &&
3458 strcmp (if_stack->fname, ip->fname) != 0)
3459 fprintf (stderr, ", file %s", if_stack->fname);
3460 fprintf (stderr, ")\n");
3462 if_stack->type = T_ELIF;
3465 if (if_stack->if_succeeded)
3466 skip_if_group (ip, 0);
3467 else {
3468 value = eval_if_expression (buf, limit - buf);
3469 if (value == 0)
3470 skip_if_group (ip, 0);
3471 else {
3472 ++if_stack->if_succeeded; /* continue processing input */
3473 output_line_command (ip, op, 1, same_file);
3479 * evaluate a #if expression in BUF, of length LENGTH,
3480 * then parse the result as a C expression and return the value as an int.
3482 static int
3483 eval_if_expression (buf, length)
3484 const U_CHAR *buf;
3485 int length;
3487 FILE_BUF temp_obuf;
3488 HASHNODE *save_defined;
3489 int value;
3491 save_defined = install (U"defined", -1, T_SPEC_DEFINED, -1);
3492 temp_obuf = expand_to_temp_buffer (buf, buf + length, 0);
3493 delete_macro (save_defined); /* clean up special symbol */
3495 value = parse_c_expression ((const char *)temp_obuf.buf);
3497 free (temp_obuf.buf);
3499 return value;
3503 * routine to handle ifdef/ifndef. Try to look up the symbol,
3504 * then do or don't skip to the #endif/#else/#elif depending
3505 * on what directive is actually being processed.
3507 static void
3508 do_xifdef (buf, limit, type)
3509 U_CHAR *buf, *limit;
3510 enum node_type type;
3512 int skip;
3513 FILE_BUF *ip = &instack[indepth];
3514 U_CHAR *end;
3516 /* Discard leading and trailing whitespace. */
3517 SKIP_WHITE_SPACE (buf);
3518 while (limit != buf && is_nvspace (limit[-1])) limit--;
3520 /* Find the end of the identifier at the beginning. */
3521 for (end = buf; is_idchar (*end); end++);
3523 if (end == buf)
3524 skip = (type == T_IFDEF);
3525 else
3526 skip = (lookup (buf, end-buf, -1) == NULL) ^ (type == T_IFNDEF);
3528 conditional_skip (ip, skip, T_IF);
3531 static void
3532 do_ifdef (buf, limit, op)
3533 U_CHAR *buf, *limit;
3534 FILE_BUF *op ATTRIBUTE_UNUSED;
3536 do_xifdef (buf, limit, T_IFDEF);
3539 static void
3540 do_ifndef (buf, limit, op)
3541 U_CHAR *buf, *limit;
3542 FILE_BUF *op ATTRIBUTE_UNUSED;
3544 do_xifdef (buf, limit, T_IFNDEF);
3548 * push TYPE on stack; then, if SKIP is nonzero, skip ahead.
3550 static void
3551 conditional_skip (ip, skip, type)
3552 FILE_BUF *ip;
3553 int skip;
3554 enum node_type type;
3556 IF_STACK_FRAME *temp;
3558 temp = (IF_STACK_FRAME *) xcalloc (1, sizeof (IF_STACK_FRAME));
3559 temp->fname = ip->fname;
3560 temp->lineno = ip->lineno;
3561 temp->next = if_stack;
3562 if_stack = temp;
3564 if_stack->type = type;
3566 if (skip != 0) {
3567 skip_if_group (ip, 0);
3568 return;
3569 } else {
3570 ++if_stack->if_succeeded;
3571 output_line_command (ip, &outbuf, 1, same_file);
3576 * skip to #endif, #else, or #elif. adjust line numbers, etc.
3577 * leaves input ptr at the sharp sign found.
3578 * If ANY is nonzero, return at next directive of any sort.
3580 static void
3581 skip_if_group (ip, any)
3582 FILE_BUF *ip;
3583 int any;
3585 register U_CHAR *bp = ip->bufp, *cp;
3586 register U_CHAR *endb = ip->buf + ip->length;
3587 struct directive *kt;
3588 IF_STACK_FRAME *save_if_stack = if_stack; /* don't pop past here */
3589 U_CHAR *beg_of_line = bp;
3591 while (bp < endb) {
3592 switch (*bp++) {
3593 case '/': /* possible comment */
3594 if (*bp == '\\' && bp[1] == '\n')
3595 newline_fix (bp);
3596 if (*bp == '*') {
3597 ip->bufp = ++bp;
3598 bp = skip_to_end_of_comment (ip, &ip->lineno);
3600 break;
3601 case '\"':
3602 case '\'':
3603 bp = skip_quoted_string (bp - 1, endb, ip->lineno, &ip->lineno, 0, 0);
3604 break;
3605 case '\\':
3606 /* Char after backslash loses its special meaning. */
3607 if (bp < endb) {
3608 if (*bp == '\n')
3609 ++ip->lineno; /* But do update the line-count. */
3610 bp++;
3612 break;
3613 case '\n':
3614 ++ip->lineno;
3615 beg_of_line = bp;
3616 break;
3617 case '#':
3618 ip->bufp = bp - 1;
3620 /* # keyword: a # must be first nonblank char on the line */
3621 if (beg_of_line == 0)
3622 break;
3623 /* Scan from start of line, skipping whitespace, comments
3624 and backslash-newlines, and see if we reach this #.
3625 If not, this # is not special. */
3626 bp = beg_of_line;
3627 while (1) {
3628 if (is_nvspace (*bp))
3629 bp++;
3630 else if (*bp == '\\' && bp[1] == '\n')
3631 bp += 2;
3632 else if (*bp == '/' && bp[1] == '*') {
3633 bp += 2;
3634 while (!(*bp == '*' && bp[1] == '/')) {
3635 if (*bp == '\n')
3636 ip->lineno++;
3637 bp++;
3639 bp += 2;
3641 else break;
3643 if (bp != ip->bufp) {
3644 bp = ip->bufp + 1; /* Reset bp to after the #. */
3645 break;
3648 bp = ip->bufp + 1; /* Point after '#'. */
3650 /* Skip whitespace and \-newline. */
3651 while (1) {
3652 if (is_nvspace (*bp))
3653 bp++;
3654 else if (*bp == '\\' && bp[1] == '\n')
3655 bp += 2;
3656 else if (*bp == '/' && bp[1] == '*') {
3657 bp += 2;
3658 while (!(*bp == '*' && bp[1] == '/'))
3659 bp++;
3660 bp += 2;
3662 else break;
3665 cp = bp;
3667 /* Now find end of directive name.
3668 If we encounter a backslash-newline, exchange it with any following
3669 symbol-constituents so that we end up with a contiguous name. */
3671 while (1) {
3672 if (is_idchar (*bp))
3673 bp++;
3674 else {
3675 if (*bp == '\\' && bp[1] == '\n')
3676 name_newline_fix (bp);
3677 if (is_idchar (*bp))
3678 bp++;
3679 else break;
3683 for (kt = directive_table; kt->length >= 0; kt++) {
3684 IF_STACK_FRAME *temp;
3685 if (strncmp ((const char *)cp, kt->name, kt->length) == 0
3686 && !is_idchar (cp[kt->length])) {
3688 /* If we are asked to return on next directive,
3689 do so now. */
3690 if (any)
3691 return;
3693 switch (kt->type) {
3694 case T_IF:
3695 case T_IFDEF:
3696 case T_IFNDEF:
3697 temp = (IF_STACK_FRAME *) xcalloc (1, sizeof (IF_STACK_FRAME));
3698 temp->next = if_stack;
3699 if_stack = temp;
3700 temp->lineno = ip->lineno;
3701 temp->fname = ip->fname;
3702 temp->type = kt->type;
3703 break;
3704 case T_ELSE:
3705 case T_ENDIF:
3706 case T_ELIF:
3707 if (if_stack == instack[indepth].if_stack) {
3708 error ("#%s not within a conditional", kt->name);
3709 break;
3711 else if (if_stack == save_if_stack)
3712 return; /* found what we came for */
3714 if (kt->type != T_ENDIF) {
3715 if (if_stack->type == T_ELSE)
3716 error ("#else or #elif after #else");
3717 if_stack->type = kt->type;
3718 break;
3721 temp = if_stack;
3722 if_stack = if_stack->next;
3723 free (temp);
3724 break;
3726 default:
3727 /* Anything else is ignored. */
3728 break;
3730 break;
3735 ip->bufp = bp;
3736 /* after this returns, rescan will exit because ip->bufp
3737 now points to the end of the buffer.
3738 rescan is responsible for the error message also. */
3742 * handle a #else directive. Do this by just continuing processing
3743 * without changing if_stack ; this is so that the error message
3744 * for missing #endif's etc. will point to the original #if. It
3745 * is possible that something different would be better.
3747 static void
3748 do_else (buf, limit, op)
3749 U_CHAR *buf ATTRIBUTE_UNUSED;
3750 U_CHAR *limit ATTRIBUTE_UNUSED;
3751 FILE_BUF *op;
3753 FILE_BUF *ip = &instack[indepth];
3755 if (if_stack == instack[indepth].if_stack) {
3756 error ("#else not within a conditional");
3757 return;
3758 } else {
3759 if (if_stack->type != T_IF && if_stack->type != T_ELIF) {
3760 error ("#else after #else");
3761 fprintf (stderr, " (matches line %d", if_stack->lineno);
3762 if (strcmp (if_stack->fname, ip->fname) != 0)
3763 fprintf (stderr, ", file %s", if_stack->fname);
3764 fprintf (stderr, ")\n");
3766 if_stack->type = T_ELSE;
3769 if (if_stack->if_succeeded)
3770 skip_if_group (ip, 0);
3771 else {
3772 ++if_stack->if_succeeded; /* continue processing input */
3773 output_line_command (ip, op, 1, same_file);
3778 * unstack after #endif command
3780 static void
3781 do_endif (buf, limit, op)
3782 U_CHAR *buf ATTRIBUTE_UNUSED;
3783 U_CHAR *limit ATTRIBUTE_UNUSED;
3784 FILE_BUF *op;
3786 if (if_stack == instack[indepth].if_stack)
3787 error ("unbalanced #endif");
3788 else {
3789 IF_STACK_FRAME *temp = if_stack;
3790 if_stack = if_stack->next;
3791 free (temp);
3792 output_line_command (&instack[indepth], op, 1, same_file);
3797 * Skip a comment, assuming the input ptr immediately follows the
3798 * initial slash-star. Bump line counter as necessary.
3799 * (The canonical line counter is &ip->lineno).
3800 * Don't use this routine (or the next one) if bumping the line
3801 * counter is not sufficient to deal with newlines in the string.
3803 static U_CHAR *
3804 skip_to_end_of_comment (ip, line_counter)
3805 register FILE_BUF *ip;
3806 int *line_counter; /* place to remember newlines, or NULL */
3808 register U_CHAR *limit = ip->buf + ip->length;
3809 register U_CHAR *bp = ip->bufp;
3810 FILE_BUF *op = &outbuf; /* JF */
3811 int output = put_out_comments && !line_counter;
3813 /* JF this line_counter stuff is a crock to make sure the
3814 comment is only put out once, no matter how many times
3815 the comment is skipped. It almost works */
3816 if (output) {
3817 *op->bufp++ = '/';
3818 *op->bufp++ = '*';
3820 while (bp < limit) {
3821 if (output)
3822 *op->bufp++ = *bp;
3823 switch (*bp++) {
3824 case '/':
3825 if (warn_comments && bp < limit && *bp == '*')
3826 warning("`/*' within comment");
3827 break;
3828 case '\n':
3829 if (line_counter != NULL)
3830 ++*line_counter;
3831 if (output)
3832 ++op->lineno;
3833 break;
3834 case '*':
3835 if (*bp == '\\' && bp[1] == '\n')
3836 newline_fix (bp);
3837 if (*bp == '/') {
3838 if (output)
3839 *op->bufp++ = '/';
3840 ip->bufp = ++bp;
3841 return bp;
3843 break;
3846 ip->bufp = bp;
3847 return bp;
3851 * Skip over a quoted string. BP points to the opening quote.
3852 * Returns a pointer after the closing quote. Don't go past LIMIT.
3853 * START_LINE is the line number of the starting point (but it need
3854 * not be valid if the starting point is inside a macro expansion).
3856 * The input stack state is not changed.
3858 * If COUNT_NEWLINES is nonzero, it points to an int to increment
3859 * for each newline passed.
3861 * If BACKSLASH_NEWLINES_P is nonzero, store 1 thru it
3862 * if we pass a backslash-newline.
3864 * If EOFP is nonzero, set *EOFP to 1 if the string is unterminated.
3866 static U_CHAR *
3867 skip_quoted_string (bp, limit, start_line, count_newlines, backslash_newlines_p, eofp)
3868 register const U_CHAR *bp;
3869 register const U_CHAR *limit;
3870 int start_line;
3871 int *count_newlines;
3872 int *backslash_newlines_p;
3873 int *eofp;
3875 register U_CHAR c, match;
3877 match = *bp++;
3878 while (1) {
3879 if (bp >= limit) {
3880 error_with_line (line_for_error (start_line),
3881 "unterminated string or character constant");
3882 if (eofp)
3883 *eofp = 1;
3884 break;
3886 c = *bp++;
3887 if (c == '\\') {
3888 while (*bp == '\\' && bp[1] == '\n') {
3889 if (backslash_newlines_p)
3890 *backslash_newlines_p = 1;
3891 if (count_newlines)
3892 ++*count_newlines;
3893 bp += 2;
3895 if (*bp == '\n' && count_newlines) {
3896 if (backslash_newlines_p)
3897 *backslash_newlines_p = 1;
3898 ++*count_newlines;
3900 bp++;
3901 } else if (c == '\n') {
3902 /* Unterminated strings and character constants are 'legal'. */
3903 bp--; /* Don't consume the newline. */
3904 if (eofp)
3905 *eofp = 1;
3906 break;
3907 } else if (c == match)
3908 break;
3910 return (U_CHAR *) bp;
3914 * write out a #line command, for instance, after an #include file.
3915 * If CONDITIONAL is nonzero, we can omit the #line if it would
3916 * appear to be a no-op, and we can output a few newlines instead
3917 * if we want to increase the line number by a small amount.
3918 * FILE_CHANGE says whether we are entering a file, leaving, or neither.
3921 static void
3922 output_line_command (ip, op, conditional, file_change)
3923 FILE_BUF *ip, *op;
3924 int conditional;
3925 enum file_change_code file_change;
3927 int len;
3928 char line_cmd_buf[500];
3930 if (no_line_commands
3931 || ip->fname == NULL
3932 || no_output) {
3933 op->lineno = ip->lineno;
3934 return;
3937 if (conditional) {
3938 if (ip->lineno == op->lineno)
3939 return;
3941 /* If the inherited line number is a little too small,
3942 output some newlines instead of a #line command. */
3943 if (ip->lineno > op->lineno && ip->lineno < op->lineno + 8) {
3944 check_expand (op, 10);
3945 while (ip->lineno > op->lineno) {
3946 *op->bufp++ = '\n';
3947 op->lineno++;
3949 return;
3953 sprintf (line_cmd_buf, "# %d \"%s\"", ip->lineno, ip->fname);
3954 if (file_change != same_file)
3955 strcat (line_cmd_buf, file_change == enter_file ? " 1" : " 2");
3956 if (system_include_depth > 0)
3957 strcat (line_cmd_buf, " 3");
3958 len = strlen (line_cmd_buf);
3959 line_cmd_buf[len++] = '\n';
3960 check_expand (op, len + 1);
3961 if (op->bufp > op->buf && op->bufp[-1] != '\n')
3962 *op->bufp++ = '\n';
3963 memcpy (op->bufp, line_cmd_buf, len);
3964 op->bufp += len;
3965 op->lineno = ip->lineno;
3969 /* Expand a macro call.
3970 HP points to the symbol that is the macro being called.
3971 Put the result of expansion onto the input stack
3972 so that subsequent input by our caller will use it.
3974 If macro wants arguments, caller has already verified that
3975 an argument list follows; arguments come from the input stack. */
3977 static void
3978 macroexpand (hp, op)
3979 HASHNODE *hp;
3980 FILE_BUF *op;
3982 int nargs;
3983 DEFINITION *defn = hp->value.defn;
3984 register U_CHAR *xbuf;
3985 int xbuf_len;
3986 int start_line = instack[indepth].lineno;
3988 CHECK_DEPTH (return;);
3990 /* it might not actually be a macro. */
3991 if (hp->type != T_MACRO) {
3992 special_symbol (hp, op);
3993 return;
3996 nargs = defn->nargs;
3998 if (nargs >= 0) {
3999 register int i;
4000 struct argdata *args;
4001 const char *parse_error = 0;
4003 args = (struct argdata *) alloca ((nargs + 1) * sizeof (struct argdata));
4005 for (i = 0; i < nargs; i++) {
4006 args[i].raw = args[i].expanded = (U_CHAR *) "";
4007 args[i].raw_length = args[i].expand_length
4008 = args[i].stringified_length = 0;
4009 args[i].free1 = args[i].free2 = 0;
4012 /* Parse all the macro args that are supplied. I counts them.
4013 The first NARGS args are stored in ARGS.
4014 The rest are discarded. */
4015 i = 0;
4016 do {
4017 /* Discard the open-parenthesis or comma before the next arg. */
4018 ++instack[indepth].bufp;
4019 parse_error
4020 = macarg ((i < nargs || (nargs == 0 && i == 0)) ? &args[i] : 0);
4021 if (parse_error)
4023 error_with_line (line_for_error (start_line), "%s", parse_error);
4024 break;
4026 i++;
4027 } while (*instack[indepth].bufp != ')');
4029 /* If we got one arg but it was just whitespace, call that 0 args. */
4030 if (i == 1) {
4031 register const U_CHAR *bp = args[0].raw;
4032 register const U_CHAR *lim = bp + args[0].raw_length;
4033 while (bp != lim && is_space (*bp)) bp++;
4034 if (bp == lim)
4035 i = 0;
4038 if (nargs == 0 && i > 0)
4039 error ("arguments given to macro `%s'", hp->name);
4040 else if (i < nargs) {
4041 /* traditional C allows foo() if foo wants one argument. */
4042 if (nargs == 1 && i == 0)
4044 else if (i == 0)
4045 error ("no args to macro `%s'", hp->name);
4046 else if (i == 1)
4047 error ("only 1 arg to macro `%s'", hp->name);
4048 else
4049 error ("only %d args to macro `%s'", i, hp->name);
4050 } else if (i > nargs)
4051 error ("too many (%d) args to macro `%s'", i, hp->name);
4053 /* Swallow the closeparen. */
4054 ++instack[indepth].bufp;
4056 /* If macro wants zero args, we parsed the arglist for checking only.
4057 Read directly from the macro definition. */
4058 if (nargs == 0) {
4059 xbuf = defn->expansion;
4060 xbuf_len = defn->length;
4061 } else {
4062 register U_CHAR *exp = defn->expansion;
4063 register int offset; /* offset in expansion,
4064 copied a piece at a time */
4065 register int totlen; /* total amount of exp buffer filled so far */
4067 register struct reflist *ap;
4069 /* Macro really takes args. Compute the expansion of this call. */
4071 /* Compute length in characters of the macro's expansion. */
4072 xbuf_len = defn->length;
4073 for (ap = defn->pattern; ap != NULL; ap = ap->next) {
4074 if (ap->stringify)
4075 xbuf_len += args[ap->argno].stringified_length;
4076 else
4077 xbuf_len += args[ap->argno].raw_length;
4080 xbuf = (U_CHAR *) xmalloc (xbuf_len + 1);
4082 /* Generate in XBUF the complete expansion
4083 with arguments substituted in.
4084 TOTLEN is the total size generated so far.
4085 OFFSET is the index in the definition
4086 of where we are copying from. */
4087 offset = totlen = 0;
4088 for (ap = defn->pattern; ap != NULL; ap = ap->next) {
4089 register struct argdata *arg = &args[ap->argno];
4091 for (i = 0; i < ap->nchars; i++)
4092 xbuf[totlen++] = exp[offset++];
4094 if (ap->stringify != 0) {
4095 int arglen = arg->raw_length;
4096 int escaped = 0;
4097 int in_string = 0;
4098 int c;
4099 i = 0;
4100 while (i < arglen
4101 && (c = arg->raw[i], is_space (c)))
4102 i++;
4103 while (i < arglen
4104 && (c = arg->raw[arglen - 1], is_space (c)))
4105 arglen--;
4106 for (; i < arglen; i++) {
4107 c = arg->raw[i];
4109 /* Special markers Newline Space
4110 generate nothing for a stringified argument. */
4111 if (c == '\n' && arg->raw[i+1] != '\n') {
4112 i++;
4113 continue;
4116 /* Internal sequences of whitespace are replaced by one space
4117 except within an string or char token. */
4118 if (! in_string
4119 && (c == '\n' ? arg->raw[i+1] == '\n' : is_space (c))) {
4120 while (1) {
4121 /* Note that Newline Space does occur within whitespace
4122 sequences; consider it part of the sequence. */
4123 if (c == '\n' && is_space (arg->raw[i+1]))
4124 i += 2;
4125 else if (c != '\n' && is_space (c))
4126 i++;
4127 else break;
4128 c = arg->raw[i];
4130 i--;
4131 c = ' ';
4134 if (escaped)
4135 escaped = 0;
4136 else {
4137 if (c == '\\')
4138 escaped = 1;
4139 if (in_string) {
4140 if (c == in_string)
4141 in_string = 0;
4142 } else if (c == '\"' || c == '\'')
4143 in_string = c;
4146 /* Escape these chars */
4147 if (c == '\"' || (in_string && c == '\\'))
4148 xbuf[totlen++] = '\\';
4149 if (ISPRINT (c))
4150 xbuf[totlen++] = c;
4151 else {
4152 sprintf ((char *) &xbuf[totlen], "\\%03o", (unsigned int) c);
4153 totlen += 4;
4156 } else {
4157 const U_CHAR *p1 = arg->raw;
4158 const U_CHAR *l1 = p1 + arg->raw_length;
4160 if (ap->raw_before) {
4161 while (p1 != l1 && is_space (*p1)) p1++;
4162 while (p1 != l1 && is_idchar (*p1))
4163 xbuf[totlen++] = *p1++;
4164 /* Delete any no-reexpansion marker that follows
4165 an identifier at the beginning of the argument
4166 if the argument is concatenated with what precedes it. */
4167 if (p1[0] == '\n' && p1[1] == '-')
4168 p1 += 2;
4170 if (ap->raw_after) {
4171 /* Arg is concatenated after: delete trailing whitespace,
4172 whitespace markers, and no-reexpansion markers. */
4173 while (p1 != l1) {
4174 if (is_space (l1[-1])) l1--;
4175 else if (l1[-1] == '-') {
4176 const U_CHAR *p2 = l1 - 1;
4177 /* If a `-' is preceded by an odd number of newlines then it
4178 and the last newline are a no-reexpansion marker. */
4179 while (p2 != p1 && p2[-1] == '\n') p2--;
4180 if ((l1 - 1 - p2) & 1) {
4181 l1 -= 2;
4183 else break;
4185 else break;
4188 memmove (xbuf + totlen, p1, l1 - p1);
4189 totlen += l1 - p1;
4192 if (totlen > xbuf_len)
4193 abort ();
4196 /* if there is anything left of the definition
4197 after handling the arg list, copy that in too. */
4199 for (i = offset; i < defn->length; i++)
4200 xbuf[totlen++] = exp[i];
4202 xbuf[totlen] = 0;
4203 xbuf_len = totlen;
4205 for (i = 0; i < nargs; i++) {
4206 if (args[i].free1 != 0)
4207 free (args[i].free1);
4208 if (args[i].free2 != 0)
4209 free (args[i].free2);
4212 } else {
4213 xbuf = defn->expansion;
4214 xbuf_len = defn->length;
4217 /* Now put the expansion on the input stack
4218 so our caller will commence reading from it. */
4220 register FILE_BUF *ip2;
4222 ip2 = &instack[++indepth];
4224 ip2->fname = 0;
4225 ip2->lineno = 0;
4226 ip2->buf = xbuf;
4227 ip2->length = xbuf_len;
4228 ip2->bufp = xbuf;
4229 ip2->free_ptr = (nargs > 0) ? xbuf : 0;
4230 ip2->macro = hp;
4231 ip2->if_stack = if_stack;
4236 * Parse a macro argument and store the info on it into *ARGPTR.
4237 * Return nonzero to indicate a syntax error.
4240 static const char *
4241 macarg (argptr)
4242 register struct argdata *argptr;
4244 FILE_BUF *ip = &instack[indepth];
4245 int paren = 0;
4246 int newlines = 0;
4247 int comments = 0;
4249 /* Try to parse as much of the argument as exists at this
4250 input stack level. */
4251 U_CHAR *bp = macarg1 (ip->bufp, ip->buf + ip->length,
4252 &paren, &newlines, &comments);
4254 /* If we find the end of the argument at this level,
4255 set up *ARGPTR to point at it in the input stack. */
4256 if (!(ip->fname != 0 && (newlines != 0 || comments != 0))
4257 && bp != ip->buf + ip->length) {
4258 if (argptr != 0) {
4259 argptr->raw = ip->bufp;
4260 argptr->raw_length = bp - ip->bufp;
4262 ip->bufp = bp;
4263 } else {
4264 /* This input stack level ends before the macro argument does.
4265 We must pop levels and keep parsing.
4266 Therefore, we must allocate a temporary buffer and copy
4267 the macro argument into it. */
4268 int bufsize = bp - ip->bufp;
4269 int extra = newlines;
4270 U_CHAR *buffer = (U_CHAR *) xmalloc (bufsize + extra + 1);
4271 int final_start = 0;
4273 memcpy (buffer, ip->bufp, bufsize);
4274 ip->bufp = bp;
4275 ip->lineno += newlines;
4277 while (bp == ip->buf + ip->length) {
4278 if (instack[indepth].macro == 0) {
4279 free (buffer);
4280 return "unterminated macro call";
4282 ip->macro->type = T_MACRO;
4283 if (ip->free_ptr)
4284 free (ip->free_ptr);
4285 ip = &instack[--indepth];
4286 newlines = 0;
4287 comments = 0;
4288 bp = macarg1 (ip->bufp, ip->buf + ip->length, &paren,
4289 &newlines, &comments);
4290 final_start = bufsize;
4291 bufsize += bp - ip->bufp;
4292 extra += newlines;
4293 buffer = (U_CHAR *) xrealloc (buffer, bufsize + extra + 1);
4294 memcpy (buffer + bufsize - (bp - ip->bufp), ip->bufp, bp - ip->bufp);
4295 ip->bufp = bp;
4296 ip->lineno += newlines;
4299 /* Now, if arg is actually wanted, record its raw form,
4300 discarding comments and duplicating newlines in whatever
4301 part of it did not come from a macro expansion.
4302 EXTRA space has been preallocated for duplicating the newlines.
4303 FINAL_START is the index of the start of that part. */
4304 if (argptr != 0) {
4305 argptr->raw = buffer;
4306 argptr->raw_length = bufsize;
4307 argptr->free1 = buffer;
4308 argptr->newlines = newlines;
4309 argptr->comments = comments;
4310 if ((newlines || comments) && ip->fname != 0)
4311 argptr->raw_length
4312 = final_start +
4313 discard_comments (argptr->raw + final_start,
4314 argptr->raw_length - final_start,
4315 newlines);
4316 argptr->raw[argptr->raw_length] = 0;
4317 if (argptr->raw_length > bufsize + extra)
4318 abort ();
4322 /* If we are not discarding this argument,
4323 macroexpand it and compute its length as stringified.
4324 All this info goes into *ARGPTR. */
4326 if (argptr != 0) {
4327 FILE_BUF obuf;
4328 register const U_CHAR *buf, *lim;
4329 register int totlen;
4331 obuf = expand_to_temp_buffer (argptr->raw,
4332 argptr->raw + argptr->raw_length,
4335 argptr->expanded = obuf.buf;
4336 argptr->expand_length = obuf.length;
4337 argptr->free2 = obuf.buf;
4339 buf = argptr->raw;
4340 lim = buf + argptr->raw_length;
4342 totlen = 0;
4343 while (buf != lim) {
4344 register U_CHAR c = *buf++;
4345 totlen++;
4346 /* Internal sequences of whitespace are replaced by one space
4347 in most cases, but not always. So count all the whitespace
4348 in case we need to keep it all. */
4349 if (c == '\"' || c == '\\') /* escape these chars */
4350 totlen++;
4351 else if (!ISPRINT (c))
4352 totlen += 3;
4354 argptr->stringified_length = totlen;
4356 return 0;
4359 /* Scan text from START (inclusive) up to LIMIT (exclusive),
4360 counting parens in *DEPTHPTR,
4361 and return if reach LIMIT
4362 or before a `)' that would make *DEPTHPTR negative
4363 or before a comma when *DEPTHPTR is zero.
4364 Single and double quotes are matched and termination
4365 is inhibited within them. Comments also inhibit it.
4366 Value returned is pointer to stopping place.
4368 Increment *NEWLINES each time a newline is passed.
4369 Set *COMMENTS to 1 if a comment is seen. */
4371 static U_CHAR *
4372 macarg1 (start, limit, depthptr, newlines, comments)
4373 U_CHAR *start;
4374 register const U_CHAR *limit;
4375 int *depthptr, *newlines, *comments;
4377 register U_CHAR *bp = start;
4379 while (bp < limit) {
4380 switch (*bp) {
4381 case '(':
4382 (*depthptr)++;
4383 break;
4384 case ')':
4385 if (--(*depthptr) < 0)
4386 return bp;
4387 break;
4388 case '\\':
4389 /* Traditionally, backslash makes following char not special. */
4390 if (bp + 1 < limit)
4392 bp++;
4393 /* But count source lines anyway. */
4394 if (*bp == '\n')
4395 ++*newlines;
4397 break;
4398 case '\n':
4399 ++*newlines;
4400 break;
4401 case '/':
4402 if (bp[1] == '\\' && bp[2] == '\n')
4403 newline_fix (bp + 1);
4404 if (bp[1] != '*' || bp + 1 >= limit)
4405 break;
4406 *comments = 1;
4407 bp += 2;
4408 while (bp + 1 < limit) {
4409 if (bp[0] == '*'
4410 && bp[1] == '\\' && bp[2] == '\n')
4411 newline_fix (bp + 1);
4412 if (bp[0] == '*' && bp[1] == '/')
4413 break;
4414 if (*bp == '\n') ++*newlines;
4415 bp++;
4417 bp += 1;
4418 break;
4419 case '\'':
4420 case '\"':
4422 int quotec;
4423 for (quotec = *bp++; bp + 1 < limit && *bp != quotec; bp++) {
4424 if (*bp == '\\') {
4425 bp++;
4426 if (*bp == '\n')
4427 ++*newlines;
4428 while (*bp == '\\' && bp[1] == '\n') {
4429 bp += 2;
4431 } else if (*bp == '\n') {
4432 ++*newlines;
4433 if (quotec == '\'')
4434 break;
4438 break;
4439 case ',':
4440 if ((*depthptr) == 0)
4441 return bp;
4442 break;
4444 bp++;
4447 return bp;
4450 /* Discard comments and duplicate newlines
4451 in the string of length LENGTH at START,
4452 except inside of string constants.
4453 The string is copied into itself with its beginning staying fixed.
4455 NEWLINES is the number of newlines that must be duplicated.
4456 We assume that that much extra space is available past the end
4457 of the string. */
4459 static int
4460 discard_comments (start, length, newlines)
4461 U_CHAR *start;
4462 int length;
4463 int newlines;
4465 register U_CHAR *ibp;
4466 register U_CHAR *obp;
4467 register const U_CHAR *limit;
4468 register int c;
4470 /* If we have newlines to duplicate, copy everything
4471 that many characters up. Then, in the second part,
4472 we will have room to insert the newlines
4473 while copying down.
4474 NEWLINES may actually be too large, because it counts
4475 newlines in string constants, and we don't duplicate those.
4476 But that does no harm. */
4477 if (newlines > 0) {
4478 ibp = start + length;
4479 obp = ibp + newlines;
4480 limit = start;
4481 while (limit != ibp)
4482 *--obp = *--ibp;
4485 ibp = start + newlines;
4486 limit = start + length + newlines;
4487 obp = start;
4489 while (ibp < limit) {
4490 *obp++ = c = *ibp++;
4491 switch (c) {
4492 case '\n':
4493 /* Duplicate the newline. */
4494 *obp++ = '\n';
4495 break;
4497 case '\\':
4498 if (*ibp == '\n') {
4499 obp--;
4500 ibp++;
4502 break;
4504 case '/':
4505 if (*ibp == '\\' && ibp[1] == '\n')
4506 newline_fix (ibp);
4507 /* Delete any comment. */
4508 if (ibp[0] != '*' || ibp + 1 >= limit)
4509 break;
4510 obp--;
4511 ibp++;
4512 while (ibp + 1 < limit) {
4513 if (ibp[0] == '*'
4514 && ibp[1] == '\\' && ibp[2] == '\n')
4515 newline_fix (ibp + 1);
4516 if (ibp[0] == '*' && ibp[1] == '/')
4517 break;
4518 ibp++;
4520 ibp += 2;
4521 break;
4523 case '\'':
4524 case '\"':
4525 /* Notice and skip strings, so that we don't
4526 think that comments start inside them,
4527 and so we don't duplicate newlines in them. */
4529 int quotec = c;
4530 while (ibp < limit) {
4531 *obp++ = c = *ibp++;
4532 if (c == quotec)
4533 break;
4534 if (c == '\n' && quotec == '\'')
4535 break;
4536 if (c == '\\' && ibp < limit) {
4537 while (*ibp == '\\' && ibp[1] == '\n')
4538 ibp += 2;
4539 *obp++ = *ibp++;
4543 break;
4547 return obp - start;
4551 /* Core error handling routine. */
4552 static void
4553 v_message (mtype, line, msgid, ap)
4554 enum msgtype mtype;
4555 int line;
4556 const char *msgid;
4557 va_list ap;
4559 const char *fname = 0;
4560 int i;
4562 if (mtype == WARNING && inhibit_warnings)
4563 return;
4565 for (i = indepth; i >= 0; i--)
4566 if (instack[i].fname != NULL) {
4567 if (line == 0)
4568 line = instack[i].lineno;
4569 fname = instack[i].fname;
4570 break;
4573 if (fname)
4574 fprintf (stderr, "%s:%d: ", fname, line);
4575 else
4576 fprintf (stderr, "%s: ", progname);
4578 if (mtype == WARNING)
4579 fputs ("warning: ", stderr);
4581 vfprintf (stderr, msgid, ap);
4582 putc ('\n', stderr);
4584 if (mtype == ERROR)
4585 errors++;
4589 * error - print error message and increment count of errors.
4591 void
4592 error VPARAMS ((const char *msgid, ...))
4594 #ifndef ANSI_PROTOTYPES
4595 const char *msgid;
4596 #endif
4597 va_list ap;
4599 VA_START(ap, msgid);
4601 #ifndef ANSI_PROTOTYPES
4602 msgid = va_arg (ap, const char *);
4603 #endif
4605 v_message (ERROR, 0, msgid, ap);
4608 void
4609 error_with_line VPARAMS ((int line, const char *msgid, ...))
4611 #ifndef ANSI_PROTOTYPES
4612 int line;
4613 const char *msgid;
4614 #endif
4615 va_list ap;
4617 VA_START(ap, msgid);
4619 #ifndef ANSI_PROTOTYPES
4620 line = va_arg (ap, int);
4621 msgid = va_arg (ap, const char *);
4622 #endif
4624 v_message (ERROR, line, msgid, ap);
4627 /* Error including a message from `errno'. */
4628 void
4629 error_from_errno (name)
4630 const char *name;
4632 error ("%s: %s", name, strerror (errno));
4635 /* Print error message but don't count it. */
4636 void
4637 warning VPARAMS ((const char *msgid, ...))
4639 #ifndef ANSI_PROTOTYPES
4640 const char *msgid;
4641 #endif
4642 va_list ap;
4644 VA_START(ap, msgid);
4646 #ifndef ANSI_PROTOTYPES
4647 msgid = va_arg (ap, const char *);
4648 #endif
4650 v_message (WARNING, 0, msgid, ap);
4653 void
4654 fatal VPARAMS ((const char *msgid, ...))
4656 #ifndef ANSI_PROTOTYPES
4657 const char *msgid;
4658 #endif
4659 va_list ap;
4661 VA_START(ap, msgid);
4663 #ifndef ANSI_PROTOTYPES
4664 msgid = va_arg (ap, const char *);
4665 #endif
4667 v_message (FATAL, 0, msgid, ap);
4668 exit (FATAL_EXIT_CODE);
4671 /* More 'friendly' abort that prints the location at which we died. */
4672 void
4673 fancy_abort (line, func)
4674 int line;
4675 const char *func;
4677 fatal ("Internal error in %s, at tradcpp.c:%d\n\
4678 Please submit a full bug report.\n\
4679 See %s for instructions.", func, line, GCCBUGURL);
4682 void
4683 perror_with_name (name)
4684 const char *name;
4686 fprintf (stderr, "%s: %s: %s\n", progname, name, strerror (errno));
4687 errors++;
4690 void
4691 pfatal_with_name (name)
4692 const char *name;
4694 perror_with_name (name);
4695 exit (FATAL_EXIT_CODE);
4698 /* Return the line at which an error occurred.
4699 The error is not necessarily associated with the current spot
4700 in the input stack, so LINE says where. LINE will have been
4701 copied from ip->lineno for the current input level.
4702 If the current level is for a file, we return LINE.
4703 But if the current level is not for a file, LINE is meaningless.
4704 In that case, we return the lineno of the innermost file. */
4705 static int
4706 line_for_error (line)
4707 int line;
4709 int i;
4710 int line1 = line;
4712 for (i = indepth; i >= 0; ) {
4713 if (instack[i].fname != 0)
4714 return line1;
4715 i--;
4716 if (i < 0)
4717 return 0;
4718 line1 = instack[i].lineno;
4720 return 0;
4724 * If OBUF doesn't have NEEDED bytes after OPTR, make it bigger.
4726 * As things stand, nothing is ever placed in the output buffer to be
4727 * removed again except when it's KNOWN to be part of an identifier,
4728 * so flushing and moving down everything left, instead of expanding,
4729 * should work ok.
4732 static void
4733 grow_outbuf (obuf, needed)
4734 register FILE_BUF *obuf;
4735 register int needed;
4737 register U_CHAR *p;
4738 int minsize;
4740 if (obuf->length - (obuf->bufp - obuf->buf) > needed)
4741 return;
4743 /* Make it at least twice as big as it is now. */
4744 obuf->length *= 2;
4745 /* Make it have at least 150% of the free space we will need. */
4746 minsize = (3 * needed) / 2 + (obuf->bufp - obuf->buf);
4747 if (minsize > obuf->length)
4748 obuf->length = minsize;
4750 p = (U_CHAR *) xrealloc (obuf->buf, obuf->length);
4751 obuf->bufp = p + (obuf->bufp - obuf->buf);
4752 obuf->buf = p;
4755 /* Symbol table for macro names and special symbols */
4758 * install a name in the main hash table, even if it is already there.
4759 * name stops with first non alphanumeric, except leading '#'.
4760 * caller must check against redefinition if that is desired.
4761 * delete_macro () removes things installed by install () in fifo order.
4762 * this is important because of the `defined' special symbol used
4763 * in #if, and also if pushdef/popdef directives are ever implemented.
4765 * If LEN is >= 0, it is the length of the name.
4766 * Otherwise, compute the length by scanning the entire name.
4768 * If HASH is >= 0, it is the precomputed hash code.
4769 * Otherwise, compute the hash code.
4771 * caller must set the value, if any is desired.
4773 static HASHNODE *
4774 install (name, len, type, hash)
4775 const U_CHAR *name;
4776 int len;
4777 enum node_type type;
4778 int hash;
4779 /* watch out here if sizeof (U_CHAR *) != sizeof (int) */
4781 register HASHNODE *hp;
4782 register int bucket;
4783 register const U_CHAR *p;
4784 U_CHAR *q;
4786 if (len < 0) {
4787 p = name;
4788 while (is_idchar (*p))
4789 p++;
4790 len = p - name;
4793 if (hash < 0)
4794 hash = hashf (name, len, HASHSIZE);
4796 hp = (HASHNODE *) xmalloc (sizeof (HASHNODE) + len + 1);
4797 bucket = hash;
4798 hp->bucket_hdr = &hashtab[bucket];
4799 hp->next = hashtab[bucket];
4800 hashtab[bucket] = hp;
4801 hp->prev = NULL;
4802 if (hp->next != NULL)
4803 hp->next->prev = hp;
4804 hp->type = type;
4805 hp->length = len;
4806 hp->name = q = ((U_CHAR *) hp) + sizeof (HASHNODE);
4807 memcpy (q, name, len);
4808 q[len] = 0;
4809 return hp;
4813 * find the most recent hash node for name name (ending with first
4814 * non-identifier char) installed by install
4816 * If LEN is >= 0, it is the length of the name.
4817 * Otherwise, compute the length by scanning the entire name.
4819 * If HASH is >= 0, it is the precomputed hash code.
4820 * Otherwise, compute the hash code.
4822 HASHNODE *
4823 lookup (name, len, hash)
4824 const U_CHAR *name;
4825 int len;
4826 int hash;
4828 register const U_CHAR *bp;
4829 register HASHNODE *bucket;
4831 if (len < 0) {
4832 for (bp = name; is_idchar (*bp); bp++) ;
4833 len = bp - name;
4836 if (hash < 0)
4837 hash = hashf (name, len, HASHSIZE);
4839 bucket = hashtab[hash];
4840 while (bucket) {
4841 if (bucket->length == len
4842 && strncmp ((const char *)bucket->name, (const char *)name, len) == 0)
4843 return bucket;
4844 bucket = bucket->next;
4846 return NULL;
4850 * Delete a hash node. Some weirdness to free junk from macros.
4851 * More such weirdness will have to be added if you define more hash
4852 * types that need it.
4855 /* Note that the DEFINITION of a macro is removed from the hash table
4856 but its storage is not freed. This would be a storage leak
4857 except that it is not reasonable to keep undefining and redefining
4858 large numbers of macros many times.
4859 In any case, this is necessary, because a macro can be #undef'd
4860 in the middle of reading the arguments to a call to it.
4861 If #undef freed the DEFINITION, that would crash. */
4862 static void
4863 delete_macro (hp)
4864 HASHNODE *hp;
4867 if (hp->prev != NULL)
4868 hp->prev->next = hp->next;
4869 if (hp->next != NULL)
4870 hp->next->prev = hp->prev;
4872 /* make sure that the bucket chain header that
4873 the deleted guy was on points to the right thing afterwards. */
4874 if (hp == *hp->bucket_hdr)
4875 *hp->bucket_hdr = hp->next;
4877 free (hp);
4881 * return hash function on name. must be compatible with the one
4882 * computed a step at a time, elsewhere
4884 static int
4885 hashf (name, len, hashsize)
4886 register const U_CHAR *name;
4887 register int len;
4888 int hashsize;
4890 register int r = 0;
4892 while (len--)
4893 r = HASHSTEP (r, *name++);
4895 return MAKE_POS (r) % hashsize;
4898 /* Dump all macro definitions as #defines to stdout. */
4900 static void
4901 dump_all_macros ()
4903 int bucket;
4905 for (bucket = 0; bucket < HASHSIZE; bucket++) {
4906 register HASHNODE *hp;
4908 for (hp = hashtab[bucket]; hp; hp= hp->next) {
4909 if (hp->type == T_MACRO) {
4910 register DEFINITION *defn = hp->value.defn;
4911 struct reflist *ap;
4912 int offset;
4913 int concat;
4916 /* Print the definition of the macro HP. */
4918 printf ("#define %s", hp->name);
4919 if (defn->nargs >= 0) {
4920 int i;
4922 printf ("(");
4923 for (i = 0; i < defn->nargs; i++) {
4924 dump_arg_n (defn, i);
4925 if (i + 1 < defn->nargs)
4926 printf (", ");
4928 printf (")");
4931 printf (" ");
4933 offset = 0;
4934 concat = 0;
4935 for (ap = defn->pattern; ap != NULL; ap = ap->next) {
4936 dump_defn_1 (defn->expansion, offset, ap->nchars);
4937 if (ap->nchars != 0)
4938 concat = 0;
4939 offset += ap->nchars;
4940 if (ap->stringify)
4941 printf (" #");
4942 if (ap->raw_before && !concat)
4943 printf (" ## ");
4944 concat = 0;
4945 dump_arg_n (defn, ap->argno);
4946 if (ap->raw_after) {
4947 printf (" ## ");
4948 concat = 1;
4951 dump_defn_1 (defn->expansion, offset, defn->length - offset);
4952 printf ("\n");
4958 /* Output to stdout a substring of a macro definition.
4959 BASE is the beginning of the definition.
4960 Output characters START thru LENGTH.
4961 Discard newlines outside of strings, thus
4962 converting funny-space markers to ordinary spaces. */
4963 static void
4964 dump_defn_1 (base, start, length)
4965 const U_CHAR *base;
4966 int start;
4967 int length;
4969 const U_CHAR *p = base + start;
4970 const U_CHAR *limit = base + start + length;
4972 while (p < limit) {
4973 if (*p != '\n')
4974 putchar (*p);
4975 else if (*p == '\"' || *p =='\'') {
4976 const U_CHAR *p1 = skip_quoted_string (p, limit, 0, 0, 0, 0);
4977 fwrite (p, p1 - p, 1, stdout);
4978 p = p1 - 1;
4980 p++;
4984 /* Print the name of argument number ARGNUM of macro definition DEFN.
4985 Recall that DEFN->argnames contains all the arg names
4986 concatenated in reverse order with comma-space in between. */
4987 static void
4988 dump_arg_n (defn, argnum)
4989 DEFINITION *defn;
4990 int argnum;
4992 register const U_CHAR *p = defn->argnames;
4993 while (argnum + 1 < defn->nargs) {
4994 p = (const U_CHAR *) strchr ((const char *)p, ' ') + 1;
4995 argnum++;
4998 while (*p && *p != ',') {
4999 putchar (*p);
5000 p++;
5004 /* Initialize the built-in macros. */
5005 #define DSC(x) U x, sizeof x - 1
5006 #define install_spec(name, type) \
5007 install(DSC(name), type, -1);
5008 #define install_value(name, val) \
5009 hp = install(DSC(name), T_CONST, -1); hp->value.cpval = val;
5010 static void
5011 initialize_builtins ()
5013 HASHNODE *hp;
5015 install_spec ("__BASE_FILE__", T_BASE_FILE);
5016 install_spec ("__DATE__", T_DATE);
5017 install_spec ("__FILE__", T_FILE);
5018 install_spec ("__TIME__", T_TIME);
5019 install_spec ("__VERSION__", T_VERSION);
5020 install_spec ("__INCLUDE_LEVEL__", T_INCLUDE_LEVEL);
5021 install_spec ("__LINE__", T_SPECLINE);
5023 #ifndef NO_BUILTIN_SIZE_TYPE
5024 install_value ("__SIZE_TYPE__", SIZE_TYPE);
5025 #endif
5026 #ifndef NO_BUILTIN_PTRDIFF_TYPE
5027 install_value ("__PTRDIFF_TYPE__", PTRDIFF_TYPE);
5028 #endif
5029 #ifndef NO_BUILTIN_WCHAR_TYPE
5030 install_value ("__WCHAR_TYPE__", WCHAR_TYPE);
5031 #endif
5032 #ifndef NO_BUILTIN_WINT_TYPE
5033 install_value ("__WINT_TYPE__", WINT_TYPE);
5034 #endif
5035 install_value ("__REGISTER_PREFIX__", REGISTER_PREFIX);
5036 install_value ("__USER_LABEL_PREFIX__", user_label_prefix);
5038 #undef DSC
5039 #undef install_spec
5040 #undef install_value
5042 /* Common handler of command line directives -U, -D and -A. */
5043 static void
5044 run_directive (str, len, type)
5045 const char *str;
5046 size_t len;
5047 enum node_type type;
5049 struct directive *kt;
5050 FILE_BUF *ip = &instack[++indepth];
5051 ip->fname = "*command line*";
5053 ip->buf = ip->bufp = (U_CHAR *) str;
5054 ip->length = len;
5055 ip->lineno = 1;
5056 ip->macro = 0;
5057 ip->free_ptr = 0;
5058 ip->if_stack = if_stack;
5060 for (kt = directive_table; kt->type != type; kt++)
5063 (*kt->func) ((U_CHAR *) str, (U_CHAR *) str + len, NULL);
5064 --indepth;
5067 /* Handle the -D option. If STR is just an identifier, define it with
5068 * value 1. If STR has anything after the identifier, then it should
5069 * be identifier-space-definition. */
5070 static void
5071 make_definition (str)
5072 const char *str;
5074 char *buf, *p;
5075 size_t count;
5077 /* Copy the entire option so we can modify it.
5078 Change the first "=" in the string to a space. If there is none,
5079 tack " 1" on the end. */
5081 /* Length including the null. */
5082 count = strlen (str);
5083 buf = (char *) alloca (count + 2);
5084 memcpy (buf, str, count);
5086 p = strchr (str, '=');
5087 if (p)
5088 buf[p - str] = ' ';
5089 else
5091 buf[count++] = ' ';
5092 buf[count++] = '1';
5095 run_directive (buf, count, T_DEFINE);
5098 /* Handle the -U option. */
5099 static void
5100 make_undef (str)
5101 const char *str;
5103 run_directive (str, strlen (str), T_UNDEF);
5106 /* Handles the #assert (-A) and #unassert (-A-) command line options. */
5107 static void
5108 make_assertion (str)
5109 const char *str;
5111 enum node_type type = T_ASSERT;
5112 size_t count;
5113 const char *p;
5115 if (*str == '-')
5117 str++;
5118 type = T_UNASSERT;
5121 count = strlen (str);
5122 p = strchr (str, '=');
5123 if (p)
5125 /* Copy the entire option so we can modify it. Change the first
5126 "=" in the string to a '(', and tack a ')' on the end. */
5127 char *buf = (char *) alloca (count + 1);
5129 memcpy (buf, str, count);
5130 buf[p - str] = '(';
5131 buf[count++] = ')';
5132 str = buf;
5135 run_directive (str, count, type);
5138 /* Get the file-mode and data size of the file open on FD
5139 and store them in *MODE_POINTER and *SIZE_POINTER. */
5141 static int
5142 file_size_and_mode (fd, mode_pointer, size_pointer)
5143 int fd;
5144 int *mode_pointer;
5145 long *size_pointer;
5147 struct stat sbuf;
5149 if (fstat (fd, &sbuf) < 0) return -1;
5150 if (mode_pointer) *mode_pointer = sbuf.st_mode;
5151 if (size_pointer) *size_pointer = sbuf.st_size;
5152 return 0;