OSDN Git Service

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