Initial import from FreeBSD RELENG_4:
[games.git] / contrib / gcc / gcc.c
1 /* Compiler driver program that can handle many languages.
2    Copyright (C) 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
3    2000 Free Software Foundation, Inc.
4
5 This file is part of GNU CC.
6
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING.  If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.
21
22 This paragraph is here to try to keep Sun CC from dying.
23 The number of chars here seems crucial!!!!  */
24
25 /* $FreeBSD: src/contrib/gcc/gcc.c,v 1.17.2.9 2002/06/20 23:12:28 obrien Exp $ */
26
27 /* This program is the user interface to the C compiler and possibly to
28 other compilers.  It is used because compilation is a complicated procedure
29 which involves running several programs and passing temporary files between
30 them, forwarding the users switches to those programs selectively,
31 and deleting the temporary files at the end.
32
33 CC recognizes how to compile each input file by suffixes in the file names.
34 Once it knows which kind of compilation to perform, the procedure for
35 compilation is specified by a string called a "spec".  */
36 \f
37 #include "config.h"
38 #include "system.h"
39 #include <signal.h>
40
41 #include "obstack.h"
42 #include "intl.h"
43 #include "prefix.h"
44
45 #ifdef VMS
46 #define exit __posix_exit
47 #endif
48
49 /* By default there is no special suffix for executables.  */
50 #ifdef EXECUTABLE_SUFFIX
51 #define HAVE_EXECUTABLE_SUFFIX
52 #else
53 #define EXECUTABLE_SUFFIX ""
54 #endif
55
56 /* By default, the suffix for object files is ".o".  */
57 #ifdef OBJECT_SUFFIX
58 #define HAVE_OBJECT_SUFFIX
59 #else
60 #define OBJECT_SUFFIX ".o"
61 #endif
62
63 /* By default, colon separates directories in a path.  */
64 #ifndef PATH_SEPARATOR
65 #define PATH_SEPARATOR ':'
66 #endif
67
68 #ifndef DIR_SEPARATOR
69 #define DIR_SEPARATOR '/'
70 #endif
71
72 /* Define IS_DIR_SEPARATOR.  */
73 #ifndef DIR_SEPARATOR_2
74 # define IS_DIR_SEPARATOR(ch) ((ch) == DIR_SEPARATOR)
75 #else /* DIR_SEPARATOR_2 */
76 # define IS_DIR_SEPARATOR(ch) \
77         (((ch) == DIR_SEPARATOR) || ((ch) == DIR_SEPARATOR_2))
78 #endif /* DIR_SEPARATOR_2 */
79
80 static char dir_separator_str[] = {DIR_SEPARATOR, 0};
81
82 #define obstack_chunk_alloc xmalloc
83 #define obstack_chunk_free free
84
85 #ifndef GET_ENV_PATH_LIST
86 #define GET_ENV_PATH_LIST(VAR,NAME)     do { (VAR) = getenv (NAME); } while (0)
87 #endif
88
89 #ifndef HAVE_KILL
90 #define kill(p,s) raise(s)
91 #endif
92
93 /* If a stage of compilation returns an exit status >= 1,
94    compilation of that file ceases.  */
95
96 #define MIN_FATAL_STATUS 1
97
98 /* Flag saying to print the directories gcc will search through looking for
99    programs, libraries, etc.  */
100
101 static int print_search_dirs;
102
103 /* Flag saying to print the full filename of this file
104    as found through our usual search mechanism.  */
105
106 static const char *print_file_name = NULL;
107
108 /* As print_file_name, but search for executable file.  */
109
110 static const char *print_prog_name = NULL;
111
112 /* Flag saying to print the relative path we'd use to
113    find libgcc.a given the current compiler flags.  */
114
115 static int print_multi_directory;
116
117 /* Flag saying to print the list of subdirectories and
118    compiler flags used to select them in a standard form.  */
119
120 static int print_multi_lib;
121
122 /* Flag saying to print the command line options understood by gcc and its
123    sub-processes.  */
124
125 static int print_help_list;
126
127 /* Flag indicating whether we should print the command and arguments */
128
129 static int verbose_flag;
130
131 /* Nonzero means write "temp" files in source directory
132    and use the source file's name in them, and don't delete them.  */
133
134 static int save_temps_flag;
135
136 /* The compiler version.  */
137
138 static char *compiler_version;
139
140 /* The target version specified with -V */
141
142 static char *spec_version = DEFAULT_TARGET_VERSION;
143
144 /* The target machine specified with -b.  */
145
146 static const char *spec_machine = DEFAULT_TARGET_MACHINE;
147
148 /* Nonzero if cross-compiling.
149    When -b is used, the value comes from the `specs' file.  */
150
151 #ifdef CROSS_COMPILE
152 static char *cross_compile = "1";
153 #else
154 static char *cross_compile = "0";
155 #endif
156
157 /* The number of errors that have occurred; the link phase will not be
158    run if this is non-zero.  */
159 static int error_count = 0;
160
161 /* This is the obstack which we use to allocate many strings.  */
162
163 static struct obstack obstack;
164
165 /* This is the obstack to build an environment variable to pass to
166    collect2 that describes all of the relevant switches of what to
167    pass the compiler in building the list of pointers to constructors
168    and destructors.  */
169
170 static struct obstack collect_obstack;
171
172 extern char *version_string;
173
174 /* Forward declaration for prototypes.  */
175 struct path_prefix;
176
177 static void init_spec           PROTO((void));
178 static void read_specs          PROTO((const char *, int));
179 static void set_spec            PROTO((const char *, const char *));
180 static struct compiler *lookup_compiler PROTO((const char *, size_t, const char *));
181 static char *build_search_list  PROTO((struct path_prefix *, const char *, int));
182 static void putenv_from_prefixes PROTO((struct path_prefix *, const char *));
183 static char *find_a_file        PROTO((struct path_prefix *, const char *, int));
184 static void add_prefix          PROTO((struct path_prefix *, const char *,
185                                        const char *, int, int, int *));
186 static char *skip_whitespace    PROTO((char *));
187 static void record_temp_file    PROTO((const char *, int, int));
188 static void delete_if_ordinary  PROTO((const char *));
189 static void delete_temp_files   PROTO((void));
190 static void delete_failure_queue PROTO((void));
191 static void clear_failure_queue PROTO((void));
192 static int check_live_switch    PROTO((int, int));
193 static const char *handle_braces PROTO((const char *));
194 static char *save_string        PROTO((const char *, int));
195 extern int do_spec              PROTO((const char *));
196 static int do_spec_1            PROTO((const char *, int, const char *));
197 static const char *find_file    PROTO((const char *));
198 static int is_directory         PROTO((const char *, const char *, int));
199 static void validate_switches   PROTO((const char *));
200 static void validate_all_switches PROTO((void));
201 static void give_switch         PROTO((int, int, int));
202 static int used_arg             PROTO((const char *, int));
203 static int default_arg          PROTO((const char *, int));
204 static void set_multilib_dir    PROTO((void));
205 static void print_multilib_info PROTO((void));
206 static void pfatal_with_name    PROTO((const char *)) ATTRIBUTE_NORETURN;
207 static void perror_with_name    PROTO((const char *));
208 static void pfatal_pexecute     PROTO((const char *, const char *))
209   ATTRIBUTE_NORETURN;
210 static void error               PVPROTO((const char *, ...))
211   ATTRIBUTE_PRINTF_1;
212 static void notice              PVPROTO((const char *, ...))
213   ATTRIBUTE_PRINTF_1;
214 static void display_help        PROTO((void));
215 static void add_preprocessor_option     PROTO ((const char *, int));
216 static void add_assembler_option        PROTO ((const char *, int));
217 static void add_linker_option           PROTO ((const char *, int));
218 static void process_command             PROTO ((int, char **));
219 static int execute                      PROTO ((void));
220 static void unused_prefix_warnings      PROTO ((struct path_prefix *));
221 static void clear_args                  PROTO ((void));
222 static void fatal_error                 PROTO ((int));
223
224 void fancy_abort                PROTO((void)) ATTRIBUTE_NORETURN;
225
226 /* Called before processing to change/add/remove arguments. */
227 extern void lang_specific_driver PROTO ((void (*) PVPROTO((const char *, ...)),
228                                          int *, char ***, int *));
229
230 /* Called before linking.  Returns 0 on success and -1 on failure. */
231 extern int lang_specific_pre_link ();
232
233 /* Number of extra output files that lang_specific_pre_link may generate. */
234 extern int lang_specific_extra_outfiles;
235 \f
236 /* Specs are strings containing lines, each of which (if not blank)
237 is made up of a program name, and arguments separated by spaces.
238 The program name must be exact and start from root, since no path
239 is searched and it is unreliable to depend on the current working directory.
240 Redirection of input or output is not supported; the subprograms must
241 accept filenames saying what files to read and write.
242
243 In addition, the specs can contain %-sequences to substitute variable text
244 or for conditional text.  Here is a table of all defined %-sequences.
245 Note that spaces are not generated automatically around the results of
246 expanding these sequences; therefore, you can concatenate them together
247 or with constant text in a single argument.
248
249  %%     substitute one % into the program name or argument.
250  %i     substitute the name of the input file being processed.
251  %b     substitute the basename of the input file being processed.
252         This is the substring up to (and not including) the last period
253         and not including the directory.
254  %gSUFFIX
255         substitute a file name that has suffix SUFFIX and is chosen
256         once per compilation, and mark the argument a la %d.  To reduce
257         exposure to denial-of-service attacks, the file name is now
258         chosen in a way that is hard to predict even when previously
259         chosen file names are known.  For example, `%g.s ... %g.o ... %g.s'
260         might turn into `ccUVUUAU.s ccXYAXZ12.o ccUVUUAU.s'.  SUFFIX matches
261         the regexp "[.A-Za-z]*" or the special string "%O", which is
262         treated exactly as if %O had been pre-processed.  Previously, %g
263         was simply substituted with a file name chosen once per compilation,
264         without regard to any appended suffix (which was therefore treated
265         just like ordinary text), making such attacks more likely to succeed.
266  %uSUFFIX
267         like %g, but generates a new temporary file name even if %uSUFFIX
268         was already seen.
269  %USUFFIX
270         substitutes the last file name generated with %uSUFFIX, generating a
271         new one if there is no such last file name.  In the absence of any
272         %uSUFFIX, this is just like %gSUFFIX, except they don't share
273         the same suffix "space", so `%g.s ... %U.s ... %g.s ... %U.s'
274         would involve the generation of two distinct file names, one
275         for each `%g.s' and another for each `%U.s'.  Previously, %U was
276         simply substituted with a file name chosen for the previous %u,
277         without regard to any appended suffix.
278  %d     marks the argument containing or following the %d as a
279         temporary file name, so that that file will be deleted if CC exits
280         successfully.  Unlike %g, this contributes no text to the argument.
281  %w     marks the argument containing or following the %w as the
282         "output file" of this compilation.  This puts the argument
283         into the sequence of arguments that %o will substitute later.
284  %W{...}
285         like %{...} but mark last argument supplied within
286         as a file to be deleted on failure.
287  %o     substitutes the names of all the output files, with spaces
288         automatically placed around them.  You should write spaces
289         around the %o as well or the results are undefined.
290         %o is for use in the specs for running the linker.
291         Input files whose names have no recognized suffix are not compiled
292         at all, but they are included among the output files, so they will
293         be linked.
294  %O     substitutes the suffix for object files.  Note that this is
295         handled specially when it immediately follows %g, %u, or %U,
296         because of the need for those to form complete file names.  The
297         handling is such that %O is treated exactly as if it had already
298         been substituted, except that %g, %u, and %U do not currently
299         support additional SUFFIX characters following %O as they would
300         following, for example, `.o'.
301  %p     substitutes the standard macro predefinitions for the
302         current target machine.  Use this when running cpp.
303  %P     like %p, but puts `__' before and after the name of each macro.
304         (Except macros that already have __.)
305         This is for ANSI C.
306  %I     Substitute a -iprefix option made from GCC_EXEC_PREFIX.
307  %s     current argument is the name of a library or startup file of some sort.
308         Search for that file in a standard list of directories
309         and substitute the full name found.
310  %eSTR  Print STR as an error message.  STR is terminated by a newline.
311         Use this when inconsistent options are detected.
312  %x{OPTION}     Accumulate an option for %X.
313  %X     Output the accumulated linker options specified by compilations.
314  %Y     Output the accumulated assembler options specified by compilations.
315  %Z     Output the accumulated preprocessor options specified by compilations.
316  %v1    Substitute the major version number of GCC.
317         (For version 2.5.n, this is 2.)
318  %v2    Substitute the minor version number of GCC.
319         (For version 2.5.n, this is 5.)
320  %a     process ASM_SPEC as a spec.
321         This allows config.h to specify part of the spec for running as.
322  %A     process ASM_FINAL_SPEC as a spec.  A capital A is actually
323         used here.  This can be used to run a post-processor after the
324         assembler has done its job.
325  %D     Dump out a -L option for each directory in startfile_prefixes.
326         If multilib_dir is set, extra entries are generated with it affixed.
327  %l     process LINK_SPEC as a spec.
328  %L     process LIB_SPEC as a spec.
329  %G     process LIBGCC_SPEC as a spec.
330  %S     process STARTFILE_SPEC as a spec.  A capital S is actually used here.
331  %E     process ENDFILE_SPEC as a spec.  A capital E is actually used here.
332  %c     process SIGNED_CHAR_SPEC as a spec.
333  %C     process CPP_SPEC as a spec.  A capital C is actually used here.
334  %1     process CC1_SPEC as a spec.
335  %2     process CC1PLUS_SPEC as a spec.
336  %|     output "-" if the input for the current command is coming from a pipe.
337  %*     substitute the variable part of a matched option.  (See below.)
338         Note that each comma in the substituted string is replaced by
339         a single space.
340  %{S}   substitutes the -S switch, if that switch was given to CC.
341         If that switch was not specified, this substitutes nothing.
342         Here S is a metasyntactic variable.
343  %{S*}  substitutes all the switches specified to CC whose names start
344         with -S.  This is used for -o, -D, -I, etc; switches that take
345         arguments.  CC considers `-o foo' as being one switch whose
346         name starts with `o'.  %{o*} would substitute this text,
347         including the space; thus, two arguments would be generated.
348  %{^S*} likewise, but don't put a blank between a switch and any args.
349  %{S*:X} substitutes X if one or more switches whose names start with -S are
350         specified to CC.  Note that the tail part of the -S option
351         (i.e. the part matched by the `*') will be substituted for each
352         occurrence of %* within X.
353  %{S:X} substitutes X, but only if the -S switch was given to CC.
354  %{!S:X} substitutes X, but only if the -S switch was NOT given to CC.
355  %{|S:X} like %{S:X}, but if no S switch, substitute `-'.
356  %{|!S:X} like %{!S:X}, but if there is an S switch, substitute `-'.
357  %{.S:X} substitutes X, but only if processing a file with suffix S.
358  %{!.S:X} substitutes X, but only if NOT processing a file with suffix S.
359  %{S|P:X} substitutes X if either -S or -P was given to CC.  This may be
360           combined with ! and . as above binding stronger than the OR.
361  %(Spec) processes a specification defined in a specs file as *Spec:
362  %[Spec] as above, but put __ around -D arguments
363
364 The conditional text X in a %{S:X} or %{!S:X} construct may contain
365 other nested % constructs or spaces, or even newlines.  They are
366 processed as usual, as described above.
367
368 The -O, -f, -m, and -W switches are handled specifically in these
369 constructs.  If another value of -O or the negated form of a -f, -m, or
370 -W switch is found later in the command line, the earlier switch
371 value is ignored, except with {S*} where S is just one letter; this
372 passes all matching options.
373
374 The character | at the beginning of the predicate text is used to indicate
375 that a command should be piped to the following command, but only if -pipe
376 is specified.
377
378 Note that it is built into CC which switches take arguments and which
379 do not.  You might think it would be useful to generalize this to
380 allow each compiler's spec to say which switches take arguments.  But
381 this cannot be done in a consistent fashion.  CC cannot even decide
382 which input files have been specified without knowing which switches
383 take arguments, and it must know which input files to compile in order
384 to tell which compilers to run.
385
386 CC also knows implicitly that arguments starting in `-l' are to be
387 treated as compiler output files, and passed to the linker in their
388 proper position among the other output files.  */
389 \f
390 /* Define the macros used for specs %a, %l, %L, %S, %c, %C, %1.  */
391
392 /* config.h can define ASM_SPEC to provide extra args to the assembler
393    or extra switch-translations.  */
394 #ifndef ASM_SPEC
395 #define ASM_SPEC ""
396 #endif
397
398 /* config.h can define ASM_FINAL_SPEC to run a post processor after
399    the assembler has run.  */
400 #ifndef ASM_FINAL_SPEC
401 #define ASM_FINAL_SPEC ""
402 #endif
403
404 /* config.h can define CPP_SPEC to provide extra args to the C preprocessor
405    or extra switch-translations.  */
406 #ifndef CPP_SPEC
407 #define CPP_SPEC ""
408 #endif
409
410 /* config.h can define CC1_SPEC to provide extra args to cc1 and cc1plus
411    or extra switch-translations.  */
412 #ifndef CC1_SPEC
413 #define CC1_SPEC ""
414 #endif
415
416 /* config.h can define CC1PLUS_SPEC to provide extra args to cc1plus
417    or extra switch-translations.  */
418 #ifndef CC1PLUS_SPEC
419 #define CC1PLUS_SPEC ""
420 #endif
421
422 /* config.h can define LINK_SPEC to provide extra args to the linker
423    or extra switch-translations.  */
424 #ifndef LINK_SPEC
425 #define LINK_SPEC ""
426 #endif
427
428 /* config.h can define LIB_SPEC to override the default libraries.  */
429 #ifndef LIB_SPEC
430 #define LIB_SPEC "%{!shared:%{g*:-lg} %{!p:%{!pg:-lc}}%{p:-lc_p}%{pg:-lc_p}}"
431 #endif
432
433 /* config.h can define LIBGCC_SPEC to override how and when libgcc.a is
434    included.  */
435 #ifndef LIBGCC_SPEC
436 #if defined(LINK_LIBGCC_SPECIAL) || defined(LINK_LIBGCC_SPECIAL_1)
437 /* Have gcc do the search for libgcc.a.  */
438 #define LIBGCC_SPEC "libgcc.a%s"
439 #else
440 #define LIBGCC_SPEC "-lgcc"
441 #endif
442 #endif
443
444 /* config.h can define STARTFILE_SPEC to override the default crt0 files.  */
445 #ifndef STARTFILE_SPEC
446 #define STARTFILE_SPEC  \
447   "%{!shared:%{pg:gcrt0%O%s}%{!pg:%{p:mcrt0%O%s}%{!p:crt0%O%s}}}"
448 #endif
449
450 /* config.h can define SWITCHES_NEED_SPACES to control which options
451    require spaces between the option and the argument.  */
452 #ifndef SWITCHES_NEED_SPACES
453 #define SWITCHES_NEED_SPACES ""
454 #endif
455
456 /* config.h can define ENDFILE_SPEC to override the default crtn files.  */
457 #ifndef ENDFILE_SPEC
458 #define ENDFILE_SPEC ""
459 #endif
460
461 /* This spec is used for telling cpp whether char is signed or not.  */
462 #ifndef SIGNED_CHAR_SPEC
463 /* Use #if rather than ?:
464    because MIPS C compiler rejects like ?: in initializers.  */
465 #if DEFAULT_SIGNED_CHAR
466 #define SIGNED_CHAR_SPEC "%{funsigned-char:-D__CHAR_UNSIGNED__}"
467 #else
468 #define SIGNED_CHAR_SPEC "%{!fsigned-char:-D__CHAR_UNSIGNED__}"
469 #endif
470 #endif
471
472 #ifndef LINKER_NAME
473 #define LINKER_NAME "collect2"
474 #endif
475
476 static char *cpp_spec = CPP_SPEC;
477 static char *cpp_predefines = CPP_PREDEFINES;
478 static char *cc1_spec = CC1_SPEC;
479 static char *cc1plus_spec = CC1PLUS_SPEC;
480 static char *signed_char_spec = SIGNED_CHAR_SPEC;
481 static char *asm_spec = ASM_SPEC;
482 static char *asm_final_spec = ASM_FINAL_SPEC;
483 static char *link_spec = LINK_SPEC;
484 static char *lib_spec = LIB_SPEC;
485 static char *libgcc_spec = LIBGCC_SPEC;
486 static char *endfile_spec = ENDFILE_SPEC;
487 static char *startfile_spec = STARTFILE_SPEC;
488 static char *switches_need_spaces = SWITCHES_NEED_SPACES;
489 static char *linker_name_spec = LINKER_NAME;
490
491 /* Some compilers have limits on line lengths, and the multilib_select
492    and/or multilib_matches strings can be very long, so we build them at
493    run time.  */
494 static struct obstack multilib_obstack;
495 static char *multilib_select;
496 static char *multilib_matches;
497 static char *multilib_defaults;
498 #include "multilib.h"
499
500 /* Check whether a particular argument is a default argument.  */
501
502 #ifndef MULTILIB_DEFAULTS
503 #define MULTILIB_DEFAULTS { "" }
504 #endif
505
506 static char *multilib_defaults_raw[] = MULTILIB_DEFAULTS;
507
508 struct user_specs {
509   struct user_specs *next;
510   const char *filename;
511 };
512
513 static struct user_specs *user_specs_head, *user_specs_tail;
514
515 /* This defines which switch letters take arguments.  */
516
517 #define DEFAULT_SWITCH_TAKES_ARG(CHAR) \
518   ((CHAR) == 'D' || (CHAR) == 'U' || (CHAR) == 'o' \
519    || (CHAR) == 'e' || (CHAR) == 'T' || (CHAR) == 'u' \
520    || (CHAR) == 'I' || (CHAR) == 'm' || (CHAR) == 'x' \
521    || (CHAR) == 'L' || (CHAR) == 'A' || (CHAR) == 'V' \
522    || (CHAR) == 'B' || (CHAR) == 'b')
523
524 #ifndef SWITCH_TAKES_ARG
525 #define SWITCH_TAKES_ARG(CHAR) DEFAULT_SWITCH_TAKES_ARG(CHAR)
526 #endif
527
528 /* This defines which multi-letter switches take arguments.  */
529
530 #define DEFAULT_WORD_SWITCH_TAKES_ARG(STR)              \
531  (!strcmp (STR, "Tdata") || !strcmp (STR, "Ttext")      \
532   || !strcmp (STR, "Tbss") || !strcmp (STR, "include")  \
533   || !strcmp (STR, "imacros") || !strcmp (STR, "aux-info") \
534   || !strcmp (STR, "idirafter") || !strcmp (STR, "iprefix") \
535   || !strcmp (STR, "iwithprefix") || !strcmp (STR, "iwithprefixbefore") \
536   || !strcmp (STR, "isystem") || !strcmp (STR, "specs"))
537
538 #ifndef WORD_SWITCH_TAKES_ARG
539 #define WORD_SWITCH_TAKES_ARG(STR) DEFAULT_WORD_SWITCH_TAKES_ARG (STR)
540 #endif
541 \f
542
543 #ifdef HAVE_EXECUTABLE_SUFFIX
544 /* This defines which switches stop a full compilation.  */
545 #define DEFAULT_SWITCH_CURTAILS_COMPILATION(CHAR) \
546   ((CHAR) == 'c' || (CHAR) == 'S')
547
548 #ifndef SWITCH_CURTAILS_COMPILATION
549 #define SWITCH_CURTAILS_COMPILATION(CHAR) \
550   DEFAULT_SWITCH_CURTAILS_COMPILATION(CHAR)
551 #endif
552 #endif
553
554 /* Record the mapping from file suffixes for compilation specs.  */
555
556 struct compiler
557 {
558   const char *suffix;           /* Use this compiler for input files
559                                    whose names end in this suffix.  */
560
561   const char *spec[4];          /* To use this compiler, concatenate these
562                                    specs and pass to do_spec.  */
563 };
564
565 /* Pointer to a vector of `struct compiler' that gives the spec for
566    compiling a file, based on its suffix.
567    A file that does not end in any of these suffixes will be passed
568    unchanged to the loader and nothing else will be done to it.
569
570    An entry containing two 0s is used to terminate the vector.
571
572    If multiple entries match a file, the last matching one is used.  */
573
574 static struct compiler *compilers;
575
576 /* Number of entries in `compilers', not counting the null terminator.  */
577
578 static int n_compilers;
579
580 /* The default list of file name suffixes and their compilation specs.  */
581
582 static struct compiler default_compilers[] =
583 {
584   /* Add lists of suffixes of known languages here.  If those languages
585      were not present when we built the driver, we will hit these copies
586      and be given a more meaningful error than "file not used since
587      linking is not done".  */
588   {".m", {"#Objective-C"}},
589   {".cc", {"#C++"}}, {".cxx", {"#C++"}}, {".cpp", {"#C++"}},
590   {".c++", {"#C++"}}, {".C", {"#C++"}},
591   {".ads", {"#Ada"}}, {".adb", {"#Ada"}}, {".ada", {"#Ada"}},
592   {".f", {"#Fortran"}}, {".for", {"#Fortran"}}, {".F", {"#Fortran"}},
593   {".fpp", {"#Fortran"}},
594   {".p", {"#Pascal"}}, {".pas", {"#Pascal"}},
595   /* Next come the entries for C.  */
596   {".c", {"@c"}},
597   {"@c",
598    {
599 #if USE_CPPLIB
600      "%{E|M|MM:cpp0 -lang-c %{ansi:-std=c89} %{std*} %{nostdinc*}\
601         %{C} %{v} %{A*} %{I*} %{P} %{$} %I\
602         %{C:%{!E:%eGNU C does not support -C without using -E}}\
603         %{M} %{MM} %{MD:-MD %b.d} %{MMD:-MMD %b.d} %{MG}\
604         %{!no-gcc:-D__GNUC__=%v1 -D__GNUC_MINOR__=%v2}\
605         %{ansi|std=*:%{!std=gnu*:-trigraphs -D__STRICT_ANSI__}}\
606         %{!undef:%{!ansi:%{!std=*:%p}%{std=gnu*:%p}} %P} %{trigraphs}\
607         %c %{Os:-D__OPTIMIZE_SIZE__} %{O*:%{!O0:-D__OPTIMIZE__}}\
608         %{ffast-math:-D__FAST_MATH__}\
609         %{traditional} %{ftraditional:-traditional}\
610         %{traditional-cpp:-traditional}\
611         %{fleading-underscore} %{fno-leading-underscore}\
612         %{g*} %{W*} %{w} %{pedantic*} %{H} %{d*} %C %{D*} %{U*} %{i*} %Z\
613         %i %{E:%W{o*}}%{M:%W{o*}}%{MM:%W{o*}}\n}\
614       %{!E:%{!M:%{!MM:cc1 %i %1 \
615                   %{std*} %{nostdinc*} %{A*} %{I*} %I\
616                   %{!Q:-quiet} -dumpbase %b.c %{d*} %{m*} %{a*}\
617                   %{MD:-MD %b.d} %{MMD:-MMD %b.d} %{MG}\
618                   %{!no-gcc:-D__GNUC__=%v1 -D__GNUC_MINOR__=%v2}\
619                   %{ansi|std=*:%{!std=gnu*:-trigraphs -D__STRICT_ANSI__}}\
620                   %{!undef:%{!ansi:%{!std=*:%p}%{std=gnu*:%p}} %P} %{trigraphs}\
621                   %c %{Os:-D__OPTIMIZE_SIZE__} %{O*:%{!O0:-D__OPTIMIZE__}}\
622                   %{ffast-math:-D__FAST_MATH__}\
623                   %{H} %C %{D*} %{U*} %{i*} %Z\
624                   %{ftraditional:-traditional}\
625                   %{traditional-cpp:-traditional}\
626                   %{traditional} %{v:-version} %{pg:-p} %{p} %{f*}\
627                   %{aux-info*} %{Qn:-fno-ident}\
628                   %{--help:--help}\
629                   %{g*} %{O*} %{W*} %{w} %{pedantic*}\
630                   %{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
631                   %{S:%W{o*}%{!o*:-o %b.s}}%{!S:-o %{|!pipe:%g.s}} |\n\
632                   %{!S:as %a %Y\
633                      %{c:%W{o*}%{!o*:-o %w%b%O}}%{!c:-o %d%w%u%O}\
634                      %{!pipe:%g.s} %A\n }}}}"
635 #else /* ! USE_CPPLIB */
636     "cpp0 -lang-c %{ansi:-std=c89} %{std*} %{nostdinc*}\
637         %{C} %{v} %{A*} %{I*} %{P} %{$} %I\
638         %{C:%{!E:%eGNU C does not support -C without using -E}}\
639         %{M} %{MM} %{MD:-MD %b.d} %{MMD:-MMD %b.d} %{MG}\
640         %{!no-gcc:-D__GNUC__=%v1 -D__GNUC_MINOR__=%v2}\
641         %{ansi|std=*:%{!std=gnu*:-trigraphs -D__STRICT_ANSI__}}\
642         %{!undef:%{!ansi:%{!std=*:%p}%{std=gnu*:%p}} %P} %{trigraphs}\
643         %c %{Os:-D__OPTIMIZE_SIZE__} %{O*:%{!O0:-D__OPTIMIZE__}}\
644         %{ffast-math:-D__FAST_MATH__}\
645         %{traditional} %{ftraditional:-traditional}\
646         %{traditional-cpp:-traditional}\
647         %{fleading-underscore} %{fno-leading-underscore}\
648         %{g*} %{W*} %{w} %{pedantic*} %{H} %{d*} %C %{D*} %{U*} %{i*} %Z\
649         %i %{!M:%{!MM:%{!E:%{!pipe:%g.i}}}}%{E:%W{o*}}%{M:%W{o*}}%{MM:%W{o*}} |\n",
650    "%{!M:%{!MM:%{!E:cc1 %{!pipe:%g.i} %1 \
651                    %{!Q:-quiet} -dumpbase %b.c %{d*} %{m*} %{a*}\
652                    %{g*} %{O*} %{W*} %{w} %{pedantic*} %{std*}\
653                    %{traditional} %{v:-version} %{pg:-p} %{p} %{f*}\
654                    %{aux-info*} %{Qn:-fno-ident}\
655                    %{--help:--help} \
656                    %{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
657                    %{S:%W{o*}%{!o*:-o %b.s}}%{!S:-o %{|!pipe:%g.s}} |\n\
658               %{!S:as %a %Y\
659                       %{c:%W{o*}%{!o*:-o %w%b%O}}%{!c:-o %d%w%u%O}\
660                       %{!pipe:%g.s} %A\n }}}}"
661 #endif /* ! USE_CPPLIB */
662   }},
663   {"-",
664    {"%{E:cpp0 -lang-c %{ansi:-std=c89} %{std*} %{nostdinc*}\
665         %{C} %{v} %{A*} %{I*} %{P} %{$} %I\
666         %{C:%{!E:%eGNU C does not support -C without using -E}}\
667         %{M} %{MM} %{MD:-MD %b.d} %{MMD:-MMD %b.d} %{MG}\
668         %{!no-gcc:-D__GNUC__=%v1 -D__GNUC_MINOR__=%v2}\
669         %{ansi|std=*:%{!std=gnu*:-trigraphs -D__STRICT_ANSI__}}\
670         %{!undef:%{!ansi:%{!std=*:%p}%{std=gnu*:%p}} %P} %{trigraphs}\
671         %c %{Os:-D__OPTIMIZE_SIZE__} %{O*:%{!O0:-D__OPTIMIZE__}}\
672         %{ffast-math:-D__FAST_MATH__}\
673         %{traditional} %{ftraditional:-traditional}\
674         %{traditional-cpp:-traditional}\
675         %{fleading-underscore} %{fno-leading-underscore}\
676         %{g*} %{W*} %{w} %{pedantic*} %{H} %{d*} %C %{D*} %{U*} %{i*} %Z\
677         %i %W{o*}}\
678     %{!E:%e-E required when input is from standard input}"}},
679   {".h", {"@c-header"}},
680   {"@c-header",
681    {"%{!E:%eCompilation of header file requested} \
682     cpp0 %{nostdinc*} %{C} %{v} %{A*} %{I*} %{P} %{$} %I\
683         %{C:%{!E:%eGNU C does not support -C without using -E}}\
684         %{M} %{MM} %{MD:-MD %b.d} %{MMD:-MMD %b.d} %{MG}\
685         %{!no-gcc:-D__GNUC__=%v1 -D__GNUC_MINOR__=%v2}\
686         %{std=*:%{!std=gnu*:-trigraphs -D__STRICT_ANSI__}}\
687         %{!undef:%{!std=*:%p}%{std=gnu*:%p} %P} %{trigraphs}\
688         %c %{Os:-D__OPTIMIZE_SIZE__} %{O*:%{!O0:-D__OPTIMIZE__}}\
689         %{ffast-math:-D__FAST_MATH__}\
690         %{traditional} %{ftraditional:-traditional}\
691         %{traditional-cpp:-traditional}\
692         %{fleading-underscore} %{fno-leading-underscore}\
693         %{g*} %{W*} %{w} %{pedantic*} %{H} %{d*} %C %{D*} %{U*} %{i*} %Z\
694         %i %W{o*}"}},
695   {".i", {"@cpp-output"}},
696   {"@cpp-output",
697    {"%{!M:%{!MM:%{!E:cc1 %i %1 %{!Q:-quiet} %{d*} %{m*} %{a*}\
698                         %{g*} %{O*} %{W*} %{w} %{pedantic*} %{std*}\
699                         %{traditional} %{v:-version} %{pg:-p} %{p} %{f*}\
700                         %{aux-info*} %{Qn:-fno-ident}\
701                         %{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
702                         %{S:%W{o*}%{!o*:-o %b.s}}%{!S:-o %{|!pipe:%g.s}} |\n\
703                      %{!S:as %a %Y\
704                              %{c:%W{o*}%{!o*:-o %w%b%O}}%{!c:-o %d%w%u%O}\
705                              %{!pipe:%g.s} %A\n }}}}"}},
706   {".s", {"@assembler"}},
707   {"@assembler",
708    {"%{!M:%{!MM:%{!E:%{!S:as %a %Y\
709                             %{c:%W{o*}%{!o*:-o %w%b%O}}%{!c:-o %d%w%u%O}\
710                             %i %A\n }}}}"}},
711   {".S", {"@assembler-with-cpp"}},
712   {"@assembler-with-cpp",
713    {"cpp0 -lang-asm %{nostdinc*} %{C} %{v} %{A*} %{I*} %{P} %{$} %I\
714         %{C:%{!E:%eGNU C does not support -C without using -E}}\
715         %{M} %{MM} %{MD:-MD %b.d} %{MMD:-MMD %b.d} %{MG} %{trigraphs}\
716         -$ %{!undef:%p %P} -D__ASSEMBLER__ \
717         %c %{Os:-D__OPTIMIZE_SIZE__} %{O*:%{!O0:-D__OPTIMIZE__}}\
718         %{ffast-math:-D__FAST_MATH__}\
719         %{traditional} %{ftraditional:-traditional}\
720         %{traditional-cpp:-traditional}\
721         %{fleading-underscore} %{fno-leading-underscore}\
722         %{g*} %{W*} %{w} %{pedantic*} %{H} %{d*} %C %{D*} %{U*} %{i*} %Z\
723         %i %{!M:%{!MM:%{!E:%{!pipe:%g.s}}}}%{E:%W{o*}}%{M:%W{o*}}%{MM:%W{o*}} |\n",
724     "%{!M:%{!MM:%{!E:%{!S:as %a %Y\
725                     %{c:%W{o*}%{!o*:-o %w%b%O}}%{!c:-o %d%w%u%O}\
726                     %{!pipe:%g.s} %A\n }}}}"}},
727 #include "specs.h"
728   /* Mark end of table */
729   {0, {0}}
730 };
731
732 /* Number of elements in default_compilers, not counting the terminator.  */
733
734 static int n_default_compilers
735   = (sizeof default_compilers / sizeof (struct compiler)) - 1;
736
737 /* Here is the spec for running the linker, after compiling all files.  */
738
739 /* -u* was put back because both BSD and SysV seem to support it.  */
740 /* %{static:} simply prevents an error message if the target machine
741    doesn't handle -static.  */
742 /* We want %{T*} after %{L*} and %D so that it can be used to specify linker
743    scripts which exist in user specified directories, or in standard
744    directories.  */
745 #ifdef LINK_COMMAND_SPEC
746 /* Provide option to override link_command_spec from machine specific
747    configuration files.  */
748 static const char *link_command_spec = 
749         LINK_COMMAND_SPEC;
750 #else
751 #ifdef LINK_LIBGCC_SPECIAL
752 /* Don't generate -L options.  */
753 static const char *link_command_spec = "\
754 %{!fsyntax-only: \
755  %{!c:%{!M:%{!MM:%{!E:%{!S:%(linker) %l %X %{o*} %{A} %{d} %{e*} %{m} %{N} %{n} \
756                         %{r} %{s} %{t} %{u*} %{x} %{z} %{Z}\
757                         %{!A:%{!nostdlib:%{!nostartfiles:%S}}}\
758                         %{static:} %{L*} %o\
759                         %{!nostdlib:%{!nodefaultlibs:%G %L %G}}\
760                         %{!A:%{!nostdlib:%{!nostartfiles:%E}}}\
761                         %{T*}\
762                         \n }}}}}}";
763 #else
764 /* Use -L.  */
765 static const char *link_command_spec = "\
766 %{!fsyntax-only: \
767  %{!c:%{!M:%{!MM:%{!E:%{!S:%(linker) %l %X %{o*} %{A} %{d} %{e*} %{m} %{N} %{n} \
768                         %{r} %{s} %{t} %{u*} %{x} %{z} %{Z}\
769                         %{!A:%{!nostdlib:%{!nostartfiles:%S}}}\
770                         %{static:} %{L*} %D %o\
771                         %{!nostdlib:%{!nodefaultlibs:%G %L %G}}\
772                         %{!A:%{!nostdlib:%{!nostartfiles:%E}}}\
773                         %{T*}\
774                         \n }}}}}}";
775 #endif
776 #endif
777
778 /* A vector of options to give to the linker.
779    These options are accumulated by %x,
780    and substituted into the linker command with %X.  */
781 static int n_linker_options;
782 static char **linker_options;
783
784 /* A vector of options to give to the assembler.
785    These options are accumulated by -Wa,
786    and substituted into the assembler command with %Y.  */
787 static int n_assembler_options;
788 static char **assembler_options;
789
790 /* A vector of options to give to the preprocessor.
791    These options are accumulated by -Wp,
792    and substituted into the preprocessor command with %Z.  */
793 static int n_preprocessor_options;
794 static char **preprocessor_options;
795 \f
796 /* Define how to map long options into short ones.  */
797
798 /* This structure describes one mapping.  */
799 struct option_map
800 {
801   /* The long option's name.  */
802   const char *name;
803   /* The equivalent short option.  */
804   const char *equivalent;
805   /* Argument info.  A string of flag chars; NULL equals no options.
806      a => argument required.
807      o => argument optional.
808      j => join argument to equivalent, making one word.
809      * => require other text after NAME as an argument.  */
810   const char *arg_info;
811 };
812
813 /* This is the table of mappings.  Mappings are tried sequentially
814    for each option encountered; the first one that matches, wins.  */
815
816 struct option_map option_map[] =
817  {
818    {"--all-warnings", "-Wall", 0},
819    {"--ansi", "-ansi", 0},
820    {"--assemble", "-S", 0},
821    {"--assert", "-A", "a"},
822    {"--classpath", "-fclasspath=", "aj"},
823    {"--CLASSPATH", "-fCLASSPATH=", "aj"},
824    {"--comments", "-C", 0},
825    {"--compile", "-c", 0},
826    {"--debug", "-g", "oj"},
827    {"--define-macro", "-D", "aj"},
828    {"--dependencies", "-M", 0},
829    {"--dump", "-d", "a"},
830    {"--dumpbase", "-dumpbase", "a"},
831    {"--entry", "-e", 0},
832    {"--extra-warnings", "-W", 0},
833    {"--for-assembler", "-Wa", "a"},
834    {"--for-linker", "-Xlinker", "a"},
835    {"--force-link", "-u", "a"},
836    {"--imacros", "-imacros", "a"},
837    {"--include", "-include", "a"},
838    {"--include-barrier", "-I-", 0},
839    {"--include-directory", "-I", "aj"},
840    {"--include-directory-after", "-idirafter", "a"},
841    {"--include-prefix", "-iprefix", "a"},
842    {"--include-with-prefix", "-iwithprefix", "a"},
843    {"--include-with-prefix-before", "-iwithprefixbefore", "a"},
844    {"--include-with-prefix-after", "-iwithprefix", "a"},
845    {"--language", "-x", "a"},
846    {"--library-directory", "-L", "a"},
847    {"--machine", "-m", "aj"},
848    {"--machine-", "-m", "*j"},
849    {"--no-line-commands", "-P", 0},
850    {"--no-precompiled-includes", "-noprecomp", 0},
851    {"--no-standard-includes", "-nostdinc", 0},
852    {"--no-standard-libraries", "-nostdlib", 0},
853    {"--no-warnings", "-w", 0},
854    {"--optimize", "-O", "oj"},
855    {"--output", "-o", "a"},
856    {"--output-class-directory", "-foutput-class-dir=", "ja"},
857    {"--pedantic", "-pedantic", 0},
858    {"--pedantic-errors", "-pedantic-errors", 0},
859    {"--pipe", "-pipe", 0},
860    {"--prefix", "-B", "a"},
861    {"--preprocess", "-E", 0},
862    {"--print-search-dirs", "-print-search-dirs", 0},
863    {"--print-file-name", "-print-file-name=", "aj"},
864    {"--print-libgcc-file-name", "-print-libgcc-file-name", 0},
865    {"--print-missing-file-dependencies", "-MG", 0},
866    {"--print-multi-lib", "-print-multi-lib", 0},
867    {"--print-multi-directory", "-print-multi-directory", 0},
868    {"--print-prog-name", "-print-prog-name=", "aj"},
869    {"--profile", "-p", 0},
870    {"--profile-blocks", "-a", 0},
871    {"--quiet", "-q", 0},
872    {"--save-temps", "-save-temps", 0},
873    {"--shared", "-shared", 0},
874    {"--silent", "-q", 0},
875    {"--specs", "-specs=", "aj"},
876    {"--static", "-static", 0},
877    {"--std", "-std=", "aj"},
878    {"--symbolic", "-symbolic", 0},
879    {"--target", "-b", "a"},
880    {"--trace-includes", "-H", 0},
881    {"--traditional", "-traditional", 0},
882    {"--traditional-cpp", "-traditional-cpp", 0},
883    {"--trigraphs", "-trigraphs", 0},
884    {"--undefine-macro", "-U", "aj"},
885    {"--use-version", "-V", "a"},
886    {"--user-dependencies", "-MM", 0},
887    {"--verbose", "-v", 0},
888    {"--version", "-dumpversion", 0},
889    {"--warn-", "-W", "*j"},
890    {"--write-dependencies", "-MD", 0},
891    {"--write-user-dependencies", "-MMD", 0},
892    {"--", "-f", "*j"}
893  };
894 \f
895 /* Translate the options described by *ARGCP and *ARGVP.
896    Make a new vector and store it back in *ARGVP,
897    and store its length in *ARGVC.  */
898
899 static void
900 translate_options (argcp, argvp)
901      int *argcp;
902      const char ***argvp;
903 {
904   int i;
905   int argc = *argcp;
906   const char **argv = *argvp;
907   const char **newv =
908     (const char **) xmalloc ((argc + 2) * 2 * sizeof (const char *));
909   int newindex = 0;
910
911   i = 0;
912   newv[newindex++] = argv[i++];
913
914   while (i < argc)
915     {
916       /* Translate -- options.  */
917       if (argv[i][0] == '-' && argv[i][1] == '-')
918         {
919           size_t j;
920           /* Find a mapping that applies to this option.  */
921           for (j = 0; j < sizeof (option_map) / sizeof (option_map[0]); j++)
922             {
923               size_t optlen = strlen (option_map[j].name);
924               size_t arglen = strlen (argv[i]);
925               size_t complen = arglen > optlen ? optlen : arglen;
926               const char *arginfo = option_map[j].arg_info;
927
928               if (arginfo == 0)
929                 arginfo = "";
930
931               if (!strncmp (argv[i], option_map[j].name, complen))
932                 {
933                   const char *arg = 0;
934
935                   if (arglen < optlen)
936                     {
937                       size_t k;
938                       for (k = j + 1;
939                            k < sizeof (option_map) / sizeof (option_map[0]);
940                            k++)
941                         if (strlen (option_map[k].name) >= arglen
942                             && !strncmp (argv[i], option_map[k].name, arglen))
943                           {
944                             error ("Ambiguous abbreviation %s", argv[i]);
945                             break;
946                           }
947
948                       if (k != sizeof (option_map) / sizeof (option_map[0]))
949                         break;
950                     }
951
952                   if (arglen > optlen)
953                     {
954                       /* If the option has an argument, accept that.  */
955                       if (argv[i][optlen] == '=')
956                         arg = argv[i] + optlen + 1;
957
958                       /* If this mapping requires extra text at end of name,
959                          accept that as "argument".  */
960                       else if (index (arginfo, '*') != 0)
961                         arg = argv[i] + optlen;
962
963                       /* Otherwise, extra text at end means mismatch.
964                          Try other mappings.  */
965                       else
966                         continue;
967                     }
968
969                   else if (index (arginfo, '*') != 0)
970                     {
971                       error ("Incomplete `%s' option", option_map[j].name);
972                       break;
973                     }
974
975                   /* Handle arguments.  */
976                   if (index (arginfo, 'a') != 0)
977                     {
978                       if (arg == 0)
979                         {
980                           if (i + 1 == argc)
981                             {
982                               error ("Missing argument to `%s' option",
983                                      option_map[j].name);
984                               break;
985                             }
986
987                           arg = argv[++i];
988                         }
989                     }
990                   else if (index (arginfo, '*') != 0)
991                     ;
992                   else if (index (arginfo, 'o') == 0)
993                     {
994                       if (arg != 0)
995                         error ("Extraneous argument to `%s' option",
996                                option_map[j].name);
997                       arg = 0;
998                     }
999
1000                   /* Store the translation as one argv elt or as two.  */
1001                   if (arg != 0 && index (arginfo, 'j') != 0)
1002                     newv[newindex++] = concat (option_map[j].equivalent, arg,
1003                                                NULL_PTR);
1004                   else if (arg != 0)
1005                     {
1006                       newv[newindex++] = option_map[j].equivalent;
1007                       newv[newindex++] = arg;
1008                     }
1009                   else
1010                     newv[newindex++] = option_map[j].equivalent;
1011
1012                   break;
1013                 }
1014             }
1015           i++;
1016         }
1017
1018       /* Handle old-fashioned options--just copy them through,
1019          with their arguments.  */
1020       else if (argv[i][0] == '-')
1021         {
1022           const char *p = argv[i] + 1;
1023           int c = *p;
1024           int nskip = 1;
1025
1026           if (SWITCH_TAKES_ARG (c) > (p[1] != 0))
1027             nskip += SWITCH_TAKES_ARG (c) - (p[1] != 0);
1028           else if (WORD_SWITCH_TAKES_ARG (p))
1029             nskip += WORD_SWITCH_TAKES_ARG (p);
1030           else if ((c == 'B' || c == 'b' || c == 'V' || c == 'x')
1031                    && p[1] == 0)
1032             nskip += 1;
1033           else if (! strcmp (p, "Xlinker"))
1034             nskip += 1;
1035
1036           /* Watch out for an option at the end of the command line that
1037              is missing arguments, and avoid skipping past the end of the
1038              command line.  */
1039           if (nskip + i > argc)
1040             nskip = argc - i;
1041
1042           while (nskip > 0)
1043             {
1044               newv[newindex++] = argv[i++];
1045               nskip--;
1046             }
1047         }
1048       else
1049         /* Ordinary operands, or +e options.  */
1050         newv[newindex++] = argv[i++];
1051     }
1052
1053   newv[newindex] = 0;
1054
1055   *argvp = newv;
1056   *argcp = newindex;
1057 }
1058 \f
1059 char *
1060 xstrerror(e)
1061      int e;
1062 {
1063 #ifdef HAVE_STRERROR
1064
1065   return strerror(e);
1066
1067 #else
1068
1069   if (!e)
1070     return "errno = 0";
1071
1072   if (e > 0 && e < sys_nerr)
1073     return sys_errlist[e];
1074
1075   return "errno = ?";
1076 #endif
1077 }
1078 \f
1079 static char *
1080 skip_whitespace (p)
1081      char *p;
1082 {
1083   while (1)
1084     {
1085       /* A fully-blank line is a delimiter in the SPEC file and shouldn't
1086          be considered whitespace.  */
1087       if (p[0] == '\n' && p[1] == '\n' && p[2] == '\n')
1088         return p + 1;
1089       else if (*p == '\n' || *p == ' ' || *p == '\t')
1090         p++;
1091       else if (*p == '#')
1092         {
1093           while (*p != '\n') p++;
1094           p++;
1095         }
1096       else
1097         break;
1098     }
1099
1100   return p;
1101 }
1102 \f
1103 /* Structure to keep track of the specs that have been defined so far.
1104    These are accessed using %(specname) or %[specname] in a compiler
1105    or link spec.  */
1106
1107 struct spec_list
1108 {
1109                                 /* The following 2 fields must be first */
1110                                 /* to allow EXTRA_SPECS to be initialized */
1111   char *name;                   /* name of the spec.  */
1112   char *ptr;                    /* available ptr if no static pointer */
1113
1114                                 /* The following fields are not initialized */
1115                                 /* by EXTRA_SPECS */
1116   char **ptr_spec;              /* pointer to the spec itself.  */
1117   struct spec_list *next;       /* Next spec in linked list.  */
1118   int name_len;                 /* length of the name */
1119   int alloc_p;                  /* whether string was allocated */
1120 };
1121
1122 #define INIT_STATIC_SPEC(NAME,PTR) \
1123 { NAME, NULL_PTR, PTR, (struct spec_list *)0, sizeof (NAME)-1, 0 }
1124
1125 /* List of statically defined specs */
1126 static struct spec_list static_specs[] = {
1127   INIT_STATIC_SPEC ("asm",                      &asm_spec),
1128   INIT_STATIC_SPEC ("asm_final",                &asm_final_spec),
1129   INIT_STATIC_SPEC ("cpp",                      &cpp_spec),
1130   INIT_STATIC_SPEC ("cc1",                      &cc1_spec),
1131   INIT_STATIC_SPEC ("cc1plus",                  &cc1plus_spec),
1132   INIT_STATIC_SPEC ("endfile",                  &endfile_spec),
1133   INIT_STATIC_SPEC ("link",                     &link_spec),
1134   INIT_STATIC_SPEC ("lib",                      &lib_spec),
1135   INIT_STATIC_SPEC ("libgcc",                   &libgcc_spec),
1136   INIT_STATIC_SPEC ("startfile",                &startfile_spec),
1137   INIT_STATIC_SPEC ("switches_need_spaces",     &switches_need_spaces),
1138   INIT_STATIC_SPEC ("signed_char",              &signed_char_spec),
1139   INIT_STATIC_SPEC ("predefines",               &cpp_predefines),
1140   INIT_STATIC_SPEC ("cross_compile",            &cross_compile),
1141   INIT_STATIC_SPEC ("version",                  &compiler_version),
1142   INIT_STATIC_SPEC ("multilib",                 &multilib_select),
1143   INIT_STATIC_SPEC ("multilib_defaults",        &multilib_defaults),
1144   INIT_STATIC_SPEC ("multilib_extra",           &multilib_extra),
1145   INIT_STATIC_SPEC ("multilib_matches",         &multilib_matches),
1146   INIT_STATIC_SPEC ("linker",                   &linker_name_spec),
1147 };
1148
1149 #ifdef EXTRA_SPECS              /* additional specs needed */
1150 /* Structure to keep track of just the first two args of a spec_list.
1151    That is all that the EXTRA_SPECS macro gives us. */
1152 struct spec_list_1
1153 {
1154   char *name;
1155   char *ptr;
1156 };
1157
1158 static struct spec_list_1 extra_specs_1[] = { EXTRA_SPECS };
1159 static struct spec_list * extra_specs = (struct spec_list *)0;
1160 #endif
1161
1162 /* List of dynamically allocates specs that have been defined so far.  */
1163
1164 static struct spec_list *specs = (struct spec_list *)0;
1165
1166 \f
1167 /* Initialize the specs lookup routines.  */
1168
1169 static void
1170 init_spec ()
1171 {
1172   struct spec_list *next = (struct spec_list *)0;
1173   struct spec_list *sl   = (struct spec_list *)0;
1174   int i;
1175
1176   if (specs)
1177     return;                     /* already initialized */
1178
1179   if (verbose_flag)
1180     notice ("Using builtin specs.\n");
1181
1182 #ifdef EXTRA_SPECS
1183   extra_specs = (struct spec_list *)
1184     xmalloc (sizeof(struct spec_list) *
1185              (sizeof(extra_specs_1)/sizeof(extra_specs_1[0])));
1186   bzero ((PTR) extra_specs, sizeof(struct spec_list) *
1187          (sizeof(extra_specs_1)/sizeof(extra_specs_1[0])));
1188   
1189   for (i = (sizeof(extra_specs_1) / sizeof(extra_specs_1[0])) - 1; i >= 0; i--)
1190     {
1191       sl = &extra_specs[i];
1192       sl->name = extra_specs_1[i].name;
1193       sl->ptr = extra_specs_1[i].ptr;
1194       sl->next = next;
1195       sl->name_len = strlen (sl->name);
1196       sl->ptr_spec = &sl->ptr;
1197       next = sl;
1198     }
1199 #endif
1200
1201   for (i = (sizeof (static_specs) / sizeof (static_specs[0])) - 1; i >= 0; i--)
1202     {
1203       sl = &static_specs[i];
1204       sl->next = next;
1205       next = sl;
1206     }
1207
1208   specs = sl;
1209 }
1210
1211 \f
1212 /* Change the value of spec NAME to SPEC.  If SPEC is empty, then the spec is
1213    removed; If the spec starts with a + then SPEC is added to the end of the
1214    current spec.  */
1215
1216 static void
1217 set_spec (name, spec)
1218      const char *name;
1219      const char *spec;
1220 {
1221   struct spec_list *sl;
1222   char *old_spec;
1223   int name_len = strlen (name);
1224   int i;
1225
1226   /* If this is the first call, initialize the statically allocated specs */
1227   if (!specs)
1228     {
1229       struct spec_list *next = (struct spec_list *)0;
1230       for (i = (sizeof (static_specs) / sizeof (static_specs[0])) - 1;
1231            i >= 0; i--)
1232         {
1233           sl = &static_specs[i];
1234           sl->next = next;
1235           next = sl;
1236         }
1237       specs = sl;
1238     }
1239
1240   /* See if the spec already exists */
1241   for (sl = specs; sl; sl = sl->next)
1242     if (name_len == sl->name_len && !strcmp (sl->name, name))
1243       break;
1244
1245   if (!sl)
1246     {
1247       /* Not found - make it */
1248       sl = (struct spec_list *) xmalloc (sizeof (struct spec_list));
1249       sl->name = save_string (name, strlen (name));
1250       sl->name_len = name_len;
1251       sl->ptr_spec = &sl->ptr;
1252       sl->alloc_p = 0;
1253       *(sl->ptr_spec) = "";
1254       sl->next = specs;
1255       specs = sl;
1256     }
1257
1258   old_spec = *(sl->ptr_spec);
1259   *(sl->ptr_spec) = ((spec[0] == '+' && ISSPACE ((unsigned char)spec[1]))
1260                      ? concat (old_spec, spec + 1, NULL_PTR)
1261                      : save_string (spec, strlen (spec)));
1262
1263 #ifdef DEBUG_SPECS
1264   if (verbose_flag)
1265     notice ("Setting spec %s to '%s'\n\n", name, *(sl->ptr_spec));
1266 #endif
1267
1268   /* Free the old spec */
1269   if (old_spec && sl->alloc_p)
1270     free (old_spec);
1271
1272   sl->alloc_p = 1;
1273 }
1274 \f
1275 /* Accumulate a command (program name and args), and run it.  */
1276
1277 /* Vector of pointers to arguments in the current line of specifications.  */
1278
1279 static char **argbuf;
1280
1281 /* Number of elements allocated in argbuf.  */
1282
1283 static int argbuf_length;
1284
1285 /* Number of elements in argbuf currently in use (containing args).  */
1286
1287 static int argbuf_index;
1288
1289 /* We want this on by default all the time now.  */
1290 #define MKTEMP_EACH_FILE
1291
1292 #ifdef MKTEMP_EACH_FILE
1293
1294 extern char *make_temp_file PROTO((const char *));
1295
1296 /* This is the list of suffixes and codes (%g/%u/%U) and the associated
1297    temp file.  */
1298
1299 static struct temp_name {
1300   const char *suffix;   /* suffix associated with the code.  */
1301   int length;           /* strlen (suffix).  */
1302   int unique;           /* Indicates whether %g or %u/%U was used.  */
1303   const char *filename; /* associated filename.  */
1304   int filename_length;  /* strlen (filename).  */
1305   struct temp_name *next;
1306 } *temp_names;
1307 #endif
1308
1309
1310 /* Number of commands executed so far.  */
1311
1312 static int execution_count;
1313
1314 /* Number of commands that exited with a signal.  */
1315
1316 static int signal_count;
1317
1318 /* Name with which this program was invoked.  */
1319
1320 static const char *programname;
1321 \f
1322 /* Structures to keep track of prefixes to try when looking for files.  */
1323
1324 struct prefix_list
1325 {
1326   char *prefix;               /* String to prepend to the path.  */
1327   struct prefix_list *next;   /* Next in linked list.  */
1328   int require_machine_suffix; /* Don't use without machine_suffix.  */
1329   /* 2 means try both machine_suffix and just_machine_suffix.  */
1330   int *used_flag_ptr;         /* 1 if a file was found with this prefix.  */
1331 };
1332
1333 struct path_prefix
1334 {
1335   struct prefix_list *plist;  /* List of prefixes to try */
1336   int max_len;                /* Max length of a prefix in PLIST */
1337   const char *name;           /* Name of this list (used in config stuff) */
1338 };
1339
1340 /* List of prefixes to try when looking for executables.  */
1341
1342 static struct path_prefix exec_prefixes = { 0, 0, "exec" };
1343
1344 /* List of prefixes to try when looking for startup (crt0) files.  */
1345
1346 static struct path_prefix startfile_prefixes = { 0, 0, "startfile" };
1347
1348 /* List of prefixes to try when looking for include files.  */
1349
1350 static struct path_prefix include_prefixes = { 0, 0, "include" };
1351
1352 /* Suffix to attach to directories searched for commands.
1353    This looks like `MACHINE/VERSION/'.  */
1354
1355 static const char *machine_suffix = 0;
1356
1357 /* Suffix to attach to directories searched for commands.
1358    This is just `MACHINE/'.  */
1359
1360 static const char *just_machine_suffix = 0;
1361
1362 /* Adjusted value of GCC_EXEC_PREFIX envvar.  */
1363
1364 static const char *gcc_exec_prefix;
1365
1366 /* Default prefixes to attach to command names.  */
1367
1368 #ifdef CROSS_COMPILE  /* Don't use these prefixes for a cross compiler.  */
1369 #undef MD_EXEC_PREFIX
1370 #undef MD_STARTFILE_PREFIX
1371 #undef MD_STARTFILE_PREFIX_1
1372 #endif
1373
1374 #ifndef STANDARD_EXEC_PREFIX
1375 #define STANDARD_EXEC_PREFIX "/usr/local/lib/gcc-lib/"
1376 #endif /* !defined STANDARD_EXEC_PREFIX */
1377
1378 static const char *standard_exec_prefix = STANDARD_EXEC_PREFIX;
1379 static const char *standard_exec_prefix_1 = "/usr/lib/gcc/";
1380 #ifdef MD_EXEC_PREFIX
1381 static const char *md_exec_prefix = MD_EXEC_PREFIX;
1382 #endif
1383
1384 #ifndef STANDARD_STARTFILE_PREFIX
1385 #define STANDARD_STARTFILE_PREFIX "/usr/local/lib/"
1386 #endif /* !defined STANDARD_STARTFILE_PREFIX */
1387
1388 #ifdef MD_STARTFILE_PREFIX
1389 static const char *md_startfile_prefix = MD_STARTFILE_PREFIX;
1390 #endif
1391 #ifdef MD_STARTFILE_PREFIX_1
1392 static const char *md_startfile_prefix_1 = MD_STARTFILE_PREFIX_1;
1393 #endif
1394 static const char *standard_startfile_prefix = STANDARD_STARTFILE_PREFIX;
1395 static const char *standard_startfile_prefix_1 = "/lib/";
1396 static const char *standard_startfile_prefix_2 = "/usr/lib/";
1397
1398 #ifndef TOOLDIR_BASE_PREFIX
1399 #define TOOLDIR_BASE_PREFIX "/usr/local/"
1400 #endif
1401 static const char *tooldir_base_prefix = TOOLDIR_BASE_PREFIX;
1402 static const char *tooldir_prefix;
1403
1404 /* Subdirectory to use for locating libraries.  Set by
1405    set_multilib_dir based on the compilation options.  */
1406
1407 static const char *multilib_dir;
1408
1409 /* Clear out the vector of arguments (after a command is executed).  */
1410
1411 static void
1412 clear_args ()
1413 {
1414   argbuf_index = 0;
1415 }
1416
1417 /* Add one argument to the vector at the end.
1418    This is done when a space is seen or at the end of the line.
1419    If DELETE_ALWAYS is nonzero, the arg is a filename
1420     and the file should be deleted eventually.
1421    If DELETE_FAILURE is nonzero, the arg is a filename
1422     and the file should be deleted if this compilation fails.  */
1423
1424 static void
1425 store_arg (arg, delete_always, delete_failure)
1426      char *arg;
1427      int delete_always, delete_failure;
1428 {
1429   if (argbuf_index + 1 == argbuf_length)
1430     argbuf
1431       = (char **) xrealloc (argbuf, (argbuf_length *= 2) * sizeof (char *));
1432
1433   argbuf[argbuf_index++] = arg;
1434   argbuf[argbuf_index] = 0;
1435
1436   if (delete_always || delete_failure)
1437     record_temp_file (arg, delete_always, delete_failure);
1438 }
1439 \f
1440 /* Read compilation specs from a file named FILENAME,
1441    replacing the default ones.
1442
1443    A suffix which starts with `*' is a definition for
1444    one of the machine-specific sub-specs.  The "suffix" should be
1445    *asm, *cc1, *cpp, *link, *startfile, *signed_char, etc.
1446    The corresponding spec is stored in asm_spec, etc.,
1447    rather than in the `compilers' vector.
1448
1449    Anything invalid in the file is a fatal error.  */
1450
1451 static void
1452 read_specs (filename, main_p)
1453      const char *filename;
1454      int main_p;
1455 {
1456   int desc;
1457   int readlen;
1458   struct stat statbuf;
1459   char *buffer;
1460   register char *p;
1461
1462   if (verbose_flag)
1463     notice ("Reading specs from %s\n", filename);
1464
1465   /* Open and stat the file.  */
1466   desc = open (filename, O_RDONLY, 0);
1467   if (desc < 0)
1468     pfatal_with_name (filename);
1469   if (stat (filename, &statbuf) < 0)
1470     pfatal_with_name (filename);
1471
1472   /* Read contents of file into BUFFER.  */
1473   buffer = xmalloc ((unsigned) statbuf.st_size + 1);
1474   readlen = read (desc, buffer, (unsigned) statbuf.st_size);
1475   if (readlen < 0)
1476     pfatal_with_name (filename);
1477   buffer[readlen] = 0;
1478   close (desc);
1479
1480   /* Scan BUFFER for specs, putting them in the vector.  */
1481   p = buffer;
1482   while (1)
1483     {
1484       char *suffix;
1485       char *spec;
1486       char *in, *out, *p1, *p2, *p3;
1487
1488       /* Advance P in BUFFER to the next nonblank nocomment line.  */
1489       p = skip_whitespace (p);
1490       if (*p == 0)
1491         break;
1492
1493       /* Is this a special command that starts with '%'? */
1494       /* Don't allow this for the main specs file, since it would
1495          encourage people to overwrite it.  */
1496       if (*p == '%' && !main_p)
1497         {
1498           p1 = p;
1499           while (*p && *p != '\n')
1500             p++;
1501
1502           p++;                  /* Skip '\n' */
1503
1504           if (!strncmp (p1, "%include", sizeof ("%include")-1)
1505               && (p1[sizeof "%include" - 1] == ' '
1506                   || p1[sizeof "%include" - 1] == '\t'))
1507             {
1508               char *new_filename;
1509
1510               p1 += sizeof ("%include");
1511               while (*p1 == ' ' || *p1 == '\t')
1512                 p1++;
1513
1514               if (*p1++ != '<' || p[-2] != '>')
1515                 fatal ("specs %%include syntax malformed after %ld characters",
1516                        (long) (p1 - buffer + 1));
1517
1518               p[-2] = '\0';
1519               new_filename = find_a_file (&startfile_prefixes, p1, R_OK);
1520               read_specs (new_filename ? new_filename : p1, FALSE);
1521               continue;
1522             }
1523           else if (!strncmp (p1, "%include_noerr", sizeof "%include_noerr" - 1)
1524                    && (p1[sizeof "%include_noerr" - 1] == ' '
1525                        || p1[sizeof "%include_noerr" - 1] == '\t'))
1526             {
1527               char *new_filename;
1528
1529               p1 += sizeof "%include_noerr";
1530               while (*p1 == ' ' || *p1 == '\t') p1++;
1531
1532               if (*p1++ != '<' || p[-2] != '>')
1533                 fatal ("specs %%include syntax malformed after %ld characters",
1534                        (long) (p1 - buffer + 1));
1535
1536               p[-2] = '\0';
1537               new_filename = find_a_file (&startfile_prefixes, p1, R_OK);
1538               if (new_filename)
1539                 read_specs (new_filename, FALSE);
1540               else if (verbose_flag)
1541                 notice ("Could not find specs file %s\n", p1);
1542               continue;
1543             }
1544           else if (!strncmp (p1, "%rename", sizeof "%rename" - 1)
1545                    && (p1[sizeof "%rename" - 1] == ' '
1546                        || p1[sizeof "%rename" - 1] == '\t'))
1547             {
1548               int name_len;
1549               struct spec_list *sl;
1550
1551               /* Get original name */
1552               p1 += sizeof "%rename";
1553               while (*p1 == ' ' || *p1 == '\t')
1554                 p1++;
1555
1556               if (! ISALPHA ((unsigned char)*p1))
1557                 fatal ("specs %%rename syntax malformed after %ld characters",
1558                        (long) (p1 - buffer));
1559
1560               p2 = p1;
1561               while (*p2 && !ISSPACE ((unsigned char)*p2))
1562                 p2++;
1563
1564               if (*p2 != ' ' && *p2 != '\t')
1565                 fatal ("specs %%rename syntax malformed after %ld characters",
1566                        (long) (p2 - buffer));
1567
1568               name_len = p2 - p1;
1569               *p2++ = '\0';
1570               while (*p2 == ' ' || *p2 == '\t')
1571                 p2++;
1572
1573               if (! ISALPHA ((unsigned char)*p2))
1574                 fatal ("specs %%rename syntax malformed after %ld characters",
1575                        (long) (p2 - buffer));
1576
1577               /* Get new spec name */
1578               p3 = p2;
1579               while (*p3 && !ISSPACE ((unsigned char)*p3))
1580                 p3++;
1581
1582               if (p3 != p-1)
1583                 fatal ("specs %%rename syntax malformed after %ld characters",
1584                        (long) (p3 - buffer));
1585               *p3 = '\0';
1586
1587               for (sl = specs; sl; sl = sl->next)
1588                 if (name_len == sl->name_len && !strcmp (sl->name, p1))
1589                   break;
1590
1591               if (!sl)
1592                 fatal ("specs %s spec was not found to be renamed", p1);
1593
1594               if (strcmp (p1, p2) == 0)
1595                 continue;
1596
1597               if (verbose_flag)
1598                 {
1599                   notice ("rename spec %s to %s\n", p1, p2);
1600 #ifdef DEBUG_SPECS
1601                   notice ("spec is '%s'\n\n", *(sl->ptr_spec));
1602 #endif
1603                 }
1604
1605               set_spec (p2, *(sl->ptr_spec));
1606               if (sl->alloc_p)
1607                 free (*(sl->ptr_spec));
1608
1609               *(sl->ptr_spec) = "";
1610               sl->alloc_p = 0;
1611               continue;
1612             }
1613           else
1614             fatal ("specs unknown %% command after %ld characters",
1615                    (long) (p1 - buffer));
1616         }
1617
1618       /* Find the colon that should end the suffix.  */
1619       p1 = p;
1620       while (*p1 && *p1 != ':' && *p1 != '\n')
1621         p1++;
1622
1623       /* The colon shouldn't be missing.  */
1624       if (*p1 != ':')
1625         fatal ("specs file malformed after %ld characters",
1626                (long) (p1 - buffer));
1627
1628       /* Skip back over trailing whitespace.  */
1629       p2 = p1;
1630       while (p2 > buffer && (p2[-1] == ' ' || p2[-1] == '\t'))
1631         p2--;
1632
1633       /* Copy the suffix to a string.  */
1634       suffix = save_string (p, p2 - p);
1635       /* Find the next line.  */
1636       p = skip_whitespace (p1 + 1);
1637       if (p[1] == 0)
1638         fatal ("specs file malformed after %ld characters",
1639                (long) (p - buffer));
1640
1641       p1 = p;
1642       /* Find next blank line or end of string.  */
1643       while (*p1 && !(*p1 == '\n' && (p1[1] == '\n' || p1[1] == '\0')))
1644         p1++;
1645
1646       /* Specs end at the blank line and do not include the newline.  */
1647       spec = save_string (p, p1 - p);
1648       p = p1;
1649
1650       /* Delete backslash-newline sequences from the spec.  */
1651       in = spec;
1652       out = spec;
1653       while (*in != 0)
1654         {
1655           if (in[0] == '\\' && in[1] == '\n')
1656             in += 2;
1657           else if (in[0] == '#')
1658             while (*in && *in != '\n')
1659               in++;
1660
1661           else
1662             *out++ = *in++;
1663         }
1664       *out = 0;
1665
1666       if (suffix[0] == '*')
1667         {
1668           if (! strcmp (suffix, "*link_command"))
1669             link_command_spec = spec;
1670           else
1671             set_spec (suffix + 1, spec);
1672         }
1673       else
1674         {
1675           /* Add this pair to the vector.  */
1676           compilers
1677             = ((struct compiler *)
1678                xrealloc (compilers,
1679                          (n_compilers + 2) * sizeof (struct compiler)));
1680
1681           compilers[n_compilers].suffix = suffix;
1682           bzero ((char *) compilers[n_compilers].spec,
1683                  sizeof compilers[n_compilers].spec);
1684           compilers[n_compilers].spec[0] = spec;
1685           n_compilers++;
1686           bzero ((char *) &compilers[n_compilers],
1687                  sizeof compilers[n_compilers]);
1688         }
1689
1690       if (*suffix == 0)
1691         link_command_spec = spec;
1692     }
1693
1694   if (link_command_spec == 0)
1695     fatal ("spec file has no spec for linking");
1696 }
1697 \f
1698 /* Record the names of temporary files we tell compilers to write,
1699    and delete them at the end of the run.  */
1700
1701 /* This is the common prefix we use to make temp file names.
1702    It is chosen once for each run of this program.
1703    It is substituted into a spec by %g.
1704    Thus, all temp file names contain this prefix.
1705    In practice, all temp file names start with this prefix.
1706
1707    This prefix comes from the envvar TMPDIR if it is defined;
1708    otherwise, from the P_tmpdir macro if that is defined;
1709    otherwise, in /usr/tmp or /tmp;
1710    or finally the current directory if all else fails.  */
1711
1712 static const char *temp_filename;
1713
1714 /* Length of the prefix.  */
1715
1716 static int temp_filename_length;
1717
1718 /* Define the list of temporary files to delete.  */
1719
1720 struct temp_file
1721 {
1722   const char *name;
1723   struct temp_file *next;
1724 };
1725
1726 /* Queue of files to delete on success or failure of compilation.  */
1727 static struct temp_file *always_delete_queue;
1728 /* Queue of files to delete on failure of compilation.  */
1729 static struct temp_file *failure_delete_queue;
1730
1731 /* Record FILENAME as a file to be deleted automatically.
1732    ALWAYS_DELETE nonzero means delete it if all compilation succeeds;
1733    otherwise delete it in any case.
1734    FAIL_DELETE nonzero means delete it if a compilation step fails;
1735    otherwise delete it in any case.  */
1736
1737 static void
1738 record_temp_file (filename, always_delete, fail_delete)
1739      const char *filename;
1740      int always_delete;
1741      int fail_delete;
1742 {
1743   register char *name;
1744   name = xmalloc (strlen (filename) + 1);
1745   strcpy (name, filename);
1746
1747   if (always_delete)
1748     {
1749       register struct temp_file *temp;
1750       for (temp = always_delete_queue; temp; temp = temp->next)
1751         if (! strcmp (name, temp->name))
1752           goto already1;
1753
1754       temp = (struct temp_file *) xmalloc (sizeof (struct temp_file));
1755       temp->next = always_delete_queue;
1756       temp->name = name;
1757       always_delete_queue = temp;
1758
1759     already1:;
1760     }
1761
1762   if (fail_delete)
1763     {
1764       register struct temp_file *temp;
1765       for (temp = failure_delete_queue; temp; temp = temp->next)
1766         if (! strcmp (name, temp->name))
1767           goto already2;
1768
1769       temp = (struct temp_file *) xmalloc (sizeof (struct temp_file));
1770       temp->next = failure_delete_queue;
1771       temp->name = name;
1772       failure_delete_queue = temp;
1773
1774     already2:;
1775     }
1776 }
1777
1778 /* Delete all the temporary files whose names we previously recorded.  */
1779
1780 static void
1781 delete_if_ordinary (name)
1782      const char *name;
1783 {
1784   struct stat st;
1785 #ifdef DEBUG
1786   int i, c;
1787
1788   printf ("Delete %s? (y or n) ", name);
1789   fflush (stdout);
1790   i = getchar ();
1791   if (i != '\n')
1792     while ((c = getchar ()) != '\n' && c != EOF)
1793       ;
1794
1795   if (i == 'y' || i == 'Y')
1796 #endif /* DEBUG */
1797     if (stat (name, &st) >= 0 && S_ISREG (st.st_mode))
1798       if (unlink (name) < 0)
1799         if (verbose_flag)
1800           perror_with_name (name);
1801 }
1802
1803 static void
1804 delete_temp_files ()
1805 {
1806   register struct temp_file *temp;
1807
1808   for (temp = always_delete_queue; temp; temp = temp->next)
1809     delete_if_ordinary (temp->name);
1810   always_delete_queue = 0;
1811 }
1812
1813 /* Delete all the files to be deleted on error.  */
1814
1815 static void
1816 delete_failure_queue ()
1817 {
1818   register struct temp_file *temp;
1819
1820   for (temp = failure_delete_queue; temp; temp = temp->next)
1821     delete_if_ordinary (temp->name);
1822 }
1823
1824 static void
1825 clear_failure_queue ()
1826 {
1827   failure_delete_queue = 0;
1828 }
1829 \f
1830 /* Routine to add variables to the environment.  We do this to pass
1831    the pathname of the gcc driver, and the directories search to the
1832    collect2 program, which is being run as ld.  This way, we can be
1833    sure of executing the right compiler when collect2 wants to build
1834    constructors and destructors.  Since the environment variables we
1835    use come from an obstack, we don't have to worry about allocating
1836    space for them.  */
1837
1838 #ifndef HAVE_PUTENV
1839
1840 void
1841 putenv (str)
1842      char *str;
1843 {
1844 #ifndef VMS                     /* nor about VMS */
1845
1846   extern char **environ;
1847   char **old_environ = environ;
1848   char **envp;
1849   int num_envs = 0;
1850   int name_len = 1;
1851   int str_len = strlen (str);
1852   char *p = str;
1853   int ch;
1854
1855   while ((ch = *p++) != '\0' && ch != '=')
1856     name_len++;
1857
1858   if (!ch)
1859     abort ();
1860
1861   /* Search for replacing an existing environment variable, and
1862      count the number of total environment variables.  */
1863   for (envp = old_environ; *envp; envp++)
1864     {
1865       num_envs++;
1866       if (!strncmp (str, *envp, name_len))
1867         {
1868           *envp = str;
1869           return;
1870         }
1871     }
1872
1873   /* Add a new environment variable */
1874   environ = (char **) xmalloc (sizeof (char *) * (num_envs+2));
1875   *environ = str;
1876   memcpy ((char *) (environ + 1), (char *) old_environ,
1877          sizeof (char *) * (num_envs+1));
1878
1879 #endif  /* VMS */
1880 }
1881
1882 #endif  /* HAVE_PUTENV */
1883
1884 \f
1885 /* Build a list of search directories from PATHS.
1886    PREFIX is a string to prepend to the list.
1887    If CHECK_DIR_P is non-zero we ensure the directory exists.
1888    This is used mostly by putenv_from_prefixes so we use `collect_obstack'.
1889    It is also used by the --print-search-dirs flag.  */
1890
1891 static char *
1892 build_search_list (paths, prefix, check_dir_p)
1893      struct path_prefix *paths;
1894      const char *prefix;
1895      int check_dir_p;
1896 {
1897   int suffix_len = (machine_suffix) ? strlen (machine_suffix) : 0;
1898   int just_suffix_len
1899     = (just_machine_suffix) ? strlen (just_machine_suffix) : 0;
1900   int first_time = TRUE;
1901   struct prefix_list *pprefix;
1902
1903   obstack_grow (&collect_obstack, prefix, strlen (prefix));
1904
1905   for (pprefix = paths->plist; pprefix != 0; pprefix = pprefix->next)
1906     {
1907       int len = strlen (pprefix->prefix);
1908
1909       if (machine_suffix
1910           && (! check_dir_p
1911               || is_directory (pprefix->prefix, machine_suffix, 0)))
1912         {
1913           if (!first_time)
1914             obstack_1grow (&collect_obstack, PATH_SEPARATOR);
1915             
1916           first_time = FALSE;
1917           obstack_grow (&collect_obstack, pprefix->prefix, len);
1918           obstack_grow (&collect_obstack, machine_suffix, suffix_len);
1919         }
1920
1921       if (just_machine_suffix
1922           && pprefix->require_machine_suffix == 2
1923           && (! check_dir_p
1924               || is_directory (pprefix->prefix, just_machine_suffix, 0)))
1925         {
1926           if (! first_time)
1927             obstack_1grow (&collect_obstack, PATH_SEPARATOR);
1928             
1929           first_time = FALSE;
1930           obstack_grow (&collect_obstack, pprefix->prefix, len);
1931           obstack_grow (&collect_obstack, just_machine_suffix,
1932                         just_suffix_len);
1933         }
1934
1935       if (! pprefix->require_machine_suffix)
1936         {
1937           if (! first_time)
1938             obstack_1grow (&collect_obstack, PATH_SEPARATOR);
1939
1940           first_time = FALSE;
1941           obstack_grow (&collect_obstack, pprefix->prefix, len);
1942         }
1943     }
1944
1945   obstack_1grow (&collect_obstack, '\0');
1946   return obstack_finish (&collect_obstack);
1947 }
1948
1949 /* Rebuild the COMPILER_PATH and LIBRARY_PATH environment variables
1950    for collect.  */
1951
1952 static void
1953 putenv_from_prefixes (paths, env_var)
1954      struct path_prefix *paths;
1955      const char *env_var;
1956 {
1957   putenv (build_search_list (paths, env_var, 1));
1958 }
1959 \f
1960 /* Search for NAME using the prefix list PREFIXES.  MODE is passed to
1961    access to check permissions.
1962    Return 0 if not found, otherwise return its name, allocated with malloc.  */
1963
1964 static char *
1965 find_a_file (pprefix, name, mode)
1966      struct path_prefix *pprefix;
1967      const char *name;
1968      int mode;
1969 {
1970   char *temp;
1971   const char *file_suffix = ((mode & X_OK) != 0 ? EXECUTABLE_SUFFIX : "");
1972   struct prefix_list *pl;
1973   int len = pprefix->max_len + strlen (name) + strlen (file_suffix) + 1;
1974
1975 #ifdef DEFAULT_ASSEMBLER
1976   if (! strcmp(name, "as") && access (DEFAULT_ASSEMBLER, mode) == 0) {
1977     name = DEFAULT_ASSEMBLER;
1978     len = strlen(name)+1;
1979     temp = xmalloc (len);
1980     strcpy (temp, name);
1981     return temp;
1982   }
1983 #endif
1984
1985 #ifdef DEFAULT_LINKER
1986   if (! strcmp(name, "ld") && access (DEFAULT_LINKER, mode) == 0) {
1987     name = DEFAULT_LINKER;
1988     len = strlen(name)+1;
1989     temp = xmalloc (len);
1990     strcpy (temp, name);
1991     return temp;
1992   }
1993 #endif
1994
1995   if (machine_suffix)
1996     len += strlen (machine_suffix);
1997
1998   temp = xmalloc (len);
1999
2000   /* Determine the filename to execute (special case for absolute paths).  */
2001
2002   if (IS_DIR_SEPARATOR (*name)
2003 #ifdef HAVE_DOS_BASED_FILESYSTEM
2004       /* Check for disk name on MS-DOS-based systems.  */
2005       || (name[0] && name[1] == ':' && IS_DIR_SEPARATOR (name[2]))
2006 #endif
2007       )
2008     {
2009       if (access (name, mode) == 0)
2010         {
2011           strcpy (temp, name);
2012           return temp;
2013         }
2014     }
2015   else
2016     for (pl = pprefix->plist; pl; pl = pl->next)
2017       {
2018         if (machine_suffix)
2019           {
2020             /* Some systems have a suffix for executable files.
2021                So try appending that first.  */
2022             if (file_suffix[0] != 0)
2023               {
2024                 strcpy (temp, pl->prefix);
2025                 strcat (temp, machine_suffix);
2026                 strcat (temp, name);
2027                 strcat (temp, file_suffix);
2028                 if (access (temp, mode) == 0)
2029                   {
2030                     if (pl->used_flag_ptr != 0)
2031                       *pl->used_flag_ptr = 1;
2032                     return temp;
2033                   }
2034               }
2035
2036             /* Now try just the name.  */
2037             strcpy (temp, pl->prefix);
2038             strcat (temp, machine_suffix);
2039             strcat (temp, name);
2040             if (access (temp, mode) == 0)
2041               {
2042                 if (pl->used_flag_ptr != 0)
2043                   *pl->used_flag_ptr = 1;
2044                 return temp;
2045               }
2046           }
2047
2048         /* Certain prefixes are tried with just the machine type,
2049            not the version.  This is used for finding as, ld, etc.  */
2050         if (just_machine_suffix && pl->require_machine_suffix == 2)
2051           {
2052             /* Some systems have a suffix for executable files.
2053                So try appending that first.  */
2054             if (file_suffix[0] != 0)
2055               {
2056                 strcpy (temp, pl->prefix);
2057                 strcat (temp, just_machine_suffix);
2058                 strcat (temp, name);
2059                 strcat (temp, file_suffix);
2060                 if (access (temp, mode) == 0)
2061                   {
2062                     if (pl->used_flag_ptr != 0)
2063                       *pl->used_flag_ptr = 1;
2064                     return temp;
2065                   }
2066               }
2067
2068             strcpy (temp, pl->prefix);
2069             strcat (temp, just_machine_suffix);
2070             strcat (temp, name);
2071             if (access (temp, mode) == 0)
2072               {
2073                 if (pl->used_flag_ptr != 0)
2074                   *pl->used_flag_ptr = 1;
2075                 return temp;
2076               }
2077           }
2078
2079         /* Certain prefixes can't be used without the machine suffix
2080            when the machine or version is explicitly specified.  */
2081         if (! pl->require_machine_suffix)
2082           {
2083             /* Some systems have a suffix for executable files.
2084                So try appending that first.  */
2085             if (file_suffix[0] != 0)
2086               {
2087                 strcpy (temp, pl->prefix);
2088                 strcat (temp, name);
2089                 strcat (temp, file_suffix);
2090                 if (access (temp, mode) == 0)
2091                   {
2092                     if (pl->used_flag_ptr != 0)
2093                       *pl->used_flag_ptr = 1;
2094                     return temp;
2095                   }
2096               }
2097
2098             strcpy (temp, pl->prefix);
2099             strcat (temp, name);
2100             if (access (temp, mode) == 0)
2101               {
2102                 if (pl->used_flag_ptr != 0)
2103                   *pl->used_flag_ptr = 1;
2104                 return temp;
2105               }
2106           }
2107       }
2108
2109   free (temp);
2110   return 0;
2111 }
2112
2113 /* Add an entry for PREFIX in PLIST.  If FIRST is set, it goes
2114    at the start of the list, otherwise it goes at the end.
2115
2116    If WARN is nonzero, we will warn if no file is found
2117    through this prefix.  WARN should point to an int
2118    which will be set to 1 if this entry is used.
2119
2120    COMPONENT is the value to be passed to update_path.
2121
2122    REQUIRE_MACHINE_SUFFIX is 1 if this prefix can't be used without
2123    the complete value of machine_suffix.
2124    2 means try both machine_suffix and just_machine_suffix.  */
2125
2126 static void
2127 add_prefix (pprefix, prefix, component, first, require_machine_suffix, warn)
2128      struct path_prefix *pprefix;
2129      const char *prefix;
2130      const char *component;
2131      int first;
2132      int require_machine_suffix;
2133      int *warn;
2134 {
2135   struct prefix_list *pl, **prev;
2136   int len;
2137
2138   if (! first && pprefix->plist)
2139     {
2140       for (pl = pprefix->plist; pl->next; pl = pl->next)
2141         ;
2142       prev = &pl->next;
2143     }
2144   else
2145     prev = &pprefix->plist;
2146
2147   /* Keep track of the longest prefix */
2148
2149   prefix = update_path (prefix, component);
2150   len = strlen (prefix);
2151   if (len > pprefix->max_len)
2152     pprefix->max_len = len;
2153
2154   pl = (struct prefix_list *) xmalloc (sizeof (struct prefix_list));
2155   pl->prefix = save_string (prefix, len);
2156   pl->require_machine_suffix = require_machine_suffix;
2157   pl->used_flag_ptr = warn;
2158   if (warn)
2159     *warn = 0;
2160
2161   if (*prev)
2162     pl->next = *prev;
2163   else
2164     pl->next = (struct prefix_list *) 0;
2165   *prev = pl;
2166 }
2167
2168 /* Print warnings for any prefixes in the list PPREFIX that were not used.  */
2169
2170 static void
2171 unused_prefix_warnings (pprefix)
2172      struct path_prefix *pprefix;
2173 {
2174   struct prefix_list *pl = pprefix->plist;
2175
2176   while (pl)
2177     {
2178       if (pl->used_flag_ptr != 0 && !*pl->used_flag_ptr)
2179         {
2180           if (pl->require_machine_suffix && machine_suffix)
2181             error ("file path prefix `%s%s' never used", pl->prefix,
2182                    machine_suffix);
2183           else
2184             error ("file path prefix `%s' never used", pl->prefix);
2185
2186           /* Prevent duplicate warnings.  */
2187           *pl->used_flag_ptr = 1;
2188         }
2189
2190       pl = pl->next;
2191     }
2192 }
2193
2194 \f
2195 /* Execute the command specified by the arguments on the current line of spec.
2196    When using pipes, this includes several piped-together commands
2197    with `|' between them.
2198
2199    Return 0 if successful, -1 if failed.  */
2200
2201 static int
2202 execute ()
2203 {
2204   int i;
2205   int n_commands;               /* # of command.  */
2206   char *string;
2207   struct command
2208     {
2209       const char *prog;         /* program name.  */
2210       char **argv;      /* vector of args.  */
2211       int pid;                  /* pid of process for this command.  */
2212     };
2213
2214   struct command *commands;     /* each command buffer with above info.  */
2215
2216   /* Count # of piped commands.  */
2217   for (n_commands = 1, i = 0; i < argbuf_index; i++)
2218     if (strcmp (argbuf[i], "|") == 0)
2219       n_commands++;
2220
2221   /* Get storage for each command.  */
2222   commands
2223     = (struct command *) alloca (n_commands * sizeof (struct command));
2224
2225   /* Split argbuf into its separate piped processes,
2226      and record info about each one.
2227      Also search for the programs that are to be run.  */
2228
2229   commands[0].prog = argbuf[0]; /* first command.  */
2230   commands[0].argv = &argbuf[0];
2231   string = find_a_file (&exec_prefixes, commands[0].prog, X_OK);
2232
2233   if (string)
2234     commands[0].argv[0] = string;
2235
2236   for (n_commands = 1, i = 0; i < argbuf_index; i++)
2237     if (strcmp (argbuf[i], "|") == 0)
2238       {                         /* each command.  */
2239 #if defined (__MSDOS__) || defined (OS2) || defined (VMS)
2240         fatal ("-pipe not supported");
2241 #endif
2242         argbuf[i] = 0;  /* termination of command args.  */
2243         commands[n_commands].prog = argbuf[i + 1];
2244         commands[n_commands].argv = &argbuf[i + 1];
2245         string = find_a_file (&exec_prefixes, commands[n_commands].prog, X_OK);
2246         if (string)
2247           commands[n_commands].argv[0] = string;
2248         n_commands++;
2249       }
2250
2251   argbuf[argbuf_index] = 0;
2252
2253   /* If -v, print what we are about to do, and maybe query.  */
2254
2255   if (verbose_flag)
2256     {
2257       /* For help listings, put a blank line between sub-processes.  */
2258       if (print_help_list)
2259         fputc ('\n', stderr);
2260       
2261       /* Print each piped command as a separate line.  */
2262       for (i = 0; i < n_commands ; i++)
2263         {
2264           char **j;
2265
2266           for (j = commands[i].argv; *j; j++)
2267             fprintf (stderr, " %s", *j);
2268
2269           /* Print a pipe symbol after all but the last command.  */
2270           if (i + 1 != n_commands)
2271             fprintf (stderr, " |");
2272           fprintf (stderr, "\n");
2273         }
2274       fflush (stderr);
2275 #ifdef DEBUG
2276       notice ("\nGo ahead? (y or n) ");
2277       fflush (stderr);
2278       i = getchar ();
2279       if (i != '\n')
2280         while (getchar () != '\n')
2281           ;
2282
2283       if (i != 'y' && i != 'Y')
2284         return 0;
2285 #endif /* DEBUG */
2286     }
2287
2288   /* Run each piped subprocess.  */
2289
2290   for (i = 0; i < n_commands; i++)
2291     {
2292       char *errmsg_fmt, *errmsg_arg;
2293       char *string = commands[i].argv[0];
2294
2295       commands[i].pid = pexecute (string, commands[i].argv,
2296                                   programname, temp_filename,
2297                                   &errmsg_fmt, &errmsg_arg,
2298                                   ((i == 0 ? PEXECUTE_FIRST : 0)
2299                                    | (i + 1 == n_commands ? PEXECUTE_LAST : 0)
2300                                    | (string == commands[i].prog
2301                                       ? PEXECUTE_SEARCH : 0)
2302                                    | (verbose_flag ? PEXECUTE_VERBOSE : 0)));
2303
2304       if (commands[i].pid == -1)
2305         pfatal_pexecute (errmsg_fmt, errmsg_arg);
2306
2307       if (string != commands[i].prog)
2308         free (string);
2309     }
2310
2311   execution_count++;
2312
2313   /* Wait for all the subprocesses to finish.
2314      We don't care what order they finish in;
2315      we know that N_COMMANDS waits will get them all.
2316      Ignore subprocesses that we don't know about,
2317      since they can be spawned by the process that exec'ed us.  */
2318
2319   {
2320     int ret_code = 0;
2321
2322     for (i = 0; i < n_commands; )
2323       {
2324         int j;
2325         int status;
2326         int pid;
2327
2328         pid = pwait (commands[i].pid, &status, 0);
2329         if (pid < 0)
2330           abort ();
2331
2332         for (j = 0; j < n_commands; j++)
2333           if (commands[j].pid == pid)
2334             {
2335               i++;
2336               if (status != 0)
2337                 {
2338                   if (WIFSIGNALED (status))
2339                     {
2340                       fatal ("Internal compiler error: program %s got fatal signal %d",
2341                              commands[j].prog, WTERMSIG (status));
2342                       signal_count++;
2343                       ret_code = -1;
2344                     }
2345                   else if (WIFEXITED (status)
2346                            && WEXITSTATUS (status) >= MIN_FATAL_STATUS)
2347                     ret_code = -1;
2348                 }
2349               break;
2350             }
2351       }
2352     return ret_code;
2353   }
2354 }
2355 \f
2356 /* Find all the switches given to us
2357    and make a vector describing them.
2358    The elements of the vector are strings, one per switch given.
2359    If a switch uses following arguments, then the `part1' field
2360    is the switch itself and the `args' field
2361    is a null-terminated vector containing the following arguments.
2362    The `live_cond' field is 1 if the switch is true in a conditional spec,
2363    -1 if false (overridden by a later switch), and is initialized to zero.
2364    The `validated' field is nonzero if any spec has looked at this switch;
2365    if it remains zero at the end of the run, it must be meaningless.  */
2366
2367 struct switchstr
2368 {
2369   const char *part1;
2370   char **args;
2371   int live_cond;
2372   int validated;
2373 };
2374
2375 static struct switchstr *switches;
2376
2377 static int n_switches;
2378
2379 struct infile
2380 {
2381   const char *name;
2382   const char *language;
2383 };
2384
2385 /* Also a vector of input files specified.  */
2386
2387 static struct infile *infiles;
2388
2389 static int n_infiles;
2390
2391 /* This counts the number of libraries added by lang_specific_driver, so that
2392    we can tell if there were any user supplied any files or libraries.  */
2393
2394 static int added_libraries;
2395
2396 /* And a vector of corresponding output files is made up later.  */
2397
2398 static const char **outfiles;
2399
2400 /* Used to track if none of the -B paths are used.  */
2401 static int warn_B;
2402
2403 /* Used to track if standard path isn't used and -b or -V is specified.  */
2404 static int warn_std;
2405
2406 /* Gives value to pass as "warn" to add_prefix for standard prefixes.  */
2407 static int *warn_std_ptr = 0;
2408
2409 #if defined(FREEBSD_NATIVE)
2410 #include <objformat.h>
2411
2412 typedef enum { OBJFMT_UNKNOWN, OBJFMT_AOUT, OBJFMT_ELF } objf_t;
2413
2414 static objf_t objformat = OBJFMT_UNKNOWN;
2415 #endif  /* FREEBSD_NATIVE */
2416 \f
2417 #if defined(HAVE_OBJECT_SUFFIX) || defined(HAVE_EXECUTABLE_SUFFIX)
2418
2419 /* Convert NAME to a new name if it is the standard suffix.  DO_EXE
2420    is true if we should look for an executable suffix as well.  */
2421
2422 static char *
2423 convert_filename (name, do_exe)
2424      char *name;
2425      int do_exe;
2426 {
2427   int i;
2428   int len;
2429
2430   if (name == NULL)
2431     return NULL;
2432   
2433   len = strlen (name);
2434
2435 #ifdef HAVE_OBJECT_SUFFIX
2436   /* Convert x.o to x.obj if OBJECT_SUFFIX is ".obj".  */
2437   if (len > 2
2438       && name[len - 2] == '.'
2439       && name[len - 1] == 'o')
2440     {
2441       obstack_grow (&obstack, name, len - 2);
2442       obstack_grow0 (&obstack, OBJECT_SUFFIX, strlen (OBJECT_SUFFIX));
2443       name = obstack_finish (&obstack);
2444     }
2445 #endif
2446
2447 #ifdef HAVE_EXECUTABLE_SUFFIX
2448   /* If there is no filetype, make it the executable suffix (which includes
2449      the ".").  But don't get confused if we have just "-o".  */
2450   if (! do_exe || EXECUTABLE_SUFFIX[0] == 0 || (len == 2 && name[0] == '-'))
2451     return name;
2452
2453   for (i = len - 1; i >= 0; i--)
2454     if (IS_DIR_SEPARATOR (name[i]))
2455       break;
2456
2457   for (i++; i < len; i++)
2458     if (name[i] == '.')
2459       return name;
2460
2461   obstack_grow (&obstack, name, len);
2462   obstack_grow0 (&obstack, EXECUTABLE_SUFFIX, strlen (EXECUTABLE_SUFFIX));
2463   name = obstack_finish (&obstack);
2464 #endif
2465
2466   return name;
2467 }
2468 #endif
2469 \f
2470 /* Display the command line switches accepted by gcc.  */
2471 static void
2472 display_help ()
2473 {
2474   printf ("Usage: %s [options] file...\n", programname);
2475   printf ("Options:\n");
2476
2477   printf ("  --help                   Display this information\n");
2478   if (! verbose_flag)
2479     printf ("  (Use '-v --help' to display command line options of sub-processes)\n");
2480   printf ("  -dumpspecs               Display all of the built in spec strings\n");
2481   printf ("  -dumpversion             Display the version of the compiler\n");
2482   printf ("  -dumpmachine             Display the compiler's target processor\n");
2483   printf ("  -print-search-dirs       Display the directories in the compiler's search path\n");
2484   printf ("  -print-libgcc-file-name  Display the name of the compiler's companion library\n");
2485   printf ("  -print-file-name=<lib>   Display the full path to library <lib>\n");
2486   printf ("  -print-prog-name=<prog>  Display the full path to compiler component <prog>\n");
2487   printf ("  -print-multi-directory   Display the root directory for versions of libgcc\n");
2488   printf ("  -print-multi-lib         Display the mapping between command line options and\n");
2489   printf ("                            multiple library search directories\n");
2490   printf ("  -Wa,<options>            Pass comma-separated <options> on to the assembler\n");
2491   printf ("  -Wp,<options>            Pass comma-separated <options> on to the preprocessor\n");
2492   printf ("  -Wl,<options>            Pass comma-separated <options> on to the linker\n");
2493   printf ("  -Xlinker <arg>           Pass <arg> on to the linker\n");
2494   printf ("  -save-temps              Do not delete intermediate files\n");
2495   printf ("  -pipe                    Use pipes rather than intermediate files\n");
2496   printf ("  -specs=<file>            Override builtin specs with the contents of <file>\n");
2497   printf ("  -std=<standard>          Assume that the input sources are for <standard>\n");
2498   printf ("  -B <directory>           Add <directory> to the compiler's search paths\n");
2499   printf ("  -b <machine>             Run gcc for target <machine>, if installed\n");
2500   printf ("  -V <version>             Run gcc version number <version>, if installed\n");
2501   printf ("  -v                       Display the programs invoked by the compiler\n");
2502   printf ("  -E                       Preprocess only; do not compile, assemble or link\n");
2503   printf ("  -S                       Compile only; do not assemble or link\n");
2504   printf ("  -c                       Compile and assemble, but do not link\n");
2505   printf ("  -o <file>                Place the output into <file>\n");
2506   printf ("  -x <language>            Specify the language of the following input files\n");
2507   printf ("                            Permissable languages include: c c++ assembler none\n");
2508   printf ("                            'none' means revert to the default behaviour of\n");
2509   printf ("                            guessing the language based on the file's extension\n");
2510
2511   printf ("\nOptions starting with -g, -f, -m, -O or -W are automatically passed on to\n");
2512   printf ("the various sub-processes invoked by %s.  In order to pass other options\n",
2513           programname);
2514   printf ("on to these processes the -W<letter> options must be used.\n");
2515
2516   /* The rest of the options are displayed by invocations of the various
2517      sub-processes.  */
2518 }
2519
2520 static void                                                             
2521 add_preprocessor_option (option, len)                                   
2522      const char * option;
2523      int len;
2524 {                                                                       
2525   n_preprocessor_options++;
2526                                                                         
2527   if (! preprocessor_options)
2528     preprocessor_options
2529       = (char **) xmalloc (n_preprocessor_options * sizeof (char *));
2530   else
2531     preprocessor_options
2532       = (char **) xrealloc (preprocessor_options,
2533                             n_preprocessor_options * sizeof (char *));
2534                                                                         
2535   preprocessor_options [n_preprocessor_options - 1] =
2536     save_string (option, len);
2537 }
2538      
2539 static void                                                             
2540 add_assembler_option (option, len)                                      
2541      const char * option;
2542      int len;
2543 {
2544   n_assembler_options++;
2545
2546   if (! assembler_options)
2547     assembler_options
2548       = (char **) xmalloc (n_assembler_options * sizeof (char *));
2549   else
2550     assembler_options
2551       = (char **) xrealloc (assembler_options,
2552                             n_assembler_options * sizeof (char *));
2553
2554   assembler_options [n_assembler_options - 1] = save_string (option, len);
2555 }
2556      
2557 static void                                                             
2558 add_linker_option (option, len)                                 
2559      const char * option;
2560      int    len;
2561 {
2562   n_linker_options++;
2563
2564   if (! linker_options)
2565     linker_options
2566       = (char **) xmalloc (n_linker_options * sizeof (char *));
2567   else
2568     linker_options
2569       = (char **) xrealloc (linker_options,
2570                             n_linker_options * sizeof (char *));
2571
2572   linker_options [n_linker_options - 1] = save_string (option, len);
2573 }
2574 \f
2575 /* Create the vector `switches' and its contents.
2576    Store its length in `n_switches'.  */
2577
2578 static void
2579 process_command (argc, argv)
2580      int argc;
2581      char **argv;
2582 {
2583   register int i;
2584   const char *temp;
2585   char *temp1;
2586   char *spec_lang = 0;
2587   int last_language_n_infiles;
2588   int have_c = 0;
2589   int have_o = 0;
2590   int lang_n_infiles = 0;
2591
2592   GET_ENV_PATH_LIST (gcc_exec_prefix, "GCC_EXEC_PREFIX");
2593
2594   n_switches = 0;
2595   n_infiles = 0;
2596   added_libraries = 0;
2597
2598   /* Figure compiler version from version string.  */
2599
2600   compiler_version = temp1 =
2601     save_string (version_string, strlen (version_string));
2602   for (; *temp1; ++temp1)
2603     {
2604       if (*temp1 == ' ')
2605         {
2606           *temp1 = '\0';
2607           break;
2608         }
2609     }
2610
2611   /* Set up the default search paths.  */
2612
2613   if (gcc_exec_prefix)
2614     {
2615       int len = strlen (gcc_exec_prefix);
2616       if (len > (int) sizeof ("/lib/gcc-lib/")-1
2617           && (IS_DIR_SEPARATOR (gcc_exec_prefix[len-1])))
2618         {
2619           temp = gcc_exec_prefix + len - sizeof ("/lib/gcc-lib/") + 1;
2620           if (IS_DIR_SEPARATOR (*temp)
2621               && strncmp (temp+1, "lib", 3) == 0
2622               && IS_DIR_SEPARATOR (temp[4])
2623               && strncmp (temp+5, "gcc-lib", 7) == 0)
2624             len -= sizeof ("/lib/gcc-lib/") - 1;
2625         }
2626
2627       set_std_prefix (gcc_exec_prefix, len);
2628       add_prefix (&exec_prefixes, gcc_exec_prefix, "GCC", 0, 0, NULL_PTR);
2629       add_prefix (&startfile_prefixes, gcc_exec_prefix, "GCC", 0, 0, NULL_PTR);
2630     }
2631
2632   /* COMPILER_PATH and LIBRARY_PATH have values
2633      that are lists of directory names with colons.  */
2634
2635   GET_ENV_PATH_LIST (temp, "COMPILER_PATH");
2636   if (temp)
2637     {
2638       const char *startp, *endp;
2639       char *nstore = (char *) alloca (strlen (temp) + 3);
2640
2641       startp = endp = temp;
2642       while (1)
2643         {
2644           if (*endp == PATH_SEPARATOR || *endp == 0)
2645             {
2646               strncpy (nstore, startp, endp-startp);
2647               if (endp == startp)
2648                 strcpy (nstore, concat (".", dir_separator_str, NULL_PTR));
2649               else if (!IS_DIR_SEPARATOR (endp[-1]))
2650                 {
2651                   nstore[endp-startp] = DIR_SEPARATOR;
2652                   nstore[endp-startp+1] = 0;
2653                 }
2654               else
2655                 nstore[endp-startp] = 0;
2656               add_prefix (&exec_prefixes, nstore, 0, 0, 0, NULL_PTR);
2657               add_prefix (&include_prefixes,
2658                           concat (nstore, "include", NULL_PTR),
2659                           0, 0, 0, NULL_PTR);
2660               if (*endp == 0)
2661                 break;
2662               endp = startp = endp + 1;
2663             }
2664           else
2665             endp++;
2666         }
2667     }
2668
2669   GET_ENV_PATH_LIST (temp, "LIBRARY_PATH");
2670   if (temp && *cross_compile == '0')
2671     {
2672       const char *startp, *endp;
2673       char *nstore = (char *) alloca (strlen (temp) + 3);
2674
2675       startp = endp = temp;
2676       while (1)
2677         {
2678           if (*endp == PATH_SEPARATOR || *endp == 0)
2679             {
2680               strncpy (nstore, startp, endp-startp);
2681               if (endp == startp)
2682                 strcpy (nstore, concat (".", dir_separator_str, NULL_PTR));
2683               else if (!IS_DIR_SEPARATOR (endp[-1]))
2684                 {
2685                   nstore[endp-startp] = DIR_SEPARATOR;
2686                   nstore[endp-startp+1] = 0;
2687                 }
2688               else
2689                 nstore[endp-startp] = 0;
2690               add_prefix (&startfile_prefixes, nstore, NULL_PTR,
2691                           0, 0, NULL_PTR);
2692               if (*endp == 0)
2693                 break;
2694               endp = startp = endp + 1;
2695             }
2696           else
2697             endp++;
2698         }
2699     }
2700
2701   /* Use LPATH like LIBRARY_PATH (for the CMU build program).  */
2702   GET_ENV_PATH_LIST (temp, "LPATH");
2703   if (temp && *cross_compile == '0')
2704     {
2705       const char *startp, *endp;
2706       char *nstore = (char *) alloca (strlen (temp) + 3);
2707
2708       startp = endp = temp;
2709       while (1)
2710         {
2711           if (*endp == PATH_SEPARATOR || *endp == 0)
2712             {
2713               strncpy (nstore, startp, endp-startp);
2714               if (endp == startp)
2715                 strcpy (nstore, concat (".", dir_separator_str, NULL_PTR));
2716               else if (!IS_DIR_SEPARATOR (endp[-1]))
2717                 {
2718                   nstore[endp-startp] = DIR_SEPARATOR;
2719                   nstore[endp-startp+1] = 0;
2720                 }
2721               else
2722                 nstore[endp-startp] = 0;
2723               add_prefix (&startfile_prefixes, nstore, NULL_PTR,
2724                           0, 0, NULL_PTR);
2725               if (*endp == 0)
2726                 break;
2727               endp = startp = endp + 1;
2728             }
2729           else
2730             endp++;
2731         }
2732     }
2733
2734 #if defined(FREEBSD_NATIVE)
2735   {
2736     char buf[64];
2737     if (getobjformat (buf, sizeof buf, &argc, argv))
2738       if (strcmp (buf, "aout") == 0)
2739         objformat = OBJFMT_AOUT;
2740       else if (strcmp (buf, "elf") == 0)
2741         objformat = OBJFMT_ELF;
2742       else
2743         fprintf(stderr, "Unrecognized object format: %s\n", buf);
2744   }
2745 #endif  /* FREEBSD_NATIVE */
2746
2747   /* Options specified as if they appeared on the command line.  */
2748   temp = getenv ("GCC_OPTIONS");
2749   if ((temp) && (strlen (temp) > 0))
2750     {
2751       int len;
2752       int optc = 1;
2753       int new_argc;
2754       char **new_argv;
2755       char *envopts;
2756
2757       while (isspace (*temp))
2758         temp++;
2759       len = strlen (temp);
2760       envopts = (char *) xmalloc (len + 1);
2761       strcpy (envopts, temp);
2762
2763       for (i = 0; i < (len - 1); i++)
2764         if ((isspace (envopts[i])) && ! (isspace (envopts[i+1])))
2765           optc++;
2766
2767       new_argv = (char **) alloca ((optc + argc) * sizeof(char *));
2768
2769       for (i = 0, new_argc = 1; new_argc <= optc; new_argc++)
2770         {
2771           while (isspace (envopts[i]))
2772             i++;
2773           new_argv[new_argc] = envopts + i;
2774           while (!isspace (envopts[i]) && (envopts[i] != '\0'))
2775             i++;
2776           envopts[i++] = '\0';
2777         }
2778       for (i = 1; i < argc; i++)
2779         new_argv[new_argc++] = argv[i];
2780
2781       argv = new_argv;
2782       argc = new_argc;
2783     }
2784
2785   /* Convert new-style -- options to old-style.  */
2786   translate_options (&argc, &argv);
2787
2788   /* Do language-specific adjustment/addition of flags.  */
2789   lang_specific_driver (fatal, &argc, &argv, &added_libraries);
2790
2791   /* Scan argv twice.  Here, the first time, just count how many switches
2792      there will be in their vector, and how many input files in theirs.
2793      Here we also parse the switches that cc itself uses (e.g. -v).  */
2794
2795   for (i = 1; i < argc; i++)
2796     {
2797       if (! strcmp (argv[i], "-dumpspecs"))
2798         {
2799           struct spec_list *sl;
2800           init_spec ();
2801           for (sl = specs; sl; sl = sl->next)
2802             printf ("*%s:\n%s\n\n", sl->name, *(sl->ptr_spec));
2803           if (link_command_spec)
2804             printf ("*link_command:\n%s\n\n", link_command_spec);
2805           exit (0);
2806         }
2807       else if (! strcmp (argv[i], "-dumpversion"))
2808         {
2809           printf ("%s\n", spec_version);
2810           exit (0);
2811         }
2812       else if (! strcmp (argv[i], "-dumpmachine"))
2813         {
2814           printf ("%s\n", spec_machine);
2815           exit  (0);
2816         }
2817       else if (strcmp (argv[i], "-fhelp") == 0)
2818         {
2819           /* translate_options () has turned --help into -fhelp.  */
2820           print_help_list = 1;
2821
2822           /* We will be passing a dummy file on to the sub-processes.  */
2823           n_infiles++;
2824           n_switches++;
2825           
2826           add_preprocessor_option ("--help", 6);
2827           add_assembler_option ("--help", 6);
2828           add_linker_option ("--help", 6);
2829         }
2830       else if (! strcmp (argv[i], "-print-search-dirs"))
2831         print_search_dirs = 1;
2832       else if (! strcmp (argv[i], "-print-libgcc-file-name"))
2833         print_file_name = "libgcc.a";
2834       else if (! strncmp (argv[i], "-print-file-name=", 17))
2835         print_file_name = argv[i] + 17;
2836       else if (! strncmp (argv[i], "-print-prog-name=", 17))
2837         print_prog_name = argv[i] + 17;
2838       else if (! strcmp (argv[i], "-print-multi-lib"))
2839         print_multi_lib = 1;
2840       else if (! strcmp (argv[i], "-print-multi-directory"))
2841         print_multi_directory = 1;
2842       else if (! strncmp (argv[i], "-Wa,", 4))
2843         {
2844           int prev, j;
2845           /* Pass the rest of this option to the assembler.  */
2846
2847           /* Split the argument at commas.  */
2848           prev = 4;
2849           for (j = 4; argv[i][j]; j++)
2850             if (argv[i][j] == ',')
2851               {
2852                 add_assembler_option (argv[i] + prev, j - prev);
2853                 prev = j + 1;
2854               }
2855           
2856           /* Record the part after the last comma.  */
2857           add_assembler_option (argv[i] + prev, j - prev);
2858         }
2859       else if (! strncmp (argv[i], "-Wp,", 4))
2860         {
2861           int prev, j;
2862           /* Pass the rest of this option to the preprocessor.  */
2863
2864           /* Split the argument at commas.  */
2865           prev = 4;
2866           for (j = 4; argv[i][j]; j++)
2867             if (argv[i][j] == ',')
2868               {
2869                 add_preprocessor_option (argv[i] + prev, j - prev);
2870                 prev = j + 1;
2871               }
2872           
2873           /* Record the part after the last comma.  */
2874           add_preprocessor_option (argv[i] + prev, j - prev);
2875         }
2876       else if (argv[i][0] == '+' && argv[i][1] == 'e')
2877         /* The +e options to the C++ front-end.  */
2878         n_switches++;
2879       else if (strncmp (argv[i], "-Wl,", 4) == 0)
2880         {
2881           int j;
2882           /* Split the argument at commas.  */
2883           for (j = 3; argv[i][j]; j++)
2884             n_infiles += (argv[i][j] == ',');
2885         }
2886       else if (strcmp (argv[i], "-Xlinker") == 0)
2887         {
2888           if (i + 1 == argc)
2889             fatal ("argument to `-Xlinker' is missing");
2890
2891           n_infiles++;
2892           i++;
2893         }
2894       else if (strcmp (argv[i], "-l") == 0)
2895         {
2896           if (i + 1 == argc)
2897             fatal ("argument to `-l' is missing");
2898
2899           n_infiles++;
2900           i++;
2901         }
2902       else if (strncmp (argv[i], "-l", 2) == 0)
2903         n_infiles++;
2904       else if (strcmp (argv[i], "-save-temps") == 0)
2905         {
2906           save_temps_flag = 1;
2907           n_switches++;
2908         }
2909       else if (strcmp (argv[i], "-specs") == 0)
2910         {
2911           struct user_specs *user = (struct user_specs *)
2912             xmalloc (sizeof (struct user_specs));
2913           if (++i >= argc)
2914             fatal ("argument to `-specs' is missing");
2915
2916           user->next = (struct user_specs *)0;
2917           user->filename = argv[i];
2918           if (user_specs_tail)
2919             user_specs_tail->next = user;
2920           else
2921             user_specs_head = user;
2922           user_specs_tail = user;
2923         }
2924       else if (strncmp (argv[i], "-specs=", 7) == 0)
2925         {
2926           struct user_specs *user = (struct user_specs *)
2927             xmalloc (sizeof (struct user_specs));
2928           if (strlen (argv[i]) == 7)
2929             fatal ("argument to `-specs=' is missing");
2930
2931           user->next = (struct user_specs *)0;
2932           user->filename = argv[i]+7;
2933           if (user_specs_tail)
2934             user_specs_tail->next = user;
2935           else
2936             user_specs_head = user;
2937           user_specs_tail = user;
2938         }
2939       else if (argv[i][0] == '-' && argv[i][1] != 0)
2940         {
2941           register char *p = &argv[i][1];
2942           register int c = *p;
2943
2944           switch (c)
2945             {
2946             case 'b':
2947               n_switches++;
2948               if (p[1] == 0 && i + 1 == argc)
2949                 fatal ("argument to `-b' is missing");
2950               if (p[1] == 0)
2951                 spec_machine = argv[++i];
2952               else
2953                 spec_machine = p + 1;
2954
2955               warn_std_ptr = &warn_std;
2956               break;
2957
2958             case 'B':
2959               {
2960                 char *value;
2961                 if (p[1] == 0 && i + 1 == argc)
2962                   fatal ("argument to `-B' is missing");
2963                 if (p[1] == 0)
2964                   value = argv[++i];
2965                 else
2966                   value = p + 1;
2967                 add_prefix (&exec_prefixes, value, NULL_PTR, 1, 0, &warn_B);
2968                 add_prefix (&startfile_prefixes, value, NULL_PTR,
2969                             1, 0, &warn_B);
2970                 add_prefix (&include_prefixes, concat (value, "include",
2971                                                        NULL_PTR),
2972                             NULL_PTR, 1, 0, NULL_PTR);
2973
2974                 /* As a kludge, if the arg is "[foo/]stageN/", just add
2975                    "[foo/]include" to the include prefix.  */
2976                 {
2977                   int len = strlen (value);
2978                   if ((len == 7
2979                        || (len > 7
2980                            && (IS_DIR_SEPARATOR (value[len - 8]))))
2981                       && strncmp (value + len - 7, "stage", 5) == 0
2982                       && ISDIGIT (value[len - 2])
2983                       && (IS_DIR_SEPARATOR (value[len - 1])))
2984                     {
2985                       if (len == 7)
2986                         add_prefix (&include_prefixes, "include", NULL_PTR,
2987                                     1, 0, NULL_PTR);
2988                       else
2989                         {
2990                           char *string = xmalloc (len + 1);
2991                           strncpy (string, value, len-7);
2992                           strcpy (string+len-7, "include");
2993                           add_prefix (&include_prefixes, string, NULL_PTR,
2994                                       1, 0, NULL_PTR);
2995                         }
2996                     }
2997                 }
2998                 n_switches++;
2999               }
3000               break;
3001
3002             case 'v':   /* Print our subcommands and print versions.  */
3003               n_switches++;
3004               /* If they do anything other than exactly `-v', don't set
3005                  verbose_flag; rather, continue on to give the error.  */
3006               if (p[1] != 0)
3007                 break;
3008               verbose_flag++;
3009               break;
3010
3011             case 'V':
3012               n_switches++;
3013               if (p[1] == 0 && i + 1 == argc)
3014                 fatal ("argument to `-V' is missing");
3015               if (p[1] == 0)
3016                 spec_version = argv[++i];
3017               else
3018                 spec_version = p + 1;
3019               compiler_version = spec_version;
3020               warn_std_ptr = &warn_std;
3021
3022               /* Validate the version number.  Use the same checks
3023                  done when inserting it into a spec.
3024
3025                  The format of the version string is
3026                  ([^0-9]*-)?[0-9]+[.][0-9]+([.][0-9]+)?([- ].*)?  */
3027               {
3028                 const char *v = compiler_version;
3029
3030                 /* Ignore leading non-digits.  i.e. "foo-" in "foo-2.7.2".  */
3031                 while (! ISDIGIT (*v))
3032                   v++;
3033
3034                 if (v > compiler_version && v[-1] != '-')
3035                   fatal ("invalid version number format");
3036
3037                 /* Set V after the first period.  */
3038                 while (ISDIGIT (*v))
3039                   v++;
3040
3041                 if (*v != '.')
3042                   fatal ("invalid version number format");
3043
3044                 v++;
3045                 while (ISDIGIT (*v))
3046                   v++;
3047
3048                 if (*v != 0 && *v != ' ' && *v != '.' && *v != '-')
3049                   fatal ("invalid version number format");
3050               }
3051               break;
3052
3053             case 'S':
3054             case 'c':
3055               if (p[1] == 0)
3056                 {
3057                   have_c = 1;
3058                   n_switches++;
3059                   break;
3060                 }
3061               goto normal_switch;
3062
3063             case 'o':
3064               have_o = 1;
3065 #if defined(HAVE_EXECUTABLE_SUFFIX)
3066               if (! have_c)
3067                 {
3068                   int skip;
3069                   
3070                   /* Forward scan, just in case -S or -c is specified
3071                      after -o.  */
3072                   int j = i + 1;
3073                   if (p[1] == 0)
3074                     ++j;
3075                   while (j < argc)
3076                     {
3077                       if (argv[j][0] == '-')
3078                         {
3079                           if (SWITCH_CURTAILS_COMPILATION (argv[j][1])
3080                               && argv[j][2] == 0)
3081                             {
3082                               have_c = 1;
3083                               break;
3084                             }
3085                           else if (skip = SWITCH_TAKES_ARG (argv[j][1]))
3086                             j += skip - (argv[j][2] != 0);
3087                           else if (skip = WORD_SWITCH_TAKES_ARG (argv[j] + 1))
3088                             j += skip;
3089                         }
3090                       j++;
3091                     }
3092                 }
3093 #endif
3094 #if defined(HAVE_EXECUTABLE_SUFFIX) || defined(HAVE_OBJECT_SUFFIX)
3095               if (p[1] == 0)
3096                 argv[i+1] = convert_filename (argv[i+1], ! have_c);
3097               else
3098                 argv[i] = convert_filename (argv[i], ! have_c);
3099 #endif
3100               goto normal_switch;
3101
3102             default:
3103             normal_switch:
3104               n_switches++;
3105
3106               if (SWITCH_TAKES_ARG (c) > (p[1] != 0))
3107                 i += SWITCH_TAKES_ARG (c) - (p[1] != 0);
3108               else if (WORD_SWITCH_TAKES_ARG (p))
3109                 i += WORD_SWITCH_TAKES_ARG (p);
3110             }
3111         }
3112       else
3113         {
3114           n_infiles++;
3115           lang_n_infiles++;
3116         }
3117     }
3118
3119   if (have_c && have_o && lang_n_infiles > 1)
3120     fatal ("cannot specify -o with -c or -S and multiple compilations");
3121
3122   /* Set up the search paths before we go looking for config files.  */
3123
3124   /* These come before the md prefixes so that we will find gcc's subcommands
3125      (such as cpp) rather than those of the host system.  */
3126   /* Use 2 as fourth arg meaning try just the machine as a suffix,
3127      as well as trying the machine and the version.  */
3128 #ifdef FREEBSD_NATIVE
3129   switch (objformat)
3130     {
3131     case OBJFMT_AOUT:
3132       n_switches++;             /* add implied -maout */
3133       add_prefix (&exec_prefixes, PREFIX"/libexec/aout/", "BINUTILS",
3134                   0, 0, NULL_PTR);
3135       break;
3136     case OBJFMT_ELF:
3137       add_prefix (&exec_prefixes, PREFIX"/libexec/elf/", "BINUTILS",
3138                   0, 0, NULL_PTR);
3139       break;
3140     case OBJFMT_UNKNOWN:
3141       fatal ("object format unknown");
3142     }
3143 #endif  /* FREEBSD_NATIVE */
3144
3145 #ifndef OS2
3146   add_prefix (&exec_prefixes, standard_exec_prefix, "BINUTILS",
3147               0, 2, warn_std_ptr);
3148 #ifndef FREEBSD_NATIVE
3149   add_prefix (&exec_prefixes, standard_exec_prefix_1, "BINUTILS",
3150               0, 2, warn_std_ptr);
3151 #endif  /* not FREEBSD_NATIVE */
3152 #endif
3153
3154 #ifndef FREEBSD_NATIVE
3155   add_prefix (&startfile_prefixes, standard_exec_prefix, "BINUTILS",
3156               0, 1, warn_std_ptr);
3157   add_prefix (&startfile_prefixes, standard_exec_prefix_1, "BINUTILS",
3158               0, 1, warn_std_ptr);
3159 #endif  /* not FREEBSD_NATIVE */
3160
3161   tooldir_prefix = concat (tooldir_base_prefix, spec_machine, 
3162                            dir_separator_str, NULL_PTR);
3163
3164   /* If tooldir is relative, base it on exec_prefixes.  A relative
3165      tooldir lets us move the installed tree as a unit.
3166
3167      If GCC_EXEC_PREFIX is defined, then we want to add two relative
3168      directories, so that we can search both the user specified directory
3169      and the standard place.  */
3170
3171   if (!IS_DIR_SEPARATOR (*tooldir_prefix))
3172     {
3173       if (gcc_exec_prefix)
3174         {
3175           char *gcc_exec_tooldir_prefix
3176             = concat (gcc_exec_prefix, spec_machine, dir_separator_str,
3177                       spec_version, dir_separator_str, tooldir_prefix, NULL_PTR);
3178
3179           add_prefix (&exec_prefixes,
3180                       concat (gcc_exec_tooldir_prefix, "bin", 
3181                               dir_separator_str, NULL_PTR),
3182                       NULL_PTR, 0, 0, NULL_PTR);
3183           add_prefix (&startfile_prefixes,
3184                       concat (gcc_exec_tooldir_prefix, "lib", 
3185                               dir_separator_str, NULL_PTR),
3186                       NULL_PTR, 0, 0, NULL_PTR);
3187         }
3188
3189       tooldir_prefix = concat (standard_exec_prefix, spec_machine,
3190                                dir_separator_str, spec_version, 
3191                                dir_separator_str, tooldir_prefix, NULL_PTR);
3192     }
3193
3194 #ifndef FREEBSD_NATIVE
3195   add_prefix (&exec_prefixes, 
3196               concat (tooldir_prefix, "bin", dir_separator_str, NULL_PTR),
3197               "BINUTILS", 0, 0, NULL_PTR);
3198   add_prefix (&startfile_prefixes,
3199               concat (tooldir_prefix, "lib", dir_separator_str, NULL_PTR),
3200               "BINUTILS", 0, 0, NULL_PTR);
3201 #endif  /* not FREEBSD_NATIVE */
3202
3203   /* More prefixes are enabled in main, after we read the specs file
3204      and determine whether this is cross-compilation or not.  */
3205
3206
3207   /* Then create the space for the vectors and scan again.  */
3208
3209   switches = ((struct switchstr *)
3210               xmalloc ((n_switches + 1) * sizeof (struct switchstr)));
3211   infiles = (struct infile *) xmalloc ((n_infiles + 1) * sizeof (struct infile));
3212   n_switches = 0;
3213   n_infiles = 0;
3214   last_language_n_infiles = -1;
3215
3216   /* This, time, copy the text of each switch and store a pointer
3217      to the copy in the vector of switches.
3218      Store all the infiles in their vector.  */
3219
3220 #if defined(FREEBSD_NATIVE)
3221   switch (objformat)
3222     {
3223     case OBJFMT_AOUT:
3224       switches[n_switches].part1 = "maout";
3225       switches[n_switches].args = 0;
3226       switches[n_switches].live_cond = 0;
3227       switches[n_switches].validated = 0;
3228       n_switches++;
3229       putenv("OBJFORMAT=aout");
3230       break;
3231     case OBJFMT_ELF:
3232       putenv("OBJFORMAT=elf");
3233       break;
3234     case OBJFMT_UNKNOWN:
3235       fatal ("object format unknown");
3236     }
3237 #endif  /* FREEBSD_NATIVE */
3238
3239   for (i = 1; i < argc; i++)
3240     {
3241       /* Just skip the switches that were handled by the preceding loop.  */
3242       if (! strncmp (argv[i], "-Wa,", 4))
3243         ;
3244       else if (! strncmp (argv[i], "-Wp,", 4))
3245         ;
3246       else if (! strcmp (argv[i], "-print-search-dirs"))
3247         ;
3248       else if (! strcmp (argv[i], "-print-libgcc-file-name"))
3249         ;
3250       else if (! strncmp (argv[i], "-print-file-name=", 17))
3251         ;
3252       else if (! strncmp (argv[i], "-print-prog-name=", 17))
3253         ;
3254       else if (! strcmp (argv[i], "-print-multi-lib"))
3255         ;
3256       else if (! strcmp (argv[i], "-print-multi-directory"))
3257         ;
3258       else if (strcmp (argv[i], "-fhelp") == 0)
3259         {
3260           if (verbose_flag)
3261             {
3262               /* Create a dummy input file, so that we can pass --help on to
3263                  the various sub-processes.  */
3264               infiles[n_infiles].language = "c";
3265               infiles[n_infiles++].name   = "help-dummy";
3266               
3267               /* Preserve the --help switch so that it can be caught by the
3268                  cc1 spec string.  */
3269               switches[n_switches].part1     = "--help";
3270               switches[n_switches].args      = 0;
3271               switches[n_switches].live_cond = 0;
3272               switches[n_switches].validated     = 0;
3273               
3274               n_switches++;
3275             }
3276         }
3277       else if (argv[i][0] == '+' && argv[i][1] == 'e')
3278         {
3279           /* Compensate for the +e options to the C++ front-end;
3280              they're there simply for cfront call-compatibility.  We do
3281              some magic in default_compilers to pass them down properly.
3282              Note we deliberately start at the `+' here, to avoid passing
3283              -e0 or -e1 down into the linker.  */
3284           switches[n_switches].part1 = &argv[i][0];
3285           switches[n_switches].args = 0;
3286           switches[n_switches].live_cond = 0;
3287           switches[n_switches].validated = 0;
3288           n_switches++;
3289         }
3290       else if (strncmp (argv[i], "-Wl,", 4) == 0)
3291         {
3292           int prev, j;
3293           /* Split the argument at commas.  */
3294           prev = 4;
3295           for (j = 4; argv[i][j]; j++)
3296             if (argv[i][j] == ',')
3297               {
3298                 infiles[n_infiles].language = "*";
3299                 infiles[n_infiles++].name
3300                   = save_string (argv[i] + prev, j - prev);
3301                 prev = j + 1;
3302               }
3303           /* Record the part after the last comma.  */
3304           infiles[n_infiles].language = "*";
3305           infiles[n_infiles++].name = argv[i] + prev;
3306         }
3307       else if (strcmp (argv[i], "-Xlinker") == 0)
3308         {
3309           infiles[n_infiles].language = "*";
3310           infiles[n_infiles++].name = argv[++i];
3311         }
3312       else if (strcmp (argv[i], "-l") == 0)
3313         { /* POSIX allows separation of -l and the lib arg;
3314              canonicalize by concatenating -l with its arg */
3315           infiles[n_infiles].language = "*";
3316           infiles[n_infiles++].name = concat ("-l", argv[++i], NULL);
3317         }
3318       else if (strncmp (argv[i], "-l", 2) == 0)
3319         {
3320           infiles[n_infiles].language = "*";
3321           infiles[n_infiles++].name = argv[i];
3322         }
3323       else if (strcmp (argv[i], "-specs") == 0)
3324         i++;
3325       else if (strncmp (argv[i], "-specs=", 7) == 0)
3326         ;
3327       /* -save-temps overrides -pipe, so that temp files are produced */
3328       else if (save_temps_flag && strcmp (argv[i], "-pipe") == 0)
3329         error ("Warning: -pipe ignored since -save-temps specified");
3330       else if (argv[i][0] == '-' && argv[i][1] != 0)
3331         {
3332           register char *p = &argv[i][1];
3333           register int c = *p;
3334
3335           if (c == 'x')
3336             {
3337               if (p[1] == 0 && i + 1 == argc)
3338                 fatal ("argument to `-x' is missing");
3339               if (p[1] == 0)
3340                 spec_lang = argv[++i];
3341               else
3342                 spec_lang = p + 1;
3343               if (! strcmp (spec_lang, "none"))
3344                 /* Suppress the warning if -xnone comes after the last input
3345                    file, because alternate command interfaces like g++ might
3346                    find it useful to place -xnone after each input file.  */
3347                 spec_lang = 0;
3348               else
3349                 last_language_n_infiles = n_infiles;
3350               continue;
3351             }
3352           switches[n_switches].part1 = p;
3353           /* Deal with option arguments in separate argv elements.  */
3354           if ((SWITCH_TAKES_ARG (c) > (p[1] != 0))
3355               || WORD_SWITCH_TAKES_ARG (p))
3356             {
3357               int j = 0;
3358               int n_args = WORD_SWITCH_TAKES_ARG (p);
3359
3360               if (n_args == 0)
3361                 {
3362                   /* Count only the option arguments in separate argv elements.  */
3363                   n_args = SWITCH_TAKES_ARG (c) - (p[1] != 0);
3364                 }
3365               if (i + n_args >= argc)
3366                 fatal ("argument to `-%s' is missing", p);
3367               switches[n_switches].args
3368                 = (char **) xmalloc ((n_args + 1) * sizeof (char *));
3369               while (j < n_args)
3370                 switches[n_switches].args[j++] = argv[++i];
3371               /* Null-terminate the vector.  */
3372               switches[n_switches].args[j] = 0;
3373             }
3374           else if (index (switches_need_spaces, c))
3375             {
3376               /* On some systems, ld cannot handle some options without
3377                  a space.  So split the option from its argument.  */
3378               char *part1 = (char *) xmalloc (2);
3379               part1[0] = c;
3380               part1[1] = '\0';
3381               
3382               switches[n_switches].part1 = part1;
3383               switches[n_switches].args = (char **) xmalloc (2 * sizeof (char *));
3384               switches[n_switches].args[0] = xmalloc (strlen (p));
3385               strcpy (switches[n_switches].args[0], &p[1]);
3386               switches[n_switches].args[1] = 0;
3387             }
3388           else
3389             switches[n_switches].args = 0;
3390
3391           switches[n_switches].live_cond = 0;
3392           switches[n_switches].validated = 0;
3393           /* This is always valid, since gcc.c itself understands it.  */
3394           if (!strcmp (p, "save-temps"))
3395             switches[n_switches].validated = 1;
3396           else
3397             {
3398               char ch = switches[n_switches].part1[0];
3399               if (ch == 'V' || ch == 'b' || ch == 'B')
3400                 switches[n_switches].validated = 1;
3401             }
3402           n_switches++;
3403         }
3404       else
3405         {
3406 #ifdef HAVE_OBJECT_SUFFIX
3407           argv[i] = convert_filename (argv[i], 0);
3408 #endif
3409
3410           if (strcmp (argv[i], "-") != 0 && access (argv[i], R_OK) < 0)
3411             {
3412               perror_with_name (argv[i]);
3413               error_count++;
3414             }
3415           else
3416             {
3417               infiles[n_infiles].language = spec_lang;
3418               infiles[n_infiles++].name = argv[i];
3419             }
3420         }
3421     }
3422
3423   if (n_infiles == last_language_n_infiles && spec_lang != 0)
3424     error ("Warning: `-x %s' after last input file has no effect", spec_lang);
3425
3426   switches[n_switches].part1 = 0;
3427   infiles[n_infiles].name = 0;
3428 }
3429 \f
3430 /* Process a spec string, accumulating and running commands.  */
3431
3432 /* These variables describe the input file name.
3433    input_file_number is the index on outfiles of this file,
3434    so that the output file name can be stored for later use by %o.
3435    input_basename is the start of the part of the input file
3436    sans all directory names, and basename_length is the number
3437    of characters starting there excluding the suffix .c or whatever.  */
3438
3439 const char *input_filename;
3440 static int input_file_number;
3441 size_t input_filename_length;
3442 static int basename_length;
3443 static const char *input_basename;
3444 static const char *input_suffix;
3445
3446 /* These are variables used within do_spec and do_spec_1.  */
3447
3448 /* Nonzero if an arg has been started and not yet terminated
3449    (with space, tab or newline).  */
3450 static int arg_going;
3451
3452 /* Nonzero means %d or %g has been seen; the next arg to be terminated
3453    is a temporary file name.  */
3454 static int delete_this_arg;
3455
3456 /* Nonzero means %w has been seen; the next arg to be terminated
3457    is the output file name of this compilation.  */
3458 static int this_is_output_file;
3459
3460 /* Nonzero means %s has been seen; the next arg to be terminated
3461    is the name of a library file and we should try the standard
3462    search dirs for it.  */
3463 static int this_is_library_file;
3464
3465 /* Nonzero means that the input of this command is coming from a pipe.  */
3466 static int input_from_pipe;
3467
3468 /* Process the spec SPEC and run the commands specified therein.
3469    Returns 0 if the spec is successfully processed; -1 if failed.  */
3470
3471 int
3472 do_spec (spec)
3473      const char *spec;
3474 {
3475   int value;
3476
3477   clear_args ();
3478   arg_going = 0;
3479   delete_this_arg = 0;
3480   this_is_output_file = 0;
3481   this_is_library_file = 0;
3482   input_from_pipe = 0;
3483
3484   value = do_spec_1 (spec, 0, NULL_PTR);
3485
3486   /* Force out any unfinished command.
3487      If -pipe, this forces out the last command if it ended in `|'.  */
3488   if (value == 0)
3489     {
3490       if (argbuf_index > 0 && !strcmp (argbuf[argbuf_index - 1], "|"))
3491         argbuf_index--;
3492
3493       if (argbuf_index > 0)
3494         value = execute ();
3495     }
3496
3497   return value;
3498 }
3499
3500 /* Process the sub-spec SPEC as a portion of a larger spec.
3501    This is like processing a whole spec except that we do
3502    not initialize at the beginning and we do not supply a
3503    newline by default at the end.
3504    INSWITCH nonzero means don't process %-sequences in SPEC;
3505    in this case, % is treated as an ordinary character.
3506    This is used while substituting switches.
3507    INSWITCH nonzero also causes SPC not to terminate an argument.
3508
3509    Value is zero unless a line was finished
3510    and the command on that line reported an error.  */
3511
3512 static int
3513 do_spec_1 (spec, inswitch, soft_matched_part)
3514      const char *spec;
3515      int inswitch;
3516      const char *soft_matched_part;
3517 {
3518   register const char *p = spec;
3519   register int c;
3520   int i;
3521   const char *string;
3522   int value;
3523
3524   while ((c = *p++))
3525     /* If substituting a switch, treat all chars like letters.
3526        Otherwise, NL, SPC, TAB and % are special.  */
3527     switch (inswitch ? 'a' : c)
3528       {
3529       case '\n':
3530         /* End of line: finish any pending argument,
3531            then run the pending command if one has been started.  */
3532         if (arg_going)
3533           {
3534             obstack_1grow (&obstack, 0);
3535             string = obstack_finish (&obstack);
3536             if (this_is_library_file)
3537               string = find_file (string);
3538             store_arg (string, delete_this_arg, this_is_output_file);
3539             if (this_is_output_file)
3540               outfiles[input_file_number] = string;
3541           }
3542         arg_going = 0;
3543
3544         if (argbuf_index > 0 && !strcmp (argbuf[argbuf_index - 1], "|"))
3545           {
3546             for (i = 0; i < n_switches; i++)
3547               if (!strcmp (switches[i].part1, "pipe"))
3548                 break;
3549
3550             /* A `|' before the newline means use a pipe here,
3551                but only if -pipe was specified.
3552                Otherwise, execute now and don't pass the `|' as an arg.  */
3553             if (i < n_switches)
3554               {
3555                 input_from_pipe = 1;
3556                 switches[i].validated = 1;
3557                 break;
3558               }
3559             else
3560               argbuf_index--;
3561           }
3562
3563         if (argbuf_index > 0)
3564           {
3565             value = execute ();
3566             if (value)
3567               return value;
3568           }
3569         /* Reinitialize for a new command, and for a new argument.  */
3570         clear_args ();
3571         arg_going = 0;
3572         delete_this_arg = 0;
3573         this_is_output_file = 0;
3574         this_is_library_file = 0;
3575         input_from_pipe = 0;
3576         break;
3577
3578       case '|':
3579         /* End any pending argument.  */
3580         if (arg_going)
3581           {
3582             obstack_1grow (&obstack, 0);
3583             string = obstack_finish (&obstack);
3584             if (this_is_library_file)
3585               string = find_file (string);
3586             store_arg (string, delete_this_arg, this_is_output_file);
3587             if (this_is_output_file)
3588               outfiles[input_file_number] = string;
3589           }
3590
3591         /* Use pipe */
3592         obstack_1grow (&obstack, c);
3593         arg_going = 1;
3594         break;
3595
3596       case '\t':
3597       case ' ':
3598         /* Space or tab ends an argument if one is pending.  */
3599         if (arg_going)
3600           {
3601             obstack_1grow (&obstack, 0);
3602             string = obstack_finish (&obstack);
3603             if (this_is_library_file)
3604               string = find_file (string);
3605             store_arg (string, delete_this_arg, this_is_output_file);
3606             if (this_is_output_file)
3607               outfiles[input_file_number] = string;
3608           }
3609         /* Reinitialize for a new argument.  */
3610         arg_going = 0;
3611         delete_this_arg = 0;
3612         this_is_output_file = 0;
3613         this_is_library_file = 0;
3614         break;
3615
3616       case '%':
3617         switch (c = *p++)
3618           {
3619           case 0:
3620             fatal ("Invalid specification!  Bug in cc.");
3621
3622           case 'b':
3623             obstack_grow (&obstack, input_basename, basename_length);
3624             arg_going = 1;
3625             break;
3626
3627           case 'd':
3628             delete_this_arg = 2;
3629             break;
3630
3631           /* Dump out the directories specified with LIBRARY_PATH,
3632              followed by the absolute directories
3633              that we search for startfiles.  */
3634           case 'D':
3635             {
3636               struct prefix_list *pl = startfile_prefixes.plist;
3637               size_t bufsize = 100;
3638               char *buffer = (char *) xmalloc (bufsize);
3639               int idx;
3640
3641               for (; pl; pl = pl->next)
3642                 {
3643 #ifdef RELATIVE_PREFIX_NOT_LINKDIR
3644                   /* Used on systems which record the specified -L dirs
3645                      and use them to search for dynamic linking.  */
3646                   /* Relative directories always come from -B,
3647                      and it is better not to use them for searching
3648                      at run time.  In particular, stage1 loses  */
3649                   if (!IS_DIR_SEPARATOR (pl->prefix[0]))
3650                     continue;
3651 #endif
3652                   /* Try subdirectory if there is one.  */
3653                   if (multilib_dir != NULL)
3654                     {
3655                       if (machine_suffix)
3656                         {
3657                           if (strlen (pl->prefix) + strlen (machine_suffix)
3658                               >= bufsize)
3659                             bufsize = (strlen (pl->prefix)
3660                                        + strlen (machine_suffix)) * 2 + 1;
3661                           buffer = (char *) xrealloc (buffer, bufsize);
3662                           strcpy (buffer, pl->prefix);
3663                           strcat (buffer, machine_suffix);
3664                           if (is_directory (buffer, multilib_dir, 1))
3665                             {
3666                               do_spec_1 ("-L", 0, NULL_PTR);
3667 #ifdef SPACE_AFTER_L_OPTION
3668                               do_spec_1 (" ", 0, NULL_PTR);
3669 #endif
3670                               do_spec_1 (buffer, 1, NULL_PTR);
3671                               do_spec_1 (multilib_dir, 1, NULL_PTR);
3672                               /* Make this a separate argument.  */
3673                               do_spec_1 (" ", 0, NULL_PTR);
3674                             }
3675                         }
3676                       if (!pl->require_machine_suffix)
3677                         {
3678                           if (is_directory (pl->prefix, multilib_dir, 1))
3679                             {
3680                               do_spec_1 ("-L", 0, NULL_PTR);
3681 #ifdef SPACE_AFTER_L_OPTION
3682                               do_spec_1 (" ", 0, NULL_PTR);
3683 #endif
3684                               do_spec_1 (pl->prefix, 1, NULL_PTR);
3685                               do_spec_1 (multilib_dir, 1, NULL_PTR);
3686                               /* Make this a separate argument.  */
3687                               do_spec_1 (" ", 0, NULL_PTR);
3688                             }
3689                         }
3690                     }
3691                   if (machine_suffix)
3692                     {
3693                       if (is_directory (pl->prefix, machine_suffix, 1))
3694                         {
3695                           do_spec_1 ("-L", 0, NULL_PTR);
3696 #ifdef SPACE_AFTER_L_OPTION
3697                           do_spec_1 (" ", 0, NULL_PTR);
3698 #endif
3699                           do_spec_1 (pl->prefix, 1, NULL_PTR);
3700                           /* Remove slash from machine_suffix.  */
3701                           if (strlen (machine_suffix) >= bufsize)
3702                             bufsize = strlen (machine_suffix) * 2 + 1;
3703                           buffer = (char *) xrealloc (buffer, bufsize);
3704                           strcpy (buffer, machine_suffix);
3705                           idx = strlen (buffer);
3706                           if (IS_DIR_SEPARATOR (buffer[idx - 1]))
3707                             buffer[idx - 1] = 0;
3708                           do_spec_1 (buffer, 1, NULL_PTR);
3709                           /* Make this a separate argument.  */
3710                           do_spec_1 (" ", 0, NULL_PTR);
3711                         }
3712                     }
3713                   if (!pl->require_machine_suffix)
3714                     {
3715                       if (is_directory (pl->prefix, "", 1))
3716                         {
3717                           do_spec_1 ("-L", 0, NULL_PTR);
3718 #ifdef SPACE_AFTER_L_OPTION
3719                           do_spec_1 (" ", 0, NULL_PTR);
3720 #endif
3721                           /* Remove slash from pl->prefix.  */
3722                           if (strlen (pl->prefix) >= bufsize)
3723                             bufsize = strlen (pl->prefix) * 2 + 1;
3724                           buffer = (char *) xrealloc (buffer, bufsize);
3725                           strcpy (buffer, pl->prefix);
3726                           idx = strlen (buffer);
3727                           if (IS_DIR_SEPARATOR (buffer[idx - 1]))
3728                             buffer[idx - 1] = 0;
3729                           do_spec_1 (buffer, 1, NULL_PTR);
3730                           /* Make this a separate argument.  */
3731                           do_spec_1 (" ", 0, NULL_PTR);
3732                         }
3733                     }
3734                 }
3735               free (buffer);
3736             }
3737             break;
3738
3739           case 'e':
3740             /* %efoo means report an error with `foo' as error message
3741                and don't execute any more commands for this file.  */
3742             {
3743               const char *q = p;
3744               char *buf;
3745               while (*p != 0 && *p != '\n') p++;
3746               buf = (char *) alloca (p - q + 1);
3747               strncpy (buf, q, p - q);
3748               buf[p - q] = 0;
3749               error (buf);
3750               return -1;
3751             }
3752             break;
3753
3754           case 'g':
3755           case 'u':
3756           case 'U':
3757             if (save_temps_flag)
3758               {
3759                 obstack_grow (&obstack, input_basename, basename_length);
3760                 delete_this_arg = 0;
3761               }
3762             else
3763               {
3764 #ifdef MKTEMP_EACH_FILE
3765                 /* ??? This has a problem: the total number of
3766                    values mktemp can return is limited.
3767                    That matters for the names of object files.
3768                    In 2.4, do something about that.  */
3769                 struct temp_name *t;
3770                 int suffix_length;
3771                 const char *suffix = p;
3772
3773                 if (p[0] == '%' && p[1] == 'O')
3774                   {
3775                     p += 2;
3776                     /* We don't support extra suffix characters after %O.  */
3777                     if (*p == '.' || ISALPHA ((unsigned char)*p))
3778                       abort ();
3779                     suffix = OBJECT_SUFFIX;
3780                     suffix_length = strlen (OBJECT_SUFFIX);
3781                   }
3782                 else
3783                   {
3784                     while (*p == '.' || ISALPHA ((unsigned char)*p))
3785                       p++;
3786                     suffix_length = p - suffix;
3787                   }
3788
3789                 /* See if we already have an association of %g/%u/%U and
3790                    suffix.  */
3791                 for (t = temp_names; t; t = t->next)
3792                   if (t->length == suffix_length
3793                       && strncmp (t->suffix, suffix, suffix_length) == 0
3794                       && t->unique == (c != 'g'))
3795                     break;
3796
3797                 /* Make a new association if needed.  %u requires one.  */
3798                 if (t == 0 || c == 'u')
3799                   {
3800                     if (t == 0)
3801                       {
3802                         t = (struct temp_name *) xmalloc (sizeof (struct temp_name));
3803                         t->next = temp_names;
3804                         temp_names = t;
3805                       }
3806                     t->length = suffix_length;
3807                     t->suffix = save_string (suffix, suffix_length);
3808                     t->unique = (c != 'g');
3809                     temp_filename = make_temp_file (t->suffix);
3810                     temp_filename_length = strlen (temp_filename);
3811                     t->filename = temp_filename;
3812                     t->filename_length = temp_filename_length;
3813                   }
3814
3815                 obstack_grow (&obstack, t->filename, t->filename_length);
3816                 delete_this_arg = 1;
3817 #else
3818                 obstack_grow (&obstack, temp_filename, temp_filename_length);
3819                 if (c == 'u' || c == 'U')
3820                   {
3821                     static int unique;
3822                     char buff[9];
3823                     if (c == 'u')
3824                       unique++;
3825                     sprintf (buff, "%d", unique);
3826                     obstack_grow (&obstack, buff, strlen (buff));
3827                   }
3828 #endif
3829                 delete_this_arg = 1;
3830               }
3831             arg_going = 1;
3832             break;
3833
3834           case 'i':
3835             obstack_grow (&obstack, input_filename, input_filename_length);
3836             arg_going = 1;
3837             break;
3838
3839           case 'I':
3840             {
3841               struct prefix_list *pl = include_prefixes.plist;
3842
3843               if (gcc_exec_prefix)
3844                 {
3845                   do_spec_1 ("-iprefix", 1, NULL_PTR);
3846                   /* Make this a separate argument.  */
3847                   do_spec_1 (" ", 0, NULL_PTR);
3848                   do_spec_1 (gcc_exec_prefix, 1, NULL_PTR);
3849                   do_spec_1 (" ", 0, NULL_PTR);
3850                 }
3851
3852               for (; pl; pl = pl->next)
3853                 {
3854                   do_spec_1 ("-isystem", 1, NULL_PTR);
3855                   /* Make this a separate argument.  */
3856                   do_spec_1 (" ", 0, NULL_PTR);
3857                   do_spec_1 (pl->prefix, 1, NULL_PTR);
3858                   do_spec_1 (" ", 0, NULL_PTR);
3859                 }
3860             }
3861             break;
3862
3863           case 'o':
3864             {
3865               int max = n_infiles;
3866               max += lang_specific_extra_outfiles;
3867
3868               for (i = 0; i < max; i++)
3869                 if (outfiles[i])
3870                   store_arg (outfiles[i], 0, 0);
3871               break;
3872             }
3873
3874           case 'O':
3875             obstack_grow (&obstack, OBJECT_SUFFIX, strlen (OBJECT_SUFFIX));
3876             arg_going = 1;
3877             break;
3878
3879           case 's':
3880             this_is_library_file = 1;
3881             break;
3882
3883           case 'w':
3884             this_is_output_file = 1;
3885             break;
3886
3887           case 'W':
3888             {
3889               int cur_index = argbuf_index;
3890               /* Handle the {...} following the %W.  */
3891               if (*p != '{')
3892                 abort ();
3893               p = handle_braces (p + 1);
3894               if (p == 0)
3895                 return -1;
3896               /* If any args were output, mark the last one for deletion
3897                  on failure.  */
3898               if (argbuf_index != cur_index)
3899                 record_temp_file (argbuf[argbuf_index - 1], 0, 1);
3900               break;
3901             }
3902
3903           /* %x{OPTION} records OPTION for %X to output.  */
3904           case 'x':
3905             {
3906               const char *p1 = p;
3907               char *string;
3908
3909               /* Skip past the option value and make a copy.  */
3910               if (*p != '{')
3911                 abort ();
3912               while (*p++ != '}')
3913                 ;
3914               string = save_string (p1 + 1, p - p1 - 2);
3915
3916               /* See if we already recorded this option.  */
3917               for (i = 0; i < n_linker_options; i++)
3918                 if (! strcmp (string, linker_options[i]))
3919                   {
3920                     free (string);
3921                     return 0;
3922                   }
3923
3924               /* This option is new; add it.  */
3925               add_linker_option (string, strlen (string));
3926             }
3927             break;
3928
3929           /* Dump out the options accumulated previously using %x.  */
3930           case 'X':
3931             for (i = 0; i < n_linker_options; i++)
3932               {
3933                 do_spec_1 (linker_options[i], 1, NULL_PTR);
3934                 /* Make each accumulated option a separate argument.  */
3935                 do_spec_1 (" ", 0, NULL_PTR);
3936               }
3937             break;
3938
3939           /* Dump out the options accumulated previously using -Wa,.  */
3940           case 'Y':
3941             for (i = 0; i < n_assembler_options; i++)
3942               {
3943                 do_spec_1 (assembler_options[i], 1, NULL_PTR);
3944                 /* Make each accumulated option a separate argument.  */
3945                 do_spec_1 (" ", 0, NULL_PTR);
3946               }
3947             break;
3948
3949           /* Dump out the options accumulated previously using -Wp,.  */
3950           case 'Z':
3951             for (i = 0; i < n_preprocessor_options; i++)
3952               {
3953                 do_spec_1 (preprocessor_options[i], 1, NULL_PTR);
3954                 /* Make each accumulated option a separate argument.  */
3955                 do_spec_1 (" ", 0, NULL_PTR);
3956               }
3957             break;
3958
3959             /* Here are digits and numbers that just process
3960                a certain constant string as a spec.  */
3961
3962           case '1':
3963             value = do_spec_1 (cc1_spec, 0, NULL_PTR);
3964             if (value != 0)
3965               return value;
3966             break;
3967
3968           case '2':
3969             value = do_spec_1 (cc1plus_spec, 0, NULL_PTR);
3970             if (value != 0)
3971               return value;
3972             break;
3973
3974           case 'a':
3975             value = do_spec_1 (asm_spec, 0, NULL_PTR);
3976             if (value != 0)
3977               return value;
3978             break;
3979
3980           case 'A':
3981             value = do_spec_1 (asm_final_spec, 0, NULL_PTR);
3982             if (value != 0)
3983               return value;
3984             break;
3985
3986           case 'c':
3987             value = do_spec_1 (signed_char_spec, 0, NULL_PTR);
3988             if (value != 0)
3989               return value;
3990             break;
3991
3992           case 'C':
3993             value = do_spec_1 (cpp_spec, 0, NULL_PTR);
3994             if (value != 0)
3995               return value;
3996             break;
3997
3998           case 'E':
3999             value = do_spec_1 (endfile_spec, 0, NULL_PTR);
4000             if (value != 0)
4001               return value;
4002             break;
4003
4004           case 'l':
4005             value = do_spec_1 (link_spec, 0, NULL_PTR);
4006             if (value != 0)
4007               return value;
4008             break;
4009
4010           case 'L':
4011             value = do_spec_1 (lib_spec, 0, NULL_PTR);
4012             if (value != 0)
4013               return value;
4014             break;
4015
4016           case 'G':
4017             value = do_spec_1 (libgcc_spec, 0, NULL_PTR);
4018             if (value != 0)
4019               return value;
4020             break;
4021
4022           case 'p':
4023             {
4024               char *x = (char *) alloca (strlen (cpp_predefines) + 1);
4025               char *buf = x;
4026               char *y;
4027
4028               /* Copy all of the -D options in CPP_PREDEFINES into BUF.  */
4029               y = cpp_predefines;
4030               while (*y != 0)
4031                 {
4032                   if (! strncmp (y, "-D", 2))
4033                     /* Copy the whole option.  */
4034                     while (*y && *y != ' ' && *y != '\t')
4035                       *x++ = *y++;
4036                   else if (*y == ' ' || *y == '\t')
4037                     /* Copy whitespace to the result.  */
4038                     *x++ = *y++;
4039                   /* Don't copy other options.  */
4040                   else
4041                     y++;
4042                 }
4043
4044               *x = 0;
4045
4046               value = do_spec_1 (buf, 0, NULL_PTR);
4047               if (value != 0)
4048                 return value;
4049             }
4050             break;
4051
4052           case 'P':
4053             {
4054               char *x = (char *) alloca (strlen (cpp_predefines) * 4 + 1);
4055               char *buf = x;
4056               char *y;
4057
4058               /* Copy all of CPP_PREDEFINES into BUF,
4059                  but put __ after every -D and at the end of each arg.  */
4060               y = cpp_predefines;
4061               while (*y != 0)
4062                 {
4063                   if (! strncmp (y, "-D", 2))
4064                     {
4065                       int flag = 0;
4066
4067                       *x++ = *y++;
4068                       *x++ = *y++;
4069
4070                       if (*y != '_'
4071                           || (*(y+1) != '_'
4072                               && ! ISUPPER ((unsigned char)*(y+1))))
4073                         {
4074                           /* Stick __ at front of macro name.  */
4075                           *x++ = '_';
4076                           *x++ = '_';
4077                           /* Arrange to stick __ at the end as well.  */
4078                           flag = 1;
4079                         }
4080
4081                       /* Copy the macro name.  */
4082                       while (*y && *y != '=' && *y != ' ' && *y != '\t')
4083                         *x++ = *y++;
4084
4085                       if (flag)
4086                         {
4087                           *x++ = '_';
4088                           *x++ = '_';
4089                         }
4090
4091                       /* Copy the value given, if any.  */
4092                       while (*y && *y != ' ' && *y != '\t')
4093                         *x++ = *y++;
4094                     }
4095                   else if (*y == ' ' || *y == '\t')
4096                     /* Copy whitespace to the result.  */
4097                     *x++ = *y++;
4098                   /* Don't copy -A options  */
4099                   else
4100                     y++;
4101                 }
4102               *x++ = ' ';
4103
4104               /* Copy all of CPP_PREDEFINES into BUF,
4105                  but put __ after every -D.  */
4106               y = cpp_predefines;
4107               while (*y != 0)
4108                 {
4109                   if (! strncmp (y, "-D", 2))
4110                     {
4111                       y += 2;
4112
4113                       if (*y != '_'
4114                           || (*(y+1) != '_'
4115                               && ! ISUPPER ((unsigned char)*(y+1))))
4116                         {
4117                           /* Stick -D__ at front of macro name.  */
4118                           *x++ = '-';
4119                           *x++ = 'D';
4120                           *x++ = '_';
4121                           *x++ = '_';
4122
4123                           /* Copy the macro name.  */
4124                           while (*y && *y != '=' && *y != ' ' && *y != '\t')
4125                             *x++ = *y++;
4126
4127                           /* Copy the value given, if any.  */
4128                           while (*y && *y != ' ' && *y != '\t')
4129                             *x++ = *y++;
4130                         }
4131                       else
4132                         {
4133                           /* Do not copy this macro - we have just done it before */
4134                           while (*y && *y != ' ' && *y != '\t')
4135                             y++;
4136                         }
4137                     }
4138                   else if (*y == ' ' || *y == '\t')
4139                     /* Copy whitespace to the result.  */
4140                     *x++ = *y++;
4141                   /* Don't copy -A options  */
4142                   else
4143                     y++;
4144                 }
4145               *x++ = ' ';
4146
4147               /* Copy all of the -A options in CPP_PREDEFINES into BUF.  */
4148               y = cpp_predefines;
4149               while (*y != 0)
4150                 {
4151                   if (! strncmp (y, "-A", 2))
4152                     /* Copy the whole option.  */
4153                     while (*y && *y != ' ' && *y != '\t')
4154                       *x++ = *y++;
4155                   else if (*y == ' ' || *y == '\t')
4156                     /* Copy whitespace to the result.  */
4157                     *x++ = *y++;
4158                   /* Don't copy other options.  */
4159                   else
4160                     y++;
4161                 }
4162
4163               *x = 0;
4164
4165               value = do_spec_1 (buf, 0, NULL_PTR);
4166               if (value != 0)
4167                 return value;
4168             }
4169             break;
4170
4171           case 'S':
4172             value = do_spec_1 (startfile_spec, 0, NULL_PTR);
4173             if (value != 0)
4174               return value;
4175             break;
4176
4177             /* Here we define characters other than letters and digits.  */
4178
4179           case '{':
4180             p = handle_braces (p);
4181             if (p == 0)
4182               return -1;
4183             break;
4184
4185           case '%':
4186             obstack_1grow (&obstack, '%');
4187             break;
4188
4189           case '*':
4190             do_spec_1 (soft_matched_part, 1, NULL_PTR);
4191             do_spec_1 (" ", 0, NULL_PTR);
4192             break;
4193
4194             /* Process a string found as the value of a spec given by name.
4195                This feature allows individual machine descriptions
4196                to add and use their own specs.
4197                %[...] modifies -D options the way %P does;
4198                %(...) uses the spec unmodified.  */
4199           case '[':
4200             error ("Warning: use of obsolete %%[ operator in specs");
4201           case '(':
4202             {
4203               const char *name = p;
4204               struct spec_list *sl;
4205               int len;
4206
4207               /* The string after the S/P is the name of a spec that is to be
4208                  processed.  */
4209               while (*p && *p != ')' && *p != ']')
4210                 p++;
4211
4212               /* See if it's in the list */
4213               for (len = p - name, sl = specs; sl; sl = sl->next)
4214                 if (sl->name_len == len && !strncmp (sl->name, name, len))
4215                   {
4216                     name = *(sl->ptr_spec);
4217 #ifdef DEBUG_SPECS
4218                     notice ("Processing spec %c%s%c, which is '%s'\n",
4219                             c, sl->name, (c == '(') ? ')' : ']', name);
4220 #endif
4221                     break;
4222                   }
4223
4224               if (sl)
4225                 {
4226                   if (c == '(')
4227                     {
4228                       value = do_spec_1 (name, 0, NULL_PTR);
4229                       if (value != 0)
4230                         return value;
4231                     }
4232                   else
4233                     {
4234                       char *x = (char *) alloca (strlen (name) * 2 + 1);
4235                       char *buf = x;
4236                       const char *y = name;
4237                       int flag = 0;
4238
4239                       /* Copy all of NAME into BUF, but put __ after
4240                          every -D and at the end of each arg,  */
4241                       while (1)
4242                         {
4243                           if (! strncmp (y, "-D", 2))
4244                             {
4245                               *x++ = '-';
4246                               *x++ = 'D';
4247                               *x++ = '_';
4248                               *x++ = '_';
4249                               y += 2;
4250                               flag = 1;
4251                               continue;
4252                             }
4253                           else if (flag && (*y == ' ' || *y == '\t' || *y == '='
4254                                             || *y == '}' || *y == 0))
4255                             {
4256                               *x++ = '_';
4257                               *x++ = '_';
4258                               flag = 0;
4259                             }
4260                           if (*y == 0)
4261                             break;
4262                           else
4263                             *x++ = *y++;
4264                         }
4265                       *x = 0;
4266
4267                       value = do_spec_1 (buf, 0, NULL_PTR);
4268                       if (value != 0)
4269                         return value;
4270                     }
4271                 }
4272
4273               /* Discard the closing paren or bracket.  */
4274               if (*p)
4275                 p++;
4276             }
4277             break;
4278
4279           case 'v':
4280             {
4281               int c1 = *p++;  /* Select first or second version number.  */
4282               char *v = compiler_version;
4283               char *q;
4284
4285               /* The format of the version string is
4286                  ([^0-9]*-)?[0-9]+[.][0-9]+([.][0-9]+)?([- ].*)?  */
4287
4288               /* Ignore leading non-digits.  i.e. "foo-" in "foo-2.7.2".  */
4289               while (! ISDIGIT (*v))
4290                 v++;
4291               if (v > compiler_version && v[-1] != '-')
4292                 abort ();
4293
4294               /* If desired, advance to second version number.  */
4295               if (c1 == '2')
4296                 {
4297                   /* Set V after the first period.  */
4298                   while (ISDIGIT (*v))
4299                     v++;
4300                   if (*v != '.')
4301                     abort ();
4302                   v++;
4303                 }
4304
4305               /* Set Q at the next period or at the end.  */
4306               q = v;
4307               while (ISDIGIT (*q))
4308                 q++;
4309               if (*q != 0 && *q != ' ' && *q != '.' && *q != '-')
4310                 abort ();
4311
4312               /* Put that part into the command.  */
4313               obstack_grow (&obstack, v, q - v);
4314               arg_going = 1;
4315             }
4316             break;
4317
4318           case '|':
4319             if (input_from_pipe)
4320               do_spec_1 ("-", 0, NULL_PTR);
4321             break;
4322
4323           default:
4324             abort ();
4325           }
4326         break;
4327
4328       case '\\':
4329         /* Backslash: treat next character as ordinary.  */
4330         c = *p++;
4331
4332         /* fall through */
4333       default:
4334         /* Ordinary character: put it into the current argument.  */
4335         obstack_1grow (&obstack, c);
4336         arg_going = 1;
4337       }
4338
4339   return 0;             /* End of string */
4340 }
4341
4342 /* Return 0 if we call do_spec_1 and that returns -1.  */
4343
4344 static const char *
4345 handle_braces (p)
4346      register const char *p;
4347 {
4348   const char *filter, *body = NULL, *endbody = NULL;
4349   int pipe_p = 0;
4350   int negate;
4351   int suffix;
4352   int include_blanks = 1;
4353
4354   if (*p == '^')
4355     /* A '^' after the open-brace means to not give blanks before args.  */
4356     include_blanks = 0, ++p;
4357
4358   if (*p == '|')
4359     /* A `|' after the open-brace means,
4360        if the test fails, output a single minus sign rather than nothing.
4361        This is used in %{|!pipe:...}.  */
4362     pipe_p = 1, ++p;
4363
4364 next_member:
4365   negate = suffix = 0;
4366
4367   if (*p == '!')
4368     /* A `!' after the open-brace negates the condition:
4369        succeed if the specified switch is not present.  */
4370     negate = 1, ++p;
4371
4372   if (*p == '.')
4373     /* A `.' after the open-brace means test against the current suffix.  */
4374     {
4375       if (pipe_p)
4376         abort ();
4377
4378       suffix = 1;
4379       ++p;
4380     }
4381
4382   filter = p;
4383   while (*p != ':' && *p != '}' && *p != '|') p++;
4384
4385   if (*p == '|' && pipe_p)
4386     abort ();
4387
4388   if (!body)
4389     {
4390       if (*p != '}')
4391         {
4392           register int count = 1;
4393           register const char *q = p;
4394
4395           while (*q++ != ':') continue;
4396           body = q;
4397           
4398           while (count > 0)
4399             {
4400               if (*q == '{')
4401                 count++;
4402               else if (*q == '}')
4403                 count--;
4404               else if (*q == 0)
4405                 abort ();
4406               q++;
4407             }
4408           endbody = q;
4409         }
4410       else
4411         body = p, endbody = p+1;
4412     }
4413
4414   if (suffix)
4415     {
4416       int found = (input_suffix != 0
4417                    && (long) strlen (input_suffix) == (long)(p - filter)
4418                    && strncmp (input_suffix, filter, p - filter) == 0);
4419
4420       if (body[0] == '}')
4421         abort ();
4422
4423       if (negate != found
4424           && do_spec_1 (save_string (body, endbody-body-1), 0, NULL_PTR) < 0)
4425         return 0;
4426     }
4427   else if (p[-1] == '*' && p[0] == '}')
4428     {
4429       /* Substitute all matching switches as separate args.  */
4430       register int i;
4431       --p;
4432       for (i = 0; i < n_switches; i++)
4433         if (!strncmp (switches[i].part1, filter, p - filter)
4434             && check_live_switch (i, p - filter))
4435           give_switch (i, 0, include_blanks);
4436     }
4437   else
4438     {
4439       /* Test for presence of the specified switch.  */
4440       register int i;
4441       int present = 0;
4442
4443       /* If name specified ends in *, as in {x*:...},
4444          check for %* and handle that case.  */
4445       if (p[-1] == '*' && !negate)
4446         {
4447           int substitution;
4448           const char *r = body;
4449
4450           /* First see whether we have %*.  */
4451           substitution = 0;
4452           while (r < endbody)
4453             {
4454               if (*r == '%' && r[1] == '*')
4455                 substitution = 1;
4456               r++;
4457             }
4458           /* If we do, handle that case.  */
4459           if (substitution)
4460             {
4461               /* Substitute all matching switches as separate args.
4462                  But do this by substituting for %*
4463                  in the text that follows the colon.  */
4464
4465               unsigned hard_match_len = p - filter - 1;
4466               char *string = save_string (body, endbody - body - 1);
4467
4468               for (i = 0; i < n_switches; i++)
4469                 if (!strncmp (switches[i].part1, filter, hard_match_len)
4470                     && check_live_switch (i, -1))
4471                   {
4472                     do_spec_1 (string, 0, &switches[i].part1[hard_match_len]);
4473                     /* Pass any arguments this switch has.  */
4474                     give_switch (i, 1, 1);
4475                   }
4476
4477               /* We didn't match.  Try again.  */
4478               if (*p++ == '|')
4479                 goto next_member;
4480               return endbody;
4481             }
4482         }
4483
4484       /* If name specified ends in *, as in {x*:...},
4485          check for presence of any switch name starting with x.  */
4486       if (p[-1] == '*')
4487         {
4488           for (i = 0; i < n_switches; i++)
4489             {
4490               unsigned hard_match_len = p - filter - 1;
4491
4492               if (!strncmp (switches[i].part1, filter, hard_match_len)
4493                   && check_live_switch (i, hard_match_len))
4494                 {
4495                   present = 1;
4496                 }
4497             }
4498         }
4499       /* Otherwise, check for presence of exact name specified.  */
4500       else
4501         {
4502           for (i = 0; i < n_switches; i++)
4503             {
4504               if (!strncmp (switches[i].part1, filter, p - filter)
4505                   && switches[i].part1[p - filter] == 0
4506                   && check_live_switch (i, -1))
4507                 {
4508                   present = 1;
4509                   break;
4510                 }
4511             }
4512         }
4513
4514       /* If it is as desired (present for %{s...}, absent for %{!s...})
4515          then substitute either the switch or the specified
4516          conditional text.  */
4517       if (present != negate)
4518         {
4519           if (*p == '}')
4520             {
4521               give_switch (i, 0, include_blanks);
4522             }
4523           else
4524             {
4525               if (do_spec_1 (save_string (body, endbody - body - 1),
4526                              0, NULL_PTR) < 0)
4527                 return 0;
4528             }
4529         }
4530       else if (pipe_p)
4531         {
4532           /* Here if a %{|...} conditional fails: output a minus sign,
4533              which means "standard output" or "standard input".  */
4534           do_spec_1 ("-", 0, NULL_PTR);
4535           return endbody;
4536         }
4537     }
4538
4539   /* We didn't match; try again.  */
4540   if (*p++ == '|')
4541     goto next_member;
4542
4543   return endbody;
4544 }
4545 \f
4546 /* Return 0 iff switch number SWITCHNUM is obsoleted by a later switch
4547    on the command line.  PREFIX_LENGTH is the length of XXX in an {XXX*}
4548    spec, or -1 if either exact match or %* is used.
4549
4550    A -O switch is obsoleted by a later -O switch.  A -f, -m, or -W switch
4551    whose value does not begin with "no-" is obsoleted by the same value
4552    with the "no-", similarly for a switch with the "no-" prefix.  */
4553
4554 static int
4555 check_live_switch (switchnum, prefix_length)
4556      int switchnum;
4557      int prefix_length;
4558 {
4559   const char *name = switches[switchnum].part1;
4560   int i;
4561
4562   /* In the common case of {<at-most-one-letter>*}, a negating
4563      switch would always match, so ignore that case.  We will just
4564      send the conflicting switches to the compiler phase.  */
4565   if (prefix_length >= 0 && prefix_length <= 1)
4566     return 1;
4567
4568   /* If we already processed this switch and determined if it was
4569      live or not, return our past determination.  */
4570   if (switches[switchnum].live_cond != 0)
4571     return switches[switchnum].live_cond > 0;
4572
4573   /* Now search for duplicate in a manner that depends on the name.  */
4574   switch (*name)
4575     {
4576     case 'O':
4577         for (i = switchnum + 1; i < n_switches; i++)
4578           if (switches[i].part1[0] == 'O')
4579             {
4580               switches[switchnum].validated = 1;
4581               switches[switchnum].live_cond = -1;
4582               return 0;
4583             }
4584       break;
4585
4586     case 'W':  case 'f':  case 'm':
4587       if (! strncmp (name + 1, "no-", 3))
4588         {
4589           /* We have Xno-YYY, search for XYYY.  */
4590           for (i = switchnum + 1; i < n_switches; i++)
4591             if (switches[i].part1[0] == name[0]
4592                 && ! strcmp (&switches[i].part1[1], &name[4]))
4593             {
4594               switches[switchnum].validated = 1;
4595               switches[switchnum].live_cond = -1;
4596               return 0;
4597             }
4598         }
4599       else
4600         {
4601           /* We have XYYY, search for Xno-YYY.  */
4602           for (i = switchnum + 1; i < n_switches; i++)
4603             if (switches[i].part1[0] == name[0]
4604                 && switches[i].part1[1] == 'n'
4605                 && switches[i].part1[2] == 'o'
4606                 && switches[i].part1[3] == '-'
4607                 && !strcmp (&switches[i].part1[4], &name[1]))
4608             {
4609               switches[switchnum].validated = 1;
4610               switches[switchnum].live_cond = -1;
4611               return 0;
4612             }
4613         }
4614       break;
4615     }
4616
4617   /* Otherwise the switch is live.  */
4618   switches[switchnum].live_cond = 1;
4619   return 1;
4620 }
4621 \f
4622 /* Pass a switch to the current accumulating command
4623    in the same form that we received it.
4624    SWITCHNUM identifies the switch; it is an index into
4625    the vector of switches gcc received, which is `switches'.
4626    This cannot fail since it never finishes a command line.
4627
4628    If OMIT_FIRST_WORD is nonzero, then we omit .part1 of the argument.
4629
4630    If INCLUDE_BLANKS is nonzero, then we include blanks before each argument
4631    of the switch.  */
4632
4633 static void
4634 give_switch (switchnum, omit_first_word, include_blanks)
4635      int switchnum;
4636      int omit_first_word;
4637      int include_blanks;
4638 {
4639   if (!omit_first_word)
4640     {
4641       do_spec_1 ("-", 0, NULL_PTR);
4642       do_spec_1 (switches[switchnum].part1, 1, NULL_PTR);
4643     }
4644
4645   if (switches[switchnum].args != 0)
4646     {
4647       char **p;
4648       for (p = switches[switchnum].args; *p; p++)
4649         {
4650           if (include_blanks)
4651             do_spec_1 (" ", 0, NULL_PTR);
4652           do_spec_1 (*p, 1, NULL_PTR);
4653         }
4654     }
4655
4656   do_spec_1 (" ", 0, NULL_PTR);
4657   switches[switchnum].validated = 1;
4658 }
4659 \f
4660 /* Search for a file named NAME trying various prefixes including the
4661    user's -B prefix and some standard ones.
4662    Return the absolute file name found.  If nothing is found, return NAME.  */
4663
4664 static const char *
4665 find_file (name)
4666      const char *name;
4667 {
4668   char *newname;
4669
4670   /* Try multilib_dir if it is defined.  */
4671   if (multilib_dir != NULL)
4672     {
4673       char *try;
4674
4675       try = (char *) alloca (strlen (multilib_dir) + strlen (name) + 2);
4676       strcpy (try, multilib_dir);
4677       strcat (try, dir_separator_str);
4678       strcat (try, name);
4679
4680       newname = find_a_file (&startfile_prefixes, try, R_OK);
4681
4682       /* If we don't find it in the multi library dir, then fall
4683          through and look for it in the normal places.  */
4684       if (newname != NULL)
4685         return newname;
4686     }
4687
4688   newname = find_a_file (&startfile_prefixes, name, R_OK);
4689   return newname ? newname : name;
4690 }
4691
4692 /* Determine whether a directory exists.  If LINKER, return 0 for
4693    certain fixed names not needed by the linker.  If not LINKER, it is
4694    only important to return 0 if the host machine has a small ARG_MAX
4695    limit.  */
4696
4697 static int
4698 is_directory (path1, path2, linker)
4699      const char *path1;
4700      const char *path2;
4701      int linker;
4702 {
4703   int len1 = strlen (path1);
4704   int len2 = strlen (path2);
4705   char *path = (char *) alloca (3 + len1 + len2);
4706   char *cp;
4707   struct stat st;
4708
4709 #ifndef SMALL_ARG_MAX
4710   if (! linker)
4711     return 1;
4712 #endif
4713
4714   /* Construct the path from the two parts.  Ensure the string ends with "/.".
4715      The resulting path will be a directory even if the given path is a
4716      symbolic link.  */
4717   memcpy (path, path1, len1);
4718   memcpy (path + len1, path2, len2);
4719   cp = path + len1 + len2;
4720   if (!IS_DIR_SEPARATOR (cp[-1]))
4721     *cp++ = DIR_SEPARATOR;
4722   *cp++ = '.';
4723   *cp = '\0';
4724
4725 #ifndef FREEBSD_NATIVE
4726   /* Exclude directories that the linker is known to search.  */
4727   if (linker
4728       && ((cp - path == 6
4729            && strcmp (path, concat (dir_separator_str, "lib", 
4730                                     dir_separator_str, ".", NULL_PTR)) == 0)
4731           || (cp - path == 10
4732               && strcmp (path, concat (dir_separator_str, "usr", 
4733                                        dir_separator_str, "lib", 
4734                                        dir_separator_str, ".", NULL_PTR)) == 0)))
4735     return 0;
4736 #endif  /* not FREEBSD_NATIVE */
4737
4738   return (stat (path, &st) >= 0 && S_ISDIR (st.st_mode));
4739 }
4740 \f
4741 /* On fatal signals, delete all the temporary files.  */
4742
4743 static void
4744 fatal_error (signum)
4745      int signum;
4746 {
4747   signal (signum, SIG_DFL);
4748   delete_failure_queue ();
4749   delete_temp_files ();
4750   /* Get the same signal again, this time not handled,
4751      so its normal effect occurs.  */
4752   kill (getpid (), signum);
4753 }
4754
4755 int
4756 main (argc, argv)
4757      int argc;
4758      char **argv;
4759 {
4760   register size_t i;
4761   size_t j;
4762   int value;
4763   int linker_was_run = 0;
4764   char *explicit_link_files;
4765   char *specs_file;
4766   const char *p;
4767   struct user_specs *uptr;
4768
4769   p = argv[0] + strlen (argv[0]);
4770   while (p != argv[0] && !IS_DIR_SEPARATOR (p[-1]))
4771     --p;
4772   programname = p;
4773
4774 #ifdef HAVE_LC_MESSAGES
4775   setlocale (LC_MESSAGES, "");
4776 #endif
4777   (void) bindtextdomain (PACKAGE, localedir);
4778   (void) textdomain (PACKAGE);
4779
4780   if (signal (SIGINT, SIG_IGN) != SIG_IGN)
4781     signal (SIGINT, fatal_error);
4782 #ifdef SIGHUP
4783   if (signal (SIGHUP, SIG_IGN) != SIG_IGN)
4784     signal (SIGHUP, fatal_error);
4785 #endif
4786   if (signal (SIGTERM, SIG_IGN) != SIG_IGN)
4787     signal (SIGTERM, fatal_error);
4788 #ifdef SIGPIPE
4789   if (signal (SIGPIPE, SIG_IGN) != SIG_IGN)
4790     signal (SIGPIPE, fatal_error);
4791 #endif
4792
4793   argbuf_length = 10;
4794   argbuf = (char **) xmalloc (argbuf_length * sizeof (char *));
4795
4796   obstack_init (&obstack);
4797
4798   /* Build multilib_select, et. al from the separate lines that make up each
4799      multilib selection.  */
4800   {
4801     char **q = multilib_raw;
4802     int need_space;
4803
4804     obstack_init (&multilib_obstack);
4805     while ((p = *q++) != (char *) 0)
4806       obstack_grow (&multilib_obstack, p, strlen (p));
4807
4808     obstack_1grow (&multilib_obstack, 0);
4809     multilib_select = obstack_finish (&multilib_obstack);
4810
4811     q = multilib_matches_raw;
4812     while ((p = *q++) != (char *) 0)
4813       obstack_grow (&multilib_obstack, p, strlen (p));
4814
4815     obstack_1grow (&multilib_obstack, 0);
4816     multilib_matches = obstack_finish (&multilib_obstack);
4817
4818     need_space = FALSE;
4819     for (i = 0;
4820          i < sizeof (multilib_defaults_raw) / sizeof (multilib_defaults_raw[0]);
4821          i++)
4822       {
4823         if (need_space)
4824           obstack_1grow (&multilib_obstack, ' ');
4825         obstack_grow (&multilib_obstack,
4826                       multilib_defaults_raw[i],
4827                       strlen (multilib_defaults_raw[i]));
4828         need_space = TRUE;
4829       }
4830
4831     obstack_1grow (&multilib_obstack, 0);
4832     multilib_defaults = obstack_finish (&multilib_obstack);
4833   }
4834
4835   /* Set up to remember the pathname of gcc and any options
4836      needed for collect.  We use argv[0] instead of programname because
4837      we need the complete pathname.  */
4838   obstack_init (&collect_obstack);
4839   obstack_grow (&collect_obstack, "COLLECT_GCC=", sizeof ("COLLECT_GCC=")-1);
4840   obstack_grow (&collect_obstack, argv[0], strlen (argv[0])+1);
4841   putenv (obstack_finish (&collect_obstack));
4842
4843 #ifdef INIT_ENVIRONMENT
4844   /* Set up any other necessary machine specific environment variables.  */
4845   putenv (INIT_ENVIRONMENT);
4846 #endif
4847
4848   /* Choose directory for temp files.  */
4849
4850 #ifndef MKTEMP_EACH_FILE
4851   temp_filename = choose_temp_base ();
4852   temp_filename_length = strlen (temp_filename);
4853 #endif
4854
4855   /* Make a table of what switches there are (switches, n_switches).
4856      Make a table of specified input files (infiles, n_infiles).
4857      Decode switches that are handled locally.  */
4858
4859   process_command (argc, argv);
4860
4861   {
4862     int first_time;
4863
4864     /* Build COLLECT_GCC_OPTIONS to have all of the options specified to
4865        the compiler.  */
4866     obstack_grow (&collect_obstack, "COLLECT_GCC_OPTIONS=",
4867                   sizeof ("COLLECT_GCC_OPTIONS=")-1);
4868
4869     first_time = TRUE;
4870     for (i = 0; (int)i < n_switches; i++)
4871       {
4872         char **args;
4873         const char *p, *q;
4874         if (!first_time)
4875           obstack_grow (&collect_obstack, " ", 1);
4876
4877         first_time = FALSE;
4878         obstack_grow (&collect_obstack, "'-", 2);
4879         q = switches[i].part1;
4880         while ((p = index (q,'\'')))
4881           {
4882             obstack_grow (&collect_obstack, q, p-q);
4883             obstack_grow (&collect_obstack, "'\\''", 4);
4884             q = ++p;
4885           }
4886         obstack_grow (&collect_obstack, q, strlen (q));
4887         obstack_grow (&collect_obstack, "'", 1);
4888
4889         for (args = switches[i].args; args && *args; args++)
4890           {
4891             obstack_grow (&collect_obstack, " '", 2);
4892             q = *args;
4893             while ((p = index (q,'\'')))
4894               {
4895                 obstack_grow (&collect_obstack, q, p-q);
4896                 obstack_grow (&collect_obstack, "'\\''", 4);
4897                 q = ++p;
4898               }
4899             obstack_grow (&collect_obstack, q, strlen (q));
4900             obstack_grow (&collect_obstack, "'", 1);
4901           }
4902       }
4903     obstack_grow (&collect_obstack, "\0", 1);
4904     putenv (obstack_finish (&collect_obstack));
4905   }
4906
4907   /* Initialize the vector of specs to just the default.
4908      This means one element containing 0s, as a terminator.  */
4909
4910   compilers = (struct compiler *) xmalloc (sizeof default_compilers);
4911   bcopy ((char *) default_compilers, (char *) compilers,
4912          sizeof default_compilers);
4913   n_compilers = n_default_compilers;
4914
4915   /* Read specs from a file if there is one.  */
4916
4917 #ifdef FREEBSD_NATIVE
4918   just_machine_suffix = "";     /* I don't support specs, gives determinism */
4919 #else   /* FREEBSD_NATIVE */
4920   machine_suffix = concat (spec_machine, dir_separator_str,
4921                            spec_version, dir_separator_str, NULL_PTR);
4922   just_machine_suffix = concat (spec_machine, dir_separator_str, NULL_PTR);
4923 #endif  /* FREEBSD_NATIVE */
4924
4925   specs_file = find_a_file (&startfile_prefixes, "specs", R_OK);
4926   /* Read the specs file unless it is a default one.  */
4927   if (specs_file != 0 && strcmp (specs_file, "specs"))
4928     read_specs (specs_file, TRUE);
4929   else
4930     init_spec ();
4931
4932   /* We need to check standard_exec_prefix/just_machine_suffix/specs
4933      for any override of as, ld and libraries. */
4934   specs_file = (char *) alloca (strlen (standard_exec_prefix)
4935                                 + strlen (just_machine_suffix)
4936                                 + sizeof ("specs"));
4937
4938   strcpy (specs_file, standard_exec_prefix);
4939   strcat (specs_file, just_machine_suffix);
4940   strcat (specs_file, "specs");
4941   if (access (specs_file, R_OK) == 0)
4942     read_specs (specs_file, TRUE);
4943  
4944   /* If not cross-compiling, look for startfiles in the standard places.  */
4945   if (*cross_compile == '0')
4946     {
4947 #ifdef MD_EXEC_PREFIX
4948       add_prefix (&exec_prefixes, md_exec_prefix, "GCC", 0, 0, NULL_PTR);
4949       add_prefix (&startfile_prefixes, md_exec_prefix, "GCC", 0, 0, NULL_PTR);
4950 #endif
4951
4952 #ifdef MD_STARTFILE_PREFIX
4953       add_prefix (&startfile_prefixes, md_startfile_prefix, "GCC",
4954                   0, 0, NULL_PTR);
4955 #endif
4956
4957 #ifdef MD_STARTFILE_PREFIX_1
4958       add_prefix (&startfile_prefixes, md_startfile_prefix_1, "GCC",
4959                   0, 0, NULL_PTR);
4960 #endif
4961
4962       /* If standard_startfile_prefix is relative, base it on
4963          standard_exec_prefix.  This lets us move the installed tree
4964          as a unit.  If GCC_EXEC_PREFIX is defined, base
4965          standard_startfile_prefix on that as well.  */
4966       if (IS_DIR_SEPARATOR (*standard_startfile_prefix)
4967             || *standard_startfile_prefix == '$'
4968 #ifdef HAVE_DOS_BASED_FILESYSTEM
4969             /* Check for disk name on MS-DOS-based systems.  */
4970           || (standard_startfile_prefix[1] == ':'
4971               && (IS_DIR_SEPARATOR (standard_startfile_prefix[2])))
4972 #endif
4973           )
4974         add_prefix (&startfile_prefixes, standard_startfile_prefix, "BINUTILS",
4975                     0, 0, NULL_PTR);
4976       else
4977         {
4978           if (gcc_exec_prefix)
4979             add_prefix (&startfile_prefixes,
4980                         concat (gcc_exec_prefix, machine_suffix,
4981                                 standard_startfile_prefix, NULL_PTR),
4982                         NULL_PTR, 0, 0, NULL_PTR);
4983           add_prefix (&startfile_prefixes,
4984                       concat (standard_exec_prefix,
4985                               machine_suffix,
4986                               standard_startfile_prefix, NULL_PTR),
4987                       NULL_PTR, 0, 0, NULL_PTR);
4988         }                      
4989
4990 #ifndef FREEBSD_NATIVE
4991       add_prefix (&startfile_prefixes, standard_startfile_prefix_1,
4992                   "BINUTILS", 0, 0, NULL_PTR);
4993       add_prefix (&startfile_prefixes, standard_startfile_prefix_2,
4994                   "BINUTILS", 0, 0, NULL_PTR);
4995 #endif /* FREEBSD_NATIVE */
4996 #if 0 /* Can cause surprises, and one can use -B./ instead.  */
4997       add_prefix (&startfile_prefixes, "./", NULL_PTR, 0, 1, NULL_PTR);
4998 #endif
4999     }
5000   else
5001     {
5002       if (!IS_DIR_SEPARATOR (*standard_startfile_prefix) && gcc_exec_prefix)
5003         add_prefix (&startfile_prefixes,
5004                     concat (gcc_exec_prefix, machine_suffix,
5005                             standard_startfile_prefix, NULL_PTR),
5006                     "BINUTILS", 0, 0, NULL_PTR);
5007 #ifdef CROSS_STARTFILE_PREFIX
5008         add_prefix (&startfile_prefixes, CROSS_STARTFILE_PREFIX, "BINUTILS",
5009                     0, 0, NULL_PTR);
5010 #endif
5011     }
5012
5013   /* Process any user specified specs in the order given on the command
5014      line.  */
5015   for (uptr = user_specs_head; uptr; uptr = uptr->next)
5016     {
5017       char *filename = find_a_file (&startfile_prefixes, uptr->filename, R_OK);
5018       read_specs (filename ? filename : uptr->filename, FALSE);
5019     }
5020
5021   /* If we have a GCC_EXEC_PREFIX envvar, modify it for cpp's sake.  */
5022   if (gcc_exec_prefix)
5023     {
5024       char * temp = (char *) xmalloc (strlen (gcc_exec_prefix)
5025                                       + strlen (spec_version)
5026                                       + strlen (spec_machine) + 3);
5027       strcpy (temp, gcc_exec_prefix);
5028       strcat (temp, spec_machine);
5029       strcat (temp, dir_separator_str);
5030       strcat (temp, spec_version);
5031       strcat (temp, dir_separator_str);
5032       gcc_exec_prefix = temp;
5033     }
5034
5035   /* Now we have the specs.
5036      Set the `valid' bits for switches that match anything in any spec.  */
5037
5038   validate_all_switches ();
5039
5040   /* Now that we have the switches and the specs, set
5041      the subdirectory based on the options.  */
5042   set_multilib_dir ();
5043
5044   /* Warn about any switches that no pass was interested in.  */
5045
5046   for (i = 0; (int)i < n_switches; i++)
5047     if (! switches[i].validated)
5048       error ("unrecognized option `-%s'", switches[i].part1);
5049
5050   /* Obey some of the options.  */
5051
5052   if (print_search_dirs)
5053     {
5054       printf ("install: %s%s\n", standard_exec_prefix, machine_suffix);
5055       printf ("programs: %s\n", build_search_list (&exec_prefixes, "", 0));
5056       printf ("libraries: %s\n", build_search_list (&startfile_prefixes, "", 0));
5057       exit (0);
5058     }
5059
5060   if (print_file_name)
5061     {
5062       printf ("%s\n", find_file (print_file_name));
5063       exit (0);
5064     }
5065
5066   if (print_prog_name)
5067     {
5068       char *newname = find_a_file (&exec_prefixes, print_prog_name, X_OK);
5069       printf ("%s\n", (newname ? newname : print_prog_name));
5070       exit (0);
5071     }
5072
5073   if (print_multi_lib)
5074     {
5075       print_multilib_info ();
5076       exit (0);
5077     }
5078
5079   if (print_multi_directory)
5080     {
5081       if (multilib_dir == NULL)
5082         printf (".\n");
5083       else
5084         printf ("%s\n", multilib_dir);
5085       exit (0);
5086     }
5087
5088   if (print_help_list)
5089     {
5090       display_help ();
5091
5092       if (! verbose_flag)
5093         {
5094           printf ("\nFor bug reporting instructions, please see:\n");
5095           printf ("%s.\n", GCCBUGURL);
5096           
5097           exit (0);
5098         }
5099
5100       /* We do not exit here.  Instead we have created a fake input file
5101          called 'help-dummy' which needs to be compiled, and we pass this
5102          on the the various sub-processes, along with the --help switch.  */
5103     }
5104   
5105   if (verbose_flag)
5106     {
5107       int n;
5108
5109       /* compiler_version is truncated at the first space when initialized
5110          from version string, so truncate version_string at the first space
5111          before comparing.  */
5112       for (n = 0; version_string[n]; n++)
5113         if (version_string[n] == ' ')
5114           break;
5115
5116       if (! strncmp (version_string, compiler_version, n)
5117           && compiler_version[n] == 0)
5118         notice ("gcc version %s\n", version_string);
5119       else
5120         notice ("gcc driver version %s executing gcc version %s\n",
5121                 version_string, compiler_version);
5122
5123       if (n_infiles == 0)
5124         exit (0);
5125     }
5126
5127   if (n_infiles == added_libraries)
5128     fatal ("No input files specified");
5129
5130   /* Make a place to record the compiler output file names
5131      that correspond to the input files.  */
5132
5133   i = n_infiles;
5134   i += lang_specific_extra_outfiles;
5135   outfiles = (const char **) xmalloc (i * sizeof (char *));
5136   bzero ((char *) outfiles, i * sizeof (char *));
5137
5138   /* Record which files were specified explicitly as link input.  */
5139
5140   explicit_link_files = xmalloc (n_infiles);
5141   bzero (explicit_link_files, n_infiles);
5142
5143   for (i = 0; (int)i < n_infiles; i++)
5144     {
5145       register struct compiler *cp = 0;
5146       int this_file_error = 0;
5147
5148       /* Tell do_spec what to substitute for %i.  */
5149
5150       input_filename = infiles[i].name;
5151       input_filename_length = strlen (input_filename);
5152       input_file_number = i;
5153
5154       /* Use the same thing in %o, unless cp->spec says otherwise.  */
5155
5156       outfiles[i] = input_filename;
5157
5158       /* Figure out which compiler from the file's suffix.  */
5159
5160       cp = lookup_compiler (infiles[i].name, input_filename_length,
5161                             infiles[i].language);
5162
5163       if (cp)
5164         {
5165           /* Ok, we found an applicable compiler.  Run its spec.  */
5166           /* First say how much of input_filename to substitute for %b  */
5167           register const char *p;
5168           int len;
5169
5170           if (cp->spec[0][0] == '#')
5171             {
5172               error ("%s: %s compiler not installed on this system",
5173                      input_filename, &cp->spec[0][1]);
5174               this_file_error = 1;
5175             }
5176           else
5177             {
5178               input_basename = input_filename;
5179               for (p = input_filename; *p; p++)
5180                 if (IS_DIR_SEPARATOR (*p))
5181                   input_basename = p + 1;
5182
5183               /* Find a suffix starting with the last period,
5184                  and set basename_length to exclude that suffix.  */
5185               basename_length = strlen (input_basename);
5186               p = input_basename + basename_length;
5187               while (p != input_basename && *p != '.') --p;
5188               if (*p == '.' && p != input_basename)
5189                 {
5190                   basename_length = p - input_basename;
5191                   input_suffix = p + 1;
5192                 }
5193               else
5194                 input_suffix = "";
5195
5196               len = 0;
5197               for (j = 0; j < sizeof cp->spec / sizeof cp->spec[0]; j++)
5198                 if (cp->spec[j])
5199                   len += strlen (cp->spec[j]);
5200
5201               {
5202                 char *p1 = (char *) xmalloc (len + 1);
5203             
5204                 len = 0;
5205                 for (j = 0; j < sizeof cp->spec / sizeof cp->spec[0]; j++)
5206                   if (cp->spec[j])
5207                     {
5208                       strcpy (p1 + len, cp->spec[j]);
5209                       len += strlen (cp->spec[j]);
5210                     }
5211             
5212                 value = do_spec (p1);
5213                 free (p1);
5214               }
5215               if (value < 0)
5216                 this_file_error = 1;
5217             }
5218         }
5219
5220       /* If this file's name does not contain a recognized suffix,
5221          record it as explicit linker input.  */
5222
5223       else
5224         explicit_link_files[i] = 1;
5225
5226       /* Clear the delete-on-failure queue, deleting the files in it
5227          if this compilation failed.  */
5228
5229       if (this_file_error)
5230         {
5231           delete_failure_queue ();
5232           error_count++;
5233         }
5234       /* If this compilation succeeded, don't delete those files later.  */
5235       clear_failure_queue ();
5236     }
5237
5238   if (error_count == 0)
5239     {
5240       /* Make sure INPUT_FILE_NUMBER points to first available open
5241          slot.  */
5242       input_file_number = n_infiles;
5243       if (lang_specific_pre_link ())
5244         error_count++;
5245     }
5246
5247   /* Run ld to link all the compiler output files.  */
5248
5249   if (error_count == 0)
5250     {
5251       int tmp = execution_count;
5252
5253       /* We'll use ld if we can't find collect2. */
5254       if (! strcmp (linker_name_spec, "collect2"))
5255         {
5256           char *s = find_a_file (&exec_prefixes, "collect2", X_OK);
5257           if (s == NULL)
5258             linker_name_spec = "ld";
5259         }
5260       /* Rebuild the COMPILER_PATH and LIBRARY_PATH environment variables
5261          for collect.  */
5262       putenv_from_prefixes (&exec_prefixes, "COMPILER_PATH=");
5263       putenv_from_prefixes (&startfile_prefixes, "LIBRARY_PATH=");
5264
5265       value = do_spec (link_command_spec);
5266       if (value < 0)
5267         error_count = 1;
5268       linker_was_run = (tmp != execution_count);
5269     }
5270
5271   /* Warn if a -B option was specified but the prefix was never used.  */
5272   unused_prefix_warnings (&exec_prefixes);
5273   unused_prefix_warnings (&startfile_prefixes);
5274
5275   /* If options said don't run linker,
5276      complain about input files to be given to the linker.  */
5277
5278   if (! linker_was_run && error_count == 0)
5279     for (i = 0; (int)i < n_infiles; i++)
5280       if (explicit_link_files[i])
5281         error ("%s: linker input file unused since linking not done",
5282                outfiles[i]);
5283
5284   /* Delete some or all of the temporary files we made.  */
5285
5286   if (error_count)
5287     delete_failure_queue ();
5288   delete_temp_files ();
5289
5290   if (print_help_list)
5291     {
5292       printf ("\nFor bug reporting instructions, please see:\n");
5293       printf ("%s\n", GCCBUGURL);
5294     }
5295   
5296   exit (error_count > 0 ? (signal_count ? 2 : 1) : 0);
5297   /* NOTREACHED */
5298   return 0;
5299 }
5300
5301 /* Find the proper compilation spec for the file name NAME,
5302    whose length is LENGTH.  LANGUAGE is the specified language,
5303    or 0 if this file is to be passed to the linker.  */
5304
5305 static struct compiler *
5306 lookup_compiler (name, length, language)
5307      const char *name;
5308      size_t length;
5309      const char *language;
5310 {
5311   struct compiler *cp;
5312
5313   /* If this was specified by the user to be a linker input, indicate that. */
5314   if (language != 0 && language[0] == '*')
5315     return 0;
5316
5317   /* Otherwise, look for the language, if one is spec'd.  */
5318   if (language != 0)
5319     {
5320       for (cp = compilers + n_compilers - 1; cp >= compilers; cp--)
5321         if (cp->suffix[0] == '@' && !strcmp (cp->suffix + 1, language))
5322           return cp;
5323
5324       error ("language %s not recognized", language);
5325       return 0;
5326     }
5327
5328   /* Look for a suffix.  */
5329   for (cp = compilers + n_compilers - 1; cp >= compilers; cp--)
5330     {
5331       if (/* The suffix `-' matches only the file name `-'.  */
5332           (!strcmp (cp->suffix, "-") && !strcmp (name, "-"))
5333           || (strlen (cp->suffix) < length
5334               /* See if the suffix matches the end of NAME.  */
5335 #ifdef OS2
5336               && ((!strcmp (cp->suffix,
5337                            name + length - strlen (cp->suffix))
5338                    || !strpbrk (cp->suffix, "ABCDEFGHIJKLMNOPQRSTUVWXYZ"))
5339                   && !strcasecmp (cp->suffix,
5340                                   name + length - strlen (cp->suffix)))
5341 #else
5342               && !strcmp (cp->suffix,
5343                           name + length - strlen (cp->suffix))
5344 #endif
5345          ))
5346         {
5347           if (cp->spec[0][0] == '@')
5348             {
5349               struct compiler *new;
5350
5351               /* An alias entry maps a suffix to a language.
5352                  Search for the language; pass 0 for NAME and LENGTH
5353                  to avoid infinite recursion if language not found.
5354                  Construct the new compiler spec.  */
5355               language = cp->spec[0] + 1;
5356               new = (struct compiler *) xmalloc (sizeof (struct compiler));
5357               new->suffix = cp->suffix;
5358               bcopy ((char *) lookup_compiler (NULL_PTR, 0, language)->spec,
5359                      (char *) new->spec, sizeof new->spec);
5360               return new;
5361             }
5362
5363           /* A non-alias entry: return it.  */
5364           return cp;
5365         }
5366     }
5367
5368   return 0;
5369 }
5370 \f
5371 PTR
5372 xmalloc (size)
5373   size_t size;
5374 {
5375   register PTR value = (PTR) malloc (size);
5376   if (value == 0)
5377     fatal ("virtual memory exhausted");
5378   return value;
5379 }
5380
5381 PTR
5382 xrealloc (old, size)
5383   PTR old;
5384   size_t size;
5385 {
5386   register PTR ptr;
5387   if (old)
5388     ptr = (PTR) realloc (old, size);
5389   else
5390     ptr = (PTR) malloc (size);
5391   if (ptr == 0)
5392     fatal ("virtual memory exhausted");
5393   return ptr;
5394 }
5395
5396 static char *
5397 save_string (s, len)
5398   const char *s;
5399   int len;
5400 {
5401   register char *result = xmalloc (len + 1);
5402
5403   bcopy (s, result, len);
5404   result[len] = 0;
5405   return result;
5406 }
5407
5408 static void
5409 pfatal_with_name (name)
5410      const char *name;
5411 {
5412   perror_with_name (name);
5413   delete_temp_files ();
5414   exit (1);
5415 }
5416
5417 static void
5418 perror_with_name (name)
5419      const char *name;
5420 {
5421   error ("%s: %s", name, xstrerror (errno));
5422 }
5423
5424 static void
5425 pfatal_pexecute (errmsg_fmt, errmsg_arg)
5426      const char *errmsg_fmt;
5427      const char *errmsg_arg;
5428 {
5429   if (errmsg_arg)
5430     {
5431       int save_errno = errno;
5432
5433       /* Space for trailing '\0' is in %s.  */
5434       char *msg = xmalloc (strlen (errmsg_fmt) + strlen (errmsg_arg));
5435       sprintf (msg, errmsg_fmt, errmsg_arg);
5436       errmsg_fmt = msg;
5437
5438       errno = save_errno;
5439     }
5440
5441   pfatal_with_name (errmsg_fmt);
5442 }
5443
5444 /* More 'friendly' abort that prints the line and file.
5445    config.h can #define abort fancy_abort if you like that sort of thing.  */
5446
5447 void
5448 fancy_abort ()
5449 {
5450   fatal ("Internal gcc abort.");
5451 }
5452 \f
5453 /* Output an error message and exit */
5454
5455 void
5456 fatal VPROTO((const char *msgid, ...))
5457 {
5458 #ifndef ANSI_PROTOTYPES
5459   const char *msgid;
5460 #endif
5461   va_list ap;
5462
5463   VA_START (ap, msgid);
5464
5465 #ifndef ANSI_PROTOTYPES
5466   msgid = va_arg (ap, const char *);
5467 #endif
5468
5469   fprintf (stderr, "%s: ", programname);
5470   vfprintf (stderr, _(msgid), ap);
5471   va_end (ap);
5472   fprintf (stderr, "\n");
5473   delete_temp_files ();
5474   exit (1);
5475 }
5476
5477 static void
5478 error VPROTO((const char *msgid, ...))
5479 {
5480 #ifndef ANSI_PROTOTYPES
5481   const char *msgid;
5482 #endif
5483   va_list ap;
5484
5485   VA_START (ap, msgid);
5486
5487 #ifndef ANSI_PROTOTYPES
5488   msgid = va_arg (ap, const char *);
5489 #endif
5490
5491   fprintf (stderr, "%s: ", programname);
5492   vfprintf (stderr, _(msgid), ap);
5493   va_end (ap);
5494
5495   fprintf (stderr, "\n");
5496 }
5497
5498 static void
5499 notice VPROTO((const char *msgid, ...))
5500 {
5501 #ifndef ANSI_PROTOTYPES
5502   const char *msgid;
5503 #endif
5504   va_list ap;
5505
5506   VA_START (ap, msgid);
5507
5508 #ifndef ANSI_PROTOTYPES
5509   msgid = va_arg (ap, const char *);
5510 #endif
5511
5512   vfprintf (stderr, _(msgid), ap);
5513   va_end (ap);
5514 }
5515
5516 \f
5517 static void
5518 validate_all_switches ()
5519 {
5520   struct compiler *comp;
5521   register const char *p;
5522   register char c;
5523   struct spec_list *spec;
5524
5525   for (comp = compilers; comp->spec[0]; comp++)
5526     {
5527       size_t i;
5528       for (i = 0; i < sizeof comp->spec / sizeof comp->spec[0] && comp->spec[i]; i++)
5529         {
5530           p = comp->spec[i];
5531           while ((c = *p++))
5532             if (c == '%' && *p == '{')
5533               /* We have a switch spec.  */
5534               validate_switches (p + 1);
5535         }
5536     }
5537
5538   /* look through the linked list of specs read from the specs file */
5539   for (spec = specs; spec ; spec = spec->next)
5540     {
5541       p = *(spec->ptr_spec);
5542       while ((c = *p++))
5543         if (c == '%' && *p == '{')
5544           /* We have a switch spec.  */
5545           validate_switches (p + 1);
5546     }
5547
5548   p = link_command_spec;
5549   while ((c = *p++))
5550     if (c == '%' && *p == '{')
5551       /* We have a switch spec.  */
5552       validate_switches (p + 1);
5553 }
5554
5555 /* Look at the switch-name that comes after START
5556    and mark as valid all supplied switches that match it.  */
5557
5558 static void
5559 validate_switches (start)
5560      const char *start;
5561 {
5562   register const char *p = start;
5563   const char *filter;
5564   register int i;
5565   int suffix = 0;
5566
5567   if (*p == '|')
5568     ++p;
5569
5570   if (*p == '!')
5571     ++p;
5572
5573   if (*p == '.')
5574     suffix = 1, ++p;
5575
5576   filter = p;
5577   while (*p != ':' && *p != '}') p++;
5578
5579   if (suffix)
5580     ;
5581   else if (p[-1] == '*')
5582     {
5583       /* Mark all matching switches as valid.  */
5584       --p;
5585       for (i = 0; i < n_switches; i++)
5586         if (!strncmp (switches[i].part1, filter, p - filter))
5587           switches[i].validated = 1;
5588     }
5589   else
5590     {
5591       /* Mark an exact matching switch as valid.  */
5592       for (i = 0; i < n_switches; i++)
5593         {
5594           if (!strncmp (switches[i].part1, filter, p - filter)
5595               && switches[i].part1[p - filter] == 0)
5596             switches[i].validated = 1;
5597         }
5598     }
5599 }
5600 \f
5601 /* Check whether a particular argument was used.  The first time we
5602    canonicalize the switches to keep only the ones we care about.  */
5603
5604 static int
5605 used_arg (p, len)
5606      const char *p;
5607      int len;
5608 {
5609   struct mswitchstr {
5610     char *str;
5611     char *replace;
5612     int len;
5613     int rep_len;
5614   };
5615
5616   static struct mswitchstr *mswitches;
5617   static int n_mswitches;
5618   int i, j;
5619
5620   if (!mswitches)
5621     {
5622       struct mswitchstr *matches;
5623       char *q;
5624       int cnt = 0;
5625
5626       /* Break multilib_matches into the component strings of string and replacement
5627          string */
5628       for (q = multilib_matches; *q != '\0'; q++)
5629         if (*q == ';')
5630           cnt++;
5631
5632       matches = (struct mswitchstr *) alloca ((sizeof (struct mswitchstr)) * cnt);
5633       i = 0;
5634       q = multilib_matches;
5635       while (*q != '\0')
5636         {
5637           matches[i].str = q;
5638           while (*q != ' ')
5639             {
5640               if (*q == '\0')
5641                 abort ();
5642               q++;
5643             }
5644           *q = '\0';
5645           matches[i].len = q - matches[i].str;
5646
5647           matches[i].replace = ++q;
5648           while (*q != ';' && *q != '\0')
5649             {
5650               if (*q == ' ')
5651                 abort ();
5652               q++;
5653             }
5654           matches[i].rep_len = q - matches[i].replace;
5655           i++;
5656           if (*q == ';')
5657             *q++ = '\0';
5658           else
5659             break;
5660         }
5661
5662       /* Now build a list of the replacement string for switches that we care
5663          about.  Make sure we allocate at least one entry.  This prevents
5664          xmalloc from calling fatal, and prevents us from re-executing this
5665          block of code.  */
5666       mswitches
5667         = (struct mswitchstr *) xmalloc ((sizeof (struct mswitchstr))
5668                                          * (n_switches ? n_switches : 1));
5669       for (i = 0; i < n_switches; i++)
5670         {
5671           int xlen = strlen (switches[i].part1);
5672           for (j = 0; j < cnt; j++)
5673             if (xlen == matches[j].len && ! strcmp (switches[i].part1, matches[j].str))
5674               {
5675                 mswitches[n_mswitches].str = matches[j].replace;
5676                 mswitches[n_mswitches].len = matches[j].rep_len;
5677                 mswitches[n_mswitches].replace = (char *)0;
5678                 mswitches[n_mswitches].rep_len = 0;
5679                 n_mswitches++;
5680                 break;
5681               }
5682         }
5683     }
5684
5685   for (i = 0; i < n_mswitches; i++)
5686     if (len == mswitches[i].len && ! strncmp (p, mswitches[i].str, len))
5687       return 1;
5688
5689   return 0;
5690 }
5691
5692 static int
5693 default_arg (p, len)
5694      const char *p;
5695      int len;
5696 {
5697   char *start, *end;
5698
5699   for (start = multilib_defaults; *start != '\0'; start = end+1)
5700     {
5701       while (*start == ' ' || *start == '\t')
5702         start++;
5703
5704       if (*start == '\0')
5705         break;
5706
5707       for (end = start+1; *end != ' ' && *end != '\t' && *end != '\0'; end++)
5708         ;
5709
5710       if ((end - start) == len && strncmp (p, start, len) == 0)
5711         return 1;
5712
5713       if (*end == '\0')
5714         break;
5715     }
5716
5717   return 0;
5718 }
5719
5720 /* Work out the subdirectory to use based on the
5721    options.  The format of multilib_select is a list of elements.
5722    Each element is a subdirectory name followed by a list of options
5723    followed by a semicolon.  gcc will consider each line in turn.  If
5724    none of the options beginning with an exclamation point are
5725    present, and all of the other options are present, that
5726    subdirectory will be used.  */
5727
5728 static void
5729 set_multilib_dir ()
5730 {
5731   char *p = multilib_select;
5732   int this_path_len;
5733   char *this_path, *this_arg;
5734   int not_arg;
5735   int ok;
5736
5737   while (*p != '\0')
5738     {
5739       /* Ignore newlines.  */
5740       if (*p == '\n')
5741         {
5742           ++p;
5743           continue;
5744         }
5745
5746       /* Get the initial path.  */
5747       this_path = p;
5748       while (*p != ' ')
5749         {
5750           if (*p == '\0')
5751             abort ();
5752           ++p;
5753         }
5754       this_path_len = p - this_path;
5755
5756       /* Check the arguments.  */
5757       ok = 1;
5758       ++p;
5759       while (*p != ';')
5760         {
5761           if (*p == '\0')
5762             abort ();
5763
5764           if (! ok)
5765             {
5766               ++p;
5767               continue;
5768             }
5769
5770           this_arg = p;
5771           while (*p != ' ' && *p != ';')
5772             {
5773               if (*p == '\0')
5774                 abort ();
5775               ++p;
5776             }
5777
5778           if (*this_arg != '!')
5779             not_arg = 0;
5780           else
5781             {
5782               not_arg = 1;
5783               ++this_arg;
5784             }
5785
5786           /* If this is a default argument, we can just ignore it.
5787              This is true even if this_arg begins with '!'.  Beginning
5788              with '!' does not mean that this argument is necessarily
5789              inappropriate for this library: it merely means that
5790              there is a more specific library which uses this
5791              argument.  If this argument is a default, we need not
5792              consider that more specific library.  */
5793           if (! default_arg (this_arg, p - this_arg))
5794             {
5795               ok = used_arg (this_arg, p - this_arg);
5796               if (not_arg)
5797                 ok = ! ok;
5798             }
5799
5800           if (*p == ' ')
5801             ++p;
5802         }
5803
5804       if (ok)
5805         {
5806           if (this_path_len != 1
5807               || this_path[0] != '.')
5808             {
5809               char * new_multilib_dir = xmalloc (this_path_len + 1);
5810               strncpy (new_multilib_dir, this_path, this_path_len);
5811               new_multilib_dir[this_path_len] = '\0';
5812               multilib_dir = new_multilib_dir;
5813             }
5814           break;
5815         }
5816
5817       ++p;
5818     }      
5819 }
5820
5821 /* Print out the multiple library subdirectory selection
5822    information.  This prints out a series of lines.  Each line looks
5823    like SUBDIRECTORY;@OPTION@OPTION, with as many options as is
5824    required.  Only the desired options are printed out, the negative
5825    matches.  The options are print without a leading dash.  There are
5826    no spaces to make it easy to use the information in the shell.
5827    Each subdirectory is printed only once.  This assumes the ordering
5828    generated by the genmultilib script.  */
5829
5830 static void
5831 print_multilib_info ()
5832 {
5833   char *p = multilib_select;
5834   char *last_path = 0, *this_path;
5835   int skip;
5836   int last_path_len = 0;
5837
5838   while (*p != '\0')
5839     {
5840       /* Ignore newlines.  */
5841       if (*p == '\n')
5842         {
5843           ++p;
5844           continue;
5845         }
5846
5847       /* Get the initial path.  */
5848       this_path = p;
5849       while (*p != ' ')
5850         {
5851           if (*p == '\0')
5852             abort ();
5853           ++p;
5854         }
5855
5856       /* If this is a duplicate, skip it.  */
5857       skip = (last_path != 0 && p - this_path == last_path_len
5858               && ! strncmp (last_path, this_path, last_path_len));
5859
5860       last_path = this_path;
5861       last_path_len = p - this_path;
5862
5863       /* If this directory requires any default arguments, we can skip
5864          it.  We will already have printed a directory identical to
5865          this one which does not require that default argument.  */
5866       if (! skip)
5867         {
5868           char *q;
5869
5870           q = p + 1;
5871           while (*q != ';')
5872             {
5873               char *arg;
5874
5875               if (*q == '\0')
5876                 abort ();
5877
5878               if (*q == '!')
5879                 arg = NULL;
5880               else
5881                 arg = q;
5882
5883               while (*q != ' ' && *q != ';')
5884                 {
5885                   if (*q == '\0')
5886                     abort ();
5887                   ++q;
5888                 }
5889
5890               if (arg != NULL
5891                   && default_arg (arg, q - arg))
5892                 {
5893                   skip = 1;
5894                   break;
5895                 }
5896
5897               if (*q == ' ')
5898                 ++q;
5899             }
5900         }
5901
5902       if (! skip)
5903         {
5904           char *p1;
5905
5906           for (p1 = last_path; p1 < p; p1++)
5907             putchar (*p1);
5908           putchar (';');
5909         }
5910
5911       ++p;
5912       while (*p != ';')
5913         {
5914           int use_arg;
5915
5916           if (*p == '\0')
5917             abort ();
5918
5919           if (skip)
5920             {
5921               ++p;
5922               continue;
5923             }
5924
5925           use_arg = *p != '!';
5926
5927           if (use_arg)
5928             putchar ('@');
5929
5930           while (*p != ' ' && *p != ';')
5931             {
5932               if (*p == '\0')
5933                 abort ();
5934               if (use_arg)
5935                 putchar (*p);
5936               ++p;
5937             }
5938
5939           if (*p == ' ')
5940             ++p;
5941         }
5942
5943       if (! skip)
5944         {
5945           /* If there are extra options, print them now */
5946           if (multilib_extra && *multilib_extra)
5947             {
5948               int print_at = TRUE;
5949               char *q;
5950
5951               for (q = multilib_extra; *q != '\0'; q++)
5952                 {
5953                   if (*q == ' ')
5954                     print_at = TRUE;
5955                   else
5956                     {
5957                       if (print_at)
5958                         putchar ('@');
5959                       putchar (*q);
5960                       print_at = FALSE;
5961                     }
5962                 }
5963             }
5964           putchar ('\n');
5965         }
5966
5967       ++p;
5968     }
5969 }