OSDN Git Service

2001-07-30 H.J. Lu (hjl@gnu.org)
[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 *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   register U_CHAR *p = bp;
1079   register 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   register U_CHAR *p = bp;
1114   register 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   register U_CHAR c;
1185
1186   /* Length of pending accumulated identifier.  */
1187   register int ident_length = 0;
1188
1189   /* Hash code of pending accumulated identifier.  */
1190   register int hash = 0;
1191
1192   /* Current input level (&instack[indepth]).  */
1193   FILE_BUF *ip;
1194
1195   /* Pointer for scanning input.  */
1196   register U_CHAR *ibp;
1197
1198   /* Pointer to end of input.  End of scan is controlled by LIMIT.  */
1199   register U_CHAR *limit;
1200
1201   /* Pointer for storing output.  */
1202   register 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 /* Pop the innermost input stack level, assuming it is a macro expansion.  */
1224
1225 #define POPMACRO \
1226 do { ip->macro->type = T_MACRO;         \
1227      if (ip->free_ptr) free (ip->free_ptr);     \
1228      --indepth; } while (0)
1229
1230 /* Reload `rescan's local variables that describe the current
1231    level of the input stack.  */
1232
1233 #define RECACHE  \
1234 do { ip = &instack[indepth];            \
1235      ibp = ip->bufp;                    \
1236      limit = ip->buf + ip->length;      \
1237      op->bufp = obp;                    \
1238      check_expand (op, limit - ibp);    \
1239      beg_of_line = 0;                   \
1240      obp = op->bufp; } while (0)
1241
1242   if (no_output && instack[indepth].fname != 0)
1243     skip_if_group (&instack[indepth], 1);
1244
1245   obp = op->bufp;
1246   RECACHE;
1247   beg_of_line = ibp;
1248
1249   /* Our caller must always put a null after the end of
1250      the input at each input stack level.  */
1251   if (*limit != 0)
1252     abort ();
1253
1254   while (1) {
1255     c = *ibp++;
1256     *obp++ = c;
1257
1258     switch (c) {
1259     case '\\':
1260       if (ibp >= limit)
1261         break;
1262       if (*ibp == '\n') {
1263         /* Always merge lines ending with backslash-newline,
1264            even in middle of identifier.  */
1265         ++ibp;
1266         ++ip->lineno;
1267         --obp;          /* remove backslash from obuf */
1268         break;
1269       }
1270       /* Otherwise, backslash suppresses specialness of following char,
1271          so copy it here to prevent the switch from seeing it.
1272          But first get any pending identifier processed.  */
1273       if (ident_length > 0)
1274         goto specialchar;
1275       *obp++ = *ibp++;
1276       break;
1277
1278     case '#':
1279       /* If this is expanding a macro definition, don't recognize
1280          preprocessor directives.  */
1281       if (ip->macro != 0)
1282         goto randomchar;
1283       if (ident_length)
1284         goto specialchar;
1285
1286       /* # keyword: a # must be the first char on the line */
1287       if (beg_of_line == 0)
1288         goto randomchar;
1289       if (beg_of_line + 1 != ibp)
1290         goto randomchar;
1291
1292       /* This # can start a directive.  */
1293
1294       --obp;            /* Don't copy the '#' */
1295
1296       ip->bufp = ibp;
1297       op->bufp = obp;
1298       if (! handle_directive (ip, op)) {
1299 #ifdef USE_C_ALLOCA
1300         alloca (0);
1301 #endif
1302         /* Not a known directive: treat it as ordinary text.
1303            IP, OP, IBP, etc. have not been changed.  */
1304         if (no_output && instack[indepth].fname) {
1305           /* If not generating expanded output,
1306              what we do with ordinary text is skip it.
1307              Discard everything until next # directive.  */
1308           skip_if_group (&instack[indepth], 1);
1309           RECACHE;
1310           beg_of_line = ibp;
1311           break;
1312         }
1313         ++obp;          /* Copy the '#' after all */
1314         goto randomchar;
1315       }
1316 #ifdef USE_C_ALLOCA
1317       alloca (0);
1318 #endif
1319       /* A # directive has been successfully processed.  */
1320       /* If not generating expanded output, ignore everything until
1321          next # directive.  */
1322       if (no_output && instack[indepth].fname)
1323         skip_if_group (&instack[indepth], 1);
1324       obp = op->bufp;
1325       RECACHE;
1326       beg_of_line = ibp;
1327       break;
1328
1329     case '\"':                  /* skip quoted string */
1330     case '\'':
1331       /* A single quoted string is treated like a double -- some
1332          programs (e.g., troff) are perverse this way */
1333
1334       if (ident_length)
1335         goto specialchar;
1336
1337       start_line = ip->lineno;
1338
1339       /* Skip ahead to a matching quote.  */
1340
1341       while (1) {
1342         if (ibp >= limit) {
1343           if (ip->macro != 0) {
1344             /* try harder: this string crosses a macro expansion boundary */
1345             POPMACRO;
1346             RECACHE;
1347             continue;
1348           }
1349           break;
1350         }
1351         *obp++ = *ibp;
1352         switch (*ibp++) {
1353         case '\n':
1354           ++ip->lineno;
1355           ++op->lineno;
1356           /* Traditionally, end of line ends a string constant with no error.
1357              So exit the loop and record the new line.  */
1358           beg_of_line = ibp;
1359           goto while2end;
1360
1361         case '\\':
1362           if (ibp >= limit)
1363             break;
1364           if (*ibp == '\n') {
1365             /* Backslash newline is replaced by nothing at all,
1366                but keep the line counts correct.  */
1367             --obp;
1368             ++ibp;
1369             ++ip->lineno;
1370           } else {
1371             /* ANSI stupidly requires that in \\ the second \
1372                is *not* prevented from combining with a newline.  */
1373             while (*ibp == '\\' && ibp[1] == '\n') {
1374               ibp += 2;
1375               ++ip->lineno;
1376             }
1377             *obp++ = *ibp++;
1378           }
1379           break;
1380
1381         case '\"':
1382         case '\'':
1383           if (ibp[-1] == c)
1384             goto while2end;
1385           break;
1386         }
1387       }
1388     while2end:
1389       break;
1390
1391     case '/':
1392       if (*ibp == '\\' && ibp[1] == '\n')
1393         newline_fix (ibp);
1394       /* Don't look for comments inside a macro definition.  */
1395       if (ip->macro != 0)
1396         goto randomchar;
1397       /* A comment constitutes white space, so it can terminate an identifier.
1398          Process the identifier, if any.  */
1399       if (ident_length)
1400         goto specialchar;
1401
1402       if (*ibp != '*')
1403         goto randomchar;
1404
1405       /* We have a comment.  Skip it, optionally copying it to output.  */
1406
1407       start_line = ip->lineno;
1408
1409       ++ibp;                    /* Skip the star. */
1410
1411       /* In K+R C, a comment is equivalent to nothing.  Note that we
1412           already output the slash; we might not want it.  */
1413       if (! put_out_comments)
1414         obp--;
1415       else
1416         *obp++ = '*';
1417
1418       {
1419         U_CHAR *before_bp = ibp;
1420
1421         while (ibp < limit) {
1422           switch (*ibp++) {
1423           case '/':
1424             if (warn_comments && ibp < limit && *ibp == '*')
1425               warning("`/*' within comment");
1426             break;
1427           case '*':
1428             if (*ibp == '\\' && ibp[1] == '\n')
1429               newline_fix (ibp);
1430             if (ibp >= limit || *ibp == '/')
1431               goto comment_end;
1432             break;
1433           case '\n':
1434             ++ip->lineno;
1435             /* Copy the newline into the output buffer, in order to
1436                avoid the pain of a #line every time a multiline comment
1437                is seen.  */
1438             if (!put_out_comments)
1439               *obp++ = '\n';
1440             ++op->lineno;
1441           }
1442         }
1443       comment_end:
1444
1445         if (ibp >= limit)
1446           error_with_line (line_for_error (start_line),
1447                            "unterminated comment");
1448         else {
1449           ibp++;
1450           if (put_out_comments) {
1451             memcpy (obp, before_bp, ibp - before_bp);
1452             obp += ibp - before_bp;
1453           }
1454         }
1455       }
1456       break;
1457
1458     case '0': case '1': case '2': case '3': case '4':
1459     case '5': case '6': case '7': case '8': case '9':
1460       /* If digit is not part of identifier, it starts a number,
1461          which means that following letters are not an identifier.
1462          "0x5" does not refer to an identifier "x5".
1463          So copy all alphanumerics that follow without accumulating
1464          as an identifier.  Periods also, for sake of "3.e7".  */
1465
1466       if (ident_length == 0) {
1467         while (ibp < limit) {
1468           while (ibp < limit && ibp[0] == '\\' && ibp[1] == '\n') {
1469             ++ip->lineno;
1470             ibp += 2;
1471           }
1472           c = *ibp++;
1473           if (!ISALNUM (c) && c != '.' && c != '_') {
1474             --ibp;
1475             break;
1476           }
1477           *obp++ = c;
1478           /* A sign can be part of a preprocessing number
1479              if it follows an e.  */
1480           if (c == 'e' || c == 'E') {
1481             while (ibp < limit && ibp[0] == '\\' && ibp[1] == '\n') {
1482               ++ip->lineno;
1483               ibp += 2;
1484             }
1485             if (ibp < limit && (*ibp == '+' || *ibp == '-')) {
1486               *obp++ = *ibp++;
1487               /* Traditional C does not let the token go past the sign.  */
1488               break;
1489             }
1490           }
1491         }
1492         break;
1493       }
1494       /* fall through */
1495
1496     case '_':
1497     case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
1498     case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
1499     case 'm': case 'n': case 'o': case 'p': case 'q': case 'r':
1500     case 's': case 't': case 'u': case 'v': case 'w': case 'x':
1501     case 'y': case 'z':
1502     case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
1503     case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
1504     case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R':
1505     case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
1506     case 'Y': case 'Z':
1507       ident_length++;
1508       /* Compute step of hash function, to avoid a proc call on every token */
1509       hash = HASHSTEP (hash, c);
1510       break;
1511
1512     case '\n':
1513       /* If reprocessing a macro expansion, newline is a special marker.  */
1514       if (ip->macro != 0) {
1515         /* Newline White is a "funny space" to separate tokens that are
1516            supposed to be separate but without space between.
1517            Here White means any horizontal whitespace character.
1518            Newline - marks a recursive macro use that is not
1519            supposed to be expandable.  */
1520
1521         if (*ibp == '-') {
1522           /* Newline - inhibits expansion of preceding token.
1523              If expanding a macro arg, we keep the newline -.
1524              In final output, it is deleted.  */
1525           if (! concatenated) {
1526             ident_length = 0;
1527             hash = 0;
1528           }
1529           ibp++;
1530           if (!output_marks) {
1531             obp--;
1532           } else {
1533             /* If expanding a macro arg, keep the newline -.  */
1534             *obp++ = '-';
1535           }
1536         } else if (is_space (*ibp)) {
1537           /* Newline Space does not prevent expansion of preceding token
1538              so expand the preceding token and then come back.  */
1539           if (ident_length > 0)
1540             goto specialchar;
1541
1542           /* If generating final output, newline space makes a space.  */
1543           if (!output_marks) {
1544             obp[-1] = *ibp++;
1545             /* And Newline Newline makes a newline, so count it.  */
1546             if (obp[-1] == '\n')
1547               op->lineno++;
1548           } else {
1549             /* If expanding a macro arg, keep the newline space.
1550                If the arg gets stringified, newline space makes nothing.  */
1551             *obp++ = *ibp++;
1552           }
1553         } else abort ();        /* Newline followed by something random?  */
1554         break;
1555       }
1556
1557       /* If there is a pending identifier, handle it and come back here.  */
1558       if (ident_length > 0)
1559         goto specialchar;
1560
1561       beg_of_line = ibp;
1562
1563       /* Update the line counts and output a #line if necessary.  */
1564       ++ip->lineno;
1565       ++op->lineno;
1566       if (ip->lineno != op->lineno) {
1567         op->bufp = obp;
1568         output_line_command (ip, op, 1, same_file);
1569         check_expand (op, ip->length - (ip->bufp - ip->buf));
1570         obp = op->bufp;
1571       }
1572       break;
1573
1574       /* Come here either after (1) a null character that is part of the input
1575          or (2) at the end of the input, because there is a null there.  */
1576     case 0:
1577       if (ibp <= limit)
1578         /* Our input really contains a null character.  */
1579         goto randomchar;
1580
1581       /* At end of a macro-expansion level, pop it and read next level.  */
1582       if (ip->macro != 0) {
1583         obp--;
1584         ibp--;
1585         /* If we have an identifier that ends here, process it now, so
1586            we get the right error for recursion.  */
1587         if (ident_length && ! is_idchar (*instack[indepth - 1].bufp)) {
1588           redo_char = 1;
1589           goto randomchar;
1590         }
1591         POPMACRO;
1592         RECACHE;
1593         break;
1594       }
1595
1596       /* If we don't have a pending identifier,
1597          return at end of input.  */
1598       if (ident_length == 0) {
1599         obp--;
1600         ibp--;
1601         op->bufp = obp;
1602         ip->bufp = ibp;
1603         goto ending;
1604       }
1605
1606       /* If we do have a pending identifier, just consider this null
1607          a special character and arrange to dispatch on it again.
1608          The second time, IDENT_LENGTH will be zero so we will return.  */
1609
1610       /* Fall through */
1611
1612 specialchar:
1613
1614       /* Handle the case of a character such as /, ', " or null
1615          seen following an identifier.  Back over it so that
1616          after the identifier is processed the special char
1617          will be dispatched on again.  */
1618
1619       ibp--;
1620       obp--;
1621       redo_char = 1;
1622
1623     default:
1624
1625 randomchar:
1626
1627       if (ident_length > 0) {
1628         register HASHNODE *hp;
1629
1630         /* We have just seen an identifier end.  If it's a macro, expand it.
1631
1632            IDENT_LENGTH is the length of the identifier
1633            and HASH is its hash code.
1634
1635            The identifier has already been copied to the output,
1636            so if it is a macro we must remove it.
1637
1638            If REDO_CHAR is 0, the char that terminated the identifier
1639            has been skipped in the output and the input.
1640            OBP-IDENT_LENGTH-1 points to the identifier.
1641            If the identifier is a macro, we must back over the terminator.
1642
1643            If REDO_CHAR is 1, the terminating char has already been
1644            backed over.  OBP-IDENT_LENGTH points to the identifier.  */
1645
1646         for (hp = hashtab[MAKE_POS (hash) % HASHSIZE]; hp != NULL;
1647              hp = hp->next) {
1648
1649           if (hp->length == ident_length) {
1650             U_CHAR *obufp_before_macroname;
1651             int op_lineno_before_macroname;
1652             register int i = ident_length;
1653             register U_CHAR *p = hp->name;
1654             register U_CHAR *q = obp - i;
1655
1656             if (! redo_char)
1657               q--;
1658
1659             do {                /* All this to avoid a strncmp () */
1660               if (*p++ != *q++)
1661                 goto hashcollision;
1662             } while (--i);
1663
1664             /* We found a use of a macro name.
1665                see if the context shows it is a macro call.  */
1666
1667             /* Back up over terminating character if not already done.  */
1668             if (! redo_char) {
1669               ibp--;
1670               obp--;
1671             }
1672
1673             obufp_before_macroname = obp - ident_length;
1674             op_lineno_before_macroname = op->lineno;
1675
1676             /* If macro wants an arglist, verify that a '(' follows.
1677                first skip all whitespace, copying it to the output
1678                after the macro name.  Then, if there is no '(',
1679                decide this is not a macro call and leave things that way.  */
1680             if (hp->type == T_MACRO && hp->value.defn->nargs >= 0)
1681               {
1682                 while (1) {
1683                   /* Scan forward over whitespace, copying it to the output.  */
1684                   if (ibp == limit && ip->macro != 0) {
1685                     POPMACRO;
1686                     RECACHE;
1687                   }
1688                   /* A comment: copy it unchanged or discard it.  */
1689                   else if (*ibp == '/' && ibp+1 != limit && ibp[1] == '*') {
1690                     if (put_out_comments) {
1691                       *obp++ = '/';
1692                       *obp++ = '*';
1693                     }
1694                     ibp += 2;
1695                     while (ibp + 1 != limit
1696                            && !(ibp[0] == '*' && ibp[1] == '/')) {
1697                       /* We need not worry about newline-marks,
1698                          since they are never found in comments.  */
1699                       if (*ibp == '\n') {
1700                         /* Newline in a file.  Count it.  */
1701                         ++ip->lineno;
1702                         ++op->lineno;
1703                       }
1704                       if (put_out_comments)
1705                         *obp++ = *ibp++;
1706                       else
1707                         ibp++;
1708                     }
1709                     ibp += 2;
1710                     if (put_out_comments) {
1711                       *obp++ = '*';
1712                       *obp++ = '/';
1713                     }
1714                   }
1715                   else if (is_space (*ibp)) {
1716                     *obp++ = *ibp++;
1717                     if (ibp[-1] == '\n') {
1718                       if (ip->macro == 0) {
1719                         /* Newline in a file.  Count it.  */
1720                         ++ip->lineno;
1721                         ++op->lineno;
1722                       } else if (!output_marks) {
1723                         /* A newline mark, and we don't want marks
1724                            in the output.  If it is newline-hyphen,
1725                            discard it entirely.  Otherwise, it is
1726                            newline-whitechar, so keep the whitechar.  */
1727                         obp--;
1728                         if (*ibp == '-')
1729                           ibp++;
1730                         else {
1731                           if (*ibp == '\n')
1732                             ++op->lineno;
1733                           *obp++ = *ibp++;
1734                         }
1735                       } else {
1736                         /* A newline mark; copy both chars to the output.  */
1737                         *obp++ = *ibp++;
1738                       }
1739                     }
1740                   }
1741                   else break;
1742                 }
1743                 if (*ibp != '(')
1744                   break;
1745               }
1746
1747             /* This is now known to be a macro call.
1748                Discard the macro name from the output,
1749                along with any following whitespace just copied.  */
1750             obp = obufp_before_macroname;
1751             op->lineno = op_lineno_before_macroname;
1752
1753             /* Expand the macro, reading arguments as needed,
1754                and push the expansion on the input stack.  */
1755             ip->bufp = ibp;
1756             op->bufp = obp;
1757             macroexpand (hp, op);
1758
1759             /* Reexamine input stack, since macroexpand has pushed
1760                a new level on it.  */
1761             obp = op->bufp;
1762             RECACHE;
1763             break;
1764           }
1765 hashcollision:
1766                ;
1767         }                       /* End hash-table-search loop */
1768         ident_length = hash = 0; /* Stop collecting identifier */
1769         redo_char = 0;
1770         concatenated = 0;
1771       }                         /* End if (ident_length > 0) */
1772     }                           /* End switch */
1773   }                             /* End per-char loop */
1774
1775   /* Come here to return -- but first give an error message
1776      if there was an unterminated successful conditional.  */
1777  ending:
1778   if (if_stack != ip->if_stack) {
1779     const char *str;
1780     switch (if_stack->type) {
1781     case T_IF:
1782       str = "if";
1783       break;
1784     case T_IFDEF:
1785       str = "ifdef";
1786       break;
1787     case T_IFNDEF:
1788       str = "ifndef";
1789       break;
1790     case T_ELSE:
1791       str = "else";
1792       break;
1793     case T_ELIF:
1794       str = "elif";
1795       break;
1796     default:
1797       abort ();
1798     }
1799     error_with_line (line_for_error (if_stack->lineno),
1800                      "unterminated #%s conditional", str);
1801   }
1802   if_stack = ip->if_stack;
1803 }
1804 \f
1805 /*
1806  * Rescan a string into a temporary buffer and return the result
1807  * as a FILE_BUF.  Note this function returns a struct, not a pointer.
1808  *
1809  * OUTPUT_MARKS nonzero means keep Newline markers found in the input
1810  * and insert such markers when appropriate.  See `rescan' for details.
1811  * OUTPUT_MARKS is 1 for macroexpanding a macro argument separately
1812  * before substitution; it is 0 for other uses.
1813  */
1814 static FILE_BUF
1815 expand_to_temp_buffer (buf, limit, output_marks)
1816      const U_CHAR *buf, *limit;
1817      int output_marks;
1818 {
1819   register FILE_BUF *ip;
1820   FILE_BUF obuf;
1821   int length = limit - buf;
1822   U_CHAR *buf1;
1823   int odepth = indepth;
1824
1825   if (length < 0)
1826     abort ();
1827
1828   /* Set up the input on the input stack.  */
1829
1830   buf1 = (U_CHAR *) alloca (length + 1);
1831   {
1832     register const U_CHAR *p1 = buf;
1833     register U_CHAR *p2 = buf1;
1834
1835     while (p1 != limit)
1836       *p2++ = *p1++;
1837   }
1838   buf1[length] = 0;
1839
1840   /* Set up to receive the output.  */
1841
1842   obuf.length = length * 2 + 100; /* Usually enough.  Why be stingy?  */
1843   obuf.bufp = obuf.buf = (U_CHAR *) xmalloc (obuf.length);
1844   obuf.fname = 0;
1845   obuf.macro = 0;
1846   obuf.free_ptr = 0;
1847
1848   CHECK_DEPTH ({return obuf;});
1849
1850   ++indepth;
1851
1852   ip = &instack[indepth];
1853   ip->fname = 0;
1854   ip->macro = 0;
1855   ip->free_ptr = 0;
1856   ip->length = length;
1857   ip->buf = ip->bufp = buf1;
1858   ip->if_stack = if_stack;
1859
1860   ip->lineno = obuf.lineno = 1;
1861
1862   /* Scan the input, create the output.  */
1863
1864   rescan (&obuf, output_marks);
1865
1866   /* Pop input stack to original state.  */
1867   --indepth;
1868
1869   if (indepth != odepth)
1870     abort ();
1871
1872   /* Record the output.  */
1873   obuf.length = obuf.bufp - obuf.buf;
1874
1875   return obuf;
1876 }
1877 \f
1878 /*
1879  * Process a # directive.  Expects IP->bufp to point to the '#', as in
1880  * `#define foo bar'.  Passes to the command handler
1881  * (do_define, do_include, etc.): the addresses of the 1st and
1882  * last chars of the command (starting immediately after the #
1883  * keyword), plus op and the keyword table pointer.  If the command
1884  * contains comments it is copied into a temporary buffer sans comments
1885  * and the temporary buffer is passed to the command handler instead.
1886  * Likewise for backslash-newlines.
1887  *
1888  * Returns nonzero if this was a known # directive.
1889  * Otherwise, returns zero, without advancing the input pointer.
1890  */
1891
1892 static int
1893 handle_directive (ip, op)
1894      FILE_BUF *ip, *op;
1895 {
1896   register U_CHAR *bp, *cp;
1897   register struct directive *kt;
1898   register int ident_length;
1899   U_CHAR *resume_p;
1900
1901   /* Nonzero means we must copy the entire command
1902      to get rid of comments or backslash-newlines.  */
1903   int copy_command = 0;
1904
1905   U_CHAR *ident, *after_ident;
1906
1907   bp = ip->bufp;
1908   /* Skip whitespace and \-newline.  */
1909   while (1) {
1910     if (is_nvspace (*bp))
1911       bp++;
1912     else if (*bp == '/' && (newline_fix (bp + 1), bp[1]) == '*') {
1913       ip->bufp = bp;
1914       skip_to_end_of_comment (ip, &ip->lineno);
1915       bp = ip->bufp;
1916     } else if (*bp == '\\' && bp[1] == '\n') {
1917       bp += 2; ip->lineno++;
1918     } else break;
1919   }
1920
1921   /* Now find end of directive name.
1922      If we encounter a backslash-newline, exchange it with any following
1923      symbol-constituents so that we end up with a contiguous name.  */
1924
1925   cp = bp;
1926   while (1) {
1927     if (is_idchar (*cp))
1928       cp++;
1929     else {
1930       if (*cp == '\\' && cp[1] == '\n')
1931         name_newline_fix (cp);
1932       if (is_idchar (*cp))
1933         cp++;
1934       else break;
1935     }
1936   }
1937   ident_length = cp - bp;
1938   ident = bp;
1939   after_ident = cp;
1940
1941   /* A line of just `#' becomes blank.  */
1942
1943   if (ident_length == 0 && *after_ident == '\n') {
1944     ip->bufp = after_ident;
1945     return 1;
1946   }
1947
1948   /*
1949    * Decode the keyword and call the appropriate expansion
1950    * routine, after moving the input pointer up to the next line.
1951    */
1952   for (kt = directive_table; kt->length > 0; kt++) {
1953     if (kt->length == ident_length
1954         && !strncmp (kt->name, (const char *)ident, ident_length)) {
1955       register U_CHAR *buf;
1956       register U_CHAR *limit = ip->buf + ip->length;
1957       int unterminated = 0;
1958
1959       /* Nonzero means do not delete comments within the directive.
1960          #define needs this to detect traditional token paste.  */
1961       int keep_comments = kt->type == T_DEFINE;
1962
1963       /* Find the end of this command (first newline not backslashed
1964          and not in a string or comment).
1965          Set COPY_COMMAND if the command must be copied
1966          (it contains a backslash-newline or a comment).  */
1967
1968       buf = bp = after_ident;
1969       while (bp < limit) {
1970         register U_CHAR c = *bp++;
1971         switch (c) {
1972         case '\\':
1973           if (bp < limit) {
1974             if (*bp == '\n') {
1975               ip->lineno++;
1976               copy_command = 1;
1977             }
1978             bp++;
1979           }
1980           break;
1981
1982         case '\'':
1983         case '\"':
1984           bp = skip_quoted_string (bp - 1, limit, ip->lineno, &ip->lineno, &copy_command, &unterminated);
1985           if (unterminated) {
1986             /* Traditional preprocessing permits unterminated strings.  */
1987             ip->bufp = bp;
1988             goto endloop1;
1989           }
1990           break;
1991
1992           /* <...> is special for #include.  */
1993         case '<':
1994           if (kt->type != T_INCLUDE)
1995             break;
1996           while (*bp && *bp != '>') bp++;
1997           break;
1998
1999         case '/':
2000           if (*bp == '\\' && bp[1] == '\n')
2001             newline_fix (bp);
2002           if (*bp == '*') {
2003             U_CHAR *obp = bp - 1;
2004             ip->bufp = bp + 1;
2005             skip_to_end_of_comment (ip, &ip->lineno);
2006             bp = ip->bufp;
2007             /* No need to copy the command because of a comment at the end;
2008                just don't include the comment in the directive.  */
2009             if (bp == limit || *bp == '\n') {
2010               bp = obp;
2011               goto endloop1;
2012             }
2013             /* Don't remove the comments if this is #define.  */
2014             if (! keep_comments)
2015               copy_command++;
2016           }
2017           break;
2018
2019         case '\n':
2020           --bp;         /* Point to the newline */
2021           ip->bufp = bp;
2022           goto endloop1;
2023         }
2024       }
2025       ip->bufp = bp;
2026
2027     endloop1:
2028       resume_p = ip->bufp;
2029       /* BP is the end of the directive.
2030          RESUME_P is the next interesting data after the directive.
2031          A comment may come between.  */
2032
2033       if (copy_command) {
2034         register U_CHAR *xp = buf;
2035         /* Need to copy entire command into temp buffer before dispatching */
2036
2037         cp = (U_CHAR *) alloca (bp - buf + 5); /* room for cmd plus
2038                                                   some slop */
2039         buf = cp;
2040
2041         /* Copy to the new buffer, deleting comments
2042            and backslash-newlines (and whitespace surrounding the latter).  */
2043
2044         while (xp < bp) {
2045           register U_CHAR c = *xp++;
2046           *cp++ = c;
2047
2048           switch (c) {
2049           case '\n':
2050             break;
2051
2052             /* <...> is special for #include.  */
2053           case '<':
2054             if (kt->type != T_INCLUDE)
2055               break;
2056             while (xp < bp && c != '>') {
2057               c = *xp++;
2058               if (c == '\\' && xp < bp && *xp == '\n')
2059                 xp++, ip->lineno++;
2060               else
2061                 *cp++ = c;
2062             }
2063             break;
2064
2065           case '\\':
2066             if (*xp == '\n') {
2067               xp++;
2068               cp--;
2069               if (cp != buf && is_space (cp[-1])) {
2070                 while (cp != buf && is_space(cp[-1])) cp--;
2071                 cp++;
2072                 SKIP_WHITE_SPACE (xp);
2073               } else if (is_space (*xp)) {
2074                 *cp++ = *xp++;
2075                 SKIP_WHITE_SPACE (xp);
2076               }
2077             } else {
2078               *cp++ = *xp++;
2079             }
2080             break;
2081
2082           case '\'':
2083           case '\"':
2084             {
2085               register const U_CHAR *bp1
2086                 = skip_quoted_string (xp - 1, limit, ip->lineno, 0, 0, 0);
2087               while (xp != bp1)
2088                 *cp++ = *xp++;
2089             }
2090             break;
2091
2092           case '/':
2093             if (*xp == '*') {
2094               ip->bufp = xp + 1;
2095               skip_to_end_of_comment (ip, 0);
2096               if (keep_comments)
2097                 while (xp != ip->bufp)
2098                   *cp++ = *xp++;
2099               /* Delete the slash.  */
2100               else
2101                 cp--;
2102               xp = ip->bufp;
2103             }
2104           }
2105         }
2106
2107         /* Null-terminate the copy.  */
2108
2109         *cp = 0;
2110       }
2111       else
2112         cp = bp;
2113
2114       ip->bufp = resume_p;
2115
2116       /* Call the appropriate command handler.  buf now points to
2117          either the appropriate place in the input buffer, or to
2118          the temp buffer if it was necessary to make one.  cp
2119          points to the first char after the contents of the (possibly
2120          copied) command, in either case. */
2121       (*kt->func) (buf, cp, op);
2122       check_expand (op, ip->length - (ip->bufp - ip->buf));
2123
2124       return 1;
2125     }
2126   }
2127
2128   return 0;
2129 }
2130 \f
2131 static const char *const
2132 monthnames[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
2133                 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
2134
2135 /*
2136  * expand things like __FILE__.  Place the expansion into the output
2137  * buffer *without* rescanning.
2138  */
2139 static void
2140 special_symbol (hp, op)
2141      HASHNODE *hp;
2142      FILE_BUF *op;
2143 {
2144   const char *buf;
2145   time_t t;
2146   int i, len;
2147   int true_indepth;
2148   FILE_BUF *ip = NULL;
2149   static struct tm *timebuf = NULL;
2150
2151   int paren = 0;                /* For special `defined' keyword */
2152
2153   for (i = indepth; i >= 0; i--)
2154     if (instack[i].fname != NULL) {
2155       ip = &instack[i];
2156       break;
2157     }
2158   if (ip == NULL)
2159     fatal ("not in any file?!");
2160
2161   switch (hp->type) {
2162   case T_FILE:
2163   case T_BASE_FILE:
2164     {
2165       const char *string;
2166       if (hp->type == T_FILE)
2167         string = ip->fname;
2168       else
2169         string = instack[0].fname;
2170
2171       if (string)
2172         {
2173           char *tmp = (char *) alloca (3 + strlen (string));
2174           sprintf (tmp, "\"%s\"", string);
2175           buf = tmp;
2176         }
2177       else
2178         buf = "";
2179
2180       break;
2181     }
2182
2183   case T_INCLUDE_LEVEL:
2184     {
2185       char *tmp = (char *) alloca (8);  /* Eigth bytes ought to be more than enough */
2186       true_indepth = 0;
2187       for (i = indepth; i >= 0; i--)
2188         if (instack[i].fname != NULL)
2189           true_indepth++;
2190
2191     sprintf (tmp, "%d", true_indepth - 1);
2192     buf = tmp;
2193     break;
2194     }
2195
2196   case T_VERSION:
2197     {
2198       char *tmp = (char *) alloca (3 + strlen (version_string));
2199       sprintf (tmp, "\"%s\"", version_string);
2200       buf = tmp;
2201       break;
2202     }
2203
2204   case T_CONST:
2205     buf = hp->value.cpval;
2206     break;
2207
2208   case T_SPECLINE:
2209     {
2210       char *tmp = (char *) alloca (10);
2211       sprintf (tmp, "%d", ip->lineno);
2212       buf = tmp;
2213       break;
2214     }
2215
2216   case T_DATE:
2217   case T_TIME:
2218     {
2219       char *tmp = (char *) alloca (20);
2220
2221       if (timebuf == NULL) {
2222         t = time (0);
2223         timebuf = localtime (&t);
2224       }
2225       if (hp->type == T_DATE)
2226         sprintf (tmp, "\"%s %2d %4d\"", monthnames[timebuf->tm_mon],
2227                  timebuf->tm_mday, timebuf->tm_year + 1900);
2228       else
2229         sprintf (tmp, "\"%02d:%02d:%02d\"", timebuf->tm_hour, timebuf->tm_min,
2230                  timebuf->tm_sec);
2231       buf = tmp;
2232       break;
2233     }
2234
2235   case T_SPEC_DEFINED:
2236     buf = " 0 ";                        /* Assume symbol is not defined */
2237     ip = &instack[indepth];
2238     SKIP_WHITE_SPACE (ip->bufp);
2239     if (*ip->bufp == '(') {
2240       paren++;
2241       ip->bufp++;                       /* Skip over the paren */
2242       SKIP_WHITE_SPACE (ip->bufp);
2243     }
2244
2245     if (!is_idstart (*ip->bufp))
2246       goto oops;
2247     {
2248       HASHNODE *hp = lookup (ip->bufp, -1, -1);
2249
2250       if (hp && hp->type != T_UNUSED && hp->type != T_SPEC_DEFINED)
2251         buf = " 1 ";
2252     }
2253     while (is_idchar (*ip->bufp))
2254       ++ip->bufp;
2255     SKIP_WHITE_SPACE (ip->bufp);
2256     if (paren) {
2257       if (*ip->bufp != ')')
2258         goto oops;
2259       ++ip->bufp;
2260     }
2261     break;
2262
2263 oops:
2264
2265     error ("`defined' must be followed by ident or (ident)");
2266     break;
2267
2268   default:
2269     error ("cccp error: invalid special hash type"); /* time for gdb */
2270     abort ();
2271   }
2272   len = strlen (buf);
2273   check_expand (op, len);
2274   memcpy (op->bufp, buf, len);
2275   op->bufp += len;
2276 }
2277
2278 \f
2279 /* Routines to handle #directives */
2280
2281 /*
2282  * Process include file by reading it in and calling rescan.
2283  * Expects to see "fname" or <fname> on the input.
2284  */
2285 static void
2286 do_include (buf, limit, op)
2287      U_CHAR *buf, *limit;
2288      FILE_BUF *op;
2289 {
2290   U_CHAR *fbeg, *fend;          /* Beginning and end of fname */
2291
2292   struct file_name_list *stackp = include; /* Chain of dirs to search */
2293   struct file_name_list dsp[1]; /* First in chain, if #include "..." */
2294   int flen;
2295
2296   int retried = 0;              /* Have already tried macro
2297                                    expanding the include line*/
2298   FILE_BUF trybuf;              /* It got expanded into here */
2299   int system_header_p = 0;      /* 0 for "...", 1 for <...> */
2300
2301 get_filename:
2302
2303   fbeg = buf;
2304   SKIP_WHITE_SPACE (fbeg);
2305   /* Discard trailing whitespace so we can easily see
2306      if we have parsed all the significant chars we were given.  */
2307   while (limit != fbeg && is_nvspace (limit[-1])) limit--;
2308
2309   switch (*fbeg++) {
2310   case '\"':
2311     fend = fbeg;
2312     while (fend != limit && *fend != '\"')
2313       fend++;
2314     if (*fend == '\"' && fend + 1 == limit) {
2315       FILE_BUF *fp;
2316
2317       /* We have "filename".  Figure out directory this source
2318          file is coming from and put it on the front of the list. */
2319
2320       /* If -I- was specified, don't search current dir, only spec'd ones. */
2321       if (ignore_srcdir) break;
2322
2323       for (fp = &instack[indepth]; fp >= instack; fp--)
2324         {
2325           size_t n;
2326           const char *ep, *nam;
2327
2328           if ((nam = fp->fname) != NULL) {
2329             /* Found a named file.  Figure out dir of the file,
2330                and put it in front of the search list.  */
2331             dsp[0].next = stackp;
2332             stackp = dsp;
2333             ep = strrchr (nam, '/');
2334             if (ep != NULL) {
2335               char *f; 
2336               n = ep - nam;
2337               f = (char *) alloca (n + 1);
2338               strncpy (f, nam, n);
2339               f[n] = '\0';
2340               dsp[0].fname = f;
2341               if (n > max_include_len) max_include_len = n;
2342             } else {
2343               dsp[0].fname = 0; /* Current directory */
2344             }
2345             break;
2346           }
2347         }
2348       break;
2349     }
2350     goto fail;
2351
2352   case '<':
2353     fend = fbeg;
2354     while (fend != limit && *fend != '>') fend++;
2355     if (*fend == '>' && fend + 1 == limit) {
2356       system_header_p = 1;
2357       /* If -I-, start with the first -I dir after the -I-.  */
2358       if (first_bracket_include)
2359         stackp = first_bracket_include;
2360       break;
2361     }
2362     goto fail;
2363
2364   default:
2365   fail:
2366     if (retried) {
2367       error ("#include expects \"fname\" or <fname>");
2368       return;
2369     } else {
2370       trybuf = expand_to_temp_buffer (buf, limit, 0);
2371       buf = (U_CHAR *) alloca (trybuf.bufp - trybuf.buf + 1);
2372       memcpy (buf, trybuf.buf, trybuf.bufp - trybuf.buf);
2373       limit = buf + (trybuf.bufp - trybuf.buf);
2374       free (trybuf.buf);
2375       retried++;
2376       goto get_filename;
2377     }
2378   }
2379
2380   flen = fend - fbeg;
2381   process_include (stackp, fbeg, flen, system_header_p, op);
2382 }
2383
2384 static void
2385 do_include_next (buf, limit, op)
2386      U_CHAR *buf, *limit;
2387      FILE_BUF *op;
2388 {
2389   U_CHAR *fbeg, *fend;          /* Beginning and end of fname */
2390
2391   struct file_name_list *stackp; /* Chain of dirs to search */
2392   int flen;
2393
2394   int retried = 0;              /* Have already tried macro
2395                                    expanding the include line*/
2396   FILE_BUF trybuf;              /* It got expanded into here */
2397   int system_header_p = 0;      /* 0 for "...", 1 for <...> */
2398
2399   /* Treat as plain #include if we don't know where to start
2400      looking.  */
2401   stackp = instack[indepth].next_header_dir;
2402   if (stackp == 0)
2403     {
2404       do_include (buf, limit, op);
2405       return;
2406     }
2407
2408 get_filename:
2409
2410   fbeg = buf;
2411   SKIP_WHITE_SPACE (fbeg);
2412   /* Discard trailing whitespace so we can easily see
2413      if we have parsed all the significant chars we were given.  */
2414   while (limit != fbeg && is_nvspace (limit[-1])) limit--;
2415
2416   switch (*fbeg++) {
2417   case '\"':
2418     fend = fbeg;
2419     while (fend != limit && *fend != '\"')
2420       fend++;
2421     if (*fend == '\"' && fend + 1 == limit)
2422       break;
2423     goto fail;
2424
2425   case '<':
2426     fend = fbeg;
2427     while (fend != limit && *fend != '>') fend++;
2428     if (*fend == '>' && fend + 1 == limit) {
2429       system_header_p = 1;
2430       break;
2431     }
2432     goto fail;
2433
2434   default:
2435   fail:
2436     if (retried) {
2437       error ("#include expects \"fname\" or <fname>");
2438       return;
2439     } else {
2440       trybuf = expand_to_temp_buffer (buf, limit, 0);
2441       buf = (U_CHAR *) alloca (trybuf.bufp - trybuf.buf + 1);
2442       memcpy (buf, trybuf.buf, trybuf.bufp - trybuf.buf);
2443       limit = buf + (trybuf.bufp - trybuf.buf);
2444       free (trybuf.buf);
2445       retried++;
2446       goto get_filename;
2447     }
2448   }
2449
2450   flen = fend - fbeg;
2451   process_include (stackp, fbeg, flen, system_header_p, op);
2452 }
2453
2454 static void
2455 process_include (stackp, fbeg, flen, system_header_p, op)
2456      struct file_name_list *stackp;
2457      const U_CHAR *fbeg;
2458      int flen;
2459      int system_header_p;
2460      FILE_BUF *op;
2461 {
2462   char *fname;
2463   int f = -1;                   /* file number */
2464
2465   fname = (char *) alloca (max_include_len + flen + 2);
2466   /* + 2 above for slash and terminating null.  */
2467
2468   /* If specified file name is absolute, just open it.  */
2469
2470   if (IS_ABSOLUTE_PATHNAME (fbeg)) {
2471     strncpy (fname, (const char *)fbeg, flen);
2472     fname[flen] = 0;
2473     f = open (fname, O_RDONLY, 0666);
2474   } else {
2475     /* Search directory path, trying to open the file.
2476        Copy each filename tried into FNAME.  */
2477
2478     for (; stackp; stackp = stackp->next) {
2479       if (stackp->fname) {
2480         strcpy (fname, stackp->fname);
2481         strcat (fname, "/");
2482         fname[strlen (fname) + flen] = 0;
2483       } else {
2484         fname[0] = 0;
2485       }
2486       strncat (fname, (const char *)fbeg, flen);
2487       if ((f = open (fname, O_RDONLY, 0666)) >= 0)
2488         break;
2489     }
2490   }
2491
2492   if (f < 0) {
2493     strncpy (fname, (const char *)fbeg, flen);
2494     fname[flen] = 0;
2495     if (deps_missing_files
2496         && print_deps > (system_header_p || (system_include_depth > 0))) {
2497
2498       /* If requested as a system header, assume it belongs in
2499          the first system header directory. */
2500       if (first_bracket_include)
2501         stackp = first_bracket_include;
2502       else
2503         stackp = include;
2504
2505       if (!system_header_p || IS_ABSOLUTE_PATHNAME (fbeg) || !stackp->fname)
2506         deps_add_dep (deps, fname);
2507       else {
2508         char *p;
2509         int len = strlen(stackp->fname);
2510
2511         p = (char *) alloca (len + flen + 2);
2512         memcpy (p, stackp->fname, len);
2513         p[len++] = '/';
2514         memcpy (p + len, fbeg, flen);
2515         len += flen;
2516         p[len] = '\0';
2517         deps_add_dep (deps, p);
2518       }
2519     } else if (print_deps
2520                && print_deps <= (system_header_p
2521                                  || (system_include_depth > 0)))
2522       warning ("No include path in which to find %.*s", flen, fbeg);
2523     else
2524       error_from_errno (fname);
2525
2526   } else {
2527
2528     /* Check to see if this include file is a once-only include file.
2529        If so, give up.  */
2530
2531     struct file_name_list* ptr;
2532
2533     for (ptr = dont_repeat_files; ptr; ptr = ptr->next) {
2534       if (!strcmp (ptr->fname, fname)) {
2535         close (f);
2536         return;                         /* This file was once'd. */
2537       }
2538     }
2539
2540     for (ptr = all_include_files; ptr; ptr = ptr->next) {
2541       if (!strcmp (ptr->fname, fname))
2542         break;                          /* This file was included before. */
2543     }
2544
2545     if (ptr == 0) {
2546       /* This is the first time for this file.  */
2547       /* Add it to list of files included.  */
2548
2549       ptr = (struct file_name_list *) xmalloc (sizeof (struct file_name_list));
2550       ptr->next = all_include_files;
2551       all_include_files = ptr;
2552       ptr->fname = xstrdup (fname);
2553
2554       /* For -M, add this file to the dependencies.  */
2555       if (print_deps > (system_header_p || (system_include_depth > 0)))
2556         deps_add_dep (deps, fname);
2557     }   
2558
2559     if (system_header_p)
2560       system_include_depth++;
2561
2562     /* Actually process the file.  */
2563     finclude (f, fname, stackp->next, op);
2564
2565     if (system_header_p)
2566       system_include_depth--;
2567
2568     close (f);
2569   }
2570 }
2571
2572 /* Process the contents of include file FNAME, already open on descriptor F,
2573    with output to OP.  */
2574
2575 static void
2576 finclude (f, fname, nhd, op)
2577      int f;
2578      const char *fname;
2579      struct file_name_list *nhd;
2580      FILE_BUF *op;
2581 {
2582   int st_mode;
2583   long st_size;
2584   long i;
2585   FILE_BUF *fp;                 /* For input stack frame */
2586
2587   CHECK_DEPTH (return;);
2588
2589   if (file_size_and_mode (f, &st_mode, &st_size))
2590     goto nope;
2591
2592   fp = &instack[indepth + 1];
2593   memset (fp, 0, sizeof (FILE_BUF));
2594   fp->fname = fname;
2595   fp->length = 0;
2596   fp->lineno = 1;
2597   fp->if_stack = if_stack;
2598   fp->next_header_dir = nhd;
2599
2600   if (S_ISREG (st_mode)) {
2601     fp->buf = (U_CHAR *) xmalloc (st_size + 2);
2602     fp->bufp = fp->buf;
2603
2604     /* Read the file contents, knowing that st_size is an upper bound
2605        on the number of bytes we can read.  */
2606     while (st_size > 0) {
2607       i = read (f, fp->buf + fp->length, st_size);
2608       if (i <= 0) {
2609         if (i == 0) break;
2610         goto nope;
2611       }
2612       fp->length += i;
2613       st_size -= i;
2614     }
2615   }
2616   else {
2617     /* Cannot count its file size before reading.  */
2618
2619     U_CHAR *bufp;
2620     U_CHAR *basep;
2621     int bsize = 2000;
2622
2623     st_size = 0;
2624     basep = (U_CHAR *) xmalloc (bsize + 2);
2625     bufp = basep;
2626
2627     for (;;) {
2628       i = read (f, bufp, bsize - st_size);
2629       if (i < 0)
2630         goto nope;      /* error! */
2631       if (i == 0)
2632         break;  /* End of file */
2633       st_size += i;
2634       bufp += i;
2635       if (bsize == st_size) {   /* Buffer is full! */
2636           bsize *= 2;
2637           basep = (U_CHAR *) xrealloc (basep, bsize + 2);
2638           bufp = basep + st_size;       /* May have moved */
2639         }
2640     }
2641     fp->buf = basep;
2642     fp->bufp = fp->buf;
2643     fp->length = st_size;
2644   }
2645   close (f);
2646
2647   /* Make sure data ends with a newline.  And put a null after it.  */
2648
2649   if (fp->length > 0 && fp->buf[fp->length-1] != '\n')
2650     fp->buf[fp->length++] = '\n';
2651   fp->buf[fp->length] = '\0';
2652
2653   indepth++;
2654   output_line_command (fp, op, 0, enter_file);
2655   rescan (op, 0);
2656   indepth--;
2657   instack[indepth].lineno++;
2658   instack[indepth].bufp++;      /* Skip the new line.  */
2659   output_line_command (&instack[indepth], op, 0, leave_file);
2660   free (fp->buf);
2661   return;
2662
2663 nope:
2664   perror_with_name (fname);
2665   close (f);
2666 }
2667 \f
2668
2669 /* Process a #define command.
2670 BUF points to the contents of the #define command, as a continguous string.
2671 LIMIT points to the first character past the end of the definition.
2672 KEYWORD is the keyword-table entry for #define.  */
2673
2674 static void
2675 do_define (buf, limit, op)
2676      U_CHAR *buf, *limit;
2677      FILE_BUF *op ATTRIBUTE_UNUSED;
2678 {
2679   U_CHAR *bp;                   /* temp ptr into input buffer */
2680   U_CHAR *symname;              /* remember where symbol name starts */
2681   int sym_length;               /* and how long it is */
2682
2683   DEFINITION *defn;
2684   int arglengths = 0;           /* Accumulate lengths of arg names
2685                                    plus number of args.  */
2686   int hashcode;
2687
2688   bp = buf;
2689
2690   while (is_nvspace (*bp))
2691     bp++;
2692
2693   symname = bp;                 /* remember where it starts */
2694   while (is_idchar (*bp) && bp < limit) {
2695     bp++;
2696   }
2697   sym_length = bp - symname;
2698   if (sym_length == 0)
2699     {
2700       error ("invalid macro name");
2701       return;
2702     }
2703   else if (!is_idstart (*symname)) {
2704     U_CHAR *msg;                        /* what pain... */
2705     msg = (U_CHAR *) alloca (sym_length + 1);
2706     memcpy (msg, symname, sym_length);
2707     msg[sym_length] = 0;
2708     error ("invalid macro name `%s'", msg);
2709     return;
2710   } else {
2711     if (! strncmp ((const char *)symname, "defined", 7) && sym_length == 7)
2712       {
2713         error ("\"defined\" cannot be used as a macro name");
2714         return;
2715       }
2716   }
2717
2718   /* lossage will occur if identifiers or control keywords are broken
2719      across lines using backslash.  This is not the right place to take
2720      care of that. */
2721
2722   if (*bp == '(') {
2723     struct arglist *arg_ptrs = NULL;
2724     int argno = 0;
2725
2726     bp++;                       /* skip '(' */
2727     SKIP_WHITE_SPACE (bp);
2728
2729     /* Loop over macro argument names.  */
2730     while (*bp != ')') {
2731       struct arglist *temp;
2732
2733       temp = (struct arglist *) alloca (sizeof (struct arglist));
2734       temp->name = bp;
2735       temp->next = arg_ptrs;
2736       temp->argno = argno++;
2737       arg_ptrs = temp;
2738
2739       if (!is_idstart (*bp))
2740         warning ("parameter name starts with a digit in #define");
2741
2742       /* Find the end of the arg name.  */
2743       while (is_idchar (*bp)) {
2744         bp++;
2745       }
2746       temp->length = bp - temp->name;
2747       arglengths += temp->length + 2;
2748       SKIP_WHITE_SPACE (bp);
2749       if (temp->length == 0 || (*bp != ',' && *bp != ')')) {
2750         error ("badly punctuated parameter list in #define");
2751         return;
2752       }
2753       if (*bp == ',') {
2754         bp++;
2755         SKIP_WHITE_SPACE (bp);
2756       }
2757       if (bp >= limit) {
2758         error ("unterminated parameter list in #define");
2759         return;
2760       }
2761     }
2762
2763     ++bp;                                       /* skip paren */
2764     while (is_nvspace (*bp) && bp < limit)      /* and leading whitespace */
2765       ++bp;
2766     /* now everything from bp before limit is the definition. */
2767     defn = collect_expansion (bp, limit, argno, arg_ptrs);
2768
2769     /* Now set defn->argnames to the result of concatenating
2770        the argument names in reverse order
2771        with comma-space between them.  */
2772     {
2773       struct arglist *temp;
2774       int i = 0;
2775       U_CHAR *tmp = (U_CHAR *) xmalloc (arglengths + 1);
2776
2777       for (temp = arg_ptrs; temp; temp = temp->next) {
2778         memcpy (&tmp[i], temp->name, temp->length);
2779         i += temp->length;
2780         if (temp->next != 0) {
2781           tmp[i++] = ',';
2782           tmp[i++] = ' ';
2783         }
2784       }
2785       tmp[i] = 0;
2786       defn->argnames = tmp;
2787       
2788     }
2789   } else {
2790     /* simple expansion or empty definition; skip leading whitespace */
2791     while (is_nvspace (*bp) && bp < limit)
2792       ++bp;
2793     /* now everything from bp before limit is the definition. */
2794     defn = collect_expansion (bp, limit, -1, 0);
2795     defn->argnames = (const U_CHAR *) "";
2796   }
2797
2798   hashcode = hashf (symname, sym_length, HASHSIZE);
2799
2800   {
2801     HASHNODE *hp;
2802     if ((hp = lookup (symname, sym_length, hashcode)) == NULL)
2803       hp = install (symname, sym_length, T_MACRO, hashcode);
2804     else {
2805       if (hp->type != T_MACRO || compare_defs (defn, hp->value.defn))
2806         warning ("\"%.*s\" redefined", sym_length, symname);
2807
2808       /* Replace the old definition.  */
2809       hp->type = T_MACRO;
2810     }
2811
2812     hp->value.defn = defn;
2813   }
2814 }
2815
2816 /*
2817  * return zero if two DEFINITIONs are isomorphic
2818  */
2819 static int
2820 compare_defs (d1, d2)
2821      DEFINITION *d1, *d2;
2822 {
2823   register struct reflist *a1, *a2;
2824   register U_CHAR *p1 = d1->expansion;
2825   register U_CHAR *p2 = d2->expansion;
2826   int first = 1;
2827
2828   if (d1->nargs != d2->nargs)
2829     return 1;
2830   if (strcmp ((const char *)d1->argnames, (const char *)d2->argnames))
2831     return 1;
2832   for (a1 = d1->pattern, a2 = d2->pattern; a1 && a2;
2833        a1 = a1->next, a2 = a2->next) {
2834     if (!((a1->nchars == a2->nchars
2835            && ! strncmp ((const char *)p1, (const char *)p2, a1->nchars))
2836           || ! comp_def_part (first, p1, a1->nchars, p2, a2->nchars, 0))
2837         || a1->argno != a2->argno
2838         || a1->stringify != a2->stringify
2839         || a1->raw_before != a2->raw_before
2840         || a1->raw_after != a2->raw_after)
2841       return 1;
2842     first = 0;
2843     p1 += a1->nchars;
2844     p2 += a2->nchars;
2845   }
2846   if (a1 != a2)
2847     return 1;
2848   if (comp_def_part (first, p1, d1->length - (p1 - d1->expansion),
2849                      p2, d2->length - (p2 - d2->expansion), 1))
2850     return 1;
2851   return 0;
2852 }
2853
2854 /* Return 1 if two parts of two macro definitions are effectively different.
2855    One of the parts starts at BEG1 and has LEN1 chars;
2856    the other has LEN2 chars at BEG2.
2857    Any sequence of whitespace matches any other sequence of whitespace.
2858    FIRST means these parts are the first of a macro definition;
2859     so ignore leading whitespace entirely.
2860    LAST means these parts are the last of a macro definition;
2861     so ignore trailing whitespace entirely.  */
2862 static int
2863 comp_def_part (first, beg1, len1, beg2, len2, last)
2864      int first;
2865      const U_CHAR *beg1, *beg2;
2866      int len1, len2;
2867      int last;
2868 {
2869   register const U_CHAR *end1 = beg1 + len1;
2870   register const U_CHAR *end2 = beg2 + len2;
2871   if (first) {
2872     while (beg1 != end1 && is_space (*beg1)) beg1++;
2873     while (beg2 != end2 && is_space (*beg2)) beg2++;
2874   }
2875   if (last) {
2876     while (beg1 != end1 && is_space (end1[-1])) end1--;
2877     while (beg2 != end2 && is_space (end2[-1])) end2--;
2878   }
2879   while (beg1 != end1 && beg2 != end2) {
2880     if (is_space (*beg1) && is_space (*beg2)) {
2881       while (beg1 != end1 && is_space (*beg1)) beg1++;
2882       while (beg2 != end2 && is_space (*beg2)) beg2++;
2883     } else if (*beg1 == *beg2) {
2884       beg1++; beg2++;
2885     } else break;
2886   }
2887   return (beg1 != end1) || (beg2 != end2);
2888 }
2889
2890 /* Read a replacement list for a macro with parameters.
2891    Build the DEFINITION structure.
2892    Reads characters of text starting at BUF until LIMIT.
2893    ARGLIST specifies the formal parameters to look for
2894    in the text of the definition; NARGS is the number of args
2895    in that list, or -1 for a macro name that wants no argument list.
2896    MACRONAME is the macro name itself (so we can avoid recursive expansion)
2897    and NAMELEN is its length in characters.
2898    
2899 Note that comments and backslash-newlines have already been deleted
2900 from the argument.  */
2901
2902 /* Leading and trailing Space, Tab, etc. are converted to markers
2903    Newline Space, Newline Tab, etc.
2904    Newline Space makes a space in the final output
2905    but is discarded if stringified.  (Newline Tab is similar but
2906    makes a Tab instead.)
2907
2908    If there is no trailing whitespace, a Newline Space is added at the end
2909    to prevent concatenation that would be contrary to the standard.  */
2910
2911 static DEFINITION *
2912 collect_expansion (buf, end, nargs, arglist)
2913      U_CHAR *buf, *end;
2914      int nargs;
2915      struct arglist *arglist;
2916 {
2917   DEFINITION *defn;
2918   register U_CHAR *p, *limit, *lastp, *exp_p;
2919   struct reflist *endpat = NULL;
2920   /* Pointer to first nonspace after last ## seen.  */
2921   U_CHAR *concat = 0;
2922   /* Pointer to first nonspace after last single-# seen.  */
2923   U_CHAR *stringify = 0;
2924   int maxsize;
2925   int expected_delimiter = '\0';
2926
2927   /* Scan thru the replacement list, ignoring comments and quoted
2928      strings, picking up on the macro calls.  It does a linear search
2929      thru the arg list on every potential symbol.  Profiling might say
2930      that something smarter should happen. */
2931
2932   if (end < buf)
2933     abort ();
2934
2935   /* Find the beginning of the trailing whitespace.  */
2936   /* Find end of leading whitespace.  */
2937   limit = end;
2938   p = buf;
2939   while (p < limit && is_space (limit[-1])) limit--;
2940   while (p < limit && is_space (*p)) p++;
2941
2942   /* Allocate space for the text in the macro definition.
2943      Leading and trailing whitespace chars need 2 bytes each.
2944      Each other input char may or may not need 1 byte,
2945      so this is an upper bound.
2946      The extra 2 are for invented trailing newline-marker and final null.  */
2947   maxsize = (sizeof (DEFINITION)
2948              + 2 * (end - limit) + 2 * (p - buf)
2949              + (limit - p) + 3);
2950   defn = (DEFINITION *) xcalloc (1, maxsize);
2951
2952   defn->nargs = nargs;
2953   exp_p = defn->expansion = (U_CHAR *) defn + sizeof (DEFINITION);
2954   lastp = exp_p;
2955
2956   p = buf;
2957
2958   /* Convert leading whitespace to Newline-markers.  */
2959   while (p < limit && is_space (*p)) {
2960     *exp_p++ = '\n';
2961     *exp_p++ = *p++;
2962   }
2963
2964   /* Process the main body of the definition.  */
2965   while (p < limit) {
2966     int skipped_arg = 0;
2967     register U_CHAR c = *p++;
2968
2969     *exp_p++ = c;
2970
2971     /* In -traditional mode, recognize arguments inside strings and
2972        and character constants, and ignore special properties of #.
2973        Arguments inside strings are considered "stringified", but no
2974        extra quote marks are supplied.  */
2975     switch (c) {
2976     case '\'':
2977     case '\"':
2978       if (expected_delimiter != '\0') {
2979         if (c == expected_delimiter)
2980           expected_delimiter = '\0';
2981       } else
2982         expected_delimiter = c;
2983       break;
2984
2985     case '\\':
2986       /* Backslash quotes delimiters and itself, but not macro args.  */
2987       if (expected_delimiter != 0 && p < limit
2988           && (*p == expected_delimiter || *p == '\\')) {
2989         *exp_p++ = *p++;
2990         continue;
2991       }
2992       break;
2993
2994     case '/':
2995       if (expected_delimiter != '\0') /* No comments inside strings.  */
2996         break;
2997       if (*p == '*') {
2998         /* If we find a comment that wasn't removed by handle_directive,
2999            this must be -traditional.  So replace the comment with
3000            nothing at all.  */
3001         exp_p--;
3002         p += 1;
3003         while (p < limit && !(p[-2] == '*' && p[-1] == '/'))
3004           p++;
3005       }
3006       break;
3007     }
3008
3009     if (is_idchar (c) && nargs > 0) {
3010       U_CHAR *id_beg = p - 1;
3011       int id_len;
3012
3013       --exp_p;
3014       while (p != limit && is_idchar (*p)) p++;
3015       id_len = p - id_beg;
3016
3017       if (is_idstart (c)) {
3018         register struct arglist *arg;
3019
3020         for (arg = arglist; arg != NULL; arg = arg->next) {
3021           struct reflist *tpat;
3022
3023           if (arg->name[0] == c
3024               && arg->length == id_len
3025               && strncmp ((const char *)arg->name,
3026                           (const char *)id_beg, id_len) == 0) {
3027             /* make a pat node for this arg and append it to the end of
3028                the pat list */
3029             tpat = (struct reflist *) xmalloc (sizeof (struct reflist));
3030             tpat->next = NULL;
3031             tpat->raw_before = concat == id_beg;
3032             tpat->raw_after = 0;
3033             tpat->stringify = expected_delimiter != '\0';
3034
3035             if (endpat == NULL)
3036               defn->pattern = tpat;
3037             else
3038               endpat->next = tpat;
3039             endpat = tpat;
3040
3041             tpat->argno = arg->argno;
3042             tpat->nchars = exp_p - lastp;
3043             {
3044               register U_CHAR *p1 = p;
3045               SKIP_WHITE_SPACE (p1);
3046               if (p1 + 2 <= limit && p1[0] == '#' && p1[1] == '#')
3047                 tpat->raw_after = 1;
3048             }
3049             lastp = exp_p;      /* place to start copying from next time */
3050             skipped_arg = 1;
3051             break;
3052           }
3053         }
3054       }
3055
3056       /* If this was not a macro arg, copy it into the expansion.  */
3057       if (! skipped_arg) {
3058         register U_CHAR *lim1 = p;
3059         p = id_beg;
3060         while (p != lim1)
3061           *exp_p++ = *p++;
3062         if (stringify == id_beg)
3063           error ("# operator should be followed by a macro argument name");
3064       }
3065     }
3066   }
3067
3068   if (limit < end) {
3069     /* Convert trailing whitespace to Newline-markers.  */
3070     while (limit < end && is_space (*limit)) {
3071       *exp_p++ = '\n';
3072       *exp_p++ = *limit++;
3073     }
3074   }
3075   *exp_p = '\0';
3076
3077   defn->length = exp_p - defn->expansion;
3078
3079   /* Crash now if we overrun the allocated size.  */
3080   if (defn->length + 1 > maxsize)
3081     abort ();
3082
3083   return defn;
3084 }
3085 \f
3086 /*
3087  * interpret #line command.  Remembers previously seen fnames
3088  * in its very own hash table.
3089  */
3090 #define FNAME_HASHSIZE 37
3091 static void
3092 do_line (buf, limit, op)
3093      U_CHAR *buf, *limit;
3094      FILE_BUF *op;
3095 {
3096   register U_CHAR *bp;
3097   FILE_BUF *ip = &instack[indepth];
3098   FILE_BUF tem;
3099   int new_lineno;
3100   enum file_change_code file_change = same_file;
3101
3102   /* Expand any macros.  */
3103   tem = expand_to_temp_buffer (buf, limit, 0);
3104
3105   /* Point to macroexpanded line, which is null-terminated now.  */
3106   bp = tem.buf;
3107   SKIP_WHITE_SPACE (bp);
3108
3109   if (!ISDIGIT (*bp)) {
3110     error ("invalid format #line command");
3111     return;
3112   }
3113
3114   /* The Newline at the end of this line remains to be processed.
3115      To put the next line at the specified line number,
3116      we must store a line number now that is one less.  */
3117   new_lineno = atoi ((const char *)bp);
3118
3119   /* skip over the line number.  */
3120   while (ISDIGIT (*bp))
3121     bp++;
3122
3123   SKIP_WHITE_SPACE (bp);
3124
3125   if (*bp == '\"') {
3126     static HASHNODE *fname_table[FNAME_HASHSIZE];
3127     HASHNODE *hp, **hash_bucket;
3128     U_CHAR *fname;
3129     int fname_length;
3130
3131     fname = ++bp;
3132
3133     while (*bp && *bp != '\"')
3134       bp++;
3135     if (*bp != '\"') {
3136       error ("invalid format #line command");
3137       return;
3138     }
3139
3140     fname_length = bp - fname;
3141
3142     bp++;
3143     SKIP_WHITE_SPACE (bp);
3144     if (*bp) {
3145       if (*bp == '1')
3146         file_change = enter_file;
3147       else if (*bp == '2')
3148         file_change = leave_file;
3149       else {
3150         error ("invalid format #line command");
3151         return;
3152       }
3153
3154       bp++;
3155       SKIP_WHITE_SPACE (bp);
3156       if (*bp) {
3157         error ("invalid format #line command");
3158         return;
3159       }
3160     }
3161
3162     hash_bucket =
3163       &fname_table[hashf (fname, fname_length, FNAME_HASHSIZE)];
3164     for (hp = *hash_bucket; hp != NULL; hp = hp->next)
3165       if (hp->length == fname_length &&
3166           strncmp (hp->value.cpval, (const char *)fname, fname_length) == 0) {
3167         ip->fname = hp->value.cpval;
3168         break;
3169       }
3170     if (hp == 0) {
3171       char *q;
3172       /* Didn't find it; cons up a new one.  */
3173       hp = (HASHNODE *) xcalloc (1, sizeof (HASHNODE) + fname_length + 1);
3174       hp->next = *hash_bucket;
3175       *hash_bucket = hp;
3176
3177       hp->length = fname_length;
3178       ip->fname = hp->value.cpval = q = ((char *) hp) + sizeof (HASHNODE);
3179       memcpy (q, fname, fname_length);
3180     }
3181   } else if (*bp) {
3182     error ("invalid format #line command");
3183     return;
3184   }
3185
3186   ip->lineno = new_lineno;
3187   output_line_command (ip, op, 0, file_change);
3188   ip->bufp++;                   /* Skip the new line.  */
3189   check_expand (op, ip->length - (ip->bufp - ip->buf));
3190 }
3191
3192 /*
3193  * remove all definitions of symbol from symbol table.
3194  * according to un*x /lib/cpp, it is not an error to undef
3195  * something that has no definitions, so it isn't one here either.
3196  */
3197 static void
3198 do_undef (buf, limit, op)
3199      U_CHAR *buf;
3200      U_CHAR *limit ATTRIBUTE_UNUSED;
3201      FILE_BUF *op ATTRIBUTE_UNUSED;
3202 {
3203   HASHNODE *hp;
3204
3205   SKIP_WHITE_SPACE (buf);
3206
3207   if (! strncmp ((const char *)buf, "defined", 7) && ! is_idchar (buf[7]))
3208     warning ("undefining `defined'");
3209
3210   while ((hp = lookup (buf, -1, -1)) != NULL) {
3211     if (hp->type != T_MACRO)
3212       warning ("undefining `%s'", hp->name);
3213     delete_macro (hp);
3214   }
3215 }
3216
3217 /* Read the tokens of the answer into the macro pool.  Only commit the
3218    memory if we intend it as permanent storage, i.e. the #assert case.
3219    Returns 0 on success.  */
3220
3221 static int
3222 parse_answer (buf, limit, answerp, type)
3223      const unsigned char *buf, *limit;
3224      struct answer **answerp;
3225      int type;
3226 {
3227   const unsigned char *start;
3228
3229   /* Skip leading whitespace.  */
3230   if (buf < limit && *buf == ' ')
3231     buf++;
3232
3233   /* Parentheses are optional here.  */
3234   if (buf == limit && type == T_UNASSERT)
3235     return 0;
3236
3237   if (buf == limit || *buf++ != '(')
3238     {
3239       if (type == T_IF)
3240         return 0;
3241
3242       error ("missing '(' after predicate");
3243       return 1;
3244     }
3245
3246   /* Drop whitespace at start.  */
3247   while (buf < limit && *buf == ' ')
3248     buf++;
3249
3250   start = buf;
3251   while (buf < limit && *buf != ')')
3252     buf++;
3253
3254   if (buf == limit)
3255     {
3256       error ("missing ')' to complete answer");
3257       return 1;
3258     }
3259
3260   if (buf == start)
3261     {
3262       error ("predicate's answer is empty");
3263       return 1;
3264     }
3265
3266   if ((type == T_ASSERT || type == T_UNASSERT) && buf + 1 != limit)
3267     {
3268       error ("extra text at end of directive");
3269       return 1;
3270     }
3271
3272   /* Lose trailing whitespace.  */
3273   if (buf[-1] == ' ')
3274     buf--;
3275
3276   *answerp = (struct answer *) xmalloc (sizeof (struct answer));
3277   (*answerp)->answer = start;
3278   (*answerp)->len = buf - start;
3279
3280   return 0;
3281 }
3282
3283 /* Parses an assertion, returning a pointer to the hash node of the
3284    predicate, or 0 on error.  If an answer was supplied, it is placed
3285    in ANSWERP, otherwise it is set to 0.  */
3286 static HASHNODE *
3287 parse_assertion (buf, limit, answerp, type)
3288      const unsigned char *buf, *limit;
3289      struct answer **answerp;
3290      int type;
3291 {
3292   HASHNODE *result = 0;
3293   const unsigned char *climit;
3294   unsigned char *bp, *symname = canonicalize_text (buf, limit, &climit);
3295   unsigned int len;
3296
3297   bp = symname;
3298   if (bp < climit && is_idstart (*bp))
3299     {
3300       do
3301         bp++;
3302       while (bp < climit && is_idchar (*bp));
3303     }
3304   len = bp - symname;
3305
3306   *answerp = 0;
3307   if (len == 0)
3308     {
3309       if (symname == climit)
3310         error ("assertion without predicate");
3311       else
3312         error ("predicate must be an identifier");
3313     }
3314   /* Unfortunately, because of the way we handle #if, we don't avoid
3315      macro expansion in answers.  This is not easy to fix.  */
3316   else if (parse_answer (bp, climit, answerp, type) == 0)
3317     {
3318       unsigned char *sym = alloca (len + 1);
3319       int hashcode;
3320       
3321       /* Prefix '#' to get it out of macro namespace.  */
3322       sym[0] = '#';
3323       memcpy (sym + 1, symname, len);
3324
3325       hashcode = hashf (sym, len + 1, HASHSIZE);
3326       result = lookup (sym, len + 1, hashcode);
3327       if (result == 0)
3328         result = install (sym, len + 1, T_UNUSED, hashcode);
3329     }
3330
3331   return result;
3332 }
3333
3334 /* Test an assertion within a preprocessor conditional.  Returns zero
3335    on error or failure, one on success.  */
3336 int
3337 test_assertion (pbuf)
3338      unsigned char **pbuf;      /* NUL-terminated.  */
3339 {
3340   unsigned char *buf = *pbuf;
3341   unsigned char *limit = buf + strlen ((char *) buf);
3342   struct answer *answer;
3343   HASHNODE *node;
3344   int result = 0;
3345
3346   node = parse_assertion (buf, limit, &answer, T_IF);
3347   if (node)
3348     {
3349       result = (node->type == T_ASSERT &&
3350                 (answer == 0 || *find_answer (node, answer) != 0));
3351
3352       /* Yuk.  We update pbuf to point after the assertion test.
3353          First, move past the identifier.  */
3354       if (is_space (*buf))
3355         buf++;
3356       while (is_idchar (*buf))
3357         buf++;
3358       /* If we have an answer, we need to move past the parentheses.  */
3359       if (answer)
3360         while (*buf++ != ')')
3361           ;
3362       *pbuf = buf;
3363     }
3364
3365   return result;
3366 }
3367
3368 /* Handle a #error directive.  */
3369 static void
3370 do_error (buf, limit, op)
3371      U_CHAR *buf;
3372      U_CHAR *limit;
3373      FILE_BUF *op ATTRIBUTE_UNUSED;
3374 {
3375   error ("#error%.*s", (int) (limit - buf), buf);
3376 }
3377
3378 /* Handle a #warning directive.  */
3379 static void
3380 do_warning (buf, limit, op)
3381      U_CHAR *buf;
3382      U_CHAR *limit;
3383      FILE_BUF *op ATTRIBUTE_UNUSED;
3384 {
3385   warning ("#warning%.*s", (int) (limit - buf), buf);
3386 }
3387
3388 /* Handle a #assert directive.  */
3389 static void
3390 do_assert (buf, limit, op)
3391      U_CHAR *buf;
3392      U_CHAR *limit;
3393      FILE_BUF *op ATTRIBUTE_UNUSED;
3394 {
3395   struct answer *new_answer;
3396   HASHNODE *node;
3397   
3398   node = parse_assertion (buf, limit, &new_answer, T_ASSERT);
3399   if (node)
3400     {
3401       /* Place the new answer in the answer list.  First check there
3402          is not a duplicate.  */
3403       new_answer->next = 0;
3404       if (node->type == T_ASSERT)
3405         {
3406           if (*find_answer (node, new_answer))
3407             {
3408               free (new_answer);
3409               warning ("\"%s\" re-asserted", node->name + 1);
3410               return;
3411             }
3412           new_answer->next = node->value.answers;
3413         }
3414       node->type = T_ASSERT;
3415       node->value.answers = new_answer;
3416     }
3417 }
3418
3419 /* Function body to be provided later.  */
3420 static void
3421 do_unassert (buf, limit, op)
3422      U_CHAR *buf;
3423      U_CHAR *limit;
3424      FILE_BUF *op ATTRIBUTE_UNUSED;
3425 {
3426   HASHNODE *node;
3427   struct answer *answer;
3428   
3429   node = parse_assertion (buf, limit, &answer, T_UNASSERT);
3430   /* It isn't an error to #unassert something that isn't asserted.  */
3431   if (node)
3432     {
3433       if (node->type == T_ASSERT)
3434         {
3435           if (answer)
3436             {
3437               struct answer **p = find_answer (node, answer), *temp;
3438
3439               /* Remove the answer from the list.  */
3440               temp = *p;
3441               if (temp)
3442                 *p = temp->next;
3443
3444               /* Did we free the last answer?  */
3445               if (node->value.answers == 0)
3446                 delete_macro (node);
3447             }
3448           else
3449             delete_macro (node);
3450         }
3451
3452       free (answer);
3453     }
3454 }
3455
3456 /* Returns a pointer to the pointer to the answer in the answer chain,
3457    or a pointer to NULL if the answer is not in the chain.  */
3458 static struct answer **
3459 find_answer (node, candidate)
3460      HASHNODE *node;
3461      const struct answer *candidate;
3462 {
3463   struct answer **result;
3464
3465   for (result = &node->value.answers; *result; result = &(*result)->next)
3466     {
3467       struct answer *answer = *result;
3468
3469       if (answer->len == candidate->len
3470           && !memcmp (answer->answer, candidate->answer, answer->len))
3471         break;
3472     }
3473
3474   return result;
3475 }
3476
3477 /* Return a malloced buffer with leading and trailing whitespace
3478    removed, and all instances of internal whitespace reduced to a
3479    single space.  */
3480 static unsigned char *
3481 canonicalize_text (buf, limit, climit)
3482      const unsigned char *buf, *limit, **climit;
3483 {
3484   unsigned int len = limit - buf;
3485   unsigned char *result = (unsigned char *) xmalloc (len), *dest;
3486
3487   for (dest = result; buf < limit;)
3488     {
3489       if (! is_space (*buf))
3490         *dest++ = *buf++;
3491       else
3492         {
3493           while (++buf < limit && is_space (*buf))
3494             ;
3495           if (dest != result && buf != limit)
3496             *dest++ = ' ';
3497         }
3498     }
3499
3500   *climit = dest;
3501   return result;
3502 }
3503
3504 /*
3505  * handle #if command by
3506  *   1) inserting special `defined' keyword into the hash table
3507  *      that gets turned into 0 or 1 by special_symbol (thus,
3508  *      if the luser has a symbol called `defined' already, it won't
3509  *      work inside the #if command)
3510  *   2) rescan the input into a temporary output buffer
3511  *   3) pass the output buffer to the yacc parser and collect a value
3512  *   4) clean up the mess left from steps 1 and 2.
3513  *   5) call conditional_skip to skip til the next #endif (etc.),
3514  *      or not, depending on the value from step 3.
3515  */
3516 static void
3517 do_if (buf, limit, op)
3518      U_CHAR *buf, *limit;
3519      FILE_BUF *op ATTRIBUTE_UNUSED;
3520 {
3521   int value;
3522   FILE_BUF *ip = &instack[indepth];
3523
3524   value = eval_if_expression (buf, limit - buf);
3525   conditional_skip (ip, value == 0, T_IF);
3526 }
3527
3528 /*
3529  * handle a #elif directive by not changing  if_stack  either.
3530  * see the comment above do_else.
3531  */
3532 static void
3533 do_elif (buf, limit, op)
3534      U_CHAR *buf, *limit;
3535      FILE_BUF *op;
3536 {
3537   int value;
3538   FILE_BUF *ip = &instack[indepth];
3539
3540   if (if_stack == instack[indepth].if_stack) {
3541     error ("#elif not within a conditional");
3542     return;
3543   } else {
3544     if (if_stack->type != T_IF && if_stack->type != T_ELIF) {
3545       error ("#elif after #else");
3546       fprintf (stderr, " (matches line %d", if_stack->lineno);
3547       if (if_stack->fname != NULL && ip->fname != NULL &&
3548           strcmp (if_stack->fname, ip->fname) != 0)
3549         fprintf (stderr, ", file %s", if_stack->fname);
3550       fprintf (stderr, ")\n");
3551     }
3552     if_stack->type = T_ELIF;
3553   }
3554
3555   if (if_stack->if_succeeded)
3556     skip_if_group (ip, 0);
3557   else {
3558     value = eval_if_expression (buf, limit - buf);
3559     if (value == 0)
3560       skip_if_group (ip, 0);
3561     else {
3562       ++if_stack->if_succeeded; /* continue processing input */
3563       output_line_command (ip, op, 1, same_file);
3564     }
3565   }
3566 }
3567
3568 /*
3569  * evaluate a #if expression in BUF, of length LENGTH,
3570  * then parse the result as a C expression and return the value as an int.
3571  */
3572 static int
3573 eval_if_expression (buf, length)
3574      const U_CHAR *buf;
3575      int length;
3576 {
3577   FILE_BUF temp_obuf;
3578   HASHNODE *save_defined;
3579   int value;
3580
3581   save_defined = install (U"defined", -1, T_SPEC_DEFINED, -1);
3582   temp_obuf = expand_to_temp_buffer (buf, buf + length, 0);
3583   delete_macro (save_defined);  /* clean up special symbol */
3584
3585   value = parse_c_expression ((const char *)temp_obuf.buf);
3586
3587   free (temp_obuf.buf);
3588
3589   return value;
3590 }
3591
3592 /*
3593  * routine to handle ifdef/ifndef.  Try to look up the symbol,
3594  * then do or don't skip to the #endif/#else/#elif depending
3595  * on what directive is actually being processed.
3596  */
3597 static void
3598 do_xifdef (buf, limit, type)
3599      U_CHAR *buf, *limit;
3600      enum node_type type;     
3601 {
3602   int skip;
3603   FILE_BUF *ip = &instack[indepth];
3604   U_CHAR *end; 
3605
3606   /* Discard leading and trailing whitespace.  */
3607   SKIP_WHITE_SPACE (buf);
3608   while (limit != buf && is_nvspace (limit[-1])) limit--;
3609
3610   /* Find the end of the identifier at the beginning.  */
3611   for (end = buf; is_idchar (*end); end++);
3612
3613   if (end == buf)
3614     skip = (type == T_IFDEF);
3615   else
3616     skip = (lookup (buf, end-buf, -1) == NULL) ^ (type == T_IFNDEF);
3617
3618   conditional_skip (ip, skip, T_IF);
3619 }
3620
3621 static void
3622 do_ifdef (buf, limit, op)
3623      U_CHAR *buf, *limit;
3624      FILE_BUF *op ATTRIBUTE_UNUSED;
3625 {
3626   do_xifdef (buf, limit, T_IFDEF);
3627 }
3628
3629 static void
3630 do_ifndef (buf, limit, op)
3631      U_CHAR *buf, *limit;
3632      FILE_BUF *op ATTRIBUTE_UNUSED;
3633 {
3634   do_xifdef (buf, limit, T_IFNDEF);
3635 }
3636
3637 /*
3638  * push TYPE on stack; then, if SKIP is nonzero, skip ahead.
3639  */
3640 static void
3641 conditional_skip (ip, skip, type)
3642      FILE_BUF *ip;
3643      int skip;
3644      enum node_type type;
3645 {
3646   IF_STACK_FRAME *temp;
3647
3648   temp = (IF_STACK_FRAME *) xcalloc (1, sizeof (IF_STACK_FRAME));
3649   temp->fname = ip->fname;
3650   temp->lineno = ip->lineno;
3651   temp->next = if_stack;
3652   if_stack = temp;
3653
3654   if_stack->type = type;
3655
3656   if (skip != 0) {
3657     skip_if_group (ip, 0);
3658     return;
3659   } else {
3660     ++if_stack->if_succeeded;
3661     output_line_command (ip, &outbuf, 1, same_file);
3662   }
3663 }
3664
3665 /*
3666  * skip to #endif, #else, or #elif.  adjust line numbers, etc.
3667  * leaves input ptr at the sharp sign found.
3668  * If ANY is nonzero, return at next directive of any sort.
3669  */
3670 static void
3671 skip_if_group (ip, any)
3672      FILE_BUF *ip;
3673      int any;
3674 {
3675   register U_CHAR *bp = ip->bufp, *cp;
3676   register U_CHAR *endb = ip->buf + ip->length;
3677   struct directive *kt;
3678   IF_STACK_FRAME *save_if_stack = if_stack; /* don't pop past here */
3679   U_CHAR *beg_of_line = bp;
3680
3681   while (bp < endb) {
3682     switch (*bp++) {
3683     case '/':                   /* possible comment */
3684       if (*bp == '\\' && bp[1] == '\n')
3685         newline_fix (bp);
3686       if (*bp == '*') {
3687         ip->bufp = ++bp;
3688         bp = skip_to_end_of_comment (ip, &ip->lineno);
3689       }
3690       break;
3691     case '\"':
3692     case '\'':
3693       bp = skip_quoted_string (bp - 1, endb, ip->lineno, &ip->lineno, 0, 0);
3694       break;
3695     case '\\':
3696       /* Char after backslash loses its special meaning.  */
3697       if (bp < endb) {
3698         if (*bp == '\n')
3699           ++ip->lineno;         /* But do update the line-count.  */
3700         bp++;
3701       }
3702       break;
3703     case '\n':
3704       ++ip->lineno;
3705       beg_of_line = bp;
3706       break;
3707     case '#':
3708       ip->bufp = bp - 1;
3709
3710       /* # keyword: a # must be first nonblank char on the line */
3711       if (beg_of_line == 0)
3712         break;
3713       /* Scan from start of line, skipping whitespace, comments
3714          and backslash-newlines, and see if we reach this #.
3715          If not, this # is not special.  */
3716       bp = beg_of_line;
3717       while (1) {
3718         if (is_nvspace (*bp))
3719           bp++;
3720         else if (*bp == '\\' && bp[1] == '\n')
3721           bp += 2;
3722         else if (*bp == '/' && bp[1] == '*') {
3723           bp += 2;
3724           while (!(*bp == '*' && bp[1] == '/')) {
3725             if (*bp == '\n')
3726               ip->lineno++;
3727             bp++;
3728           }
3729           bp += 2;
3730         }
3731         else break;
3732       }
3733       if (bp != ip->bufp) {
3734         bp = ip->bufp + 1;      /* Reset bp to after the #.  */
3735         break;
3736       }
3737
3738       bp = ip->bufp + 1;        /* Point after '#'.  */
3739
3740       /* Skip whitespace and \-newline.  */
3741       while (1) {
3742         if (is_nvspace (*bp))
3743           bp++;
3744         else if (*bp == '\\' && bp[1] == '\n')
3745           bp += 2;
3746         else if (*bp == '/' && bp[1] == '*') {
3747           bp += 2;
3748           while (!(*bp == '*' && bp[1] == '/'))
3749             bp++;
3750           bp += 2;
3751         }
3752         else break;
3753       }
3754
3755       cp = bp;
3756
3757       /* Now find end of directive name.
3758          If we encounter a backslash-newline, exchange it with any following
3759          symbol-constituents so that we end up with a contiguous name.  */
3760
3761       while (1) {
3762         if (is_idchar (*bp))
3763           bp++;
3764         else {
3765           if (*bp == '\\' && bp[1] == '\n')
3766             name_newline_fix (bp);
3767           if (is_idchar (*bp))
3768             bp++;
3769           else break;
3770         }
3771       }
3772
3773       for (kt = directive_table; kt->length >= 0; kt++) {
3774         IF_STACK_FRAME *temp;
3775         if (strncmp ((const char *)cp, kt->name, kt->length) == 0
3776             && !is_idchar (cp[kt->length])) {
3777
3778           /* If we are asked to return on next directive,
3779              do so now.  */
3780           if (any)
3781             return;
3782
3783           switch (kt->type) {
3784           case T_IF:
3785           case T_IFDEF:
3786           case T_IFNDEF:
3787             temp = (IF_STACK_FRAME *) xcalloc (1, sizeof (IF_STACK_FRAME));
3788             temp->next = if_stack;
3789             if_stack = temp;
3790             temp->lineno = ip->lineno;
3791             temp->fname = ip->fname;
3792             temp->type = kt->type;
3793             break;
3794           case T_ELSE:
3795           case T_ENDIF:
3796           case T_ELIF:
3797             if (if_stack == instack[indepth].if_stack) {
3798               error ("#%s not within a conditional", kt->name);
3799               break;
3800             }
3801             else if (if_stack == save_if_stack)
3802               return;           /* found what we came for */
3803
3804             if (kt->type != T_ENDIF) {
3805               if (if_stack->type == T_ELSE)
3806                 error ("#else or #elif after #else");
3807               if_stack->type = kt->type;
3808               break;
3809             }
3810
3811             temp = if_stack;
3812             if_stack = if_stack->next;
3813             free (temp);
3814             break;
3815
3816           default:
3817             /* Anything else is ignored.  */
3818             break;
3819           }
3820           break;
3821         }
3822       }
3823     }
3824   }
3825   ip->bufp = bp;
3826   /* after this returns, rescan will exit because ip->bufp
3827      now points to the end of the buffer.
3828      rescan is responsible for the error message also.  */
3829 }
3830
3831 /*
3832  * handle a #else directive.  Do this by just continuing processing
3833  * without changing  if_stack ;  this is so that the error message
3834  * for missing #endif's etc. will point to the original #if.  It
3835  * is possible that something different would be better.
3836  */
3837 static void
3838 do_else (buf, limit, op)
3839      U_CHAR *buf ATTRIBUTE_UNUSED;
3840      U_CHAR *limit ATTRIBUTE_UNUSED;
3841      FILE_BUF *op;
3842 {
3843   FILE_BUF *ip = &instack[indepth];
3844
3845   if (if_stack == instack[indepth].if_stack) {
3846     error ("#else not within a conditional");
3847     return;
3848   } else {
3849     if (if_stack->type != T_IF && if_stack->type != T_ELIF) {
3850       error ("#else after #else");
3851       fprintf (stderr, " (matches line %d", if_stack->lineno);
3852       if (strcmp (if_stack->fname, ip->fname) != 0)
3853         fprintf (stderr, ", file %s", if_stack->fname);
3854       fprintf (stderr, ")\n");
3855     }
3856     if_stack->type = T_ELSE;
3857   }
3858
3859   if (if_stack->if_succeeded)
3860     skip_if_group (ip, 0);
3861   else {
3862     ++if_stack->if_succeeded;   /* continue processing input */
3863     output_line_command (ip, op, 1, same_file);
3864   }
3865 }
3866
3867 /*
3868  * unstack after #endif command
3869  */
3870 static void
3871 do_endif (buf, limit, op)
3872      U_CHAR *buf ATTRIBUTE_UNUSED;
3873      U_CHAR *limit ATTRIBUTE_UNUSED;
3874      FILE_BUF *op;
3875 {
3876   if (if_stack == instack[indepth].if_stack)
3877     error ("unbalanced #endif");
3878   else {
3879     IF_STACK_FRAME *temp = if_stack;
3880     if_stack = if_stack->next;
3881     free (temp);
3882     output_line_command (&instack[indepth], op, 1, same_file);
3883   }
3884 }
3885
3886 /*
3887  * Skip a comment, assuming the input ptr immediately follows the
3888  * initial slash-star.  Bump line counter as necessary.
3889  * (The canonical line counter is &ip->lineno).
3890  * Don't use this routine (or the next one) if bumping the line
3891  * counter is not sufficient to deal with newlines in the string.
3892  */
3893 static U_CHAR *
3894 skip_to_end_of_comment (ip, line_counter)
3895      register FILE_BUF *ip;
3896      int *line_counter;         /* place to remember newlines, or NULL */
3897 {
3898   register U_CHAR *limit = ip->buf + ip->length;
3899   register U_CHAR *bp = ip->bufp;
3900   FILE_BUF *op = &outbuf;       /* JF */
3901   int output = put_out_comments && !line_counter;
3902
3903         /* JF this line_counter stuff is a crock to make sure the
3904            comment is only put out once, no matter how many times
3905            the comment is skipped.  It almost works */
3906   if (output) {
3907     *op->bufp++ = '/';
3908     *op->bufp++ = '*';
3909   }
3910   while (bp < limit) {
3911     if (output)
3912       *op->bufp++ = *bp;
3913     switch (*bp++) {
3914     case '/':
3915       if (warn_comments && bp < limit && *bp == '*')
3916         warning("`/*' within comment");
3917       break;
3918     case '\n':
3919       if (line_counter != NULL)
3920         ++*line_counter;
3921       if (output)
3922         ++op->lineno;
3923       break;
3924     case '*':
3925       if (*bp == '\\' && bp[1] == '\n')
3926         newline_fix (bp);
3927       if (*bp == '/') {
3928         if (output)
3929           *op->bufp++ = '/';
3930         ip->bufp = ++bp;
3931         return bp;
3932       }
3933       break;
3934     }
3935   }
3936   ip->bufp = bp;
3937   return bp;
3938 }
3939
3940 /*
3941  * Skip over a quoted string.  BP points to the opening quote.
3942  * Returns a pointer after the closing quote.  Don't go past LIMIT.
3943  * START_LINE is the line number of the starting point (but it need
3944  * not be valid if the starting point is inside a macro expansion).
3945  *
3946  * The input stack state is not changed.
3947  *
3948  * If COUNT_NEWLINES is nonzero, it points to an int to increment
3949  * for each newline passed.
3950  *
3951  * If BACKSLASH_NEWLINES_P is nonzero, store 1 thru it
3952  * if we pass a backslash-newline.
3953  *
3954  * If EOFP is nonzero, set *EOFP to 1 if the string is unterminated.
3955  */
3956 static U_CHAR *
3957 skip_quoted_string (bp, limit, start_line, count_newlines, backslash_newlines_p, eofp)
3958      register const U_CHAR *bp;
3959      register const U_CHAR *limit;
3960      int start_line;
3961      int *count_newlines;
3962      int *backslash_newlines_p;
3963      int *eofp;
3964 {
3965   register U_CHAR c, match;
3966
3967   match = *bp++;
3968   while (1) {
3969     if (bp >= limit) {
3970       error_with_line (line_for_error (start_line),
3971                        "unterminated string or character constant");
3972       if (eofp)
3973         *eofp = 1;
3974       break;
3975     }
3976     c = *bp++;
3977     if (c == '\\') {
3978       while (*bp == '\\' && bp[1] == '\n') {
3979         if (backslash_newlines_p)
3980           *backslash_newlines_p = 1;
3981         if (count_newlines)
3982           ++*count_newlines;
3983         bp += 2;
3984       }
3985       if (*bp == '\n' && count_newlines) {
3986         if (backslash_newlines_p)
3987           *backslash_newlines_p = 1;
3988         ++*count_newlines;
3989       }
3990       bp++;
3991     } else if (c == '\n') {
3992       /* Unterminated strings and character constants are 'legal'.  */
3993       bp--;     /* Don't consume the newline. */
3994       if (eofp)
3995         *eofp = 1;
3996       break;
3997     } else if (c == match)
3998       break;
3999   }
4000   return (U_CHAR *) bp;
4001 }
4002 \f
4003 /*
4004  * write out a #line command, for instance, after an #include file.
4005  * If CONDITIONAL is nonzero, we can omit the #line if it would
4006  * appear to be a no-op, and we can output a few newlines instead
4007  * if we want to increase the line number by a small amount.
4008  * FILE_CHANGE says whether we are entering a file, leaving, or neither.
4009  */
4010
4011 static void
4012 output_line_command (ip, op, conditional, file_change)
4013      FILE_BUF *ip, *op;
4014      int conditional;
4015      enum file_change_code file_change;
4016 {
4017   int len;
4018   char line_cmd_buf[500];
4019
4020   if (no_line_commands
4021       || ip->fname == NULL
4022       || no_output) {
4023     op->lineno = ip->lineno;
4024     return;
4025   }
4026
4027   if (conditional) {
4028     if (ip->lineno == op->lineno)
4029       return;
4030
4031     /* If the inherited line number is a little too small,
4032        output some newlines instead of a #line command.  */
4033     if (ip->lineno > op->lineno && ip->lineno < op->lineno + 8) {
4034       check_expand (op, 10);
4035       while (ip->lineno > op->lineno) {
4036         *op->bufp++ = '\n';
4037         op->lineno++;
4038       }
4039       return;
4040     }
4041   }
4042
4043   sprintf (line_cmd_buf, "# %d \"%s\"", ip->lineno, ip->fname);
4044   if (file_change != same_file)
4045     strcat (line_cmd_buf, file_change == enter_file ? " 1" : " 2");
4046   if (system_include_depth > 0)
4047     strcat (line_cmd_buf, " 3");
4048   len = strlen (line_cmd_buf);
4049   line_cmd_buf[len++] = '\n';
4050   check_expand (op, len + 1);
4051   if (op->bufp > op->buf && op->bufp[-1] != '\n')
4052     *op->bufp++ = '\n';
4053   memcpy (op->bufp, line_cmd_buf, len);
4054   op->bufp += len;
4055   op->lineno = ip->lineno;
4056 }
4057 \f
4058
4059 /* Expand a macro call.
4060    HP points to the symbol that is the macro being called.
4061    Put the result of expansion onto the input stack
4062    so that subsequent input by our caller will use it.
4063
4064    If macro wants arguments, caller has already verified that
4065    an argument list follows; arguments come from the input stack.  */
4066
4067 static void
4068 macroexpand (hp, op)
4069      HASHNODE *hp;
4070      FILE_BUF *op;
4071 {
4072   int nargs;
4073   DEFINITION *defn = hp->value.defn;
4074   register U_CHAR *xbuf;
4075   int xbuf_len;
4076   int start_line = instack[indepth].lineno;
4077
4078   CHECK_DEPTH (return;);
4079
4080   /* it might not actually be a macro.  */
4081   if (hp->type != T_MACRO) {
4082     special_symbol (hp, op);
4083     return;
4084   }
4085
4086   nargs = defn->nargs;
4087
4088   if (nargs >= 0) {
4089     register int i;
4090     struct argdata *args;
4091     const char *parse_error = 0;
4092
4093     args = (struct argdata *) alloca ((nargs + 1) * sizeof (struct argdata));
4094
4095     for (i = 0; i < nargs; i++) {
4096       args[i].raw = args[i].expanded = (U_CHAR *) "";
4097       args[i].raw_length = args[i].expand_length
4098         = args[i].stringified_length = 0;
4099       args[i].free1 = args[i].free2 = 0;
4100     }
4101
4102     /* Parse all the macro args that are supplied.  I counts them.
4103        The first NARGS args are stored in ARGS.
4104        The rest are discarded.  */
4105     i = 0;
4106     do {
4107       /* Discard the open-parenthesis or comma before the next arg.  */
4108       ++instack[indepth].bufp;
4109       parse_error
4110         = macarg ((i < nargs || (nargs == 0 && i == 0)) ? &args[i] : 0);
4111       if (parse_error)
4112         {
4113           error_with_line (line_for_error (start_line), "%s", parse_error);
4114           break;
4115         }
4116       i++;
4117     } while (*instack[indepth].bufp != ')');
4118
4119     /* If we got one arg but it was just whitespace, call that 0 args.  */
4120     if (i == 1) {
4121       register const U_CHAR *bp = args[0].raw;
4122       register const U_CHAR *lim = bp + args[0].raw_length;
4123       while (bp != lim && is_space (*bp)) bp++;
4124       if (bp == lim)
4125         i = 0;
4126     }
4127
4128     if (nargs == 0 && i > 0)
4129       error ("arguments given to macro `%s'", hp->name);
4130     else if (i < nargs) {
4131       /* traditional C allows foo() if foo wants one argument.  */
4132       if (nargs == 1 && i == 0)
4133         ;
4134       else if (i == 0)
4135         error ("no args to macro `%s'", hp->name);
4136       else if (i == 1)
4137         error ("only 1 arg to macro `%s'", hp->name);
4138       else
4139         error ("only %d args to macro `%s'", i, hp->name);
4140     } else if (i > nargs)
4141       error ("too many (%d) args to macro `%s'", i, hp->name);
4142
4143     /* Swallow the closeparen.  */
4144     ++instack[indepth].bufp;
4145
4146     /* If macro wants zero args, we parsed the arglist for checking only.
4147        Read directly from the macro definition.  */
4148     if (nargs == 0) {
4149       xbuf = defn->expansion;
4150       xbuf_len = defn->length;
4151     } else {
4152       register U_CHAR *exp = defn->expansion;
4153       register int offset;      /* offset in expansion,
4154                                    copied a piece at a time */
4155       register int totlen;      /* total amount of exp buffer filled so far */
4156
4157       register struct reflist *ap;
4158
4159       /* Macro really takes args.  Compute the expansion of this call.  */
4160
4161       /* Compute length in characters of the macro's expansion.  */
4162       xbuf_len = defn->length;
4163       for (ap = defn->pattern; ap != NULL; ap = ap->next) {
4164         if (ap->stringify)
4165           xbuf_len += args[ap->argno].stringified_length;
4166         else 
4167           xbuf_len += args[ap->argno].raw_length;
4168       }
4169
4170       xbuf = (U_CHAR *) xmalloc (xbuf_len + 1);
4171
4172       /* Generate in XBUF the complete expansion
4173          with arguments substituted in.
4174          TOTLEN is the total size generated so far.
4175          OFFSET is the index in the definition
4176          of where we are copying from.  */
4177       offset = totlen = 0;
4178       for (ap = defn->pattern; ap != NULL; ap = ap->next) {
4179         register struct argdata *arg = &args[ap->argno];
4180
4181         for (i = 0; i < ap->nchars; i++)
4182           xbuf[totlen++] = exp[offset++];
4183
4184         if (ap->stringify != 0) {
4185           int arglen = arg->raw_length;
4186           int escaped = 0;
4187           int in_string = 0;
4188           int c;
4189           i = 0;
4190           while (i < arglen
4191                  && (c = arg->raw[i], is_space (c)))
4192             i++;
4193           while (i < arglen
4194                  && (c = arg->raw[arglen - 1], is_space (c)))
4195             arglen--;
4196           for (; i < arglen; i++) {
4197             c = arg->raw[i];
4198
4199             /* Special markers Newline Space
4200                generate nothing for a stringified argument.  */
4201             if (c == '\n' && arg->raw[i+1] != '\n') {
4202               i++;
4203               continue;
4204             }
4205
4206             /* Internal sequences of whitespace are replaced by one space
4207                except within an string or char token.  */
4208             if (! in_string
4209                 && (c == '\n' ? arg->raw[i+1] == '\n' : is_space (c))) {
4210               while (1) {
4211                 /* Note that Newline Space does occur within whitespace
4212                    sequences; consider it part of the sequence.  */
4213                 if (c == '\n' && is_space (arg->raw[i+1]))
4214                   i += 2;
4215                 else if (c != '\n' && is_space (c))
4216                   i++;
4217                 else break;
4218                 c = arg->raw[i];
4219               }
4220               i--;
4221               c = ' ';
4222             }
4223
4224             if (escaped)
4225               escaped = 0;
4226             else {
4227               if (c == '\\')
4228                 escaped = 1;
4229               if (in_string) {
4230                 if (c == in_string)
4231                   in_string = 0;
4232               } else if (c == '\"' || c == '\'')
4233                 in_string = c;
4234             }
4235
4236             /* Escape these chars */
4237             if (c == '\"' || (in_string && c == '\\'))
4238               xbuf[totlen++] = '\\';
4239             if (ISPRINT (c))
4240               xbuf[totlen++] = c;
4241             else {
4242               sprintf ((char *) &xbuf[totlen], "\\%03o", (unsigned int) c);
4243               totlen += 4;
4244             }
4245           }
4246         } else {
4247           const U_CHAR *p1 = arg->raw;
4248           const U_CHAR *l1 = p1 + arg->raw_length;
4249
4250           if (ap->raw_before) {
4251             while (p1 != l1 && is_space (*p1)) p1++;
4252             while (p1 != l1 && is_idchar (*p1))
4253               xbuf[totlen++] = *p1++;
4254             /* Delete any no-reexpansion marker that follows
4255                an identifier at the beginning of the argument
4256                if the argument is concatenated with what precedes it.  */
4257             if (p1[0] == '\n' && p1[1] == '-')
4258               p1 += 2;
4259           }
4260           if (ap->raw_after) {
4261             /* Arg is concatenated after: delete trailing whitespace,
4262                whitespace markers, and no-reexpansion markers.  */
4263             while (p1 != l1) {
4264               if (is_space (l1[-1])) l1--;
4265               else if (l1[-1] == '-') {
4266                 const U_CHAR *p2 = l1 - 1;
4267                 /* If a `-' is preceded by an odd number of newlines then it
4268                    and the last newline are a no-reexpansion marker.  */
4269                 while (p2 != p1 && p2[-1] == '\n') p2--;
4270                 if ((l1 - 1 - p2) & 1) {
4271                   l1 -= 2;
4272                 }
4273                 else break;
4274               }
4275               else break;
4276             }
4277           }
4278           memmove (xbuf + totlen, p1, l1 - p1);
4279           totlen += l1 - p1;
4280         }
4281
4282         if (totlen > xbuf_len)
4283           abort ();
4284       }
4285
4286       /* if there is anything left of the definition
4287          after handling the arg list, copy that in too. */
4288
4289       for (i = offset; i < defn->length; i++)
4290         xbuf[totlen++] = exp[i];
4291
4292       xbuf[totlen] = 0;
4293       xbuf_len = totlen;
4294
4295       for (i = 0; i < nargs; i++) {
4296         if (args[i].free1 != 0)
4297           free (args[i].free1);
4298         if (args[i].free2 != 0)
4299           free (args[i].free2);
4300       }
4301     }
4302   } else {
4303     xbuf = defn->expansion;
4304     xbuf_len = defn->length;
4305   }
4306
4307   /* Now put the expansion on the input stack
4308      so our caller will commence reading from it.  */
4309   {
4310     register FILE_BUF *ip2;
4311
4312     ip2 = &instack[++indepth];
4313
4314     ip2->fname = 0;
4315     ip2->lineno = 0;
4316     ip2->buf = xbuf;
4317     ip2->length = xbuf_len;
4318     ip2->bufp = xbuf;
4319     ip2->free_ptr = (nargs > 0) ? xbuf : 0;
4320     ip2->macro = hp;
4321     ip2->if_stack = if_stack;
4322   }
4323 }
4324 \f
4325 /*
4326  * Parse a macro argument and store the info on it into *ARGPTR.
4327  * Return nonzero to indicate a syntax error.
4328  */
4329
4330 static const char *
4331 macarg (argptr)
4332      register struct argdata *argptr;
4333 {
4334   FILE_BUF *ip = &instack[indepth];
4335   int paren = 0;
4336   int newlines = 0;
4337   int comments = 0;
4338
4339   /* Try to parse as much of the argument as exists at this
4340      input stack level.  */
4341   U_CHAR *bp = macarg1 (ip->bufp, ip->buf + ip->length,
4342                         &paren, &newlines, &comments);
4343
4344   /* If we find the end of the argument at this level,
4345      set up *ARGPTR to point at it in the input stack.  */
4346   if (!(ip->fname != 0 && (newlines != 0 || comments != 0))
4347       && bp != ip->buf + ip->length) {
4348     if (argptr != 0) {
4349       argptr->raw = ip->bufp;
4350       argptr->raw_length = bp - ip->bufp;
4351     }
4352     ip->bufp = bp;
4353   } else {
4354     /* This input stack level ends before the macro argument does.
4355        We must pop levels and keep parsing.
4356        Therefore, we must allocate a temporary buffer and copy
4357        the macro argument into it.  */
4358     int bufsize = bp - ip->bufp;
4359     int extra = newlines;
4360     U_CHAR *buffer = (U_CHAR *) xmalloc (bufsize + extra + 1);
4361     int final_start = 0;
4362
4363     memcpy (buffer, ip->bufp, bufsize);
4364     ip->bufp = bp;
4365     ip->lineno += newlines;
4366
4367     while (bp == ip->buf + ip->length) {
4368       if (instack[indepth].macro == 0) {
4369         free (buffer);
4370         return "unterminated macro call";
4371       }
4372       ip->macro->type = T_MACRO;
4373       if (ip->free_ptr)
4374         free (ip->free_ptr);
4375       ip = &instack[--indepth];
4376       newlines = 0;
4377       comments = 0;
4378       bp = macarg1 (ip->bufp, ip->buf + ip->length, &paren,
4379                     &newlines, &comments);
4380       final_start = bufsize;
4381       bufsize += bp - ip->bufp;
4382       extra += newlines;
4383       buffer = (U_CHAR *) xrealloc (buffer, bufsize + extra + 1);
4384       memcpy (buffer + bufsize - (bp - ip->bufp), ip->bufp, bp - ip->bufp);
4385       ip->bufp = bp;
4386       ip->lineno += newlines;
4387     }
4388
4389     /* Now, if arg is actually wanted, record its raw form,
4390        discarding comments and duplicating newlines in whatever
4391        part of it did not come from a macro expansion.
4392        EXTRA space has been preallocated for duplicating the newlines.
4393        FINAL_START is the index of the start of that part.  */
4394     if (argptr != 0) {
4395       argptr->raw = buffer;
4396       argptr->raw_length = bufsize;
4397       argptr->free1 = buffer;
4398       argptr->newlines = newlines;
4399       argptr->comments = comments;
4400       if ((newlines || comments) && ip->fname != 0)
4401         argptr->raw_length
4402           = final_start +
4403             discard_comments (argptr->raw + final_start,
4404                               argptr->raw_length - final_start,
4405                               newlines);
4406       argptr->raw[argptr->raw_length] = 0;
4407       if (argptr->raw_length > bufsize + extra)
4408         abort ();
4409     }
4410   }
4411
4412   /* If we are not discarding this argument,
4413      macroexpand it and compute its length as stringified.
4414      All this info goes into *ARGPTR.  */
4415
4416   if (argptr != 0) {
4417     FILE_BUF obuf;
4418     register const U_CHAR *buf, *lim;
4419     register int totlen;
4420
4421     obuf = expand_to_temp_buffer (argptr->raw,
4422                                   argptr->raw + argptr->raw_length,
4423                                   1);
4424
4425     argptr->expanded = obuf.buf;
4426     argptr->expand_length = obuf.length;
4427     argptr->free2 = obuf.buf;
4428
4429     buf = argptr->raw;
4430     lim = buf + argptr->raw_length;
4431
4432     totlen = 0;
4433     while (buf != lim) {
4434       register U_CHAR c = *buf++;
4435       totlen++;
4436       /* Internal sequences of whitespace are replaced by one space
4437          in most cases, but not always.  So count all the whitespace
4438          in case we need to keep it all.  */
4439       if (c == '\"' || c == '\\') /* escape these chars */
4440         totlen++;
4441       else if (!ISPRINT (c))
4442         totlen += 3;
4443     }
4444     argptr->stringified_length = totlen;
4445   }
4446   return 0;
4447 }
4448
4449 /* Scan text from START (inclusive) up to LIMIT (exclusive),
4450    counting parens in *DEPTHPTR,
4451    and return if reach LIMIT
4452    or before a `)' that would make *DEPTHPTR negative
4453    or before a comma when *DEPTHPTR is zero.
4454    Single and double quotes are matched and termination
4455    is inhibited within them.  Comments also inhibit it.
4456    Value returned is pointer to stopping place.
4457
4458    Increment *NEWLINES each time a newline is passed.
4459    Set *COMMENTS to 1 if a comment is seen.  */
4460
4461 static U_CHAR *
4462 macarg1 (start, limit, depthptr, newlines, comments)
4463      U_CHAR *start;
4464      register const U_CHAR *limit;
4465      int *depthptr, *newlines, *comments;
4466 {
4467   register U_CHAR *bp = start;
4468
4469   while (bp < limit) {
4470     switch (*bp) {
4471     case '(':
4472       (*depthptr)++;
4473       break;
4474     case ')':
4475       if (--(*depthptr) < 0)
4476         return bp;
4477       break;
4478     case '\\':
4479       /* Traditionally, backslash makes following char not special.  */
4480       if (bp + 1 < limit)
4481         {
4482           bp++;
4483           /* But count source lines anyway.  */
4484           if (*bp == '\n')
4485             ++*newlines;
4486         }
4487       break;
4488     case '\n':
4489       ++*newlines;
4490       break;
4491     case '/':
4492       if (bp[1] == '\\' && bp[2] == '\n')
4493         newline_fix (bp + 1);
4494       if (bp[1] != '*' || bp + 1 >= limit)
4495         break;
4496       *comments = 1;
4497       bp += 2;
4498       while (bp + 1 < limit) {
4499         if (bp[0] == '*'
4500             && bp[1] == '\\' && bp[2] == '\n')
4501           newline_fix (bp + 1);
4502         if (bp[0] == '*' && bp[1] == '/')
4503           break;
4504         if (*bp == '\n') ++*newlines;
4505         bp++;
4506       }
4507       bp += 1;
4508       break;
4509     case '\'':
4510     case '\"':
4511       {
4512         int quotec;
4513         for (quotec = *bp++; bp + 1 < limit && *bp != quotec; bp++) {
4514           if (*bp == '\\') {
4515             bp++;
4516             if (*bp == '\n')
4517               ++*newlines;
4518             while (*bp == '\\' && bp[1] == '\n') {
4519               bp += 2;
4520             }
4521           } else if (*bp == '\n') {
4522             ++*newlines;
4523             if (quotec == '\'')
4524               break;
4525           }
4526         }
4527       }
4528       break;
4529     case ',':
4530       if ((*depthptr) == 0)
4531         return bp;
4532       break;
4533     }
4534     bp++;
4535   }
4536
4537   return bp;
4538 }
4539
4540 /* Discard comments and duplicate newlines
4541    in the string of length LENGTH at START,
4542    except inside of string constants.
4543    The string is copied into itself with its beginning staying fixed.  
4544
4545    NEWLINES is the number of newlines that must be duplicated.
4546    We assume that that much extra space is available past the end
4547    of the string.  */
4548
4549 static int
4550 discard_comments (start, length, newlines)
4551      U_CHAR *start;
4552      int length;
4553      int newlines;
4554 {
4555   register U_CHAR *ibp;
4556   register U_CHAR *obp;
4557   register const U_CHAR *limit;
4558   register int c;
4559
4560   /* If we have newlines to duplicate, copy everything
4561      that many characters up.  Then, in the second part,
4562      we will have room to insert the newlines
4563      while copying down.
4564      NEWLINES may actually be too large, because it counts
4565      newlines in string constants, and we don't duplicate those.
4566      But that does no harm.  */
4567   if (newlines > 0) {
4568     ibp = start + length;
4569     obp = ibp + newlines;
4570     limit = start;
4571     while (limit != ibp)
4572       *--obp = *--ibp;
4573   }
4574
4575   ibp = start + newlines;
4576   limit = start + length + newlines;
4577   obp = start;
4578
4579   while (ibp < limit) {
4580     *obp++ = c = *ibp++;
4581     switch (c) {
4582     case '\n':
4583       /* Duplicate the newline.  */
4584       *obp++ = '\n';
4585       break;
4586
4587     case '\\':
4588       if (*ibp == '\n') {
4589         obp--;
4590         ibp++;
4591       }
4592       break;
4593
4594     case '/':
4595       if (*ibp == '\\' && ibp[1] == '\n')
4596         newline_fix (ibp);
4597       /* Delete any comment.  */
4598       if (ibp[0] != '*' || ibp + 1 >= limit)
4599         break;
4600       obp--;
4601       ibp++;
4602       while (ibp + 1 < limit) {
4603         if (ibp[0] == '*'
4604             && ibp[1] == '\\' && ibp[2] == '\n')
4605           newline_fix (ibp + 1);
4606         if (ibp[0] == '*' && ibp[1] == '/')
4607           break;
4608         ibp++;
4609       }
4610       ibp += 2;
4611       break;
4612
4613     case '\'':
4614     case '\"':
4615       /* Notice and skip strings, so that we don't
4616          think that comments start inside them,
4617          and so we don't duplicate newlines in them.  */
4618       {
4619         int quotec = c;
4620         while (ibp < limit) {
4621           *obp++ = c = *ibp++;
4622           if (c == quotec)
4623             break;
4624           if (c == '\n' && quotec == '\'')
4625             break;
4626           if (c == '\\' && ibp < limit) {
4627             while (*ibp == '\\' && ibp[1] == '\n')
4628               ibp += 2;
4629             *obp++ = *ibp++;
4630           }
4631         }
4632       }
4633       break;
4634     }
4635   }
4636
4637   return obp - start;
4638 }
4639 \f
4640
4641 /* Core error handling routine.  */
4642 static void
4643 v_message (mtype, line, msgid, ap)
4644      enum msgtype mtype;
4645      int line;
4646      const char *msgid;
4647      va_list ap;
4648 {
4649   const char *fname = 0;
4650   int i;
4651
4652   if (mtype == WARNING && inhibit_warnings)
4653     return;
4654
4655   for (i = indepth; i >= 0; i--)
4656     if (instack[i].fname != NULL) {
4657       if (line == 0)
4658         line = instack[i].lineno;
4659       fname = instack[i].fname;
4660       break;
4661     }
4662
4663   if (fname)
4664     fprintf (stderr, "%s:%d: ", fname, line);
4665   else
4666     fprintf (stderr, "%s: ", progname);
4667
4668   if (mtype == WARNING)
4669     fputs ("warning: ", stderr);
4670
4671   vfprintf (stderr, msgid, ap);
4672   putc ('\n', stderr);
4673
4674   if (mtype == ERROR)
4675     errors++;
4676 }
4677
4678 /*
4679  * error - print error message and increment count of errors.
4680  */
4681 void
4682 error VPARAMS ((const char *msgid, ...))
4683 {
4684 #ifndef ANSI_PROTOTYPES
4685   const char *msgid;
4686 #endif
4687   va_list ap;
4688
4689   VA_START(ap, msgid);
4690   
4691 #ifndef ANSI_PROTOTYPES
4692   msgid = va_arg (ap, const char *);
4693 #endif
4694
4695   v_message (ERROR, 0, msgid, ap);
4696 }
4697
4698 void
4699 error_with_line VPARAMS ((int line, const char *msgid, ...))
4700 {
4701 #ifndef ANSI_PROTOTYPES
4702   int line;
4703   const char *msgid;
4704 #endif
4705   va_list ap;
4706
4707   VA_START(ap, msgid);
4708   
4709 #ifndef ANSI_PROTOTYPES
4710   line = va_arg (ap, int);
4711   msgid = va_arg (ap, const char *);
4712 #endif
4713
4714   v_message (ERROR, line, msgid, ap);
4715 }
4716
4717 /* Error including a message from `errno'.  */
4718 void
4719 error_from_errno (name)
4720      const char *name;
4721 {
4722   error ("%s: %s", name, strerror (errno));
4723 }
4724
4725 /* Print error message but don't count it.  */
4726 void
4727 warning VPARAMS ((const char *msgid, ...))
4728 {
4729 #ifndef ANSI_PROTOTYPES
4730   const char *msgid;
4731 #endif
4732   va_list ap;
4733
4734   VA_START(ap, msgid);
4735   
4736 #ifndef ANSI_PROTOTYPES
4737   msgid = va_arg (ap, const char *);
4738 #endif
4739
4740   v_message (WARNING, 0, msgid, ap);
4741 }
4742
4743 void
4744 fatal VPARAMS ((const char *msgid, ...))
4745 {
4746 #ifndef ANSI_PROTOTYPES
4747   const char *msgid;
4748 #endif
4749   va_list ap;
4750
4751   VA_START(ap, msgid);
4752   
4753 #ifndef ANSI_PROTOTYPES
4754   msgid = va_arg (ap, const char *);
4755 #endif
4756
4757   v_message (FATAL, 0, msgid, ap);
4758   exit (FATAL_EXIT_CODE);
4759 }
4760
4761 /* More 'friendly' abort that prints the location at which we died.  */
4762 void
4763 fancy_abort (line, func)
4764      int line;
4765      const char *func;
4766 {
4767   fatal ("Internal error in %s, at tradcpp.c:%d\n\
4768 Please submit a full bug report.\n\
4769 See %s for instructions.", func, line, GCCBUGURL);
4770 }
4771
4772 void
4773 perror_with_name (name)
4774      const char *name;
4775 {
4776   fprintf (stderr, "%s: %s: %s\n", progname, name, strerror (errno));
4777   errors++;
4778 }
4779
4780 void
4781 pfatal_with_name (name)
4782      const char *name;
4783 {
4784   perror_with_name (name);
4785   exit (FATAL_EXIT_CODE);
4786 }
4787
4788 /* Return the line at which an error occurred.
4789    The error is not necessarily associated with the current spot
4790    in the input stack, so LINE says where.  LINE will have been
4791    copied from ip->lineno for the current input level.
4792    If the current level is for a file, we return LINE.
4793    But if the current level is not for a file, LINE is meaningless.
4794    In that case, we return the lineno of the innermost file.  */
4795 static int
4796 line_for_error (line)
4797      int line;
4798 {
4799   int i;
4800   int line1 = line;
4801
4802   for (i = indepth; i >= 0; ) {
4803     if (instack[i].fname != 0)
4804       return line1;
4805     i--;
4806     if (i < 0)
4807       return 0;
4808     line1 = instack[i].lineno;
4809   }
4810   return 0;
4811 }
4812
4813 /*
4814  * If OBUF doesn't have NEEDED bytes after OPTR, make it bigger.
4815  *
4816  * As things stand, nothing is ever placed in the output buffer to be
4817  * removed again except when it's KNOWN to be part of an identifier,
4818  * so flushing and moving down everything left, instead of expanding,
4819  * should work ok.
4820  */
4821
4822 static void
4823 grow_outbuf (obuf, needed)
4824      register FILE_BUF *obuf;
4825      register int needed;
4826 {
4827   register U_CHAR *p;
4828   int minsize;
4829
4830   if (obuf->length - (obuf->bufp - obuf->buf) > needed)
4831     return;
4832
4833   /* Make it at least twice as big as it is now.  */
4834   obuf->length *= 2;
4835   /* Make it have at least 150% of the free space we will need.  */
4836   minsize = (3 * needed) / 2 + (obuf->bufp - obuf->buf);
4837   if (minsize > obuf->length)
4838     obuf->length = minsize;
4839
4840   p = (U_CHAR *) xrealloc (obuf->buf, obuf->length);
4841   obuf->bufp = p + (obuf->bufp - obuf->buf);
4842   obuf->buf = p;
4843 }
4844 \f
4845 /* Symbol table for macro names and special symbols */
4846
4847 /*
4848  * install a name in the main hash table, even if it is already there.
4849  *   name stops with first non alphanumeric, except leading '#'.
4850  * caller must check against redefinition if that is desired.
4851  * delete_macro () removes things installed by install () in fifo order.
4852  * this is important because of the `defined' special symbol used
4853  * in #if, and also if pushdef/popdef directives are ever implemented.
4854  *
4855  * If LEN is >= 0, it is the length of the name.
4856  * Otherwise, compute the length by scanning the entire name.
4857  *
4858  * If HASH is >= 0, it is the precomputed hash code.
4859  * Otherwise, compute the hash code.
4860  *
4861  * caller must set the value, if any is desired.
4862  */
4863 static HASHNODE *
4864 install (name, len, type, hash)
4865      const U_CHAR *name;
4866      int len;
4867      enum node_type type;
4868      int hash;
4869         /* watch out here if sizeof (U_CHAR *) != sizeof (int) */
4870 {
4871   register HASHNODE *hp;
4872   register int bucket;
4873   register const U_CHAR *p;
4874   U_CHAR *q;
4875
4876   if (len < 0) {
4877     p = name;
4878     while (is_idchar (*p))
4879       p++;
4880     len = p - name;
4881   }
4882
4883   if (hash < 0)
4884     hash = hashf (name, len, HASHSIZE);
4885
4886   hp = (HASHNODE *) xmalloc (sizeof (HASHNODE) + len + 1);
4887   bucket = hash;
4888   hp->bucket_hdr = &hashtab[bucket];
4889   hp->next = hashtab[bucket];
4890   hashtab[bucket] = hp;
4891   hp->prev = NULL;
4892   if (hp->next != NULL)
4893     hp->next->prev = hp;
4894   hp->type = type;
4895   hp->length = len;
4896   hp->name = q = ((U_CHAR *) hp) + sizeof (HASHNODE);
4897   memcpy (q, name, len);
4898   q[len] = 0;
4899   return hp;
4900 }
4901
4902 /*
4903  * find the most recent hash node for name name (ending with first
4904  * non-identifier char) installed by install
4905  *
4906  * If LEN is >= 0, it is the length of the name.
4907  * Otherwise, compute the length by scanning the entire name.
4908  *
4909  * If HASH is >= 0, it is the precomputed hash code.
4910  * Otherwise, compute the hash code.
4911  */
4912 HASHNODE *
4913 lookup (name, len, hash)
4914      const U_CHAR *name;
4915      int len;
4916      int hash;
4917 {
4918   register const U_CHAR *bp;
4919   register HASHNODE *bucket;
4920
4921   if (len < 0) {
4922     for (bp = name; is_idchar (*bp); bp++) ;
4923     len = bp - name;
4924   }
4925
4926   if (hash < 0)
4927     hash = hashf (name, len, HASHSIZE);
4928
4929   bucket = hashtab[hash];
4930   while (bucket) {
4931     if (bucket->length == len
4932         && strncmp ((const char *)bucket->name, (const char *)name, len) == 0)
4933       return bucket;
4934     bucket = bucket->next;
4935   }
4936   return NULL;
4937 }
4938
4939 /*
4940  * Delete a hash node.  Some weirdness to free junk from macros.
4941  * More such weirdness will have to be added if you define more hash
4942  * types that need it.
4943  */
4944
4945 /* Note that the DEFINITION of a macro is removed from the hash table
4946    but its storage is not freed.  This would be a storage leak
4947    except that it is not reasonable to keep undefining and redefining
4948    large numbers of macros many times.
4949    In any case, this is necessary, because a macro can be #undef'd
4950    in the middle of reading the arguments to a call to it.
4951    If #undef freed the DEFINITION, that would crash.  */
4952 static void
4953 delete_macro (hp)
4954      HASHNODE *hp;
4955 {
4956
4957   if (hp->prev != NULL)
4958     hp->prev->next = hp->next;
4959   if (hp->next != NULL)
4960     hp->next->prev = hp->prev;
4961
4962   /* make sure that the bucket chain header that
4963      the deleted guy was on points to the right thing afterwards. */
4964   if (hp == *hp->bucket_hdr)
4965     *hp->bucket_hdr = hp->next;
4966
4967   free (hp);
4968 }
4969
4970 /*
4971  * return hash function on name.  must be compatible with the one
4972  * computed a step at a time, elsewhere
4973  */
4974 static int
4975 hashf (name, len, hashsize)
4976      register const U_CHAR *name;
4977      register int len;
4978      int hashsize;
4979 {
4980   register int r = 0;
4981
4982   while (len--)
4983     r = HASHSTEP (r, *name++);
4984
4985   return MAKE_POS (r) % hashsize;
4986 }
4987 \f
4988 /* Dump all macro definitions as #defines to stdout.  */
4989
4990 static void
4991 dump_all_macros ()
4992 {
4993   int bucket;
4994
4995   for (bucket = 0; bucket < HASHSIZE; bucket++) {
4996     register HASHNODE *hp;
4997
4998     for (hp = hashtab[bucket]; hp; hp= hp->next) {
4999       if (hp->type == T_MACRO) {
5000         register DEFINITION *defn = hp->value.defn;
5001         struct reflist *ap;
5002         int offset;
5003         int concat;
5004
5005
5006         /* Print the definition of the macro HP.  */
5007
5008         printf ("#define %s", hp->name);
5009         if (defn->nargs >= 0) {
5010           int i;
5011
5012           printf ("(");
5013           for (i = 0; i < defn->nargs; i++) {
5014             dump_arg_n (defn, i);
5015             if (i + 1 < defn->nargs)
5016               printf (", ");
5017           }
5018           printf (")");
5019         }
5020
5021         printf (" ");
5022
5023         offset = 0;
5024         concat = 0;
5025         for (ap = defn->pattern; ap != NULL; ap = ap->next) {
5026           dump_defn_1 (defn->expansion, offset, ap->nchars);
5027           if (ap->nchars != 0)
5028             concat = 0;
5029           offset += ap->nchars;
5030           if (ap->stringify)
5031             printf (" #");
5032           if (ap->raw_before && !concat)
5033             printf (" ## ");
5034           concat = 0;
5035           dump_arg_n (defn, ap->argno);
5036           if (ap->raw_after) {
5037             printf (" ## ");
5038             concat = 1;
5039           }
5040         }
5041         dump_defn_1 (defn->expansion, offset, defn->length - offset);
5042         printf ("\n");
5043       }
5044     }
5045   }
5046 }
5047
5048 /* Output to stdout a substring of a macro definition.
5049    BASE is the beginning of the definition.
5050    Output characters START thru LENGTH.
5051    Discard newlines outside of strings, thus
5052    converting funny-space markers to ordinary spaces.  */
5053 static void
5054 dump_defn_1 (base, start, length)
5055      const U_CHAR *base;
5056      int start;
5057      int length;
5058 {
5059   const U_CHAR *p = base + start;
5060   const U_CHAR *limit = base + start + length;
5061
5062   while (p < limit) {
5063     if (*p != '\n')
5064       putchar (*p);
5065     else if (*p == '\"' || *p =='\'') {
5066       const U_CHAR *p1 = skip_quoted_string (p, limit, 0, 0, 0, 0);
5067       fwrite (p, p1 - p, 1, stdout);
5068       p = p1 - 1;
5069     }
5070     p++;
5071   }
5072 }
5073
5074 /* Print the name of argument number ARGNUM of macro definition DEFN.
5075    Recall that DEFN->argnames contains all the arg names
5076    concatenated in reverse order with comma-space in between.  */
5077 static void
5078 dump_arg_n (defn, argnum)
5079      DEFINITION *defn;
5080      int argnum;
5081 {
5082   register const U_CHAR *p = defn->argnames;
5083   while (argnum + 1 < defn->nargs) {
5084     p = (const U_CHAR *) strchr ((const char *)p, ' ') + 1;
5085     argnum++;
5086   }
5087
5088   while (*p && *p != ',') {
5089     putchar (*p);
5090     p++;
5091   }
5092 }
5093
5094 /* Initialize the built-in macros.  */
5095 #define DSC(x) U x, sizeof x - 1
5096 #define install_spec(name, type) \
5097  install(DSC(name), type, -1);
5098 #define install_value(name, val) \
5099  hp = install(DSC(name), T_CONST, -1); hp->value.cpval = val;
5100 static void
5101 initialize_builtins ()
5102 {
5103   HASHNODE *hp;
5104
5105   install_spec ("__BASE_FILE__",     T_BASE_FILE);
5106   install_spec ("__DATE__",          T_DATE);
5107   install_spec ("__FILE__",          T_FILE);
5108   install_spec ("__TIME__",          T_TIME);
5109   install_spec ("__VERSION__",       T_VERSION);
5110   install_spec ("__INCLUDE_LEVEL__", T_INCLUDE_LEVEL);
5111   install_spec ("__LINE__",          T_SPECLINE);
5112
5113 #ifndef NO_BUILTIN_SIZE_TYPE
5114   install_value ("__SIZE_TYPE__",         SIZE_TYPE);
5115 #endif
5116 #ifndef NO_BUILTIN_PTRDIFF_TYPE
5117   install_value ("__PTRDIFF_TYPE__",      PTRDIFF_TYPE);
5118 #endif
5119 #ifndef NO_BUILTIN_WCHAR_TYPE
5120   install_value ("__WCHAR_TYPE__",        WCHAR_TYPE);
5121 #endif
5122 #ifndef NO_BUILTIN_WINT_TYPE
5123   install_value ("__WINT_TYPE__",         WINT_TYPE);
5124 #endif
5125   install_value ("__REGISTER_PREFIX__",   REGISTER_PREFIX);
5126   install_value ("__USER_LABEL_PREFIX__", user_label_prefix);
5127 }
5128 #undef DSC
5129 #undef install_spec
5130 #undef install_value
5131 \f
5132 /* Common handler of command line directives -U, -D and -A.  */
5133 static void
5134 run_directive (str, len, type)
5135      const char *str;
5136      size_t len;
5137      enum node_type type;
5138 {
5139   struct directive *kt;
5140   FILE_BUF *ip = &instack[++indepth];
5141   ip->fname = "*command line*";
5142
5143   ip->buf = ip->bufp = (U_CHAR *) str;
5144   ip->length = len;
5145   ip->lineno = 1;
5146   ip->macro = 0;
5147   ip->free_ptr = 0;
5148   ip->if_stack = if_stack;
5149
5150   for (kt = directive_table; kt->type != type; kt++)
5151     ;
5152
5153   (*kt->func) ((U_CHAR *) str, (U_CHAR *) str + len, NULL);
5154   --indepth;
5155 }
5156
5157 /* Handle the -D option.  If STR is just an identifier, define it with
5158  * value 1.  If STR has anything after the identifier, then it should
5159  * be identifier-space-definition.  */
5160 static void
5161 make_definition (str)
5162      const char *str;
5163 {
5164   char *buf, *p;
5165   size_t count;
5166
5167   /* Copy the entire option so we can modify it. 
5168      Change the first "=" in the string to a space.  If there is none,
5169      tack " 1" on the end.  */
5170
5171   /* Length including the null.  */  
5172   count = strlen (str);
5173   buf = (char *) alloca (count + 2);
5174   memcpy (buf, str, count);
5175
5176   p = strchr (str, '=');
5177   if (p)
5178     buf[p - str] = ' ';
5179   else
5180     {
5181       buf[count++] = ' ';
5182       buf[count++] = '1';
5183     }
5184
5185   run_directive (buf, count, T_DEFINE);
5186 }
5187
5188 /* Handle the -U option.  */
5189 static void
5190 make_undef (str)
5191      const char *str;
5192 {
5193   run_directive (str, strlen (str), T_UNDEF);
5194 }
5195
5196 /* Handles the #assert (-A) and #unassert (-A-) command line options.  */
5197 static void
5198 make_assertion (str)
5199      const char *str;
5200 {
5201   enum node_type type = T_ASSERT;
5202   size_t count;
5203   const char *p;
5204
5205   if (*str == '-')
5206     {
5207       str++;
5208       type = T_UNASSERT;
5209     }
5210   
5211   count = strlen (str);
5212   p = strchr (str, '=');
5213   if (p)
5214     {
5215       /* Copy the entire option so we can modify it.  Change the first
5216          "=" in the string to a '(', and tack a ')' on the end.  */
5217       char *buf = (char *) alloca (count + 1);
5218
5219       memcpy (buf, str, count);
5220       buf[p - str] = '(';
5221       buf[count++] = ')';
5222       str = buf;
5223     }
5224
5225   run_directive (str, count, type);
5226 }
5227 \f
5228 /* Get the file-mode and data size of the file open on FD
5229    and store them in *MODE_POINTER and *SIZE_POINTER.  */
5230
5231 static int
5232 file_size_and_mode (fd, mode_pointer, size_pointer)
5233      int fd;
5234      int *mode_pointer;
5235      long *size_pointer;
5236 {
5237   struct stat sbuf;
5238
5239   if (fstat (fd, &sbuf) < 0) return -1;
5240   if (mode_pointer) *mode_pointer = sbuf.st_mode;
5241   if (size_pointer) *size_pointer = sbuf.st_size;
5242   return 0;
5243 }