OSDN Git Service

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