OSDN Git Service

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