OSDN Git Service

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