OSDN Git Service

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