OSDN Git Service

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