OSDN Git Service

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