* support/cpp2/cpplex.c:
[fw/sdcc] / support / cpp2 / cppinit.c
1 /* CPP Library.
2    Copyright (C) 1986, 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3    1999, 2000, 2001, 2002 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 "prefix.h"
27 #include "intl.h"
28 #include "version.h"
29 #include "mkdeps.h"
30 #include "cppdefault.h"
31 #include "except.h"     /* for USING_SJLJ_EXCEPTIONS */
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 # define INO_T_COPY(DEST, SRC) memcpy(&(DEST), &(SRC), sizeof (SRC))
45 #else
46 # if (defined _WIN32 && ! defined (_UWIN)) || defined __MSDOS__
47 #  define INO_T_EQ(A, B) 0
48 # else
49 #  define INO_T_EQ(A, B) ((A) == (B))
50 # endif
51 # define INO_T_COPY(DEST, SRC) (DEST) = (SRC)
52 #endif
53
54 /* Internal structures and prototypes.  */
55
56 /* A `struct pending_option' remembers one -D, -A, -U, -include, or
57    -imacros switch.  */
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_read_main_file.  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 search_path *quote_head, *quote_tail;
75   struct search_path *brack_head, *brack_tail;
76   struct search_path *systm_head, *systm_tail;
77   struct search_path *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                                                  char *, int));
100 static void init_library                PARAMS ((void));
101 static void init_builtins               PARAMS ((cpp_reader *));
102 static void mark_named_operators        PARAMS ((cpp_reader *));
103 static void append_include_chain        PARAMS ((cpp_reader *,
104                                                  char *, int, int));
105 static struct search_path * remove_dup_dir      PARAMS ((cpp_reader *,
106                                                  struct search_path *,
107                                                  struct search_path **));
108 static struct search_path * remove_dup_nonsys_dirs PARAMS ((cpp_reader *,
109                                                  struct search_path **,
110                                                  struct search_path *));
111 static struct search_path * remove_dup_dirs PARAMS ((cpp_reader *,
112                                                  struct search_path **));
113 static void merge_include_chains        PARAMS ((cpp_reader *));
114 static bool push_include                PARAMS ((cpp_reader *,
115                                                  struct pending_option *));
116 static void free_chain                  PARAMS ((struct pending_option *));
117 static void set_lang                    PARAMS ((cpp_reader *, enum c_lang));
118 static void init_dependency_output      PARAMS ((cpp_reader *));
119 static void init_standard_includes      PARAMS ((cpp_reader *));
120 static void read_original_filename      PARAMS ((cpp_reader *));
121 static void new_pending_directive       PARAMS ((struct cpp_pending *,
122                                                  const char *,
123                                                  cl_directive_handler));
124 static void output_deps                 PARAMS ((cpp_reader *));
125 static int parse_option                 PARAMS ((const char *));
126
127 /* Fourth argument to append_include_chain: chain to use.
128    Note it's never asked to append to the quote chain.  */
129 enum { BRACKET = 0, SYSTEM, AFTER };
130
131 /* If we have designated initializers (GCC >2.7) these tables can be
132    initialized, constant data.  Otherwise, they have to be filled in at
133    runtime.  */
134 #if HAVE_DESIGNATED_INITIALIZERS
135
136 #define init_trigraph_map()  /* Nothing.  */
137 #define TRIGRAPH_MAP \
138 __extension__ const U_CHAR _cpp_trigraph_map[UCHAR_MAX + 1] = {
139
140 #define END };
141 #define s(p, v) [p] = v,
142
143 #else
144
145 #define TRIGRAPH_MAP U_CHAR _cpp_trigraph_map[UCHAR_MAX + 1] = { 0 }; \
146  static void init_trigraph_map PARAMS ((void)) { \
147  unsigned char *x = _cpp_trigraph_map;
148
149 #define END }
150 #define s(p, v) x[p] = v;
151
152 #endif
153
154 TRIGRAPH_MAP
155   s('=', '#')   s(')', ']')     s('!', '|')
156   s('(', '[')   s('\'', '^')    s('>', '}')
157   s('/', '\\')  s('<', '{')     s('-', '~')
158 END
159
160 #undef s
161 #undef END
162 #undef TRIGRAPH_MAP
163
164 /* Given a colon-separated list of file names PATH,
165    add all the names to the search path for include files.  */
166 static void
167 path_include (pfile, list, path)
168      cpp_reader *pfile;
169      char *list;
170      int path;
171 {
172   char *p, *q, *name;
173
174   p = list;
175
176   do
177     {
178       /* Find the end of this name.  */
179       q = p;
180       while (*q != 0 && *q != PATH_SEPARATOR) q++;
181       if (q == p)
182         {
183           /* An empty name in the path stands for the current directory.  */
184           name = (char *) xmalloc (2);
185           name[0] = '.';
186           name[1] = 0;
187         }
188       else
189         {
190           /* Otherwise use the directory that is named.  */
191           name = (char *) xmalloc (q - p + 1);
192           memcpy (name, p, q - p);
193           name[q - p] = 0;
194         }
195
196       append_include_chain (pfile, name, path, path == SYSTEM);
197
198       /* Advance past this name.  */
199       if (*q == 0)
200         break;
201       p = q + 1;
202     }
203   while (1);
204 }
205
206 /* Append DIR to include path PATH.  DIR must be allocated on the
207    heap; this routine takes responsibility for freeing it.  CXX_AWARE
208    is non-zero if the header contains extern "C" guards for C++,
209    otherwise it is zero.  */
210 static void
211 append_include_chain (pfile, dir, path, cxx_aware)
212      cpp_reader *pfile;
213      char *dir;
214      int path;
215      int cxx_aware ATTRIBUTE_UNUSED;
216 {
217   struct cpp_pending *pend = CPP_OPTION (pfile, pending);
218   struct search_path *new;
219   struct stat st;
220   unsigned int len;
221
222   if (*dir == '\0')
223     {
224       free (dir);
225       dir = xstrdup (".");
226     }
227   _cpp_simplify_pathname (dir);
228
229   if (stat (dir, &st))
230     {
231       /* Dirs that don't exist are silently ignored.  */
232       if (errno != ENOENT)
233         cpp_notice_from_errno (pfile, dir);
234       else if (CPP_OPTION (pfile, verbose))
235         fprintf (stderr, _("ignoring nonexistent directory \"%s\"\n"), dir);
236       free (dir);
237       return;
238     }
239
240   if (!S_ISDIR (st.st_mode))
241     {
242       cpp_notice (pfile, "%s: Not a directory", dir);
243       free (dir);
244       return;
245     }
246
247   len = strlen (dir);
248   if (len > pfile->max_include_len)
249     pfile->max_include_len = len;
250
251   new = (struct search_path *) xmalloc (sizeof (struct search_path));
252   new->name = dir;
253   new->len = len;
254   INO_T_COPY (new->ino, st.st_ino);
255   new->dev  = st.st_dev;
256   /* Both systm and after include file lists should be treated as system
257      include files since these two lists are really just a concatenation
258      of one "system" list.  */
259   if (path == SYSTEM || path == AFTER)
260 #ifdef NO_IMPLICIT_EXTERN_C
261     new->sysp = 1;
262 #else
263     new->sysp = cxx_aware ? 1 : 2;
264 #endif
265   else
266     new->sysp = 0;
267   new->name_map = NULL;
268   new->next = NULL;
269
270   switch (path)
271     {
272     case BRACKET:       APPEND (pend, brack, new); break;
273     case SYSTEM:        APPEND (pend, systm, new); break;
274     case AFTER:         APPEND (pend, after, new); break;
275     }
276 }
277
278 /* Handle a duplicated include path.  PREV is the link in the chain
279    before the duplicate, or NULL if the duplicate is at the head of
280    the chain.  The duplicate is removed from the chain and freed.
281    Returns PREV.  */
282 static struct search_path *
283 remove_dup_dir (pfile, prev, head_ptr)
284      cpp_reader *pfile;
285      struct search_path *prev;
286      struct search_path **head_ptr;
287 {
288   struct search_path *cur;
289
290   if (prev != NULL)
291     {
292       cur = prev->next;
293       prev->next = cur->next;
294     }
295   else
296     {
297       cur = *head_ptr;
298       *head_ptr = cur->next;
299     }
300
301   if (CPP_OPTION (pfile, verbose))
302     fprintf (stderr, _("ignoring duplicate directory \"%s\"\n"), cur->name);
303
304   free ((PTR) cur->name);
305   free (cur);
306
307   return prev;
308 }
309
310 /* Remove duplicate non-system directories for which there is an equivalent
311    system directory later in the chain.  The range for removal is between
312    *HEAD_PTR and END.  Returns the directory before END, or NULL if none.
313    This algorithm is quadratic in the number of system directories, which is
314    acceptable since there aren't usually that many of them.  */
315 static struct search_path *
316 remove_dup_nonsys_dirs (pfile, head_ptr, end)
317      cpp_reader *pfile;
318      struct search_path **head_ptr;
319      struct search_path *end;
320 {
321   int sysdir = 0;
322   struct search_path *prev = NULL, *cur, *other;
323
324   for (cur = *head_ptr; cur; cur = cur->next)
325     {
326       if (cur->sysp)
327         {
328           sysdir = 1;
329           for (other = *head_ptr, prev = NULL;
330                other != end;
331                other = other ? other->next : *head_ptr)
332             {
333               if (!other->sysp
334                   && INO_T_EQ (cur->ino, other->ino)
335                   && cur->dev == other->dev)
336                 {
337                   other = remove_dup_dir (pfile, prev, head_ptr);
338                   if (CPP_OPTION (pfile, verbose))
339                     fprintf (stderr,
340   _("  as it is a non-system directory that duplicates a system directory\n"));
341                 }
342               prev = other;
343             }
344         }
345     }
346
347   if (!sysdir)
348     for (cur = *head_ptr; cur != end; cur = cur->next)
349       prev = cur;
350
351   return prev;
352 }
353
354 /* Remove duplicate directories from a chain.  Returns the tail of the
355    chain, or NULL if the chain is empty.  This algorithm is quadratic
356    in the number of -I switches, which is acceptable since there
357    aren't usually that many of them.  */
358 static struct search_path *
359 remove_dup_dirs (pfile, head_ptr)
360      cpp_reader *pfile;
361      struct search_path **head_ptr;
362 {
363   struct search_path *prev = NULL, *cur, *other;
364
365   for (cur = *head_ptr; cur; cur = cur->next)
366     {
367       for (other = *head_ptr; other != cur; other = other->next)
368         if (INO_T_EQ (cur->ino, other->ino) && cur->dev == other->dev)
369           {
370             cur = remove_dup_dir (pfile, prev, head_ptr);
371             break;
372           }
373       prev = cur;
374     }
375
376   return prev;
377 }
378
379 /* Merge the four include chains together in the order quote, bracket,
380    system, after.  Remove duplicate dirs (as determined by
381    INO_T_EQ()).  The system_include and after_include chains are never
382    referred to again after this function; all access is through the
383    bracket_include path.  */
384 static void
385 merge_include_chains (pfile)
386      cpp_reader *pfile;
387 {
388   struct search_path *quote, *brack, *systm, *qtail;
389
390   struct cpp_pending *pend = CPP_OPTION (pfile, pending);
391
392   quote = pend->quote_head;
393   brack = pend->brack_head;
394   systm = pend->systm_head;
395   qtail = pend->quote_tail;
396
397   /* Paste together bracket, system, and after include chains.  */
398   if (systm)
399     pend->systm_tail->next = pend->after_head;
400   else
401     systm = pend->after_head;
402
403   if (brack)
404     pend->brack_tail->next = systm;
405   else
406     brack = systm;
407
408   /* This is a bit tricky.  First we drop non-system dupes of system
409      directories from the merged bracket-include list.  Next we drop
410      dupes from the bracket and quote include lists.  Then we drop
411      non-system dupes from the merged quote-include list.  Finally,
412      if qtail and brack are the same directory, we cut out brack and
413      move brack up to point to qtail.
414
415      We can't just merge the lists and then uniquify them because
416      then we may lose directories from the <> search path that should
417      be there; consider -Ifoo -Ibar -I- -Ifoo -Iquux.  It is however
418      safe to treat -Ibar -Ifoo -I- -Ifoo -Iquux as if written
419      -Ibar -I- -Ifoo -Iquux.  */
420
421   remove_dup_nonsys_dirs (pfile, &brack, systm);
422   remove_dup_dirs (pfile, &brack);
423
424   if (quote)
425     {
426       qtail = remove_dup_dirs (pfile, &quote);
427       qtail->next = brack;
428
429       qtail = remove_dup_nonsys_dirs (pfile, &quote, brack);
430
431       /* If brack == qtail, remove brack as it's simpler.  */
432       if (qtail && brack && INO_T_EQ (qtail->ino, brack->ino)
433           && qtail->dev == brack->dev)
434         brack = remove_dup_dir (pfile, qtail, &quote);
435     }
436   else
437     quote = brack;
438
439   CPP_OPTION (pfile, quote_include) = quote;
440   CPP_OPTION (pfile, bracket_include) = brack;
441 }
442
443 /* A set of booleans indicating what CPP features each source language
444    requires.  */
445 struct lang_flags
446 {
447   char c99;
448   char objc;
449   char cplusplus;
450   char extended_numbers;
451   char std;
452   char dollars_in_ident;
453   char cplusplus_comments;
454   char digraphs;
455 };
456
457 /* ??? Enable $ in identifiers in assembly? */
458 static const struct lang_flags lang_defaults[] =
459 { /*              c99 objc c++ xnum std dollar c++comm digr  */
460   /* GNUC89 */  { 0,  0,   0,  1,   0,   1,     1,      1     },
461   /* GNUC99 */  { 1,  0,   0,  1,   0,   1,     1,      1     },
462   /* STDC89 */  { 0,  0,   0,  0,   1,   0,     0,      0     },
463   /* STDC94 */  { 0,  0,   0,  0,   1,   0,     0,      1     },
464   /* STDC99 */  { 1,  0,   0,  1,   1,   0,     1,      1     },
465   /* GNUCXX */  { 0,  0,   1,  1,   0,   1,     1,      1     },
466   /* CXX98  */  { 0,  0,   1,  1,   1,   0,     1,      1     },
467   /* OBJC   */  { 0,  1,   0,  1,   0,   1,     1,      1     },
468   /* OBJCXX */  { 0,  1,   1,  1,   0,   1,     1,      1     },
469   /* ASM    */  { 0,  0,   0,  1,   0,   0,     1,      0     }
470 };
471
472 /* Sets internal flags correctly for a given language.  */
473 static void
474 set_lang (pfile, lang)
475      cpp_reader *pfile;
476      enum c_lang lang;
477 {
478   const struct lang_flags *l = &lang_defaults[(int) lang];
479   
480   CPP_OPTION (pfile, lang) = lang;
481
482   CPP_OPTION (pfile, c99)                = l->c99;
483   CPP_OPTION (pfile, objc)               = l->objc;
484   CPP_OPTION (pfile, cplusplus)          = l->cplusplus;
485   CPP_OPTION (pfile, extended_numbers)   = l->extended_numbers;
486   CPP_OPTION (pfile, std)                = l->std;
487   CPP_OPTION (pfile, trigraphs)          = l->std;
488   CPP_OPTION (pfile, dollars_in_ident)   = l->dollars_in_ident;
489   CPP_OPTION (pfile, cplusplus_comments) = l->cplusplus_comments;
490   CPP_OPTION (pfile, digraphs)           = l->digraphs;
491 }
492
493 #ifdef HOST_EBCDIC
494 static int opt_comp PARAMS ((const void *, const void *));
495
496 /* Run-time sorting of options array.  */
497 static int
498 opt_comp (p1, p2)
499      const void *p1, *p2;
500 {
501   return strcmp (((struct cl_option *) p1)->opt_text,
502                  ((struct cl_option *) p2)->opt_text);
503 }
504 #endif
505
506 /* init initializes library global state.  It might not need to
507    do anything depending on the platform and compiler.  */
508 static void
509 init_library ()
510 {
511   static int initialized = 0;
512
513   if (! initialized)
514     {
515       initialized = 1;
516
517 #ifdef HOST_EBCDIC
518       /* For non-ASCII hosts, the cl_options array needs to be sorted at
519          runtime.  */
520       qsort (cl_options, N_OPTS, sizeof (struct cl_option), opt_comp);
521 #endif
522
523       /* Set up the trigraph map.  This doesn't need to do anything if
524          we were compiled with a compiler that supports C99 designated
525          initializers.  */
526       init_trigraph_map ();
527     }
528 }
529
530 /* Initialize a cpp_reader structure.  */
531 cpp_reader *
532 cpp_create_reader (lang)
533      enum c_lang lang;
534 {
535   cpp_reader *pfile;
536
537   /* Initialise this instance of the library if it hasn't been already.  */
538   init_library ();
539
540   pfile = (cpp_reader *) xcalloc (1, sizeof (cpp_reader));
541
542   set_lang (pfile, lang);
543   CPP_OPTION (pfile, warn_import) = 1;
544   CPP_OPTION (pfile, discard_comments) = 1;
545   CPP_OPTION (pfile, show_column) = 1;
546   CPP_OPTION (pfile, tabstop) = 8;
547   CPP_OPTION (pfile, operator_names) = 1;
548 #if DEFAULT_SIGNED_CHAR
549   CPP_OPTION (pfile, signed_char) = 1;
550 #else
551   CPP_OPTION (pfile, signed_char) = 0;
552 #endif
553   /* SDCC _asm specific */
554   CPP_OPTION (pfile, preproc_asm) = 1;
555
556   CPP_OPTION (pfile, pending) =
557     (struct cpp_pending *) xcalloc (1, sizeof (struct cpp_pending));
558
559   /* It's simplest to just create this struct whether or not it will
560      be needed.  */
561   pfile->deps = deps_init ();
562
563   /* Initialise the line map.  Start at logical line 1, so we can use
564      a line number of zero for special states.  */
565   init_line_maps (&pfile->line_maps);
566   pfile->line = 1;
567
568   /* Initialize lexer state.  */
569   pfile->state.save_comments = ! CPP_OPTION (pfile, discard_comments);
570
571   /* Set up static tokens.  */
572   pfile->date.type = CPP_EOF;
573   pfile->avoid_paste.type = CPP_PADDING;
574   pfile->avoid_paste.val.source = NULL;
575   pfile->eof.type = CPP_EOF;
576   pfile->eof.flags = 0;
577
578   /* Create a token buffer for the lexer.  */
579   _cpp_init_tokenrun (&pfile->base_run, 250);
580   pfile->cur_run = &pfile->base_run;
581   pfile->cur_token = pfile->base_run.base;
582
583   /* Initialise the base context.  */
584   pfile->context = &pfile->base_context;
585   pfile->base_context.macro = 0;
586   pfile->base_context.prev = pfile->base_context.next = 0;
587
588   /* Aligned and unaligned storage.  */
589   pfile->a_buff = _cpp_get_buff (pfile, 0);
590   pfile->u_buff = _cpp_get_buff (pfile, 0);
591
592   /* Initialise the buffer obstack.  */
593   gcc_obstack_init (&pfile->buffer_ob);
594
595   _cpp_init_includes (pfile);
596
597   return pfile;
598 }
599
600 /* Free resources used by PFILE.  Accessing PFILE after this function
601    returns leads to undefined behaviour.  Returns the error count.  */
602 int
603 cpp_destroy (pfile)
604      cpp_reader *pfile;
605 {
606   int result;
607   struct search_path *dir, *dirn;
608   cpp_context *context, *contextn;
609   tokenrun *run, *runn;
610
611   while (CPP_BUFFER (pfile) != NULL)
612     _cpp_pop_buffer (pfile);
613
614   if (pfile->macro_buffer)
615     {
616       free ((PTR) pfile->macro_buffer);
617       pfile->macro_buffer = NULL;
618       pfile->macro_buffer_len = 0;
619     }
620
621   deps_free (pfile->deps);
622   obstack_free (&pfile->buffer_ob, 0);
623
624   _cpp_destroy_hashtable (pfile);
625   _cpp_cleanup_includes (pfile);
626
627   _cpp_free_buff (pfile->a_buff);
628   _cpp_free_buff (pfile->u_buff);
629   _cpp_free_buff (pfile->free_buffs);
630
631   for (run = &pfile->base_run; run; run = runn)
632     {
633       runn = run->next;
634       free (run->base);
635       if (run != &pfile->base_run)
636         free (run);
637     }
638
639   for (dir = CPP_OPTION (pfile, quote_include); dir; dir = dirn)
640     {
641       dirn = dir->next;
642       free ((PTR) dir->name);
643       free (dir);
644     }
645
646   for (context = pfile->base_context.next; context; context = contextn)
647     {
648       contextn = context->next;
649       free (context);
650     }
651
652   free_line_maps (&pfile->line_maps);
653
654   result = pfile->errors;
655   free (pfile);
656
657   return result;
658 }
659
660
661 /* This structure defines one built-in identifier.  A node will be
662    entered in the hash table under the name NAME, with value VALUE (if
663    any).  If flags has OPERATOR, the node's operator field is used; if
664    flags has BUILTIN the node's builtin field is used.  Macros that are
665    known at build time should not be flagged BUILTIN, as then they do
666    not appear in macro dumps with e.g. -dM or -dD.
667
668    Two values are not compile time constants, so we tag
669    them in the FLAGS field instead:
670    VERS         value is the global version_string, quoted
671    ULP          value is the global user_label_prefix  */
672 struct builtin
673 {
674   const U_CHAR *name;
675   const char *value;
676   unsigned char builtin;
677   unsigned short flags;
678   unsigned short len;
679 };
680 #define VERS            0x01
681 #define ULP             0x02
682 #define BUILTIN         0x08
683
684 #define B(n, t)       { U n, 0, t, BUILTIN, sizeof n - 1 }
685 #define C(n, v)       { U n, v, 0, 0, sizeof n - 1 }
686 #define X(n, f)       { U n, 0, 0, f, sizeof n - 1 }
687 static const struct builtin builtin_array[] =
688 {
689   B("__TIME__",          BT_TIME),
690   B("__DATE__",          BT_DATE),
691   B("__FILE__",          BT_FILE),
692   B("__BASE_FILE__",     BT_BASE_FILE),
693   B("__LINE__",          BT_SPECLINE),
694   B("__INCLUDE_LEVEL__", BT_INCLUDE_LEVEL),
695   B("_Pragma",           BT_PRAGMA),
696
697   X("__VERSION__",              VERS),
698   X("__USER_LABEL_PREFIX__",    ULP),
699   C("__REGISTER_PREFIX__",      REGISTER_PREFIX),
700   C("__HAVE_BUILTIN_SETJMP__",  "1"),
701 #if USING_SJLJ_EXCEPTIONS
702   /* libgcc needs to know this.  */
703   C("__USING_SJLJ_EXCEPTIONS__","1"),
704 #endif
705 #ifndef NO_BUILTIN_SIZE_TYPE
706   C("__SIZE_TYPE__",            SIZE_TYPE),
707 #endif
708 #ifndef NO_BUILTIN_PTRDIFF_TYPE
709   C("__PTRDIFF_TYPE__",         PTRDIFF_TYPE),
710 #endif
711 #ifndef NO_BUILTIN_WCHAR_TYPE
712   C("__WCHAR_TYPE__",           WCHAR_TYPE),
713 #endif
714 #ifndef NO_BUILTIN_WINT_TYPE
715   C("__WINT_TYPE__",            WINT_TYPE),
716 #endif
717 #ifdef STDC_0_IN_SYSTEM_HEADERS
718   B("__STDC__",          BT_STDC),
719 #else
720   C("__STDC__",          "1"),
721 #endif
722 };
723 #undef B
724 #undef C
725 #undef X
726 #define builtin_array_end \
727  builtin_array + sizeof(builtin_array)/sizeof(struct builtin)
728
729 /* Named operators known to the preprocessor.  These cannot be
730    #defined and always have their stated meaning.  They are treated
731    like normal identifiers except for the type code and the meaning.
732    Most of them are only for C++ (but see iso646.h).  */
733 #define B(n, t)    { DSC(n), t }
734 static const struct named_op
735 {
736   const U_CHAR *name;
737   unsigned int len;
738   enum cpp_ttype value;
739 } operator_array[] = {
740   B("and",      CPP_AND_AND),
741   B("and_eq",   CPP_AND_EQ),
742   B("bitand",   CPP_AND),
743   B("bitor",    CPP_OR),
744   B("compl",    CPP_COMPL),
745   B("not",      CPP_NOT),
746   B("not_eq",   CPP_NOT_EQ),
747   B("or",       CPP_OR_OR),
748   B("or_eq",    CPP_OR_EQ),
749   B("xor",      CPP_XOR),
750   B("xor_eq",   CPP_XOR_EQ)
751 };
752 #undef B
753
754 /* Mark the C++ named operators in the hash table.  */
755 static void
756 mark_named_operators (pfile)
757      cpp_reader *pfile;
758 {
759   const struct named_op *b;
760
761   for (b = operator_array;
762        b < (operator_array + ARRAY_SIZE (operator_array));
763        b++)
764     {
765       cpp_hashnode *hp = cpp_lookup (pfile, b->name, b->len);
766       hp->flags |= NODE_OPERATOR;
767       hp->value.operator = b->value;
768     }
769 }
770
771 /* Subroutine of cpp_read_main_file; reads the builtins table above and
772    enters them, and language-specific macros, into the hash table.  */
773 static void
774 init_builtins (pfile)
775      cpp_reader *pfile;
776 {
777   const struct builtin *b;
778
779   for(b = builtin_array; b < builtin_array_end; b++)
780     {
781       if (b->flags & BUILTIN)
782         {
783           cpp_hashnode *hp = cpp_lookup (pfile, b->name, b->len);
784           hp->type = NT_MACRO;
785           hp->flags |= NODE_BUILTIN | NODE_WARN;
786           hp->value.builtin = b->builtin;
787         }
788       else                      /* A standard macro of some kind.  */
789         {
790           const char *val;
791           char *str;
792
793           if (b->flags & VERS)
794             {
795               /* Allocate enough space for 'name "value"\n\0'.  */
796               str = alloca (b->len + strlen (version_string) + 5);
797               sprintf (str, "%s \"%s\"\n", b->name, version_string);
798             }
799           else
800             {
801               if (b->flags & ULP)
802                 val = CPP_OPTION (pfile, user_label_prefix);
803               else
804                 val = b->value;
805
806               /* Allocate enough space for "name value\n\0".  */
807               str = alloca (b->len + strlen (val) + 3);
808               sprintf(str, "%s %s\n", b->name, val);
809             }
810
811           _cpp_define_builtin (pfile, str);
812         }
813     }
814
815   if (CPP_OPTION (pfile, cplusplus))
816     {
817       _cpp_define_builtin (pfile, "__cplusplus 1");
818       if (SUPPORTS_ONE_ONLY)
819         _cpp_define_builtin (pfile, "__GXX_WEAK__ 1");
820       else
821         _cpp_define_builtin (pfile, "__GXX_WEAK__ 0");
822     }
823   if (CPP_OPTION (pfile, objc))
824     _cpp_define_builtin (pfile, "__OBJC__ 1");
825
826   if (CPP_OPTION (pfile, lang) == CLK_STDC94)
827     _cpp_define_builtin (pfile, "__STDC_VERSION__ 199409L");
828   else if (CPP_OPTION (pfile, c99))
829     _cpp_define_builtin (pfile, "__STDC_VERSION__ 199901L");
830
831   if (CPP_OPTION (pfile, signed_char) == 0)
832     _cpp_define_builtin (pfile, "__CHAR_UNSIGNED__ 1");
833
834   if (CPP_OPTION (pfile, lang) == CLK_STDC89
835       || CPP_OPTION (pfile, lang) == CLK_STDC94
836       || CPP_OPTION (pfile, lang) == CLK_STDC99)
837     _cpp_define_builtin (pfile, "__STRICT_ANSI__ 1");
838   else if (CPP_OPTION (pfile, lang) == CLK_ASM)
839     _cpp_define_builtin (pfile, "__ASSEMBLER__ 1");
840 }
841 #undef BUILTIN
842 #undef OPERATOR
843 #undef VERS
844 #undef ULP
845 #undef builtin_array_end
846
847 /* And another subroutine.  This one sets up the standard include path.  */
848 static void
849 init_standard_includes (pfile)
850      cpp_reader *pfile;
851 {
852   char *path;
853   const struct default_include *p;
854   const char *specd_prefix = CPP_OPTION (pfile, include_prefix);
855
856   /* Several environment variables may add to the include search path.
857      CPATH specifies an additional list of directories to be searched
858      as if specified with -I, while C_INCLUDE_PATH, CPLUS_INCLUDE_PATH,
859      etc. specify an additional list of directories to be searched as
860      if specified with -isystem, for the language indicated.  */
861
862   GET_ENV_PATH_LIST (path, "CPATH");
863   if (path != 0 && *path != 0)
864     path_include (pfile, path, BRACKET);
865
866   switch ((CPP_OPTION (pfile, objc) << 1) + CPP_OPTION (pfile, cplusplus))
867     {
868     case 0:
869       GET_ENV_PATH_LIST (path, "C_INCLUDE_PATH");
870       break;
871     case 1:
872       GET_ENV_PATH_LIST (path, "CPLUS_INCLUDE_PATH");
873       break;
874     case 2:
875       GET_ENV_PATH_LIST (path, "OBJC_INCLUDE_PATH");
876       break;
877     case 3:
878       GET_ENV_PATH_LIST (path, "OBJCPLUS_INCLUDE_PATH");
879       break;
880     }
881   if (path != 0 && *path != 0)
882     path_include (pfile, path, SYSTEM);
883
884   /* Search "translated" versions of GNU directories.
885      These have /usr/local/lib/gcc... replaced by specd_prefix.  */
886   if (specd_prefix != 0 && cpp_GCC_INCLUDE_DIR_len)
887     {
888       /* Remove the `include' from /usr/local/lib/gcc.../include.
889          GCC_INCLUDE_DIR will always end in /include.  */
890       int default_len = cpp_GCC_INCLUDE_DIR_len;
891       char *default_prefix = (char *) alloca (default_len + 1);
892       int specd_len = strlen (specd_prefix);
893
894       memcpy (default_prefix, cpp_GCC_INCLUDE_DIR, default_len);
895       default_prefix[default_len] = '\0';
896
897       for (p = cpp_include_defaults; p->fname; p++)
898         {
899           /* Some standard dirs are only for C++.  */
900           if (!p->cplusplus
901               || (CPP_OPTION (pfile, cplusplus)
902                   && !CPP_OPTION (pfile, no_standard_cplusplus_includes)))
903             {
904               /* Does this dir start with the prefix?  */
905               if (!strncmp (p->fname, default_prefix, default_len))
906                 {
907                   /* Yes; change prefix and add to search list.  */
908                   int flen = strlen (p->fname);
909                   int this_len = specd_len + flen - default_len;
910                   char *str = (char *) xmalloc (this_len + 1);
911                   memcpy (str, specd_prefix, specd_len);
912                   memcpy (str + specd_len,
913                           p->fname + default_len,
914                           flen - default_len + 1);
915
916                   append_include_chain (pfile, str, SYSTEM, p->cxx_aware);
917                 }
918             }
919         }
920     }
921
922   /* Search ordinary names for GNU include directories.  */
923   for (p = cpp_include_defaults; p->fname; p++)
924     {
925       /* Some standard dirs are only for C++.  */
926       if (!p->cplusplus
927           || (CPP_OPTION (pfile, cplusplus)
928               && !CPP_OPTION (pfile, no_standard_cplusplus_includes)))
929         {
930           char *str = update_path (p->fname, p->component);
931           append_include_chain (pfile, str, SYSTEM, p->cxx_aware);
932         }
933     }
934 }
935
936 /* Pushes a command line -imacro and -include file indicated by P onto
937    the buffer stack.  Returns non-zero if successful.  */
938 static bool
939 push_include (pfile, p)
940      cpp_reader *pfile;
941      struct pending_option *p;
942 {
943   cpp_token header;
944
945   /* Later: maybe update this to use the #include "" search path
946      if cpp_read_file fails.  */
947   header.type = CPP_STRING;
948   header.val.str.text = (const unsigned char *) p->arg;
949   header.val.str.len = strlen (p->arg);
950   /* Make the command line directive take up a line.  */
951   pfile->line++;
952
953   return _cpp_execute_include (pfile, &header, IT_CMDLINE);
954 }
955
956 /* Frees a pending_option chain.  */
957 static void
958 free_chain (head)
959      struct pending_option *head;
960 {
961   struct pending_option *next;
962
963   while (head)
964     {
965       next = head->next;
966       free (head);
967       head = next;
968     }
969 }
970
971 /* This is called after options have been parsed, and partially
972    processed.  Setup for processing input from the file named FNAME,
973    or stdin if it is the empty string.  Return the original filename
974    on success (e.g. foo.i->foo.c), or NULL on failure.  */
975 const char *
976 cpp_read_main_file (pfile, fname, table)
977      cpp_reader *pfile;
978      const char *fname;
979      hash_table *table;
980 {
981   /* The front ends don't set up the hash table until they have
982      finished processing the command line options, so initializing the
983      hashtable is deferred until now.  */
984   _cpp_init_hashtable (pfile, table);
985
986   /* Set up the include search path now.  */
987   if (! CPP_OPTION (pfile, no_standard_includes))
988     init_standard_includes (pfile);
989
990   merge_include_chains (pfile);
991
992   /* With -v, print the list of dirs to search.  */
993   if (CPP_OPTION (pfile, verbose))
994     {
995       struct search_path *l;
996       fprintf (stderr, _("#include \"...\" search starts here:\n"));
997       for (l = CPP_OPTION (pfile, quote_include); l; l = l->next)
998         {
999           if (l == CPP_OPTION (pfile, bracket_include))
1000             fprintf (stderr, _("#include <...> search starts here:\n"));
1001           fprintf (stderr, " %s\n", l->name);
1002         }
1003       fprintf (stderr, _("End of search list.\n"));
1004     }
1005
1006   if (CPP_OPTION (pfile, print_deps))
1007     /* Set the default target (if there is none already).  */
1008     deps_add_default_target (pfile, fname);
1009
1010   /* Open the main input file.  */
1011   if (!_cpp_read_file (pfile, fname))
1012     return NULL;
1013
1014   /* Set this after cpp_post_options so the client can change the
1015      option if it wishes, and after stacking the main file so we don't
1016      trace the main file.  */
1017   pfile->line_maps.trace_includes = CPP_OPTION (pfile, print_include_names);
1018
1019   /* For foo.i, read the original filename foo.c now, for the benefit
1020      of the front ends.  */
1021   if (CPP_OPTION (pfile, preprocessed))
1022     read_original_filename (pfile);
1023
1024   return pfile->map->to_file;
1025 }
1026
1027 /* For preprocessed files, if the first tokens are of the form # NUM.
1028    handle the directive so we know the original file name.  This will
1029    generate file_change callbacks, which the front ends must handle
1030    appropriately given their state of initialization.  */
1031 static void
1032 read_original_filename (pfile)
1033      cpp_reader *pfile;
1034 {
1035   const cpp_token *token, *token1;
1036
1037   /* Lex ahead; if the first tokens are of the form # NUM, then
1038      process the directive, otherwise back up.  */
1039   token = _cpp_lex_direct (pfile);
1040   if (token->type == CPP_HASH)
1041     {
1042       token1 = _cpp_lex_direct (pfile);
1043       _cpp_backup_tokens (pfile, 1);
1044
1045       /* If it's a #line directive, handle it.  */
1046       if (token1->type == CPP_NUMBER)
1047         {
1048           _cpp_handle_directive (pfile, token->flags & PREV_WHITE);
1049           return;
1050         }
1051     }
1052
1053   /* Backup as if nothing happened.  */
1054   _cpp_backup_tokens (pfile, 1);
1055 }
1056
1057 /* Handle pending command line options: -D, -U, -A, -imacros and
1058    -include.  This should be called after debugging has been properly
1059    set up in the front ends.  */
1060 void
1061 cpp_finish_options (pfile)
1062      cpp_reader *pfile;
1063 {
1064   /* Mark named operators before handling command line macros.  */
1065   if (CPP_OPTION (pfile, cplusplus) && CPP_OPTION (pfile, operator_names))
1066     mark_named_operators (pfile);
1067
1068   /* Install builtins and process command line macros etc. in the order
1069      they appeared, but only if not already preprocessed.  */
1070   if (! CPP_OPTION (pfile, preprocessed))
1071     {
1072       struct pending_option *p;
1073
1074       _cpp_do_file_change (pfile, LC_RENAME, _("<built-in>"), 1, 0);
1075       init_builtins (pfile);
1076       _cpp_do_file_change (pfile, LC_RENAME, _("<command line>"), 1, 0);
1077       for (p = CPP_OPTION (pfile, pending)->directive_head; p; p = p->next)
1078         (*p->handler) (pfile, p->arg);
1079
1080       /* Scan -imacros files after command line defines, but before
1081          files given with -include.  */
1082       while ((p = CPP_OPTION (pfile, pending)->imacros_head) != NULL)
1083         {
1084           if (push_include (pfile, p))
1085             {
1086               pfile->buffer->return_at_eof = true;
1087               cpp_scan_nooutput (pfile);
1088             }
1089           CPP_OPTION (pfile, pending)->imacros_head = p->next;
1090           free (p);
1091         }
1092     }
1093
1094   free_chain (CPP_OPTION (pfile, pending)->directive_head);
1095   _cpp_push_next_buffer (pfile);
1096 }
1097
1098 /* Called to push the next buffer on the stack given by -include.  If
1099    there are none, free the pending structure and restore the line map
1100    for the main file.  */
1101 bool
1102 _cpp_push_next_buffer (pfile)
1103      cpp_reader *pfile;
1104 {
1105   bool pushed = false;
1106
1107   /* This is't pretty; we'd rather not be relying on this as a boolean
1108      for reverting the line map.  Further, we only free the chains in
1109      this conditional, so an early call to cpp_finish / cpp_destroy
1110      will leak that memory.  */
1111   if (CPP_OPTION (pfile, pending)
1112       && CPP_OPTION (pfile, pending)->imacros_head == NULL)
1113     {
1114       while (!pushed)
1115         {
1116           struct pending_option *p = CPP_OPTION (pfile, pending)->include_head;
1117
1118           if (p == NULL)
1119             break;
1120           if (! CPP_OPTION (pfile, preprocessed))
1121             pushed = push_include (pfile, p);
1122           CPP_OPTION (pfile, pending)->include_head = p->next;
1123           free (p);
1124         }
1125
1126       if (!pushed)
1127         {
1128           free (CPP_OPTION (pfile, pending));
1129           CPP_OPTION (pfile, pending) = NULL;
1130
1131           /* Restore the line map for the main file.  */
1132           if (! CPP_OPTION (pfile, preprocessed))
1133             _cpp_do_file_change (pfile, LC_RENAME,
1134                                  pfile->line_maps.maps[0].to_file, 1, 0);
1135         }
1136     }
1137
1138   return pushed;
1139 }
1140
1141 /* Use mkdeps.c to output dependency information.  */
1142 static void
1143 output_deps (pfile)
1144      cpp_reader *pfile;
1145 {
1146   /* Stream on which to print the dependency information.  */
1147   FILE *deps_stream = 0;
1148   const char *const deps_mode =
1149     CPP_OPTION (pfile, print_deps_append) ? "a" : "w";
1150
1151   if (CPP_OPTION (pfile, deps_file)[0] == '\0')
1152     deps_stream = stdout;
1153   else
1154     {
1155       deps_stream = fopen (CPP_OPTION (pfile, deps_file), deps_mode);
1156       if (deps_stream == 0)
1157         {
1158           cpp_notice_from_errno (pfile, CPP_OPTION (pfile, deps_file));
1159           return;
1160         }
1161     }
1162
1163   deps_write (pfile->deps, deps_stream, 72);
1164
1165   if (CPP_OPTION (pfile, deps_phony_targets))
1166     deps_phony_targets (pfile->deps, deps_stream);
1167
1168   /* Don't close stdout.  */
1169   if (deps_stream != stdout)
1170     {
1171       if (ferror (deps_stream) || fclose (deps_stream) != 0)
1172         cpp_fatal (pfile, "I/O error on output");
1173     }
1174 }
1175
1176 /* This is called at the end of preprocessing.  It pops the
1177    last buffer and writes dependency output.  It should also
1178    clear macro definitions, such that you could call cpp_start_read
1179    with a new filename to restart processing.  */
1180 void
1181 cpp_finish (pfile)
1182      cpp_reader *pfile;
1183 {
1184   /* cpplex.c leaves the final buffer on the stack.  This it so that
1185      it returns an unending stream of CPP_EOFs to the client.  If we
1186      popped the buffer, we'd dereference a NULL buffer pointer and
1187      segfault.  It's nice to allow the client to do worry-free excess
1188      cpp_get_token calls.  */
1189   while (pfile->buffer)
1190     _cpp_pop_buffer (pfile);
1191
1192   /* Don't write the deps file if preprocessing has failed.  */
1193   if (CPP_OPTION (pfile, print_deps) && pfile->errors == 0)
1194     output_deps (pfile);
1195
1196   /* Report on headers that could use multiple include guards.  */
1197   if (CPP_OPTION (pfile, print_include_names))
1198     _cpp_report_missing_guards (pfile);
1199 }
1200
1201 /* Add a directive to be handled later in the initialization phase.  */
1202 static void
1203 new_pending_directive (pend, text, handler)
1204      struct cpp_pending *pend;
1205      const char *text;
1206      cl_directive_handler handler;
1207 {
1208   struct pending_option *o = (struct pending_option *)
1209     xmalloc (sizeof (struct pending_option));
1210
1211   o->arg = text;
1212   o->next = NULL;
1213   o->handler = handler;
1214   APPEND (pend, directive, o);
1215 }
1216
1217 /* Irix6 "cc -n32" and OSF4 cc have problems with char foo[] = ("string");
1218    I.e. a const string initializer with parens around it.  That is
1219    what N_("string") resolves to, so we make no_* be macros instead.  */
1220 #define no_arg N_("argument missing after %s")
1221 #define no_ass N_("assertion missing after %s")
1222 #define no_dir N_("directory name missing after %s")
1223 #define no_fil N_("file name missing after %s")
1224 #define no_mac N_("macro name missing after %s")
1225 #define no_pth N_("path name missing after %s")
1226 #define no_num N_("number missing after %s")
1227 #define no_tgt N_("target missing after %s")
1228
1229 /* This is the list of all command line options, with the leading
1230    "-" removed.  It must be sorted in ASCII collating order.  */
1231 #define COMMAND_LINE_OPTIONS                                                  \
1232   DEF_OPT("$",                        0,      OPT_dollar)                     \
1233   DEF_OPT("+",                        0,      OPT_plus)                       \
1234   DEF_OPT("-help",                    0,      OPT__help)                      \
1235   DEF_OPT("-target-help",             0,      OPT_target__help)               \
1236   DEF_OPT("-version",                 0,      OPT__version)                   \
1237   DEF_OPT("A",                        no_ass, OPT_A)                          \
1238   DEF_OPT("C",                        0,      OPT_C)                          \
1239   DEF_OPT("D",                        no_mac, OPT_D)                          \
1240   DEF_OPT("H",                        0,      OPT_H)                          \
1241   DEF_OPT("I",                        no_dir, OPT_I)                          \
1242   DEF_OPT("M",                        0,      OPT_M)                          \
1243   DEF_OPT("MD",                       no_fil, OPT_MD)                         \
1244   DEF_OPT("MF",                       no_fil, OPT_MF)                         \
1245   DEF_OPT("MG",                       0,      OPT_MG)                         \
1246   DEF_OPT("MM",                       0,      OPT_MM)                         \
1247   DEF_OPT("MMD",                      no_fil, OPT_MMD)                        \
1248   DEF_OPT("MP",                       0,      OPT_MP)                         \
1249   DEF_OPT("MQ",                       no_tgt, OPT_MQ)                         \
1250   DEF_OPT("MT",                       no_tgt, OPT_MT)                         \
1251   DEF_OPT("P",                        0,      OPT_P)                          \
1252   DEF_OPT("U",                        no_mac, OPT_U)                          \
1253   DEF_OPT("W",                        no_arg, OPT_W)  /* arg optional */      \
1254   DEF_OPT("d",                        no_arg, OPT_d)                          \
1255   DEF_OPT("fleading-underscore",      0,      OPT_fleading_underscore)        \
1256   DEF_OPT("fno-leading-underscore",   0,      OPT_fno_leading_underscore)     \
1257   DEF_OPT("fno-operator-names",       0,      OPT_fno_operator_names)         \
1258   DEF_OPT("fno-preprocessed",         0,      OPT_fno_preprocessed)           \
1259   DEF_OPT("fno-show-column",          0,      OPT_fno_show_column)            \
1260   DEF_OPT("fpreprocessed",            0,      OPT_fpreprocessed)              \
1261   DEF_OPT("fshow-column",             0,      OPT_fshow_column)               \
1262   DEF_OPT("fsigned-char",             0,      OPT_fsigned_char)               \
1263   DEF_OPT("ftabstop=",                no_num, OPT_ftabstop)                   \
1264   DEF_OPT("funsigned-char",           0,      OPT_funsigned_char)             \
1265   DEF_OPT("h",                        0,      OPT_h)                          \
1266   DEF_OPT("idirafter",                no_dir, OPT_idirafter)                  \
1267   DEF_OPT("imacros",                  no_fil, OPT_imacros)                    \
1268   DEF_OPT("include",                  no_fil, OPT_include)                    \
1269   DEF_OPT("iprefix",                  no_pth, OPT_iprefix)                    \
1270   DEF_OPT("isystem",                  no_dir, OPT_isystem)                    \
1271   DEF_OPT("iwithprefix",              no_dir, OPT_iwithprefix)                \
1272   DEF_OPT("iwithprefixbefore",        no_dir, OPT_iwithprefixbefore)          \
1273   DEF_OPT("lang-asm",                 0,      OPT_lang_asm)                   \
1274   DEF_OPT("lang-c",                   0,      OPT_lang_c)                     \
1275   DEF_OPT("lang-c++",                 0,      OPT_lang_cplusplus)             \
1276   DEF_OPT("lang-c89",                 0,      OPT_lang_c89)                   \
1277   DEF_OPT("lang-objc",                0,      OPT_lang_objc)                  \
1278   DEF_OPT("lang-objc++",              0,      OPT_lang_objcplusplus)          \
1279   DEF_OPT("nostdinc",                 0,      OPT_nostdinc)                   \
1280   DEF_OPT("nostdinc++",               0,      OPT_nostdincplusplus)           \
1281   DEF_OPT("o",                        no_fil, OPT_o)                          \
1282   /* SDCC specific */                                                         \
1283   DEF_OPT("obj-ext=",                 no_arg, OPT_obj_ext)                    \
1284   DEF_OPT("pedantic",                 0,      OPT_pedantic)                   \
1285   DEF_OPT("pedantic-errors",          0,      OPT_pedantic_errors)            \
1286   DEF_OPT("remap",                    0,      OPT_remap)                      \
1287   DEF_OPT("std=c++98",                0,      OPT_std_cplusplus98)            \
1288   DEF_OPT("std=c89",                  0,      OPT_std_c89)                    \
1289   DEF_OPT("std=c99",                  0,      OPT_std_c99)                    \
1290   DEF_OPT("std=c9x",                  0,      OPT_std_c9x)                    \
1291   DEF_OPT("std=gnu89",                0,      OPT_std_gnu89)                  \
1292   DEF_OPT("std=gnu99",                0,      OPT_std_gnu99)                  \
1293   DEF_OPT("std=gnu9x",                0,      OPT_std_gnu9x)                  \
1294   DEF_OPT("std=iso9899:1990",         0,      OPT_std_iso9899_1990)           \
1295   DEF_OPT("std=iso9899:199409",       0,      OPT_std_iso9899_199409)         \
1296   DEF_OPT("std=iso9899:1999",         0,      OPT_std_iso9899_1999)           \
1297   DEF_OPT("std=iso9899:199x",         0,      OPT_std_iso9899_199x)           \
1298   DEF_OPT("trigraphs",                0,      OPT_trigraphs)                  \
1299   DEF_OPT("v",                        0,      OPT_v)                          \
1300   DEF_OPT("version",                  0,      OPT_version)                    \
1301   DEF_OPT("w",                        0,      OPT_w)
1302
1303 #define DEF_OPT(text, msg, code) code,
1304 enum opt_code
1305 {
1306   COMMAND_LINE_OPTIONS
1307   N_OPTS
1308 };
1309 #undef DEF_OPT
1310
1311 struct cl_option
1312 {
1313   const char *opt_text;
1314   const char *msg;
1315   size_t opt_len;
1316   enum opt_code opt_code;
1317 };
1318
1319 #define DEF_OPT(text, msg, code) { text, msg, sizeof(text) - 1, code },
1320 #ifdef HOST_EBCDIC
1321 static struct cl_option cl_options[] =
1322 #else
1323 static const struct cl_option cl_options[] =
1324 #endif
1325 {
1326   COMMAND_LINE_OPTIONS
1327 };
1328 #undef DEF_OPT
1329 #undef COMMAND_LINE_OPTIONS
1330
1331 /* Perform a binary search to find which, if any, option the given
1332    command-line matches.  Returns its index in the option array,
1333    negative on failure.  Complications arise since some options can be
1334    suffixed with an argument, and multiple complete matches can occur,
1335    e.g. -iwithprefix and -iwithprefixbefore.  Moreover, we need to
1336    accept options beginning with -W that we do not recognise, but not
1337    to swallow any subsequent command line argument; this is handled as
1338    special cases in cpp_handle_option.  */
1339 static int
1340 parse_option (input)
1341      const char *input;
1342 {
1343   unsigned int md, mn, mx;
1344   size_t opt_len;
1345   int comp;
1346
1347   mn = 0;
1348   mx = N_OPTS;
1349
1350   while (mx > mn)
1351     {
1352       md = (mn + mx) / 2;
1353
1354       opt_len = cl_options[md].opt_len;
1355       comp = strncmp (input, cl_options[md].opt_text, opt_len);
1356
1357       if (comp > 0)
1358         mn = md + 1;
1359       else if (comp < 0)
1360         mx = md;
1361       else
1362         {
1363           if (input[opt_len] == '\0')
1364             return md;
1365           /* We were passed more text.  If the option takes an argument,
1366              we may match a later option or we may have been passed the
1367              argument.  The longest possible option match succeeds.
1368              If the option takes no arguments we have not matched and
1369              continue the search (e.g. input="stdc++" match was "stdc").  */
1370           mn = md + 1;
1371           if (cl_options[md].msg)
1372             {
1373               /* Scan forwards.  If we get an exact match, return it.
1374                  Otherwise, return the longest option-accepting match.
1375                  This loops no more than twice with current options.  */
1376               mx = md;
1377               for (; mn < (unsigned int) N_OPTS; mn++)
1378                 {
1379                   opt_len = cl_options[mn].opt_len;
1380                   if (strncmp (input, cl_options[mn].opt_text, opt_len))
1381                     break;
1382                   if (input[opt_len] == '\0')
1383                     return mn;
1384                   if (cl_options[mn].msg)
1385                     mx = mn;
1386                 }
1387               return mx;
1388             }
1389         }
1390     }
1391
1392   return -1;
1393 }
1394
1395 /* Handle one command-line option in (argc, argv).
1396    Can be called multiple times, to handle multiple sets of options.
1397    If ignore is non-zero, this will ignore unrecognized -W* options.
1398    Returns number of strings consumed.  */
1399 int
1400 cpp_handle_option (pfile, argc, argv, ignore)
1401      cpp_reader *pfile;
1402      int argc;
1403      char **argv;
1404      int ignore;
1405 {
1406   int i = 0;
1407   struct cpp_pending *pend = CPP_OPTION (pfile, pending);
1408
1409   /* Interpret "-" or a non-option as a file name.  */
1410   if (argv[i][0] != '-' || argv[i][1] == '\0')
1411     {
1412       if (CPP_OPTION (pfile, in_fname) == NULL)
1413         CPP_OPTION (pfile, in_fname) = argv[i];
1414       else if (CPP_OPTION (pfile, out_fname) == NULL)
1415         CPP_OPTION (pfile, out_fname) = argv[i];
1416       else
1417         cpp_fatal (pfile, "too many filenames. Type %s --help for usage info",
1418                    progname);
1419     }
1420   else
1421     {
1422       enum opt_code opt_code;
1423       int opt_index;
1424       const char *arg = 0;
1425
1426       /* Skip over '-'.  */
1427       opt_index = parse_option (&argv[i][1]);
1428       if (opt_index < 0)
1429         return i;
1430
1431       opt_code = cl_options[opt_index].opt_code;
1432       if (cl_options[opt_index].msg)
1433         {
1434           arg = &argv[i][cl_options[opt_index].opt_len + 1];
1435
1436           /* Yuk. Special case for -W as it must not swallow
1437              up any following argument.  If this becomes common, add
1438              another field to the cl_options table.  */
1439           if (arg[0] == '\0' && opt_code != OPT_W)
1440             {
1441               arg = argv[++i];
1442               if (!arg)
1443                 {
1444                   cpp_fatal (pfile, cl_options[opt_index].msg, argv[i - 1]);
1445                   return argc;
1446                 }
1447             }
1448         }
1449
1450       switch (opt_code)
1451         {
1452         case N_OPTS: /* Shut GCC up.  */
1453           break;
1454         case OPT_fleading_underscore:
1455           CPP_OPTION (pfile, user_label_prefix) = "_";
1456           break;
1457         case OPT_fno_leading_underscore:
1458           CPP_OPTION (pfile, user_label_prefix) = "";
1459           break;
1460         case OPT_fno_operator_names:
1461           CPP_OPTION (pfile, operator_names) = 0;
1462           break;
1463         case OPT_fpreprocessed:
1464           CPP_OPTION (pfile, preprocessed) = 1;
1465           break;
1466         case OPT_fno_preprocessed:
1467           CPP_OPTION (pfile, preprocessed) = 0;
1468           break;
1469         case OPT_fshow_column:
1470           CPP_OPTION (pfile, show_column) = 1;
1471           break;
1472         case OPT_fno_show_column:
1473           CPP_OPTION (pfile, show_column) = 0;
1474           break;
1475         case OPT_fsigned_char:
1476           CPP_OPTION (pfile, signed_char) = 1;
1477           break;
1478         case OPT_funsigned_char:
1479           CPP_OPTION (pfile, signed_char) = 0;
1480           break;
1481         case OPT_ftabstop:
1482           /* Silently ignore empty string, non-longs and silly values.  */
1483           if (arg[0] != '\0')
1484             {
1485               char *endptr;
1486               long tabstop = strtol (arg, &endptr, 10);
1487               if (*endptr == '\0' && tabstop >= 1 && tabstop <= 100)
1488                 CPP_OPTION (pfile, tabstop) = tabstop;
1489             }
1490           break;
1491         case OPT_w:
1492           CPP_OPTION (pfile, inhibit_warnings) = 1;
1493           break;
1494         case OPT_h:
1495         case OPT__help:
1496           print_help ();
1497           CPP_OPTION (pfile, help_only) = 1;
1498           break;
1499         case OPT_target__help:
1500           /* Print if any target specific options. cpplib has none, but
1501              make sure help_only gets set.  */
1502           CPP_OPTION (pfile, help_only) = 1;
1503           break;
1504
1505           /* --version inhibits compilation, -version doesn't. -v means
1506              verbose and -version.  Historical reasons, don't ask.  */
1507         case OPT__version:
1508           CPP_OPTION (pfile, help_only) = 1;
1509           pfile->print_version = 1;
1510           break;
1511         case OPT_v:
1512           CPP_OPTION (pfile, verbose) = 1;
1513           pfile->print_version = 1;
1514           break;
1515         case OPT_version:
1516           pfile->print_version = 1;
1517           break;
1518
1519         case OPT_C:
1520           CPP_OPTION (pfile, discard_comments) = 0;
1521           break;
1522         case OPT_P:
1523           CPP_OPTION (pfile, no_line_commands) = 1;
1524           break;
1525         case OPT_dollar:        /* Don't include $ in identifiers.  */
1526           CPP_OPTION (pfile, dollars_in_ident) = 0;
1527           break;
1528         case OPT_H:
1529           CPP_OPTION (pfile, print_include_names) = 1;
1530           break;
1531         case OPT_D:
1532           new_pending_directive (pend, arg, cpp_define);
1533           break;
1534         case OPT_pedantic_errors:
1535           CPP_OPTION (pfile, pedantic_errors) = 1;
1536           /* fall through */
1537         case OPT_pedantic:
1538           CPP_OPTION (pfile, pedantic) = 1;
1539           break;
1540         case OPT_trigraphs:
1541           CPP_OPTION (pfile, trigraphs) = 1;
1542           break;
1543         case OPT_plus:
1544           CPP_OPTION (pfile, cplusplus) = 1;
1545           CPP_OPTION (pfile, cplusplus_comments) = 1;
1546           break;
1547         case OPT_remap:
1548           CPP_OPTION (pfile, remap) = 1;
1549           break;
1550         case OPT_iprefix:
1551           CPP_OPTION (pfile, include_prefix) = arg;
1552           CPP_OPTION (pfile, include_prefix_len) = strlen (arg);
1553           break;
1554         case OPT_lang_c:
1555           set_lang (pfile, CLK_GNUC89);
1556           break;
1557         case OPT_lang_cplusplus:
1558           set_lang (pfile, CLK_GNUCXX);
1559           break;
1560         case OPT_lang_objc:
1561           set_lang (pfile, CLK_OBJC);
1562           break;
1563         case OPT_lang_objcplusplus:
1564           set_lang (pfile, CLK_OBJCXX);
1565           break;
1566         case OPT_lang_asm:
1567           set_lang (pfile, CLK_ASM);
1568           break;
1569         case OPT_std_cplusplus98:
1570           set_lang (pfile, CLK_CXX98);
1571           break;
1572         case OPT_std_gnu89:
1573           set_lang (pfile, CLK_GNUC89);
1574           break;
1575         case OPT_std_gnu9x:
1576         case OPT_std_gnu99:
1577           set_lang (pfile, CLK_GNUC99);
1578           break;
1579         case OPT_std_iso9899_199409:
1580           set_lang (pfile, CLK_STDC94);
1581           break;
1582         case OPT_std_iso9899_1990:
1583         case OPT_std_c89:
1584         case OPT_lang_c89:
1585           set_lang (pfile, CLK_STDC89);
1586           break;
1587         case OPT_std_iso9899_199x:
1588         case OPT_std_iso9899_1999:
1589         case OPT_std_c9x:
1590         case OPT_std_c99:
1591           set_lang (pfile, CLK_STDC99);
1592           break;
1593         case OPT_nostdinc:
1594           /* -nostdinc causes no default include directories.
1595              You must specify all include-file directories with -I.  */
1596           CPP_OPTION (pfile, no_standard_includes) = 1;
1597           break;
1598         case OPT_nostdincplusplus:
1599           /* -nostdinc++ causes no default C++-specific include directories.  */
1600           CPP_OPTION (pfile, no_standard_cplusplus_includes) = 1;
1601           break;
1602         case OPT_o:
1603           if (CPP_OPTION (pfile, out_fname) == NULL)
1604             CPP_OPTION (pfile, out_fname) = arg;
1605           else
1606             {
1607               cpp_fatal (pfile, "output filename specified twice");
1608               return argc;
1609             }
1610           break;
1611         case OPT_d:
1612           /* Args to -d specify what parts of macros to dump.
1613              Silently ignore unrecognised options; they may
1614              be aimed at the compiler proper.  */
1615           {
1616             char c;
1617
1618             while ((c = *arg++) != '\0')
1619               switch (c)
1620                 {
1621                 case 'M':
1622                   CPP_OPTION (pfile, dump_macros) = dump_only;
1623                   break;
1624                 case 'N':
1625                   CPP_OPTION (pfile, dump_macros) = dump_names;
1626                   break;
1627                 case 'D':
1628                   CPP_OPTION (pfile, dump_macros) = dump_definitions;
1629                   break;
1630                 case 'I':
1631                   CPP_OPTION (pfile, dump_includes) = 1;
1632                   break;
1633                 }
1634           }
1635           break;
1636
1637         case OPT_MG:
1638           CPP_OPTION (pfile, print_deps_missing_files) = 1;
1639           break;
1640         case OPT_M:
1641           /* When doing dependencies with -M or -MM, suppress normal
1642              preprocessed output, but still do -dM etc. as software
1643              depends on this.  Preprocessed output occurs if -MD, -MMD
1644              or environment var dependency generation is used.  */
1645           CPP_OPTION (pfile, print_deps) = 2;
1646           CPP_OPTION (pfile, no_output) = 1;
1647           break;
1648         case OPT_MM:
1649           CPP_OPTION (pfile, print_deps) = 1;
1650           CPP_OPTION (pfile, no_output) = 1;
1651           break;
1652         case OPT_MF:
1653           CPP_OPTION (pfile, deps_file) = arg;
1654           break;
1655         case OPT_MP:
1656           CPP_OPTION (pfile, deps_phony_targets) = 1;
1657           break;
1658         case OPT_MQ:
1659         case OPT_MT:
1660           /* Add a target.  -MQ quotes for Make.  */
1661           deps_add_target (pfile->deps, arg, opt_code == OPT_MQ);
1662           break;
1663
1664         case OPT_MD:
1665           CPP_OPTION (pfile, print_deps) = 2;
1666           CPP_OPTION (pfile, deps_file) = arg;
1667           break;
1668         case OPT_MMD:
1669           CPP_OPTION (pfile, print_deps) = 1;
1670           CPP_OPTION (pfile, deps_file) = arg;
1671           break;
1672
1673         case OPT_A:
1674           if (arg[0] == '-')
1675             {
1676               /* -A with an argument beginning with '-' acts as
1677                  #unassert on whatever immediately follows the '-'.
1678                  If "-" is the whole argument, we eliminate all
1679                  predefined macros and assertions, including those
1680                  that were specified earlier on the command line.
1681                  That way we can get rid of any that were passed
1682                  automatically in from GCC.  */
1683
1684               if (arg[1] == '\0')
1685                 {
1686                   free_chain (pend->directive_head);
1687                   pend->directive_head = NULL;
1688                   pend->directive_tail = NULL;
1689                 }
1690               else
1691                 new_pending_directive (pend, arg + 1, cpp_unassert);
1692             }
1693           else
1694             new_pending_directive (pend, arg, cpp_assert);
1695           break;
1696         case OPT_U:
1697           new_pending_directive (pend, arg, cpp_undef);
1698           break;
1699         case OPT_I:           /* Add directory to path for includes.  */
1700           if (!strcmp (arg, "-"))
1701             {
1702               /* -I- means:
1703                  Use the preceding -I directories for #include "..."
1704                  but not #include <...>.
1705                  Don't search the directory of the present file
1706                  for #include "...".  (Note that -I. -I- is not the same as
1707                  the default setup; -I. uses the compiler's working dir.)  */
1708               if (! CPP_OPTION (pfile, ignore_srcdir))
1709                 {
1710                   pend->quote_head = pend->brack_head;
1711                   pend->quote_tail = pend->brack_tail;
1712                   pend->brack_head = 0;
1713                   pend->brack_tail = 0;
1714                   CPP_OPTION (pfile, ignore_srcdir) = 1;
1715                 }
1716               else
1717                 {
1718                   cpp_fatal (pfile, "-I- specified twice");
1719                   return argc;
1720                 }
1721             }
1722           else
1723             append_include_chain (pfile, (char *)xstrdup(arg), BRACKET, 0);
1724           break;
1725         case OPT_isystem:
1726           /* Add directory to beginning of system include path, as a system
1727              include directory.  */
1728           append_include_chain (pfile, (char *)xstrdup(arg), SYSTEM, 0);
1729           break;
1730         case OPT_include:
1731         case OPT_imacros:
1732           {
1733             struct pending_option *o = (struct pending_option *)
1734               xmalloc (sizeof (struct pending_option));
1735             o->arg = arg;
1736             o->next = NULL;
1737
1738             if (opt_code == OPT_include)
1739               APPEND (pend, include, o);
1740             else
1741               APPEND (pend, imacros, o);
1742           }
1743           break;
1744         case OPT_iwithprefix:
1745           /* Add directory to end of path for includes,
1746              with the default prefix at the front of its name.  */
1747           /* fall through */
1748         case OPT_iwithprefixbefore:
1749           /* Add directory to main path for includes,
1750              with the default prefix at the front of its name.  */
1751           {
1752             char *fname;
1753             int len;
1754
1755             len = strlen (arg);
1756
1757             if (CPP_OPTION (pfile, include_prefix) != 0)
1758               {
1759                 size_t ipl = CPP_OPTION (pfile, include_prefix_len);
1760                 fname = xmalloc (ipl + len + 1);
1761                 memcpy (fname, CPP_OPTION (pfile, include_prefix), ipl);
1762                 memcpy (fname + ipl, arg, len + 1);
1763               }
1764             else if (cpp_GCC_INCLUDE_DIR_len)
1765               {
1766                 fname = xmalloc (cpp_GCC_INCLUDE_DIR_len + len + 1);
1767                 memcpy (fname, cpp_GCC_INCLUDE_DIR, cpp_GCC_INCLUDE_DIR_len);
1768                 memcpy (fname + cpp_GCC_INCLUDE_DIR_len, arg, len + 1);
1769               }
1770             else
1771               fname = xstrdup (arg);
1772
1773             append_include_chain (pfile, fname,
1774                           opt_code == OPT_iwithprefix ? SYSTEM: BRACKET, 0);
1775           }
1776           break;
1777         case OPT_idirafter:
1778           /* Add directory to end of path for includes.  */
1779           append_include_chain (pfile, (char *)xstrdup (arg), AFTER, 0);
1780           break;
1781         case OPT_W:
1782           /* Silently ignore unrecognised options.  */
1783           if (!strcmp (argv[i], "-Wall"))
1784             {
1785               CPP_OPTION (pfile, warn_trigraphs) = 1;
1786               CPP_OPTION (pfile, warn_comments) = 1;
1787             }
1788           else if (!strcmp (argv[i], "-Wtraditional"))
1789             CPP_OPTION (pfile, warn_traditional) = 1;
1790           else if (!strcmp (argv[i], "-Wtrigraphs"))
1791             CPP_OPTION (pfile, warn_trigraphs) = 1;
1792           else if (!strcmp (argv[i], "-Wcomment"))
1793             CPP_OPTION (pfile, warn_comments) = 1;
1794           else if (!strcmp (argv[i], "-Wcomments"))
1795             CPP_OPTION (pfile, warn_comments) = 1;
1796           else if (!strcmp (argv[i], "-Wundef"))
1797             CPP_OPTION (pfile, warn_undef) = 1;
1798           else if (!strcmp (argv[i], "-Wimport"))
1799             CPP_OPTION (pfile, warn_import) = 1;
1800           else if (!strcmp (argv[i], "-Werror"))
1801             CPP_OPTION (pfile, warnings_are_errors) = 1;
1802           else if (!strcmp (argv[i], "-Wsystem-headers"))
1803             CPP_OPTION (pfile, warn_system_headers) = 1;
1804           else if (!strcmp (argv[i], "-Wno-traditional"))
1805             CPP_OPTION (pfile, warn_traditional) = 0;
1806           else if (!strcmp (argv[i], "-Wno-trigraphs"))
1807             CPP_OPTION (pfile, warn_trigraphs) = 0;
1808           else if (!strcmp (argv[i], "-Wno-comment"))
1809             CPP_OPTION (pfile, warn_comments) = 0;
1810           else if (!strcmp (argv[i], "-Wno-comments"))
1811             CPP_OPTION (pfile, warn_comments) = 0;
1812           else if (!strcmp (argv[i], "-Wno-undef"))
1813             CPP_OPTION (pfile, warn_undef) = 0;
1814           else if (!strcmp (argv[i], "-Wno-import"))
1815             CPP_OPTION (pfile, warn_import) = 0;
1816           else if (!strcmp (argv[i], "-Wno-error"))
1817             CPP_OPTION (pfile, warnings_are_errors) = 0;
1818           else if (!strcmp (argv[i], "-Wno-system-headers"))
1819             CPP_OPTION (pfile, warn_system_headers) = 0;
1820           else if (! ignore)
1821             return i;
1822           break;
1823         /* SDCC specific */
1824         case OPT_obj_ext:
1825           CPP_OPTION (pfile, obj_ext) = arg;
1826           break;
1827         }
1828     }
1829   return i + 1;
1830 }
1831
1832 /* Handle command-line options in (argc, argv).
1833    Can be called multiple times, to handle multiple sets of options.
1834    Returns if an unrecognized option is seen.
1835    Returns number of strings consumed.  */
1836 int
1837 cpp_handle_options (pfile, argc, argv)
1838      cpp_reader *pfile;
1839      int argc;
1840      char **argv;
1841 {
1842   int i;
1843   int strings_processed;
1844
1845   for (i = 0; i < argc; i += strings_processed)
1846     {
1847       strings_processed = cpp_handle_option (pfile, argc - i, argv + i, 1);
1848       if (strings_processed == 0)
1849         break;
1850     }
1851
1852   return i;
1853 }
1854
1855 /* Extra processing when all options are parsed, after all calls to
1856    cpp_handle_option[s].  Consistency checks etc.  */
1857 void
1858 cpp_post_options (pfile)
1859      cpp_reader *pfile;
1860 {
1861   if (pfile->print_version)
1862     {
1863       fprintf (stderr, _("GNU CPP version %s (cpplib)"), version_string);
1864 #ifdef TARGET_VERSION
1865       TARGET_VERSION;
1866 #endif
1867       fputc ('\n', stderr);
1868     }
1869
1870   /* Canonicalize in_fname and out_fname.  We guarantee they are not
1871      NULL, and that the empty string represents stdin / stdout.  */
1872   if (CPP_OPTION (pfile, in_fname) == NULL
1873       || !strcmp (CPP_OPTION (pfile, in_fname), "-"))
1874     CPP_OPTION (pfile, in_fname) = "";
1875
1876   if (CPP_OPTION (pfile, out_fname) == NULL
1877       || !strcmp (CPP_OPTION (pfile, out_fname), "-"))
1878     CPP_OPTION (pfile, out_fname) = "";
1879
1880   /* -Wtraditional is not useful in C++ mode.  */
1881   if (CPP_OPTION (pfile, cplusplus))
1882     CPP_OPTION (pfile, warn_traditional) = 0;
1883
1884   /* Set this if it hasn't been set already.  */
1885   if (CPP_OPTION (pfile, user_label_prefix) == NULL)
1886     CPP_OPTION (pfile, user_label_prefix) = USER_LABEL_PREFIX;
1887
1888   /* Permanently disable macro expansion if we are rescanning
1889      preprocessed text.  */
1890   if (CPP_OPTION (pfile, preprocessed))
1891     pfile->state.prevent_expansion = 1;
1892
1893   /* -dM makes no normal output.  This is set here so that -dM -dD
1894      works as expected.  */
1895   if (CPP_OPTION (pfile, dump_macros) == dump_only)
1896     CPP_OPTION (pfile, no_output) = 1;
1897
1898   /* Disable -dD, -dN and -dI if we should make no normal output
1899      (such as with -M). Allow -M -dM since some software relies on
1900      this.  */
1901   if (CPP_OPTION (pfile, no_output))
1902     {
1903       if (CPP_OPTION (pfile, dump_macros) != dump_only)
1904         CPP_OPTION (pfile, dump_macros) = dump_none;
1905       CPP_OPTION (pfile, dump_includes) = 0;
1906     }
1907
1908   /* We need to do this after option processing and before
1909      cpp_start_read, as cppmain.c relies on the options->no_output to
1910      set its callbacks correctly before calling cpp_start_read.  */
1911   init_dependency_output (pfile);
1912
1913   /* After checking the environment variables, check if -M or -MM has
1914      not been specified, but other -M options have.  */
1915   if (CPP_OPTION (pfile, print_deps) == 0 &&
1916       (CPP_OPTION (pfile, print_deps_missing_files)
1917        || CPP_OPTION (pfile, deps_file)
1918        || CPP_OPTION (pfile, deps_phony_targets)))
1919     cpp_fatal (pfile, "you must additionally specify either -M or -MM");
1920 }
1921
1922 /* Set up dependency-file output.  On exit, if print_deps is non-zero
1923    then deps_file is not NULL; stdout is the empty string.  */
1924 static void
1925 init_dependency_output (pfile)
1926      cpp_reader *pfile;
1927 {
1928   char *spec, *s, *output_file;
1929
1930   /* Either of two environment variables can specify output of deps.
1931      Its value is either "OUTPUT_FILE" or "OUTPUT_FILE DEPS_TARGET",
1932      where OUTPUT_FILE is the file to write deps info to
1933      and DEPS_TARGET is the target to mention in the deps.  */
1934
1935   if (CPP_OPTION (pfile, print_deps) == 0)
1936     {
1937       spec = getenv ("DEPENDENCIES_OUTPUT");
1938       if (spec)
1939         CPP_OPTION (pfile, print_deps) = 1;
1940       else
1941         {
1942           spec = getenv ("SUNPRO_DEPENDENCIES");
1943           if (spec)
1944             {
1945               CPP_OPTION (pfile, print_deps) = 2;
1946               CPP_OPTION (pfile, deps_ignore_main_file) = 1;
1947             }
1948           else
1949             return;
1950         }
1951
1952       /* Find the space before the DEPS_TARGET, if there is one.  */
1953       s = strchr (spec, ' ');
1954       if (s)
1955         {
1956           /* Let the caller perform MAKE quoting.  */
1957           deps_add_target (pfile->deps, s + 1, 0);
1958           output_file = (char *) xmalloc (s - spec + 1);
1959           memcpy (output_file, spec, s - spec);
1960           output_file[s - spec] = 0;
1961         }
1962       else
1963         output_file = spec;
1964
1965       /* Command line -MF overrides environment variables and default.  */
1966       if (CPP_OPTION (pfile, deps_file) == 0)
1967         CPP_OPTION (pfile, deps_file) = output_file;
1968
1969       CPP_OPTION (pfile, print_deps_append) = 1;
1970     }
1971   else if (CPP_OPTION (pfile, deps_file) == 0)
1972     /* If -M or -MM was seen without -MF, default output to wherever
1973        was specified with -o.  out_fname is non-NULL here.  */
1974     CPP_OPTION (pfile, deps_file) = CPP_OPTION (pfile, out_fname);
1975 }
1976
1977 /* Handle --help output.  */
1978 static void
1979 print_help ()
1980 {
1981   /* To keep the lines from getting too long for some compilers, limit
1982      to about 500 characters (6 lines) per chunk.  */
1983   fputs (_("\
1984 Switches:\n\
1985   -include <file>           Include the contents of <file> before other files\n\
1986   -imacros <file>           Accept definition of macros in <file>\n\
1987   -iprefix <path>           Specify <path> as a prefix for next two options\n\
1988   -iwithprefix <dir>        Add <dir> to the end of the system include path\n\
1989   -iwithprefixbefore <dir>  Add <dir> to the end of the main include path\n\
1990   -isystem <dir>            Add <dir> to the start of the system include path\n\
1991 "), stdout);
1992   fputs (_("\
1993   -idirafter <dir>          Add <dir> to the end of the system include path\n\
1994   -I <dir>                  Add <dir> to the end of the main include path\n\
1995   -I-                       Fine-grained include path control; see info docs\n\
1996   -nostdinc                 Do not search system include directories\n\
1997                              (dirs specified with -isystem will still be used)\n\
1998   -nostdinc++               Do not search system include directories for C++\n\
1999   -o <file>                 Put output into <file>\n\
2000 "), stdout);
2001   fputs (_("\
2002   -pedantic                 Issue all warnings demanded by strict ISO C\n\
2003   -pedantic-errors          Issue -pedantic warnings as errors instead\n\
2004   -trigraphs                Support ISO C trigraphs\n\
2005   -lang-c                   Assume that the input sources are in C\n\
2006   -lang-c89                 Assume that the input sources are in C89\n\
2007 "), stdout);
2008   fputs (_("\
2009   -lang-c++                 Assume that the input sources are in C++\n\
2010   -lang-objc                Assume that the input sources are in ObjectiveC\n\
2011   -lang-objc++              Assume that the input sources are in ObjectiveC++\n\
2012   -lang-asm                 Assume that the input sources are in assembler\n\
2013 "), stdout);
2014   fputs (_("\
2015   -std=<std name>           Specify the conformance standard; one of:\n\
2016                             gnu89, gnu99, c89, c99, iso9899:1990,\n\
2017                             iso9899:199409, iso9899:1999\n\
2018   -+                        Allow parsing of C++ style features\n\
2019   -w                        Inhibit warning messages\n\
2020   -Wtrigraphs               Warn if trigraphs are encountered\n\
2021   -Wno-trigraphs            Do not warn about trigraphs\n\
2022   -Wcomment{s}              Warn if one comment starts inside another\n\
2023 "), stdout);
2024   fputs (_("\
2025   -Wno-comment{s}           Do not warn about comments\n\
2026   -Wtraditional             Warn about features not present in traditional C\n\
2027   -Wno-traditional          Do not warn about traditional C\n\
2028   -Wundef                   Warn if an undefined macro is used by #if\n\
2029   -Wno-undef                Do not warn about testing undefined macros\n\
2030   -Wimport                  Warn about the use of the #import directive\n\
2031 "), stdout);
2032   fputs (_("\
2033   -Wno-import               Do not warn about the use of #import\n\
2034   -Werror                   Treat all warnings as errors\n\
2035   -Wno-error                Do not treat warnings as errors\n\
2036   -Wsystem-headers          Do not suppress warnings from system headers\n\
2037   -Wno-system-headers       Suppress warnings from system headers\n\
2038   -Wall                     Enable all preprocessor warnings\n\
2039 "), stdout);
2040   fputs (_("\
2041   -M                        Generate make dependencies\n\
2042   -MM                       As -M, but ignore system header files\n\
2043   -MD                       Generate make dependencies and compile\n\
2044   -MMD                      As -MD, but ignore system header files\n\
2045   -MF <file>                Write dependency output to the given file\n\
2046   -MG                       Treat missing header file as generated files\n\
2047 "), stdout);
2048   fputs (_("\
2049   -MP                       Generate phony targets for all headers\n\
2050   -MQ <target>              Add a MAKE-quoted target\n\
2051   -MT <target>              Add an unquoted target\n\
2052 "), stdout);
2053   /* SDCC specific */
2054   fputs (_("\
2055   -obj-ext=<extension>      Define object file extension, used for generation\n\
2056                             of make dependencies\n\
2057 "), stdout);
2058   fputs (_("\
2059   -D<macro>                 Define a <macro> with string '1' as its value\n\
2060   -D<macro>=<val>           Define a <macro> with <val> as its value\n\
2061   -A<question>=<answer>     Assert the <answer> to <question>\n\
2062   -A-<question>=<answer>    Disable the <answer> to <question>\n\
2063   -U<macro>                 Undefine <macro> \n\
2064   -v                        Display the version number\n\
2065 "), stdout);
2066   fputs (_("\
2067   -H                        Print the name of header files as they are used\n\
2068   -C                        Do not discard comments\n\
2069   -dM                       Display a list of macro definitions active at end\n\
2070   -dD                       Preserve macro definitions in output\n\
2071   -dN                       As -dD except that only the names are preserved\n\
2072   -dI                       Include #include directives in the output\n\
2073 "), stdout);
2074   fputs (_("\
2075   -fpreprocessed            Treat the input file as already preprocessed\n\
2076   -ftabstop=<number>        Distance between tab stops for column reporting\n\
2077   -P                        Do not generate #line directives\n\
2078   -$                        Do not allow '$' in identifiers\n\
2079   -remap                    Remap file names when including files\n\
2080   --version                 Display version information\n\
2081   -h or --help              Display this information\n\
2082 "), stdout);
2083 }