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