OSDN Git Service

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