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