OSDN Git Service

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