OSDN Git Service

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