OSDN Git Service

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