OSDN Git Service

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