OSDN Git Service

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