OSDN Git Service

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