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