OSDN Git Service

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