home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / gnat-2.06-src.tgz / tar.out / fsf / gnat / gcc.c < prev    next >
C/C++ Source or Header  |  1996-09-28  |  141KB  |  5,204 lines

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