OSDN Git Service

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