OSDN Git Service

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