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