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