OSDN Git Service

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