OSDN Git Service

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