home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 7 / FreshFishVol7.bin / bbs / gnu / gcc-2.3.3-src.lha / GNU / src / amiga / gcc-2.3.3 / gcc.c < prev    next >
C/C++ Source or Header  |  1994-02-06  |  108KB  |  3,992 lines

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