OSDN Git Service

entered into RCS
[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   /* 2 means try both machine_suffix and just_machine_suffix.  */
817   int *used_flag_ptr;         /* 1 if a file was found with this prefix.  */
818 };
819
820 struct path_prefix
821 {
822   struct prefix_list *plist;  /* List of prefixes to try */
823   int max_len;                /* Max length of a prefix in PLIST */
824   char *name;                 /* Name of this list (used in config stuff) */
825 };
826
827 /* List of prefixes to try when looking for executables. */
828
829 static struct path_prefix exec_prefix = { 0, 0, "exec" };
830
831 /* List of prefixes to try when looking for startup (crt0) files. */
832
833 static struct path_prefix startfile_prefix = { 0, 0, "startfile" };
834
835 /* List of prefixes to try when looking for libraries. */
836
837 static struct path_prefix library_prefix = { 0, 0, "libraryfile" };
838
839 /* Suffix to attach to directories searched for commands.
840    This looks like `MACHINE/VERSION/'.  */
841
842 static char *machine_suffix = 0;
843
844 /* Suffix to attach to directories searched for commands.
845    This is just `MACHINE/'.  */
846
847 static char *just_machine_suffix = 0;
848
849 /* Adjusted value of GCC_EXEC_PREFIX envvar.  */
850
851 static char *gcc_exec_prefix;
852
853 /* Default prefixes to attach to command names.  */
854
855 #ifdef CROSS_COMPILE  /* Don't use these prefixes for a cross compiler.  */
856 #undef MD_EXEC_PREFIX
857 #undef MD_STARTFILE_PREFIX
858 #undef MD_STARTFILE_PREFIX_1
859 #endif
860
861 #ifndef STANDARD_EXEC_PREFIX
862 #define STANDARD_EXEC_PREFIX "/usr/local/lib/gcc-lib/"
863 #endif /* !defined STANDARD_EXEC_PREFIX */
864
865 static char *standard_exec_prefix = STANDARD_EXEC_PREFIX;
866 static char *standard_exec_prefix_1 = "/usr/lib/gcc/";
867 #ifdef MD_EXEC_PREFIX
868 static char *md_exec_prefix = MD_EXEC_PREFIX;
869 #endif
870
871 #ifndef STANDARD_STARTFILE_PREFIX
872 #define STANDARD_STARTFILE_PREFIX "/usr/local/lib/"
873 #endif /* !defined STANDARD_STARTFILE_PREFIX */
874
875 #ifdef MD_STARTFILE_PREFIX
876 static char *md_startfile_prefix = MD_STARTFILE_PREFIX;
877 #endif
878 #ifdef MD_STARTFILE_PREFIX_1
879 static char *md_startfile_prefix_1 = MD_STARTFILE_PREFIX_1;
880 #endif
881 static char *standard_startfile_prefix = STANDARD_STARTFILE_PREFIX;
882 static char *standard_startfile_prefix_1 = "/lib/";
883 static char *standard_startfile_prefix_2 = "/usr/lib/";
884
885 /* Clear out the vector of arguments (after a command is executed).  */
886
887 static void
888 clear_args ()
889 {
890   argbuf_index = 0;
891 }
892
893 /* Add one argument to the vector at the end.
894    This is done when a space is seen or at the end of the line.
895    If DELETE_ALWAYS is nonzero, the arg is a filename
896     and the file should be deleted eventually.
897    If DELETE_FAILURE is nonzero, the arg is a filename
898     and the file should be deleted if this compilation fails.  */
899
900 static void
901 store_arg (arg, delete_always, delete_failure)
902      char *arg;
903      int delete_always, delete_failure;
904 {
905   if (argbuf_index + 1 == argbuf_length)
906     {
907       argbuf = (char **) xrealloc (argbuf, (argbuf_length *= 2) * sizeof (char *));
908     }
909
910   argbuf[argbuf_index++] = arg;
911   argbuf[argbuf_index] = 0;
912
913   if (delete_always || delete_failure)
914     record_temp_file (arg, delete_always, delete_failure);
915 }
916 \f
917 /* Record the names of temporary files we tell compilers to write,
918    and delete them at the end of the run.  */
919
920 /* This is the common prefix we use to make temp file names.
921    It is chosen once for each run of this program.
922    It is substituted into a spec by %g.
923    Thus, all temp file names contain this prefix.
924    In practice, all temp file names start with this prefix.
925
926    This prefix comes from the envvar TMPDIR if it is defined;
927    otherwise, from the P_tmpdir macro if that is defined;
928    otherwise, in /usr/tmp or /tmp.  */
929
930 static char *temp_filename;
931
932 /* Length of the prefix.  */
933
934 static int temp_filename_length;
935
936 /* Define the list of temporary files to delete.  */
937
938 struct temp_file
939 {
940   char *name;
941   struct temp_file *next;
942 };
943
944 /* Queue of files to delete on success or failure of compilation.  */
945 static struct temp_file *always_delete_queue;
946 /* Queue of files to delete on failure of compilation.  */
947 static struct temp_file *failure_delete_queue;
948
949 /* Record FILENAME as a file to be deleted automatically.
950    ALWAYS_DELETE nonzero means delete it if all compilation succeeds;
951    otherwise delete it in any case.
952    FAIL_DELETE nonzero means delete it if a compilation step fails;
953    otherwise delete it in any case.  */
954
955 static void
956 record_temp_file (filename, always_delete, fail_delete)
957      char *filename;
958      int always_delete;
959      int fail_delete;
960 {
961   register char *name;
962   name = xmalloc (strlen (filename) + 1);
963   strcpy (name, filename);
964
965   if (always_delete)
966     {
967       register struct temp_file *temp;
968       for (temp = always_delete_queue; temp; temp = temp->next)
969         if (! strcmp (name, temp->name))
970           goto already1;
971       temp = (struct temp_file *) xmalloc (sizeof (struct temp_file));
972       temp->next = always_delete_queue;
973       temp->name = name;
974       always_delete_queue = temp;
975     already1:;
976     }
977
978   if (fail_delete)
979     {
980       register struct temp_file *temp;
981       for (temp = failure_delete_queue; temp; temp = temp->next)
982         if (! strcmp (name, temp->name))
983           goto already2;
984       temp = (struct temp_file *) xmalloc (sizeof (struct temp_file));
985       temp->next = failure_delete_queue;
986       temp->name = name;
987       failure_delete_queue = temp;
988     already2:;
989     }
990 }
991
992 /* Delete all the temporary files whose names we previously recorded.  */
993
994 static void
995 delete_temp_files ()
996 {
997   register struct temp_file *temp;
998
999   for (temp = always_delete_queue; temp; temp = temp->next)
1000     {
1001 #ifdef DEBUG
1002       int i;
1003       printf ("Delete %s? (y or n) ", temp->name);
1004       fflush (stdout);
1005       i = getchar ();
1006       if (i != '\n')
1007         while (getchar () != '\n') ;
1008       if (i == 'y' || i == 'Y')
1009 #endif /* DEBUG */
1010         {
1011           struct stat st;
1012           if (stat (temp->name, &st) >= 0)
1013             {
1014               /* Delete only ordinary files.  */
1015               if (S_ISREG (st.st_mode))
1016                 if (unlink (temp->name) < 0)
1017                   if (verbose_flag)
1018                     perror_with_name (temp->name);
1019             }
1020         }
1021     }
1022
1023   always_delete_queue = 0;
1024 }
1025
1026 /* Delete all the files to be deleted on error.  */
1027
1028 static void
1029 delete_failure_queue ()
1030 {
1031   register struct temp_file *temp;
1032
1033   for (temp = failure_delete_queue; temp; temp = temp->next)
1034     {
1035 #ifdef DEBUG
1036       int i;
1037       printf ("Delete %s? (y or n) ", temp->name);
1038       fflush (stdout);
1039       i = getchar ();
1040       if (i != '\n')
1041         while (getchar () != '\n') ;
1042       if (i == 'y' || i == 'Y')
1043 #endif /* DEBUG */
1044         {
1045           if (unlink (temp->name) < 0)
1046             if (verbose_flag)
1047               perror_with_name (temp->name);
1048         }
1049     }
1050 }
1051
1052 static void
1053 clear_failure_queue ()
1054 {
1055   failure_delete_queue = 0;
1056 }
1057
1058 /* Compute a string to use as the base of all temporary file names.
1059    It is substituted for %g.  */
1060
1061 static void
1062 choose_temp_base ()
1063 {
1064   char *base = getenv ("TMPDIR");
1065   int len;
1066
1067   if (base == (char *)0)
1068     {
1069 #ifdef P_tmpdir
1070       if (access (P_tmpdir, R_OK | W_OK) == 0)
1071         base = P_tmpdir;
1072 #endif
1073       if (base == (char *)0)
1074         {
1075           if (access ("/usr/tmp", R_OK | W_OK) == 0)
1076             base = "/usr/tmp/";
1077           else
1078             base = "/tmp/";
1079         }
1080     }
1081
1082   len = strlen (base);
1083   temp_filename = xmalloc (len + sizeof("/ccXXXXXX"));
1084   strcpy (temp_filename, base);
1085   if (len > 0 && temp_filename[len-1] != '/')
1086     temp_filename[len++] = '/';
1087   strcpy (temp_filename + len, "ccXXXXXX");
1088
1089   mktemp (temp_filename);
1090   temp_filename_length = strlen (temp_filename);
1091 }
1092 \f
1093
1094 /* Routine to add variables to the environment.  We do this to pass
1095    the pathname of the gcc driver, and the directories search to the
1096    collect2 program, which is being run as ld.  This way, we can be
1097    sure of executing the right compiler when collect2 wants to build
1098    constructors and destructors.  Since the environment variables we
1099    use come from an obstack, we don't have to worry about allocating
1100    space for them.  */
1101
1102 #ifndef HAVE_PUTENV
1103
1104 putenv (str)
1105      char *str;
1106 {
1107 #ifndef VMS                     /* nor about VMS */
1108
1109   extern char **environ;
1110   char **old_environ = environ;
1111   char **envp;
1112   int num_envs = 0;
1113   int name_len = 1;
1114   int str_len = strlen (str);
1115   char *p = str;
1116   int ch;
1117
1118   while ((ch = *p++) != '\0' && ch != '=')
1119     name_len++;
1120
1121   if (!ch)
1122     abort ();
1123
1124   /* Search for replacing an existing environment variable, and
1125      count the number of total environment variables.  */
1126   for (envp = old_environ; *envp; envp++)
1127     {
1128       num_envs++;
1129       if (!strncmp (str, *envp, name_len))
1130         {
1131           *envp = str;
1132           return;
1133         }
1134     }
1135
1136   /* Add a new environment variable */
1137   environ = (char **) xmalloc (sizeof (char *) * (num_envs+2));
1138   *environ = str;
1139   bcopy (old_environ, environ+1, sizeof (char *) * (num_envs+1));
1140
1141 #endif  /* VMS */
1142 }
1143
1144 #endif  /* HAVE_PUTENV */
1145
1146 \f
1147 /* Rebuild the COMPILER_PATH and LIBRARY_PATH environment variables for collect.  */
1148
1149 static void
1150 putenv_from_prefixes (paths, env_var)
1151      struct path_prefix *paths;
1152      char *env_var;
1153 {
1154   int suffix_len = (machine_suffix) ? strlen (machine_suffix) : 0;
1155   int first_time = TRUE;
1156   struct prefix_list *pprefix;
1157
1158   obstack_grow (&collect_obstack, env_var, strlen (env_var));
1159
1160   for (pprefix = paths->plist; pprefix != 0; pprefix = pprefix->next)
1161     {
1162       int len = strlen (pprefix->prefix);
1163
1164       if (machine_suffix)
1165         {
1166           if (!first_time)
1167             obstack_grow (&collect_obstack, ":", 1);
1168             
1169           first_time = FALSE;
1170           obstack_grow (&collect_obstack, pprefix->prefix, len);
1171           obstack_grow (&collect_obstack, machine_suffix, suffix_len);
1172         }
1173
1174       if (just_machine_suffix && pprefix->require_machine_suffix == 2)
1175         {
1176           if (!first_time)
1177             obstack_grow (&collect_obstack, ":", 1);
1178             
1179           first_time = FALSE;
1180           obstack_grow (&collect_obstack, pprefix->prefix, len);
1181           obstack_grow (&collect_obstack, machine_suffix, suffix_len);
1182         }
1183
1184       if (!pprefix->require_machine_suffix)
1185         {
1186           if (!first_time)
1187             obstack_grow (&collect_obstack, ":", 1);
1188
1189           first_time = FALSE;
1190           obstack_grow (&collect_obstack, pprefix->prefix, len);
1191         }
1192     }
1193   obstack_grow (&collect_obstack, "\0", 1);
1194   putenv (obstack_finish (&collect_obstack));
1195 }
1196
1197 \f
1198 /* Search for NAME using the prefix list PREFIXES.  MODE is passed to
1199    access to check permissions.
1200    Return 0 if not found, otherwise return its name, allocated with malloc. */
1201
1202 static char *
1203 find_a_file (pprefix, name, mode)
1204      struct path_prefix *pprefix;
1205      char *name;
1206      int mode;
1207 {
1208   char *temp;
1209   char *file_suffix = ((mode & X_OK) != 0 ? EXECUTABLE_SUFFIX : "");
1210   struct prefix_list *pl;
1211   int len = pprefix->max_len + strlen (name) + strlen (file_suffix) + 1;
1212
1213   if (machine_suffix)
1214     len += strlen (machine_suffix);
1215
1216   temp = xmalloc (len);
1217
1218   /* Determine the filename to execute (special case for absolute paths).  */
1219
1220   if (*name == '/')
1221     {
1222       if (access (name, mode))
1223         {
1224           strcpy (temp, name);
1225           return temp;
1226         }
1227     }
1228   else
1229     for (pl = pprefix->plist; pl; pl = pl->next)
1230       {
1231         if (machine_suffix)
1232           {
1233             strcpy (temp, pl->prefix);
1234             strcat (temp, machine_suffix);
1235             strcat (temp, name);
1236             if (access (temp, mode) == 0)
1237               {
1238                 if (pl->used_flag_ptr != 0)
1239                   *pl->used_flag_ptr = 1;
1240                 return temp;
1241               }
1242             /* Some systems have a suffix for executable files.
1243                So try appending that.  */
1244             if (file_suffix[0] != 0)
1245               {
1246                 strcat (temp, file_suffix);
1247                 if (access (temp, mode) == 0)
1248                   {
1249                     if (pl->used_flag_ptr != 0)
1250                       *pl->used_flag_ptr = 1;
1251                     return temp;
1252                   }
1253               }
1254           }
1255         /* Certain prefixes are tried with just the machine type,
1256            not the version.  This is used for finding as, ld, etc.  */
1257         if (just_machine_suffix && pl->require_machine_suffix == 2)
1258           {
1259             strcpy (temp, pl->prefix);
1260             strcat (temp, just_machine_suffix);
1261             strcat (temp, name);
1262             if (access (temp, mode) == 0)
1263               {
1264                 if (pl->used_flag_ptr != 0)
1265                   *pl->used_flag_ptr = 1;
1266                 return temp;
1267               }
1268             /* Some systems have a suffix for executable files.
1269                So try appending that.  */
1270             if (file_suffix[0] != 0)
1271               {
1272                 strcat (temp, file_suffix);
1273                 if (access (temp, mode) == 0)
1274                   {
1275                     if (pl->used_flag_ptr != 0)
1276                       *pl->used_flag_ptr = 1;
1277                     return temp;
1278                   }
1279               }
1280           }
1281         /* Certain prefixes can't be used without the machine suffix
1282            when the machine or version is explicitly specified.  */
1283         if (!pl->require_machine_suffix)
1284           {
1285             strcpy (temp, pl->prefix);
1286             strcat (temp, name);
1287             if (access (temp, mode) == 0)
1288               {
1289                 if (pl->used_flag_ptr != 0)
1290                   *pl->used_flag_ptr = 1;
1291                 return temp;
1292               }
1293             /* Some systems have a suffix for executable files.
1294                So try appending that.  */
1295             if (file_suffix[0] != 0)
1296               {
1297                 strcat (temp, file_suffix);
1298                 if (access (temp, mode) == 0)
1299                   {
1300                     if (pl->used_flag_ptr != 0)
1301                       *pl->used_flag_ptr = 1;
1302                     return temp;
1303                   }
1304               }
1305           }
1306       }
1307
1308   free (temp);
1309   return 0;
1310 }
1311
1312 /* Add an entry for PREFIX in PLIST.  If FIRST is set, it goes
1313    at the start of the list, otherwise it goes at the end.
1314
1315    If WARN is nonzero, we will warn if no file is found
1316    through this prefix.  WARN should point to an int
1317    which will be set to 1 if this entry is used.
1318
1319    REQUIRE_MACHINE_SUFFIX is 1 if this prefix can't be used without
1320    the complete value of machine_suffix.
1321    2 means try both machine_suffix and just_machine_suffix.  */
1322
1323 static void
1324 add_prefix (pprefix, prefix, first, require_machine_suffix, warn)
1325      struct path_prefix *pprefix;
1326      char *prefix;
1327      int first;
1328      int require_machine_suffix;
1329      int *warn;
1330 {
1331   struct prefix_list *pl, **prev;
1332   int len;
1333
1334   if (!first && pprefix->plist)
1335     {
1336       for (pl = pprefix->plist; pl->next; pl = pl->next)
1337         ;
1338       prev = &pl->next;
1339     }
1340   else
1341     prev = &pprefix->plist;
1342
1343   /* Keep track of the longest prefix */
1344
1345   len = strlen (prefix);
1346   if (len > pprefix->max_len)
1347     pprefix->max_len = len;
1348
1349   pl = (struct prefix_list *) xmalloc (sizeof (struct prefix_list));
1350   pl->prefix = save_string (prefix, len);
1351   pl->require_machine_suffix = require_machine_suffix;
1352   pl->used_flag_ptr = warn;
1353   if (warn)
1354     *warn = 0;
1355
1356   if (*prev)
1357     pl->next = *prev;
1358   else
1359     pl->next = (struct prefix_list *) 0;
1360   *prev = pl;
1361 }
1362
1363 /* Print warnings for any prefixes in the list PPREFIX that were not used.  */
1364
1365 static void
1366 unused_prefix_warnings (pprefix)
1367      struct path_prefix *pprefix;
1368 {
1369   struct prefix_list *pl = pprefix->plist;
1370
1371   while (pl)
1372     {
1373       if (pl->used_flag_ptr != 0 && !*pl->used_flag_ptr)
1374         {
1375           error ("file path prefix `%s' never used",
1376                  pl->prefix);
1377           /* Prevent duplicate warnings.  */
1378           *pl->used_flag_ptr = 1;
1379         }
1380       pl = pl->next;
1381     }
1382 }
1383
1384 /* Get rid of all prefixes built up so far in *PLISTP. */
1385
1386 static void
1387 free_path_prefix (pprefix)
1388      struct path_prefix *pprefix;
1389 {
1390   struct prefix_list *pl = pprefix->plist;
1391   struct prefix_list *temp;
1392
1393   while (pl)
1394     {
1395       temp = pl;
1396       pl = pl->next;
1397       free (temp->prefix);
1398       free ((char *) temp);
1399     }
1400   pprefix->plist = (struct prefix_list *) 0;
1401 }
1402 \f
1403 /* stdin file number.  */
1404 #define STDIN_FILE_NO 0
1405
1406 /* stdout file number.  */
1407 #define STDOUT_FILE_NO 1
1408
1409 /* value of `pipe': port index for reading.  */
1410 #define READ_PORT 0
1411
1412 /* value of `pipe': port index for writing.  */
1413 #define WRITE_PORT 1
1414
1415 /* Pipe waiting from last process, to be used as input for the next one.
1416    Value is STDIN_FILE_NO if no pipe is waiting
1417    (i.e. the next command is the first of a group).  */
1418
1419 static int last_pipe_input;
1420
1421 /* Fork one piped subcommand.  FUNC is the system call to use
1422    (either execv or execvp).  ARGV is the arg vector to use.
1423    NOT_LAST is nonzero if this is not the last subcommand
1424    (i.e. its output should be piped to the next one.)  */
1425
1426 #ifndef OS2
1427 #ifdef __MSDOS__
1428
1429 /* Declare these to avoid compilation error.  They won't be called.  */
1430 int execv(const char *a, const char **b){}
1431 int execvp(const char *a, const char **b){}
1432
1433 static int
1434 pexecute (search_flag, program, argv, not_last)
1435      int search_flag;
1436      char *program;
1437      char *argv[];
1438      int not_last;
1439 {
1440   char *scmd;
1441   FILE *argfile;
1442   int i;
1443
1444   scmd = (char *)malloc (strlen (program) + strlen (temp_filename) + 6);
1445   sprintf (scmd, "%s @%s.gp", program, temp_filename);
1446   argfile = fopen (scmd+strlen (program) + 2, "w");
1447   if (argfile == 0)
1448     pfatal_with_name (scmd + strlen (program) + 2);
1449
1450   for (i=1; argv[i]; i++)
1451   {
1452     char *cp;
1453     for (cp = argv[i]; *cp; cp++)
1454       {
1455         if (*cp == '"' || *cp == '\'' || *cp == '\\' || isspace (*cp))
1456           fputc ('\\', argfile);
1457         fputc (*cp, argfile);
1458       }
1459     fputc ('\n', argfile);
1460   }
1461   fclose (argfile);
1462
1463   i = system (scmd);
1464
1465   remove (scmd + strlen (program) + 2);
1466   return i << 8;
1467 }
1468
1469 #else /* not __MSDOS__ */
1470
1471 static int
1472 pexecute (search_flag, program, argv, not_last)
1473      int search_flag;
1474      char *program;
1475      char *argv[];
1476      int not_last;
1477 {
1478   int (*func)() = (search_flag ? execv : execvp);
1479   int pid;
1480   int pdes[2];
1481   int input_desc = last_pipe_input;
1482   int output_desc = STDOUT_FILE_NO;
1483   int retries, sleep_interval;
1484
1485   /* If this isn't the last process, make a pipe for its output,
1486      and record it as waiting to be the input to the next process.  */
1487
1488   if (not_last)
1489     {
1490       if (pipe (pdes) < 0)
1491         pfatal_with_name ("pipe");
1492       output_desc = pdes[WRITE_PORT];
1493       last_pipe_input = pdes[READ_PORT];
1494     }
1495   else
1496     last_pipe_input = STDIN_FILE_NO;
1497
1498   /* Fork a subprocess; wait and retry if it fails.  */
1499   sleep_interval = 1;
1500   for (retries = 0; retries < 4; retries++)
1501     {
1502       pid = vfork ();
1503       if (pid >= 0)
1504         break;
1505       sleep (sleep_interval);
1506       sleep_interval *= 2;
1507     }
1508
1509   switch (pid)
1510     {
1511     case -1:
1512 #ifdef vfork
1513       pfatal_with_name ("fork");
1514 #else
1515       pfatal_with_name ("vfork");
1516 #endif
1517       /* NOTREACHED */
1518       return 0;
1519
1520     case 0: /* child */
1521       /* Move the input and output pipes into place, if nec.  */
1522       if (input_desc != STDIN_FILE_NO)
1523         {
1524           close (STDIN_FILE_NO);
1525           dup (input_desc);
1526           close (input_desc);
1527         }
1528       if (output_desc != STDOUT_FILE_NO)
1529         {
1530           close (STDOUT_FILE_NO);
1531           dup (output_desc);
1532           close (output_desc);
1533         }
1534
1535       /* Close the parent's descs that aren't wanted here.  */
1536       if (last_pipe_input != STDIN_FILE_NO)
1537         close (last_pipe_input);
1538
1539       /* Exec the program.  */
1540       (*func) (program, argv);
1541       perror_exec (program);
1542       exit (-1);
1543       /* NOTREACHED */
1544       return 0;
1545
1546     default:
1547       /* In the parent, after forking.
1548          Close the descriptors that we made for this child.  */
1549       if (input_desc != STDIN_FILE_NO)
1550         close (input_desc);
1551       if (output_desc != STDOUT_FILE_NO)
1552         close (output_desc);
1553
1554       /* Return child's process number.  */
1555       return pid;
1556     }
1557 }
1558
1559 #endif /* not __MSDOS__ */
1560 #else /* not OS2 */
1561
1562 static int
1563 pexecute (search_flag, program, argv, not_last)
1564      int search_flag;
1565      char *program;
1566      char *argv[];
1567      int not_last;
1568 {
1569   return (search_flag ? spawnv : spawnvp) (1, program, argv);
1570 }
1571 #endif /* not OS2 */
1572 \f
1573 /* Execute the command specified by the arguments on the current line of spec.
1574    When using pipes, this includes several piped-together commands
1575    with `|' between them.
1576
1577    Return 0 if successful, -1 if failed.  */
1578
1579 static int
1580 execute ()
1581 {
1582   int i;
1583   int n_commands;               /* # of command.  */
1584   char *string;
1585   struct command
1586     {
1587       char *prog;               /* program name.  */
1588       char **argv;              /* vector of args.  */
1589       int pid;                  /* pid of process for this command.  */
1590     };
1591
1592   struct command *commands;     /* each command buffer with above info.  */
1593
1594   /* Count # of piped commands.  */
1595   for (n_commands = 1, i = 0; i < argbuf_index; i++)
1596     if (strcmp (argbuf[i], "|") == 0)
1597       n_commands++;
1598
1599   /* Get storage for each command.  */
1600   commands
1601     = (struct command *) alloca (n_commands * sizeof (struct command));
1602
1603   /* Split argbuf into its separate piped processes,
1604      and record info about each one.
1605      Also search for the programs that are to be run.  */
1606
1607   commands[0].prog = argbuf[0]; /* first command.  */
1608   commands[0].argv = &argbuf[0];
1609   string = find_a_file (&exec_prefix, commands[0].prog, X_OK);
1610   if (string)
1611     commands[0].argv[0] = string;
1612
1613   for (n_commands = 1, i = 0; i < argbuf_index; i++)
1614     if (strcmp (argbuf[i], "|") == 0)
1615       {                         /* each command.  */
1616 #ifdef __MSDOS__
1617         fatal ("-pipe not supported under MS-DOS");
1618 #endif
1619         argbuf[i] = 0;  /* termination of command args.  */
1620         commands[n_commands].prog = argbuf[i + 1];
1621         commands[n_commands].argv = &argbuf[i + 1];
1622         string = find_a_file (&exec_prefix, commands[n_commands].prog, X_OK);
1623         if (string)
1624           commands[n_commands].argv[0] = string;
1625         n_commands++;
1626       }
1627
1628   argbuf[argbuf_index] = 0;
1629
1630   /* If -v, print what we are about to do, and maybe query.  */
1631
1632   if (verbose_flag)
1633     {
1634       /* Print each piped command as a separate line.  */
1635       for (i = 0; i < n_commands ; i++)
1636         {
1637           char **j;
1638
1639           for (j = commands[i].argv; *j; j++)
1640             fprintf (stderr, " %s", *j);
1641
1642           /* Print a pipe symbol after all but the last command.  */
1643           if (i + 1 != n_commands)
1644             fprintf (stderr, " |");
1645           fprintf (stderr, "\n");
1646         }
1647       fflush (stderr);
1648 #ifdef DEBUG
1649       fprintf (stderr, "\nGo ahead? (y or n) ");
1650       fflush (stderr);
1651       i = getchar ();
1652       if (i != '\n')
1653         while (getchar () != '\n') ;
1654       if (i != 'y' && i != 'Y')
1655         return 0;
1656 #endif /* DEBUG */
1657     }
1658
1659   /* Run each piped subprocess.  */
1660
1661   last_pipe_input = STDIN_FILE_NO;
1662   for (i = 0; i < n_commands; i++)
1663     {
1664       char *string = commands[i].argv[0];
1665
1666       commands[i].pid = pexecute (string != commands[i].prog,
1667                                   string, commands[i].argv,
1668                                   i + 1 < n_commands);
1669
1670       if (string != commands[i].prog)
1671         free (string);
1672     }
1673
1674   execution_count++;
1675
1676   /* Wait for all the subprocesses to finish.
1677      We don't care what order they finish in;
1678      we know that N_COMMANDS waits will get them all.  */
1679
1680   {
1681     int ret_code = 0;
1682
1683     for (i = 0; i < n_commands; i++)
1684       {
1685         int status;
1686         int pid;
1687         char *prog;
1688
1689 #ifdef __MSDOS__
1690         status = pid = commands[i].pid;
1691 #else
1692         pid = wait (&status);
1693 #endif
1694         if (pid < 0)
1695           abort ();
1696
1697         if (status != 0)
1698           {
1699             int j;
1700             for (j = 0; j < n_commands; j++)
1701               if (commands[j].pid == pid)
1702                 prog = commands[j].prog;
1703
1704             if ((status & 0x7F) != 0)
1705               fatal ("Internal compiler error: program %s got fatal signal %d",
1706                      prog, (status & 0x7F));
1707             if (((status & 0xFF00) >> 8) >= MIN_FATAL_STATUS)
1708               ret_code = -1;
1709           }
1710       }
1711     return ret_code;
1712   }
1713 }
1714 \f
1715 /* Find all the switches given to us
1716    and make a vector describing them.
1717    The elements of the vector are strings, one per switch given.
1718    If a switch uses following arguments, then the `part1' field
1719    is the switch itself and the `args' field
1720    is a null-terminated vector containing the following arguments.
1721    The `valid' field is nonzero if any spec has looked at this switch;
1722    if it remains zero at the end of the run, it must be meaningless.  */
1723
1724 struct switchstr
1725 {
1726   char *part1;
1727   char **args;
1728   int valid;
1729 };
1730
1731 static struct switchstr *switches;
1732
1733 static int n_switches;
1734
1735 struct infile
1736 {
1737   char *name;
1738   char *language;
1739 };
1740
1741 /* Also a vector of input files specified.  */
1742
1743 static struct infile *infiles;
1744
1745 static int n_infiles;
1746
1747 /* And a vector of corresponding output files is made up later.  */
1748
1749 static char **outfiles;
1750
1751 /* Create the vector `switches' and its contents.
1752    Store its length in `n_switches'.  */
1753
1754 static void
1755 process_command (argc, argv)
1756      int argc;
1757      char **argv;
1758 {
1759   register int i;
1760   char *temp;
1761   char *spec_lang = 0;
1762   int last_language_n_infiles;
1763
1764   gcc_exec_prefix = getenv ("GCC_EXEC_PREFIX");
1765
1766   n_switches = 0;
1767   n_infiles = 0;
1768   spec_version = version_string;
1769
1770   /* Set up the default search paths.  */
1771
1772   if (gcc_exec_prefix)
1773     {
1774       add_prefix (&exec_prefix, gcc_exec_prefix, 0, 0, NULL_PTR);
1775       add_prefix (&startfile_prefix, gcc_exec_prefix, 0, 0, NULL_PTR);
1776     }
1777
1778   /* COMPILER_PATH and LIBRARY_PATH have values
1779      that are lists of directory names with colons.  */
1780
1781   temp = getenv ("COMPILER_PATH");
1782   if (temp)
1783     {
1784       char *startp, *endp;
1785       char *nstore = (char *) alloca (strlen (temp) + 3);
1786
1787       startp = endp = temp;
1788       while (1)
1789         {
1790           if (*endp == PATH_SEPARATOR || *endp == 0)
1791             {
1792               strncpy (nstore, startp, endp-startp);
1793               if (endp == startp)
1794                 {
1795                   strcpy (nstore, "./");
1796                 }
1797               else if (endp[-1] != '/')
1798                 {
1799                   nstore[endp-startp] = '/';
1800                   nstore[endp-startp+1] = 0;
1801                 }
1802               else
1803                 nstore[endp-startp] = 0;
1804               add_prefix (&exec_prefix, nstore, 0, 0, NULL_PTR);
1805               if (*endp == 0)
1806                 break;
1807               endp = startp = endp + 1;
1808             }
1809           else
1810             endp++;
1811         }
1812     }
1813
1814   temp = getenv ("LIBRARY_PATH");
1815   if (temp)
1816     {
1817       char *startp, *endp;
1818       char *nstore = (char *) alloca (strlen (temp) + 3);
1819
1820       startp = endp = temp;
1821       while (1)
1822         {
1823           if (*endp == PATH_SEPARATOR || *endp == 0)
1824             {
1825               strncpy (nstore, startp, endp-startp);
1826               if (endp == startp)
1827                 {
1828                   strcpy (nstore, "./");
1829                 }
1830               else if (endp[-1] != '/')
1831                 {
1832                   nstore[endp-startp] = '/';
1833                   nstore[endp-startp+1] = 0;
1834                 }
1835               else
1836                 nstore[endp-startp] = 0;
1837               add_prefix (&startfile_prefix, nstore, 0, 0, NULL_PTR);
1838               /* Make separate list of dirs that came from LIBRARY_PATH.  */
1839               add_prefix (&library_prefix, nstore, 0, 0, NULL_PTR);
1840               if (*endp == 0)
1841                 break;
1842               endp = startp = endp + 1;
1843             }
1844           else
1845             endp++;
1846         }
1847     }
1848
1849   /* Use LPATH like LIBRARY_PATH (for the CMU build program).  */
1850   temp = getenv ("LPATH");
1851   if (temp)
1852     {
1853       char *startp, *endp;
1854       char *nstore = (char *) alloca (strlen (temp) + 3);
1855
1856       startp = endp = temp;
1857       while (1)
1858         {
1859           if (*endp == PATH_SEPARATOR || *endp == 0)
1860             {
1861               strncpy (nstore, startp, endp-startp);
1862               if (endp == startp)
1863                 {
1864                   strcpy (nstore, "./");
1865                 }
1866               else if (endp[-1] != '/')
1867                 {
1868                   nstore[endp-startp] = '/';
1869                   nstore[endp-startp+1] = 0;
1870                 }
1871               else
1872                 nstore[endp-startp] = 0;
1873               add_prefix (&startfile_prefix, nstore, 0, 0, NULL_PTR);
1874               /* Make separate list of dirs that came from LIBRARY_PATH.  */
1875               add_prefix (&library_prefix, nstore, 0, 0, NULL_PTR);
1876               if (*endp == 0)
1877                 break;
1878               endp = startp = endp + 1;
1879             }
1880           else
1881             endp++;
1882         }
1883     }
1884
1885   /* Scan argv twice.  Here, the first time, just count how many switches
1886      there will be in their vector, and how many input files in theirs.
1887      Here we also parse the switches that cc itself uses (e.g. -v).  */
1888
1889   for (i = 1; i < argc; i++)
1890     {
1891       if (! strcmp (argv[i], "-dumpspecs"))
1892         {
1893           printf ("*asm:\n%s\n\n", asm_spec);
1894           printf ("*asm_final:\n%s\n\n", asm_final_spec);
1895           printf ("*cpp:\n%s\n\n", cpp_spec);
1896           printf ("*cc1:\n%s\n\n", cc1_spec);
1897           printf ("*cc1plus:\n%s\n\n", cc1plus_spec);
1898           printf ("*endfile:\n%s\n\n", endfile_spec);
1899           printf ("*link:\n%s\n\n", link_spec);
1900           printf ("*lib:\n%s\n\n", lib_spec);
1901           printf ("*startfile:\n%s\n\n", startfile_spec);
1902           printf ("*switches_need_spaces:\n%s\n\n", switches_need_spaces);
1903           printf ("*signed_char:\n%s\n\n", signed_char_spec);
1904           printf ("*predefines:\n%s\n\n", cpp_predefines);
1905           printf ("*cross_compile:\n%d\n\n", cross_compile);
1906
1907           exit (0);
1908         }
1909       else if (! strcmp (argv[i], "-dumpversion"))
1910         {
1911           printf ("%s\n", version_string);
1912           exit (0);
1913         }
1914       else if (! strcmp (argv[i], "-print-libgcc-file-name"))
1915         {
1916           print_libgcc_file_name = 1;
1917         }
1918       else if (! strcmp (argv[i], "-Xlinker"))
1919         {
1920           /* Pass the argument of this option to the linker when we link.  */
1921
1922           if (i + 1 == argc)
1923             fatal ("argument to `-Xlinker' is missing");
1924
1925           n_linker_options++;
1926           if (!linker_options)
1927             linker_options
1928               = (char **) xmalloc (n_linker_options * sizeof (char **));
1929           else
1930             linker_options
1931               = (char **) xrealloc (linker_options,
1932                                     n_linker_options * sizeof (char **));
1933
1934           linker_options[n_linker_options - 1] = argv[++i];
1935         }
1936       else if (argv[i][0] == '-' && argv[i][1] != 0 && argv[i][1] != 'l')
1937         {
1938           register char *p = &argv[i][1];
1939           register int c = *p;
1940
1941           switch (c)
1942             {
1943             case 'b':
1944               if (p[1] == 0 && i + 1 == argc)
1945                 fatal ("argument to `-b' is missing");
1946               if (p[1] == 0)
1947                 spec_machine = argv[++i];
1948               else
1949                 spec_machine = p + 1;
1950               break;
1951
1952             case 'B':
1953               {
1954                 int *temp = (int *) xmalloc (sizeof (int));
1955                 char *value;
1956                 if (p[1] == 0 && i + 1 == argc)
1957                   fatal ("argument to `-B' is missing");
1958                 if (p[1] == 0)
1959                   value = argv[++i];
1960                 else
1961                   value = p + 1;
1962                 add_prefix (&exec_prefix, value, 1, 0, temp);
1963                 add_prefix (&startfile_prefix, value, 1, 0, temp);
1964               }
1965               break;
1966
1967             case 'v':   /* Print our subcommands and print versions.  */
1968               verbose_flag++;
1969               n_switches++;
1970               break;
1971
1972             case 'V':
1973               if (p[1] == 0 && i + 1 == argc)
1974                 fatal ("argument to `-V' is missing");
1975               if (p[1] == 0)
1976                 spec_version = argv[++i];
1977               else
1978                 spec_version = p + 1;
1979               break;
1980
1981             case 's':
1982               if (!strcmp (p, "save-temps"))
1983                 {
1984                   save_temps_flag = 1;
1985                   n_switches++;
1986                   break;
1987                 }
1988             default:
1989               n_switches++;
1990
1991               if (SWITCH_TAKES_ARG (c) > (p[1] != 0))
1992                 i += SWITCH_TAKES_ARG (c) - (p[1] != 0);
1993               else if (WORD_SWITCH_TAKES_ARG (p))
1994                 i += WORD_SWITCH_TAKES_ARG (p);
1995             }
1996         }
1997       else
1998         n_infiles++;
1999     }
2000
2001   /* Set up the search paths before we go looking for config files.  */
2002
2003   /* These come before the md prefixes so that we will find gcc's subcommands
2004      (such as cpp) rather than those of the host system.  */
2005   /* Use 2 as fourth arg meaning try just the machine as a suffix,
2006      as well as trying the machine and the version.  */
2007   add_prefix (&exec_prefix, standard_exec_prefix, 0, 2, NULL_PTR);
2008   add_prefix (&exec_prefix, standard_exec_prefix_1, 0, 2, NULL_PTR);
2009
2010   add_prefix (&startfile_prefix, standard_exec_prefix, 0, 1, NULL_PTR);
2011   add_prefix (&startfile_prefix, standard_exec_prefix_1, 0, 1, NULL_PTR);
2012
2013   /* More prefixes are enabled in main, after we read the specs file
2014      and determine whether this is cross-compilation or not.  */
2015
2016
2017   /* Then create the space for the vectors and scan again.  */
2018
2019   switches = ((struct switchstr *)
2020               xmalloc ((n_switches + 1) * sizeof (struct switchstr)));
2021   infiles = (struct infile *) xmalloc ((n_infiles + 1) * sizeof (struct infile));
2022   n_switches = 0;
2023   n_infiles = 0;
2024   last_language_n_infiles = -1;
2025
2026   /* This, time, copy the text of each switch and store a pointer
2027      to the copy in the vector of switches.
2028      Store all the infiles in their vector.  */
2029
2030   for (i = 1; i < argc; i++)
2031     {
2032       if (!strcmp (argv[i], "-Xlinker"))
2033         i++;
2034       else if (! strcmp (argv[i], "-print-libgcc-file-name"))
2035         i++;
2036       else if (argv[i][0] == '-' && argv[i][1] != 0 && argv[i][1] != 'l')
2037         {
2038           register char *p = &argv[i][1];
2039           register int c = *p;
2040
2041           if (c == 'B' || c == 'b' || c == 'V')
2042             {
2043               /* Skip a separate arg, if any.  */
2044               if (p[1] == 0)
2045                 i++;
2046               continue;
2047             }
2048           if (c == 'x')
2049             {
2050               if (p[1] == 0 && i + 1 == argc)
2051                 fatal ("argument to `-x' is missing");
2052               if (p[1] == 0)
2053                 spec_lang = argv[++i];
2054               else
2055                 spec_lang = p + 1;
2056               if (! strcmp (spec_lang, "none"))
2057                 /* Suppress the warning if -xnone comes after the last input file,
2058                    because alternate command interfaces like g++ might find it
2059                    useful to place -xnone after each input file.  */
2060                 spec_lang = 0;
2061               else
2062                 last_language_n_infiles = n_infiles;
2063               continue;
2064             }
2065           switches[n_switches].part1 = p;
2066           /* Deal with option arguments in separate argv elements.  */
2067           if ((SWITCH_TAKES_ARG (c) > (p[1] != 0))
2068               || WORD_SWITCH_TAKES_ARG (p)) {
2069             int j = 0;
2070             int n_args = WORD_SWITCH_TAKES_ARG (p);
2071
2072             if (n_args == 0) {
2073               /* Count only the option arguments in separate argv elements.  */
2074               n_args = SWITCH_TAKES_ARG (c) - (p[1] != 0);
2075             }
2076             switches[n_switches].args
2077               = (char **) xmalloc ((n_args + 1) * sizeof (char *));
2078             while (j < n_args)
2079               switches[n_switches].args[j++] = argv[++i];
2080             /* Null-terminate the vector.  */
2081             switches[n_switches].args[j] = 0;
2082           } else if (*switches_need_spaces != 0 && (c == 'o' || c == 'L')) {
2083             /* On some systems, ld cannot handle -o or -L without space.
2084                So split the -o or -L from its argument.  */
2085             switches[n_switches].part1 = (c == 'o' ? "o" : "L");
2086             switches[n_switches].args = (char **) xmalloc (2 * sizeof (char *));
2087             switches[n_switches].args[0] = xmalloc (strlen (p));
2088             strcpy (switches[n_switches].args[0], &p[1]);
2089             switches[n_switches].args[1] = 0;
2090           } else
2091             switches[n_switches].args = 0;
2092           switches[n_switches].valid = 0;
2093           /* This is always valid, since gcc.c itself understands it.  */
2094           if (!strcmp (p, "save-temps"))
2095             switches[n_switches].valid = 1;
2096           n_switches++;
2097         }
2098       else
2099         {
2100           infiles[n_infiles].language = spec_lang;
2101           infiles[n_infiles++].name = argv[i];
2102         }
2103     }
2104
2105   if (n_infiles == last_language_n_infiles)
2106     error ("Warning: `-x %s' after last input file has no effect", spec_lang);
2107
2108   switches[n_switches].part1 = 0;
2109   infiles[n_infiles].name = 0;
2110
2111   /* If we have a GCC_EXEC_PREFIX envvar, modify it for cpp's sake.  */
2112   if (gcc_exec_prefix)
2113     {
2114       temp = (char *) xmalloc (strlen (gcc_exec_prefix) + strlen (spec_version)
2115                                + strlen (spec_machine) + 3);
2116       strcpy (temp, gcc_exec_prefix);
2117       strcat (temp, spec_machine);
2118       strcat (temp, "/");
2119       strcat (temp, spec_version);
2120       strcat (temp, "/");
2121       gcc_exec_prefix = temp;
2122     }
2123 }
2124 \f
2125 /* Process a spec string, accumulating and running commands.  */
2126
2127 /* These variables describe the input file name.
2128    input_file_number is the index on outfiles of this file,
2129    so that the output file name can be stored for later use by %o.
2130    input_basename is the start of the part of the input file
2131    sans all directory names, and basename_length is the number
2132    of characters starting there excluding the suffix .c or whatever.  */
2133
2134 static char *input_filename;
2135 static int input_file_number;
2136 static int input_filename_length;
2137 static int basename_length;
2138 static char *input_basename;
2139 static char *input_suffix;
2140
2141 /* These are variables used within do_spec and do_spec_1.  */
2142
2143 /* Nonzero if an arg has been started and not yet terminated
2144    (with space, tab or newline).  */
2145 static int arg_going;
2146
2147 /* Nonzero means %d or %g has been seen; the next arg to be terminated
2148    is a temporary file name.  */
2149 static int delete_this_arg;
2150
2151 /* Nonzero means %w has been seen; the next arg to be terminated
2152    is the output file name of this compilation.  */
2153 static int this_is_output_file;
2154
2155 /* Nonzero means %s has been seen; the next arg to be terminated
2156    is the name of a library file and we should try the standard
2157    search dirs for it.  */
2158 static int this_is_library_file;
2159
2160 /* Process the spec SPEC and run the commands specified therein.
2161    Returns 0 if the spec is successfully processed; -1 if failed.  */
2162
2163 static int
2164 do_spec (spec)
2165      char *spec;
2166 {
2167   int value;
2168
2169   clear_args ();
2170   arg_going = 0;
2171   delete_this_arg = 0;
2172   this_is_output_file = 0;
2173   this_is_library_file = 0;
2174
2175   value = do_spec_1 (spec, 0, NULL_PTR);
2176
2177   /* Force out any unfinished command.
2178      If -pipe, this forces out the last command if it ended in `|'.  */
2179   if (value == 0)
2180     {
2181       if (argbuf_index > 0 && !strcmp (argbuf[argbuf_index - 1], "|"))
2182         argbuf_index--;
2183
2184       if (argbuf_index > 0)
2185         value = execute ();
2186     }
2187
2188   return value;
2189 }
2190
2191 /* Process the sub-spec SPEC as a portion of a larger spec.
2192    This is like processing a whole spec except that we do
2193    not initialize at the beginning and we do not supply a
2194    newline by default at the end.
2195    INSWITCH nonzero means don't process %-sequences in SPEC;
2196    in this case, % is treated as an ordinary character.
2197    This is used while substituting switches.
2198    INSWITCH nonzero also causes SPC not to terminate an argument.
2199
2200    Value is zero unless a line was finished
2201    and the command on that line reported an error.  */
2202
2203 static int
2204 do_spec_1 (spec, inswitch, soft_matched_part)
2205      char *spec;
2206      int inswitch;
2207      char *soft_matched_part;
2208 {
2209   register char *p = spec;
2210   register int c;
2211   int i;
2212   char *string;
2213
2214   while (c = *p++)
2215     /* If substituting a switch, treat all chars like letters.
2216        Otherwise, NL, SPC, TAB and % are special.  */
2217     switch (inswitch ? 'a' : c)
2218       {
2219       case '\n':
2220         /* End of line: finish any pending argument,
2221            then run the pending command if one has been started.  */
2222         if (arg_going)
2223           {
2224             obstack_1grow (&obstack, 0);
2225             string = obstack_finish (&obstack);
2226             if (this_is_library_file)
2227               string = find_file (string);
2228             store_arg (string, delete_this_arg, this_is_output_file);
2229             if (this_is_output_file)
2230               outfiles[input_file_number] = string;
2231           }
2232         arg_going = 0;
2233
2234         if (argbuf_index > 0 && !strcmp (argbuf[argbuf_index - 1], "|"))
2235           {
2236             int i;
2237             for (i = 0; i < n_switches; i++)
2238               if (!strcmp (switches[i].part1, "pipe"))
2239                 break;
2240
2241             /* A `|' before the newline means use a pipe here,
2242                but only if -pipe was specified.
2243                Otherwise, execute now and don't pass the `|' as an arg.  */
2244             if (i < n_switches)
2245               {
2246                 switches[i].valid = 1;
2247                 break;
2248               }
2249             else
2250               argbuf_index--;
2251           }
2252
2253         if (argbuf_index > 0)
2254           {
2255             int value = execute ();
2256             if (value)
2257               return value;
2258           }
2259         /* Reinitialize for a new command, and for a new argument.  */
2260         clear_args ();
2261         arg_going = 0;
2262         delete_this_arg = 0;
2263         this_is_output_file = 0;
2264         this_is_library_file = 0;
2265         break;
2266
2267       case '|':
2268         /* End any pending argument.  */
2269         if (arg_going)
2270           {
2271             obstack_1grow (&obstack, 0);
2272             string = obstack_finish (&obstack);
2273             if (this_is_library_file)
2274               string = find_file (string);
2275             store_arg (string, delete_this_arg, this_is_output_file);
2276             if (this_is_output_file)
2277               outfiles[input_file_number] = string;
2278           }
2279
2280         /* Use pipe */
2281         obstack_1grow (&obstack, c);
2282         arg_going = 1;
2283         break;
2284
2285       case '\t':
2286       case ' ':
2287         /* Space or tab ends an argument if one is pending.  */
2288         if (arg_going)
2289           {
2290             obstack_1grow (&obstack, 0);
2291             string = obstack_finish (&obstack);
2292             if (this_is_library_file)
2293               string = find_file (string);
2294             store_arg (string, delete_this_arg, this_is_output_file);
2295             if (this_is_output_file)
2296               outfiles[input_file_number] = string;
2297           }
2298         /* Reinitialize for a new argument.  */
2299         arg_going = 0;
2300         delete_this_arg = 0;
2301         this_is_output_file = 0;
2302         this_is_library_file = 0;
2303         break;
2304
2305       case '%':
2306         switch (c = *p++)
2307           {
2308           case 0:
2309             fatal ("Invalid specification!  Bug in cc.");
2310
2311           case 'b':
2312             obstack_grow (&obstack, input_basename, basename_length);
2313             arg_going = 1;
2314             break;
2315
2316           case 'd':
2317             delete_this_arg = 2;
2318             break;
2319
2320           /* Dump out the directories specified with LIBRARY_PATH,
2321              followed by the absolute directories
2322              that we search for startfiles.  */
2323           case 'D':
2324             for (i = 0; i < 2; i++)
2325               {
2326                 struct prefix_list *pl
2327                   = (i == 0 ? library_prefix.plist : startfile_prefix.plist);
2328                 int bufsize = 100;
2329                 char *buffer = (char *) xmalloc (bufsize);
2330                 int idx;
2331
2332                 for (; pl; pl = pl->next)
2333                   {
2334 #ifdef RELATIVE_PREFIX_NOT_LINKDIR
2335                     /* Used on systems which record the specified -L dirs
2336                        and use them to search for dynamic linking.  */
2337                     /* Relative directories always come from -B,
2338                        and it is better not to use them for searching
2339                        at run time.  In particular, stage1 loses  */
2340                     if (pl->prefix[0] != '/')
2341                       continue;
2342 #endif
2343                     if (machine_suffix)
2344                       {
2345                         if (is_linker_dir (pl->prefix, machine_suffix))
2346                           {
2347                             do_spec_1 ("-L", 0, NULL_PTR);
2348 #ifdef SPACE_AFTER_L_OPTION
2349                             do_spec_1 (" ", 0, NULL_PTR);
2350 #endif
2351                             do_spec_1 (pl->prefix, 1, NULL_PTR);
2352                             /* Remove slash from machine_suffix.  */
2353                             if (strlen (machine_suffix) >= bufsize)
2354                               bufsize = strlen (machine_suffix) * 2 + 1;
2355                             buffer = (char *) xrealloc (buffer, bufsize);
2356                             strcpy (buffer, machine_suffix);
2357                             idx = strlen (buffer);
2358                             if (buffer[idx - 1] == '/')
2359                               buffer[idx - 1] = 0;
2360                             do_spec_1 (buffer, 1, NULL_PTR);
2361                             /* Make this a separate argument.  */
2362                             do_spec_1 (" ", 0, NULL_PTR);
2363                           }
2364                       }
2365                     if (!pl->require_machine_suffix)
2366                       {
2367                         if (is_linker_dir (pl->prefix, ""))
2368                           {
2369                             do_spec_1 ("-L", 0, NULL_PTR);
2370 #ifdef SPACE_AFTER_L_OPTION
2371                             do_spec_1 (" ", 0, NULL_PTR);
2372 #endif
2373                             /* Remove slash from pl->prefix.  */
2374                             if (strlen (pl->prefix) >= bufsize)
2375                               bufsize = strlen (pl->prefix) * 2 + 1;
2376                             buffer = (char *) xrealloc (buffer, bufsize);
2377                             strcpy (buffer, pl->prefix);
2378                             idx = strlen (buffer);
2379                             if (buffer[idx - 1] == '/')
2380                               buffer[idx - 1] = 0;
2381                             do_spec_1 (buffer, 1, NULL_PTR);
2382                             /* Make this a separate argument.  */
2383                             do_spec_1 (" ", 0, NULL_PTR);
2384                           }
2385                       }
2386                   }
2387                 free (buffer);
2388               }
2389             break;
2390
2391           case 'e':
2392             /* {...:%efoo} means report an error with `foo' as error message
2393                and don't execute any more commands for this file.  */
2394             {
2395               char *q = p;
2396               char *buf;
2397               while (*p != 0 && *p != '\n') p++;
2398               buf = (char *) alloca (p - q + 1);
2399               strncpy (buf, q, p - q);
2400               buf[p - q] = 0;
2401               error ("%s", buf);
2402               return -1;
2403             }
2404             break;
2405
2406           case 'g':
2407             if (save_temps_flag)
2408               obstack_grow (&obstack, input_basename, basename_length);
2409             else
2410               {
2411                 obstack_grow (&obstack, temp_filename, temp_filename_length);
2412                 delete_this_arg = 1;
2413               }
2414             arg_going = 1;
2415             break;
2416
2417           case 'i':
2418             obstack_grow (&obstack, input_filename, input_filename_length);
2419             arg_going = 1;
2420             break;
2421
2422           case 'I':
2423             if (gcc_exec_prefix)
2424               {
2425                 do_spec_1 ("-iprefix", 1, NULL_PTR);
2426                 /* Make this a separate argument.  */
2427                 do_spec_1 (" ", 0, NULL_PTR);
2428                 do_spec_1 (gcc_exec_prefix, 1, NULL_PTR);
2429                 do_spec_1 (" ", 0, NULL_PTR);
2430               }
2431             break;
2432
2433           case 'o':
2434             {
2435               register int f;
2436               for (f = 0; f < n_infiles; f++)
2437                 store_arg (outfiles[f], 0, 0);
2438             }
2439             break;
2440
2441           case 's':
2442             this_is_library_file = 1;
2443             break;
2444
2445           case 'w':
2446             this_is_output_file = 1;
2447             break;
2448
2449           case 'W':
2450             {
2451               int index = argbuf_index;
2452               /* Handle the {...} following the %W.  */
2453               if (*p != '{')
2454                 abort ();
2455               p = handle_braces (p + 1);
2456               if (p == 0)
2457                 return -1;
2458               /* If any args were output, mark the last one for deletion
2459                  on failure.  */
2460               if (argbuf_index != index)
2461                 record_temp_file (argbuf[argbuf_index - 1], 0, 1);
2462               break;
2463             }
2464
2465           /* %x{OPTION} records OPTION for %X to output.  */
2466           case 'x':
2467             {
2468               char *p1 = p;
2469               char *string;
2470
2471               /* Skip past the option value and make a copy.  */
2472               if (*p != '{')
2473                 abort ();
2474               while (*p++ != '}')
2475                 ;
2476               string = save_string (p1 + 1, p - p1 - 2);
2477
2478               /* See if we already recorded this option.  */
2479               for (i = 0; i < n_linker_options; i++)
2480                 if (! strcmp (string, linker_options[i]))
2481                   {
2482                     free (string);
2483                     return 0;
2484                   }
2485
2486               /* This option is new; add it.  */
2487               n_linker_options++;
2488               if (!linker_options)
2489                 linker_options
2490                   = (char **) xmalloc (n_linker_options * sizeof (char **));
2491               else
2492                 linker_options
2493                   = (char **) xrealloc (linker_options,
2494                                         n_linker_options * sizeof (char **));
2495
2496               linker_options[n_linker_options - 1] = string;
2497             }
2498             break;
2499
2500           /* Dump out the options accumulated previously using %x.  */
2501           case 'X':
2502             for (i = 0; i < n_linker_options; i++)
2503               {
2504                 do_spec_1 (linker_options[i], 1, NULL_PTR);
2505                 /* Make each accumulated option a separate argument.  */
2506                 do_spec_1 (" ", 0, NULL_PTR);
2507               }
2508             break;
2509
2510             /* Here are digits and numbers that just process
2511                a certain constant string as a spec.  */
2512
2513           case '1':
2514             do_spec_1 (cc1_spec, 0, NULL_PTR);
2515             break;
2516
2517           case '2':
2518             do_spec_1 (cc1plus_spec, 0, NULL_PTR);
2519             break;
2520
2521           case 'a':
2522             do_spec_1 (asm_spec, 0, NULL_PTR);
2523             break;
2524
2525           case 'A':
2526             do_spec_1 (asm_final_spec, 0, NULL_PTR);
2527             break;
2528
2529           case 'c':
2530             do_spec_1 (signed_char_spec, 0, NULL_PTR);
2531             break;
2532
2533           case 'C':
2534             do_spec_1 (cpp_spec, 0, NULL_PTR);
2535             break;
2536
2537           case 'E':
2538             do_spec_1 (endfile_spec, 0, NULL_PTR);
2539             break;
2540
2541           case 'l':
2542             do_spec_1 (link_spec, 0, NULL_PTR);
2543             break;
2544
2545           case 'L':
2546             do_spec_1 (lib_spec, 0, NULL_PTR);
2547             break;
2548
2549           case 'p':
2550             {
2551               char *x = (char *) alloca (strlen (cpp_predefines) + 1);
2552               char *buf = x;
2553               char *y;
2554
2555               /* Copy all of the -D options in CPP_PREDEFINES into BUF.  */
2556               y = cpp_predefines;
2557               while (*y != 0)
2558                 {
2559                   if (! strncmp (y, "-D", 2))
2560                     /* Copy the whole option.  */
2561                     while (*y && *y != ' ' && *y != '\t')
2562                       *x++ = *y++;
2563                   else if (*y == ' ' || *y == '\t')
2564                     /* Copy whitespace to the result.  */
2565                     *x++ = *y++;
2566                   /* Don't copy other options.  */
2567                   else
2568                     y++;
2569                 }
2570
2571               *x = 0;
2572
2573               do_spec_1 (buf, 0, NULL_PTR);
2574             }
2575             break;
2576
2577           case 'P':
2578             {
2579               char *x = (char *) alloca (strlen (cpp_predefines) * 4 + 1);
2580               char *buf = x;
2581               char *y;
2582
2583               /* Copy all of CPP_PREDEFINES into BUF,
2584                  but put __ after every -D and at the end of each arg.  */
2585               y = cpp_predefines;
2586               while (*y != 0)
2587                 {
2588                   if (! strncmp (y, "-D", 2))
2589                     {
2590                       int flag = 0;
2591
2592                       *x++ = *y++;
2593                       *x++ = *y++;
2594
2595                       if (strncmp (y, "__", 2))
2596                         {
2597                           /* Stick __ at front of macro name.  */
2598                           *x++ = '_';
2599                           *x++ = '_';
2600                           /* Arrange to stick __ at the end as well.  */
2601                           flag = 1;
2602                         }
2603
2604                       /* Copy the macro name.  */
2605                       while (*y && *y != '=' && *y != ' ' && *y != '\t')
2606                         *x++ = *y++;
2607
2608                       if (flag)
2609                         {
2610                           *x++ = '_';
2611                           *x++ = '_';
2612                         }
2613
2614                       /* Copy the value given, if any.  */
2615                       while (*y && *y != ' ' && *y != '\t')
2616                         *x++ = *y++;
2617                     }
2618                   else if (*y == ' ' || *y == '\t')
2619                     /* Copy whitespace to the result.  */
2620                     *x++ = *y++;
2621                   /* Don't copy -A options  */
2622                   else
2623                     y++;
2624                 }
2625               *x++ = ' ';
2626
2627               /* Copy all of CPP_PREDEFINES into BUF,
2628                  but put __ after every -D.  */
2629               y = cpp_predefines;
2630               while (*y != 0)
2631                 {
2632                   if (! strncmp (y, "-D", 2))
2633                     {
2634                       *x++ = *y++;
2635                       *x++ = *y++;
2636
2637                       if (strncmp (y, "__", 2))
2638                         {
2639                           /* Stick __ at front of macro name.  */
2640                           *x++ = '_';
2641                           *x++ = '_';
2642                         }
2643
2644                       /* Copy the macro name.  */
2645                       while (*y && *y != '=' && *y != ' ' && *y != '\t')
2646                         *x++ = *y++;
2647
2648                       /* Copy the value given, if any.  */
2649                       while (*y && *y != ' ' && *y != '\t')
2650                         *x++ = *y++;
2651                     }
2652                   else if (*y == ' ' || *y == '\t')
2653                     /* Copy whitespace to the result.  */
2654                     *x++ = *y++;
2655                   /* Don't copy -A options  */
2656                   else
2657                     y++;
2658                 }
2659               *x++ = ' ';
2660
2661               /* Copy all of the -A options in CPP_PREDEFINES into BUF.  */
2662               y = cpp_predefines;
2663               while (*y != 0)
2664                 {
2665                   if (! strncmp (y, "-A", 2))
2666                     /* Copy the whole option.  */
2667                     while (*y && *y != ' ' && *y != '\t')
2668                       *x++ = *y++;
2669                   else if (*y == ' ' || *y == '\t')
2670                     /* Copy whitespace to the result.  */
2671                     *x++ = *y++;
2672                   /* Don't copy other options.  */
2673                   else
2674                     y++;
2675                 }
2676
2677               *x = 0;
2678
2679               do_spec_1 (buf, 0, NULL_PTR);
2680             }
2681             break;
2682
2683           case 'S':
2684             do_spec_1 (startfile_spec, 0, NULL_PTR);
2685             break;
2686
2687             /* Here we define characters other than letters and digits.  */
2688
2689           case '{':
2690             p = handle_braces (p);
2691             if (p == 0)
2692               return -1;
2693             break;
2694
2695           case '%':
2696             obstack_1grow (&obstack, '%');
2697             break;
2698
2699           case '*':
2700             do_spec_1 (soft_matched_part, 1, NULL_PTR);
2701             do_spec_1 (" ", 0, NULL_PTR);
2702             break;
2703
2704             /* Process a string found as the value of a spec given by name.
2705                This feature allows individual machine descriptions
2706                to add and use their own specs.
2707                %[...] modifies -D options the way %P does;
2708                %(...) uses the spec unmodified.  */
2709           case '(':
2710           case '[':
2711             {
2712               char *name = p;
2713               struct spec_list *sl;
2714               int len;
2715
2716               /* The string after the S/P is the name of a spec that is to be
2717                  processed. */
2718               while (*p && *p != ')' && *p != ']')
2719                 p++;
2720
2721               /* See if it's in the list */
2722               for (len = p - name, sl = specs; sl; sl = sl->next)
2723                 if (strncmp (sl->name, name, len) == 0 && !sl->name[len])
2724                   {
2725                     name = sl->spec;
2726                     break;
2727                   }
2728
2729               if (sl)
2730                 {
2731                   if (c == '(')
2732                     do_spec_1 (name, 0, NULL_PTR);
2733                   else
2734                     {
2735                       char *x = (char *) alloca (strlen (name) * 2 + 1);
2736                       char *buf = x;
2737                       char *y = name;
2738
2739                       /* Copy all of NAME into BUF, but put __ after
2740                          every -D and at the end of each arg,  */
2741                       while (1)
2742                         {
2743                           if (! strncmp (y, "-D", 2))
2744                             {
2745                               *x++ = '-';
2746                               *x++ = 'D';
2747                               *x++ = '_';
2748                               *x++ = '_';
2749                               y += 2;
2750                             }
2751                           else if (*y == ' ' || *y == 0)
2752                             {
2753                               *x++ = '_';
2754                               *x++ = '_';
2755                               if (*y == 0)
2756                                 break;
2757                               else
2758                                 *x++ = *y++;
2759                             }
2760                           else
2761                             *x++ = *y++;
2762                         }
2763                       *x = 0;
2764
2765                       do_spec_1 (buf, 0, NULL_PTR);
2766                     }
2767                 }
2768
2769               /* Discard the closing paren or bracket.  */
2770               if (*p)
2771                 p++;
2772             }
2773             break;
2774
2775           default:
2776             abort ();
2777           }
2778         break;
2779
2780       case '\\':
2781         /* Backslash: treat next character as ordinary.  */
2782         c = *p++;
2783
2784         /* fall through */
2785       default:
2786         /* Ordinary character: put it into the current argument.  */
2787         obstack_1grow (&obstack, c);
2788         arg_going = 1;
2789       }
2790
2791   return 0;             /* End of string */
2792 }
2793
2794 /* Return 0 if we call do_spec_1 and that returns -1.  */
2795
2796 static char *
2797 handle_braces (p)
2798      register char *p;
2799 {
2800   register char *q;
2801   char *filter;
2802   int pipe = 0;
2803   int negate = 0;
2804   int suffix = 0;
2805
2806   if (*p == '|')
2807     /* A `|' after the open-brace means,
2808        if the test fails, output a single minus sign rather than nothing.
2809        This is used in %{|!pipe:...}.  */
2810     pipe = 1, ++p;
2811
2812   if (*p == '!')
2813     /* A `!' after the open-brace negates the condition:
2814        succeed if the specified switch is not present.  */
2815     negate = 1, ++p;
2816
2817   if (*p == '.')
2818     /* A `.' after the open-brace means test against the current suffix.  */
2819     {
2820       if (pipe)
2821         abort ();
2822
2823       suffix = 1;
2824       ++p;
2825     }
2826
2827   filter = p;
2828   while (*p != ':' && *p != '}') p++;
2829   if (*p != '}')
2830     {
2831       register int count = 1;
2832       q = p + 1;
2833       while (count > 0)
2834         {
2835           if (*q == '{')
2836             count++;
2837           else if (*q == '}')
2838             count--;
2839           else if (*q == 0)
2840             abort ();
2841           q++;
2842         }
2843     }
2844   else
2845     q = p + 1;
2846
2847   if (suffix)
2848     {
2849       int found = (input_suffix != 0
2850                    && strlen (input_suffix) == p - filter
2851                    && strncmp (input_suffix, filter, p - filter) == 0);
2852
2853       if (p[0] == '}')
2854         abort ();
2855
2856       if (negate != found
2857           && do_spec_1 (save_string (p + 1, q - p - 2), 0, NULL_PTR) < 0)
2858         return 0;
2859
2860       return q;
2861     }
2862   else if (p[-1] == '*' && p[0] == '}')
2863     {
2864       /* Substitute all matching switches as separate args.  */
2865       register int i;
2866       --p;
2867       for (i = 0; i < n_switches; i++)
2868         if (!strncmp (switches[i].part1, filter, p - filter))
2869           give_switch (i, 0);
2870     }
2871   else
2872     {
2873       /* Test for presence of the specified switch.  */
2874       register int i;
2875       int present = 0;
2876
2877       /* If name specified ends in *, as in {x*:...},
2878          check for %* and handle that case.  */
2879       if (p[-1] == '*' && !negate)
2880         {
2881           int substitution;
2882           char *r = p;
2883
2884           /* First see whether we have %*.  */
2885           substitution = 0;
2886           while (r < q)
2887             {
2888               if (*r == '%' && r[1] == '*')
2889                 substitution = 1;
2890               r++;
2891             }
2892           /* If we do, handle that case.  */
2893           if (substitution)
2894             {
2895               /* Substitute all matching switches as separate args.
2896                  But do this by substituting for %*
2897                  in the text that follows the colon.  */
2898
2899               unsigned hard_match_len = p - filter - 1;
2900               char *string = save_string (p + 1, q - p - 2);
2901
2902               for (i = 0; i < n_switches; i++)
2903                 if (!strncmp (switches[i].part1, filter, hard_match_len))
2904                   {
2905                     do_spec_1 (string, 0, &switches[i].part1[hard_match_len]);
2906                     /* Pass any arguments this switch has.  */
2907                     give_switch (i, 1);
2908                   }
2909
2910               return q;
2911             }
2912         }
2913
2914       /* If name specified ends in *, as in {x*:...},
2915          check for presence of any switch name starting with x.  */
2916       if (p[-1] == '*')
2917         {
2918           for (i = 0; i < n_switches; i++)
2919             {
2920               unsigned hard_match_len = p - filter - 1;
2921
2922               if (!strncmp (switches[i].part1, filter, hard_match_len))
2923                 {
2924                   switches[i].valid = 1;
2925                   present = 1;
2926                 }
2927             }
2928         }
2929       /* Otherwise, check for presence of exact name specified.  */
2930       else
2931         {
2932           for (i = 0; i < n_switches; i++)
2933             {
2934               if (!strncmp (switches[i].part1, filter, p - filter)
2935                   && switches[i].part1[p - filter] == 0)
2936                 {
2937                   switches[i].valid = 1;
2938                   present = 1;
2939                   break;
2940                 }
2941             }
2942         }
2943
2944       /* If it is as desired (present for %{s...}, absent for %{-s...})
2945          then substitute either the switch or the specified
2946          conditional text.  */
2947       if (present != negate)
2948         {
2949           if (*p == '}')
2950             {
2951               give_switch (i, 0);
2952             }
2953           else
2954             {
2955               if (do_spec_1 (save_string (p + 1, q - p - 2), 0, NULL_PTR) < 0)
2956                 return 0;
2957             }
2958         }
2959       else if (pipe)
2960         {
2961           /* Here if a %{|...} conditional fails: output a minus sign,
2962              which means "standard output" or "standard input".  */
2963           do_spec_1 ("-", 0, NULL_PTR);
2964         }
2965     }
2966
2967   return q;
2968 }
2969
2970 /* Pass a switch to the current accumulating command
2971    in the same form that we received it.
2972    SWITCHNUM identifies the switch; it is an index into
2973    the vector of switches gcc received, which is `switches'.
2974    This cannot fail since it never finishes a command line.
2975
2976    If OMIT_FIRST_WORD is nonzero, then we omit .part1 of the argument.  */
2977
2978 static void
2979 give_switch (switchnum, omit_first_word)
2980      int switchnum;
2981      int omit_first_word;
2982 {
2983   if (!omit_first_word)
2984     {
2985       do_spec_1 ("-", 0, NULL_PTR);
2986       do_spec_1 (switches[switchnum].part1, 1, NULL_PTR);
2987     }
2988   do_spec_1 (" ", 0, NULL_PTR);
2989   if (switches[switchnum].args != 0)
2990     {
2991       char **p;
2992       for (p = switches[switchnum].args; *p; p++)
2993         {
2994           do_spec_1 (*p, 1, NULL_PTR);
2995           do_spec_1 (" ", 0, NULL_PTR);
2996         }
2997     }
2998   switches[switchnum].valid = 1;
2999 }
3000 \f
3001 /* Search for a file named NAME trying various prefixes including the
3002    user's -B prefix and some standard ones.
3003    Return the absolute file name found.  If nothing is found, return NAME.  */
3004
3005 static char *
3006 find_file (name)
3007      char *name;
3008 {
3009   char *newname;
3010
3011   newname = find_a_file (&startfile_prefix, name, R_OK);
3012   return newname ? newname : name;
3013 }
3014
3015 /* Determine whether a -L option is relevant.  Not required for certain
3016    fixed names and for directories that don't exist.  */
3017
3018 static int
3019 is_linker_dir (path1, path2)
3020      char *path1;
3021      char *path2;
3022 {
3023   int len1 = strlen (path1);
3024   int len2 = strlen (path2);
3025   char *path = (char *) alloca (3 + len1 + len2);
3026   char *cp;
3027   struct stat st;
3028
3029   /* Construct the path from the two parts.  Ensure the string ends with "/.".
3030      The resulting path will be a directory even if the given path is a
3031      symbolic link.  */
3032   bcopy (path1, path, len1);
3033   bcopy (path2, path + len1, len2);
3034   cp = path + len1 + len2;
3035   if (cp[-1] != '/')
3036     *cp++ = '/';
3037   *cp++ = '.';
3038   *cp = '\0';
3039
3040   /* Exclude directories that the linker is known to search.  */
3041   if ((cp - path == 6 && strcmp (path, "/lib/.") == 0)
3042       || (cp - path == 10 && strcmp (path, "/usr/lib/.") == 0))
3043     return 0;
3044
3045   return (stat (path, &st) >= 0 && S_ISDIR (st.st_mode));
3046 }
3047 \f
3048 /* On fatal signals, delete all the temporary files.  */
3049
3050 static void
3051 fatal_error (signum)
3052      int signum;
3053 {
3054   signal (signum, SIG_DFL);
3055   delete_failure_queue ();
3056   delete_temp_files ();
3057   /* Get the same signal again, this time not handled,
3058      so its normal effect occurs.  */
3059   kill (getpid (), signum);
3060 }
3061
3062 int
3063 main (argc, argv)
3064      int argc;
3065      char **argv;
3066 {
3067   register int i;
3068   int value;
3069   int error_count = 0;
3070   int linker_was_run = 0;
3071   char *explicit_link_files;
3072   char *specs_file;
3073
3074   programname = argv[0];
3075
3076   if (signal (SIGINT, SIG_IGN) != SIG_IGN)
3077     signal (SIGINT, fatal_error);
3078   if (signal (SIGHUP, SIG_IGN) != SIG_IGN)
3079     signal (SIGHUP, fatal_error);
3080   if (signal (SIGTERM, SIG_IGN) != SIG_IGN)
3081     signal (SIGTERM, fatal_error);
3082 #ifdef SIGPIPE
3083   if (signal (SIGPIPE, SIG_IGN) != SIG_IGN)
3084     signal (SIGPIPE, fatal_error);
3085 #endif
3086
3087   argbuf_length = 10;
3088   argbuf = (char **) xmalloc (argbuf_length * sizeof (char *));
3089
3090   obstack_init (&obstack);
3091
3092   /* Set up to remember the pathname of gcc and any options
3093      needed for collect.  */
3094   obstack_init (&collect_obstack);
3095   obstack_grow (&collect_obstack, "COLLECT_GCC=", sizeof ("COLLECT_GCC=")-1);
3096   obstack_grow (&collect_obstack, programname, strlen (programname)+1);
3097   putenv (obstack_finish (&collect_obstack));
3098
3099   /* Choose directory for temp files.  */
3100
3101   choose_temp_base ();
3102
3103   /* Make a table of what switches there are (switches, n_switches).
3104      Make a table of specified input files (infiles, n_infiles).
3105      Decode switches that are handled locally.  */
3106
3107   process_command (argc, argv);
3108
3109   /* Initialize the vector of specs to just the default.
3110      This means one element containing 0s, as a terminator.  */
3111
3112   compilers = (struct compiler *) xmalloc (sizeof default_compilers);
3113   bcopy (default_compilers, compilers, sizeof default_compilers);
3114   n_compilers = n_default_compilers;
3115
3116   /* Read specs from a file if there is one.  */
3117
3118   machine_suffix = concat (spec_machine, "/", concat (spec_version, "/", ""));
3119   just_machine_suffix = concat (spec_machine, "/", "");
3120
3121   specs_file = find_a_file (&startfile_prefix, "specs", R_OK);
3122   /* Read the specs file unless it is a default one.  */
3123   if (specs_file != 0 && strcmp (specs_file, "specs"))
3124     read_specs (specs_file);
3125
3126   /* If not cross-compiling, look for startfiles in the standard places.  */
3127   /* The fact that these are done here, after reading the specs file,
3128      means that it cannot be found in these directories.
3129      But that's okay.  It should never be there anyway.  */
3130   if (!cross_compile)
3131     {
3132 #ifdef MD_EXEC_PREFIX
3133       add_prefix (&exec_prefix, md_exec_prefix, 0, 0, NULL_PTR);
3134       add_prefix (&startfile_prefix, md_exec_prefix, 0, 0, NULL_PTR);
3135 #endif
3136
3137 #ifdef MD_STARTFILE_PREFIX
3138       add_prefix (&startfile_prefix, md_startfile_prefix, 0, 0, NULL_PTR);
3139 #endif
3140
3141 #ifdef MD_STARTFILE_PREFIX_1
3142       add_prefix (&startfile_prefix, md_startfile_prefix_1, 0, 0, NULL_PTR);
3143 #endif
3144
3145       add_prefix (&startfile_prefix, standard_startfile_prefix, 0, 0,
3146                   NULL_PTR);
3147       add_prefix (&startfile_prefix, standard_startfile_prefix_1, 0, 0,
3148                   NULL_PTR);
3149       add_prefix (&startfile_prefix, standard_startfile_prefix_2, 0, 0,
3150                   NULL_PTR);
3151 #if 0 /* Can cause surprises, and one can use -B./ instead.  */
3152       add_prefix (&startfile_prefix, "./", 0, 1, NULL_PTR);
3153 #endif
3154     }
3155
3156   /* Now we have the specs.
3157      Set the `valid' bits for switches that match anything in any spec.  */
3158
3159   validate_all_switches ();
3160
3161   /* Warn about any switches that no pass was interested in.  */
3162
3163   for (i = 0; i < n_switches; i++)
3164     if (! switches[i].valid)
3165       error ("unrecognized option `-%s'", switches[i].part1);
3166
3167   if (print_libgcc_file_name)
3168     {
3169       printf ("%s\n", find_file ("libgcc.a"));
3170       exit (0);
3171     }
3172
3173   /* Obey some of the options.  */
3174
3175   if (verbose_flag)
3176     {
3177       fprintf (stderr, "gcc version %s\n", version_string);
3178       if (n_infiles == 0)
3179         exit (0);
3180     }
3181
3182   if (n_infiles == 0)
3183     fatal ("No input files specified.");
3184
3185   /* Make a place to record the compiler output file names
3186      that correspond to the input files.  */
3187
3188   outfiles = (char **) xmalloc (n_infiles * sizeof (char *));
3189   bzero (outfiles, n_infiles * sizeof (char *));
3190
3191   /* Record which files were specified explicitly as link input.  */
3192
3193   explicit_link_files = xmalloc (n_infiles);
3194   bzero (explicit_link_files, n_infiles);
3195
3196   for (i = 0; i < n_infiles; i++)
3197     {
3198       register struct compiler *cp = 0;
3199       int this_file_error = 0;
3200
3201       /* Tell do_spec what to substitute for %i.  */
3202
3203       input_filename = infiles[i].name;
3204       input_filename_length = strlen (input_filename);
3205       input_file_number = i;
3206
3207       /* Use the same thing in %o, unless cp->spec says otherwise.  */
3208
3209       outfiles[i] = input_filename;
3210
3211       /* Figure out which compiler from the file's suffix.  */
3212
3213       cp = lookup_compiler (infiles[i].name, input_filename_length,
3214                             infiles[i].language);
3215
3216       if (cp)
3217         {
3218           /* Ok, we found an applicable compiler.  Run its spec.  */
3219           /* First say how much of input_filename to substitute for %b  */
3220           register char *p;
3221
3222           input_basename = input_filename;
3223           for (p = input_filename; *p; p++)
3224             if (*p == '/')
3225               input_basename = p + 1;
3226
3227           /* Find a suffix starting with the last period,
3228              and set basename_length to exclude that suffix.  */
3229           basename_length = strlen (input_basename);
3230           p = input_basename + basename_length;
3231           while (p != input_basename && *p != '.') --p;
3232           if (*p == '.' && p != input_basename)
3233             {
3234               basename_length = p - input_basename;
3235               input_suffix = p + 1;
3236             }
3237           else
3238             input_suffix = "";
3239
3240           value = do_spec (cp->spec);
3241           if (value < 0)
3242             this_file_error = 1;
3243         }
3244
3245       /* If this file's name does not contain a recognized suffix,
3246          record it as explicit linker input.  */
3247
3248       else
3249         explicit_link_files[i] = 1;
3250
3251       /* Clear the delete-on-failure queue, deleting the files in it
3252          if this compilation failed.  */
3253
3254       if (this_file_error)
3255         {
3256           delete_failure_queue ();
3257           error_count++;
3258         }
3259       /* If this compilation succeeded, don't delete those files later.  */
3260       clear_failure_queue ();
3261     }
3262
3263   /* Run ld to link all the compiler output files.  */
3264
3265   if (error_count == 0)
3266     {
3267       int tmp = execution_count;
3268       int i;
3269       int first_time;
3270
3271       /* Rebuild the COMPILER_PATH and LIBRARY_PATH environment variables
3272          for collect.  */
3273       putenv_from_prefixes (&exec_prefix, "COMPILER_PATH=");
3274       putenv_from_prefixes (&startfile_prefix, "LIBRARY_PATH=");
3275
3276       /* Build COLLECT_GCC_OPTIONS to have all of the options specified to
3277          the compiler.  */
3278       obstack_grow (&collect_obstack, "COLLECT_GCC_OPTIONS=",
3279                     sizeof ("COLLECT_GCC_OPTIONS=")-1);
3280
3281       first_time = TRUE;
3282       for (i = 0; i < n_switches; i++)
3283         {
3284           char **args;
3285           if (!first_time)
3286             obstack_grow (&collect_obstack, " ", 1);
3287
3288           first_time = FALSE;
3289           obstack_grow (&collect_obstack, "-", 1);
3290           obstack_grow (&collect_obstack, switches[i].part1,
3291                         strlen (switches[i].part1));
3292
3293           for (args = switches[i].args; args && *args; args++)
3294             {
3295               obstack_grow (&collect_obstack, " ", 1);
3296               obstack_grow (&collect_obstack, *args, strlen (*args));
3297             }
3298         }
3299       obstack_grow (&collect_obstack, "\0", 1);
3300       putenv (obstack_finish (&collect_obstack));
3301
3302       value = do_spec (link_command_spec);
3303       if (value < 0)
3304         error_count = 1;
3305       linker_was_run = (tmp != execution_count);
3306     }
3307
3308   /* Warn if a -B option was specified but the prefix was never used.  */
3309   unused_prefix_warnings (&exec_prefix);
3310   unused_prefix_warnings (&startfile_prefix);
3311
3312   /* If options said don't run linker,
3313      complain about input files to be given to the linker.  */
3314
3315   if (! linker_was_run && error_count == 0)
3316     for (i = 0; i < n_infiles; i++)
3317       if (explicit_link_files[i])
3318         error ("%s: linker input file unused since linking not done",
3319                outfiles[i]);
3320
3321   /* Delete some or all of the temporary files we made.  */
3322
3323   if (error_count)
3324     delete_failure_queue ();
3325   delete_temp_files ();
3326
3327   exit (error_count);
3328   /* NOTREACHED */
3329   return 0;
3330 }
3331
3332 /* Find the proper compilation spec for the file name NAME,
3333    whose length is LENGTH.  LANGUAGE is the specified language,
3334    or 0 if none specified.  */
3335
3336 static struct compiler *
3337 lookup_compiler (name, length, language)
3338      char *name;
3339      int length;
3340      char *language;
3341 {
3342   struct compiler *cp;
3343
3344   /* Look for the language, if one is spec'd.  */
3345   if (language != 0)
3346     {
3347       for (cp = compilers + n_compilers - 1; cp >= compilers; cp--)
3348         {
3349           if (language != 0)
3350             {
3351               if (cp->suffix[0] == '@'
3352                   && !strcmp (cp->suffix + 1, language))
3353                 return cp;
3354             }
3355         }
3356       error ("language %s not recognized", language);
3357     }
3358
3359   /* Look for a suffix.  */
3360   for (cp = compilers + n_compilers - 1; cp >= compilers; cp--)
3361     {
3362       if (strlen (cp->suffix) < length
3363                /* See if the suffix matches the end of NAME.  */
3364                && !strcmp (cp->suffix,
3365                            name + length - strlen (cp->suffix))
3366                /* The suffix `-' matches only the file name `-'.  */
3367                && !(!strcmp (cp->suffix, "-") && length != 1))
3368         {
3369           if (cp->spec[0] == '@')
3370             {
3371               struct compiler *new;
3372               /* An alias entry maps a suffix to a language.
3373                  Search for the language; pass 0 for NAME and LENGTH
3374                  to avoid infinite recursion if language not found.
3375                  Construct the new compiler spec.  */
3376               language = cp->spec + 1;
3377               new = (struct compiler *) xmalloc (sizeof (struct compiler));
3378               new->suffix = cp->suffix;
3379               new->spec = lookup_compiler (NULL_PTR, 0, language)->spec;
3380               return new;
3381             }
3382           /* A non-alias entry: return it.  */
3383           return cp;
3384         }
3385     }
3386
3387   return 0;
3388 }
3389 \f
3390 char *
3391 xmalloc (size)
3392      unsigned size;
3393 {
3394   register char *value = (char *) malloc (size);
3395   if (value == 0)
3396     fatal ("virtual memory exhausted");
3397   return value;
3398 }
3399
3400 char *
3401 xrealloc (ptr, size)
3402      char *ptr;
3403      unsigned size;
3404 {
3405   register char *value = (char *) realloc (ptr, size);
3406   if (value == 0)
3407     fatal ("virtual memory exhausted");
3408   return value;
3409 }
3410
3411 /* Return a newly-allocated string whose contents concatenate those of s1, s2, s3.  */
3412
3413 static char *
3414 concat (s1, s2, s3)
3415      char *s1, *s2, *s3;
3416 {
3417   int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
3418   char *result = xmalloc (len1 + len2 + len3 + 1);
3419
3420   strcpy (result, s1);
3421   strcpy (result + len1, s2);
3422   strcpy (result + len1 + len2, s3);
3423   *(result + len1 + len2 + len3) = 0;
3424
3425   return result;
3426 }
3427
3428 static char *
3429 save_string (s, len)
3430      char *s;
3431      int len;
3432 {
3433   register char *result = xmalloc (len + 1);
3434
3435   bcopy (s, result, len);
3436   result[len] = 0;
3437   return result;
3438 }
3439
3440 static void
3441 pfatal_with_name (name)
3442      char *name;
3443 {
3444   char *s;
3445
3446   if (errno < sys_nerr)
3447     s = concat ("%s: ", sys_errlist[errno], "");
3448   else
3449     s = "cannot open %s";
3450   fatal (s, name);
3451 }
3452
3453 static void
3454 perror_with_name (name)
3455      char *name;
3456 {
3457   char *s;
3458
3459   if (errno < sys_nerr)
3460     s = concat ("%s: ", sys_errlist[errno], "");
3461   else
3462     s = "cannot open %s";
3463   error (s, name);
3464 }
3465
3466 static void
3467 perror_exec (name)
3468      char *name;
3469 {
3470   char *s;
3471
3472   if (errno < sys_nerr)
3473     s = concat ("installation problem, cannot exec %s: ",
3474                 sys_errlist[errno], "");
3475   else
3476     s = "installation problem, cannot exec %s";
3477   error (s, name);
3478 }
3479
3480 /* More 'friendly' abort that prints the line and file.
3481    config.h can #define abort fancy_abort if you like that sort of thing.  */
3482
3483 void
3484 fancy_abort ()
3485 {
3486   fatal ("Internal gcc abort.");
3487 }
3488 \f
3489 #ifdef HAVE_VPRINTF
3490
3491 /* Output an error message and exit */
3492
3493 static void
3494 fatal (va_alist)
3495      va_dcl
3496 {
3497   va_list ap;
3498   char *format;
3499
3500   va_start (ap);
3501   format = va_arg (ap, char *);
3502   fprintf (stderr, "%s: ", programname);
3503   vfprintf (stderr, format, ap);
3504   va_end (ap);
3505   fprintf (stderr, "\n");
3506   delete_temp_files ();
3507   exit (1);
3508 }
3509
3510 static void
3511 error (va_alist)
3512      va_dcl
3513 {
3514   va_list ap;
3515   char *format;
3516
3517   va_start (ap);
3518   format = va_arg (ap, char *);
3519   fprintf (stderr, "%s: ", programname);
3520   vfprintf (stderr, format, ap);
3521   va_end (ap);
3522
3523   fprintf (stderr, "\n");
3524 }
3525
3526 #else /* not HAVE_VPRINTF */
3527
3528 static void
3529 fatal (msg, arg1, arg2)
3530      char *msg, *arg1, *arg2;
3531 {
3532   error (msg, arg1, arg2);
3533   delete_temp_files ();
3534   exit (1);
3535 }
3536
3537 static void
3538 error (msg, arg1, arg2)
3539      char *msg, *arg1, *arg2;
3540 {
3541   fprintf (stderr, "%s: ", programname);
3542   fprintf (stderr, msg, arg1, arg2);
3543   fprintf (stderr, "\n");
3544 }
3545
3546 #endif /* not HAVE_VPRINTF */
3547
3548 \f
3549 static void
3550 validate_all_switches ()
3551 {
3552   struct compiler *comp;
3553   register char *p;
3554   register char c;
3555   struct spec_list *spec;
3556
3557   for (comp = compilers; comp->spec; comp++)
3558     {
3559       p = comp->spec;
3560       while (c = *p++)
3561         if (c == '%' && *p == '{')
3562           /* We have a switch spec.  */
3563           validate_switches (p + 1);
3564     }
3565
3566   /* look through the linked list of extra specs read from the specs file */
3567   for (spec = specs ; spec ; spec = spec->next)
3568     {
3569       p = spec->spec;
3570       while (c = *p++)
3571         if (c == '%' && *p == '{')
3572           /* We have a switch spec.  */
3573           validate_switches (p + 1);
3574     }
3575
3576   p = link_command_spec;
3577   while (c = *p++)
3578     if (c == '%' && *p == '{')
3579       /* We have a switch spec.  */
3580       validate_switches (p + 1);
3581
3582   /* Now notice switches mentioned in the machine-specific specs.  */
3583
3584   p = asm_spec;
3585   while (c = *p++)
3586     if (c == '%' && *p == '{')
3587       /* We have a switch spec.  */
3588       validate_switches (p + 1);
3589
3590   p = asm_final_spec;
3591   while (c = *p++)
3592     if (c == '%' && *p == '{')
3593       /* We have a switch spec.  */
3594       validate_switches (p + 1);
3595
3596   p = cpp_spec;
3597   while (c = *p++)
3598     if (c == '%' && *p == '{')
3599       /* We have a switch spec.  */
3600       validate_switches (p + 1);
3601
3602   p = signed_char_spec;
3603   while (c = *p++)
3604     if (c == '%' && *p == '{')
3605       /* We have a switch spec.  */
3606       validate_switches (p + 1);
3607
3608   p = cc1_spec;
3609   while (c = *p++)
3610     if (c == '%' && *p == '{')
3611       /* We have a switch spec.  */
3612       validate_switches (p + 1);
3613
3614   p = cc1plus_spec;
3615   while (c = *p++)
3616     if (c == '%' && *p == '{')
3617       /* We have a switch spec.  */
3618       validate_switches (p + 1);
3619
3620   p = link_spec;
3621   while (c = *p++)
3622     if (c == '%' && *p == '{')
3623       /* We have a switch spec.  */
3624       validate_switches (p + 1);
3625
3626   p = lib_spec;
3627   while (c = *p++)
3628     if (c == '%' && *p == '{')
3629       /* We have a switch spec.  */
3630       validate_switches (p + 1);
3631
3632   p = startfile_spec;
3633   while (c = *p++)
3634     if (c == '%' && *p == '{')
3635       /* We have a switch spec.  */
3636       validate_switches (p + 1);
3637 }
3638
3639 /* Look at the switch-name that comes after START
3640    and mark as valid all supplied switches that match it.  */
3641
3642 static void
3643 validate_switches (start)
3644      char *start;
3645 {
3646   register char *p = start;
3647   char *filter;
3648   register int i;
3649   int suffix = 0;
3650
3651   if (*p == '|')
3652     ++p;
3653
3654   if (*p == '!')
3655     ++p;
3656
3657   if (*p == '.')
3658     suffix = 1, ++p;
3659
3660   filter = p;
3661   while (*p != ':' && *p != '}') p++;
3662
3663   if (suffix)
3664     ;
3665   else if (p[-1] == '*')
3666     {
3667       /* Mark all matching switches as valid.  */
3668       --p;
3669       for (i = 0; i < n_switches; i++)
3670         if (!strncmp (switches[i].part1, filter, p - filter))
3671           switches[i].valid = 1;
3672     }
3673   else
3674     {
3675       /* Mark an exact matching switch as valid.  */
3676       for (i = 0; i < n_switches; i++)
3677         {
3678           if (!strncmp (switches[i].part1, filter, p - filter)
3679               && switches[i].part1[p - filter] == 0)
3680             switches[i].valid = 1;
3681         }
3682     }
3683 }