OSDN Git Service

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