OSDN Git Service

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