Add -fstack-protector and -fno-stack-protector support to GCC. Note
[dragonfly.git] / contrib / gcc / cppinit.c
1 /* CPP Library.
2    Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3    1999, 2000 Free Software Foundation, Inc.
4    Contributed by Per Bothner, 1994-95.
5    Based on CCCP program by Paul Rubin, June 1986
6    Adapted to ANSI C, Richard Stallman, Jan 1987
7
8 This program is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 2, or (at your option) any
11 later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
21
22 #include "config.h"
23 #include "system.h"
24
25 #define FAKE_CONST
26 #include "cpplib.h"
27 #include "cpphash.h"
28 #include "output.h"
29 #include "prefix.h"
30 #include "intl.h"
31
32 /* XXX Should be in a header file. */
33 extern char *version_string;
34
35 /* Predefined symbols, built-in macros, and the default include path. */
36
37 #ifndef GET_ENV_PATH_LIST
38 #define GET_ENV_PATH_LIST(VAR,NAME)     do { (VAR) = getenv (NAME); } while (0)
39 #endif
40
41 /* By default, colon separates directories in a path.  */
42 #ifndef PATH_SEPARATOR
43 #define PATH_SEPARATOR ':'
44 #endif
45
46 #ifndef STANDARD_INCLUDE_DIR
47 #define STANDARD_INCLUDE_DIR "/usr/include"
48 #endif
49
50 /* We let tm.h override the types used here, to handle trivial differences
51    such as the choice of unsigned int or long unsigned int for size_t.
52    When machines start needing nontrivial differences in the size type,
53    it would be best to do something here to figure out automatically
54    from other information what type to use.  */
55
56 /* The string value for __SIZE_TYPE__.  */
57
58 #ifndef SIZE_TYPE
59 #define SIZE_TYPE "long unsigned int"
60 #endif
61
62 /* The string value for __PTRDIFF_TYPE__.  */
63
64 #ifndef PTRDIFF_TYPE
65 #define PTRDIFF_TYPE "long int"
66 #endif
67
68 /* The string value for __WCHAR_TYPE__.  */
69
70 #ifndef WCHAR_TYPE
71 #define WCHAR_TYPE "int"
72 #endif
73 #define CPP_WCHAR_TYPE(PFILE) \
74         (CPP_OPTIONS (PFILE)->cplusplus ? "__wchar_t" : WCHAR_TYPE)
75
76 /* The string value for __USER_LABEL_PREFIX__ */
77
78 #ifndef USER_LABEL_PREFIX
79 #define USER_LABEL_PREFIX ""
80 #endif
81
82 /* The string value for __REGISTER_PREFIX__ */
83
84 #ifndef REGISTER_PREFIX
85 #define REGISTER_PREFIX ""
86 #endif
87
88 /* Suffix for object files, and known input-file extensions. */
89 static char *known_suffixes[] =
90 {
91   ".c",  ".C",   ".s",   ".S",   ".m",
92   ".cc", ".cxx", ".cpp", ".cp",  ".c++",
93   NULL
94 };
95
96 #ifndef OBJECT_SUFFIX
97 # ifdef VMS
98 #  define OBJECT_SUFFIX ".obj"
99 # else
100 #  define OBJECT_SUFFIX ".o"
101 # endif
102 #endif
103
104
105 /* This is the default list of directories to search for include files.
106    It may be overridden by the various -I and -ixxx options.
107
108    #include "file" looks in the same directory as the current file,
109    then this list. 
110    #include <file> just looks in this list.
111
112    All these directories are treated as `system' include directories
113    (they are not subject to pedantic warnings in some cases).  */
114
115 static struct default_include
116 {
117   char *fname;                  /* The name of the directory.  */
118   char *component;              /* The component containing the directory
119                                    (see update_path in prefix.c) */
120   int cplusplus;                /* Only look here if we're compiling C++.  */
121   int cxx_aware;                /* Includes in this directory don't need to
122                                    be wrapped in extern "C" when compiling
123                                    C++.  This is not used anymore.  */
124 }
125 include_defaults_array[]
126 #ifdef INCLUDE_DEFAULTS
127 = INCLUDE_DEFAULTS;
128 #else
129 = {
130     /* Pick up GNU C++ specific include files.  */
131     { GPLUSPLUS_INCLUDE_DIR, "G++", 1, 1 },
132 #ifdef CROSS_COMPILE
133     /* This is the dir for fixincludes.  Put it just before
134        the files that we fix.  */
135     { GCC_INCLUDE_DIR, "GCC", 0, 0 },
136     /* For cross-compilation, this dir name is generated
137        automatically in Makefile.in.  */
138     { CROSS_INCLUDE_DIR, "GCC", 0, 0 },
139 #ifdef TOOL_INCLUDE_DIR
140     /* This is another place that the target system's headers might be.  */
141     { TOOL_INCLUDE_DIR, "BINUTILS", 0, 1 },
142 #endif
143 #else /* not CROSS_COMPILE */
144 #ifdef LOCAL_INCLUDE_DIR
145     /* This should be /usr/local/include and should come before
146        the fixincludes-fixed header files.  */
147     { LOCAL_INCLUDE_DIR, 0, 0, 1 },
148 #endif
149 #ifdef TOOL_INCLUDE_DIR
150     /* This is here ahead of GCC_INCLUDE_DIR because assert.h goes here.
151        Likewise, behind LOCAL_INCLUDE_DIR, where glibc puts its assert.h.  */
152     { TOOL_INCLUDE_DIR, "BINUTILS", 0, 1 },
153 #endif
154     /* This is the dir for fixincludes.  Put it just before
155        the files that we fix.  */
156     { GCC_INCLUDE_DIR, "GCC", 0, 0 },
157     /* Some systems have an extra dir of include files.  */
158 #ifdef SYSTEM_INCLUDE_DIR
159     { SYSTEM_INCLUDE_DIR, 0, 0, 0 },
160 #endif
161 #ifndef STANDARD_INCLUDE_COMPONENT
162 #define STANDARD_INCLUDE_COMPONENT 0
163 #endif
164     { STANDARD_INCLUDE_DIR, STANDARD_INCLUDE_COMPONENT, 0, 0 },
165 #endif /* not CROSS_COMPILE */
166     { 0, 0, 0, 0 }
167   };
168 #endif /* no INCLUDE_DEFAULTS */
169
170 /* Internal structures and prototypes. */
171
172 /* A `struct pending_option' remembers one -D, -A, -U, -include, or -imacros
173    switch.  There are four lists: one for -D and -U, one for -A, one
174    for -include, one for -imacros.  `undef' is set for -U, clear for
175    -D, ignored for the others.
176    (Future: add an equivalent of -U for -A) */
177 struct pending_option
178 {
179   struct pending_option *next;
180   char *arg;
181   int undef;
182 };
183
184 #ifdef __STDC__
185 #define APPEND(pend, list, elt) \
186   do {  if (!(pend)->list##_head) (pend)->list##_head = (elt); \
187         else (pend)->list##_tail->next = (elt); \
188         (pend)->list##_tail = (elt); \
189   } while (0)
190 #else
191 #define APPEND(pend, list, elt) \
192   do {  if (!(pend)->list/**/_head) (pend)->list/**/_head = (elt); \
193         else (pend)->list/**/_tail->next = (elt); \
194         (pend)->list/**/_tail = (elt); \
195   } while (0)
196 #endif
197
198 static void initialize_char_syntax      PARAMS ((int));
199 static void print_help                  PARAMS ((void));
200 static void path_include                PARAMS ((cpp_reader *,
201                                                  struct cpp_pending *,
202                                                  char *, int));
203 static void initialize_builtins         PARAMS ((cpp_reader *));
204 static void append_include_chain        PARAMS ((cpp_reader *,
205                                                  struct cpp_pending *,
206                                                  char *, int));
207
208 /* Last argument to append_include_chain: chain to use */
209 enum { QUOTE = 0, BRACKET, SYSTEM, AFTER };
210
211 /* If gcc is in use (stage2/stage3) we can make these tables initialized
212    data. */
213 #if defined __GNUC__ && (__GNUC__ > 2 \
214                          || (__GNUC__ == 2 && __GNUC_MINOR__ > 8))
215 /* Table to tell if a character is legal as the second or later character
216    of a C identifier. */
217 U_CHAR is_idchar[256] =
218 {
219   ['a'] = 1, ['b'] = 1, ['c'] = 1,  ['d'] = 1, ['e'] = 1, ['f'] = 1,
220   ['g'] = 1, ['h'] = 1, ['i'] = 1,  ['j'] = 1, ['k'] = 1, ['l'] = 1,
221   ['m'] = 1, ['n'] = 1, ['o'] = 1,  ['p'] = 1, ['q'] = 1, ['r'] = 1,
222   ['s'] = 1, ['t'] = 1, ['u'] = 1,  ['v'] = 1, ['w'] = 1, ['x'] = 1,
223   ['y'] = 1, ['z'] = 1,
224
225   ['A'] = 1, ['B'] = 1, ['C'] = 1,  ['D'] = 1, ['E'] = 1, ['F'] = 1,
226   ['G'] = 1, ['H'] = 1, ['I'] = 1,  ['J'] = 1, ['K'] = 1, ['L'] = 1,
227   ['M'] = 1, ['N'] = 1, ['O'] = 1,  ['P'] = 1, ['Q'] = 1, ['R'] = 1,
228   ['S'] = 1, ['T'] = 1, ['U'] = 1,  ['V'] = 1, ['W'] = 1, ['X'] = 1,
229   ['Y'] = 1, ['Z'] = 1,
230
231   ['1'] = 1, ['2'] = 1, ['3'] = 1,  ['4'] = 1, ['5'] = 1, ['6'] = 1,
232   ['7'] = 1, ['8'] = 1, ['9'] = 1,  ['0'] = 1,
233
234   ['_']  = 1,
235 };
236
237 /* Table to tell if a character is legal as the first character of
238    a C identifier. */
239 U_CHAR is_idstart[256] =
240 {
241   ['a'] = 1, ['b'] = 1, ['c'] = 1,  ['d'] = 1, ['e'] = 1, ['f'] = 1,
242   ['g'] = 1, ['h'] = 1, ['i'] = 1,  ['j'] = 1, ['k'] = 1, ['l'] = 1,
243   ['m'] = 1, ['n'] = 1, ['o'] = 1,  ['p'] = 1, ['q'] = 1, ['r'] = 1,
244   ['s'] = 1, ['t'] = 1, ['u'] = 1,  ['v'] = 1, ['w'] = 1, ['x'] = 1,
245   ['y'] = 1, ['z'] = 1,
246
247   ['A'] = 1, ['B'] = 1, ['C'] = 1,  ['D'] = 1, ['E'] = 1, ['F'] = 1,
248   ['G'] = 1, ['H'] = 1, ['I'] = 1,  ['J'] = 1, ['K'] = 1, ['L'] = 1,
249   ['M'] = 1, ['N'] = 1, ['O'] = 1,  ['P'] = 1, ['Q'] = 1, ['R'] = 1,
250   ['S'] = 1, ['T'] = 1, ['U'] = 1,  ['V'] = 1, ['W'] = 1, ['X'] = 1,
251   ['Y'] = 1, ['Z'] = 1,
252
253   ['_']  = 1,
254 };
255
256 /* Table to tell if a character is horizontal space.
257    \r is magical, so it is not in here.  */
258 U_CHAR is_hor_space[256] =
259 {
260   [' '] = 1, ['\t'] = 1, ['\v'] = 1, ['\f'] = 1,
261 };
262 /* table to tell if a character is horizontal or vertical space.  */
263 U_CHAR is_space[256] =
264 {
265   [' '] = 1, ['\t'] = 1, ['\v'] = 1, ['\f'] = 1, ['\n'] = 1,
266 };
267 /* Table to handle trigraph conversion, which occurs before all other
268    processing, everywhere in the file.  (This is necessary since one
269    of the trigraphs encodes backslash.)  Note it's off by default.
270
271         from    to      from    to      from    to
272         ?? =    #       ?? )    ]       ?? !    |
273         ?? (    [       ?? '    ^       ?? >    }
274         ?? /    \       ?? <    {       ?? -    ~
275
276    There is not a space between the ?? and the third char.  I put spaces
277    there to avoid warnings when compiling this file. */
278 U_CHAR trigraph_table[256] =
279 {
280   ['='] = '#',  [')'] = ']',  ['!'] = '|',
281   ['('] = '[',  ['\''] = '^', ['>'] = '}',
282   ['/'] = '\\', ['<'] = '{',  ['-'] = '~',
283 };
284
285 /* This function will be entirely removed soon. */
286 static inline void
287 initialize_char_syntax (dollar_in_ident)
288      int dollar_in_ident;
289 {
290   is_idchar['$'] = dollar_in_ident;
291   is_idstart['$'] = dollar_in_ident;
292 }
293
294 #else /* Not GCC. */
295
296 U_CHAR is_idchar[256] = { 0 };
297 U_CHAR is_idstart[256] = { 0 };
298 U_CHAR is_hor_space[256] = { 0 };
299 U_CHAR is_space[256] = { 0 };
300 U_CHAR trigraph_table[256] = { 0 };
301
302 /* Initialize syntactic classifications of characters. */
303 static void
304 initialize_char_syntax (dollar_in_ident)
305      int dollar_in_ident;
306 {
307   is_idstart['a'] = 1; is_idstart['b'] = 1; is_idstart['c'] = 1;
308   is_idstart['d'] = 1; is_idstart['e'] = 1; is_idstart['f'] = 1;
309   is_idstart['g'] = 1; is_idstart['h'] = 1; is_idstart['i'] = 1;
310   is_idstart['j'] = 1; is_idstart['k'] = 1; is_idstart['l'] = 1;
311   is_idstart['m'] = 1; is_idstart['n'] = 1; is_idstart['o'] = 1;
312   is_idstart['p'] = 1; is_idstart['q'] = 1; is_idstart['r'] = 1;
313   is_idstart['s'] = 1; is_idstart['t'] = 1; is_idstart['u'] = 1;
314   is_idstart['v'] = 1; is_idstart['w'] = 1; is_idstart['x'] = 1;
315   is_idstart['y'] = 1; is_idstart['z'] = 1;
316
317   is_idstart['A'] = 1; is_idstart['B'] = 1; is_idstart['C'] = 1;
318   is_idstart['D'] = 1; is_idstart['E'] = 1; is_idstart['F'] = 1;
319   is_idstart['G'] = 1; is_idstart['H'] = 1; is_idstart['I'] = 1;
320   is_idstart['J'] = 1; is_idstart['K'] = 1; is_idstart['L'] = 1;
321   is_idstart['M'] = 1; is_idstart['N'] = 1; is_idstart['O'] = 1;
322   is_idstart['P'] = 1; is_idstart['Q'] = 1; is_idstart['R'] = 1;
323   is_idstart['S'] = 1; is_idstart['T'] = 1; is_idstart['U'] = 1;
324   is_idstart['V'] = 1; is_idstart['W'] = 1; is_idstart['X'] = 1;
325   is_idstart['Y'] = 1; is_idstart['Z'] = 1;
326
327   is_idstart['_'] = 1;
328
329   is_idchar['a'] = 1; is_idchar['b'] = 1; is_idchar['c'] = 1;
330   is_idchar['d'] = 1; is_idchar['e'] = 1; is_idchar['f'] = 1;
331   is_idchar['g'] = 1; is_idchar['h'] = 1; is_idchar['i'] = 1;
332   is_idchar['j'] = 1; is_idchar['k'] = 1; is_idchar['l'] = 1;
333   is_idchar['m'] = 1; is_idchar['n'] = 1; is_idchar['o'] = 1;
334   is_idchar['p'] = 1;  is_idchar['q'] = 1; is_idchar['r'] = 1;
335   is_idchar['s'] = 1; is_idchar['t'] = 1;  is_idchar['u'] = 1;
336   is_idchar['v'] = 1; is_idchar['w'] = 1; is_idchar['x'] = 1;
337   is_idchar['y'] = 1; is_idchar['z'] = 1;
338
339   is_idchar['A'] = 1; is_idchar['B'] = 1; is_idchar['C'] = 1;
340   is_idchar['D'] = 1; is_idchar['E'] = 1; is_idchar['F'] = 1;
341   is_idchar['G'] = 1; is_idchar['H'] = 1; is_idchar['I'] = 1;
342   is_idchar['J'] = 1; is_idchar['K'] = 1; is_idchar['L'] = 1;
343   is_idchar['M'] = 1; is_idchar['N'] = 1; is_idchar['O'] = 1;
344   is_idchar['P'] = 1; is_idchar['Q'] = 1; is_idchar['R'] = 1;
345   is_idchar['S'] = 1; is_idchar['T'] = 1;  is_idchar['U'] = 1;
346   is_idchar['V'] = 1; is_idchar['W'] = 1; is_idchar['X'] = 1;
347   is_idchar['Y'] = 1; is_idchar['Z'] = 1;
348
349   is_idchar['1'] = 1; is_idchar['2'] = 1; is_idchar['3'] = 1;
350   is_idchar['4'] = 1; is_idchar['5'] = 1; is_idchar['6'] = 1;
351   is_idchar['7'] = 1; is_idchar['8'] = 1; is_idchar['9'] = 1;
352   is_idchar['0'] = 1;
353
354   is_idchar['_']  = 1;
355
356   is_idchar['$']  = dollar_in_ident;
357   is_idstart['$'] = dollar_in_ident;
358
359   /* white space tables */
360   is_hor_space[' '] = 1;
361   is_hor_space['\t'] = 1;
362   is_hor_space['\v'] = 1;
363   is_hor_space['\f'] = 1;
364
365   is_space[' '] = 1;
366   is_space['\t'] = 1;
367   is_space['\v'] = 1;
368   is_space['\f'] = 1;
369   is_space['\n'] = 1;
370
371   /* trigraph conversion */
372   trigraph_table['='] = '#';  trigraph_table[')'] = ']';
373   trigraph_table['!'] = '|';  trigraph_table['('] = '[';
374   trigraph_table['\''] = '^'; trigraph_table['>'] = '}';
375   trigraph_table['/'] = '\\'; trigraph_table['<'] = '{';
376   trigraph_table['-'] = '~';
377 }
378
379 #endif /* Not GCC. */
380
381 /* Given a colon-separated list of file names PATH,
382    add all the names to the search path for include files.  */
383
384 static void
385 path_include (pfile, pend, list, path)
386      cpp_reader *pfile;
387      struct cpp_pending *pend;
388      char *list;
389      int path;
390 {
391   char *p, *q, *name;
392
393   p = list;
394
395   do
396     {
397       /* Find the end of this name.  */
398       q = p;
399       while (*q != 0 && *q != PATH_SEPARATOR) q++;
400       if (q == p)
401         {
402           /* An empty name in the path stands for the current directory.  */
403           name = (char *) xmalloc (2);
404           name[0] = '.';
405           name[1] = 0;
406         }
407       else
408         {
409           /* Otherwise use the directory that is named.  */
410           name = (char *) xmalloc (q - p + 1);
411           memcpy (name, p, q - p);
412           name[q - p] = 0;
413         }
414
415       append_include_chain (pfile, pend, name, path);
416
417       /* Advance past this name.  */
418       if (*q == 0)
419         break;
420       p = q + 1;
421     }
422   while (1);
423 }
424
425 /* Find the base name of a (partial) pathname FNAME.
426    Returns a pointer into the string passed in.
427    Accepts Unix (/-separated) paths on all systems,
428    DOS and VMS paths on those systems.  */
429 static char *
430 base_name (fname)
431      const char *fname;
432 {
433   char *s = (char *)fname;
434   char *p;
435 #if defined (HAVE_DOS_BASED_FILE_SYSTEM)
436   if (ISALPHA (s[0]) && s[1] == ':') s += 2;
437   if ((p = rindex (s, '\\'))) s = p + 1;
438 #elif defined VMS
439   if ((p = rindex (s, ':'))) s = p + 1; /* Skip device.  */
440   if ((p = rindex (s, ']'))) s = p + 1; /* Skip directory.  */
441   if ((p = rindex (s, '>'))) s = p + 1; /* Skip alternate (int'n'l) dir.  */
442 #endif
443   if ((p = rindex (s, '/'))) s = p + 1;
444   return s;
445 }
446      
447
448 /* Append DIR to include path PATH.  DIR must be permanently allocated
449    and writable. */
450 static void
451 append_include_chain (pfile, pend, dir, path)
452      cpp_reader *pfile;
453      struct cpp_pending *pend;
454      char *dir;
455      int path;
456 {
457   struct file_name_list *new;
458   struct stat st;
459   unsigned int len;
460
461   simplify_pathname (dir);
462   if (stat (dir, &st))
463     {
464       /* Dirs that don't exist are silently ignored. */
465       if (errno != ENOENT)
466         cpp_perror_with_name (pfile, dir);
467       else if (CPP_OPTIONS (pfile)->verbose)
468         cpp_notice ("ignoring nonexistent directory `%s'\n", dir);
469       return;
470     }
471
472   if (!S_ISDIR (st.st_mode))
473     {
474       cpp_message (pfile, 1, "%s: %s: Not a directory", progname, dir);
475       return;
476     }
477
478   len = strlen (dir);
479   if (len > pfile->max_include_len)
480     pfile->max_include_len = len;
481   
482   new = (struct file_name_list *)xmalloc (sizeof (struct file_name_list));
483   new->name = dir;
484   new->nlen = len;
485   new->ino  = st.st_ino;
486   new->dev  = st.st_dev;
487   new->sysp = (path == SYSTEM);
488   new->name_map = NULL;
489
490   switch (path)
491     {
492     case QUOTE:         APPEND (pend, quote, new); break;
493     case BRACKET:       APPEND (pend, brack, new); break;
494     case SYSTEM:        APPEND (pend, systm, new); break;
495     case AFTER:         APPEND (pend, after, new); break;
496     }
497 }
498
499
500 /* Write out a #define command for the special named MACRO_NAME
501    to PFILE's token_buffer.  */
502
503 static void
504 dump_special_to_buffer (pfile, macro_name)
505      cpp_reader *pfile;
506      char *macro_name;
507 {
508   static char define_directive[] = "#define ";
509   int macro_name_length = strlen (macro_name);
510   output_line_command (pfile, same_file);
511   CPP_RESERVE (pfile, sizeof(define_directive) + macro_name_length);
512   CPP_PUTS_Q (pfile, define_directive, sizeof(define_directive)-1);
513   CPP_PUTS_Q (pfile, macro_name, macro_name_length);
514   CPP_PUTC_Q (pfile, ' ');
515   cpp_expand_to_buffer (pfile, macro_name, macro_name_length);
516   CPP_PUTC (pfile, '\n');
517 }
518
519 /* Initialize a cpp_options structure. */
520 void
521 cpp_options_init (opts)
522      cpp_options *opts;
523 {
524   bzero ((char *) opts, sizeof (struct cpp_options));
525
526   opts->dollars_in_ident = 1;
527   opts->cplusplus_comments = 1;
528   opts->warn_import = 1;
529
530   opts->pending = (struct cpp_pending *) xmalloc (sizeof (struct cpp_pending));
531   bzero ((char *) opts->pending, sizeof (struct cpp_pending));
532 }
533
534 /* Initialize a cpp_reader structure. */
535 void
536 cpp_reader_init (pfile)
537      cpp_reader *pfile;
538 {
539   bzero ((char *) pfile, sizeof (cpp_reader));
540 #if 0
541   pfile->get_token = cpp_get_token;
542 #endif
543
544   pfile->token_buffer_size = 200;
545   pfile->token_buffer = (U_CHAR *) xmalloc (pfile->token_buffer_size);
546   CPP_SET_WRITTEN (pfile, 0);
547
548   pfile->hashtab = (HASHNODE **) xcalloc (HASHSIZE, sizeof (HASHNODE *));
549 }
550
551 /* Free resources used by PFILE.
552    This is the cpp_reader 'finalizer' or 'destructor' (in C++ terminology).  */
553 void
554 cpp_cleanup (pfile)
555      cpp_reader *pfile;
556 {
557   int i;
558   while (CPP_BUFFER (pfile) != CPP_NULL_BUFFER (pfile))
559     cpp_pop_buffer (pfile);
560
561   if (pfile->token_buffer)
562     {
563       free (pfile->token_buffer);
564       pfile->token_buffer = NULL;
565     }
566
567   if (pfile->deps_buffer)
568     {
569       free (pfile->deps_buffer);
570       pfile->deps_buffer = NULL;
571       pfile->deps_allocated_size = 0;
572     }
573
574   if (pfile->input_buffer)
575     {
576       free (pfile->input_buffer);
577       free (pfile->input_speccase);
578       pfile->input_buffer = pfile->input_speccase = NULL;
579       pfile->input_buffer_len = 0;
580     }
581
582   while (pfile->if_stack)
583     {
584       IF_STACK_FRAME *temp = pfile->if_stack;
585       pfile->if_stack = temp->next;
586       free (temp);
587     }
588
589   for (i = ALL_INCLUDE_HASHSIZE; --i >= 0; )
590     {
591       struct include_hash *imp = pfile->all_include_files[i];
592       while (imp)
593         {
594           struct include_hash *next = imp->next;
595 #if 0
596           /* This gets freed elsewhere - I think. */
597           free (imp->name);
598 #endif
599           free (imp);
600           imp = next;
601         }
602       pfile->all_include_files[i] = 0;
603     }
604
605   for (i = HASHSIZE; --i >= 0;)
606     {
607       while (pfile->hashtab[i])
608         delete_macro (pfile->hashtab[i]);
609     }
610   free (pfile->hashtab);
611 }
612
613
614 /* Initialize the built-in macros.  */
615 static void
616 initialize_builtins (pfile)
617      cpp_reader *pfile;
618 {
619 #define NAME(str) (U_CHAR *)str, sizeof str - 1
620   cpp_install (pfile, NAME("__TIME__"),           T_TIME,       0, -1);
621   cpp_install (pfile, NAME("__DATE__"),           T_DATE,       0, -1);
622   cpp_install (pfile, NAME("__FILE__"),           T_FILE,       0, -1);
623   cpp_install (pfile, NAME("__BASE_FILE__"),      T_BASE_FILE,  0, -1);
624   cpp_install (pfile, NAME("__LINE__"),           T_SPECLINE,   0, -1);
625   cpp_install (pfile, NAME("__INCLUDE_LEVEL__"),  T_INCLUDE_LEVEL, 0, -1);
626   cpp_install (pfile, NAME("__VERSION__"),        T_VERSION,    0, -1);
627 #ifndef NO_BUILTIN_SIZE_TYPE
628   cpp_install (pfile, NAME("__SIZE_TYPE__"),      T_CONST, SIZE_TYPE, -1);
629 #endif
630 #ifndef NO_BUILTIN_PTRDIFF_TYPE
631   cpp_install (pfile, NAME("__PTRDIFF_TYPE__ "),  T_CONST, PTRDIFF_TYPE, -1);
632 #endif
633   cpp_install (pfile, NAME("__WCHAR_TYPE__"),     T_CONST, WCHAR_TYPE, -1);
634   cpp_install (pfile, NAME("__USER_LABEL_PREFIX__"), T_CONST, user_label_prefix, -1);
635   cpp_install (pfile, NAME("__REGISTER_PREFIX__"),  T_CONST, REGISTER_PREFIX, -1);
636   cpp_install (pfile, NAME("__HAVE_BUILTIN_SETJMP__"), T_CONST, "1", -1);
637   if (!CPP_TRADITIONAL (pfile))
638     {
639       cpp_install (pfile, NAME("__STDC__"),       T_STDC,  0, -1);
640 #if 0
641       if (CPP_OPTIONS (pfile)->c9x)
642         cpp_install (pfile, NAME("__STDC_VERSION__"),T_CONST, "199909L", -1);
643       else
644 #endif
645         cpp_install (pfile, NAME("__STDC_VERSION__"),T_CONST, "199409L", -1);
646     }
647 #undef NAME
648
649   if (CPP_OPTIONS (pfile)->debug_output)
650     {
651       dump_special_to_buffer (pfile, "__BASE_FILE__");
652       dump_special_to_buffer (pfile, "__VERSION__");
653 #ifndef NO_BUILTIN_SIZE_TYPE
654       dump_special_to_buffer (pfile, "__SIZE_TYPE__");
655 #endif
656 #ifndef NO_BUILTIN_PTRDIFF_TYPE
657       dump_special_to_buffer (pfile, "__PTRDIFF_TYPE__");
658 #endif
659       dump_special_to_buffer (pfile, "__WCHAR_TYPE__");
660       dump_special_to_buffer (pfile, "__DATE__");
661       dump_special_to_buffer (pfile, "__TIME__");
662       if (!CPP_TRADITIONAL (pfile))
663         dump_special_to_buffer (pfile, "__STDC__");
664     }
665 }
666
667 /* Another subroutine of cpp_start_read.  This one sets up to do
668    dependency-file output. */
669 static void
670 initialize_dependency_output (pfile)
671      cpp_reader *pfile;
672 {
673   cpp_options *opts = CPP_OPTIONS (pfile);
674   char *spec, *s, *output_file;
675   
676   /* Either of two environment variables can specify output of deps.
677      Its value is either "OUTPUT_FILE" or "OUTPUT_FILE DEPS_TARGET",
678      where OUTPUT_FILE is the file to write deps info to
679      and DEPS_TARGET is the target to mention in the deps.  */
680
681   if (opts->print_deps == 0)
682     {
683       spec = getenv ("DEPENDENCIES_OUTPUT");
684       if (spec)
685         opts->print_deps = 1;
686       else
687         {
688           spec = getenv ("SUNPRO_DEPENDENCIES");
689           if (spec)
690             opts->print_deps = 2;
691           else
692             return;
693         }
694
695       /* Find the space before the DEPS_TARGET, if there is one.  */
696       s = strchr (spec, ' ');
697       if (s)
698         {
699           opts->deps_target = s + 1;
700           output_file = (char *) xmalloc (s - spec + 1);
701           memcpy (output_file, spec, s - spec);
702           output_file[s - spec] = 0;
703         }
704       else
705         {
706           opts->deps_target = 0;
707           output_file = spec;
708         }
709
710       opts->deps_file = output_file;
711       opts->print_deps_append = 1;
712     }
713
714   /* Print the expected object file name as the target of this Make-rule.  */
715   pfile->deps_allocated_size = 200;
716   pfile->deps_buffer = (char *) xmalloc (pfile->deps_allocated_size);
717   pfile->deps_buffer[0] = 0;
718   pfile->deps_size = 0;
719   pfile->deps_column = 0;
720
721   if (opts->deps_target)
722     deps_output (pfile, opts->deps_target, ':');
723   else if (*opts->in_fname == 0)
724     deps_output (pfile, "-", ':');
725   else
726     {
727       char *p, *q, *r;
728       int len, x;
729
730       /* Discard all directory prefixes from filename.  */
731       q = base_name (opts->in_fname);
732
733       /* Copy remainder to mungable area.  */
734       len = strlen (q);
735       p = (char *) alloca (len + 8);
736       strcpy (p, q);
737
738       /* Output P, but remove known suffixes.  */
739       q = p + len;
740       /* Point to the filename suffix.  */
741       r = rindex (p, '.');
742       /* Compare against the known suffixes.  */
743       for (x = 0; known_suffixes[x]; x++)
744         {
745           if (strncmp (known_suffixes[x], r, q - r) == 0)
746             {
747               /* Make q point to the bit we're going to overwrite
748                  with an object suffix.  */
749               q = r;
750               break;
751             }
752         }
753
754       /* Supply our own suffix.  */
755       strcpy (q, OBJECT_SUFFIX);
756
757       deps_output (pfile, p, ':');
758       deps_output (pfile, opts->in_fname, ' ');
759     }
760 }
761
762 /* This is called after options have been processed.
763  * Check options for consistency, and setup for processing input
764  * from the file named FNAME.  (Use standard input if FNAME==NULL.)
765  * Return 1 on success, 0 on failure.
766  */
767
768 int
769 cpp_start_read (pfile, fname)
770      cpp_reader *pfile;
771      char *fname;
772 {
773   struct cpp_options *opts = CPP_OPTIONS (pfile);
774   struct pending_option *p, *q;
775   int f;
776   cpp_buffer *fp;
777   struct include_hash *ih_fake;
778
779   /* -MG doesn't select the form of output and must be specified with one of
780      -M or -MM.  -MG doesn't make sense with -MD or -MMD since they don't
781      inhibit compilation.  */
782   if (opts->print_deps_missing_files
783       && (opts->print_deps == 0 || !opts->no_output))
784     {
785       cpp_fatal (pfile, "-MG must be specified with one of -M or -MM");
786       return 0;
787     }
788
789   /* Chill should not be used with -trigraphs. */
790   if (opts->chill && opts->trigraphs)
791     {
792       cpp_warning (pfile, "-lang-chill and -trigraphs are mutually exclusive");
793       opts->trigraphs = 0;
794     }
795
796   /* Set this if it hasn't been set already. */
797   if (user_label_prefix == NULL)
798     user_label_prefix = USER_LABEL_PREFIX;
799   
800   /* Now that we know dollars_in_ident, we can initialize the syntax
801      tables. */
802   initialize_char_syntax (opts->dollars_in_ident);
803
804   /* Do partial setup of input buffer for the sake of generating
805      early #line directives (when -g is in effect).  */
806   fp = cpp_push_buffer (pfile, NULL, 0);
807   if (!fp)
808     return 0;
809   if (opts->in_fname == NULL || *opts->in_fname == 0)
810     {
811       opts->in_fname = fname;
812       if (opts->in_fname == NULL)
813         opts->in_fname = "";
814     }
815   fp->nominal_fname = fp->fname = opts->in_fname;
816   fp->lineno = 0;
817
818   /* Install __LINE__, etc.  Must follow initialize_char_syntax
819      and option processing.  */
820   initialize_builtins (pfile);
821
822   /* Do -U's, -D's and -A's in the order they were seen.  */
823   p = opts->pending->define_head;
824   while (p)
825     {
826       if (opts->debug_output)
827         output_line_command (pfile, same_file);
828       if (p->undef)
829         cpp_undef (pfile, p->arg);
830       else
831         cpp_define (pfile, p->arg);
832
833       q = p->next;
834       free (p);
835       p = q;
836     }
837
838   p = opts->pending->assert_head;
839   while (p)
840     {
841       if (opts->debug_output)
842         output_line_command (pfile, same_file);
843       if (p->undef)
844         cpp_unassert (pfile, p->arg);
845       else
846         cpp_assert (pfile, p->arg);
847
848       q = p->next;
849       free (p);
850       p = q;
851     }
852   
853   opts->done_initializing = 1;
854
855   /* Several environment variables may add to the include search path.
856      CPATH specifies an additional list of directories to be searched
857      as if specified with -I, while C_INCLUDE_PATH, CPLUS_INCLUDE_PATH,
858      etc. specify an additional list of directories to be searched as
859      if specified with -isystem, for the language indicated.
860
861      These variables are ignored if -nostdinc is on.  */
862   if (! opts->no_standard_includes)
863     {
864       char *path;
865       GET_ENV_PATH_LIST (path, "CPATH");
866       if (path != 0 && *path != 0)
867         path_include (pfile, opts->pending, path, BRACKET);
868
869       switch ((opts->objc << 1) + opts->cplusplus)
870         {
871         case 0:
872           GET_ENV_PATH_LIST (path, "C_INCLUDE_PATH");
873           break;
874         case 1:
875           GET_ENV_PATH_LIST (path, "CPLUS_INCLUDE_PATH");
876           break;
877         case 2:
878           GET_ENV_PATH_LIST (path, "OBJC_INCLUDE_PATH");
879           break;
880         case 3:
881           GET_ENV_PATH_LIST (path, "OBJCPLUS_INCLUDE_PATH");
882           break;
883         }
884       if (path != 0 && *path != 0)
885         path_include (pfile, opts->pending, path, SYSTEM);
886     }
887
888   /* Unless -nostdinc, add the compiled-in include path to the list,
889      translating prefixes. */
890   if (!opts->no_standard_includes)
891     {
892       struct default_include *p = include_defaults_array;
893       char *specd_prefix = opts->include_prefix;
894
895       /* Search "translated" versions of GNU directories.
896          These have /usr/local/lib/gcc... replaced by specd_prefix.  */
897       if (specd_prefix != 0)
898         {
899           char *default_prefix = alloca (sizeof GCC_INCLUDE_DIR - 7);
900           /* Remove the `include' from /usr/local/lib/gcc.../include.
901              GCC_INCLUDE_DIR will always end in /include. */
902           int default_len = sizeof GCC_INCLUDE_DIR - 8;
903           int specd_len = strlen (specd_prefix);
904
905           default_len = sizeof GCC_INCLUDE_DIR - 8;
906           memcpy (default_prefix, GCC_INCLUDE_DIR, default_len);
907           default_prefix[default_len] = '\0';
908
909           for (p = include_defaults_array; p->fname; p++)
910             {
911               /* Some standard dirs are only for C++.  */
912               if (!p->cplusplus
913                   || (opts->cplusplus
914                       && !opts->no_standard_cplusplus_includes))
915                 {
916                   /* Does this dir start with the prefix?  */
917                   if (!strncmp (p->fname, default_prefix, default_len))
918                     {
919                       /* Yes; change prefix and add to search list.  */
920                       int flen = strlen (p->fname);
921                       int this_len = specd_len + flen - default_len;
922                       char *str = (char *) xmalloc (this_len + 1);
923                       memcpy (str, specd_prefix, specd_len);
924                       memcpy (str + specd_len,
925                               p->fname + default_len,
926                               flen - default_len + 1);
927
928                       append_include_chain (pfile, opts->pending,
929                                             str, SYSTEM);
930                     }
931                 }
932             }
933         }
934
935       /* Search ordinary names for GNU include directories.  */
936       for (p = include_defaults_array; p->fname; p++)
937         {
938           /* Some standard dirs are only for C++.  */
939           if (!p->cplusplus
940               || (opts->cplusplus
941                   && !opts->no_standard_cplusplus_includes))
942             {
943               char *str = (char *) update_path (p->fname, p->component);
944               str = xstrdup (str);  /* XXX Potential memory leak! */
945               append_include_chain (pfile, opts->pending, str, SYSTEM);
946             }
947         }
948     }
949
950   merge_include_chains (opts);
951
952   /* With -v, print the list of dirs to search.  */
953   if (opts->verbose)
954     {
955       struct file_name_list *p;
956       cpp_message (pfile, -1, "#include \"...\" search starts here:\n");
957       for (p = opts->quote_include; p; p = p->next)
958         {
959           if (p == opts->bracket_include)
960             cpp_message (pfile, -1, "#include <...> search starts here:\n");
961           fprintf (stderr, " %s\n", p->name);
962         }
963       cpp_message (pfile, -1, "End of search list.\n");
964     }
965
966   /* Open the main input file.
967      We do this in nonblocking mode so we don't get stuck here if
968      someone clever has asked cpp to process /dev/rmt0;
969      finclude() will check that we have a real file to work with.  */
970   if (fname == NULL || *fname == 0)
971     {
972       fname = "";
973       f = 0;
974     }
975   else if ((f = open (fname, O_RDONLY|O_NONBLOCK|O_NOCTTY, 0666)) < 0)
976     cpp_pfatal_with_name (pfile, fname);
977
978   initialize_dependency_output (pfile);
979
980   /* Must call finclude() on the main input before processing
981      -include switches; otherwise the -included text winds up
982      after the main input. */
983   ih_fake = (struct include_hash *) xmalloc (sizeof (struct include_hash));
984   ih_fake->next = 0;
985   ih_fake->next_this_file = 0;
986   ih_fake->foundhere = ABSOLUTE_PATH;  /* well sort of ... */
987   ih_fake->name = fname;
988   ih_fake->control_macro = 0;
989   ih_fake->buf = (char *)-1;
990   ih_fake->limit = 0;
991   if (!finclude (pfile, f, ih_fake))
992     return 0;
993   output_line_command (pfile, same_file);
994   pfile->only_seen_white = 2;
995
996   /* The -imacros files can be scanned now, but the -include files
997      have to be pushed onto the include stack and processed later,
998      in the main loop calling cpp_get_token.  */
999   
1000   pfile->no_record_file++;
1001   opts->no_output++;
1002   p = opts->pending->imacros_head;
1003   while (p)
1004     {
1005       int fd = open (p->arg, O_RDONLY|O_NONBLOCK|O_NOCTTY, 0666);
1006       if (fd < 0)
1007         {
1008           cpp_perror_with_name (pfile, p->arg);
1009           return 0;
1010         }
1011       if (!cpp_push_buffer (pfile, NULL, 0))
1012         return 0;
1013
1014       ih_fake = (struct include_hash *)
1015         xmalloc (sizeof (struct include_hash));
1016       ih_fake->next = 0;
1017       ih_fake->next_this_file = 0;
1018       ih_fake->foundhere = ABSOLUTE_PATH;  /* well sort of ... */
1019       ih_fake->name = p->arg;
1020       ih_fake->control_macro = 0;
1021       ih_fake->buf = (char *)-1;
1022       ih_fake->limit = 0;
1023       if (!finclude (pfile, fd, ih_fake))
1024         cpp_scan_buffer (pfile);
1025       free (ih_fake);
1026
1027       q = p->next;
1028       free (p);
1029       p = q;
1030     }
1031
1032   opts->no_output--;
1033
1034   p = opts->pending->include_head;
1035   while (p)
1036     {
1037       int fd = open (p->arg, O_RDONLY|O_NONBLOCK|O_NOCTTY, 0666);
1038       if (fd < 0)
1039         {
1040           cpp_perror_with_name (pfile, p->arg);
1041           return 0;
1042         }
1043       if (!cpp_push_buffer (pfile, NULL, 0))
1044         return 0;
1045
1046       ih_fake = (struct include_hash *)
1047         xmalloc (sizeof (struct include_hash));
1048       ih_fake->next = 0;
1049       ih_fake->next_this_file = 0;
1050       ih_fake->foundhere = ABSOLUTE_PATH;  /* well sort of ... */
1051       ih_fake->name = p->arg;
1052       ih_fake->control_macro = 0;
1053       ih_fake->buf = (char *)-1;
1054       ih_fake->limit = 0;
1055       if (finclude (pfile, fd, ih_fake))
1056         output_line_command (pfile, enter_file);
1057
1058       q = p->next;
1059       free (p);
1060       p = q;
1061     }
1062   pfile->no_record_file--;
1063
1064   free (opts->pending);
1065   opts->pending = NULL;
1066
1067   return 1;
1068 }
1069
1070 /* This is called at the end of preprocessing.  It pops the
1071    last buffer and writes dependency output.  It should also
1072    clear macro definitions, such that you could call cpp_start_read
1073    with a new filename to restart processing. */
1074 void
1075 cpp_finish (pfile)
1076      cpp_reader *pfile;
1077 {
1078   struct cpp_options *opts = CPP_OPTIONS (pfile);
1079
1080   if (CPP_PREV_BUFFER (CPP_BUFFER (pfile)) != CPP_NULL_BUFFER (pfile))
1081     cpp_fatal (pfile,
1082                "cpplib internal error: buffers still stacked in cpp_finish");
1083   cpp_pop_buffer (pfile);
1084   
1085   if (opts->print_deps)
1086     {
1087       /* Stream on which to print the dependency information.  */
1088       FILE *deps_stream;
1089
1090       /* Don't actually write the deps file if compilation has failed.  */
1091       if (pfile->errors == 0)
1092         {
1093           char *deps_mode = opts->print_deps_append ? "a" : "w";
1094           if (opts->deps_file == 0)
1095             deps_stream = stdout;
1096           else if ((deps_stream = fopen (opts->deps_file, deps_mode)) == 0)
1097             cpp_pfatal_with_name (pfile, opts->deps_file);
1098           fputs (pfile->deps_buffer, deps_stream);
1099           putc ('\n', deps_stream);
1100           if (opts->deps_file)
1101             {
1102               if (ferror (deps_stream) || fclose (deps_stream) != 0)
1103                 cpp_fatal (pfile, "I/O error on output");
1104             }
1105         }
1106     }
1107
1108   if (opts->dump_macros == dump_only)
1109     {
1110       int i;
1111       HASHNODE *h;
1112       MACRODEF m;
1113       for (i = HASHSIZE; --i >= 0;)
1114         {
1115           for (h = pfile->hashtab[i]; h; h = h->next)
1116             if (h->type == T_MACRO)
1117               {
1118                 m.defn = h->value.defn;
1119                 m.symnam = h->name;
1120                 m.symlen = h->length;
1121                 dump_definition (pfile, m);
1122                 CPP_PUTC (pfile, '\n');
1123               }
1124         }
1125     }
1126 }
1127
1128 /* Handle one command-line option in (argc, argv).
1129    Can be called multiple times, to handle multiple sets of options.
1130    Returns number of strings consumed.  */
1131 int
1132 cpp_handle_option (pfile, argc, argv)
1133      cpp_reader *pfile;
1134      int argc;
1135      char **argv;
1136 {
1137   struct cpp_options *opts = CPP_OPTIONS (pfile);
1138   int i = 0;
1139
1140   if (argv[i][0] != '-')
1141     {
1142       if (opts->out_fname != NULL)
1143         {
1144           print_help ();
1145           cpp_fatal (pfile, "Too many arguments");
1146         }
1147       else if (opts->in_fname != NULL)
1148         opts->out_fname = argv[i];
1149       else
1150         opts->in_fname = argv[i];
1151     }
1152   else
1153     switch (argv[i][1])
1154       {
1155       case 'f':
1156         if (!strcmp (argv[i], "-fleading-underscore"))
1157           user_label_prefix = "_";
1158         else if (!strcmp (argv[i], "-fno-leading-underscore"))
1159           user_label_prefix = "";
1160         break;
1161
1162       case 'I':                 /* Add directory to path for includes.  */
1163         if (!strcmp (argv[i] + 2, "-"))
1164           {
1165             /* -I- means:
1166                Use the preceding -I directories for #include "..."
1167                but not #include <...>.
1168                Don't search the directory of the present file
1169                for #include "...".  (Note that -I. -I- is not the same as
1170                the default setup; -I. uses the compiler's working dir.)  */
1171             if (! opts->ignore_srcdir)
1172               {
1173                 opts->ignore_srcdir = 1;
1174                 opts->pending->quote_head = opts->pending->brack_head;
1175                 opts->pending->quote_tail = opts->pending->brack_tail;
1176                 opts->pending->brack_head = 0;
1177                 opts->pending->brack_tail = 0;
1178               }
1179             else
1180               {
1181                 cpp_fatal (pfile, "-I- specified twice");
1182                 return argc;
1183               }
1184           }
1185         else
1186           {
1187             char *fname;
1188             if (argv[i][2] != 0)
1189               fname = argv[i] + 2;
1190             else if (i + 1 == argc)
1191               goto missing_dirname;
1192             else
1193               fname = argv[++i];
1194             append_include_chain (pfile, opts->pending,
1195                                   xstrdup (fname), BRACKET);
1196           }
1197         break;
1198
1199       case 'i':
1200         /* Add directory to beginning of system include path, as a system
1201            include directory. */
1202         if (!strcmp (argv[i], "-isystem"))
1203           {
1204             if (i + 1 == argc)
1205               goto missing_filename;
1206             append_include_chain (pfile, opts->pending,
1207                                   xstrdup (argv[++i]), SYSTEM);
1208           }
1209         else if (!strcmp (argv[i], "-include"))
1210           {
1211             if (i + 1 == argc)
1212               goto missing_filename;
1213             else
1214               {
1215                 struct pending_option *o = (struct pending_option *)
1216                   xmalloc (sizeof (struct pending_option));
1217                 o->arg = argv[++i];
1218
1219                 /* This list has to be built in reverse order so that
1220                    when cpp_start_read pushes all the -include files onto
1221                    the buffer stack, they will be scanned in forward order.  */
1222                 o->next = opts->pending->include_head;
1223                 opts->pending->include_head = o;
1224               }
1225           }
1226         else if (!strcmp (argv[i], "-imacros"))
1227           {
1228             if (i + 1 == argc)
1229               goto missing_filename;
1230             else
1231               {
1232                 struct pending_option *o = (struct pending_option *)
1233                   xmalloc (sizeof (struct pending_option));
1234                 o->arg = argv[++i];
1235                 o->next = NULL;
1236
1237                 APPEND (opts->pending, imacros, o);
1238               }
1239           }
1240         /* Add directory to end of path for includes,
1241            with the default prefix at the front of its name.  */
1242         else if (!strcmp (argv[i], "-iwithprefix"))
1243           {
1244             char *fname;
1245             int len;
1246             if (i + 1 == argc)
1247               goto missing_dirname;
1248             ++i;
1249             len = strlen (argv[i]);
1250
1251             if (opts->include_prefix != 0)
1252               {
1253                 fname = xmalloc (opts->include_prefix_len + len + 1);
1254                 memcpy (fname, opts->include_prefix, opts->include_prefix_len);
1255                 memcpy (fname + opts->include_prefix_len, argv[i], len + 1);
1256               }
1257             else
1258               {
1259                 fname = xmalloc (sizeof GCC_INCLUDE_DIR - 8 + len);
1260                 memcpy (fname, GCC_INCLUDE_DIR, sizeof GCC_INCLUDE_DIR - 9);
1261                 memcpy (fname + sizeof GCC_INCLUDE_DIR - 9, argv[i], len + 1);
1262               }
1263           
1264             append_include_chain (pfile, opts->pending, fname, SYSTEM);
1265           }
1266         /* Add directory to main path for includes,
1267            with the default prefix at the front of its name.  */
1268         else if (!strcmp (argv[i], "-iwithprefixbefore"))
1269           {
1270             char *fname;
1271             int len;
1272             if (i + 1 == argc)
1273               goto missing_dirname;
1274             ++i;
1275             len = strlen (argv[i]);
1276
1277             if (opts->include_prefix != 0)
1278               {
1279                 fname = xmalloc (opts->include_prefix_len + len + 1);
1280                 memcpy (fname, opts->include_prefix, opts->include_prefix_len);
1281                 memcpy (fname + opts->include_prefix_len, argv[i], len + 1);
1282               }
1283             else
1284               {
1285                 fname = xmalloc (sizeof GCC_INCLUDE_DIR - 8 + len);
1286                 memcpy (fname, GCC_INCLUDE_DIR, sizeof GCC_INCLUDE_DIR - 9);
1287                 memcpy (fname + sizeof GCC_INCLUDE_DIR - 9, argv[i], len + 1);
1288               }
1289           
1290             append_include_chain (pfile, opts->pending, fname, BRACKET);
1291           }
1292         /* Add directory to end of path for includes.  */
1293         else if (!strcmp (argv[i], "-idirafter"))
1294           {
1295             if (i + 1 == argc)
1296               goto missing_dirname;
1297             append_include_chain (pfile, opts->pending,
1298                                   xstrdup (argv[++i]), AFTER);
1299           }
1300         else if (!strcmp (argv[i], "-iprefix"))
1301           {
1302             if (i + 1 == argc)
1303               goto missing_filename;
1304             else
1305               {
1306                 opts->include_prefix = argv[++i];
1307                 opts->include_prefix_len = strlen (argv[i]);
1308               }
1309           }
1310         else if (!strcmp (argv[i], "-ifoutput"))
1311           opts->output_conditionals = 1;
1312
1313         break;
1314       
1315       case 'o':
1316         if (opts->out_fname != NULL)
1317           {
1318             cpp_fatal (pfile, "Output filename specified twice");
1319             return argc;
1320           }
1321         if (i + 1 == argc)
1322           goto missing_filename;
1323         opts->out_fname = argv[++i];
1324         if (!strcmp (opts->out_fname, "-"))
1325           opts->out_fname = "";
1326         break;
1327       
1328       case 'p':
1329         if (!strcmp (argv[i], "-pedantic"))
1330           CPP_PEDANTIC (pfile) = 1;
1331         else if (!strcmp (argv[i], "-pedantic-errors"))
1332           {
1333             CPP_PEDANTIC (pfile) = 1;
1334             opts->pedantic_errors = 1;
1335           }
1336 #if 0
1337         else if (!strcmp (argv[i], "-pcp")) {
1338           char *pcp_fname = argv[++i];
1339           pcp_outfile = ((pcp_fname[0] != '-' || pcp_fname[1] != '\0')
1340                          ? fopen (pcp_fname, "w")
1341                          : fdopen (dup (fileno (stdout)), "w"));
1342           if (pcp_outfile == 0)
1343             cpp_pfatal_with_name (pfile, pcp_fname);
1344           no_precomp = 1;
1345         }
1346 #endif
1347         break;
1348       
1349       case 't':
1350         if (!strcmp (argv[i], "-traditional"))
1351           {
1352             opts->traditional = 1;
1353             opts->cplusplus_comments = 0;
1354           }
1355         else if (!strcmp (argv[i], "-trigraphs"))
1356           opts->trigraphs = 1;
1357         break;
1358       
1359       case 'l':
1360         if (! strcmp (argv[i], "-lang-c"))
1361           opts->cplusplus = 0, opts->cplusplus_comments = 1, opts->c89 = 0,
1362             opts->c9x = 1, opts->objc = 0;
1363         if (! strcmp (argv[i], "-lang-c89"))
1364           opts->cplusplus = 0, opts->cplusplus_comments = 0, opts->c89 = 1,
1365             opts->c9x = 0, opts->objc = 0;
1366         if (! strcmp (argv[i], "-lang-c++"))
1367           opts->cplusplus = 1, opts->cplusplus_comments = 1, opts->c89 = 0,
1368             opts->c9x = 0, opts->objc = 0;
1369         if (! strcmp (argv[i], "-lang-objc"))
1370           opts->cplusplus = 0, opts->cplusplus_comments = 1, opts->c89 = 0,
1371             opts->c9x = 0, opts->objc = 1;
1372         if (! strcmp (argv[i], "-lang-objc++"))
1373           opts->cplusplus = 1, opts->cplusplus_comments = 1, opts->c89 = 0,
1374             opts->c9x = 0, opts->objc = 1;
1375         if (! strcmp (argv[i], "-lang-asm"))
1376           opts->lang_asm = 1;
1377         if (! strcmp (argv[i], "-lint"))
1378           opts->for_lint = 1;
1379         if (! strcmp (argv[i], "-lang-chill"))
1380           opts->objc = 0, opts->cplusplus = 0, opts->chill = 1,
1381             opts->traditional = 1;
1382         break;
1383       
1384       case '+':
1385         opts->cplusplus = 1, opts->cplusplus_comments = 1;
1386         break;
1387
1388       case 's':
1389         if (!strcmp (argv[i], "-std=iso9899:1990")
1390             || !strcmp (argv[i], "-std=iso9899:199409")
1391             || !strcmp (argv[i], "-std=c89")
1392             || !strcmp (argv[i], "-std=gnu89"))
1393           opts->cplusplus = 0, opts->cplusplus_comments = 0,
1394             opts->c89 = 1, opts->c9x = 0, opts->objc = 0;
1395         else if (!strcmp (argv[i], "-std=iso9899:199x")
1396                  || !strcmp (argv[i], "-std=c9x")
1397                  || !strcmp (argv[i], "-std=gnu9x"))
1398           opts->cplusplus = 0, opts->cplusplus_comments = 1, opts->c89 = 0,
1399             opts->c9x = 1, opts->objc = 0;
1400         break;
1401
1402       case 'w':
1403         opts->inhibit_warnings = 1;
1404         break;
1405       
1406       case 'W':
1407         if (!strcmp (argv[i], "-Wtrigraphs"))
1408           opts->warn_trigraphs = 1;
1409         else if (!strcmp (argv[i], "-Wno-trigraphs"))
1410           opts->warn_trigraphs = 0;
1411         else if (!strcmp (argv[i], "-Wcomment"))
1412           opts->warn_comments = 1;
1413         else if (!strcmp (argv[i], "-Wno-comment"))
1414           opts->warn_comments = 0;
1415         else if (!strcmp (argv[i], "-Wcomments"))
1416           opts->warn_comments = 1;
1417         else if (!strcmp (argv[i], "-Wno-comments"))
1418           opts->warn_comments = 0;
1419         else if (!strcmp (argv[i], "-Wtraditional"))
1420           opts->warn_stringify = 1;
1421         else if (!strcmp (argv[i], "-Wno-traditional"))
1422           opts->warn_stringify = 0;
1423         else if (!strcmp (argv[i], "-Wundef"))
1424           opts->warn_undef = 1;
1425         else if (!strcmp (argv[i], "-Wno-undef"))
1426           opts->warn_undef = 0;
1427         else if (!strcmp (argv[i], "-Wimport"))
1428           opts->warn_import = 1;
1429         else if (!strcmp (argv[i], "-Wno-import"))
1430           opts->warn_import = 0;
1431         else if (!strcmp (argv[i], "-Werror"))
1432           opts->warnings_are_errors = 1;
1433         else if (!strcmp (argv[i], "-Wno-error"))
1434           opts->warnings_are_errors = 0;
1435         else if (!strcmp (argv[i], "-Wall"))
1436           {
1437             opts->warn_trigraphs = 1;
1438             opts->warn_comments = 1;
1439           }
1440         break;
1441       
1442       case 'M':
1443         /* The style of the choices here is a bit mixed.
1444            The chosen scheme is a hybrid of keeping all options in one string
1445            and specifying each option in a separate argument:
1446            -M|-MM|-MD file|-MMD file [-MG].  An alternative is:
1447            -M|-MM|-MD file|-MMD file|-MG|-MMG; or more concisely:
1448            -M[M][G][D file].  This is awkward to handle in specs, and is not
1449            as extensible.  */
1450         /* ??? -MG must be specified in addition to one of -M or -MM.
1451            This can be relaxed in the future without breaking anything.
1452            The converse isn't true.  */
1453       
1454         /* -MG isn't valid with -MD or -MMD.  This is checked for later.  */
1455         if (!strcmp (argv[i], "-MG"))
1456           {
1457             opts->print_deps_missing_files = 1;
1458             break;
1459           }
1460         if (!strcmp (argv[i], "-M"))
1461           opts->print_deps = 2;
1462         else if (!strcmp (argv[i], "-MM"))
1463           opts->print_deps = 1;
1464         else if (!strcmp (argv[i], "-MD"))
1465           opts->print_deps = 2;
1466         else if (!strcmp (argv[i], "-MMD"))
1467           opts->print_deps = 1;
1468         /* For -MD and -MMD options, write deps on file named by next arg.  */
1469         if (!strcmp (argv[i], "-MD") || !strcmp (argv[i], "-MMD"))
1470           {
1471             if (i+1 == argc)
1472               goto missing_filename;
1473             opts->deps_file = argv[++i];
1474           }
1475         else
1476           {
1477             /* For -M and -MM, write deps on standard output
1478                and suppress the usual output.  */
1479             opts->no_output = 1;
1480           }       
1481         break;
1482       
1483       case 'd':
1484         {
1485           char *p = argv[i] + 2;
1486           char c;
1487           while ((c = *p++) != 0)
1488             {
1489               /* Arg to -d specifies what parts of macros to dump */
1490               switch (c)
1491                 {
1492                 case 'M':
1493                   opts->dump_macros = dump_only;
1494                   opts->no_output = 1;
1495                   break;
1496                 case 'N':
1497                   opts->dump_macros = dump_names;
1498                   break;
1499                 case 'D':
1500                   opts->dump_macros = dump_definitions;
1501                   break;
1502                 case 'I':
1503                   opts->dump_includes = 1;
1504                   break;
1505                 }
1506             }
1507         }
1508         break;
1509     
1510       case 'g':
1511         if (argv[i][2] == '3')
1512           opts->debug_output = 1;
1513         break;
1514       
1515       case '-':
1516         if (!strcmp (argv[i], "--help"))
1517           print_help ();
1518         else if (!strcmp (argv[i], "--version"))
1519           cpp_notice ("GNU CPP version %s\n", version_string);
1520         exit (0);  /* XXX */
1521         break;
1522         
1523       case 'v':
1524         cpp_notice ("GNU CPP version %s", version_string);
1525 #ifdef TARGET_VERSION
1526         TARGET_VERSION;
1527 #endif
1528         fputc ('\n', stderr);
1529         opts->verbose = 1;
1530         break;
1531       
1532       case 'H':
1533         opts->print_include_names = 1;
1534         break;
1535       
1536       case 'D':
1537         {
1538           struct pending_option *o = (struct pending_option *)
1539             xmalloc (sizeof (struct pending_option));
1540           if (argv[i][2] != 0)
1541             o->arg = argv[i] + 2;
1542           else if (i + 1 == argc)
1543             {
1544               cpp_fatal (pfile, "Macro name missing after -D option");
1545               return argc;
1546             }
1547           else
1548             o->arg = argv[++i];
1549
1550           o->next = NULL;
1551           o->undef = 0;
1552           APPEND (opts->pending, define, o);
1553         }
1554         break;
1555       
1556       case 'A':
1557         {
1558           char *p;
1559         
1560           if (argv[i][2] != 0)
1561             p = argv[i] + 2;
1562           else if (i + 1 == argc)
1563             {
1564               cpp_fatal (pfile, "Assertion missing after -A option");
1565               return argc;
1566             }
1567           else
1568             p = argv[++i];
1569         
1570           if (strcmp (p, "-"))
1571             {
1572               struct pending_option *o = (struct pending_option *)
1573                 xmalloc (sizeof (struct pending_option));
1574
1575               o->arg = p;
1576               o->next = NULL;
1577               o->undef = 0;
1578               APPEND (opts->pending, assert, o);
1579             }
1580           else
1581             {
1582               /* -A- eliminates all predefined macros and assertions.
1583                  Let's include also any that were specified earlier
1584                  on the command line.  That way we can get rid of any
1585                  that were passed automatically in from GCC.  */
1586               struct pending_option *o1, *o2;
1587
1588               o1 = opts->pending->define_head;
1589               while (o1)
1590                 {
1591                   o2 = o1->next;
1592                   free (o1);
1593                   o1 = o2;
1594                 }
1595               o1 = opts->pending->assert_head;
1596               while (o1)
1597                 {
1598                   o2 = o1->next;
1599                   free (o1);
1600                   o1 = o2;
1601                 }
1602               opts->pending->assert_head = NULL;
1603               opts->pending->assert_tail = NULL;
1604               opts->pending->define_head = NULL;
1605               opts->pending->define_tail = NULL;
1606             }
1607         }
1608         break;
1609     
1610       case 'U':
1611         {
1612           struct pending_option *o = (struct pending_option *)
1613             xmalloc (sizeof (struct pending_option));
1614           
1615           if (argv[i][2] != 0)
1616             o->arg = argv[i] + 2;
1617           else if (i + 1 == argc)
1618             {
1619               cpp_fatal (pfile, "Macro name missing after -U option");
1620               return argc;
1621             }
1622           else
1623             o->arg = argv[++i];
1624
1625           o->next = NULL;
1626           o->undef = 1;
1627           APPEND (opts->pending, define, o);
1628         }
1629         break;
1630       
1631       case 'C':
1632         opts->put_out_comments = 1;
1633         break;
1634       
1635       case 'E':                 /* -E comes from cc -E; ignore it.  */
1636         break;
1637       
1638       case 'P':
1639         opts->no_line_commands = 1;
1640         break;
1641       
1642       case '$':                 /* Don't include $ in identifiers.  */
1643         opts->dollars_in_ident = 0;
1644         break;
1645       
1646       case 'n':
1647         if (!strcmp (argv[i], "-nostdinc"))
1648           /* -nostdinc causes no default include directories.
1649              You must specify all include-file directories with -I.  */
1650           opts->no_standard_includes = 1;
1651         else if (!strcmp (argv[i], "-nostdinc++"))
1652           /* -nostdinc++ causes no default C++-specific include directories. */
1653           opts->no_standard_cplusplus_includes = 1;
1654 #if 0
1655         else if (!strcmp (argv[i], "-noprecomp"))
1656           no_precomp = 1;
1657 #endif
1658         break;
1659       
1660       case 'r':
1661         if (!strcmp (argv[i], "-remap"))
1662           opts->remap = 1;
1663         break;
1664       
1665       case '\0': /* JF handle '-' as file name meaning stdin or stdout */
1666         if (opts->in_fname == NULL)
1667           opts->in_fname = "";
1668         else if (opts->out_fname == NULL)
1669           opts->out_fname = "";
1670         else
1671           return i;  /* error */
1672         break;
1673
1674       default:
1675         return i;
1676       }
1677
1678   return i + 1;
1679
1680  missing_filename:
1681   cpp_fatal (pfile, "Filename missing after `%s' option", argv[i]);
1682   return argc;
1683  missing_dirname:
1684   cpp_fatal (pfile, "Directory name missing after `%s' option", argv[i]);
1685   return argc;
1686 }
1687
1688 /* Handle command-line options in (argc, argv).
1689    Can be called multiple times, to handle multiple sets of options.
1690    Returns if an unrecognized option is seen.
1691    Returns number of strings consumed.  */
1692
1693 int
1694 cpp_handle_options (pfile, argc, argv)
1695      cpp_reader *pfile;
1696      int argc;
1697      char **argv;
1698 {
1699   int i;
1700   int strings_processed;
1701   for (i = 0; i < argc; i += strings_processed)
1702     {
1703       strings_processed = cpp_handle_option (pfile, argc - i, argv + i);
1704       if (strings_processed == 0)
1705         break;
1706     }
1707   return i;
1708 }
1709
1710 static void
1711 print_help ()
1712 {
1713   cpp_notice ("Usage: %s [switches] input output\n", progname);
1714   fputs (_("\
1715 Switches:\n\
1716   -include <file>           Include the contents of <file> before other files\n\
1717   -imacros <file>           Accept definition of macros in <file>\n\
1718   -iprefix <path>           Specify <path> as a prefix for next two options\n\
1719   -iwithprefix <dir>        Add <dir> to the end of the system include path\n\
1720   -iwithprefixbefore <dir>  Add <dir> to the end of the main include path\n\
1721   -isystem <dir>            Add <dir> to the start of the system include path\n\
1722   -idirafter <dir>          Add <dir> to the end of the system include path\n\
1723   -I <dir>                  Add <dir> to the end of the main include path\n\
1724   -nostdinc                 Do not search system include directories\n\
1725                              (dirs specified with -isystem will still be used)\n\
1726   -nostdinc++               Do not search system include directories for C++\n\
1727   -o <file>                 Put output into <file>\n\
1728   -pedantic                 Issue all warnings demanded by strict ANSI C\n\
1729   -traditional              Follow K&R pre-processor behaviour\n\
1730   -trigraphs                Support ANSI C trigraphs\n\
1731   -lang-c                   Assume that the input sources are in C\n\
1732   -lang-c89                 Assume that the input sources are in C89\n\
1733   -lang-c++                 Assume that the input sources are in C++\n\
1734   -lang-objc                Assume that the input sources are in ObjectiveC\n\
1735   -lang-objc++              Assume that the input sources are in ObjectiveC++\n\
1736   -lang-asm                 Assume that the input sources are in assembler\n\
1737   -lang-chill               Assume that the input sources are in Chill\n\
1738   -std=<std name>           Specify the conformance standard; one of:\n\
1739                             gnu89, gnu9x, c89, c9x, iso9899:1990,\n\
1740                             iso9899:199409, iso9899:199x\n\
1741   -+                        Allow parsing of C++ style features\n\
1742   -w                        Inhibit warning messages\n\
1743   -Wtrigraphs               Warn if trigraphs are encountered\n\
1744   -Wno-trigraphs            Do not warn about trigraphs\n\
1745   -Wcomment{s}              Warn if one comment starts inside another\n\
1746   -Wno-comment{s}           Do not warn about comments\n\
1747   -Wtraditional             Warn if a macro argument is/would be turned into\n\
1748                              a string if -traditional is specified\n\
1749   -Wno-traditional          Do not warn about stringification\n\
1750   -Wundef                   Warn if an undefined macro is used by #if\n\
1751   -Wno-undef                Do not warn about testing undefined macros\n\
1752   -Wimport                  Warn about the use of the #import directive\n\
1753   -Wno-import               Do not warn about the use of #import\n\
1754   -Werror                   Treat all warnings as errors\n\
1755   -Wno-error                Do not treat warnings as errors\n\
1756   -Wall                     Enable all preprocessor warnings\n\
1757   -M                        Generate make dependencies\n\
1758   -MM                       As -M, but ignore system header files\n\
1759   -MD                       As -M, but put output in a .d file\n\
1760   -MMD                      As -MD, but ignore system header files\n\
1761   -MG                       Treat missing header file as generated files\n\
1762   -g                        Include #define and #undef directives in the output\n\
1763   -D<macro>                 Define a <macro> with string '1' as its value\n\
1764   -D<macro>=<val>           Define a <macro> with <val> as its value\n\
1765   -A<question> (<answer>)   Assert the <answer> to <question>\n\
1766   -U<macro>                 Undefine <macro> \n\
1767   -v                        Display the version number\n\
1768   -H                        Print the name of header files as they are used\n\
1769   -C                        Do not discard comments\n\
1770   -dM                       Display a list of macro definitions active at end\n\
1771   -dD                       Preserve macro definitions in output\n\
1772   -dN                       As -dD except that only the names are preserved\n\
1773   -dI                       Include #include directives in the output\n\
1774   -ifoutput                 Describe skipped code blocks in output \n\
1775   -P                        Do not generate #line directives\n\
1776   -$                        Do not allow '$' in identifiers\n\
1777   -remap                    Remap file names when including files.\n\
1778   -h or --help              Display this information\n\
1779 "), stdout);
1780 }