OSDN Git Service

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