OSDN Git Service

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