OSDN Git Service

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