OSDN Git Service

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