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