OSDN Git Service

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