OSDN Git Service

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