Merge in gcc2 snapshot 19980929. See gcc/ChangeLog and gcc/FSFChangeLog for
[platform/upstream/gcc.git] / gcc / cpplib.c
1 /* CPP Library.
2    Copyright (C) 1986, 87, 89, 92-98, 1999 Free Software Foundation, Inc.
3    Contributed by Per Bothner, 1994-95.
4    Based on CCCP program by Paul Rubin, June 1986
5    Adapted to ANSI C, Richard Stallman, Jan 1987
6
7 This program is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 2, or (at your option) any
10 later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
20
21 #include "config.h"
22 #include "system.h"
23
24 #ifndef STDC_VALUE
25 #define STDC_VALUE 1
26 #endif
27
28 #include "cpplib.h"
29 #include "cpphash.h"
30 #include "output.h"
31 #include "intl.h"
32 #include "prefix.h"
33
34 #ifndef GET_ENV_PATH_LIST
35 #define GET_ENV_PATH_LIST(VAR,NAME)     do { (VAR) = getenv (NAME); } while (0)
36 #endif
37
38 /* By default, colon separates directories in a path.  */
39 #ifndef PATH_SEPARATOR
40 #define PATH_SEPARATOR ':'
41 #endif
42
43 #ifndef STANDARD_INCLUDE_DIR
44 #define STANDARD_INCLUDE_DIR "/usr/include"
45 #endif
46
47 /* Symbols to predefine.  */
48
49 #ifdef CPP_PREDEFINES
50 static char *predefs = CPP_PREDEFINES;
51 #else
52 static char *predefs = "";
53 #endif
54 \f
55 /* We let tm.h override the types used here, to handle trivial differences
56    such as the choice of unsigned int or long unsigned int for size_t.
57    When machines start needing nontrivial differences in the size type,
58    it would be best to do something here to figure out automatically
59    from other information what type to use.  */
60
61 /* The string value for __SIZE_TYPE__.  */
62
63 #ifndef SIZE_TYPE
64 #define SIZE_TYPE "long unsigned int"
65 #endif
66
67 /* The string value for __PTRDIFF_TYPE__.  */
68
69 #ifndef PTRDIFF_TYPE
70 #define PTRDIFF_TYPE "long int"
71 #endif
72
73 /* The string value for __WCHAR_TYPE__.  */
74
75 #ifndef WCHAR_TYPE
76 #define WCHAR_TYPE "int"
77 #endif
78 #define CPP_WCHAR_TYPE(PFILE) \
79         (CPP_OPTIONS (PFILE)->cplusplus ? "__wchar_t" : WCHAR_TYPE)
80
81 /* The string value for __USER_LABEL_PREFIX__ */
82
83 #ifndef USER_LABEL_PREFIX
84 #define USER_LABEL_PREFIX ""
85 #endif
86
87 /* The string value for __REGISTER_PREFIX__ */
88
89 #ifndef REGISTER_PREFIX
90 #define REGISTER_PREFIX ""
91 #endif
92 \f
93 #define SKIP_WHITE_SPACE(p) do { while (is_hor_space[*p]) p++; } while (0)
94 #define SKIP_ALL_WHITE_SPACE(p) do { while (is_space[*p]) p++; } while (0)
95
96 #define PEEKN(N) (CPP_BUFFER (pfile)->rlimit - CPP_BUFFER (pfile)->cur >= (N) ? CPP_BUFFER (pfile)->cur[N] : EOF)
97 #define FORWARD(N) CPP_FORWARD (CPP_BUFFER (pfile), (N))
98 #define GETC() CPP_BUF_GET (CPP_BUFFER (pfile))
99 #define PEEKC() CPP_BUF_PEEK (CPP_BUFFER (pfile))
100 /* CPP_IS_MACRO_BUFFER is true if the buffer contains macro expansion.
101    (Note that it is false while we're expanding marco *arguments*.) */
102 #define CPP_IS_MACRO_BUFFER(PBUF) ((PBUF)->cleanup == macro_cleanup)
103
104 /* Move all backslash-newline pairs out of embarrassing places.
105    Exchange all such pairs following BP
106    with any potentially-embarrassing characters that follow them.
107    Potentially-embarrassing characters are / and *
108    (because a backslash-newline inside a comment delimiter
109    would cause it not to be recognized).  */
110
111 #define NEWLINE_FIX \
112   do {while (PEEKC() == '\\' && PEEKN(1) == '\n') FORWARD(2); } while(0)
113
114 /* Same, but assume we've already read the potential '\\' into C.  */
115 #define NEWLINE_FIX1(C) do { \
116     while ((C) == '\\' && PEEKC() == '\n') { FORWARD(1); (C) = GETC(); }\
117   } while(0)
118
119 struct cpp_pending {
120   struct cpp_pending *next;
121   char *cmd;
122   char *arg;
123 };
124
125 /* Forward declarations.  */
126
127 extern void cpp_hash_cleanup PARAMS ((cpp_reader *));
128
129 static char *my_strerror                PROTO ((int));
130 static void make_assertion              PROTO ((cpp_reader *, char *, U_CHAR *));
131 static void path_include                PROTO ((cpp_reader *, char *));
132 static void initialize_builtins         PROTO ((cpp_reader *));
133 static void initialize_char_syntax      PROTO ((void));
134 static void validate_else               PROTO ((cpp_reader *, char *));
135 static int comp_def_part                PROTO ((int, U_CHAR *, int, U_CHAR *,
136                                                 int, int));
137 #ifdef abort
138 extern void fancy_abort ();
139 #endif
140 static int check_macro_name             PROTO ((cpp_reader *, U_CHAR *, int));
141 static int compare_defs                 PROTO ((cpp_reader *,
142                                                 DEFINITION *, DEFINITION *));
143 static HOST_WIDE_INT eval_if_expression PROTO ((cpp_reader *));
144 static int change_newlines              PROTO ((U_CHAR *, int));
145 static void push_macro_expansion PARAMS ((cpp_reader *,
146                                           U_CHAR *, int, HASHNODE *));
147 static struct cpp_pending *nreverse_pending PARAMS ((struct cpp_pending *));
148
149 static void conditional_skip            PROTO ((cpp_reader *, int,
150                                                 enum node_type, U_CHAR *));
151 static void skip_if_group               PROTO ((cpp_reader *));
152 static int parse_name                   PARAMS ((cpp_reader *, int));
153 static void print_help                  PROTO ((void));
154
155 /* Last arg to output_line_command.  */
156 enum file_change_code {same_file, enter_file, leave_file};
157
158 /* External declarations.  */
159
160 extern HOST_WIDE_INT cpp_parse_expr PARAMS ((cpp_reader *));
161 extern char *version_string;
162
163 /* #include "file" looks in source file dir, then stack.  */
164 /* #include <file> just looks in the stack.  */
165 /* -I directories are added to the end, then the defaults are added.  */
166 /* The */
167 static struct default_include {
168   char *fname;                  /* The name of the directory.  */
169   char *component;              /* The component containing the directory */
170   int cplusplus;                /* Only look here if we're compiling C++.  */
171   int cxx_aware;                /* Includes in this directory don't need to
172                                    be wrapped in extern "C" when compiling
173                                    C++.  */
174 } include_defaults_array[]
175 #ifdef INCLUDE_DEFAULTS
176   = INCLUDE_DEFAULTS;
177 #else
178   = {
179     /* Pick up GNU C++ specific include files.  */
180     { GPLUSPLUS_INCLUDE_DIR, "G++", 1, 1 },
181 #ifdef CROSS_COMPILE
182     /* This is the dir for fixincludes.  Put it just before
183        the files that we fix.  */
184     { GCC_INCLUDE_DIR, "GCC", 0, 0 },
185     /* For cross-compilation, this dir name is generated
186        automatically in Makefile.in.  */
187     { CROSS_INCLUDE_DIR, "GCC",0, 0 },
188 #ifdef TOOL_INCLUDE_DIR
189     /* This is another place that the target system's headers might be.  */
190     { TOOL_INCLUDE_DIR, "BINUTILS", 0, 1 },
191 #endif
192 #else /* not CROSS_COMPILE */
193 #ifdef LOCAL_INCLUDE_DIR
194     /* This should be /usr/local/include and should come before
195        the fixincludes-fixed header files.  */
196     { LOCAL_INCLUDE_DIR, 0, 0, 1 },
197 #endif
198 #ifdef TOOL_INCLUDE_DIR
199     /* This is here ahead of GCC_INCLUDE_DIR because assert.h goes here.
200        Likewise, behind LOCAL_INCLUDE_DIR, where glibc puts its assert.h.  */
201     { TOOL_INCLUDE_DIR, "BINUTILS", 0, 1 },
202 #endif
203     /* This is the dir for fixincludes.  Put it just before
204        the files that we fix.  */
205     { GCC_INCLUDE_DIR, "GCC", 0, 0 },
206     /* Some systems have an extra dir of include files.  */
207 #ifdef SYSTEM_INCLUDE_DIR
208     { SYSTEM_INCLUDE_DIR, 0, 0, 0 },
209 #endif
210 #ifndef STANDARD_INCLUDE_COMPONENT
211 #define STANDARD_INCLUDE_COMPONENT 0
212 #endif
213     { STANDARD_INCLUDE_DIR, STANDARD_INCLUDE_COMPONENT, 0, 0 },
214 #endif /* not CROSS_COMPILE */
215     { 0, 0, 0, 0 }
216     };
217 #endif /* no INCLUDE_DEFAULTS */
218
219 /* `struct directive' defines one #-directive, including how to handle it.  */
220
221 struct directive {
222   int length;                   /* Length of name */
223   int (*func)                   /* Function to handle directive */
224     PARAMS ((cpp_reader *, struct directive *));
225   char *name;                   /* Name of directive */
226   enum node_type type;          /* Code which describes which directive.  */
227 };
228
229 /* These functions are declared to return int instead of void since they
230    are going to be placed in a table and some old compilers have trouble with
231    pointers to functions returning void.  */
232
233 static int do_define PARAMS ((cpp_reader *, struct directive *));
234 static int do_line PARAMS ((cpp_reader *, struct directive *));
235 static int do_include PARAMS ((cpp_reader *, struct directive *));
236 static int do_undef PARAMS ((cpp_reader *, struct directive *));
237 static int do_error PARAMS ((cpp_reader *, struct directive *));
238 static int do_pragma PARAMS ((cpp_reader *, struct directive *));
239 static int do_ident PARAMS ((cpp_reader *, struct directive *));
240 static int do_if PARAMS ((cpp_reader *, struct directive *));
241 static int do_xifdef PARAMS ((cpp_reader *, struct directive *));
242 static int do_else PARAMS ((cpp_reader *, struct directive *));
243 static int do_elif PARAMS ((cpp_reader *, struct directive *));
244 static int do_endif PARAMS ((cpp_reader *, struct directive *));
245 #ifdef SCCS_DIRECTIVE
246 static int do_sccs PARAMS ((cpp_reader *, struct directive *));
247 #endif
248 static int do_assert PARAMS ((cpp_reader *, struct directive *));
249 static int do_unassert PARAMS ((cpp_reader *, struct directive *));
250 static int do_warning PARAMS ((cpp_reader *, struct directive *));
251
252 #define IS_INCLUDE_DIRECTIVE_TYPE(t) \
253 ((int) T_INCLUDE <= (int) (t) && (int) (t) <= (int) T_IMPORT)
254
255 /* Here is the actual list of #-directives, most-often-used first.
256    The initialize_builtins function assumes #define is the very first.  */
257
258 static struct directive directive_table[] = {
259   {  6, do_define,   "define",       T_DEFINE },
260   {  5, do_xifdef,   "ifdef",        T_IFDEF },
261   {  6, do_xifdef,   "ifndef",       T_IFNDEF },
262   {  7, do_include,  "include",      T_INCLUDE },
263   { 12, do_include,  "include_next", T_INCLUDE_NEXT },
264   {  6, do_include,  "import",       T_IMPORT },
265   {  5, do_endif,    "endif",        T_ENDIF },
266   {  4, do_else,     "else",         T_ELSE },
267   {  2, do_if,       "if",           T_IF },
268   {  4, do_elif,     "elif",         T_ELIF },
269   {  5, do_undef,    "undef",        T_UNDEF },
270   {  5, do_error,    "error",        T_ERROR },
271   {  7, do_warning,  "warning",      T_WARNING },
272   {  6, do_pragma,   "pragma",       T_PRAGMA },
273   {  4, do_line,     "line",         T_LINE },
274   {  5, do_ident,    "ident",        T_IDENT },
275 #ifdef SCCS_DIRECTIVE
276   {  4, do_sccs,     "sccs",         T_SCCS },
277 #endif
278   {  6, do_assert,   "assert",       T_ASSERT },
279   {  8, do_unassert, "unassert",     T_UNASSERT },
280   {  -1, 0, "", T_UNUSED }
281 };
282 \f
283 /* table to tell if char can be part of a C identifier.  */
284 U_CHAR is_idchar[256] = { 0 };
285 /* table to tell if char can be first char of a c identifier.  */
286 U_CHAR is_idstart[256] = { 0 };
287 /* table to tell if c is horizontal space.  */
288 U_CHAR is_hor_space[256] = { 0 };
289 /* table to tell if c is horizontal or vertical space.  */
290 U_CHAR is_space[256] = { 0 };
291 /* Table to handle trigraph conversion, which occurs before all other
292    processing, everywhere in the file.  (This is necessary since one
293    of the trigraphs encodes backslash.)  Note it's off by default.
294
295         from    to      from    to      from    to
296         ?? =    #       ?? )    ]       ?? !    |
297         ?? (    [       ?? '    ^       ?? >    }
298         ?? /    \       ?? <    {       ?? -    ~
299
300    There is not a space between the ?? and the third char.  I put spaces
301    there to avoid warnings when compiling this file. */
302 U_CHAR trigraph_table[256] = { 0 };
303
304 /* Initialize syntactic classifications of characters. */
305 static void
306 initialize_char_syntax ()
307 {
308   register int i;
309
310   /*
311    * Set up is_idchar and is_idstart tables.  These should be
312    * faster than saying (is_alpha (c) || c == '_'), etc.
313    * Set up these things before calling any routines tthat
314    * refer to them.
315    * XXX We should setlocale(LC_CTYPE, "C") here for safety.
316    */
317   for (i = 0; i < 256; i++)
318     {
319       is_idchar[i]  = ISALNUM (i);
320       is_idstart[i] = ISALPHA (i);
321     }
322
323   is_idchar['_']  = 1;
324   is_idstart['_'] = 1;
325
326   /* These will be reset later if -$ is in effect. */
327   is_idchar['$']  = 1;
328   is_idstart['$'] = 1;
329
330   /* horizontal space table */
331   is_hor_space[' '] = 1;
332   is_hor_space['\t'] = 1;
333   is_hor_space['\v'] = 1;
334   is_hor_space['\f'] = 1;
335   is_hor_space['\r'] = 1;
336
337   is_space[' '] = 1;
338   is_space['\t'] = 1;
339   is_space['\v'] = 1;
340   is_space['\f'] = 1;
341   is_space['\n'] = 1;
342   is_space['\r'] = 1;
343
344   /* trigraph conversion */
345   trigraph_table['='] = '#';  trigraph_table[')'] = ']';
346   trigraph_table['!'] = '|';  trigraph_table['('] = '[';
347   trigraph_table['\''] = '^'; trigraph_table['>'] = '}';
348   trigraph_table['/'] = '\\'; trigraph_table['<'] = '{';
349   trigraph_table['-'] = '~';
350 }
351
352 /* Place into PFILE a quoted string representing the string SRC.
353    Caller must reserve enough space in pfile->token_buffer.  */
354
355 static void
356 quote_string (pfile, src)
357      cpp_reader *pfile;
358      char *src;
359 {
360   U_CHAR c;
361
362   CPP_PUTC_Q (pfile, '\"');
363   for (;;)
364     switch ((c = *src++))
365       {
366       default:
367         if (ISPRINT (c))
368           CPP_PUTC_Q (pfile, c);
369         else
370           {
371             sprintf ((char *)CPP_PWRITTEN (pfile), "\\%03o", c);
372             CPP_ADJUST_WRITTEN (pfile, 4);
373           }
374         break;
375
376       case '\"':
377       case '\\':
378         CPP_PUTC_Q (pfile, '\\');
379         CPP_PUTC_Q (pfile, c);
380         break;
381       
382       case '\0':
383         CPP_PUTC_Q (pfile, '\"');
384         CPP_NUL_TERMINATE_Q (pfile);
385         return;
386       }
387 }
388
389 /* Re-allocates PFILE->token_buffer so it will hold at least N more chars.  */
390
391 void
392 cpp_grow_buffer (pfile, n)
393      cpp_reader *pfile;
394      long n;
395 {
396   long old_written = CPP_WRITTEN (pfile);
397   pfile->token_buffer_size = n + 2 * pfile->token_buffer_size;
398   pfile->token_buffer = (U_CHAR *)
399     xrealloc(pfile->token_buffer, pfile->token_buffer_size);
400   CPP_SET_WRITTEN (pfile, old_written);
401 }
402
403 \f
404 /*
405  * process a given definition string, for initialization
406  * If STR is just an identifier, define it with value 1.
407  * If STR has anything after the identifier, then it should
408  * be identifier=definition.
409  */
410
411 void
412 cpp_define (pfile, str)
413      cpp_reader *pfile;
414      U_CHAR *str;
415 {
416   U_CHAR *buf, *p;
417
418   buf = str;
419   p = str;
420   if (!is_idstart[*p])
421     {
422       cpp_error (pfile, "malformed option `-D %s'", str);
423       return;
424     }
425   while (is_idchar[*++p])
426     ;
427   if (*p == '(') {
428     while (is_idchar[*++p] || *p == ',' || is_hor_space[*p])
429       ;
430     if (*p++ != ')')
431       p = (U_CHAR *) str;                       /* Error */
432   }
433   if (*p == 0)
434     {
435       buf = (U_CHAR *) alloca (p - buf + 4);
436       strcpy ((char *)buf, str);
437       strcat ((char *)buf, " 1");
438     }
439   else if (*p != '=')
440     {
441       cpp_error (pfile, "malformed option `-D %s'", str);
442       return;
443     }
444   else
445     {
446       U_CHAR *q;
447       /* Copy the entire option so we can modify it.  */
448       buf = (U_CHAR *) alloca (2 * strlen (str) + 1);
449       strncpy (buf, str, p - str);
450       /* Change the = to a space.  */
451       buf[p - str] = ' ';
452       /* Scan for any backslash-newline and remove it.  */
453       p++;
454       q = &buf[p - str];
455       while (*p)
456         {
457       if (*p == '\\' && p[1] == '\n')
458         p += 2;
459       else
460         *q++ = *p++;
461     }
462     *q = 0;
463   }
464   
465   if (cpp_push_buffer (pfile, buf, strlen (buf)) != NULL)
466     {
467       do_define (pfile, NULL);
468       cpp_pop_buffer (pfile);
469     }
470 }
471 \f
472 /* Process the string STR as if it appeared as the body of a #assert.
473    OPTION is the option name for which STR was the argument.  */
474
475 static void
476 make_assertion (pfile, option, str)
477      cpp_reader *pfile;
478      char *option;
479      U_CHAR *str;
480 {
481   U_CHAR *buf, *p, *q;
482
483   /* Copy the entire option so we can modify it.  */
484   buf = (U_CHAR *) alloca (strlen (str) + 1);
485   strcpy ((char *) buf, str);
486   /* Scan for any backslash-newline and remove it.  */
487   p = q = buf;
488   while (*p) {
489 #if 0
490     if (*p == '\\' && p[1] == '\n')
491       p += 2;
492     else
493 #endif
494       *q++ = *p++;
495   }
496   *q = 0;
497
498   p = buf;
499   if (!is_idstart[*p]) {
500     cpp_error (pfile, "malformed option `%s %s'", option, str);
501     return;
502   }
503   while (is_idchar[*++p])
504     ;
505   while (*p == ' ' || *p == '\t') p++;
506   if (! (*p == 0 || *p == '(')) {
507     cpp_error (pfile, "malformed option `%s %s'", option, str);
508     return;
509   }
510
511   if (cpp_push_buffer (pfile, buf, strlen (buf)) != NULL)
512     {
513       do_assert (pfile, NULL);
514       cpp_pop_buffer (pfile);
515     }
516 }
517
518 /* Given a colon-separated list of file names PATH,
519    add all the names to the search path for include files.  */
520
521 static void
522 path_include (pfile, path)
523      cpp_reader *pfile;
524      char *path;
525 {
526   char *p;
527
528   p = path;
529
530   if (*p)
531     while (1) {
532       char *q = p;
533       char *name;
534
535       /* Find the end of this name.  */
536       while (*q != 0 && *q != PATH_SEPARATOR) q++;
537       if (p == q) {
538         /* An empty name in the path stands for the current directory.  */
539         name = (char *) xmalloc (2);
540         name[0] = '.';
541         name[1] = 0;
542       } else {
543         /* Otherwise use the directory that is named.  */
544         name = (char *) xmalloc (q - p + 1);
545         bcopy (p, name, q - p);
546         name[q - p] = 0;
547       }
548
549       append_include_chain (pfile,
550                             &(CPP_OPTIONS (pfile)->bracket_include), name, 0);
551
552       /* Advance past this name.  */
553       p = q;
554       if (*p == 0)
555         break;
556       /* Skip the colon.  */
557       p++;
558     }
559 }
560 \f
561 void
562 cpp_options_init (opts)
563      cpp_options *opts;
564 {
565   bzero ((char *) opts, sizeof *opts);
566   opts->in_fname = NULL;
567   opts->out_fname = NULL;
568
569   opts->dollars_in_ident = 1;
570   initialize_char_syntax ();
571
572   opts->no_line_commands = 0;
573   opts->trigraphs = 0;
574   opts->put_out_comments = 0;
575   opts->print_include_names = 0;
576   opts->dump_macros = dump_none;
577   opts->no_output = 0;
578   opts->remap = 0;
579   opts->cplusplus = 0;
580   opts->cplusplus_comments = 1;
581
582   opts->verbose = 0;
583   opts->objc = 0;
584   opts->lang_asm = 0;
585   opts->for_lint = 0;
586   opts->chill = 0;
587   opts->pedantic_errors = 0;
588   opts->inhibit_warnings = 0;
589   opts->warn_comments = 0;
590   opts->warn_import = 1;
591   opts->warnings_are_errors = 0;
592 }
593
594 enum cpp_token
595 null_underflow (pfile)
596      cpp_reader *pfile ATTRIBUTE_UNUSED;
597 {
598   return CPP_EOF;
599 }
600
601 int
602 null_cleanup (pbuf, pfile)
603      cpp_buffer *pbuf ATTRIBUTE_UNUSED;
604      cpp_reader *pfile ATTRIBUTE_UNUSED;
605 {
606   return 0;
607 }
608
609 int
610 macro_cleanup (pbuf, pfile)
611      cpp_buffer *pbuf;
612      cpp_reader *pfile ATTRIBUTE_UNUSED;
613 {
614   HASHNODE *macro = (HASHNODE *) pbuf->data;
615   if (macro->type == T_DISABLED)
616     macro->type = T_MACRO;
617   if (macro->type != T_MACRO || pbuf->buf != macro->value.defn->expansion)
618     free (pbuf->buf);
619   return 0;
620 }
621
622 /* Assuming we have read '/'.
623    If this is the start of a comment (followed by '*' or '/'),
624    skip to the end of the comment, and return ' '.
625    Return EOF if we reached the end of file before the end of the comment.
626    If not the start of a comment, return '/'.  */
627
628 static int
629 skip_comment (pfile, linep)
630      cpp_reader *pfile;
631      long *linep;
632 {
633   int c = 0;
634   while (PEEKC() == '\\' && PEEKN(1) == '\n')
635     {
636       if (linep)
637         (*linep)++;
638       FORWARD(2);
639     }
640   if (PEEKC() == '*')
641     {
642       FORWARD(1);
643       for (;;)
644         {
645           int prev_c = c;
646           c = GETC ();
647           if (c == EOF)
648             return EOF;
649           while (c == '\\' && PEEKC() == '\n')
650             {
651               if (linep)
652                 (*linep)++;
653               FORWARD(1), c = GETC();
654             }
655           if (prev_c == '*' && c == '/')
656             return ' ';
657           if (c == '\n' && linep)
658             (*linep)++;
659         }
660     }
661   else if (PEEKC() == '/' && CPP_OPTIONS (pfile)->cplusplus_comments)
662     {
663       FORWARD(1);
664       for (;;)
665         {
666           c = GETC ();
667           if (c == EOF)
668             return ' '; /* Allow // to be terminated by EOF.  */
669           while (c == '\\' && PEEKC() == '\n')
670             {
671               FORWARD(1);
672               c = GETC();
673               if (linep)
674                 (*linep)++;
675             }
676           if (c == '\n')
677             {
678               /* Don't consider final '\n' to be part of comment.  */
679               FORWARD(-1);
680               return ' ';
681             }
682         }
683     }
684   else
685     return '/';
686 }     
687
688 /* Skip whitespace \-newline and comments.  Does not macro-expand.  */
689
690 void
691 cpp_skip_hspace (pfile)
692      cpp_reader *pfile;
693 {
694   while (1)
695     {
696       int c = PEEKC();
697       if (c == EOF)
698         return; /* FIXME */
699       if (is_hor_space[c])
700         {
701           if ((c == '\f' || c == '\v') && CPP_PEDANTIC (pfile))
702             cpp_pedwarn (pfile, "%s in preprocessing directive",
703                          c == '\f' ? "formfeed" : "vertical tab");
704           FORWARD(1);
705         }
706       else if (c == '/')
707         {
708           FORWARD (1);
709           c = skip_comment (pfile, NULL);
710           if (c == '/')
711             FORWARD(-1);
712           if (c == EOF || c == '/')
713             return;
714         }
715       else if (c == '\\' && PEEKN(1) == '\n') {
716         FORWARD(2);
717       }
718       else if (c == '@' && CPP_BUFFER (pfile)->has_escapes
719                && is_hor_space[PEEKN(1)])
720         FORWARD(2);
721       else return;
722     }
723 }
724
725 /* Read the rest of the current line.
726    The line is appended to PFILE's output buffer.  */
727
728 static void
729 copy_rest_of_line (pfile)
730      cpp_reader *pfile;
731 {
732   struct cpp_options *opts = CPP_OPTIONS (pfile);
733   for (;;)
734     {
735       int c = GETC();
736       int nextc;
737       switch (c)
738         {
739         case EOF:
740           goto end_directive;
741         case '\\':
742           if (PEEKC() == '\n')
743             {
744               FORWARD (1);
745               continue;
746             }
747         case '\'':
748         case '\"':
749           goto scan_directive_token;
750           break;
751         case '/':
752           nextc = PEEKC();
753           if (nextc == '*' || (opts->cplusplus_comments && nextc == '/'))
754             goto scan_directive_token;
755           break;
756         case '\f':
757         case '\v':
758           if (CPP_PEDANTIC (pfile))
759             cpp_pedwarn (pfile, "%s in preprocessing directive",
760                          c == '\f' ? "formfeed" : "vertical tab");
761           break;
762
763         case '\n':
764           FORWARD(-1);
765           goto end_directive;
766         scan_directive_token:
767           FORWARD(-1);
768           cpp_get_token (pfile);
769           continue;
770         }
771       CPP_PUTC (pfile, c);
772     }
773  end_directive: ;
774   CPP_NUL_TERMINATE (pfile);
775 }
776
777 void
778 skip_rest_of_line (pfile)
779      cpp_reader *pfile;
780 {
781   long old = CPP_WRITTEN (pfile);
782   copy_rest_of_line (pfile);
783   CPP_SET_WRITTEN (pfile, old);
784 }
785
786 /* Handle a possible # directive.
787    '#' has already been read.  */
788
789 int
790 handle_directive (pfile)
791      cpp_reader *pfile;
792 { int c;
793   register struct directive *kt;
794   int ident_length;
795   U_CHAR *ident;
796   long old_written = CPP_WRITTEN (pfile);
797
798   cpp_skip_hspace (pfile);
799
800   c = PEEKC ();
801   if (c >= '0' && c <= '9')
802     {
803       /* Handle # followed by a line number.  */
804       if (CPP_PEDANTIC (pfile))
805         cpp_pedwarn (pfile, "`#' followed by integer");
806       do_line (pfile, NULL);
807       goto done_a_directive;
808     }
809
810   /* Now find the directive name.  */
811   CPP_PUTC (pfile, '#');
812   parse_name (pfile, GETC());
813   ident = pfile->token_buffer + old_written + 1;
814   ident_length = CPP_PWRITTEN (pfile) - ident;
815   if (ident_length == 0 && PEEKC() == '\n')
816     {
817       /* A line of just `#' becomes blank.  */
818       goto done_a_directive;
819     }
820
821 #if 0
822   if (ident_length == 0 || !is_idstart[*ident]) {
823     U_CHAR *p = ident;
824     while (is_idchar[*p]) {
825       if (*p < '0' || *p > '9')
826         break;
827       p++;
828     }
829     /* Avoid error for `###' and similar cases unless -pedantic.  */
830     if (p == ident) {
831       while (*p == '#' || is_hor_space[*p]) p++;
832       if (*p == '\n') {
833         if (pedantic && !lang_asm)
834           cpp_warning (pfile, "invalid preprocessor directive");
835         return 0;
836       }
837     }
838
839     if (!lang_asm)
840       cpp_error (pfile, "invalid preprocessor directive name");
841
842     return 0;
843   }
844 #endif
845   /*
846    * Decode the keyword and call the appropriate expansion
847    * routine, after moving the input pointer up to the next line.
848    */
849   for (kt = directive_table; ; kt++) {
850     if (kt->length <= 0)
851       goto not_a_directive;
852     if (kt->length == ident_length
853         && !strncmp (kt->name, ident, ident_length)) 
854       break;
855   }
856
857   /* We may want to pass through #define, #undef, #pragma, and #include.
858      Other directives may create output, but we don't want the directive
859      itself out, so we pop it now.  For example conditionals may emit
860      #failed ... #endfailed stuff.  */
861
862   if (! (kt->type == T_DEFINE
863          || kt->type == T_PRAGMA
864          || (IS_INCLUDE_DIRECTIVE_TYPE (kt->type)
865              && CPP_OPTIONS (pfile)->dump_includes)))
866     CPP_SET_WRITTEN (pfile, old_written);
867
868   (*kt->func) (pfile, kt);
869
870   if (kt->type == T_DEFINE)
871     {
872       if (CPP_OPTIONS (pfile)->dump_macros == dump_names)
873         {
874           /* Skip "#define". */
875           U_CHAR *p = pfile->token_buffer + old_written + 7;
876
877           SKIP_WHITE_SPACE (p);
878           while (is_idchar[*p]) p++;
879           pfile->limit = p;
880           CPP_PUTC (pfile, '\n');
881         }
882       else if (CPP_OPTIONS (pfile)->dump_macros != dump_definitions)
883         CPP_SET_WRITTEN (pfile, old_written);
884     }
885
886  done_a_directive:
887   return 1;
888
889  not_a_directive:
890   return 0;
891 }
892
893 /* Pass a directive through to the output file.
894    BUF points to the contents of the directive, as a contiguous string.
895 m   LIMIT points to the first character past the end of the directive.
896    KEYWORD is the keyword-table entry for the directive.  */
897
898 static void
899 pass_thru_directive (buf, limit, pfile, keyword)
900      U_CHAR *buf, *limit;
901      cpp_reader *pfile;
902      struct directive *keyword;
903 {
904   register unsigned keyword_length = keyword->length;
905
906   CPP_RESERVE (pfile, 1 + keyword_length + (limit - buf));
907   CPP_PUTC_Q (pfile, '#');
908   CPP_PUTS_Q (pfile, keyword->name, keyword_length);
909   if (limit != buf && buf[0] != ' ')
910     CPP_PUTC_Q (pfile, ' ');
911   CPP_PUTS_Q (pfile, buf, limit - buf);
912 #if 0
913   CPP_PUTS_Q (pfile, '\n');
914   /* Count the line we have just made in the output,
915      to get in sync properly.  */
916   pfile->lineno++;
917 #endif
918 }
919 \f
920 /* The arglist structure is built by do_define to tell
921    collect_definition where the argument names begin.  That
922    is, for a define like "#define f(x,y,z) foo+x-bar*y", the arglist
923    would contain pointers to the strings x, y, and z.
924    Collect_definition would then build a DEFINITION node,
925    with reflist nodes pointing to the places x, y, and z had
926    appeared.  So the arglist is just convenience data passed
927    between these two routines.  It is not kept around after
928    the current #define has been processed and entered into the
929    hash table.  */
930
931 struct arglist {
932   struct arglist *next;
933   U_CHAR *name;
934   int length;
935   int argno;
936   char rest_args;
937 };
938
939 /* Read a replacement list for a macro with parameters.
940    Build the DEFINITION structure.
941    Reads characters of text starting at BUF until END.
942    ARGLIST specifies the formal parameters to look for
943    in the text of the definition; NARGS is the number of args
944    in that list, or -1 for a macro name that wants no argument list.
945    MACRONAME is the macro name itself (so we can avoid recursive expansion)
946    and NAMELEN is its length in characters.
947    
948    Note that comments, backslash-newlines, and leading white space
949    have already been deleted from the argument.  */
950
951 static DEFINITION *
952 collect_expansion (pfile, buf, limit, nargs, arglist)
953      cpp_reader *pfile;
954      U_CHAR *buf, *limit;
955      int nargs;
956      struct arglist *arglist;
957 {
958   DEFINITION *defn;
959   register U_CHAR *p, *lastp, *exp_p;
960   struct reflist *endpat = NULL;
961   /* Pointer to first nonspace after last ## seen.  */
962   U_CHAR *concat = 0;
963   /* Pointer to first nonspace after last single-# seen.  */
964   U_CHAR *stringify = 0;
965   int maxsize;
966   int expected_delimiter = '\0';
967
968   /* Scan thru the replacement list, ignoring comments and quoted
969      strings, picking up on the macro calls.  It does a linear search
970      thru the arg list on every potential symbol.  Profiling might say
971      that something smarter should happen.  */
972
973   if (limit < buf)
974     abort ();
975
976   /* Find the beginning of the trailing whitespace.  */
977   p = buf;
978   while (p < limit && is_space[limit[-1]]) limit--;
979
980   /* Allocate space for the text in the macro definition.
981      Leading and trailing whitespace chars need 2 bytes each.
982      Each other input char may or may not need 1 byte,
983      so this is an upper bound.  The extra 5 are for invented
984      leading and trailing newline-marker and final null.  */
985   maxsize = (sizeof (DEFINITION)
986              + (limit - p) + 5);
987   /* Occurrences of '@' get doubled, so allocate extra space for them.  */
988   while (p < limit)
989     if (*p++ == '@')
990       maxsize++;
991   defn = (DEFINITION *) xcalloc (1, maxsize);
992
993   defn->nargs = nargs;
994   exp_p = defn->expansion = (U_CHAR *) defn + sizeof (DEFINITION);
995   lastp = exp_p;
996
997   p = buf;
998
999   /* Add one initial space escape-marker to prevent accidental
1000      token-pasting (often removed by macroexpand).  */
1001   *exp_p++ = '@';
1002   *exp_p++ = ' ';
1003
1004   if (limit - p >= 2 && p[0] == '#' && p[1] == '#') {
1005     cpp_error (pfile, "`##' at start of macro definition");
1006     p += 2;
1007   }
1008
1009   /* Process the main body of the definition.  */
1010   while (p < limit) {
1011     int skipped_arg = 0;
1012     register U_CHAR c = *p++;
1013
1014     *exp_p++ = c;
1015
1016     if (!CPP_TRADITIONAL (pfile)) {
1017       switch (c) {
1018       case '\'':
1019       case '\"':
1020         if (expected_delimiter != '\0') {
1021           if (c == expected_delimiter)
1022             expected_delimiter = '\0';
1023         } else
1024           expected_delimiter = c;
1025         break;
1026
1027       case '\\':
1028         if (p < limit && expected_delimiter) {
1029           /* In a string, backslash goes through
1030              and makes next char ordinary.  */
1031           *exp_p++ = *p++;
1032         }
1033         break;
1034
1035       case '@':
1036         /* An '@' in a string or character constant stands for itself,
1037            and does not need to be escaped.  */
1038         if (!expected_delimiter)
1039           *exp_p++ = c;
1040         break;
1041
1042       case '#':
1043         /* # is ordinary inside a string.  */
1044         if (expected_delimiter)
1045           break;
1046         if (p < limit && *p == '#') {
1047           /* ##: concatenate preceding and following tokens.  */
1048           /* Take out the first #, discard preceding whitespace.  */
1049           exp_p--;
1050           while (exp_p > lastp && is_hor_space[exp_p[-1]])
1051             --exp_p;
1052           /* Skip the second #.  */
1053           p++;
1054           /* Discard following whitespace.  */
1055           SKIP_WHITE_SPACE (p);
1056           concat = p;
1057           if (p == limit)
1058             cpp_error (pfile, "`##' at end of macro definition");
1059         } else if (nargs >= 0) {
1060           /* Single #: stringify following argument ref.
1061              Don't leave the # in the expansion.  */
1062           exp_p--;
1063           SKIP_WHITE_SPACE (p);
1064           if (p == limit || ! is_idstart[*p]
1065               || (*p == 'L' && p + 1 < limit && (p[1] == '\'' || p[1] == '"')))
1066             cpp_error (pfile,
1067                      "`#' operator is not followed by a macro argument name");
1068           else
1069             stringify = p;
1070         }
1071         break;
1072       }
1073     } else {
1074       /* In -traditional mode, recognize arguments inside strings and
1075          character constants, and ignore special properties of #.
1076          Arguments inside strings are considered "stringified", but no
1077          extra quote marks are supplied.  */
1078       switch (c) {
1079       case '\'':
1080       case '\"':
1081         if (expected_delimiter != '\0') {
1082           if (c == expected_delimiter)
1083             expected_delimiter = '\0';
1084         } else
1085           expected_delimiter = c;
1086         break;
1087
1088       case '\\':
1089         /* Backslash quotes delimiters and itself, but not macro args.  */
1090         if (expected_delimiter != 0 && p < limit
1091             && (*p == expected_delimiter || *p == '\\')) {
1092           *exp_p++ = *p++;
1093           continue;
1094         }
1095         break;
1096
1097       case '/':
1098         if (expected_delimiter != '\0') /* No comments inside strings.  */
1099           break;
1100         if (*p == '*') {
1101           /* If we find a comment that wasn't removed by handle_directive,
1102              this must be -traditional.  So replace the comment with
1103              nothing at all.  */
1104           exp_p--;
1105           p += 1;
1106           while (p < limit && !(p[-2] == '*' && p[-1] == '/'))
1107             p++;
1108 #if 0
1109           /* Mark this as a concatenation-point, as if it had been ##.  */
1110           concat = p;
1111 #endif
1112         }
1113         break;
1114       }
1115     }
1116
1117     /* Handle the start of a symbol.  */
1118     if (is_idchar[c] && nargs > 0) {
1119       U_CHAR *id_beg = p - 1;
1120       int id_len;
1121
1122       --exp_p;
1123       while (p != limit && is_idchar[*p]) p++;
1124       id_len = p - id_beg;
1125
1126       if (is_idstart[c]
1127           && ! (id_len == 1 && c == 'L' && (*p == '\'' || *p == '"'))) {
1128         register struct arglist *arg;
1129
1130         for (arg = arglist; arg != NULL; arg = arg->next) {
1131           struct reflist *tpat;
1132
1133           if (arg->name[0] == c
1134               && arg->length == id_len
1135               && strncmp (arg->name, id_beg, id_len) == 0) {
1136             if (expected_delimiter && CPP_OPTIONS (pfile)->warn_stringify) {
1137               if (CPP_TRADITIONAL (pfile)) {
1138                 cpp_warning (pfile, "macro argument `%.*s' is stringified.",
1139                              id_len, arg->name);
1140               } else {
1141                 cpp_warning (pfile,
1142                     "macro arg `%.*s' would be stringified with -traditional.",
1143                              id_len, arg->name);
1144               }
1145             }
1146             /* If ANSI, don't actually substitute inside a string.  */
1147             if (!CPP_TRADITIONAL (pfile) && expected_delimiter)
1148               break;
1149             /* make a pat node for this arg and append it to the end of
1150                the pat list */
1151             tpat = (struct reflist *) xmalloc (sizeof (struct reflist));
1152             tpat->next = NULL;
1153             tpat->raw_before = concat == id_beg;
1154             tpat->raw_after = 0;
1155             tpat->rest_args = arg->rest_args;
1156             tpat->stringify = (CPP_TRADITIONAL (pfile)
1157                                ? expected_delimiter != '\0'
1158                                : stringify == id_beg);
1159
1160             if (endpat == NULL)
1161               defn->pattern = tpat;
1162             else
1163               endpat->next = tpat;
1164             endpat = tpat;
1165
1166             tpat->argno = arg->argno;
1167             tpat->nchars = exp_p - lastp;
1168             {
1169               register U_CHAR *p1 = p;
1170               SKIP_WHITE_SPACE (p1);
1171               if (p1 + 2 <= limit && p1[0] == '#' && p1[1] == '#')
1172                 tpat->raw_after = 1;
1173             }
1174             lastp = exp_p;      /* place to start copying from next time */
1175             skipped_arg = 1;
1176             break;
1177           }
1178         }
1179       }
1180
1181       /* If this was not a macro arg, copy it into the expansion.  */
1182       if (! skipped_arg) {
1183         register U_CHAR *lim1 = p;
1184         p = id_beg;
1185         while (p != lim1)
1186           *exp_p++ = *p++;
1187         if (stringify == id_beg)
1188           cpp_error (pfile,
1189                    "`#' operator should be followed by a macro argument name");
1190       }
1191     }
1192   }
1193
1194   if (!CPP_TRADITIONAL (pfile) && expected_delimiter == 0)
1195     {
1196       /* If ANSI, put in a "@ " marker to prevent token pasting.
1197          But not if "inside a string" (which in ANSI mode
1198          happens only for -D option).  */
1199       *exp_p++ = '@';
1200       *exp_p++ = ' ';
1201     }
1202
1203   *exp_p = '\0';
1204
1205   defn->length = exp_p - defn->expansion;
1206
1207   /* Crash now if we overrun the allocated size.  */
1208   if (defn->length + 1 > maxsize)
1209     abort ();
1210
1211 #if 0
1212 /* This isn't worth the time it takes.  */
1213   /* give back excess storage */
1214   defn->expansion = (U_CHAR *) xrealloc (defn->expansion, defn->length + 1);
1215 #endif
1216
1217   return defn;
1218 }
1219
1220 /*
1221  * special extension string that can be added to the last macro argument to 
1222  * allow it to absorb the "rest" of the arguments when expanded.  Ex:
1223  *              #define wow(a, b...)            process (b, a, b)
1224  *              { wow (1, 2, 3); }      ->      { process (2, 3, 1, 2, 3); }
1225  *              { wow (one, two); }     ->      { process (two, one, two); }
1226  * if this "rest_arg" is used with the concat token '##' and if it is not
1227  * supplied then the token attached to with ## will not be outputted.  Ex:
1228  *              #define wow (a, b...)           process (b ## , a, ## b)
1229  *              { wow (1, 2); }         ->      { process (2, 1, 2); }
1230  *              { wow (one); }          ->      { process (one); {
1231  */
1232 static char rest_extension[] = "...";
1233 #define REST_EXTENSION_LENGTH   (sizeof (rest_extension) - 1)
1234
1235 /* Create a DEFINITION node from a #define directive.  Arguments are 
1236    as for do_define.  */
1237
1238 static MACRODEF
1239 create_definition (buf, limit, pfile, predefinition)
1240      U_CHAR *buf, *limit;
1241      cpp_reader *pfile;
1242      int predefinition;
1243 {
1244   U_CHAR *bp;                   /* temp ptr into input buffer */
1245   U_CHAR *symname;              /* remember where symbol name starts */
1246   int sym_length;               /* and how long it is */
1247   int rest_args = 0;
1248   long line, col;
1249   char *file = CPP_BUFFER (pfile) ? CPP_BUFFER (pfile)->nominal_fname : "";
1250   DEFINITION *defn;
1251   int arglengths = 0;           /* Accumulate lengths of arg names
1252                                    plus number of args.  */
1253   MACRODEF mdef;
1254   cpp_buf_line_and_col (CPP_BUFFER (pfile), &line, &col);
1255
1256   bp = buf;
1257
1258   while (is_hor_space[*bp])
1259     bp++;
1260
1261   symname = bp;                 /* remember where it starts */
1262
1263   sym_length = check_macro_name (pfile, bp, 0);
1264   bp += sym_length;
1265
1266   /* Lossage will occur if identifiers or control keywords are broken
1267      across lines using backslash.  This is not the right place to take
1268      care of that.  */
1269
1270   if (*bp == '(') {
1271     struct arglist *arg_ptrs = NULL;
1272     int argno = 0;
1273
1274     bp++;                       /* skip '(' */
1275     SKIP_WHITE_SPACE (bp);
1276
1277     /* Loop over macro argument names.  */
1278     while (*bp != ')') {
1279       struct arglist *temp;
1280
1281       temp = (struct arglist *) alloca (sizeof (struct arglist));
1282       temp->name = bp;
1283       temp->next = arg_ptrs;
1284       temp->argno = argno++;
1285       temp->rest_args = 0;
1286       arg_ptrs = temp;
1287
1288       if (rest_args)
1289         cpp_pedwarn (pfile, "another parameter follows `%s'", rest_extension);
1290
1291       if (!is_idstart[*bp])
1292         cpp_pedwarn (pfile, "invalid character in macro parameter name");
1293       
1294       /* Find the end of the arg name.  */
1295       while (is_idchar[*bp]) {
1296         bp++;
1297         /* do we have a "special" rest-args extension here? */
1298         if ((size_t)(limit - bp) > REST_EXTENSION_LENGTH
1299             && strncmp (rest_extension, bp, REST_EXTENSION_LENGTH) == 0) {
1300           rest_args = 1;
1301           temp->rest_args = 1;
1302           break;
1303         }
1304       }
1305       temp->length = bp - temp->name;
1306       if (rest_args == 1)
1307         bp += REST_EXTENSION_LENGTH;
1308       arglengths += temp->length + 2;
1309       SKIP_WHITE_SPACE (bp);
1310       if (temp->length == 0 || (*bp != ',' && *bp != ')')) {
1311         cpp_error (pfile, "badly punctuated parameter list in `#define'");
1312         goto nope;
1313       }
1314       if (*bp == ',') {
1315         bp++;
1316         SKIP_WHITE_SPACE (bp);
1317       }
1318       if (bp >= limit) {
1319         cpp_error (pfile, "unterminated parameter list in `#define'");
1320         goto nope;
1321       }
1322       {
1323         struct arglist *otemp;
1324
1325         for (otemp = temp->next; otemp != NULL; otemp = otemp->next)
1326           if (temp->length == otemp->length
1327               && strncmp (temp->name, otemp->name, temp->length) == 0) {
1328               U_CHAR *name;
1329
1330               name = (U_CHAR *) alloca (temp->length + 1);
1331               (void) strncpy (name, temp->name, temp->length);
1332               name[temp->length] = '\0';
1333               cpp_error (pfile,
1334                          "duplicate argument name `%s' in `#define'", name);
1335               goto nope;
1336           }
1337       }
1338     }
1339
1340     ++bp;                       /* skip paren */
1341     SKIP_WHITE_SPACE (bp);
1342     /* now everything from bp before limit is the definition.  */
1343     defn = collect_expansion (pfile, bp, limit, argno, arg_ptrs);
1344     defn->rest_args = rest_args;
1345
1346     /* Now set defn->args.argnames to the result of concatenating
1347        the argument names in reverse order
1348        with comma-space between them.  */
1349     defn->args.argnames = (U_CHAR *) xmalloc (arglengths + 1);
1350     {
1351       struct arglist *temp;
1352       int i = 0;
1353       for (temp = arg_ptrs; temp; temp = temp->next) {
1354         bcopy (temp->name, &defn->args.argnames[i], temp->length);
1355         i += temp->length;
1356         if (temp->next != 0) {
1357           defn->args.argnames[i++] = ',';
1358           defn->args.argnames[i++] = ' ';
1359         }
1360       }
1361       defn->args.argnames[i] = 0;
1362     }
1363   } else {
1364     /* Simple expansion or empty definition.  */
1365
1366     if (bp < limit)
1367       {
1368         if (is_hor_space[*bp]) {
1369           bp++;
1370           SKIP_WHITE_SPACE (bp);
1371         } else {
1372           switch (*bp) {
1373             case '!':  case '"':  case '#':  case '%':  case '&':  case '\'':
1374             case ')':  case '*':  case '+':  case ',':  case '-':  case '.':
1375             case '/':  case ':':  case ';':  case '<':  case '=':  case '>':
1376             case '?':  case '[':  case '\\': case ']':  case '^':  case '{':
1377             case '|':  case '}':  case '~':
1378               cpp_warning (pfile, "missing white space after `#define %.*s'",
1379                            sym_length, symname);
1380               break;
1381
1382             default:
1383               cpp_pedwarn (pfile, "missing white space after `#define %.*s'",
1384                            sym_length, symname);
1385               break;
1386           }
1387         }
1388       }
1389     /* now everything from bp before limit is the definition.  */
1390     defn = collect_expansion (pfile, bp, limit, -1, NULL_PTR);
1391     defn->args.argnames = (U_CHAR *) "";
1392   }
1393
1394   defn->line = line;
1395   defn->file = file;
1396
1397   /* OP is null if this is a predefinition */
1398   defn->predefined = predefinition;
1399   mdef.defn = defn;
1400   mdef.symnam = symname;
1401   mdef.symlen = sym_length;
1402
1403   return mdef;
1404
1405  nope:
1406   mdef.defn = 0;
1407   return mdef;
1408 }
1409
1410 /* Check a purported macro name SYMNAME, and yield its length.
1411    ASSERTION is nonzero if this is really for an assertion name.  */
1412
1413 static int
1414 check_macro_name (pfile, symname, assertion)
1415      cpp_reader *pfile;
1416      U_CHAR *symname;
1417      int assertion;
1418 {
1419   U_CHAR *p;
1420   int sym_length;
1421
1422   for (p = symname; is_idchar[*p]; p++)
1423     ;
1424   sym_length = p - symname;
1425   if (sym_length == 0
1426       || (sym_length == 1 && *symname == 'L' && (*p == '\'' || *p == '"')))
1427     cpp_error (pfile,
1428                assertion ? "invalid assertion name" : "invalid macro name");
1429   else if (!is_idstart[*symname]
1430            || (! strncmp (symname, "defined", 7) && sym_length == 7)) {
1431     U_CHAR *msg;                        /* what pain...  */
1432     msg = (U_CHAR *) alloca (sym_length + 1);
1433     bcopy (symname, msg, sym_length);
1434     msg[sym_length] = 0;
1435     cpp_error (pfile,
1436                (assertion
1437                 ? "invalid assertion name `%s'"
1438                 : "invalid macro name `%s'"),
1439                msg);
1440   }
1441   return sym_length;
1442 }
1443
1444 /* Return zero if two DEFINITIONs are isomorphic.  */
1445
1446 static int
1447 compare_defs (pfile, d1, d2)
1448      cpp_reader *pfile;
1449      DEFINITION *d1, *d2;
1450 {
1451   register struct reflist *a1, *a2;
1452   register U_CHAR *p1 = d1->expansion;
1453   register U_CHAR *p2 = d2->expansion;
1454   int first = 1;
1455
1456   if (d1->nargs != d2->nargs)
1457     return 1;
1458   if (CPP_PEDANTIC (pfile)
1459       && strcmp ((char *)d1->args.argnames, (char *)d2->args.argnames))
1460     return 1;
1461   for (a1 = d1->pattern, a2 = d2->pattern; a1 && a2;
1462        a1 = a1->next, a2 = a2->next) {
1463     if (!((a1->nchars == a2->nchars && ! strncmp (p1, p2, a1->nchars))
1464           || ! comp_def_part (first, p1, a1->nchars, p2, a2->nchars, 0))
1465         || a1->argno != a2->argno
1466         || a1->stringify != a2->stringify
1467         || a1->raw_before != a2->raw_before
1468         || a1->raw_after != a2->raw_after)
1469       return 1;
1470     first = 0;
1471     p1 += a1->nchars;
1472     p2 += a2->nchars;
1473   }
1474   if (a1 != a2)
1475     return 1;
1476   if (comp_def_part (first, p1, d1->length - (p1 - d1->expansion),
1477                      p2, d2->length - (p2 - d2->expansion), 1))
1478     return 1;
1479   return 0;
1480 }
1481
1482 /* Return 1 if two parts of two macro definitions are effectively different.
1483    One of the parts starts at BEG1 and has LEN1 chars;
1484    the other has LEN2 chars at BEG2.
1485    Any sequence of whitespace matches any other sequence of whitespace.
1486    FIRST means these parts are the first of a macro definition;
1487     so ignore leading whitespace entirely.
1488    LAST means these parts are the last of a macro definition;
1489     so ignore trailing whitespace entirely.  */
1490
1491 static int
1492 comp_def_part (first, beg1, len1, beg2, len2, last)
1493      int first;
1494      U_CHAR *beg1, *beg2;
1495      int len1, len2;
1496      int last;
1497 {
1498   register U_CHAR *end1 = beg1 + len1;
1499   register U_CHAR *end2 = beg2 + len2;
1500   if (first) {
1501     while (beg1 != end1 && is_space[*beg1]) beg1++;
1502     while (beg2 != end2 && is_space[*beg2]) beg2++;
1503   }
1504   if (last) {
1505     while (beg1 != end1 && is_space[end1[-1]]) end1--;
1506     while (beg2 != end2 && is_space[end2[-1]]) end2--;
1507   }
1508   while (beg1 != end1 && beg2 != end2) {
1509     if (is_space[*beg1] && is_space[*beg2]) {
1510       while (beg1 != end1 && is_space[*beg1]) beg1++;
1511       while (beg2 != end2 && is_space[*beg2]) beg2++;
1512     } else if (*beg1 == *beg2) {
1513       beg1++; beg2++;
1514     } else break;
1515   }
1516   return (beg1 != end1) || (beg2 != end2);
1517 }
1518
1519 /* Process a #define command.
1520 KEYWORD is the keyword-table entry for #define,
1521 or NULL for a "predefined" macro.  */
1522
1523 static int
1524 do_define (pfile, keyword)
1525      cpp_reader *pfile;
1526      struct directive *keyword;
1527 {
1528   int hashcode;
1529   MACRODEF mdef;
1530   HASHNODE *hp;
1531   int save_put_out_comments;
1532   long here;
1533   U_CHAR *macro, *buf, *end;
1534
1535   here = CPP_WRITTEN (pfile);
1536   
1537   save_put_out_comments = CPP_OPTIONS (pfile)->put_out_comments;
1538   CPP_OPTIONS (pfile)->put_out_comments = CPP_TRADITIONAL (pfile);
1539   copy_rest_of_line (pfile);
1540   CPP_OPTIONS (pfile)->put_out_comments = save_put_out_comments;
1541
1542   /* Copy out the line so we can pop the token buffer. */
1543   buf = pfile->token_buffer + here;
1544   end = CPP_PWRITTEN (pfile);
1545   macro = alloca (end - buf + 1);
1546   bcopy (buf, macro, end - buf + 1);
1547   end = macro + (end - buf);
1548
1549   CPP_SET_WRITTEN (pfile, here);
1550
1551 #if 0
1552   /* If this is a precompiler run (with -pcp) pass thru #define commands.  */
1553   if (pcp_outfile && keyword)
1554     pass_thru_directive (macro, end, pfile, keyword);
1555 #endif
1556
1557   mdef = create_definition (macro, end, pfile, keyword == NULL);
1558   if (mdef.defn == 0)
1559     goto nope;
1560
1561   hashcode = hashf (mdef.symnam, mdef.symlen, HASHSIZE);
1562
1563   if ((hp = cpp_lookup (pfile, mdef.symnam, mdef.symlen, hashcode)) != NULL)
1564     {
1565       int ok = 0;
1566       /* Redefining a precompiled key is ok.  */
1567       if (hp->type == T_PCSTRING)
1568         ok = 1;
1569       /* Redefining a macro is ok if the definitions are the same.  */
1570       else if (hp->type == T_MACRO)
1571         ok = ! compare_defs (pfile, mdef.defn, hp->value.defn);
1572       /* Redefining a constant is ok with -D.  */
1573       else if (hp->type == T_CONST)
1574         ok = ! CPP_OPTIONS (pfile)->done_initializing;
1575       /* Print the warning if it's not ok.  */
1576       if (!ok)
1577         {
1578           /* If we are passing through #define and #undef directives, do
1579              that for this re-definition now.  */
1580           if (CPP_OPTIONS (pfile)->debug_output && keyword)
1581             pass_thru_directive (macro, end, pfile, keyword);
1582
1583           cpp_pedwarn (pfile, "`%.*s' redefined", mdef.symlen, mdef.symnam);
1584           if (hp->type == T_MACRO)
1585             cpp_pedwarn_with_file_and_line (pfile, hp->value.defn->file, hp->value.defn->line,
1586                                       "this is the location of the previous definition");
1587         }
1588       /* Replace the old definition.  */
1589       hp->type = T_MACRO;
1590       hp->value.defn = mdef.defn;
1591     }
1592   else
1593     {
1594       /* If we are passing through #define and #undef directives, do
1595          that for this new definition now.  */
1596       if (CPP_OPTIONS (pfile)->debug_output && keyword)
1597         pass_thru_directive (macro, end, pfile, keyword);
1598       install (mdef.symnam, mdef.symlen, T_MACRO, 0,
1599                (char *) mdef.defn, hashcode);
1600     }
1601
1602   return 0;
1603
1604 nope:
1605
1606   return 1;
1607 }
1608
1609 /* This structure represents one parsed argument in a macro call.
1610    `raw' points to the argument text as written (`raw_length' is its length).
1611    `expanded' points to the argument's macro-expansion
1612    (its length is `expand_length').
1613    `stringified_length' is the length the argument would have
1614    if stringified.
1615    `use_count' is the number of times this macro arg is substituted
1616    into the macro.  If the actual use count exceeds 10, 
1617    the value stored is 10.  */
1618
1619 /* raw and expanded are relative to ARG_BASE */
1620 #define ARG_BASE ((pfile)->token_buffer)
1621
1622 struct argdata {
1623   /* Strings relative to pfile->token_buffer */
1624   long raw, expanded, stringified;
1625   int raw_length, expand_length;
1626   int stringified_length;
1627   char newlines;
1628   char use_count;
1629 };
1630
1631 /* Allocate a new cpp_buffer for PFILE, and push it on the input buffer stack.
1632    If BUFFER != NULL, then use the LENGTH characters in BUFFER
1633    as the new input buffer.
1634    Return the new buffer, or NULL on failure.  */
1635
1636 cpp_buffer *
1637 cpp_push_buffer (pfile, buffer, length)
1638      cpp_reader *pfile;
1639      U_CHAR *buffer;
1640      long length;
1641 {
1642   register cpp_buffer *buf = CPP_BUFFER (pfile);
1643   if (buf == pfile->buffer_stack)
1644     {
1645       cpp_fatal (pfile, "%s: macro or `#include' recursion too deep",
1646                  buf->fname);
1647       return NULL;
1648     }
1649   buf--;
1650   bzero ((char *) buf, sizeof (cpp_buffer));
1651   CPP_BUFFER (pfile) = buf;
1652   buf->if_stack = pfile->if_stack;
1653   buf->cleanup = null_cleanup;
1654   buf->underflow = null_underflow;
1655   buf->buf = buf->cur = buffer;
1656   buf->alimit = buf->rlimit = buffer + length;
1657   
1658   return buf;
1659 }
1660
1661 cpp_buffer *
1662 cpp_pop_buffer (pfile)
1663      cpp_reader *pfile;
1664 {
1665   cpp_buffer *buf = CPP_BUFFER (pfile);
1666   (*buf->cleanup) (buf, pfile);
1667   return ++CPP_BUFFER (pfile);
1668 }
1669
1670 /* Scan until CPP_BUFFER (PFILE) is exhausted into PFILE->token_buffer.
1671    Pop the buffer when done.  */
1672
1673 void
1674 cpp_scan_buffer (pfile)
1675      cpp_reader *pfile;
1676 {
1677   cpp_buffer *buffer = CPP_BUFFER (pfile);
1678   for (;;)
1679     {
1680       enum cpp_token token = cpp_get_token (pfile);
1681       if (token == CPP_EOF) /* Should not happen ...  */
1682         break;
1683       if (token == CPP_POP && CPP_BUFFER (pfile) == buffer)
1684         {
1685           cpp_pop_buffer (pfile);
1686           break;
1687         }
1688     }
1689 }
1690
1691 /*
1692  * Rescan a string (which may have escape marks) into pfile's buffer.
1693  * Place the result in pfile->token_buffer.
1694  *
1695  * The input is copied before it is scanned, so it is safe to pass
1696  * it something from the token_buffer that will get overwritten
1697  * (because it follows CPP_WRITTEN).  This is used by do_include.
1698  */
1699
1700 static void
1701 cpp_expand_to_buffer (pfile, buf, length)
1702      cpp_reader *pfile;
1703      U_CHAR *buf;
1704      int length;
1705 {
1706   register cpp_buffer *ip;
1707 #if 0
1708   cpp_buffer obuf;
1709 #endif
1710   U_CHAR *limit = buf + length;
1711   U_CHAR *buf1;
1712 #if 0
1713   int odepth = indepth;
1714 #endif
1715
1716   if (length < 0)
1717     abort ();
1718
1719   /* Set up the input on the input stack.  */
1720
1721   buf1 = (U_CHAR *) alloca (length + 1);
1722   {
1723     register U_CHAR *p1 = buf;
1724     register U_CHAR *p2 = buf1;
1725
1726     while (p1 != limit)
1727       *p2++ = *p1++;
1728   }
1729   buf1[length] = 0;
1730
1731   ip = cpp_push_buffer (pfile, buf1, length);
1732   if (ip == NULL)
1733     return;
1734   ip->has_escapes = 1;
1735 #if 0
1736   ip->lineno = obuf.lineno = 1;
1737 #endif
1738
1739   /* Scan the input, create the output.  */
1740   cpp_scan_buffer (pfile);
1741
1742 #if 0
1743   if (indepth != odepth)
1744     abort ();
1745 #endif
1746
1747   CPP_NUL_TERMINATE (pfile);
1748 }
1749
1750 \f
1751 static void
1752 adjust_position (buf, limit, linep, colp)
1753      U_CHAR *buf;
1754      U_CHAR *limit;
1755      long *linep;
1756      long *colp;
1757 {
1758   while (buf < limit)
1759     {
1760       U_CHAR ch = *buf++;
1761       if (ch == '\n')
1762         (*linep)++, (*colp) = 1;
1763       else
1764         (*colp)++;
1765     }
1766 }
1767
1768 /* Move line_base forward, updating lineno and colno.  */
1769
1770 static void
1771 update_position (pbuf)
1772      register cpp_buffer *pbuf;
1773 {
1774   unsigned char *old_pos = pbuf->buf + pbuf->line_base;
1775   unsigned char *new_pos = pbuf->cur;
1776   register struct parse_marker *mark;
1777   for (mark = pbuf->marks;  mark != NULL; mark = mark->next)
1778     {
1779       if (pbuf->buf + mark->position < new_pos)
1780         new_pos = pbuf->buf + mark->position;
1781     }
1782   pbuf->line_base += new_pos - old_pos;
1783   adjust_position (old_pos, new_pos, &pbuf->lineno, &pbuf->colno);
1784 }
1785
1786 void
1787 cpp_buf_line_and_col (pbuf, linep, colp)
1788      register cpp_buffer *pbuf;
1789      long *linep, *colp;
1790 {
1791   long dummy;
1792   if (colp == NULL)
1793     colp = &dummy;
1794   if (pbuf)
1795     {
1796       *linep = pbuf->lineno;
1797       *colp = pbuf->colno;
1798       adjust_position (pbuf->buf + pbuf->line_base, pbuf->cur, linep, colp);
1799     }
1800   else
1801     {
1802       *linep = 0;
1803       *colp = 0;
1804     }
1805 }
1806
1807 /* Return the cpp_buffer that corresponds to a file (not a macro).  */
1808
1809 cpp_buffer *
1810 cpp_file_buffer (pfile)
1811      cpp_reader *pfile;
1812 {
1813   cpp_buffer *ip = CPP_BUFFER (pfile);
1814
1815   for ( ; ip != CPP_NULL_BUFFER (pfile); ip = CPP_PREV_BUFFER (ip))
1816     if (ip->fname != NULL)
1817       return ip;
1818   return NULL;
1819 }
1820
1821 static long
1822 count_newlines (buf, limit)
1823      register U_CHAR *buf;
1824      register U_CHAR *limit;
1825 {
1826   register long count = 0;
1827   while (buf < limit)
1828     {
1829       U_CHAR ch = *buf++;
1830       if (ch == '\n')
1831         count++;
1832     }
1833   return count;
1834 }
1835
1836 /*
1837  * write out a #line command, for instance, after an #include file.
1838  * If CONDITIONAL is nonzero, we can omit the #line if it would
1839  * appear to be a no-op, and we can output a few newlines instead
1840  * if we want to increase the line number by a small amount.
1841  * FILE_CHANGE says whether we are entering a file, leaving, or neither.
1842  */
1843
1844 static void
1845 output_line_command (pfile, conditional, file_change)
1846      cpp_reader *pfile;
1847      int conditional;
1848      enum file_change_code file_change;
1849 {
1850   long line, col;
1851   cpp_buffer *ip = CPP_BUFFER (pfile);
1852
1853   if (ip->fname == NULL)
1854     return;
1855
1856   update_position (ip);
1857
1858   if (CPP_OPTIONS (pfile)->no_line_commands
1859       || CPP_OPTIONS (pfile)->no_output)
1860     return;
1861
1862   line = CPP_BUFFER (pfile)->lineno;
1863   col = CPP_BUFFER (pfile)->colno;
1864   adjust_position (CPP_LINE_BASE (ip), ip->cur, &line, &col);
1865
1866   if (CPP_OPTIONS (pfile)->no_line_commands)
1867     return;
1868
1869   if (conditional) {
1870     if (line == pfile->lineno)
1871       return;
1872
1873     /* If the inherited line number is a little too small,
1874        output some newlines instead of a #line command.  */
1875     if (line > pfile->lineno && line < pfile->lineno + 8) {
1876       CPP_RESERVE (pfile, 20);
1877       while (line > pfile->lineno) {
1878         CPP_PUTC_Q (pfile, '\n');
1879         pfile->lineno++;
1880       }
1881       return;
1882     }
1883   }
1884
1885 #if 0
1886   /* Don't output a line number of 0 if we can help it.  */
1887   if (ip->lineno == 0 && ip->bufp - ip->buf < ip->length
1888       && *ip->bufp == '\n') {
1889     ip->lineno++;
1890     ip->bufp++;
1891   }
1892 #endif
1893
1894   CPP_RESERVE (pfile, 4 * strlen (ip->nominal_fname) + 50);
1895   {
1896 #ifdef OUTPUT_LINE_COMMANDS
1897     static char sharp_line[] = "#line ";
1898 #else
1899     static char sharp_line[] = "# ";
1900 #endif
1901     CPP_PUTS_Q (pfile, sharp_line, sizeof(sharp_line)-1);
1902   }
1903
1904   sprintf ((char *) CPP_PWRITTEN (pfile), "%ld ", line);
1905   CPP_ADJUST_WRITTEN (pfile, strlen (CPP_PWRITTEN (pfile)));
1906
1907   quote_string (pfile, ip->nominal_fname); 
1908   if (file_change != same_file) {
1909     CPP_PUTC_Q (pfile, ' ');
1910     CPP_PUTC_Q (pfile, file_change == enter_file ? '1' : '2');
1911   }
1912   /* Tell cc1 if following text comes from a system header file.  */
1913   if (ip->system_header_p) {
1914     CPP_PUTC_Q (pfile, ' ');
1915     CPP_PUTC_Q (pfile, '3');
1916   }
1917 #ifndef NO_IMPLICIT_EXTERN_C
1918   /* Tell cc1plus if following text should be treated as C.  */
1919   if (ip->system_header_p == 2 && CPP_OPTIONS (pfile)->cplusplus) {
1920     CPP_PUTC_Q (pfile, ' ');
1921     CPP_PUTC_Q (pfile, '4');
1922   }
1923 #endif
1924   CPP_PUTC_Q (pfile, '\n');
1925   pfile->lineno = line;
1926 }
1927 \f
1928 /*
1929  * Parse a macro argument and append the info on PFILE's token_buffer.
1930  * REST_ARGS means to absorb the rest of the args.
1931  * Return nonzero to indicate a syntax error.
1932  */
1933
1934 static enum cpp_token
1935 macarg (pfile, rest_args)
1936      cpp_reader *pfile;
1937      int rest_args;
1938 {
1939   int paren = 0;
1940   enum cpp_token token;
1941   char save_put_out_comments = CPP_OPTIONS (pfile)->put_out_comments;
1942   CPP_OPTIONS (pfile)->put_out_comments = 0;
1943
1944   /* Try to parse as much of the argument as exists at this
1945      input stack level.  */
1946   pfile->no_macro_expand++;
1947   for (;;)
1948     {
1949       token = cpp_get_token (pfile);
1950       switch (token)
1951         {
1952         case CPP_EOF:
1953           goto done;
1954         case CPP_POP:
1955           /* If we've hit end of file, it's an error (reported by caller).
1956              Ditto if it's the end of cpp_expand_to_buffer text.
1957              If we've hit end of macro, just continue.  */
1958           if (! CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
1959             goto done;
1960           break;
1961         case CPP_LPAREN:
1962           paren++;
1963           break;
1964         case CPP_RPAREN:
1965           if (--paren < 0)
1966             goto found;
1967           break;
1968         case CPP_COMMA:
1969           /* if we've returned to lowest level and
1970              we aren't absorbing all args */
1971           if (paren == 0 && rest_args == 0)
1972             goto found;
1973           break;
1974         found:
1975           /* Remove ',' or ')' from argument buffer.  */
1976           CPP_ADJUST_WRITTEN (pfile, -1);
1977           goto done;
1978       default: ;
1979         }
1980     }
1981
1982  done:
1983   CPP_OPTIONS (pfile)->put_out_comments = save_put_out_comments;
1984   pfile->no_macro_expand--;
1985
1986   return token;
1987 }
1988 \f
1989 /* Turn newlines to spaces in the string of length LENGTH at START,
1990    except inside of string constants.
1991    The string is copied into itself with its beginning staying fixed.  */
1992
1993 static int
1994 change_newlines (start, length)
1995      U_CHAR *start;
1996      int length;
1997 {
1998   register U_CHAR *ibp;
1999   register U_CHAR *obp;
2000   register U_CHAR *limit;
2001   register int c;
2002
2003   ibp = start;
2004   limit = start + length;
2005   obp = start;
2006
2007   while (ibp < limit) {
2008     *obp++ = c = *ibp++;
2009     switch (c) {
2010
2011     case '\'':
2012     case '\"':
2013       /* Notice and skip strings, so that we don't delete newlines in them.  */
2014       {
2015         int quotec = c;
2016         while (ibp < limit) {
2017           *obp++ = c = *ibp++;
2018           if (c == quotec)
2019             break;
2020           if (c == '\n' && quotec == '\'')
2021             break;
2022         }
2023       }
2024       break;
2025     }
2026   }
2027
2028   return obp - start;
2029 }
2030
2031 \f
2032 static struct tm *
2033 timestamp (pfile)
2034      cpp_reader *pfile;
2035 {
2036   if (!pfile->timebuf) {
2037     time_t t = time ((time_t *) 0);
2038     pfile->timebuf = localtime (&t);
2039   }
2040   return pfile->timebuf;
2041 }
2042
2043 static char *monthnames[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
2044                              "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
2045                             };
2046
2047 /*
2048  * expand things like __FILE__.  Place the expansion into the output
2049  * buffer *without* rescanning.
2050  */
2051
2052 static void
2053 special_symbol (hp, pfile)
2054      HASHNODE *hp;
2055      cpp_reader *pfile;
2056 {
2057   const char *buf;
2058   char *wbuf;
2059   int len;
2060   int true_indepth;
2061   cpp_buffer *ip = NULL;
2062   struct tm *timebuf;
2063
2064   int paren = 0;                /* For special `defined' keyword */
2065
2066 #if 0
2067   if (pcp_outfile && pcp_inside_if
2068       && hp->type != T_SPEC_DEFINED && hp->type != T_CONST)
2069     cpp_error (pfile,
2070                "Predefined macro `%s' used inside `#if' during precompilation",
2071                hp->name);
2072 #endif
2073     
2074   for (ip = CPP_BUFFER (pfile); ; ip = CPP_PREV_BUFFER (ip))
2075     {
2076       if (ip == CPP_NULL_BUFFER (pfile))
2077         {
2078           cpp_error (pfile, "cccp error: not in any file?!");
2079           return;                       /* the show must go on */
2080         }
2081       if (ip->fname != NULL)
2082         break;
2083     }
2084
2085   switch (hp->type)
2086     {
2087     case T_FILE:
2088     case T_BASE_FILE:
2089       {
2090         char *string;
2091         if (hp->type == T_BASE_FILE)
2092           {
2093             while (CPP_PREV_BUFFER (ip) != CPP_NULL_BUFFER (pfile))
2094               ip = CPP_PREV_BUFFER (ip);
2095           }
2096         string = ip->nominal_fname;
2097
2098         if (!string)
2099           string = "";
2100         CPP_RESERVE (pfile, 3 + 4 * strlen (string));
2101         quote_string (pfile, string);
2102         return;
2103       }
2104
2105     case T_INCLUDE_LEVEL:
2106       true_indepth = 0;
2107       ip = CPP_BUFFER (pfile);
2108       for (;  ip != CPP_NULL_BUFFER (pfile); ip = CPP_PREV_BUFFER (ip))
2109         if (ip->fname != NULL)
2110           true_indepth++;
2111
2112       wbuf = (char *) alloca (8); /* Eight bytes ought to be more than enough*/
2113       sprintf (wbuf, "%d", true_indepth - 1);
2114       buf = wbuf;
2115       break;
2116
2117   case T_VERSION:
2118       wbuf = (char *) alloca (3 + strlen (version_string));
2119       sprintf (wbuf, "\"%s\"", version_string);
2120       buf = wbuf;
2121       break;
2122
2123 #ifndef NO_BUILTIN_SIZE_TYPE
2124     case T_SIZE_TYPE:
2125       buf = SIZE_TYPE;
2126       break;
2127 #endif
2128
2129 #ifndef NO_BUILTIN_PTRDIFF_TYPE
2130     case T_PTRDIFF_TYPE:
2131       buf = PTRDIFF_TYPE;
2132       break;
2133 #endif
2134
2135     case T_WCHAR_TYPE:
2136       buf = CPP_WCHAR_TYPE (pfile);
2137     break;
2138
2139     case T_USER_LABEL_PREFIX_TYPE:
2140       buf = user_label_prefix;
2141       break;
2142
2143     case T_REGISTER_PREFIX_TYPE:
2144       buf = REGISTER_PREFIX;
2145       break;
2146
2147   case T_CONST:
2148       wbuf = (char *) alloca (4 * sizeof (int));
2149       sprintf (wbuf, "%d", hp->value.ival);
2150 #ifdef STDC_0_IN_SYSTEM_HEADERS
2151       if (ip->system_header_p
2152           && hp->length == 8 && bcmp (hp->name, "__STDC__", 8) == 0
2153           && ! cpp_lookup (pfile, (U_CHAR *) "__STRICT_ANSI__", -1, -1))
2154         strcpy (wbuf, "0");
2155 #endif
2156 #if 0
2157       if (pcp_inside_if && pcp_outfile)
2158         /* Output a precondition for this macro use */
2159         fprintf (pcp_outfile, "#define %s %d\n", hp->name, hp->value.ival);
2160 #endif
2161       buf = wbuf;
2162       break;
2163
2164     case T_SPECLINE:
2165       {
2166         long line = ip->lineno;
2167         long col = ip->colno;
2168         adjust_position (CPP_LINE_BASE (ip), ip->cur, &line, &col);
2169
2170         wbuf = (char *) alloca (10);
2171         sprintf (wbuf, "%ld", line);
2172         buf = wbuf;
2173       }
2174       break;
2175
2176     case T_DATE:
2177     case T_TIME:
2178       wbuf = (char *) alloca (20);
2179       timebuf = timestamp (pfile);
2180       if (hp->type == T_DATE)
2181         sprintf (wbuf, "\"%s %2d %4d\"", monthnames[timebuf->tm_mon],
2182                  timebuf->tm_mday, timebuf->tm_year + 1900);
2183       else
2184         sprintf (wbuf, "\"%02d:%02d:%02d\"", timebuf->tm_hour, timebuf->tm_min,
2185                  timebuf->tm_sec);
2186       buf = wbuf;
2187       break;
2188
2189     case T_SPEC_DEFINED:
2190       buf = " 0 ";              /* Assume symbol is not defined */
2191       ip = CPP_BUFFER (pfile);
2192       SKIP_WHITE_SPACE (ip->cur);
2193       if (*ip->cur == '(')
2194         {
2195           paren++;
2196           ip->cur++;                    /* Skip over the paren */
2197           SKIP_WHITE_SPACE (ip->cur);
2198         }
2199
2200       if (!is_idstart[*ip->cur])
2201         goto oops;
2202       if (ip->cur[0] == 'L' && (ip->cur[1] == '\'' || ip->cur[1] == '"'))
2203         goto oops;
2204       if ((hp = cpp_lookup (pfile, ip->cur, -1, -1)))
2205         {
2206 #if 0
2207           if (pcp_outfile && pcp_inside_if
2208               && (hp->type == T_CONST
2209                   || (hp->type == T_MACRO && hp->value.defn->predefined)))
2210             /* Output a precondition for this macro use.  */
2211             fprintf (pcp_outfile, "#define %s\n", hp->name);
2212 #endif
2213           buf = " 1 ";
2214         }
2215 #if 0
2216       else
2217         if (pcp_outfile && pcp_inside_if)
2218           {
2219             /* Output a precondition for this macro use */
2220             U_CHAR *cp = ip->bufp;
2221             fprintf (pcp_outfile, "#undef ");
2222             while (is_idchar[*cp]) /* Ick! */
2223               fputc (*cp++, pcp_outfile);
2224             putc ('\n', pcp_outfile);
2225           }
2226 #endif
2227       while (is_idchar[*ip->cur])
2228         ++ip->cur;
2229       SKIP_WHITE_SPACE (ip->cur);
2230       if (paren)
2231         {
2232           if (*ip->cur != ')')
2233             goto oops;
2234           ++ip->cur;
2235         }
2236       break;
2237
2238     oops:
2239
2240       cpp_error (pfile, "`defined' without an identifier");
2241       break;
2242
2243     default:
2244       cpp_error (pfile, "cccp error: invalid special hash type"); /* time for gdb */
2245       abort ();
2246     }
2247   len = strlen (buf);
2248   CPP_RESERVE (pfile, len + 1);
2249   CPP_PUTS_Q (pfile, buf, len);
2250   CPP_NUL_TERMINATE_Q (pfile);
2251
2252   return;
2253 }
2254
2255 /* Write out a #define command for the special named MACRO_NAME
2256    to PFILE's token_buffer.  */
2257
2258 static void
2259 dump_special_to_buffer (pfile, macro_name)
2260      cpp_reader *pfile;
2261      char *macro_name;
2262 {
2263   static char define_directive[] = "#define ";
2264   int macro_name_length = strlen (macro_name);
2265   output_line_command (pfile, 0, same_file);
2266   CPP_RESERVE (pfile, sizeof(define_directive) + macro_name_length);
2267   CPP_PUTS_Q (pfile, define_directive, sizeof(define_directive)-1);
2268   CPP_PUTS_Q (pfile, macro_name, macro_name_length);
2269   CPP_PUTC_Q (pfile, ' ');
2270   cpp_expand_to_buffer (pfile, macro_name, macro_name_length);
2271   CPP_PUTC (pfile, '\n');
2272 }
2273
2274 /* Initialize the built-in macros.  */
2275
2276 static void
2277 initialize_builtins (pfile)
2278      cpp_reader *pfile;
2279 {
2280   install ((U_CHAR *)"__LINE__", -1, T_SPECLINE, 0, 0, -1);
2281   install ((U_CHAR *)"__DATE__", -1, T_DATE, 0, 0, -1);
2282   install ((U_CHAR *)"__FILE__", -1, T_FILE, 0, 0, -1);
2283   install ((U_CHAR *)"__BASE_FILE__", -1, T_BASE_FILE, 0, 0, -1);
2284   install ((U_CHAR *)"__INCLUDE_LEVEL__", -1, T_INCLUDE_LEVEL, 0, 0, -1);
2285   install ((U_CHAR *)"__VERSION__", -1, T_VERSION, 0, 0, -1);
2286 #ifndef NO_BUILTIN_SIZE_TYPE
2287   install ((U_CHAR *)"__SIZE_TYPE__", -1, T_SIZE_TYPE, 0, 0, -1);
2288 #endif
2289 #ifndef NO_BUILTIN_PTRDIFF_TYPE
2290   install ((U_CHAR *)"__PTRDIFF_TYPE__ ", -1, T_PTRDIFF_TYPE, 0, 0, -1);
2291 #endif
2292   install ((U_CHAR *)"__WCHAR_TYPE__", -1, T_WCHAR_TYPE, 0, 0, -1);
2293   install ((U_CHAR *)"__USER_LABEL_PREFIX__", -1, T_USER_LABEL_PREFIX_TYPE, 0, 0, -1);
2294   install ((U_CHAR *)"__REGISTER_PREFIX__", -1, T_REGISTER_PREFIX_TYPE, 0, 0, -1);
2295   install ((U_CHAR *)"__TIME__", -1, T_TIME, 0, 0, -1);
2296   if (!CPP_TRADITIONAL (pfile))
2297     install ((U_CHAR *)"__STDC__", -1, T_CONST, STDC_VALUE, 0, -1);
2298   if (CPP_OPTIONS (pfile)->objc)
2299     install ((U_CHAR *)"__OBJC__", -1, T_CONST, 1, 0, -1);
2300 /*  This is supplied using a -D by the compiler driver
2301     so that it is present only when truly compiling with GNU C.  */
2302 /*  install ("__GNUC__", -1, T_CONST, 2, 0, -1);  */
2303
2304   if (CPP_OPTIONS (pfile)->debug_output)
2305     {
2306       dump_special_to_buffer (pfile, "__BASE_FILE__");
2307       dump_special_to_buffer (pfile, "__VERSION__");
2308 #ifndef NO_BUILTIN_SIZE_TYPE
2309       dump_special_to_buffer (pfile, "__SIZE_TYPE__");
2310 #endif
2311 #ifndef NO_BUILTIN_PTRDIFF_TYPE
2312       dump_special_to_buffer (pfile, "__PTRDIFF_TYPE__");
2313 #endif
2314       dump_special_to_buffer (pfile, "__WCHAR_TYPE__");
2315       dump_special_to_buffer (pfile, "__DATE__");
2316       dump_special_to_buffer (pfile, "__TIME__");
2317       if (!CPP_TRADITIONAL (pfile))
2318         dump_special_to_buffer (pfile, "__STDC__");
2319       if (CPP_OPTIONS (pfile)->objc)
2320         dump_special_to_buffer (pfile, "__OBJC__");
2321     }
2322 }
2323 \f
2324 /* Return 1 iff a token ending in C1 followed directly by a token C2
2325    could cause mis-tokenization.  */
2326
2327 static int
2328 unsafe_chars (c1, c2)
2329      int c1, c2;
2330 {
2331   switch (c1)
2332     {
2333     case '+': case '-':
2334       if (c2 == c1 || c2 == '=')
2335         return 1;
2336       goto letter;
2337     case '.':
2338     case '0': case '1': case '2': case '3': case '4':
2339     case '5': case '6': case '7': case '8': case '9':
2340     case 'e': case 'E': case 'p': case 'P':
2341       if (c2 == '-' || c2 == '+')
2342         return 1; /* could extend a pre-processing number */
2343       goto letter;
2344     case 'L':
2345       if (c2 == '\'' || c2 == '\"')
2346         return 1;   /* Could turn into L"xxx" or L'xxx'.  */
2347       goto letter;
2348     letter:
2349     case '_':
2350     case 'a': case 'b': case 'c': case 'd':           case 'f':
2351     case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
2352     case 'm': case 'n': case 'o':           case 'q': case 'r':
2353     case 's': case 't': case 'u': case 'v': case 'w': case 'x':
2354     case 'y': case 'z':
2355     case 'A': case 'B': case 'C': case 'D':           case 'F':
2356     case 'G': case 'H': case 'I': case 'J': case 'K':
2357     case 'M': case 'N': case 'O':           case 'Q': case 'R':
2358     case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
2359     case 'Y': case 'Z':
2360       /* We're in the middle of either a name or a pre-processing number.  */
2361       return (is_idchar[c2] || c2 == '.');
2362     case '<': case '>': case '!': case '%': case '#': case ':':
2363     case '^': case '&': case '|': case '*': case '/': case '=':
2364       return (c2 == c1 || c2 == '=');
2365     }
2366   return 0;
2367 }
2368
2369 /* Expand a macro call.
2370    HP points to the symbol that is the macro being called.
2371    Put the result of expansion onto the input stack
2372    so that subsequent input by our caller will use it.
2373
2374    If macro wants arguments, caller has already verified that
2375    an argument list follows; arguments come from the input stack.  */
2376
2377 static void
2378 macroexpand (pfile, hp)
2379      cpp_reader *pfile;
2380      HASHNODE *hp;
2381 {
2382   int nargs;
2383   DEFINITION *defn;
2384   register U_CHAR *xbuf;
2385   long start_line, start_column;
2386   int xbuf_len;
2387   struct argdata *args;
2388   long old_written = CPP_WRITTEN (pfile);
2389 #if 0
2390   int start_line = instack[indepth].lineno;
2391 #endif
2392   int rest_args, rest_zero;
2393       register int i;
2394
2395 #if 0
2396   CHECK_DEPTH (return;);
2397 #endif
2398
2399 #if 0
2400   /* This macro is being used inside a #if, which means it must be */
2401   /* recorded as a precondition.  */
2402   if (pcp_inside_if && pcp_outfile && defn->predefined)
2403     dump_single_macro (hp, pcp_outfile);
2404 #endif
2405
2406   cpp_buf_line_and_col (cpp_file_buffer (pfile), &start_line, &start_column);
2407
2408   /* Check for and handle special symbols. */
2409   if (hp->type != T_MACRO)
2410     {
2411       special_symbol (hp, pfile);
2412       xbuf_len = CPP_WRITTEN (pfile) - old_written;
2413       xbuf = (U_CHAR *) xmalloc (xbuf_len + 1);
2414       CPP_SET_WRITTEN (pfile, old_written);
2415       bcopy (CPP_PWRITTEN (pfile), xbuf, xbuf_len + 1);
2416       push_macro_expansion (pfile, xbuf, xbuf_len, hp);
2417       return;
2418     }
2419
2420   defn = hp->value.defn;
2421   nargs = defn->nargs;
2422   pfile->output_escapes++;
2423
2424   if (nargs >= 0)
2425     {
2426       enum cpp_token token;
2427
2428       args = (struct argdata *) alloca ((nargs + 1) * sizeof (struct argdata));
2429
2430       for (i = 0; i < nargs; i++)
2431         {
2432           args[i].raw = args[i].expanded = 0;
2433           args[i].raw_length = 0; 
2434           args[i].expand_length = args[i].stringified_length = -1;
2435           args[i].use_count = 0;
2436         }
2437
2438       /* Parse all the macro args that are supplied.  I counts them.
2439          The first NARGS args are stored in ARGS.
2440          The rest are discarded.  If rest_args is set then we assume
2441          macarg absorbed the rest of the args.  */
2442       i = 0;
2443       rest_args = 0;
2444       rest_args = 0;
2445       FORWARD(1); /* Discard the open-parenthesis before the first arg.  */
2446       do
2447         {
2448           if (rest_args)
2449             continue;
2450           if (i < nargs || (nargs == 0 && i == 0))
2451             {
2452               /* if we are working on last arg which absorbs rest of args... */
2453               if (i == nargs - 1 && defn->rest_args)
2454                 rest_args = 1;
2455               args[i].raw = CPP_WRITTEN (pfile);
2456               token = macarg (pfile, rest_args);
2457               args[i].raw_length = CPP_WRITTEN (pfile) - args[i].raw;
2458               args[i].newlines = 0; /* FIXME */
2459             }
2460           else
2461             token = macarg (pfile, 0);
2462           if (token == CPP_EOF || token == CPP_POP)
2463             {
2464               cpp_error_with_line (pfile, start_line, start_column,
2465                                    "unterminated macro call");
2466               return;
2467             }
2468           i++;
2469         } while (token == CPP_COMMA);
2470
2471       /* If we got one arg but it was just whitespace, call that 0 args.  */
2472       if (i == 1)
2473         {
2474           register U_CHAR *bp = ARG_BASE + args[0].raw;
2475           register U_CHAR *lim = bp + args[0].raw_length;
2476           /* cpp.texi says for foo ( ) we provide one argument.
2477              However, if foo wants just 0 arguments, treat this as 0.  */
2478           if (nargs == 0)
2479             while (bp != lim && is_space[*bp]) bp++;
2480           if (bp == lim)
2481             i = 0;
2482         }
2483
2484       /* Don't output an error message if we have already output one for
2485          a parse error above.  */
2486       rest_zero = 0;
2487       if (nargs == 0 && i > 0)
2488         {
2489           cpp_error (pfile, "arguments given to macro `%s'", hp->name);
2490         }
2491       else if (i < nargs)
2492         {
2493           /* traditional C allows foo() if foo wants one argument.  */
2494           if (nargs == 1 && i == 0 && CPP_TRADITIONAL (pfile))
2495             ;
2496           /* the rest args token is allowed to absorb 0 tokens */
2497           else if (i == nargs - 1 && defn->rest_args)
2498             rest_zero = 1;
2499           else if (i == 0)
2500             cpp_error (pfile, "macro `%s' used without args", hp->name);
2501           else if (i == 1)
2502             cpp_error (pfile, "macro `%s' used with just one arg", hp->name);
2503           else
2504             cpp_error (pfile, "macro `%s' used with only %d args",
2505                        hp->name, i);
2506       }
2507       else if (i > nargs)
2508         {
2509           cpp_error (pfile,
2510                      "macro `%s' used with too many (%d) args", hp->name, i);
2511         }
2512     }
2513
2514   /* If macro wants zero args, we parsed the arglist for checking only.
2515      Read directly from the macro definition.  */
2516   if (nargs <= 0)
2517     {
2518       xbuf = defn->expansion;
2519       xbuf_len = defn->length;
2520     }
2521   else
2522     {
2523       register U_CHAR *exp = defn->expansion;
2524       register int offset;      /* offset in expansion,
2525                                    copied a piece at a time */
2526       register int totlen;      /* total amount of exp buffer filled so far */
2527
2528       register struct reflist *ap, *last_ap;
2529
2530       /* Macro really takes args.  Compute the expansion of this call.  */
2531
2532       /* Compute length in characters of the macro's expansion.
2533          Also count number of times each arg is used.  */
2534       xbuf_len = defn->length;
2535       for (ap = defn->pattern; ap != NULL; ap = ap->next)
2536         {
2537           if (ap->stringify)
2538             {
2539               register struct argdata *arg = &args[ap->argno];
2540               /* Stringify if it hasn't already been */
2541               if (arg->stringified_length < 0)
2542                 {
2543                   int arglen = arg->raw_length;
2544                   int escaped = 0;
2545                   int in_string = 0;
2546                   int c;
2547                   /* Initially need_space is -1.  Otherwise, 1 means the
2548                      previous character was a space, but we suppressed it;
2549                      0 means the previous character was a non-space.  */
2550                   int need_space = -1;
2551                   i = 0;
2552                   arg->stringified = CPP_WRITTEN (pfile);
2553                   if (!CPP_TRADITIONAL (pfile))
2554                     CPP_PUTC (pfile, '\"'); /* insert beginning quote */
2555                   for (; i < arglen; i++)
2556                     {
2557                       c = (ARG_BASE + arg->raw)[i];
2558
2559                       if (! in_string)
2560                         {
2561                           /* Internal sequences of whitespace are replaced by
2562                              one space except within an string or char token.*/
2563                           if (is_space[c])
2564                             {
2565                               if (CPP_WRITTEN (pfile) > (unsigned)arg->stringified
2566                                   && (CPP_PWRITTEN (pfile))[-1] == '@')
2567                                 {
2568                                   /* "@ " escape markers are removed */
2569                                   CPP_ADJUST_WRITTEN (pfile, -1);
2570                                   continue;
2571                                 }
2572                               if (need_space == 0)
2573                                 need_space = 1;
2574                               continue;
2575                             }
2576                           else if (need_space > 0)
2577                             CPP_PUTC (pfile, ' ');
2578                           need_space = 0;
2579                         }
2580
2581                       if (escaped)
2582                         escaped = 0;
2583                       else
2584                         {
2585                           if (c == '\\')
2586                             escaped = 1;
2587                           if (in_string)
2588                             {
2589                               if (c == in_string)
2590                                 in_string = 0;
2591                             }
2592                           else if (c == '\"' || c == '\'')
2593                             in_string = c;
2594                         }
2595
2596                       /* Escape these chars */
2597                       if (c == '\"' || (in_string && c == '\\'))
2598                         CPP_PUTC (pfile, '\\');
2599                       if (ISPRINT (c))
2600                         CPP_PUTC (pfile, c);
2601                       else
2602                         {
2603                           CPP_RESERVE (pfile, 4);
2604                           sprintf ((char *)CPP_PWRITTEN (pfile), "\\%03o",
2605                                    (unsigned int) c);
2606                           CPP_ADJUST_WRITTEN (pfile, 4);
2607                         }
2608                     }
2609                   if (!CPP_TRADITIONAL (pfile))
2610                     CPP_PUTC (pfile, '\"'); /* insert ending quote */
2611                   arg->stringified_length
2612                     = CPP_WRITTEN (pfile) - arg->stringified;
2613                 }
2614               xbuf_len += args[ap->argno].stringified_length;
2615             }
2616           else if (ap->raw_before || ap->raw_after || CPP_TRADITIONAL (pfile))
2617             /* Add 4 for two newline-space markers to prevent
2618                token concatenation.  */
2619             xbuf_len += args[ap->argno].raw_length + 4;
2620           else
2621             {
2622               /* We have an ordinary (expanded) occurrence of the arg.
2623                  So compute its expansion, if we have not already.  */
2624               if (args[ap->argno].expand_length < 0)
2625                 {
2626                   args[ap->argno].expanded = CPP_WRITTEN (pfile);
2627                   cpp_expand_to_buffer (pfile,
2628                                         ARG_BASE + args[ap->argno].raw,
2629                                         args[ap->argno].raw_length);
2630
2631                   args[ap->argno].expand_length
2632                     = CPP_WRITTEN (pfile) - args[ap->argno].expanded;
2633                 }
2634
2635               /* Add 4 for two newline-space markers to prevent
2636                  token concatenation.  */
2637               xbuf_len += args[ap->argno].expand_length + 4;
2638             }
2639           if (args[ap->argno].use_count < 10)
2640             args[ap->argno].use_count++;
2641         }
2642
2643       xbuf = (U_CHAR *) xmalloc (xbuf_len + 1);
2644
2645       /* Generate in XBUF the complete expansion
2646          with arguments substituted in.
2647          TOTLEN is the total size generated so far.
2648          OFFSET is the index in the definition
2649          of where we are copying from.  */
2650       offset = totlen = 0;
2651       for (last_ap = NULL, ap = defn->pattern; ap != NULL;
2652            last_ap = ap, ap = ap->next)
2653         {
2654           register struct argdata *arg = &args[ap->argno];
2655           int count_before = totlen;
2656
2657           /* Add chars to XBUF.  */
2658           for (i = 0; i < ap->nchars; i++, offset++)
2659             xbuf[totlen++] = exp[offset];
2660
2661           /* If followed by an empty rest arg with concatenation,
2662              delete the last run of nonwhite chars.  */
2663           if (rest_zero && totlen > count_before
2664               && ((ap->rest_args && ap->raw_before)
2665                   || (last_ap != NULL && last_ap->rest_args
2666                       && last_ap->raw_after)))
2667             {
2668               /* Delete final whitespace.  */
2669               while (totlen > count_before && is_space[xbuf[totlen - 1]])
2670                 totlen--;
2671
2672               /* Delete the nonwhites before them.  */
2673               while (totlen > count_before && ! is_space[xbuf[totlen - 1]])
2674                 totlen--;
2675             }
2676
2677           if (ap->stringify != 0)
2678             {
2679               bcopy (ARG_BASE + arg->stringified,
2680                      xbuf + totlen, arg->stringified_length);
2681               totlen += arg->stringified_length;
2682             }
2683           else if (ap->raw_before || ap->raw_after || CPP_TRADITIONAL (pfile))
2684             {
2685               U_CHAR *p1 = ARG_BASE + arg->raw;
2686               U_CHAR *l1 = p1 + arg->raw_length;
2687               if (ap->raw_before)
2688                 {
2689                   while (p1 != l1 && is_space[*p1]) p1++;
2690                   while (p1 != l1 && is_idchar[*p1])
2691                     xbuf[totlen++] = *p1++;
2692                 }
2693               if (ap->raw_after)
2694                 {
2695                   /* Arg is concatenated after: delete trailing whitespace,
2696                      whitespace markers, and no-reexpansion markers.  */
2697                   while (p1 != l1)
2698                     {
2699                       if (is_space[l1[-1]]) l1--;
2700                       else if (l1[-1] == '@')
2701                         {
2702                           U_CHAR *p2 = l1 - 1;
2703                           /* If whitespace is preceded by an odd number
2704                              of `@' signs, the last `@' was a whitespace
2705                              marker; drop it too. */
2706                           while (p2 != p1 && p2[0] == '@') p2--;
2707                           if ((l1 - p2) & 1)
2708                             l1--;
2709                           break;
2710                         }
2711                       else if (l1[-1] == '-')
2712                         {
2713                           U_CHAR *p2 = l1 - 1;
2714                           /* If a `-' is preceded by an odd number of
2715                              `@' signs then it and the last `@' are
2716                              a no-reexpansion marker.  */
2717                           while (p2 != p1 && p2[0] == '@') p2--;
2718                           if ((l1 - p2) & 1)
2719                             l1 -= 2;
2720                           else
2721                             break;
2722                         }
2723                       else break;
2724                     }
2725                 }
2726
2727               /* Delete any no-reexpansion marker that precedes
2728                  an identifier at the beginning of the argument. */
2729               if (p1[0] == '@' && p1[1] == '-')
2730                 p1 += 2;
2731
2732               bcopy (p1, xbuf + totlen, l1 - p1);
2733               totlen += l1 - p1;
2734             }
2735           else
2736             {
2737               U_CHAR *expanded = ARG_BASE + arg->expanded;
2738               if (!ap->raw_before && totlen > 0 && arg->expand_length
2739                   && !CPP_TRADITIONAL(pfile)
2740                   && unsafe_chars (xbuf[totlen-1], expanded[0]))
2741                 {
2742                   xbuf[totlen++] = '@';
2743                   xbuf[totlen++] = ' ';
2744                 }
2745
2746               bcopy (expanded, xbuf + totlen, arg->expand_length);
2747               totlen += arg->expand_length;
2748
2749               if (!ap->raw_after && totlen > 0 && offset < defn->length
2750                   && !CPP_TRADITIONAL(pfile)
2751                   && unsafe_chars (xbuf[totlen-1], exp[offset]))
2752                 {
2753                   xbuf[totlen++] = '@';
2754                   xbuf[totlen++] = ' ';
2755                 }
2756
2757               /* If a macro argument with newlines is used multiple times,
2758                  then only expand the newlines once.  This avoids creating
2759                  output lines which don't correspond to any input line,
2760                  which confuses gdb and gcov.  */
2761               if (arg->use_count > 1 && arg->newlines > 0)
2762                 {
2763                   /* Don't bother doing change_newlines for subsequent
2764                      uses of arg.  */
2765                   arg->use_count = 1;
2766                   arg->expand_length
2767                     = change_newlines (expanded, arg->expand_length);
2768                 }
2769             }
2770
2771           if (totlen > xbuf_len)
2772             abort ();
2773       }
2774
2775       /* if there is anything left of the definition
2776          after handling the arg list, copy that in too.  */
2777
2778       for (i = offset; i < defn->length; i++)
2779         {
2780           /* if we've reached the end of the macro */
2781           if (exp[i] == ')')
2782             rest_zero = 0;
2783           if (! (rest_zero && last_ap != NULL && last_ap->rest_args
2784                  && last_ap->raw_after))
2785             xbuf[totlen++] = exp[i];
2786         }
2787
2788       xbuf[totlen] = 0;
2789       xbuf_len = totlen;
2790
2791     }
2792
2793   pfile->output_escapes--;
2794
2795   /* Now put the expansion on the input stack
2796      so our caller will commence reading from it.  */
2797   push_macro_expansion (pfile, xbuf, xbuf_len, hp);
2798   CPP_BUFFER (pfile)->has_escapes = 1;
2799
2800   /* Pop the space we've used in the token_buffer for argument expansion.  */
2801   CPP_SET_WRITTEN (pfile, old_written);
2802     
2803   /* Recursive macro use sometimes works traditionally.
2804      #define foo(x,y) bar (x (y,0), y)
2805      foo (foo, baz)  */
2806   
2807   if (!CPP_TRADITIONAL (pfile))
2808     hp->type = T_DISABLED;
2809 }
2810
2811 static void
2812 push_macro_expansion (pfile, xbuf, xbuf_len, hp)
2813      cpp_reader *pfile;
2814      register U_CHAR *xbuf;
2815      int xbuf_len;
2816      HASHNODE *hp;
2817 {
2818   register cpp_buffer *mbuf = cpp_push_buffer (pfile, xbuf, xbuf_len);
2819   if (mbuf == NULL)
2820     return;
2821   mbuf->cleanup = macro_cleanup;
2822   mbuf->data = hp;
2823
2824   /* The first chars of the expansion should be a "@ " added by
2825      collect_expansion.  This is to prevent accidental token-pasting
2826      between the text preceding the macro invocation, and the macro
2827      expansion text.
2828
2829      We would like to avoid adding unneeded spaces (for the sake of
2830      tools that use cpp, such as imake).  In some common cases we can
2831      tell that it is safe to omit the space.
2832
2833      The character before the macro invocation cannot have been an
2834      idchar (or else it would have been pasted with the idchars of
2835      the macro name).  Therefore, if the first non-space character
2836      of the expansion is an idchar, we do not need the extra space
2837      to prevent token pasting.
2838
2839      Also, we don't need the extra space if the first char is '(',
2840      or some other (less common) characters.  */
2841
2842   if (xbuf[0] == '@' && xbuf[1] == ' '
2843       && (is_idchar[xbuf[2]] || xbuf[2] == '(' || xbuf[2] == '\''
2844           || xbuf[2] == '\"'))
2845     mbuf->cur += 2;
2846
2847   /* Likewise, avoid the extra space at the end of the macro expansion
2848      if this is safe.  (We can do a better job here since we can know
2849      what the next char will be.) */
2850   if (xbuf_len >= 3
2851       && mbuf->rlimit[-2] == '@'
2852       && mbuf->rlimit[-1] == ' ')
2853     {
2854       int c1 = mbuf->rlimit[-3];
2855       int c2 = CPP_BUF_PEEK (CPP_PREV_BUFFER (CPP_BUFFER (pfile)));
2856       if (c2 == EOF || ! unsafe_chars (c1, c2))
2857         mbuf->rlimit -= 2;
2858     }
2859 }
2860 \f
2861 /* Like cpp_get_token, except that it does not read past end-of-line.
2862    Also, horizontal space is skipped, and macros are popped.  */
2863
2864 static enum cpp_token
2865 get_directive_token (pfile)
2866      cpp_reader *pfile;
2867 {
2868   for (;;)
2869     {
2870       long old_written = CPP_WRITTEN (pfile);
2871       enum cpp_token token;
2872       cpp_skip_hspace (pfile);
2873       if (PEEKC () == '\n')
2874           return CPP_VSPACE;
2875       token = cpp_get_token (pfile);
2876       switch (token)
2877       {
2878       case CPP_POP:
2879           if (! CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
2880               return token;
2881           /* ... else fall though ...  */
2882       case CPP_HSPACE:  case CPP_COMMENT:
2883           CPP_SET_WRITTEN (pfile, old_written);
2884           break;
2885       default:
2886           return token;
2887       }
2888     }
2889 }
2890 \f
2891 /* Handle #include and #import.
2892    This function expects to see "fname" or <fname> on the input.
2893
2894    The input is normally in part of the output_buffer following
2895    CPP_WRITTEN, and will get overwritten by output_line_command.
2896    I.e. in input file specification has been popped by handle_directive.
2897    This is safe.  */
2898
2899 static int
2900 do_include (pfile, keyword)
2901      cpp_reader *pfile;
2902      struct directive *keyword;
2903 {
2904   int importing = (keyword->type == T_IMPORT);
2905   int skip_dirs = (keyword->type == T_INCLUDE_NEXT);
2906   int angle_brackets = 0;       /* 0 for "...", 1 for <...> */
2907   int before;  /* included before? */
2908   long flen;
2909   char *fbeg, *fend;
2910   cpp_buffer *fp;
2911
2912   enum cpp_token token;
2913
2914   /* Chain of dirs to search */
2915   struct include_hash *ihash;
2916   struct file_name_list *search_start;
2917   
2918   long old_written = CPP_WRITTEN (pfile);
2919
2920   int fd;
2921
2922   if (CPP_PEDANTIC (pfile) && !CPP_BUFFER (pfile)->system_header_p)
2923     {
2924       if (importing)
2925         cpp_pedwarn (pfile, "ANSI C does not allow `#import'");
2926       if (skip_dirs)
2927         cpp_pedwarn (pfile, "ANSI C does not allow `#include_next'");
2928     }
2929
2930   if (importing && CPP_OPTIONS (pfile)->warn_import
2931       && !CPP_OPTIONS (pfile)->inhibit_warnings
2932       && !CPP_BUFFER (pfile)->system_header_p && !pfile->import_warning)
2933     {
2934       pfile->import_warning = 1;
2935       cpp_warning (pfile, "using `#import' is not recommended");
2936       cpp_notice ("The fact that a certain header file need not be processed more than once\n\
2937 should be indicated in the header file, not where it is used.\n\
2938 The best way to do this is with a conditional of this form:\n\
2939 \n\
2940   #ifndef _FOO_H_INCLUDED\n\
2941   #define _FOO_H_INCLUDED\n\
2942   ... <real contents of file> ...\n\
2943   #endif /* Not _FOO_H_INCLUDED */\n\
2944 \n\
2945 Then users can use `#include' any number of times.\n\
2946 GNU C automatically avoids processing the file more than once\n\
2947 when it is equipped with such a conditional.\n");
2948     }
2949
2950   pfile->parsing_include_directive++;
2951   token = get_directive_token (pfile);
2952   pfile->parsing_include_directive--;
2953
2954   if (token == CPP_STRING)
2955     {
2956       fbeg = pfile->token_buffer + old_written + 1;
2957       fend = CPP_PWRITTEN (pfile) - 1;
2958       *fend = '\0';
2959       if (fbeg[-1] == '<')
2960           angle_brackets = 1;
2961     }
2962 #ifdef VMS
2963   else if (token == CPP_NAME)
2964     {
2965       /* Support '#include xyz' like VAX-C to allow for easy use of
2966        * all the decwindow include files. It defaults to '#include
2967        * <xyz.h>' and generates a warning.  */
2968       cpp_warning (pfile,
2969                    "VAX-C-style include specification found, use '#include <filename.h>' !");
2970       angle_brackets = 1;
2971
2972       /* Append the missing `.h' to the name. */
2973       CPP_PUTS (pfile, ".h", 3)
2974       CPP_NUL_TERMINATE_Q (pfile);
2975
2976       fbeg = pfile->token_buffer + old_written;
2977       fend = CPP_PWRITTEN (pfile);
2978     }
2979 #endif
2980   else
2981     {
2982       cpp_error (pfile,
2983                  "`#%s' expects \"FILENAME\" or <FILENAME>", keyword->name);
2984       CPP_SET_WRITTEN (pfile, old_written);
2985       skip_rest_of_line (pfile);
2986       return 0;
2987     }
2988
2989   token = get_directive_token (pfile);
2990   if (token != CPP_VSPACE)
2991     {
2992       cpp_error (pfile, "junk at end of `#include'");
2993       skip_rest_of_line (pfile);
2994     }
2995
2996   CPP_SET_WRITTEN (pfile, old_written);
2997
2998   flen = fend - fbeg;
2999
3000   if (flen == 0)
3001     {
3002       cpp_error (pfile, "empty file name in `#%s'", keyword->name);
3003       return 0;
3004     }
3005
3006   search_start = 0;
3007
3008   for (fp = CPP_BUFFER (pfile);
3009        fp != CPP_NULL_BUFFER (pfile);
3010        fp = CPP_PREV_BUFFER (fp))
3011     if (fp->fname != NULL)
3012       break;
3013
3014   if (fp == CPP_NULL_BUFFER (pfile))
3015     {
3016       cpp_fatal (pfile, "cpp internal error: fp == NULL_BUFFER in do_include");
3017       return 1;
3018     }
3019   
3020   /* For #include_next, skip in the search path past the dir in which the
3021      containing file was found.  Treat files specified using an absolute path
3022      as if there are no more directories to search.  Treat the primary source
3023      file like any other included source, but generate a warning.  */
3024   if (skip_dirs && CPP_PREV_BUFFER(fp) != CPP_NULL_BUFFER (pfile))
3025     {
3026       if (fp->ihash->foundhere != ABSOLUTE_PATH)
3027         search_start = fp->ihash->foundhere->next;
3028     }
3029   else
3030     {
3031       if (skip_dirs)
3032         cpp_warning (pfile, "#include_next in primary source file");
3033       
3034       if (angle_brackets)
3035         search_start = CPP_OPTIONS (pfile)->bracket_include;
3036       else
3037         {
3038           if (!CPP_OPTIONS (pfile)->ignore_srcdir)
3039             {
3040               if (fp)
3041                 search_start = fp->actual_dir;
3042             }
3043           else
3044             search_start = CPP_OPTIONS (pfile)->quote_include;
3045         }
3046     }
3047
3048   if (!search_start)
3049     {
3050       cpp_error (pfile, "No include path in which to find %s", fbeg);
3051       return 0;
3052     }
3053
3054   fd = find_include_file (pfile, fbeg, search_start, &ihash, &before);
3055
3056   if (fd == -2)
3057     return 0;
3058   
3059   if (fd == -1)
3060     {
3061       if (CPP_OPTIONS (pfile)->print_deps_missing_files
3062           && CPP_PRINT_DEPS (pfile) > (angle_brackets ||
3063                                        (pfile->system_include_depth > 0)))
3064         {
3065           if (!angle_brackets)
3066             deps_output (pfile, fbeg, ' ');
3067           else
3068             {
3069               char *p;
3070               struct file_name_list *ptr;
3071               /* If requested as a system header, assume it belongs in
3072                  the first system header directory. */
3073               if (CPP_OPTIONS (pfile)->bracket_include)
3074                 ptr = CPP_OPTIONS (pfile)->bracket_include;
3075               else
3076                 ptr = CPP_OPTIONS (pfile)->quote_include;
3077
3078               p = (char *) alloca (strlen (ptr->name)
3079                                    + strlen (fbeg) + 2);
3080               if (*ptr->name != '\0')
3081                 {
3082                   strcpy (p, ptr->name);
3083                   strcat (p, "/");
3084                 }
3085               strcat (p, fbeg);
3086               deps_output (pfile, p, ' ');
3087             }
3088         }
3089       /* If -M was specified, and this header file won't be added to
3090          the dependency list, then don't count this as an error,
3091          because we can still produce correct output.  Otherwise, we
3092          can't produce correct output, because there may be
3093          dependencies we need inside the missing file, and we don't
3094          know what directory this missing file exists in. */
3095       else if (CPP_PRINT_DEPS (pfile)
3096                && (CPP_PRINT_DEPS (pfile)
3097                    <= (angle_brackets || (pfile->system_include_depth > 0))))
3098         cpp_warning (pfile, "No include path in which to find %s", fbeg);
3099       else
3100         cpp_error_from_errno (pfile, fbeg);
3101
3102       return 0;
3103     }
3104
3105   /* For -M, add the file to the dependencies on its first inclusion. */
3106   if (!before && (CPP_PRINT_DEPS (pfile)
3107                   > (angle_brackets || (pfile->system_include_depth > 0))))
3108     deps_output (pfile, ihash->name, ' ');
3109
3110   /* Handle -H option.  */
3111   if (CPP_OPTIONS(pfile)->print_include_names)
3112     {
3113       fp = CPP_BUFFER (pfile);
3114       while ((fp = CPP_PREV_BUFFER (fp)) != CPP_NULL_BUFFER (pfile))
3115         putc ('.', stderr);
3116       fprintf (stderr, " %s\n", ihash->name);
3117     }
3118
3119   /* Actually process the file */
3120
3121   if (importing)
3122     ihash->control_macro = "";
3123   
3124   if (cpp_push_buffer (pfile, NULL, 0) == NULL)
3125     {
3126       close (fd);
3127       return 0;
3128     }
3129   
3130   if (angle_brackets)
3131     pfile->system_include_depth++;   /* Decremented in file_cleanup. */
3132
3133   if (finclude (pfile, fd, ihash))
3134     {
3135       output_line_command (pfile, 0, enter_file);
3136       pfile->only_seen_white = 2;
3137     }
3138
3139   return 0;
3140 }
3141
3142 \f
3143 /* Convert a character string literal into a nul-terminated string.
3144    The input string is [IN ... LIMIT).
3145    The result is placed in RESULT.  RESULT can be the same as IN.
3146    The value returned in the end of the string written to RESULT,
3147    or NULL on error.  */
3148
3149 static U_CHAR *
3150 convert_string (pfile, result, in, limit, handle_escapes)
3151      cpp_reader *pfile;
3152      register U_CHAR *result, *in, *limit;
3153      int handle_escapes;
3154 {
3155   U_CHAR c;
3156   c = *in++;
3157   if (c != '\"')
3158     return NULL;
3159   while (in < limit)
3160     {
3161       U_CHAR c = *in++;
3162       switch (c)
3163         {
3164         case '\0':
3165           return NULL;
3166         case '\"':
3167           limit = in;
3168           break;
3169         case '\\':
3170           if (handle_escapes)
3171             {
3172               char *bpc = (char *) in;
3173               int i = (U_CHAR) cpp_parse_escape (pfile, &bpc, 0x00ff);
3174               in = (U_CHAR *) bpc;
3175               if (i >= 0)
3176                 *result++ = (U_CHAR)c;
3177               break;
3178             }
3179           /* else fall through */
3180         default:
3181           *result++ = c;
3182         }
3183     }
3184   *result = 0;
3185   return result;
3186 }
3187
3188 /*
3189  * interpret #line command.  Remembers previously seen fnames
3190  * in its very own hash table.
3191  */
3192 #define FNAME_HASHSIZE 37
3193
3194 static int
3195 do_line (pfile, keyword)
3196      cpp_reader *pfile;
3197      struct directive *keyword ATTRIBUTE_UNUSED;
3198 {
3199   cpp_buffer *ip = CPP_BUFFER (pfile);
3200   int new_lineno;
3201   long old_written = CPP_WRITTEN (pfile);
3202   enum file_change_code file_change = same_file;
3203   enum cpp_token token;
3204
3205   token = get_directive_token (pfile);
3206
3207   if (token != CPP_NUMBER
3208       || !ISDIGIT(pfile->token_buffer[old_written]))
3209     {
3210       cpp_error (pfile, "invalid format `#line' command");
3211       goto bad_line_directive;
3212     }
3213
3214   /* The Newline at the end of this line remains to be processed.
3215      To put the next line at the specified line number,
3216      we must store a line number now that is one less.  */
3217   new_lineno = atoi ((char *)(pfile->token_buffer + old_written)) - 1;
3218   CPP_SET_WRITTEN (pfile, old_written);
3219
3220   /* NEW_LINENO is one less than the actual line number here.  */
3221   if (CPP_PEDANTIC (pfile) && new_lineno < 0)
3222     cpp_pedwarn (pfile, "line number out of range in `#line' command");
3223
3224 #if 0 /* #line 10"foo.c" is supposed to be allowed.  */
3225   if (PEEKC() && !is_space[PEEKC()]) {
3226     cpp_error (pfile, "invalid format `#line' command");
3227     goto bad_line_directive;
3228   }
3229 #endif
3230
3231   token = get_directive_token (pfile);
3232
3233   if (token == CPP_STRING) {
3234     U_CHAR *fname = pfile->token_buffer + old_written;
3235     U_CHAR *end_name;
3236     static HASHNODE *fname_table[FNAME_HASHSIZE];
3237     HASHNODE *hp, **hash_bucket;
3238     U_CHAR *p;
3239     long num_start;
3240     int fname_length;
3241
3242     /* Turn the file name, which is a character string literal,
3243        into a null-terminated string.  Do this in place.  */
3244     end_name = convert_string (pfile, fname, fname, CPP_PWRITTEN (pfile), 1);
3245     if (end_name == NULL)
3246     {
3247         cpp_error (pfile, "invalid format `#line' command");
3248         goto bad_line_directive;
3249     }
3250
3251     fname_length = end_name - fname;
3252
3253     num_start = CPP_WRITTEN (pfile);
3254     token = get_directive_token (pfile);
3255     if (token != CPP_VSPACE && token != CPP_EOF && token != CPP_POP) {
3256       p = pfile->token_buffer + num_start;
3257       if (CPP_PEDANTIC (pfile))
3258         cpp_pedwarn (pfile, "garbage at end of `#line' command");
3259
3260       if (token != CPP_NUMBER || *p < '0' || *p > '4' || p[1] != '\0')
3261       {
3262         cpp_error (pfile, "invalid format `#line' command");
3263         goto bad_line_directive;
3264       }
3265       if (*p == '1')
3266         file_change = enter_file;
3267       else if (*p == '2')
3268         file_change = leave_file;
3269       else if (*p == '3')
3270         ip->system_header_p = 1;
3271       else /* if (*p == '4') */
3272         ip->system_header_p = 2;
3273
3274       CPP_SET_WRITTEN (pfile, num_start);
3275       token = get_directive_token (pfile);
3276       p = pfile->token_buffer + num_start;
3277       if (token == CPP_NUMBER && p[1] == '\0' && (*p == '3' || *p== '4')) {
3278         ip->system_header_p = *p == '3' ? 1 : 2;
3279         token = get_directive_token (pfile);
3280       }
3281       if (token != CPP_VSPACE) {
3282         cpp_error (pfile, "invalid format `#line' command");
3283         goto bad_line_directive;
3284       }
3285     }
3286
3287     hash_bucket = &fname_table[hashf (fname, fname_length, FNAME_HASHSIZE)];
3288     for (hp = *hash_bucket; hp != NULL; hp = hp->next)
3289       if (hp->length == fname_length
3290           && strncmp (hp->value.cpval, fname, fname_length) == 0) {
3291         ip->nominal_fname = hp->value.cpval;
3292         break;
3293       }
3294     if (hp == 0) {
3295       /* Didn't find it; cons up a new one.  */
3296       hp = (HASHNODE *) xcalloc (1, sizeof (HASHNODE) + fname_length + 1);
3297       hp->next = *hash_bucket;
3298       *hash_bucket = hp;
3299
3300       hp->length = fname_length;
3301       ip->nominal_fname = hp->value.cpval = ((char *) hp) + sizeof (HASHNODE);
3302       bcopy (fname, hp->value.cpval, fname_length);
3303     }
3304   }
3305   else if (token != CPP_VSPACE && token != CPP_EOF) {
3306     cpp_error (pfile, "invalid format `#line' command");
3307     goto bad_line_directive;
3308   }
3309
3310   ip->lineno = new_lineno;
3311  bad_line_directive:
3312   skip_rest_of_line (pfile);
3313   CPP_SET_WRITTEN (pfile, old_written);
3314   output_line_command (pfile, 0, file_change);
3315   return 0;
3316 }
3317
3318 /*
3319  * remove the definition of a symbol from the symbol table.
3320  * according to un*x /lib/cpp, it is not an error to undef
3321  * something that has no definitions, so it isn't one here either.
3322  */
3323
3324 static int
3325 do_undef (pfile, keyword)
3326      cpp_reader *pfile;
3327      struct directive *keyword;
3328 {
3329   int sym_length;
3330   HASHNODE *hp;
3331   U_CHAR *buf, *name, *limit;
3332   int c;
3333   long here = CPP_WRITTEN (pfile);
3334   enum cpp_token token;
3335
3336   cpp_skip_hspace (pfile);
3337   c = GETC();
3338   if (! is_idstart[c])
3339   {
3340       cpp_error (pfile, "token after #undef is not an identifier");
3341       skip_rest_of_line (pfile);
3342       return 1;
3343   }
3344
3345   parse_name (pfile, c);
3346   buf = pfile->token_buffer + here;
3347   limit = CPP_PWRITTEN(pfile);
3348
3349   /* Copy out the token so we can pop the token buffer. */
3350   name = alloca (limit - buf + 1);
3351   bcopy(buf, name, limit - buf);
3352   name[limit - buf] = '\0';
3353
3354   token = get_directive_token (pfile);
3355   if (token != CPP_VSPACE && token != CPP_POP)
3356   {
3357       cpp_pedwarn (pfile, "junk on line after #undef");
3358       skip_rest_of_line (pfile);
3359   }
3360
3361   CPP_SET_WRITTEN (pfile, here);
3362
3363 #if 0
3364   /* If this is a precompiler run (with -pcp) pass thru #undef commands.  */
3365   if (pcp_outfile && keyword)
3366     pass_thru_directive (buf, limit, pfile, keyword);
3367 #endif
3368
3369   sym_length = check_macro_name (pfile, buf, 0);
3370
3371   while ((hp = cpp_lookup (pfile, name, sym_length, -1)) != NULL)
3372     {
3373       /* If we are generating additional info for debugging (with -g) we
3374          need to pass through all effective #undef commands.  */
3375       if (CPP_OPTIONS (pfile)->debug_output && keyword)
3376         pass_thru_directive (name, name+sym_length, pfile, keyword);
3377       if (hp->type != T_MACRO)
3378         cpp_warning (pfile, "undefining `%s'", hp->name);
3379       delete_macro (hp);
3380     }
3381
3382   return 0;
3383 }
3384
3385 /* Wrap do_undef for -U processing. */
3386 static void
3387 cpp_undef (pfile, macro)
3388      cpp_reader *pfile;
3389      U_CHAR *macro;
3390 {
3391     if (cpp_push_buffer (pfile, macro, strlen(macro)))
3392     {
3393         do_undef (pfile, NULL);
3394         cpp_pop_buffer (pfile);
3395     }
3396 }
3397
3398 \f
3399 /*
3400  * Report an error detected by the program we are processing.
3401  * Use the text of the line in the error message.
3402  * (We use error because it prints the filename & line#.)
3403  */
3404
3405 static int
3406 do_error (pfile, keyword)
3407      cpp_reader *pfile;
3408      struct directive *keyword ATTRIBUTE_UNUSED;
3409 {
3410   long here = CPP_WRITTEN (pfile);
3411   U_CHAR *text;
3412   copy_rest_of_line (pfile);
3413   text = pfile->token_buffer + here;
3414   SKIP_WHITE_SPACE(text);
3415
3416   cpp_error (pfile, "#error %s", text);
3417   CPP_SET_WRITTEN (pfile, here);
3418   return 0;
3419 }
3420
3421 /*
3422  * Report a warning detected by the program we are processing.
3423  * Use the text of the line in the warning message, then continue.
3424  */
3425
3426 static int
3427 do_warning (pfile, keyword)
3428      cpp_reader *pfile;
3429      struct directive *keyword ATTRIBUTE_UNUSED;
3430 {
3431   U_CHAR *text;
3432   long here = CPP_WRITTEN(pfile);
3433   copy_rest_of_line (pfile);
3434   text = pfile->token_buffer + here;
3435   SKIP_WHITE_SPACE(text);
3436
3437   if (CPP_PEDANTIC (pfile) && !CPP_BUFFER (pfile)->system_header_p)
3438     cpp_pedwarn (pfile, "ANSI C does not allow `#warning'");
3439
3440   /* Use `pedwarn' not `warning', because #warning isn't in the C Standard;
3441      if -pedantic-errors is given, #warning should cause an error.  */
3442   cpp_pedwarn (pfile, "#warning %s", text);
3443   CPP_SET_WRITTEN (pfile, here);
3444   return 0;
3445 }
3446
3447 /* Report program identification.  */
3448
3449 static int
3450 do_ident (pfile, keyword)
3451      cpp_reader *pfile;
3452      struct directive *keyword ATTRIBUTE_UNUSED;
3453 {
3454   /* Allow #ident in system headers, since that's not user's fault.  */
3455   if (CPP_PEDANTIC (pfile) && !CPP_BUFFER (pfile)->system_header_p)
3456     cpp_pedwarn (pfile, "ANSI C does not allow `#ident'");
3457
3458   skip_rest_of_line (pfile);  /* Correct?  Appears to match cccp.  */
3459
3460   return 0;
3461 }
3462
3463 /* Just check for some recognized pragmas that need validation here,
3464    and leave the text in the token buffer to be output. */
3465
3466 static int
3467 do_pragma (pfile, keyword)
3468      cpp_reader *pfile;
3469      struct directive *keyword ATTRIBUTE_UNUSED;
3470 {
3471   long here = CPP_WRITTEN (pfile);
3472   U_CHAR *buf;
3473   
3474   copy_rest_of_line (pfile);
3475   buf = pfile->token_buffer + here;
3476   SKIP_WHITE_SPACE (buf);
3477   
3478   if (!strncmp (buf, "once", 4))
3479     {
3480       cpp_buffer *ip = NULL;
3481
3482       /* Allow #pragma once in system headers, since that's not the user's
3483          fault.  */
3484       if (!CPP_BUFFER (pfile)->system_header_p)
3485         cpp_warning (pfile, "`#pragma once' is obsolete");
3486       
3487       for (ip = CPP_BUFFER (pfile); ; ip = CPP_PREV_BUFFER (ip))
3488         {
3489           if (ip == CPP_NULL_BUFFER (pfile))
3490             return 0;
3491           if (ip->fname != NULL)
3492             break;
3493         }
3494
3495       if (CPP_PREV_BUFFER (ip) == CPP_NULL_BUFFER (pfile))
3496         cpp_warning (pfile, "`#pragma once' outside include file");
3497       else
3498         ip->ihash->control_macro = "";  /* never repeat */
3499     }
3500
3501   if (!strncmp (buf, "implementation", 14))
3502     {
3503       /* Be quiet about `#pragma implementation' for a file only if it hasn't
3504          been included yet.  */
3505       struct include_hash *ptr;
3506       U_CHAR *p = buf + 14, *fname, *fcopy;
3507       SKIP_WHITE_SPACE (p);
3508       if (*p == '\n' || *p != '\"')
3509         return 0;
3510
3511       fname = p + 1;
3512       p = (U_CHAR *) index (fname, '\"');
3513
3514       fcopy = alloca (p - fname + 1);
3515       bcopy (fname, fcopy, p - fname);
3516       fcopy[p-fname] = '\0';
3517
3518       ptr = include_hash (pfile, fcopy, 0);
3519       if (ptr)
3520         cpp_warning (pfile,
3521           "`#pragma implementation' for `%s' appears after file is included",
3522                      fcopy);
3523     }
3524
3525   return 0;
3526 }
3527
3528 #ifdef SCCS_DIRECTIVE
3529 /* Just ignore #sccs, on systems where we define it at all.  */
3530
3531 static int
3532 do_sccs (pfile, keyword)
3533      cpp_reader *pfile;
3534      struct directive *keyword ATTRIBUTE_UNUSED;
3535 {
3536   if (CPP_PEDANTIC (pfile))
3537     cpp_pedwarn (pfile, "ANSI C does not allow `#sccs'");
3538   skip_rest_of_line (pfile);
3539   return 0;
3540 }
3541 #endif
3542 \f
3543 /*
3544  * handle #if command by
3545  *   1) inserting special `defined' keyword into the hash table
3546  *      that gets turned into 0 or 1 by special_symbol (thus,
3547  *      if the luser has a symbol called `defined' already, it won't
3548  *      work inside the #if command)
3549  *   2) rescan the input into a temporary output buffer
3550  *   3) pass the output buffer to the yacc parser and collect a value
3551  *   4) clean up the mess left from steps 1 and 2.
3552  *   5) call conditional_skip to skip til the next #endif (etc.),
3553  *      or not, depending on the value from step 3.
3554  */
3555
3556 static int
3557 do_if (pfile, keyword)
3558      cpp_reader *pfile;
3559      struct directive *keyword ATTRIBUTE_UNUSED;
3560 {
3561   HOST_WIDE_INT value = eval_if_expression (pfile);
3562   conditional_skip (pfile, value == 0, T_IF, NULL_PTR);
3563   return 0;
3564 }
3565
3566 /*
3567  * handle a #elif directive by not changing  if_stack  either.
3568  * see the comment above do_else.
3569  */
3570
3571 static int
3572 do_elif (pfile, keyword)
3573      cpp_reader *pfile;
3574      struct directive *keyword ATTRIBUTE_UNUSED;
3575 {
3576   if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack) {
3577     cpp_error (pfile, "`#elif' not within a conditional");
3578     return 0;
3579   } else {
3580     if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF) {
3581       cpp_error (pfile, "`#elif' after `#else'");
3582 #if 0
3583       fprintf (stderr, " (matches line %d", pfile->if_stack->lineno);
3584 #endif
3585       if (pfile->if_stack->fname != NULL && CPP_BUFFER (pfile)->fname != NULL
3586           && strcmp (pfile->if_stack->fname,
3587                      CPP_BUFFER (pfile)->nominal_fname) != 0)
3588         fprintf (stderr, ", file %s", pfile->if_stack->fname);
3589       fprintf (stderr, ")\n");
3590     }
3591     pfile->if_stack->type = T_ELIF;
3592   }
3593
3594   if (pfile->if_stack->if_succeeded)
3595     skip_if_group (pfile);
3596   else {
3597     HOST_WIDE_INT value = eval_if_expression (pfile);
3598     if (value == 0)
3599       skip_if_group (pfile);
3600     else {
3601       ++pfile->if_stack->if_succeeded;  /* continue processing input */
3602       output_line_command (pfile, 1, same_file);
3603     }
3604   }
3605   return 0;
3606 }
3607
3608 /*
3609  * evaluate a #if expression in BUF, of length LENGTH,
3610  * then parse the result as a C expression and return the value as an int.
3611  */
3612
3613 static HOST_WIDE_INT
3614 eval_if_expression (pfile)
3615      cpp_reader *pfile;
3616 {
3617   HASHNODE *save_defined;
3618   HOST_WIDE_INT value;
3619   long old_written = CPP_WRITTEN (pfile);
3620
3621   save_defined = install ((U_CHAR *)"defined", -1, T_SPEC_DEFINED, 0, 0, -1);
3622   pfile->pcp_inside_if = 1;
3623
3624   value = cpp_parse_expr (pfile);
3625   pfile->pcp_inside_if = 0;
3626   delete_macro (save_defined);  /* clean up special symbol */
3627
3628   CPP_SET_WRITTEN (pfile, old_written); /* Pop */
3629
3630   return value;
3631 }
3632
3633 /*
3634  * routine to handle ifdef/ifndef.  Try to look up the symbol,
3635  * then do or don't skip to the #endif/#else/#elif depending
3636  * on what directive is actually being processed.
3637  */
3638
3639 static int
3640 do_xifdef (pfile, keyword)
3641      cpp_reader *pfile;
3642      struct directive *keyword;
3643 {
3644   int skip;
3645   cpp_buffer *ip = CPP_BUFFER (pfile);
3646   U_CHAR *ident;
3647   int ident_length;
3648   enum cpp_token token;
3649   int start_of_file = 0;
3650   U_CHAR *control_macro = 0;
3651   int old_written = CPP_WRITTEN (pfile);
3652
3653   /* Detect a #ifndef at start of file (not counting comments).  */
3654   if (ip->fname != 0 && keyword->type == T_IFNDEF)
3655     start_of_file = pfile->only_seen_white == 2;
3656
3657   pfile->no_macro_expand++;
3658   token = get_directive_token (pfile);
3659   pfile->no_macro_expand--;
3660
3661   ident = pfile->token_buffer + old_written;
3662   ident_length = CPP_WRITTEN (pfile) - old_written;
3663   CPP_SET_WRITTEN (pfile, old_written); /* Pop */
3664
3665   if (token == CPP_VSPACE || token == CPP_POP || token == CPP_EOF)
3666     {
3667       skip = (keyword->type == T_IFDEF);
3668       if (! CPP_TRADITIONAL (pfile))
3669         cpp_pedwarn (pfile, "`#%s' with no argument", keyword->name);
3670     }
3671   else if (token == CPP_NAME)
3672     {
3673       HASHNODE *hp = cpp_lookup (pfile, ident, ident_length, -1);
3674       skip = (hp == NULL) ^ (keyword->type == T_IFNDEF);
3675       if (start_of_file && !skip)
3676         {
3677           control_macro = (U_CHAR *) xmalloc (ident_length + 1);
3678           bcopy (ident, control_macro, ident_length + 1);
3679         }
3680     }
3681   else
3682     {
3683       skip = (keyword->type == T_IFDEF);
3684       if (! CPP_TRADITIONAL (pfile))
3685         cpp_error (pfile, "`#%s' with invalid argument", keyword->name);
3686     }
3687
3688   if (!CPP_TRADITIONAL (pfile))
3689     { int c;
3690       cpp_skip_hspace (pfile);
3691       c = PEEKC ();
3692       if (c != EOF && c != '\n')
3693         cpp_pedwarn (pfile, "garbage at end of `#%s' argument", keyword->name);
3694     }
3695   skip_rest_of_line (pfile);
3696
3697 #if 0
3698     if (pcp_outfile) {
3699       /* Output a precondition for this macro.  */
3700       if (hp && hp->value.defn->predefined)
3701         fprintf (pcp_outfile, "#define %s\n", hp->name);
3702       else {
3703         U_CHAR *cp = buf;
3704         fprintf (pcp_outfile, "#undef ");
3705         while (is_idchar[*cp]) /* Ick! */
3706           fputc (*cp++, pcp_outfile);
3707         putc ('\n', pcp_outfile);
3708       }
3709 #endif
3710
3711   conditional_skip (pfile, skip, T_IF, control_macro);
3712   return 0;
3713 }
3714
3715 /* Push TYPE on stack; then, if SKIP is nonzero, skip ahead.
3716    If this is a #ifndef starting at the beginning of a file,
3717    CONTROL_MACRO is the macro name tested by the #ifndef.
3718    Otherwise, CONTROL_MACRO is 0.  */
3719
3720 static void
3721 conditional_skip (pfile, skip, type, control_macro)
3722      cpp_reader *pfile;
3723      int skip;
3724      enum node_type type;
3725      U_CHAR *control_macro;
3726 {
3727   IF_STACK_FRAME *temp;
3728
3729   temp = (IF_STACK_FRAME *) xcalloc (1, sizeof (IF_STACK_FRAME));
3730   temp->fname = CPP_BUFFER (pfile)->nominal_fname;
3731 #if 0
3732   temp->lineno = CPP_BUFFER (pfile)->lineno;
3733 #endif
3734   temp->next = pfile->if_stack;
3735   temp->control_macro = control_macro;
3736   pfile->if_stack = temp;
3737
3738   pfile->if_stack->type = type;
3739
3740   if (skip != 0) {
3741     skip_if_group (pfile);
3742     return;
3743   } else {
3744     ++pfile->if_stack->if_succeeded;
3745     output_line_command (pfile, 1, same_file);
3746   }
3747 }
3748
3749 /* Subroutine of skip_if_group.  Examine one preprocessing directive and
3750    return 0 if skipping should continue, 1 if it should halt.  Also
3751    adjusts the if_stack as appropriate.
3752    The `#' has been read, but not the identifier. */
3753
3754 static int
3755 consider_directive_while_skipping (pfile, stack)
3756     cpp_reader *pfile;
3757     IF_STACK_FRAME *stack; 
3758 {
3759   long ident_len, ident;
3760   struct directive *kt;
3761   IF_STACK_FRAME *temp;
3762     
3763   cpp_skip_hspace (pfile);
3764
3765   ident = CPP_WRITTEN (pfile);
3766   parse_name (pfile, GETC());
3767   ident_len = CPP_WRITTEN (pfile) - ident;
3768
3769   CPP_SET_WRITTEN (pfile, ident);
3770
3771   for (kt = directive_table; kt->length >= 0; kt++)
3772     if (kt->length == ident_len
3773         && strncmp (pfile->token_buffer + ident, kt->name, kt->length) == 0)
3774       switch (kt->type)
3775         {
3776         case T_IF:
3777         case T_IFDEF:
3778         case T_IFNDEF:
3779             temp = (IF_STACK_FRAME *) xmalloc (sizeof (IF_STACK_FRAME));
3780             temp->next = pfile->if_stack;
3781             pfile->if_stack = temp;
3782             temp->fname = CPP_BUFFER(pfile)->nominal_fname;
3783             temp->type = kt->type;
3784             return 0;
3785
3786         case T_ELSE:
3787             if (CPP_PEDANTIC (pfile) && pfile->if_stack != stack)
3788               validate_else (pfile, "#else");
3789             /* fall through */
3790         case T_ELIF:
3791             if (pfile->if_stack->type == T_ELSE)
3792               cpp_error (pfile, "`%s' after `#else'", kt->name);
3793             
3794             if (pfile->if_stack == stack)
3795               return 1;
3796             else
3797               {
3798                 pfile->if_stack->type = kt->type;
3799                 return 0;
3800               }
3801
3802             case T_ENDIF:
3803                 if (CPP_PEDANTIC (pfile) && pfile->if_stack != stack)
3804                   validate_else (pfile, "#endif");
3805
3806                 if (pfile->if_stack == stack)
3807                   return 1;
3808                     
3809                 temp = pfile->if_stack;
3810                 pfile->if_stack = temp->next;
3811                 free (temp);
3812                 return 0;
3813
3814             default:
3815                 return 0;
3816             }
3817
3818     /* Don't let erroneous code go by.  */
3819     if (!CPP_OPTIONS (pfile)->lang_asm && CPP_PEDANTIC (pfile))
3820         cpp_pedwarn (pfile, "invalid preprocessor directive name");
3821     return 0;
3822 }
3823
3824 /* skip to #endif, #else, or #elif.  adjust line numbers, etc.
3825  * leaves input ptr at the sharp sign found.
3826  */
3827 static void
3828 skip_if_group (pfile)
3829     cpp_reader *pfile;
3830 {
3831   int c;
3832   IF_STACK_FRAME *save_if_stack = pfile->if_stack; /* don't pop past here */
3833   U_CHAR *beg_of_line;
3834   long old_written;
3835
3836   if (CPP_OPTIONS (pfile)->output_conditionals)
3837     {
3838       CPP_PUTS (pfile, "#failed\n", 8);
3839       pfile->lineno++;
3840       output_line_command (pfile, 1, same_file);
3841     }
3842
3843   old_written = CPP_WRITTEN (pfile);
3844   
3845   for (;;)
3846     {
3847       beg_of_line = CPP_BUFFER (pfile)->cur;
3848
3849       if (! CPP_TRADITIONAL (pfile))
3850         cpp_skip_hspace (pfile);
3851       c = GETC();
3852       if (c == '\n')
3853         {
3854           if (CPP_OPTIONS (pfile)->output_conditionals)
3855             CPP_PUTC (pfile, c);
3856           continue;
3857         }
3858       else if (c == '#')
3859         {
3860           if (consider_directive_while_skipping (pfile, save_if_stack))
3861             break;
3862         }
3863       else if (c == EOF)
3864         return;  /* Caller will issue error. */
3865
3866       FORWARD(-1);
3867       if (CPP_OPTIONS (pfile)->output_conditionals)
3868         {
3869           CPP_PUTS (pfile, beg_of_line, CPP_BUFFER (pfile)->cur - beg_of_line);
3870           copy_rest_of_line (pfile);
3871         }
3872       else
3873         {
3874           copy_rest_of_line (pfile);
3875           CPP_SET_WRITTEN (pfile, old_written);  /* discard it */
3876         }
3877
3878       c = GETC();
3879       if (c == EOF)
3880         return;  /* Caller will issue error. */
3881       else
3882         {
3883           /* \n */
3884           if (CPP_OPTIONS (pfile)->output_conditionals)
3885             CPP_PUTC (pfile, c);
3886         }
3887     }     
3888
3889   /* Back up to the beginning of this line.  Caller will process the
3890      directive. */
3891   CPP_BUFFER (pfile)->cur = beg_of_line;
3892   pfile->only_seen_white = 1;
3893   if (CPP_OPTIONS (pfile)->output_conditionals)
3894     {
3895       CPP_PUTS (pfile, "#endfailed\n", 11);
3896       pfile->lineno++;
3897     }
3898 }
3899
3900 /*
3901  * handle a #else directive.  Do this by just continuing processing
3902  * without changing  if_stack ;  this is so that the error message
3903  * for missing #endif's etc. will point to the original #if.  It
3904  * is possible that something different would be better.
3905  */
3906
3907 static int
3908 do_else (pfile, keyword)
3909      cpp_reader *pfile;
3910      struct directive *keyword ATTRIBUTE_UNUSED;
3911 {
3912   cpp_buffer *ip = CPP_BUFFER (pfile);
3913
3914   if (CPP_PEDANTIC (pfile))
3915     validate_else (pfile, "#else");
3916   skip_rest_of_line (pfile);
3917
3918   if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack) {
3919     cpp_error (pfile, "`#else' not within a conditional");
3920     return 0;
3921   } else {
3922     /* #ifndef can't have its special treatment for containing the whole file
3923        if it has a #else clause.  */
3924     pfile->if_stack->control_macro = 0;
3925
3926     if (pfile->if_stack->type != T_IF && pfile->if_stack->type != T_ELIF) {
3927       cpp_error (pfile, "`#else' after `#else'");
3928       fprintf (stderr, " (matches line %d", pfile->if_stack->lineno);
3929       if (strcmp (pfile->if_stack->fname, ip->nominal_fname) != 0)
3930         fprintf (stderr, ", file %s", pfile->if_stack->fname);
3931       fprintf (stderr, ")\n");
3932     }
3933     pfile->if_stack->type = T_ELSE;
3934   }
3935
3936   if (pfile->if_stack->if_succeeded)
3937     skip_if_group (pfile);
3938   else {
3939     ++pfile->if_stack->if_succeeded;    /* continue processing input */
3940     output_line_command (pfile, 1, same_file);
3941   }
3942   return 0;
3943 }
3944
3945 /*
3946  * unstack after #endif command
3947  */
3948
3949 static int
3950 do_endif (pfile, keyword)
3951      cpp_reader *pfile;
3952      struct directive *keyword ATTRIBUTE_UNUSED;
3953 {
3954   if (CPP_PEDANTIC (pfile))
3955     validate_else (pfile, "#endif");
3956   skip_rest_of_line (pfile);
3957
3958   if (pfile->if_stack == CPP_BUFFER (pfile)->if_stack)
3959     cpp_error (pfile, "unbalanced `#endif'");
3960   else
3961     {
3962       IF_STACK_FRAME *temp = pfile->if_stack;
3963       pfile->if_stack = temp->next;
3964       if (temp->control_macro != 0)
3965         {
3966           /* This #endif matched a #ifndef at the start of the file.
3967              See if it is at the end of the file.  */
3968           struct parse_marker start_mark;
3969           int c;
3970
3971           parse_set_mark (&start_mark, pfile);
3972
3973           for (;;)
3974             {
3975               cpp_skip_hspace (pfile);
3976               c = GETC ();
3977               if (c != '\n')
3978                 break;
3979             }
3980           parse_goto_mark (&start_mark, pfile);
3981           parse_clear_mark (&start_mark);
3982
3983           if (c == EOF)
3984             {
3985               /* This #endif ends a #ifndef
3986                  that contains all of the file (aside from whitespace).
3987                  Arrange not to include the file again
3988                  if the macro that was tested is defined. */
3989               struct cpp_buffer *ip;
3990               for (ip = CPP_BUFFER (pfile); ; ip = CPP_PREV_BUFFER (ip))
3991                 if (ip->fname != NULL)
3992                   break;
3993               ip->ihash->control_macro = temp->control_macro;
3994             }
3995         }
3996       free (temp);
3997       output_line_command (pfile, 1, same_file);
3998     }
3999   return 0;
4000 }
4001
4002 /* When an #else or #endif is found while skipping failed conditional,
4003    if -pedantic was specified, this is called to warn about text after
4004    the command name.  P points to the first char after the command name.  */
4005
4006 static void
4007 validate_else (pfile, directive)
4008      cpp_reader *pfile;
4009      char *directive;
4010 {
4011   int c;
4012   cpp_skip_hspace (pfile);
4013   c = PEEKC ();
4014   if (c != EOF && c != '\n')
4015     cpp_pedwarn (pfile,
4016                  "text following `%s' violates ANSI standard", directive);
4017 }
4018
4019 /* Get the next token, and add it to the text in pfile->token_buffer.
4020    Return the kind of token we got.  */
4021   
4022 enum cpp_token
4023 cpp_get_token (pfile)
4024      cpp_reader *pfile;
4025 {
4026   register int c, c2, c3;
4027   long old_written;
4028   long start_line, start_column;
4029   enum cpp_token token;
4030   struct cpp_options *opts = CPP_OPTIONS (pfile);
4031   CPP_BUFFER (pfile)->prev = CPP_BUFFER (pfile)->cur;
4032  get_next:
4033   c = GETC();
4034   if (c == EOF)
4035     {
4036     handle_eof:
4037       if (CPP_BUFFER (pfile)->seen_eof)
4038         {
4039           if (cpp_pop_buffer (pfile) != CPP_NULL_BUFFER (pfile))
4040             goto get_next;
4041           else
4042             return CPP_EOF;
4043         }
4044       else
4045         {
4046           cpp_buffer *next_buf
4047             = CPP_PREV_BUFFER (CPP_BUFFER (pfile));
4048           CPP_BUFFER (pfile)->seen_eof = 1;
4049           if (CPP_BUFFER (pfile)->nominal_fname
4050               && next_buf != CPP_NULL_BUFFER (pfile))
4051             {
4052               /* We're about to return from an #include file.
4053                  Emit #line information now (as part of the CPP_POP) result.
4054                  But the #line refers to the file we will pop to.  */
4055               cpp_buffer *cur_buffer = CPP_BUFFER (pfile);
4056               CPP_BUFFER (pfile) = next_buf;
4057               pfile->input_stack_listing_current = 0;
4058               output_line_command (pfile, 0, leave_file);
4059               CPP_BUFFER (pfile) = cur_buffer;
4060             }
4061           return CPP_POP;
4062         }
4063     }
4064   else
4065     {
4066       switch (c)
4067         {
4068           long newlines;
4069           struct parse_marker start_mark;
4070         case '/':
4071           if (PEEKC () == '=')
4072             goto op2;
4073           if (opts->put_out_comments)
4074             parse_set_mark (&start_mark, pfile);
4075           newlines = 0;
4076           cpp_buf_line_and_col (cpp_file_buffer (pfile),
4077                                 &start_line, &start_column);
4078           c = skip_comment (pfile, &newlines);
4079           if (opts->put_out_comments && (c == '/' || c == EOF))
4080             parse_clear_mark (&start_mark);
4081           if (c == '/')
4082             goto randomchar;
4083           if (c == EOF)
4084             {
4085               cpp_error_with_line (pfile, start_line, start_column,
4086                                    "unterminated comment");
4087               goto handle_eof;
4088             }
4089           c = '/';  /* Initial letter of comment.  */
4090         return_comment:
4091           /* Comments are equivalent to spaces.
4092              For -traditional, a comment is equivalent to nothing.  */
4093           if (opts->put_out_comments)
4094             {
4095               cpp_buffer *pbuf = CPP_BUFFER (pfile);
4096               U_CHAR *start = pbuf->buf + start_mark.position;
4097               int len = pbuf->cur - start;
4098               CPP_RESERVE(pfile, 1 + len);
4099               CPP_PUTC_Q (pfile, c);
4100               CPP_PUTS_Q (pfile, start, len);
4101               pfile->lineno += newlines;
4102               parse_clear_mark (&start_mark);
4103               return CPP_COMMENT;
4104             }
4105           else if (CPP_TRADITIONAL (pfile))
4106             {
4107               return CPP_COMMENT;
4108             }
4109           else
4110             {
4111 #if 0
4112               /* This may not work if cpp_get_token is called recursively,
4113                  since many places look for horizontal space.  */
4114               if (newlines)
4115                 {
4116                   /* Copy the newlines into the output buffer, in order to
4117                      avoid the pain of a #line every time a multiline comment
4118                      is seen.  */
4119                   CPP_RESERVE(pfile, newlines);
4120                   while (--newlines >= 0)
4121                     {
4122                       CPP_PUTC_Q (pfile, '\n');
4123                       pfile->lineno++;
4124                     }
4125                   return CPP_VSPACE;
4126                 }
4127 #endif
4128               CPP_RESERVE(pfile, 1);
4129               CPP_PUTC_Q (pfile, ' ');
4130               return CPP_HSPACE;
4131             }
4132 #if 0
4133           if (opts->for_lint) {
4134             U_CHAR *argbp;
4135             int cmdlen, arglen;
4136             char *lintcmd = get_lintcmd (ibp, limit, &argbp, &arglen, &cmdlen);
4137             
4138             if (lintcmd != NULL) {
4139               /* I believe it is always safe to emit this newline: */
4140               obp[-1] = '\n';
4141               bcopy ("#pragma lint ", (char *) obp, 13);
4142               obp += 13;
4143               bcopy (lintcmd, (char *) obp, cmdlen);
4144               obp += cmdlen;
4145
4146               if (arglen != 0) {
4147                 *(obp++) = ' ';
4148                 bcopy (argbp, (char *) obp, arglen);
4149                 obp += arglen;
4150               }
4151
4152               /* OK, now bring us back to the state we were in before we entered
4153                  this branch.  We need #line because the newline for the pragma
4154                  could mess things up.  */
4155               output_line_command (pfile, 0, same_file);
4156               *(obp++) = ' ';   /* just in case, if comments are copied thru */
4157               *(obp++) = '/';
4158             }
4159           }
4160 #endif
4161
4162         case '#':
4163 #if 0
4164           /* If this is expanding a macro definition, don't recognize
4165              preprocessor directives.  */
4166           if (ip->macro != 0)
4167             goto randomchar;
4168           /* If this is expand_into_temp_buffer, recognize them
4169              only after an actual newline at this level,
4170              not at the beginning of the input level.  */
4171           if (ip->fname == 0 && beg_of_line == ip->buf)
4172             goto randomchar;
4173           if (ident_length)
4174             goto specialchar;
4175 #endif
4176
4177           if (!pfile->only_seen_white)
4178             goto randomchar;
4179           if (handle_directive (pfile))
4180             return CPP_DIRECTIVE;
4181           pfile->only_seen_white = 0;
4182           return CPP_OTHER;
4183
4184         case '\"':
4185         case '\'':
4186         string:
4187           /* A single quoted string is treated like a double -- some
4188              programs (e.g., troff) are perverse this way */
4189           cpp_buf_line_and_col (cpp_file_buffer (pfile),
4190                                 &start_line, &start_column);
4191           old_written = CPP_WRITTEN (pfile);
4192           CPP_PUTC (pfile, c);
4193           while (1)
4194             {
4195               int cc = GETC();
4196               if (cc == EOF)
4197                 {
4198                   if (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
4199                     {
4200                       /* try harder: this string crosses a macro expansion
4201                          boundary.  This can happen naturally if -traditional.
4202                          Otherwise, only -D can make a macro with an unmatched
4203                          quote.  */
4204                         cpp_pop_buffer (pfile);
4205                         continue;
4206                     }
4207                   if (!CPP_TRADITIONAL (pfile))
4208                     {
4209                       cpp_error_with_line (pfile, start_line, start_column,
4210                               "unterminated string or character constant");
4211                       if (pfile->multiline_string_line != start_line
4212                           && pfile->multiline_string_line != 0)
4213                         cpp_error_with_line (pfile,
4214                                              pfile->multiline_string_line, -1,
4215                                "possible real start of unterminated constant");
4216                       pfile->multiline_string_line = 0;
4217                     }
4218                   break;
4219                 }
4220               CPP_PUTC (pfile, cc);
4221               switch (cc)
4222                 {
4223                 case '\n':
4224                   /* Traditionally, end of line ends a string constant with
4225                  no error.  So exit the loop and record the new line.  */
4226                   if (CPP_TRADITIONAL (pfile))
4227                     goto while2end;
4228                   if (c == '\'')
4229                     {
4230                       cpp_error_with_line (pfile, start_line, start_column,
4231                                            "unterminated character constant");
4232                       goto while2end;
4233                     }
4234                   if (CPP_PEDANTIC (pfile)
4235                       && pfile->multiline_string_line == 0)
4236                     {
4237                       cpp_pedwarn_with_line (pfile, start_line, start_column,
4238                                "string constant runs past end of line");
4239                     }
4240                   if (pfile->multiline_string_line == 0)
4241                     pfile->multiline_string_line = start_line;
4242                   break;
4243                 
4244                 case '\\':
4245                   cc = GETC();
4246                   if (cc == '\n')
4247                     {
4248                       /* Backslash newline is replaced by nothing at all.  */
4249                       CPP_ADJUST_WRITTEN (pfile, -1);
4250                       pfile->lineno++;
4251                     }
4252                   else
4253                     {
4254                       /* ANSI stupidly requires that in \\ the second \
4255                          is *not* prevented from combining with a newline.  */
4256                       NEWLINE_FIX1(cc);
4257                       if (cc != EOF)
4258                         CPP_PUTC (pfile, cc);
4259                     }
4260                   break;
4261
4262                 case '\"':
4263                 case '\'':
4264                   if (cc == c)
4265                     goto while2end;
4266                   break;
4267                 }
4268             }
4269         while2end:
4270           pfile->lineno += count_newlines (pfile->token_buffer + old_written,
4271                                            CPP_PWRITTEN (pfile));
4272           pfile->only_seen_white = 0;
4273           return c == '\'' ? CPP_CHAR : CPP_STRING;
4274
4275         case '$':
4276           if (!opts->dollars_in_ident)
4277             goto randomchar;
4278           goto letter;
4279
4280         case ':':
4281           if (opts->cplusplus && PEEKC () == ':')
4282             goto op2;
4283           goto randomchar;
4284
4285         case '&':
4286         case '+':
4287         case '|':
4288           NEWLINE_FIX;
4289           c2 = PEEKC ();
4290           if (c2 == c || c2 == '=')
4291             goto op2;
4292           goto randomchar;
4293
4294         case '*':
4295         case '!':
4296         case '%':
4297         case '=':
4298         case '^':
4299           NEWLINE_FIX;
4300           if (PEEKC () == '=')
4301             goto op2;
4302           goto randomchar;
4303
4304         case '-':
4305           NEWLINE_FIX;
4306           c2 = PEEKC ();
4307           if (c2 == '-' && opts->chill)
4308             {
4309               /* Chill style comment */
4310               if (opts->put_out_comments)
4311                 parse_set_mark (&start_mark, pfile);
4312               FORWARD(1);  /* Skip second '-'.  */
4313               for (;;)
4314                 {
4315                   c = GETC ();
4316                   if (c == EOF)
4317                     break;
4318                   if (c == '\n')
4319                     {
4320                       /* Don't consider final '\n' to be part of comment.  */
4321                       FORWARD(-1);
4322                       break;
4323                     }
4324                 }
4325               c = '-';
4326               goto return_comment;
4327             }
4328           if (c2 == '-' || c2 == '=' || c2 == '>')
4329             goto op2;
4330           goto randomchar;
4331
4332         case '<':
4333           if (pfile->parsing_include_directive)
4334             {
4335               for (;;)
4336                 {
4337                   CPP_PUTC (pfile, c);
4338                   if (c == '>')
4339                     break;
4340                   c = GETC ();
4341                   NEWLINE_FIX1 (c);
4342                   if (c == '\n' || c == EOF)
4343                     {
4344                       cpp_error (pfile,
4345                                  "missing '>' in `#include <FILENAME>'");
4346                       break;
4347                     }
4348                 }
4349               return CPP_STRING;
4350             }
4351           /* else fall through */
4352         case '>':
4353           NEWLINE_FIX;
4354           c2 = PEEKC ();
4355           if (c2 == '=')
4356             goto op2;
4357           if (c2 != c)
4358             goto randomchar;
4359           FORWARD(1);
4360           CPP_RESERVE (pfile, 4);
4361           CPP_PUTC (pfile, c);
4362           CPP_PUTC (pfile, c2);
4363           NEWLINE_FIX;
4364           c3 = PEEKC ();
4365           if (c3 == '=')
4366             CPP_PUTC_Q (pfile, GETC ());
4367           CPP_NUL_TERMINATE_Q (pfile);
4368           pfile->only_seen_white = 0;
4369           return CPP_OTHER;
4370
4371         case '@':
4372           if (CPP_BUFFER (pfile)->has_escapes)
4373             {
4374               c = GETC ();
4375               if (c == '-')
4376                 {
4377                   if (pfile->output_escapes)
4378                     CPP_PUTS (pfile, "@-", 2);
4379                   parse_name (pfile, GETC ());
4380                   return CPP_NAME;
4381                 }
4382               else if (is_space [c])
4383                 {
4384                   CPP_RESERVE (pfile, 2);
4385                   if (pfile->output_escapes)
4386                     CPP_PUTC_Q (pfile, '@');
4387                   CPP_PUTC_Q (pfile, c);
4388                   return CPP_HSPACE;
4389                 }
4390             }
4391           if (pfile->output_escapes)
4392             {
4393               CPP_PUTS (pfile, "@@", 2);
4394               return CPP_OTHER;
4395             }
4396           goto randomchar;
4397
4398         case '.':
4399           NEWLINE_FIX;
4400           c2 = PEEKC ();
4401           if (ISDIGIT(c2))
4402             {
4403               CPP_RESERVE(pfile, 2);
4404               CPP_PUTC_Q (pfile, '.');
4405               c = GETC ();
4406               goto number;
4407             }
4408           /* FIXME - misses the case "..\\\n." */
4409           if (c2 == '.' && PEEKN(1) == '.')
4410             {
4411               CPP_RESERVE(pfile, 4);
4412               CPP_PUTC_Q (pfile, '.');
4413               CPP_PUTC_Q (pfile, '.');
4414               CPP_PUTC_Q (pfile, '.');
4415               FORWARD (2);
4416               CPP_NUL_TERMINATE_Q (pfile);
4417               pfile->only_seen_white = 0;
4418               return CPP_3DOTS;
4419             }
4420           goto randomchar;
4421
4422         op2:
4423           token = CPP_OTHER;
4424           pfile->only_seen_white = 0;
4425         op2any:
4426           CPP_RESERVE(pfile, 3);
4427           CPP_PUTC_Q (pfile, c);
4428           CPP_PUTC_Q (pfile, GETC ());
4429           CPP_NUL_TERMINATE_Q (pfile);
4430           return token;
4431
4432         case 'L':
4433           NEWLINE_FIX;
4434           c2 = PEEKC ();
4435           if ((c2 == '\'' || c2 == '\"') && !CPP_TRADITIONAL (pfile))
4436             {
4437               CPP_PUTC (pfile, c);
4438               c = GETC ();
4439               goto string;
4440             }
4441           goto letter;
4442
4443         case '0': case '1': case '2': case '3': case '4':
4444         case '5': case '6': case '7': case '8': case '9':
4445         number:
4446           c2  = '.';
4447           for (;;)
4448             {
4449               CPP_RESERVE (pfile, 2);
4450               CPP_PUTC_Q (pfile, c);
4451               NEWLINE_FIX;
4452               c = PEEKC ();
4453               if (c == EOF)
4454                 break;
4455               if (!is_idchar[c] && c != '.'
4456                   && ((c2 != 'e' && c2 != 'E'
4457                        && ((c2 != 'p' && c2 != 'P') || CPP_C89 (pfile)))
4458                       || (c != '+' && c != '-')))
4459                 break;
4460               FORWARD(1);
4461               c2= c;
4462             }
4463           CPP_NUL_TERMINATE_Q (pfile);
4464           pfile->only_seen_white = 0;
4465           return CPP_NUMBER;
4466         case 'b': case 'c': case 'd': case 'h': case 'o':
4467         case 'B': case 'C': case 'D': case 'H': case 'O':
4468           if (opts->chill && PEEKC () == '\'')
4469             {
4470               pfile->only_seen_white = 0;
4471               CPP_RESERVE (pfile, 2);
4472               CPP_PUTC_Q (pfile, c);
4473               CPP_PUTC_Q (pfile, '\'');
4474               FORWARD(1);
4475               for (;;)
4476                 {
4477                   c = GETC();
4478                   if (c == EOF)
4479                     goto chill_number_eof;
4480                   if (!is_idchar[c])
4481                     {
4482                       if (c == '\\' && PEEKC() == '\n')
4483                         {
4484                           FORWARD(2);
4485                           continue;
4486                         }
4487                       break;
4488                     }
4489                   CPP_PUTC (pfile, c);
4490                 }
4491               if (c == '\'')
4492                 {
4493                   CPP_RESERVE (pfile, 2);
4494                   CPP_PUTC_Q (pfile, c);
4495                   CPP_NUL_TERMINATE_Q (pfile);
4496                   return CPP_STRING;
4497                 }
4498               else
4499                 {
4500                   FORWARD(-1);
4501                 chill_number_eof:
4502                   CPP_NUL_TERMINATE (pfile);
4503                   return CPP_NUMBER;
4504                 }
4505             }
4506           else
4507             goto letter;
4508         case '_':
4509         case 'a': case 'e': case 'f': case 'g': case 'i': case 'j':
4510         case 'k': case 'l': case 'm': case 'n': case 'p': case 'q':
4511         case 'r': case 's': case 't': case 'u': case 'v': case 'w':
4512         case 'x': case 'y': case 'z':
4513         case 'A': case 'E': case 'F': case 'G': case 'I': case 'J':
4514         case 'K': case 'M': case 'N': case 'P': case 'Q': case 'R':
4515         case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
4516         case 'Y': case 'Z':
4517         letter:
4518           {
4519             HASHNODE *hp;
4520             unsigned char *ident;
4521             int before_name_written = CPP_WRITTEN (pfile);
4522             int ident_len;
4523             parse_name (pfile, c);
4524             pfile->only_seen_white = 0;
4525             if (pfile->no_macro_expand)
4526               return CPP_NAME;
4527             ident = pfile->token_buffer + before_name_written;
4528             ident_len = CPP_PWRITTEN (pfile) - ident;
4529             hp = cpp_lookup (pfile, ident, ident_len, -1);
4530             if (!hp)
4531               return CPP_NAME;
4532             if (hp->type == T_DISABLED)
4533               {
4534                 if (pfile->output_escapes)
4535                   { /* Return "@-IDENT", followed by '\0'.  */
4536                     int i;
4537                     CPP_RESERVE (pfile, 3);
4538                     ident = pfile->token_buffer + before_name_written;
4539                     CPP_ADJUST_WRITTEN (pfile, 2);
4540                     for (i = ident_len; i >= 0; i--) ident[i+2] = ident[i];
4541                     ident[0] = '@';
4542                     ident[1] = '-';
4543                   }
4544                 return CPP_NAME;
4545               }
4546
4547             /* If macro wants an arglist, verify that a '(' follows.
4548                first skip all whitespace, copying it to the output
4549                after the macro name.  Then, if there is no '(',
4550                decide this is not a macro call and leave things that way.  */
4551             if (hp->type == T_MACRO && hp->value.defn->nargs >= 0)
4552             {
4553               struct parse_marker macro_mark;
4554               int is_macro_call, macbuf_whitespace = 0;
4555
4556               parse_set_mark (&macro_mark, pfile);
4557               for (;;)
4558                 {
4559                   cpp_skip_hspace (pfile);
4560                   c = PEEKC ();
4561                   is_macro_call = c == '(';
4562                   if (c != EOF)
4563                     {
4564                       if (c != '\n')
4565                         break;
4566                       FORWARD (1);
4567                     }
4568                   else
4569                     {
4570                       if (CPP_IS_MACRO_BUFFER (CPP_BUFFER (pfile)))
4571                         {
4572                           if (macro_mark.position !=
4573                               (CPP_BUFFER (pfile)->cur
4574                                - CPP_BUFFER (pfile)->buf))
4575                              macbuf_whitespace = 1;
4576
4577                           parse_clear_mark (&macro_mark);
4578                           cpp_pop_buffer (pfile);
4579                           parse_set_mark (&macro_mark, pfile);
4580                         }
4581                       else
4582                         break;
4583                     }
4584                 }
4585               if (!is_macro_call)
4586                 {
4587                   parse_goto_mark (&macro_mark, pfile);
4588                   if (macbuf_whitespace)
4589                     CPP_PUTC (pfile, ' ');
4590                 }
4591               parse_clear_mark (&macro_mark);
4592               if (!is_macro_call)
4593                 return CPP_NAME;
4594             }
4595             /* This is now known to be a macro call.
4596                Expand the macro, reading arguments as needed,
4597                and push the expansion on the input stack.  */
4598             macroexpand (pfile, hp);
4599             CPP_SET_WRITTEN (pfile, before_name_written);
4600           }
4601           goto get_next;
4602
4603         case ' ':  case '\t':  case '\v':  case '\r':
4604           for (;;)
4605             {
4606               CPP_PUTC (pfile, c);
4607               c = PEEKC ();
4608               if (c == EOF || !is_hor_space[c])
4609                 break;
4610               FORWARD(1);
4611             }
4612           return CPP_HSPACE;
4613
4614         case '\\':
4615           c2 = PEEKC ();
4616           if (c2 != '\n')
4617             goto randomchar;
4618           token = CPP_HSPACE;
4619           goto op2any;
4620
4621         case '\n':
4622           CPP_PUTC (pfile, c);
4623           if (pfile->only_seen_white == 0)
4624             pfile->only_seen_white = 1;
4625           pfile->lineno++;
4626           output_line_command (pfile, 1, same_file);
4627           return CPP_VSPACE;
4628
4629         case '(': token = CPP_LPAREN;    goto char1;
4630         case ')': token = CPP_RPAREN;    goto char1;
4631         case '{': token = CPP_LBRACE;    goto char1;
4632         case '}': token = CPP_RBRACE;    goto char1;
4633         case ',': token = CPP_COMMA;     goto char1;
4634         case ';': token = CPP_SEMICOLON; goto char1;
4635
4636         randomchar:
4637         default:
4638           token = CPP_OTHER;
4639         char1:
4640           pfile->only_seen_white = 0;
4641           CPP_PUTC (pfile, c);
4642           return token;
4643         }
4644     }
4645 }
4646
4647 /* Like cpp_get_token, but skip spaces and comments.  */
4648
4649 enum cpp_token
4650 cpp_get_non_space_token (pfile)
4651      cpp_reader *pfile;
4652 {
4653   int old_written = CPP_WRITTEN (pfile);
4654   for (;;)
4655     {
4656       enum cpp_token token = cpp_get_token (pfile);
4657       if (token != CPP_COMMENT && token != CPP_POP
4658           && token != CPP_HSPACE && token != CPP_VSPACE)
4659         return token;
4660       CPP_SET_WRITTEN (pfile, old_written);
4661     }
4662 }
4663
4664 /* Parse an identifier starting with C.  */
4665
4666 static int
4667 parse_name (pfile, c)
4668      cpp_reader *pfile; int c;
4669 {
4670   for (;;)
4671   {
4672       if (! is_idchar[c])
4673       {
4674           if (c == '\\' && PEEKC() == '\n')
4675           {
4676               FORWARD(2);
4677               continue;
4678           }
4679           FORWARD (-1);
4680           break;
4681       }
4682
4683       if (c == '$' && CPP_PEDANTIC (pfile))
4684         cpp_pedwarn (pfile, "`$' in identifier");
4685
4686       CPP_RESERVE(pfile, 2); /* One more for final NUL.  */
4687       CPP_PUTC_Q (pfile, c);
4688       c = GETC();
4689       if (c == EOF)
4690         break;
4691   }
4692   CPP_NUL_TERMINATE_Q (pfile);
4693   return 1;
4694 }
4695
4696 /* This is called after options have been processed.
4697  * Check options for consistency, and setup for processing input
4698  * from the file named FNAME.  (Use standard input if FNAME==NULL.)
4699  * Return 1 on success, 0 on failure.
4700  */
4701
4702 int
4703 cpp_start_read (pfile, fname)
4704      cpp_reader *pfile;
4705      char *fname;
4706 {
4707   struct cpp_options *opts = CPP_OPTIONS (pfile);
4708   struct cpp_pending *pend;
4709   char *p;
4710   int f;
4711   cpp_buffer *fp;
4712   struct include_hash *ih_fake;
4713
4714   /* The code looks at the defaults through this pointer, rather than through
4715      the constant structure above.  This pointer gets changed if an environment
4716      variable specifies other defaults.  */
4717   struct default_include *include_defaults = include_defaults_array;
4718
4719   /* Now that we know dollars_in_ident for real,
4720      reset is_idchar/is_idstart. */
4721   is_idchar['$'] = opts->dollars_in_ident;
4722   is_idstart['$'] = opts->dollars_in_ident;
4723   
4724   /* Add dirs from CPATH after dirs from -I.  */
4725   /* There seems to be confusion about what CPATH should do,
4726      so for the moment it is not documented.  */
4727   /* Some people say that CPATH should replace the standard include dirs,
4728      but that seems pointless: it comes before them, so it overrides them
4729      anyway.  */
4730   GET_ENV_PATH_LIST (p, "CPATH");
4731   if (p != 0 && ! opts->no_standard_includes)
4732     path_include (pfile, p);
4733
4734   /* Do partial setup of input buffer for the sake of generating
4735      early #line directives (when -g is in effect).  */
4736   fp = cpp_push_buffer (pfile, NULL, 0);
4737   if (!fp)
4738     return 0;
4739   if (opts->in_fname == NULL || *opts->in_fname == 0)
4740     {
4741       opts->in_fname = fname;
4742       if (opts->in_fname == NULL)
4743         opts->in_fname = "";
4744     }
4745   fp->nominal_fname = fp->fname = opts->in_fname;
4746   fp->lineno = 0;
4747
4748   /* Install __LINE__, etc.  Must follow initialize_char_syntax
4749      and option processing.  */
4750   initialize_builtins (pfile);
4751
4752   /* Do standard #defines and assertions
4753      that identify system and machine type.  */
4754
4755   if (!opts->inhibit_predefs) {
4756     char *p = (char *) alloca (strlen (predefs) + 1);
4757     strcpy (p, predefs);
4758     while (*p) {
4759       char *q;
4760       while (*p == ' ' || *p == '\t')
4761         p++;
4762       /* Handle -D options.  */ 
4763       if (p[0] == '-' && p[1] == 'D') {
4764         q = &p[2];
4765         while (*p && *p != ' ' && *p != '\t')
4766           p++;
4767         if (*p != 0)
4768           *p++= 0;
4769         if (opts->debug_output)
4770           output_line_command (pfile, 0, same_file);
4771         cpp_define (pfile, q);
4772         while (*p == ' ' || *p == '\t')
4773           p++;
4774       } else if (p[0] == '-' && p[1] == 'A') {
4775         /* Handle -A options (assertions).  */ 
4776         char *assertion;
4777         char *past_name;
4778         char *value;
4779         char *past_value;
4780         char *termination;
4781         int save_char;
4782
4783         assertion = &p[2];
4784         past_name = assertion;
4785         /* Locate end of name.  */
4786         while (*past_name && *past_name != ' '
4787                && *past_name != '\t' && *past_name != '(')
4788           past_name++;
4789         /* Locate `(' at start of value.  */
4790         value = past_name;
4791         while (*value && (*value == ' ' || *value == '\t'))
4792           value++;
4793         if (*value++ != '(')
4794           abort ();
4795         while (*value && (*value == ' ' || *value == '\t'))
4796           value++;
4797         past_value = value;
4798         /* Locate end of value.  */
4799         while (*past_value && *past_value != ' '
4800                && *past_value != '\t' && *past_value != ')')
4801           past_value++;
4802         termination = past_value;
4803         while (*termination && (*termination == ' ' || *termination == '\t'))
4804           termination++;
4805         if (*termination++ != ')')
4806           abort ();
4807         if (*termination && *termination != ' ' && *termination != '\t')
4808           abort ();
4809         /* Temporarily null-terminate the value.  */
4810         save_char = *termination;
4811         *termination = '\0';
4812         /* Install the assertion.  */
4813         make_assertion (pfile, "-A", assertion);
4814         *termination = (char) save_char;
4815         p = termination;
4816         while (*p == ' ' || *p == '\t')
4817           p++;
4818       } else {
4819         abort ();
4820       }
4821     }
4822   }
4823
4824   /* Now handle the command line options.  */
4825
4826   /* Do -U's, -D's and -A's in the order they were seen.  */
4827   /* First reverse the list.  */
4828   opts->pending = nreverse_pending (opts->pending);
4829
4830   for (pend = opts->pending;  pend;  pend = pend->next)
4831     {
4832       if (pend->cmd != NULL && pend->cmd[0] == '-')
4833         {
4834           switch (pend->cmd[1])
4835             {
4836             case 'U':
4837               if (opts->debug_output)
4838                 output_line_command (pfile, 0, same_file);
4839               cpp_undef (pfile, pend->arg);
4840               break;
4841             case 'D':
4842               if (opts->debug_output)
4843                 output_line_command (pfile, 0, same_file);
4844               cpp_define (pfile, pend->arg);
4845               break;
4846             case 'A':
4847               make_assertion (pfile, "-A", pend->arg);
4848               break;
4849             }
4850         }
4851     }
4852
4853   opts->done_initializing = 1;
4854
4855   { /* Read the appropriate environment variable and if it exists
4856        replace include_defaults with the listed path.  */
4857     char *epath = 0;
4858     switch ((opts->objc << 1) + opts->cplusplus)
4859       {
4860       case 0:
4861         GET_ENV_PATH_LIST (epath, "C_INCLUDE_PATH");
4862         break;
4863       case 1:
4864         GET_ENV_PATH_LIST (epath, "CPLUS_INCLUDE_PATH");
4865         break;
4866       case 2:
4867         GET_ENV_PATH_LIST (epath, "OBJC_INCLUDE_PATH");
4868         break;
4869       case 3:
4870         GET_ENV_PATH_LIST (epath, "OBJCPLUS_INCLUDE_PATH");
4871         break;
4872       }
4873     /* If the environment var for this language is set,
4874        add to the default list of include directories.  */
4875     if (epath) {
4876       char *nstore = (char *) alloca (strlen (epath) + 2);
4877       int num_dirs;
4878       char *startp, *endp;
4879
4880       for (num_dirs = 1, startp = epath; *startp; startp++)
4881         if (*startp == PATH_SEPARATOR)
4882           num_dirs++;
4883       include_defaults
4884         = (struct default_include *) xmalloc ((num_dirs
4885                                                * sizeof (struct default_include))
4886                                               + sizeof (include_defaults_array));
4887       startp = endp = epath;
4888       num_dirs = 0;
4889       while (1) {
4890         /* Handle cases like c:/usr/lib:d:/gcc/lib */
4891         if ((*endp == PATH_SEPARATOR)
4892             || *endp == 0) {
4893           strncpy (nstore, startp, endp-startp);
4894           if (endp == startp)
4895             strcpy (nstore, ".");
4896           else
4897             nstore[endp-startp] = '\0';
4898
4899           include_defaults[num_dirs].fname = xstrdup (nstore);
4900           include_defaults[num_dirs].component = 0;
4901           include_defaults[num_dirs].cplusplus = opts->cplusplus;
4902           include_defaults[num_dirs].cxx_aware = 1;
4903           num_dirs++;
4904           if (*endp == '\0')
4905             break;
4906           endp = startp = endp + 1;
4907         } else
4908           endp++;
4909       }
4910       /* Put the usual defaults back in at the end.  */
4911       bcopy ((char *) include_defaults_array,
4912              (char *) &include_defaults[num_dirs],
4913              sizeof (include_defaults_array));
4914     }
4915   }
4916
4917   /* Unless -fnostdinc,
4918      tack on the standard include file dirs to the specified list */
4919   if (!opts->no_standard_includes) {
4920     struct default_include *p = include_defaults;
4921     char *specd_prefix = opts->include_prefix;
4922     char *default_prefix = xstrdup (GCC_INCLUDE_DIR);
4923     int default_len = 0;
4924     /* Remove the `include' from /usr/local/lib/gcc.../include.  */
4925     if (!strcmp (default_prefix + strlen (default_prefix) - 8, "/include")) {
4926       default_len = strlen (default_prefix) - 7;
4927       default_prefix[default_len] = 0;
4928     }
4929     /* Search "translated" versions of GNU directories.
4930        These have /usr/local/lib/gcc... replaced by specd_prefix.  */
4931     if (specd_prefix != 0 && default_len != 0)
4932       for (p = include_defaults; p->fname; p++) {
4933         /* Some standard dirs are only for C++.  */
4934         if (!p->cplusplus
4935             || (opts->cplusplus && !opts->no_standard_cplusplus_includes)) {
4936           /* Does this dir start with the prefix?  */
4937           if (!strncmp (p->fname, default_prefix, default_len)) {
4938             /* Yes; change prefix and add to search list.  */
4939             int this_len = strlen (specd_prefix)
4940                            + strlen (p->fname) - default_len;
4941             char *str = (char *) xmalloc (this_len + 1);
4942             strcpy (str, specd_prefix);
4943             strcat (str, p->fname + default_len);
4944
4945             append_include_chain (pfile, &opts->system_include,
4946                                   str, !p->cxx_aware);
4947           }
4948         }
4949       }
4950     /* Search ordinary names for GNU include directories.  */
4951     for (p = include_defaults; p->fname; p++) {
4952       /* Some standard dirs are only for C++.  */
4953       if (!p->cplusplus
4954           || (opts->cplusplus && !opts->no_standard_cplusplus_includes)) {
4955         const char *str = update_path (p->fname, p->component);
4956         append_include_chain (pfile, &opts->system_include,
4957                               str, !p->cxx_aware);
4958       }
4959     }
4960   }
4961
4962   merge_include_chains (opts);
4963
4964   /* With -v, print the list of dirs to search.  */
4965   if (opts->verbose) {
4966     struct file_name_list *p;
4967     cpp_notice ("#include \"...\" search starts here:\n");
4968     for (p = opts->quote_include; p; p = p->next) {
4969       if (p == opts->bracket_include)
4970         cpp_notice ("#include <...> search starts here:\n");
4971       fprintf (stderr, " %s\n", p->name);
4972     }
4973     cpp_notice ("End of search list.\n");
4974   }
4975
4976   /* Copy the entire contents of the main input file into
4977      the stacked input buffer previously allocated for it.  */
4978   if (fname == NULL || *fname == 0) {
4979     fname = "";
4980     f = 0;
4981   } else if ((f = open (fname, O_RDONLY|O_NONBLOCK|O_NOCTTY, 0666)) < 0)
4982     cpp_pfatal_with_name (pfile, fname);
4983
4984   /* -MG doesn't select the form of output and must be specified with one of
4985      -M or -MM.  -MG doesn't make sense with -MD or -MMD since they don't
4986      inhibit compilation.  */
4987   if (opts->print_deps_missing_files
4988       && (opts->print_deps == 0 || !opts->no_output))
4989     {
4990       cpp_fatal (pfile, "-MG must be specified with one of -M or -MM");
4991       return 0;
4992     }
4993
4994   /* Either of two environment variables can specify output of deps.
4995      Its value is either "OUTPUT_FILE" or "OUTPUT_FILE DEPS_TARGET",
4996      where OUTPUT_FILE is the file to write deps info to
4997      and DEPS_TARGET is the target to mention in the deps.  */
4998
4999   if (opts->print_deps == 0
5000       && (getenv ("SUNPRO_DEPENDENCIES") != 0
5001           || getenv ("DEPENDENCIES_OUTPUT") != 0)) {
5002     char *spec = getenv ("DEPENDENCIES_OUTPUT");
5003     char *s;
5004     char *output_file;
5005
5006     if (spec == 0)
5007       {
5008         spec = getenv ("SUNPRO_DEPENDENCIES");
5009         opts->print_deps = 2;
5010       }
5011     else
5012       opts->print_deps = 1;
5013
5014     s = spec;
5015     /* Find the space before the DEPS_TARGET, if there is one.  */
5016     /* This should use index.  (mrs) */
5017     while (*s != 0 && *s != ' ') s++;
5018     if (*s != 0)
5019       {
5020         opts->deps_target = s + 1;
5021         output_file = (char *) xmalloc (s - spec + 1);
5022         bcopy (spec, output_file, s - spec);
5023         output_file[s - spec] = 0;
5024       }
5025     else
5026       {
5027         opts->deps_target = 0;
5028         output_file = spec;
5029       }
5030
5031     opts->deps_file = output_file;
5032     opts->print_deps_append = 1;
5033   }
5034
5035   /* For -M, print the expected object file name
5036      as the target of this Make-rule.  */
5037   if (opts->print_deps)
5038     {
5039       pfile->deps_allocated_size = 200;
5040       pfile->deps_buffer = (char *) xmalloc (pfile->deps_allocated_size);
5041       pfile->deps_buffer[0] = 0;
5042       pfile->deps_size = 0;
5043       pfile->deps_column = 0;
5044
5045       if (opts->deps_target)
5046         deps_output (pfile, opts->deps_target, ':');
5047       else if (*opts->in_fname == 0)
5048         deps_output (pfile, "-", ':');
5049       else
5050         {
5051           char *p, *q, *r;
5052           int len, x;
5053           static char *known_suffixes[] = { ".c", ".C", ".s", ".S", ".m",
5054                                      ".cc", ".cxx", ".cpp", ".cp",
5055                                      ".c++", 0
5056                                    };
5057
5058           /* Discard all directory prefixes from filename.  */
5059           if ((q = rindex (opts->in_fname, '/')) != NULL
5060 #ifdef DIR_SEPARATOR
5061               && (q = rindex (opts->in_fname, DIR_SEPARATOR)) != NULL
5062 #endif
5063               )
5064             ++q;
5065           else
5066             q = opts->in_fname;
5067
5068           /* Copy remainder to mungable area.  */
5069           p = (char *) alloca (strlen(q) + 8);
5070           strcpy (p, q);
5071
5072           /* Output P, but remove known suffixes.  */
5073           len = strlen (p);
5074           q = p + len;
5075           /* Point to the filename suffix.  */
5076           r = rindex (p, '.');
5077           /* Compare against the known suffixes.  */
5078           x = 0;
5079           while (known_suffixes[x] != 0)
5080             {
5081               if (strncmp (known_suffixes[x], r, q - r) == 0)
5082                 {
5083                   /* Make q point to the bit we're going to overwrite
5084                      with an object suffix.  */
5085                   q = r;
5086                   break;
5087                 }
5088               x++;
5089             }
5090
5091           /* Supply our own suffix.  */
5092 #ifndef VMS
5093           strcpy (q, ".o");
5094 #else
5095           strcpy (q, ".obj");
5096 #endif
5097
5098           deps_output (pfile, p, ':');
5099           deps_output (pfile, opts->in_fname, ' ');
5100         }
5101     }
5102
5103   /* Must call finclude() on the main input before processing
5104      -include switches; otherwise the -included text winds up
5105      after the main input. */
5106   ih_fake = (struct include_hash *) xmalloc (sizeof (struct include_hash));
5107   ih_fake->next = 0;
5108   ih_fake->next_this_file = 0;
5109   ih_fake->foundhere = ABSOLUTE_PATH;  /* well sort of ... */
5110   ih_fake->name = fname;
5111   ih_fake->control_macro = 0;
5112   ih_fake->buf = (char *)-1;
5113   ih_fake->limit = 0;
5114   if (!finclude (pfile, f, ih_fake))
5115     return 0;
5116   output_line_command (pfile, 0, same_file);
5117   pfile->only_seen_white = 2;
5118
5119   /* The -imacros files can be scanned now, but the -include files
5120      have to be pushed onto the include stack and processed later,
5121      in the main loop calling cpp_get_token.  That means the -include
5122      files have to be processed in reverse order of the pending list,
5123      which means the pending list has to be reversed again, which
5124      means the -imacros files have to be done separately and first. */
5125   
5126   pfile->no_record_file++;
5127   opts->no_output++;
5128   for (pend = opts->pending; pend; pend = pend->next)
5129     {
5130       if (pend->cmd != NULL)
5131         {
5132           if (strcmp (pend->cmd, "-imacros") == 0)
5133             {
5134               int fd = open (pend->arg, O_RDONLY|O_NONBLOCK|O_NOCTTY, 0666);
5135               if (fd < 0)
5136                 {
5137                   cpp_perror_with_name (pfile, pend->arg);
5138                   return 0;
5139                 }
5140               if (!cpp_push_buffer (pfile, NULL, 0))
5141                 return 0;
5142
5143               ih_fake = (struct include_hash *)
5144                   xmalloc (sizeof (struct include_hash));
5145               ih_fake->next = 0;
5146               ih_fake->next_this_file = 0;
5147               ih_fake->foundhere = ABSOLUTE_PATH;  /* well sort of ... */
5148               ih_fake->name = pend->arg;
5149               ih_fake->control_macro = 0;
5150               ih_fake->buf = (char *)-1;
5151               ih_fake->limit = 0;
5152               if (!finclude (pfile, fd, ih_fake))
5153                 cpp_scan_buffer (pfile);
5154               free (ih_fake);
5155             }
5156         }
5157     }
5158   opts->no_output--;
5159   opts->pending = nreverse_pending (opts->pending);
5160   for (pend = opts->pending; pend; pend = pend->next)
5161     {
5162       if (pend->cmd != NULL)
5163         {
5164           if (strcmp (pend->cmd, "-include") == 0)
5165             {
5166               int fd = open (pend->arg, O_RDONLY|O_NONBLOCK|O_NOCTTY, 0666);
5167               if (fd < 0)
5168                 {
5169                   cpp_perror_with_name (pfile, pend->arg);
5170                   return 0;
5171                 }
5172               if (!cpp_push_buffer (pfile, NULL, 0))
5173                 return 0;
5174
5175               ih_fake = (struct include_hash *)
5176                   xmalloc (sizeof (struct include_hash));
5177               ih_fake->next = 0;
5178               ih_fake->next_this_file = 0;
5179               ih_fake->foundhere = ABSOLUTE_PATH;  /* well sort of ... */
5180               ih_fake->name = pend->arg;
5181               ih_fake->control_macro = 0;
5182               ih_fake->buf = (char *)-1;
5183               ih_fake->limit = 0;
5184               if (finclude (pfile, fd, ih_fake))
5185                 output_line_command (pfile, 0, enter_file);
5186             }
5187         }
5188     }
5189   pfile->no_record_file--;
5190
5191   /* Free the pending list.  */
5192   for (pend = opts->pending;  pend; )
5193     {
5194       struct cpp_pending *next = pend->next;
5195       free (pend);
5196       pend = next;
5197     }
5198   opts->pending = NULL;
5199
5200   return 1;
5201 }
5202
5203 void
5204 cpp_reader_init (pfile)
5205      cpp_reader *pfile;
5206 {
5207   bzero ((char *) pfile, sizeof (cpp_reader));
5208   pfile->get_token = cpp_get_token;
5209
5210   pfile->token_buffer_size = 200;
5211   pfile->token_buffer = (U_CHAR *) xmalloc (pfile->token_buffer_size);
5212   CPP_SET_WRITTEN (pfile, 0);
5213
5214   pfile->timebuf = NULL;
5215   pfile->only_seen_white = 1;
5216   pfile->buffer = CPP_NULL_BUFFER(pfile);
5217   pfile->actual_dirs = NULL;
5218 }
5219
5220 static struct cpp_pending *
5221 nreverse_pending (list)
5222      struct cpp_pending *list;
5223      
5224 {
5225   register struct cpp_pending *prev = 0, *next, *pend;
5226   for (pend = list;  pend;  pend = next)
5227     {
5228       next = pend->next;
5229       pend->next = prev;
5230       prev = pend;
5231     }
5232   return prev;
5233 }
5234
5235 static void
5236 push_pending (pfile, cmd, arg)
5237      cpp_reader *pfile;
5238      char *cmd;
5239      char *arg;
5240 {
5241   struct cpp_pending *pend
5242     = (struct cpp_pending *) xmalloc (sizeof (struct cpp_pending));
5243   pend->cmd = cmd;
5244   pend->arg = arg;
5245   pend->next = CPP_OPTIONS (pfile)->pending;
5246   CPP_OPTIONS (pfile)->pending = pend;
5247 }
5248
5249 \f
5250 static void
5251 print_help ()
5252 {
5253   printf ("Usage: %s [switches] input output\n", progname);
5254   printf ("Switches:\n");
5255   printf ("  -include <file>           Include the contents of <file> before other files\n");
5256   printf ("  -imacros <file>           Accept definition of marcos in <file>\n");
5257   printf ("  -iprefix <path>           Specify <path> as a prefix for next two options\n");
5258   printf ("  -iwithprefix <dir>        Add <dir> to the end of the system include paths\n");
5259   printf ("  -iwithprefixbefore <dir>  Add <dir> to the end of the main include paths\n");
5260   printf ("  -isystem <dir>            Add <dir> to the start of the system include paths\n");
5261   printf ("  -idirafter <dir>          Add <dir> to the end of the system include paths\n");
5262   printf ("  -I <dir>                  Add <dir> to the end of the main include paths\n");
5263   printf ("  -nostdinc                 Do not search the system include directories\n");
5264   printf ("  -nostdinc++               Do not search the system include directories for C++\n");
5265   printf ("  -o <file>                 Put output into <file>\n");
5266   printf ("  -pedantic                 Issue all warnings demanded by strict ANSI C\n");
5267   printf ("  -traditional              Follow K&R pre-processor behaviour\n");
5268   printf ("  -trigraphs                Support ANSI C trigraphs\n");
5269   printf ("  -lang-c                   Assume that the input sources are in C\n");
5270   printf ("  -lang-c89                 Assume that the input sources are in C89\n");
5271   printf ("  -lang-c++                 Assume that the input sources are in C++\n");
5272   printf ("  -lang-objc                Assume that the input sources are in ObjectiveC\n");
5273   printf ("  -lang-objc++              Assume that the input sources are in ObjectiveC++\n");
5274   printf ("  -lang-asm                 Assume that the input sources are in assembler\n");
5275   printf ("  -lang-chill               Assume that the input sources are in Chill\n");
5276   printf ("  -+                        Allow parsing of C++ style features\n");
5277   printf ("  -w                        Inhibit warning messages\n");
5278   printf ("  -Wtrigraphs               Warn if trigraphs are encountered\n");
5279   printf ("  -Wno-trigraphs            Do not warn about trigraphs\n");
5280   printf ("  -Wcomment{s}              Warn if one comment starts inside another\n");
5281   printf ("  -Wno-comment{s}           Do not warn about comments\n");
5282   printf ("  -Wtraditional             Warn if a macro argument is/would be turned into\n");
5283   printf ("                             a string if -traditional is specified\n");
5284   printf ("  -Wno-traditional          Do not warn about stringification\n");
5285   printf ("  -Wundef                   Warn if an undefined macro is used by #if\n");
5286   printf ("  -Wno-undef                Do not warn about testing undefined macros\n");
5287   printf ("  -Wimport                  Warn about the use of the #import directive\n");
5288   printf ("  -Wno-import               Do not warn about the use of #import\n");
5289   printf ("  -Werror                   Treat all warnings as errors\n");
5290   printf ("  -Wno-error                Do not treat warnings as errors\n");
5291   printf ("  -Wall                     Enable all preprocessor warnings\n");
5292   printf ("  -M                        Generate make dependencies\n");
5293   printf ("  -MM                       As -M, but ignore system header files\n");
5294   printf ("  -MD                       As -M, but put output in a .d file\n");
5295   printf ("  -MMD                      As -MD, but ignore system header files\n");
5296   printf ("  -MG                       Treat missing header file as generated files\n");
5297   printf ("  -g                        Include #define and #undef directives in the output\n");
5298   printf ("  -D<macro>                 Define a <macro> with string '1' as its value\n");
5299   printf ("  -D<macro>=<val>           Define a <macro> with <val> as its value\n");
5300   printf ("  -A<question> (<answer>)   Assert the <answer> to <question>\n");
5301   printf ("  -U<macro>                 Undefine <macro> \n");
5302   printf ("  -u or -undef              Do not predefine any macros\n");
5303   printf ("  -v                        Display the version number\n");
5304   printf ("  -H                        Print the name of header files as they are used\n");
5305   printf ("  -C                        Do not discard comments\n");
5306   printf ("  -dM                       Display a list of macro definitions active at end\n");
5307   printf ("  -dD                       Preserve macro definitions in output\n");
5308   printf ("  -dN                       As -dD except that only the names are preserved\n");
5309   printf ("  -dI                       Include #include directives in the output\n");
5310   printf ("  -ifoutput                 Describe skipped code blocks in output \n");
5311   printf ("  -P                        Do not generate #line directives\n");
5312   printf ("  -$                        Do not include '$' in identifiers\n");
5313   printf ("  -remap                    Remap file names when including files.\n");
5314   printf ("  -h or --help              Display this information\n");
5315 }
5316 \f
5317
5318 /* Handle one command-line option in (argc, argv).
5319    Can be called multiple times, to handle multiple sets of options.
5320    Returns number of strings consumed.  */
5321 int
5322 cpp_handle_option (pfile, argc, argv)
5323      cpp_reader *pfile;
5324      int argc;
5325      char **argv;
5326 {
5327   struct cpp_options *opts = CPP_OPTIONS (pfile);
5328   int i = 0;
5329
5330   if (user_label_prefix == NULL)
5331     user_label_prefix = USER_LABEL_PREFIX;
5332
5333   if (argv[i][0] != '-') {
5334     if (opts->out_fname != NULL)
5335       {
5336         print_help ();
5337         cpp_fatal (pfile, "Too many arguments");
5338       }
5339     else if (opts->in_fname != NULL)
5340       opts->out_fname = argv[i];
5341     else
5342       opts->in_fname = argv[i];
5343   } else {
5344     switch (argv[i][1]) {
5345       
5346     missing_filename:
5347       cpp_fatal (pfile, "Filename missing after `%s' option", argv[i]);
5348       return argc;
5349     missing_dirname:
5350       cpp_fatal (pfile, "Directory name missing after `%s' option", argv[i]);
5351       return argc;
5352       
5353     case 'f':
5354       if (!strcmp (argv[i], "-fleading-underscore"))
5355         user_label_prefix = "_";
5356       else if (!strcmp (argv[i], "-fno-leading-underscore"))
5357         user_label_prefix = "";
5358       break;
5359
5360     case 'I':                   /* Add directory to path for includes.  */
5361       if (!strcmp (argv[i] + 2, "-"))
5362         {
5363           if (! opts->ignore_srcdir)
5364             {
5365               opts->ignore_srcdir = 1;
5366               /* Don't use any preceding -I directories for #include <...>. */
5367               opts->quote_include = opts->bracket_include;
5368               opts->bracket_include = 0;
5369             }
5370         }
5371       else
5372         {
5373           char *fname;
5374           if (argv[i][2] != 0)
5375             fname = argv[i] + 2;
5376           else if (i + 1 == argc)
5377             goto missing_dirname;
5378           else
5379             fname = argv[++i];
5380           append_include_chain (pfile, &opts->bracket_include, fname, 0);
5381         }
5382       break;
5383
5384     case 'i':
5385       /* Add directory to beginning of system include path, as a system
5386          include directory. */
5387       if (!strcmp (argv[i], "-isystem"))
5388         {
5389           if (i + 1 == argc)
5390             goto missing_filename;
5391           append_include_chain (pfile, &opts->system_include, argv[++i], 1);
5392         }
5393       /* Add directory to end of path for includes,
5394          with the default prefix at the front of its name.  */
5395       else if (!strcmp (argv[i], "-iwithprefix"))
5396         {
5397           char *fname;
5398           if (i + 1 == argc)
5399             goto missing_dirname;
5400           ++i;
5401
5402           if (opts->include_prefix != 0)
5403             {
5404               fname = xmalloc (strlen (opts->include_prefix)
5405                                + strlen (argv[i]) + 1);
5406               strcpy (fname, opts->include_prefix);
5407               strcat (fname, argv[i]);
5408             }
5409           else
5410             {
5411               fname = xmalloc (strlen (GCC_INCLUDE_DIR)
5412                                + strlen (argv[i]) + 1);
5413               strcpy (fname, GCC_INCLUDE_DIR);
5414               /* Remove the `include' from /usr/local/lib/gcc.../include.  */
5415               if (!strcmp (fname + strlen (fname) - 8, "/include"))
5416                 fname[strlen (fname) - 7] = 0;
5417               strcat (fname, argv[i]);
5418             }
5419           
5420           append_include_chain (pfile, &opts->system_include, fname, 0);
5421       }
5422       /* Add directory to main path for includes,
5423          with the default prefix at the front of its name.  */
5424       else if (!strcmp (argv[i], "-iwithprefix"))
5425         {
5426           char *fname;
5427           if (i + 1 == argc)
5428             goto missing_dirname;
5429           ++i;
5430
5431           if (opts->include_prefix != 0)
5432             {
5433               fname = xmalloc (strlen (opts->include_prefix)
5434                                + strlen (argv[i]) + 1);
5435               strcpy (fname, opts->include_prefix);
5436               strcat (fname, argv[i]);
5437             }
5438           else
5439             {
5440               fname = xmalloc (strlen (GCC_INCLUDE_DIR)
5441                                + strlen (argv[i]) + 1);
5442               strcpy (fname, GCC_INCLUDE_DIR);
5443               /* Remove the `include' from /usr/local/lib/gcc.../include.  */
5444               if (!strcmp (fname + strlen (fname) - 8, "/include"))
5445                 fname[strlen (fname) - 7] = 0;
5446               strcat (fname, argv[i]);
5447             }
5448           
5449           append_include_chain (pfile, &opts->bracket_include, fname, 0);
5450         }
5451       /* Add directory to end of path for includes.  */
5452       else if (!strcmp (argv[i], "-idirafter"))
5453         {
5454           if (i + 1 == argc)
5455             goto missing_dirname;
5456           append_include_chain (pfile, &opts->after_include, argv[++i], 0);
5457         }
5458       else if (!strcmp (argv[i], "-include") || !strcmp (argv[i], "-imacros"))
5459         {
5460           if (i + 1 == argc)
5461             goto missing_filename;
5462           else
5463             push_pending (pfile, argv[i], argv[i+1]), i++;
5464         }
5465       else if (!strcmp (argv[i], "-iprefix"))
5466         {
5467           if (i + 1 == argc)
5468             goto missing_filename;
5469           else
5470               opts->include_prefix = argv[++i];
5471         }
5472       else if (!strcmp (argv[i], "-ifoutput"))
5473         opts->output_conditionals = 1;
5474
5475       break;
5476       
5477     case 'o':
5478       if (opts->out_fname != NULL)
5479         {
5480           cpp_fatal (pfile, "Output filename specified twice");
5481           return argc;
5482         }
5483       if (i + 1 == argc)
5484         goto missing_filename;
5485       opts->out_fname = argv[++i];
5486       if (!strcmp (opts->out_fname, "-"))
5487         opts->out_fname = "";
5488       break;
5489       
5490     case 'p':
5491       if (!strcmp (argv[i], "-pedantic"))
5492         CPP_PEDANTIC (pfile) = 1;
5493       else if (!strcmp (argv[i], "-pedantic-errors")) {
5494         CPP_PEDANTIC (pfile) = 1;
5495         opts->pedantic_errors = 1;
5496       }
5497 #if 0
5498       else if (!strcmp (argv[i], "-pcp")) {
5499         char *pcp_fname = argv[++i];
5500         pcp_outfile = ((pcp_fname[0] != '-' || pcp_fname[1] != '\0')
5501                        ? fopen (pcp_fname, "w")
5502                        : fdopen (dup (fileno (stdout)), "w"));
5503         if (pcp_outfile == 0)
5504           cpp_pfatal_with_name (pfile, pcp_fname);
5505         no_precomp = 1;
5506       }
5507 #endif
5508       break;
5509       
5510     case 't':
5511       if (!strcmp (argv[i], "-traditional")) {
5512         opts->traditional = 1;
5513         opts->cplusplus_comments = 0;
5514       } else if (!strcmp (argv[i], "-trigraphs")) {
5515         if (!opts->chill)
5516           opts->trigraphs = 1;
5517       }
5518       break;
5519       
5520     case 'l':
5521       if (! strcmp (argv[i], "-lang-c"))
5522         opts->cplusplus = 0, opts->cplusplus_comments = 1, opts->c89 = 0,
5523           opts->objc = 0;
5524       if (! strcmp (argv[i], "-lang-c89"))
5525         opts->cplusplus = 0, opts->cplusplus_comments = 0, opts->c89 = 1,
5526           opts->objc = 0;
5527       if (! strcmp (argv[i], "-lang-c++"))
5528         opts->cplusplus = 1, opts->cplusplus_comments = 1, opts->c89 = 0,
5529           opts->objc = 0;
5530       if (! strcmp (argv[i], "-lang-objc"))
5531         opts->cplusplus = 0, opts->cplusplus_comments = 1, opts->c89 = 0,
5532           opts->objc = 1;
5533       if (! strcmp (argv[i], "-lang-objc++"))
5534         opts->cplusplus = 1, opts->cplusplus_comments = 1, opts->c89 = 0,
5535           opts->objc = 1;
5536       if (! strcmp (argv[i], "-lang-asm"))
5537         opts->lang_asm = 1;
5538       if (! strcmp (argv[i], "-lint"))
5539         opts->for_lint = 1;
5540       if (! strcmp (argv[i], "-lang-chill"))
5541         opts->objc = 0, opts->cplusplus = 0, opts->chill = 1,
5542           opts->traditional = 1, opts->trigraphs = 0;
5543       break;
5544       
5545     case '+':
5546       opts->cplusplus = 1, opts->cplusplus_comments = 1;
5547       break;
5548       
5549     case 'w':
5550       opts->inhibit_warnings = 1;
5551       break;
5552       
5553     case 'W':
5554       if (!strcmp (argv[i], "-Wtrigraphs"))
5555         opts->warn_trigraphs = 1;
5556       else if (!strcmp (argv[i], "-Wno-trigraphs"))
5557         opts->warn_trigraphs = 0;
5558       else if (!strcmp (argv[i], "-Wcomment"))
5559         opts->warn_comments = 1;
5560       else if (!strcmp (argv[i], "-Wno-comment"))
5561         opts->warn_comments = 0;
5562       else if (!strcmp (argv[i], "-Wcomments"))
5563         opts->warn_comments = 1;
5564       else if (!strcmp (argv[i], "-Wno-comments"))
5565         opts->warn_comments = 0;
5566       else if (!strcmp (argv[i], "-Wtraditional"))
5567         opts->warn_stringify = 1;
5568       else if (!strcmp (argv[i], "-Wno-traditional"))
5569         opts->warn_stringify = 0;
5570       else if (!strcmp (argv[i], "-Wundef"))
5571         opts->warn_undef = 1;
5572       else if (!strcmp (argv[i], "-Wno-undef"))
5573         opts->warn_undef = 0;
5574       else if (!strcmp (argv[i], "-Wimport"))
5575         opts->warn_import = 1;
5576       else if (!strcmp (argv[i], "-Wno-import"))
5577         opts->warn_import = 0;
5578       else if (!strcmp (argv[i], "-Werror"))
5579         opts->warnings_are_errors = 1;
5580       else if (!strcmp (argv[i], "-Wno-error"))
5581         opts->warnings_are_errors = 0;
5582       else if (!strcmp (argv[i], "-Wall"))
5583         {
5584           opts->warn_trigraphs = 1;
5585           opts->warn_comments = 1;
5586         }
5587       break;
5588       
5589     case 'M':
5590       /* The style of the choices here is a bit mixed.
5591          The chosen scheme is a hybrid of keeping all options in one string
5592          and specifying each option in a separate argument:
5593          -M|-MM|-MD file|-MMD file [-MG].  An alternative is:
5594          -M|-MM|-MD file|-MMD file|-MG|-MMG; or more concisely:
5595          -M[M][G][D file].  This is awkward to handle in specs, and is not
5596          as extensible.  */
5597       /* ??? -MG must be specified in addition to one of -M or -MM.
5598          This can be relaxed in the future without breaking anything.
5599          The converse isn't true.  */
5600       
5601       /* -MG isn't valid with -MD or -MMD.  This is checked for later.  */
5602       if (!strcmp (argv[i], "-MG"))
5603         {
5604           opts->print_deps_missing_files = 1;
5605           break;
5606         }
5607       if (!strcmp (argv[i], "-M"))
5608         opts->print_deps = 2;
5609       else if (!strcmp (argv[i], "-MM"))
5610         opts->print_deps = 1;
5611       else if (!strcmp (argv[i], "-MD"))
5612         opts->print_deps = 2;
5613       else if (!strcmp (argv[i], "-MMD"))
5614         opts->print_deps = 1;
5615       /* For -MD and -MMD options, write deps on file named by next arg.  */
5616       if (!strcmp (argv[i], "-MD") || !strcmp (argv[i], "-MMD"))
5617         {
5618           if (i+1 == argc)
5619             goto missing_filename;
5620           opts->deps_file = argv[++i];
5621         }
5622       else
5623         {
5624           /* For -M and -MM, write deps on standard output
5625              and suppress the usual output.  */
5626           opts->no_output = 1;
5627         }         
5628       break;
5629       
5630     case 'd':
5631       {
5632         char *p = argv[i] + 2;
5633         char c;
5634         while ((c = *p++) != 0) {
5635           /* Arg to -d specifies what parts of macros to dump */
5636           switch (c) {
5637           case 'M':
5638             opts->dump_macros = dump_only;
5639             opts->no_output = 1;
5640             break;
5641           case 'N':
5642             opts->dump_macros = dump_names;
5643             break;
5644           case 'D':
5645             opts->dump_macros = dump_definitions;
5646             break;
5647           case 'I':
5648             opts->dump_includes = 1;
5649             break;
5650           }
5651         }
5652       }
5653     break;
5654     
5655     case 'g':
5656       if (argv[i][2] == '3')
5657         opts->debug_output = 1;
5658       break;
5659       
5660     case '-':
5661       if (strcmp (argv[i], "--help") != 0)
5662         return i;
5663       print_help ();
5664       break;
5665         
5666     case 'v':
5667       cpp_notice ("GNU CPP version %s", version_string);
5668 #ifdef TARGET_VERSION
5669       TARGET_VERSION;
5670 #endif
5671       fprintf (stderr, "\n");
5672       opts->verbose = 1;
5673       break;
5674       
5675     case 'H':
5676       opts->print_include_names = 1;
5677       break;
5678       
5679     case 'D':
5680       if (argv[i][2] != 0)
5681         push_pending (pfile, "-D", argv[i] + 2);
5682       else if (i + 1 == argc)
5683         {
5684           cpp_fatal (pfile, "Macro name missing after -D option");
5685           return argc;
5686         }
5687       else
5688         i++, push_pending (pfile, "-D", argv[i]);
5689       break;
5690       
5691     case 'A':
5692       {
5693         char *p;
5694         
5695         if (argv[i][2] != 0)
5696           p = argv[i] + 2;
5697         else if (i + 1 == argc)
5698           {
5699             cpp_fatal (pfile, "Assertion missing after -A option");
5700             return argc;
5701           }
5702         else
5703           p = argv[++i];
5704         
5705         if (!strcmp (p, "-")) {
5706           struct cpp_pending **ptr;
5707           /* -A- eliminates all predefined macros and assertions.
5708              Let's include also any that were specified earlier
5709              on the command line.  That way we can get rid of any
5710              that were passed automatically in from GCC.  */
5711           opts->inhibit_predefs = 1;
5712           for (ptr = &opts->pending; *ptr != NULL; )
5713             {
5714               struct cpp_pending *pend = *ptr;
5715               if (pend->cmd && pend->cmd[0] == '-'
5716                   && (pend->cmd[1] == 'D' || pend->cmd[1] == 'A'))
5717                 {
5718                   *ptr = pend->next;
5719                   free (pend);
5720                 }
5721               else
5722                 ptr = &pend->next;
5723             }
5724         } else {
5725           push_pending (pfile, "-A", p);
5726         }
5727       }
5728     break;
5729     
5730     case 'U':           /* JF #undef something */
5731       if (argv[i][2] != 0)
5732         push_pending (pfile, "-U", argv[i] + 2);
5733       else if (i + 1 == argc)
5734         {
5735           cpp_fatal (pfile, "Macro name missing after -U option");
5736           return argc;
5737         }
5738       else
5739         push_pending (pfile, "-U", argv[i+1]), i++;
5740       break;
5741       
5742     case 'C':
5743       opts->put_out_comments = 1;
5744       break;
5745       
5746     case 'E':                   /* -E comes from cc -E; ignore it.  */
5747       break;
5748       
5749     case 'P':
5750       opts->no_line_commands = 1;
5751       break;
5752       
5753     case '$':                   /* Don't include $ in identifiers.  */
5754       opts->dollars_in_ident = 0;
5755       break;
5756       
5757     case 'n':
5758       if (!strcmp (argv[i], "-nostdinc"))
5759         /* -nostdinc causes no default include directories.
5760            You must specify all include-file directories with -I.  */
5761         opts->no_standard_includes = 1;
5762       else if (!strcmp (argv[i], "-nostdinc++"))
5763         /* -nostdinc++ causes no default C++-specific include directories. */
5764         opts->no_standard_cplusplus_includes = 1;
5765 #if 0
5766       else if (!strcmp (argv[i], "-noprecomp"))
5767         no_precomp = 1;
5768 #endif
5769       break;
5770       
5771     case 'r':
5772       if (!strcmp (argv[i], "-remap"))
5773         opts->remap = 1;
5774       break;
5775       
5776     case 'u':
5777       /* Sun compiler passes undocumented switch "-undef".
5778          Let's assume it means to inhibit the predefined symbols.  */
5779       opts->inhibit_predefs = 1;
5780       break;
5781       
5782     case '\0': /* JF handle '-' as file name meaning stdin or stdout */
5783       if (opts->in_fname == NULL) {
5784         opts->in_fname = "";
5785         break;
5786       } else if (opts->out_fname == NULL) {
5787         opts->out_fname = "";
5788         break;
5789       } /* else fall through into error */
5790
5791     default:
5792       return i;
5793     }
5794   }
5795
5796   return i + 1;
5797 }
5798
5799 /* Handle command-line options in (argc, argv).
5800    Can be called multiple times, to handle multiple sets of options.
5801    Returns if an unrecognized option is seen.
5802    Returns number of strings consumed.  */
5803
5804 int
5805 cpp_handle_options (pfile, argc, argv)
5806      cpp_reader *pfile;
5807      int argc;
5808      char **argv;
5809 {
5810   int i;
5811   int strings_processed;
5812   for (i = 0; i < argc; i += strings_processed)
5813     {
5814       strings_processed = cpp_handle_option (pfile, argc - i, argv + i);
5815       if (strings_processed == 0)
5816         break;
5817     }
5818   return i;
5819 }
5820 \f
5821 void
5822 cpp_finish (pfile)
5823      cpp_reader *pfile;
5824 {
5825   struct cpp_options *opts = CPP_OPTIONS (pfile);
5826   
5827   if (opts->print_deps)
5828     {
5829       /* Stream on which to print the dependency information.  */
5830       FILE *deps_stream;
5831
5832       /* Don't actually write the deps file if compilation has failed.  */
5833       if (pfile->errors == 0)
5834         {
5835           char *deps_mode = opts->print_deps_append ? "a" : "w";
5836           if (opts->deps_file == 0)
5837             deps_stream = stdout;
5838           else if ((deps_stream = fopen (opts->deps_file, deps_mode)) == 0)
5839             cpp_pfatal_with_name (pfile, opts->deps_file);
5840           fputs (pfile->deps_buffer, deps_stream);
5841           putc ('\n', deps_stream);
5842           if (opts->deps_file)
5843             {
5844               if (ferror (deps_stream) || fclose (deps_stream) != 0)
5845                 cpp_fatal (pfile, "I/O error on output");
5846             }
5847         }
5848     }
5849
5850 #if 0
5851   /* Debugging: dump statistics on the include hash table. */
5852   {
5853       struct include_hash *x;
5854       int i, j;
5855
5856       for(i = 0; i < ALL_INCLUDE_HASHSIZE; i++)
5857       {
5858           x = pfile->all_include_files[i];
5859           j = 0;
5860           while(x)
5861           {
5862               j++;
5863               x = x->next;
5864           }
5865           fprintf(stderr, "%d/%d ", i, j);
5866       }
5867       fputc('\n', stderr);
5868   }
5869 #endif
5870   
5871 }
5872
5873 /* Free resources used by PFILE.
5874    This is the cpp_reader 'finalizer' or 'destructor' (in C++ terminology).  */
5875
5876 void
5877 cpp_cleanup (pfile)
5878      cpp_reader *pfile;
5879 {
5880   int i;
5881   while ( CPP_BUFFER (pfile) != CPP_NULL_BUFFER (pfile))
5882     cpp_pop_buffer (pfile);
5883
5884   if (pfile->token_buffer)
5885     {
5886       free (pfile->token_buffer);
5887       pfile->token_buffer = NULL;
5888     }
5889
5890   if (pfile->deps_buffer)
5891     {
5892       free (pfile->deps_buffer);
5893       pfile->deps_buffer = NULL;
5894       pfile->deps_allocated_size = 0;
5895     }
5896
5897   while (pfile->if_stack)
5898     {
5899       IF_STACK_FRAME *temp = pfile->if_stack;
5900       pfile->if_stack = temp->next;
5901       free (temp);
5902     }
5903
5904   for (i = ALL_INCLUDE_HASHSIZE; --i >= 0; )
5905     {
5906       struct include_hash *imp = pfile->all_include_files[i];
5907       while (imp)
5908         {
5909           struct include_hash *next = imp->next;
5910 #if 0
5911           /* This gets freed elsewhere - I think. */
5912           free (imp->name);
5913 #endif
5914           free (imp);
5915           imp = next;
5916         }
5917       pfile->all_include_files[i] = 0;
5918     }
5919
5920   cpp_hash_cleanup (pfile);
5921 }
5922 \f
5923 /* Read an assertion into the token buffer, converting to
5924    canonical form: `#predicate(a n swe r)'  The next non-whitespace
5925    character to read should be the first letter of the predicate.
5926    Returns 0 for syntax error, 1 for bare predicate, 2 for predicate
5927    with answer (see callers for why). In case of 0, an error has been
5928    printed. */
5929 static int
5930 parse_assertion (pfile)
5931      cpp_reader *pfile;
5932 {
5933   int c, dropwhite;
5934   cpp_skip_hspace (pfile);
5935   c = PEEKC();
5936   if (! is_idstart[c])
5937     {
5938       cpp_error (pfile, "assertion predicate is not an identifier");
5939       return 0;
5940     }
5941   CPP_PUTC(pfile, '#');
5942   FORWARD(1);
5943   parse_name(pfile, c);
5944
5945   c = PEEKC();
5946   if (c != '(')
5947     {
5948       if (is_hor_space[c])
5949         cpp_skip_hspace (pfile);
5950       c = PEEKC();
5951     }
5952   if (c != '(')
5953     return 1;
5954
5955   CPP_PUTC(pfile, '(');
5956   FORWARD(1);
5957   dropwhite = 1;
5958   while ((c = GETC()) != ')')
5959     {
5960       if (is_hor_space[c])
5961         {
5962           if (! dropwhite)
5963             {
5964               CPP_PUTC(pfile, ' ');
5965               dropwhite = 1;
5966             }
5967         }
5968       else if (c == '\\' && PEEKC() == '\n')
5969         FORWARD(1);
5970       else if (c == '\n' || c == EOF)
5971         {
5972           if (c == '\n') FORWARD(-1);
5973           cpp_error (pfile, "un-terminated assertion answer");
5974           return 0;
5975         }
5976       else
5977         {
5978           CPP_PUTC(pfile, c);
5979           dropwhite = 0;
5980         }
5981     }
5982
5983   if (pfile->limit[-1] == ' ')
5984     pfile->limit[-1] = ')';
5985   else if (pfile->limit[-1] == '(')
5986     {
5987       cpp_error (pfile, "empty token sequence in assertion");
5988       return 0;
5989     }
5990   else
5991     CPP_PUTC(pfile, ')');
5992
5993   CPP_NUL_TERMINATE(pfile);
5994   return 2;
5995 }
5996
5997 static int
5998 do_assert (pfile, keyword)
5999      cpp_reader *pfile;
6000      struct directive *keyword ATTRIBUTE_UNUSED;
6001 {
6002   char *sym;
6003   int ret, c;
6004   HASHNODE *base, *this;
6005   int baselen, thislen;
6006
6007   if (CPP_PEDANTIC (pfile) && CPP_OPTIONS (pfile)->done_initializing
6008       && !CPP_BUFFER (pfile)->system_header_p)
6009     cpp_pedwarn (pfile, "ANSI C does not allow `#assert'");
6010
6011   cpp_skip_hspace (pfile);
6012   sym = CPP_PWRITTEN (pfile);   /* remember where it starts */
6013   ret = parse_assertion (pfile);
6014   if (ret == 0)
6015     goto error;
6016   else if (ret == 1)
6017     {
6018       cpp_error (pfile, "missing token-sequence in `#assert'");
6019       goto error;
6020     }
6021
6022   cpp_skip_hspace (pfile);
6023   c = PEEKC();
6024   if (c != EOF && c != '\n')
6025     {
6026       cpp_error (pfile, "junk at end of `#assert'");
6027       goto error;
6028     }
6029
6030   thislen = strlen (sym);
6031   baselen = index (sym, '(') - sym;
6032   this = cpp_lookup (pfile, sym, thislen, -1);
6033   if (this)
6034     {
6035       cpp_warning (pfile, "`%s' re-asserted", sym);
6036       goto error;
6037     }
6038
6039   base = cpp_lookup (pfile, sym, baselen, -1);
6040   if (! base)
6041     base = install (sym, baselen, T_ASSERT, 0, 0, -1);
6042   else if (base->type != T_ASSERT)
6043   {
6044     /* Token clash - but with what?! */
6045     cpp_fatal (pfile,
6046                "cpp internal error: base->type != T_ASSERT in do_assert");
6047     goto error;
6048   }
6049
6050   this = install (sym, thislen, T_ASSERT, 0,
6051                   (char *)base->value.aschain, -1);
6052   base->value.aschain = this;
6053   
6054   pfile->limit = sym; /* Pop */
6055   return 0;
6056
6057  error:
6058   pfile->limit = sym; /* Pop */
6059   skip_rest_of_line (pfile);
6060   return 1;
6061 }
6062
6063 static int
6064 do_unassert (pfile, keyword)
6065      cpp_reader *pfile;
6066      struct directive *keyword ATTRIBUTE_UNUSED;
6067 {
6068   int c, ret;
6069   char *sym;
6070   long baselen, thislen;
6071   HASHNODE *base, *this, *next;
6072   
6073   if (CPP_PEDANTIC (pfile) && CPP_OPTIONS (pfile)->done_initializing
6074       && !CPP_BUFFER (pfile)->system_header_p)
6075     cpp_pedwarn (pfile, "ANSI C does not allow `#unassert'");
6076
6077   cpp_skip_hspace (pfile);
6078
6079   sym = CPP_PWRITTEN (pfile);   /* remember where it starts */
6080   ret = parse_assertion (pfile);
6081   if (ret == 0)
6082     goto error;
6083   
6084   cpp_skip_hspace (pfile);
6085   c = PEEKC ();
6086   if (c != EOF && c != '\n')
6087       cpp_error (pfile, "junk at end of `#unassert'");
6088
6089   thislen = strlen (sym);
6090   if (ret == 1)
6091     {
6092       base = cpp_lookup (pfile, sym, thislen, -1);
6093       if (! base)
6094         goto error;  /* It isn't an error to #undef what isn't #defined,
6095                         so it isn't an error to #unassert what isn't
6096                         #asserted either. */
6097       
6098       for (this = base->value.aschain; this; this = next)
6099         {
6100           next = this->value.aschain;
6101           delete_macro (this);
6102         }
6103       delete_macro (base);
6104     }
6105   else
6106     {
6107       baselen = index (sym, '(') - sym;
6108       base = cpp_lookup (pfile, sym, baselen, -1);
6109       if (! base) goto error;
6110       this = cpp_lookup (pfile, sym, thislen, -1);
6111       if (! this) goto error;
6112
6113       next = base;
6114       while (next->value.aschain != this)
6115         next = next->value.aschain;
6116
6117       next->value.aschain = this->value.aschain;
6118       delete_macro (this);
6119
6120       if (base->value.aschain == NULL)
6121         delete_macro (base);  /* Last answer for this predicate deleted. */
6122     }
6123   
6124   pfile->limit = sym; /* Pop */
6125   return 0;
6126  error:
6127   pfile->limit = sym; /* Pop */
6128   skip_rest_of_line (pfile);
6129   return 1;
6130 }
6131
6132 int
6133 cpp_read_check_assertion (pfile)
6134      cpp_reader *pfile;
6135 {
6136   char *name = CPP_PWRITTEN (pfile);
6137   int result;
6138   HASHNODE *hp;
6139   
6140   FORWARD (1);  /* Skip '#' */
6141   cpp_skip_hspace (pfile);
6142   if (! parse_assertion (pfile))
6143     result = 0;
6144   else
6145     {
6146       hp = cpp_lookup (pfile, name, (char *)CPP_PWRITTEN (pfile) - name, -1);
6147       result = (hp != 0);
6148     }
6149
6150   pfile->limit = name;
6151   return result;
6152 }
6153
6154 /* Initialize PMARK to remember the current position of PFILE.  */
6155
6156 void
6157 parse_set_mark (pmark, pfile)
6158      struct parse_marker *pmark;
6159      cpp_reader *pfile;
6160 {
6161   cpp_buffer *pbuf = CPP_BUFFER (pfile);
6162   pmark->next = pbuf->marks;
6163   pbuf->marks = pmark;
6164   pmark->buf = pbuf;
6165   pmark->position = pbuf->cur - pbuf->buf;
6166 }
6167
6168 /* Cleanup PMARK - we no longer need it.  */
6169
6170 void
6171 parse_clear_mark (pmark)
6172      struct parse_marker *pmark;
6173 {
6174   struct parse_marker **pp = &pmark->buf->marks;
6175   for (; ; pp = &(*pp)->next) {
6176     if (*pp == NULL) abort ();
6177     if (*pp == pmark) break;
6178   }
6179   *pp = pmark->next;
6180 }
6181
6182 /* Backup the current position of PFILE to that saved in PMARK.  */
6183
6184 void
6185 parse_goto_mark (pmark, pfile)
6186      struct parse_marker *pmark;
6187      cpp_reader *pfile;
6188 {
6189   cpp_buffer *pbuf = CPP_BUFFER (pfile);
6190   if (pbuf != pmark->buf)
6191     cpp_fatal (pfile, "internal error %s", "parse_goto_mark");
6192   pbuf->cur = pbuf->buf + pmark->position;
6193 }
6194
6195 /* Reset PMARK to point to the current position of PFILE.  (Same
6196    as parse_clear_mark (PMARK), parse_set_mark (PMARK, PFILE) but faster.  */
6197
6198 void
6199 parse_move_mark (pmark, pfile)
6200      struct parse_marker *pmark;
6201      cpp_reader *pfile;
6202 {
6203   cpp_buffer *pbuf = CPP_BUFFER (pfile);
6204   if (pbuf != pmark->buf)
6205     cpp_fatal (pfile, "internal error %s", "parse_move_mark");
6206   pmark->position = pbuf->cur - pbuf->buf;
6207 }
6208
6209 \f
6210 void
6211 cpp_print_file_and_line (pfile)
6212      cpp_reader *pfile;
6213 {
6214   cpp_buffer *ip = cpp_file_buffer (pfile);
6215
6216   if (ip != NULL)
6217     {
6218       long line, col;
6219       cpp_buf_line_and_col (ip, &line, &col);
6220       cpp_file_line_for_message (pfile, ip->nominal_fname,
6221                                  line, pfile->show_column ? col : -1);
6222     }
6223 }
6224
6225 static void
6226 v_cpp_error (pfile, msgid, ap)
6227   cpp_reader *pfile;
6228   const char *msgid;
6229   va_list ap;
6230 {
6231   cpp_print_containing_files (pfile);
6232   cpp_print_file_and_line (pfile);
6233   v_cpp_message (pfile, 1, msgid, ap);
6234 }
6235
6236 void
6237 cpp_error VPROTO ((cpp_reader * pfile, const char *msgid, ...))
6238 {
6239 #ifndef ANSI_PROTOTYPES
6240   cpp_reader *pfile;
6241   const char *msgid;
6242 #endif
6243   va_list ap;
6244
6245   VA_START(ap, msgid);
6246   
6247 #ifndef ANSI_PROTOTYPES
6248   pfile = va_arg (ap, cpp_reader *);
6249   msgid = va_arg (ap, const char *);
6250 #endif
6251
6252   v_cpp_error (pfile, msgid, ap);
6253   va_end(ap);
6254 }
6255
6256 /* Print error message but don't count it.  */
6257
6258 static void
6259 v_cpp_warning (pfile, msgid, ap)
6260   cpp_reader *pfile;
6261   const char *msgid;
6262   va_list ap;
6263 {
6264   if (CPP_OPTIONS (pfile)->inhibit_warnings)
6265     return;
6266
6267   if (CPP_OPTIONS (pfile)->warnings_are_errors)
6268     pfile->errors++;
6269
6270   cpp_print_containing_files (pfile);
6271   cpp_print_file_and_line (pfile);
6272   v_cpp_message (pfile, 0, msgid, ap);
6273 }
6274
6275 void
6276 cpp_warning VPROTO ((cpp_reader * pfile, const char *msgid, ...))
6277 {
6278 #ifndef ANSI_PROTOTYPES
6279   cpp_reader *pfile;
6280   const char *msg;
6281 #endif
6282   va_list ap;
6283   
6284   VA_START (ap, msgid);
6285   
6286 #ifndef ANSI_PROTOTYPES
6287   pfile = va_arg (ap, cpp_reader *);
6288   msgid = va_arg (ap, const char *);
6289 #endif
6290
6291   v_cpp_warning (pfile, msgid, ap);
6292   va_end(ap);
6293 }
6294
6295 /* Print an error message and maybe count it.  */
6296
6297 void
6298 cpp_pedwarn VPROTO ((cpp_reader * pfile, const char *msgid, ...))
6299 {
6300 #ifndef ANSI_PROTOTYPES
6301   cpp_reader *pfile;
6302   const char *msgid;
6303 #endif
6304   va_list ap;
6305   
6306   VA_START (ap, msgid);
6307   
6308 #ifndef ANSI_PROTOTYPES
6309   pfile = va_arg (ap, cpp_reader *);
6310   msgid = va_arg (ap, const char *);
6311 #endif
6312
6313   if (CPP_OPTIONS (pfile)->pedantic_errors)
6314     v_cpp_error (pfile, msgid, ap);
6315   else
6316     v_cpp_warning (pfile, msgid, ap);
6317   va_end(ap);
6318 }
6319
6320 static void
6321 v_cpp_error_with_line (pfile, line, column, msgid, ap)
6322   cpp_reader * pfile;
6323   int line;
6324   int column;
6325   const char * msgid;
6326   va_list ap;
6327 {
6328   cpp_buffer *ip = cpp_file_buffer (pfile);
6329
6330   cpp_print_containing_files (pfile);
6331
6332   if (ip != NULL)
6333     cpp_file_line_for_message (pfile, ip->nominal_fname, line, column);
6334
6335   v_cpp_message (pfile, 1, msgid, ap);
6336 }
6337
6338 void
6339 cpp_error_with_line VPROTO ((cpp_reader * pfile, int line, int column,
6340                              const char *msgid, ...))
6341 {
6342 #ifndef ANSI_PROTOTYPES
6343   cpp_reader *pfile;
6344   int line;
6345   int column;
6346   const char *msgid;
6347 #endif
6348   va_list ap;
6349   
6350   VA_START (ap, msgid);
6351   
6352 #ifndef ANSI_PROTOTYPES
6353   pfile = va_arg (ap, cpp_reader *);
6354   line = va_arg (ap, int);
6355   column = va_arg (ap, int);
6356   msgid = va_arg (ap, const char *);
6357 #endif
6358
6359   v_cpp_error_with_line(pfile, line, column, msgid, ap);
6360   va_end(ap);
6361 }
6362
6363 static void
6364 v_cpp_warning_with_line (pfile, line, column, msgid, ap)
6365   cpp_reader * pfile;
6366   int line;
6367   int column;
6368   const char *msgid;
6369   va_list ap;
6370 {
6371   cpp_buffer *ip;
6372
6373   if (CPP_OPTIONS (pfile)->inhibit_warnings)
6374     return;
6375
6376   if (CPP_OPTIONS (pfile)->warnings_are_errors)
6377     pfile->errors++;
6378
6379   cpp_print_containing_files (pfile);
6380
6381   ip = cpp_file_buffer (pfile);
6382
6383   if (ip != NULL)
6384     cpp_file_line_for_message (pfile, ip->nominal_fname, line, column);
6385
6386   v_cpp_message (pfile, 0, msgid, ap);
6387 }  
6388
6389 void
6390 cpp_warning_with_line VPROTO ((cpp_reader * pfile, int line, int column,
6391                                const char *msgid, ...))
6392 {
6393 #ifndef ANSI_PROTOTYPES
6394   cpp_reader *pfile;
6395   int line;
6396   int column;
6397   const char *msgid;
6398 #endif
6399   va_list ap;
6400   
6401   VA_START (ap, msgid);
6402   
6403 #ifndef ANSI_PROTOTYPES
6404   pfile = va_arg (ap, cpp_reader *);
6405   line = va_arg (ap, int);
6406   column = va_arg (ap, int);
6407   msgid = va_arg (ap, const char *);
6408 #endif
6409
6410   v_cpp_warning_with_line (pfile, line, column, msgid, ap);
6411   va_end(ap);
6412 }
6413
6414 void
6415 cpp_pedwarn_with_line VPROTO ((cpp_reader * pfile, int line, int column,
6416                                const char *msgid, ...))
6417 {
6418 #ifndef ANSI_PROTOTYPES
6419   cpp_reader *pfile;
6420   int line;
6421   int column;
6422   const char *msgid;
6423 #endif
6424   va_list ap;
6425   
6426   VA_START (ap, msgid);
6427   
6428 #ifndef ANSI_PROTOTYPES
6429   pfile = va_arg (ap, cpp_reader *);
6430   line = va_arg (ap, int);
6431   column = va_arg (ap, int);
6432   msgid = va_arg (ap, const char *);
6433 #endif
6434
6435   if (CPP_OPTIONS (pfile)->pedantic_errors)
6436     v_cpp_error_with_line (pfile, column, line, msgid, ap);
6437   else
6438     v_cpp_warning_with_line (pfile, line, column, msgid, ap);
6439   va_end(ap);
6440 }
6441
6442 /* Report a warning (or an error if pedantic_errors)
6443    giving specified file name and line number, not current.  */
6444
6445 void
6446 cpp_pedwarn_with_file_and_line VPROTO ((cpp_reader *pfile, char *file, int line,
6447                                         const char *msgid, ...))
6448 {
6449 #ifndef ANSI_PROTOTYPES
6450   cpp_reader *pfile;
6451   char *file;
6452   int line;
6453   const char *msgid;
6454 #endif
6455   va_list ap;
6456   
6457   VA_START (ap, msgid);
6458
6459 #ifndef ANSI_PROTOTYPES
6460   pfile = va_arg (ap, cpp_reader *);
6461   file = va_arg (ap, char *);
6462   line = va_arg (ap, int);
6463   msgid = va_arg (ap, const char *);
6464 #endif
6465
6466   if (!CPP_OPTIONS (pfile)->pedantic_errors
6467       && CPP_OPTIONS (pfile)->inhibit_warnings)
6468     return;
6469   if (file != NULL)
6470     cpp_file_line_for_message (pfile, file, line, -1);
6471   v_cpp_message (pfile, CPP_OPTIONS (pfile)->pedantic_errors, msgid, ap);
6472   va_end(ap);
6473 }
6474
6475 /* my_strerror - return the descriptive text associated with an
6476    `errno' code.  */
6477
6478 static char *
6479 my_strerror (errnum)
6480      int errnum;
6481 {
6482   char *result;
6483
6484 #ifndef VMS
6485 #ifndef HAVE_STRERROR
6486   result = (char *) ((errnum < sys_nerr) ? sys_errlist[errnum] : 0);
6487 #else
6488   result = strerror (errnum);
6489 #endif
6490 #else   /* VMS */
6491   /* VAXCRTL's strerror() takes an optional second argument, which only
6492      matters when the first argument is EVMSERR.  However, it's simplest
6493      just to pass it unconditionally.  `vaxc$errno' is declared in
6494      <errno.h>, and maintained by the library in parallel with `errno'.
6495      We assume that caller's `errnum' either matches the last setting of
6496      `errno' by the library or else does not have the value `EVMSERR'.  */
6497
6498   result = strerror (errnum, vaxc$errno);
6499 #endif
6500
6501   if (!result)
6502     result = "errno = ?";
6503
6504   return result;
6505 }
6506
6507 /* Error including a message from `errno'.  */
6508
6509 void
6510 cpp_error_from_errno (pfile, name)
6511      cpp_reader *pfile;
6512      const char *name;
6513 {
6514   cpp_message_from_errno (pfile, 1, name);
6515 }
6516
6517 void
6518 cpp_message_from_errno (pfile, is_error, name)
6519      cpp_reader *pfile;
6520      int is_error;
6521      const char *name;
6522 {
6523   int e = errno;
6524   cpp_buffer *ip = cpp_file_buffer (pfile);
6525
6526   cpp_print_containing_files (pfile);
6527
6528   if (ip != NULL)
6529     cpp_file_line_for_message (pfile, ip->nominal_fname, ip->lineno, -1);
6530
6531   cpp_message (pfile, is_error, "%s: %s", name, my_strerror (e));
6532 }
6533
6534 void
6535 cpp_perror_with_name (pfile, name)
6536      cpp_reader *pfile;
6537      const char *name;
6538 {
6539   cpp_message (pfile, 1, "%s: %s: %s", progname, name, my_strerror (errno));
6540 }
6541
6542 /* TODO:
6543  * No pre-compiled header file support.
6544  *
6545  * Possibly different enum token codes for each C/C++ token.
6546  *
6547  * Find and cleanup remaining uses of static variables,
6548  *
6549  * Support -dM flag (dump_all_macros).
6550  *
6551  * Support for_lint flag.
6552  */