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