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