OSDN Git Service

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