OSDN Git Service

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