OSDN Git Service

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