OSDN Git Service

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