OSDN Git Service

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