OSDN Git Service

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