OSDN Git Service

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