OSDN Git Service

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