OSDN Git Service

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