OSDN Git Service

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