OSDN Git Service

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