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