OSDN Git Service

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