OSDN Git Service

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