OSDN Git Service

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