home *** CD-ROM | disk | FTP | other *** search
/ OpenStep 4.2J (Developer) / os42jdev.iso / NextDeveloper / Source / GNU / gcc / gcc.c < prev    next >
C/C++ Source or Header  |  1997-02-08  |  172KB  |  6,342 lines

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