OSDN Git Service

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