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