OSDN Git Service

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