home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / benchmarks / itc / gcc / gcc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-08-30  |  30.6 KB  |  1,238 lines

  1. /* Compiler driver program that can handle many languages.
  2.    Copyright (C) 1987 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU CC.
  5.  
  6. GNU CC is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY.  No author or distributor
  8. accepts responsibility to anyone for the consequences of using it
  9. or for whether it serves any particular purpose or works at all,
  10. unless he says so in writing.  Refer to the GNU CC General Public
  11. License for full details.
  12.  
  13. Everyone is granted permission to copy, modify and redistribute
  14. GNU CC, but only under the conditions described in the
  15. GNU CC General Public License.   A copy of this license is
  16. supposed to have been given to you along with GNU CC so you
  17. can know your rights and responsibilities.  It should be in a
  18. file named COPYING.  Among other things, the copyright notice
  19. and this notice must be preserved on all copies.  */
  20.  
  21.  
  22. /* This program is the user interface to the C compiler and possibly to
  23. other compilers.  It is used because compilation is a complicated procedure
  24. which involves running several programs and passing temporary files between
  25. them, forwarding the users switches to those programs selectively,
  26. and deleting the temporary files at the end.
  27.  
  28. CC recognizes how to compile each input file by suffixes in the file names.
  29. Once it knows which kind of compilation to perform, the procedure for
  30. compilation is specified by a string called a "spec".
  31.  
  32. Specs are strings containing lines, each of which (if not blank)
  33. is made up of a program name, and arguments separated by spaces.
  34. The program name must be exact and start from root, since no path
  35. is searched and it is unreliable to depend on the current working directory.
  36. Redirection of input or output is not supported; the subprograms must
  37. accept filenames saying what files to read and write.
  38.  
  39. In addition, the specs can contain %-sequences to substitute variable text
  40. or for conditional text.  Here is a table of all defined %-sequences.
  41. Note that spaces are not generated automatically around the results of
  42. expanding these sequences; therefore, you can concatenate them together
  43. or with constant text in a single argument.
  44.  
  45.  %%    substitute one % into the program name or argument.
  46.  %i     substitute the name of the input file being processed.
  47.  %b     substitute the basename of the input file being processed.
  48.     This is the substring up to (and not including) the last period.
  49.  %g     substitute the temporary-file-name-base.  This is a string chosen
  50.     once per compilation.  Different temporary file names are made by
  51.     concatenation of constant strings on the end, as in `%g.s'.
  52.     %g also has the same effect of %d.
  53.  %d    marks the argument containing or following the %d as a
  54.     temporary file name, so that that file will be deleted if CC exits
  55.     successfully.  Unlike %g, this contributes no text to the argument.
  56.  %w    marks the argument containing or following the %w as the
  57.     "output file" of this compilation.  This puts the argument
  58.     into the sequence of arguments that %o will substitute later.
  59.  %o    substitutes the names of all the output files, with spaces
  60.     automatically placed around them.  You should write spaces
  61.     around the %o as well or the results are undefined.
  62.     %o is for use in the specs for running the linker.
  63.     Input files whose names have no recognized suffix are not compiled
  64.     at all, but they are included among the output files, so they will
  65.     be linked.
  66.  %p    substitutes the standard macro predefinitions for the
  67.     current target machine.  Use this when running cpp.
  68.  %s     current argument is the name of a library file of some sort.
  69.         Search for that file in a standard list of directories
  70.     and substitute the full pathname found.
  71.  %C    process target->compile_spec as a spec.  This gives target-specific
  72.     switches for the C compiler, including the name of the compiler.
  73.  %a     process target->asm_spec as a spec.
  74.         This allows specs.h to specify part of the spec for running the
  75.     assembler, including the name of the assembler.
  76.  %l     process target->link_spec as a spec.  Spec must start with name
  77.     of linker.
  78.  %L     process target->lib_spec as a spec.
  79.  %m    substitute the official name of the target machine (may be
  80.     different than the name specified in the -m switch, if there are
  81.     several unofficial names for the same machine).
  82.  %S     process target->startfile_spec as a spec.  A capital S is actually
  83.     used here.
  84.  %{S}   substitutes the -S switch, if that switch was given to CC.
  85.     If that switch was not specified, this substitutes nothing.
  86.     Here S is a metasyntactic variable.
  87.  %{S*}  substitutes all the switches specified to CC whose names start
  88.     with -S.  This is used for -o, -D, -I, etc; switches that take
  89.     arguments.  CC considers `-o foo' as being one switch whose
  90.     name starts with `o'.  %{o*} would substitute this text,
  91.     including the space; thus, two arguments would be generated.
  92.  %{S:X} substitutes X, but only if the -S switch was given to CC.
  93.  %{!S:X} substitutes X, but only if the -S switch was NOT given to CC.
  94.  
  95. The conditional text X in a %{S:X} or %{!S:X} construct may contain
  96. other nested % constructs or spaces, or even newlines.
  97. They are processed as usual, as described above.
  98.  
  99. Note that it is built into CC which switches take arguments and which
  100. do not.  You might think it would be useful to generalize this to
  101. allow each compiler's spec to say which switches take arguments.  But
  102. this cannot be done in a consistent fashion.  CC cannot even decide
  103. which input files have been specified without knowing which switches
  104. take arguments, and it must know which input files to compile in order
  105. to tell which compilers to run.
  106.  
  107. CC also knows implicitly that arguments starting in `-l' are to
  108. be treated as output files, and passed to the linker in their proper
  109. position among the other output files.
  110.  
  111. */
  112.  
  113. /* This defines which switches take arguments.  */
  114.  
  115. #define SWITCH_TAKES_ARG(CHAR)      \
  116.   ((CHAR) == 'D' || (CHAR) == 'U' || (CHAR) == 'o' \
  117.    || (CHAR) == 'e' || (CHAR) == 'T' || (CHAR) == 'u' \
  118.    || (CHAR) == 'I' || (CHAR) == 'Y' || (CHAR) == 'm' \
  119.    || (CHAR) == 'L' || (CHAR) == 'B')
  120.  
  121. #include <stdio.h>
  122. #include <sys/types.h>
  123. #include <signal.h>
  124. #include <sys/file.h>
  125. #include "obstack.h"
  126. #include "alloca.h"
  127.  
  128. #ifdef USG
  129. #define R_OK 4
  130. #define W_OK 2
  131. #define X_OK 1
  132. #define vfork fork
  133. #endif
  134.  
  135. #define obstack_chunk_alloc xmalloc
  136. #define obstack_chunk_free free
  137. extern int xmalloc ();
  138. extern void free ();
  139.  
  140. /* If a stage of compilation returns an exit status >= 1,
  141.    compilation of that file ceases.  */
  142.  
  143. #define MIN_FATAL_STATUS 1
  144.  
  145. /* This is the obstack which we use to allocate many strings.  */
  146.  
  147. struct obstack obstack;
  148.  
  149. char *handle_braces ();
  150. char *save_string ();
  151. char *concat ();
  152. int do_spec ();
  153. int do_spec_1 ();
  154. int give_string ();
  155. char *find_file ();
  156.  
  157. /*
  158.  * Generate tables containing specs for all known target machines:
  159.  */
  160.  
  161. #include "specs.h"
  162.  
  163. /* Record the names of temporary files we tell compilers to write,
  164.    and delete them at the end of the run.  */
  165.  
  166. /* This is the common prefix we use to make temp file names.
  167.    It is chosen once for each run of this program.
  168.    It is substituted into a spec by %g.
  169.    Thus, all temp file names contain this prefix.
  170.    In practice, all temp file names start with this prefix.
  171.    The prefix starts with `/tmp'.  */
  172.  
  173. char *temp_filename;
  174.  
  175. /* Length of the prefix.  */
  176.  
  177. int temp_filename_length;
  178.  
  179. /* Define the list of temporary files to delete.  */
  180.  
  181. struct temp_file
  182. {
  183.   char *name;
  184.   struct temp_file *next;
  185.   int success_only;        /* Nonzero means delete this file
  186.                    only if compilation succeeds fully.  */
  187. };
  188.  
  189. struct temp_file *temp_file_queue;
  190.  
  191. /* Record FILENAME as a file to be deleted automatically.
  192.    SUCCESS_ONLY nonzero means delete it only if all compilation succeeds;
  193.    otherwise delete it in any case.  */
  194.  
  195. void
  196. record_temp_file (filename, success_only)
  197.      char *filename;
  198.      int success_only;
  199. {
  200.   register struct temp_file *temp;
  201.   register char *name;
  202.   temp = (struct temp_file *) xmalloc (sizeof (struct temp_file));
  203.   name = (char *) xmalloc (strlen (filename) + 1);
  204.   strcpy (name, filename);
  205.   temp->next = temp_file_queue;
  206.   temp->name = name;
  207.   temp->success_only = success_only;
  208.   temp_file_queue = temp;
  209. }
  210.  
  211. /* Delete all the temporary files whose names we previously recorded.
  212.    SUCCESS nonzero means "delete on success only" files should be deleted.  */
  213.  
  214. void
  215. delete_temp_files (success)
  216.      int success;
  217. {
  218.   register struct temp_file *temp;
  219.   for (temp = temp_file_queue; temp; temp = temp->next)
  220.     if (success || ! temp->success_only)
  221.       {
  222. #ifdef DEBUG
  223.     int i;
  224.     printf ("Delete %s? (y or n) ", temp->name);
  225.     fflush (stdout);
  226.     i = getchar ();
  227.     if (i != '\n')
  228.       while (getchar () != '\n') ;
  229.     if (i == 'y' || i == 'Y')
  230. #endif /* DEBUG */
  231.       unlink (temp->name);
  232.       }
  233.   temp_file_queue = 0;
  234. }
  235.  
  236. /* Compute a string to use as the base of all temporary file names.
  237.    It is substituted for %g.  */
  238.  
  239. void
  240. choose_temp_base ()
  241. {
  242.   register char *foo = "/tmp/ccXXXXXX";
  243.   temp_filename = (char *) xmalloc (strlen (foo) + 1);
  244.   strcpy (temp_filename, foo);
  245.   mktemp (temp_filename);
  246.   temp_filename_length = strlen (temp_filename);
  247. }
  248.  
  249. /* Accumulate a command (program name and args), and run it.  */
  250.  
  251. /* Vector of pointers to arguments in the current line of specifications.  */
  252.  
  253. char **argbuf;
  254.  
  255. /* Number of elements allocated in argbuf.  */
  256.  
  257. int argbuf_length;
  258.  
  259. /* Number of elements in argbuf currently in use (containing args).  */
  260.  
  261. int argbuf_index;
  262.  
  263. /* Flag indicating whether we should print the command and arguments */
  264.  
  265. unsigned char vflag;
  266.  
  267. /* User-specified prefix to attach to command names,
  268.    or 0 if none specified.  */
  269.  
  270. char *user_exec_prefix = 0;
  271.  
  272. /* Default prefixes to attach to command names.  If a name contains a
  273.    "%s", then it will be replaced with the type of machine on which
  274.    we're running, to get the correct binaries for this machine. */
  275.  
  276. char *standard_exec_prefix = "/sprite/cmds.%s/";
  277. char *standard_exec_prefix_1 = "/sprite/lib/gcc/%s.md/";
  278.  
  279. char *standard_startfile_prefix = "/sprite/lib/%s.md/";
  280. char *standard_startfile_prefix_1 = "/sprite/lib/gcc/%s.md/";
  281.  
  282. /* Type of machine on which we're running (used to select correct
  283.    binaries for programs like cpp). */
  284.  
  285. char *machine;
  286. int machine_length;        /* No. of characters in machine. */
  287.  
  288. /* Clear out the vector of arguments (after a command is executed).  */
  289.  
  290. void
  291. clear_args ()
  292. {
  293.   argbuf_index = 0;
  294. }
  295.  
  296. /* Add one argument to the vector at the end.
  297.    This is done when a space is seen or at the end of the line.
  298.    If TEMPNAMEP is nonzero, this arg is a file that should be deleted
  299.    at the end of compilation.  (If TEMPNAMEP is 2, delete the file
  300.    only if compilation is fully successful.)  */
  301.  
  302. void
  303. store_arg (arg, tempnamep)
  304.      char *arg;
  305.      int tempnamep;
  306. {
  307.   if (argbuf_index + 1 == argbuf_length)
  308.     {
  309.       argbuf = (char **) realloc (argbuf, (argbuf_length *= 2) * sizeof (char *));
  310.     }
  311.  
  312.   argbuf[argbuf_index++] = arg;
  313.   argbuf[argbuf_index] = 0;
  314.  
  315.   if (tempnamep)
  316.     record_temp_file (arg, tempnamep == 2);
  317. }
  318.  
  319. /* Execute the command specified by the arguments on the current line of spec.
  320.    Returns 0 if successful, -1 if failed.  */
  321.  
  322. int
  323. execute ()
  324. {
  325.   int pid;
  326.   int status;
  327.   int size;
  328.   char *temp;
  329.   int win = 0;
  330.  
  331.   size = strlen (standard_exec_prefix);
  332.   if (user_exec_prefix != 0 && strlen (user_exec_prefix) > size)
  333.     size = strlen (user_exec_prefix);
  334.   if (strlen (standard_exec_prefix_1) > size)
  335.     size = strlen (standard_exec_prefix_1);
  336.   size += strlen (argbuf[0]) + machine_length + 1;
  337.   temp = (char *) alloca (size);
  338.  
  339.   /* Determine the filename to execute.  */
  340.  
  341.   if (user_exec_prefix)
  342.     {
  343.       sprintf (temp, user_exec_prefix, machine);
  344.       strcat (temp, argbuf[0]);
  345.       win = (access (temp, X_OK) == 0);
  346.     }
  347.  
  348.   if (!win)
  349.     {
  350.       sprintf (temp, standard_exec_prefix, machine);
  351.       strcat (temp, argbuf[0]);
  352.       win = (access (temp, X_OK) == 0);
  353.     }
  354.  
  355.   if (!win)
  356.     {
  357.       sprintf (temp, standard_exec_prefix_1, machine);
  358.       strcat (temp, argbuf[0]);
  359.       win = (access (temp, X_OK) == 0);
  360.     }
  361.  
  362.   if (vflag)
  363.     {
  364.       int i;
  365.       for (i = 0; argbuf[i]; i++)
  366.     {
  367.       if (i == 0 && win)
  368.         fprintf (stderr, " %s", temp);
  369.       else
  370.         fprintf (stderr, " %s", argbuf[i]);
  371.     }
  372.       fprintf (stderr, "\n");
  373.       fflush (stderr);
  374. #ifdef DEBUG
  375.       fprintf (stderr, "\nGo ahead? (y or n) ");
  376.       fflush (stderr);
  377.       i = getchar ();
  378.       if (i != '\n')
  379.     while (getchar () != '\n') ;
  380.       if (i != 'y' && i != 'Y')
  381.     return 0;
  382. #endif                /* DEBUG */
  383.     }
  384.  
  385. #ifdef USG
  386.   pid = fork ();
  387.   if (pid < 0)
  388.     pfatal_with_name ("fork");
  389. #else
  390.   pid = vfork ();
  391.   if (pid < 0)
  392.     pfatal_with_name ("vfork");
  393. #endif
  394.   if (pid == 0)
  395.     {
  396.       if (win)
  397.     execv (temp, argbuf);
  398.       else
  399.     execvp (argbuf[0], argbuf);
  400.       perror_with_name (argbuf[0]);
  401.       _exit (65);
  402.     }
  403.   wait (&status);
  404.   if ((status & 0x7F) != 0)
  405.     fatal ("Program %s got fatal signal %d.", argbuf[0], (status & 0x7F));
  406.   if (((status & 0xFF00) >> 8) >= MIN_FATAL_STATUS)
  407.     return -1;
  408.   return 0;
  409. }
  410.  
  411. /* Find all the switches given to us
  412.    and make a vector describing them.
  413.    The elements of the vector a strings, one per switch given.
  414.    If a switch uses the following argument, then the `part1' field
  415.    is the switch itself and the `part2' field is the following argument.  */
  416.  
  417. struct switchstr
  418. {
  419.   char *part1;
  420.   char *part2;
  421. };
  422.  
  423. struct switchstr *switches;
  424.  
  425. int n_switches;
  426.  
  427. /* Also a vector of input files specified.  */
  428.  
  429. char **infiles;
  430.  
  431. int n_infiles;
  432.  
  433. /* And a vector of corresponding output files is made up later.  */
  434.  
  435. char **outfiles;
  436.  
  437. char *
  438. make_switch (p1, s1, p2, s2)
  439.      char *p1;
  440.      int s1;
  441.      char *p2;
  442.      int s2;
  443. {
  444.   register char *new;
  445.   if (p2 && s2 == 0)
  446.     s2 = strlen (p2);
  447.   new = (char *) xmalloc (s1 + s2 + 2);
  448.   bcopy (p1, new, s1);
  449.   if (p2)
  450.     {
  451.       new[s1++] = ' ';
  452.       bcopy (p2, new + s1, s2);
  453.     }
  454.   new[s1 + s2] = 0;
  455.   return new;
  456. }
  457.  
  458. void
  459. set_target_machine(name)
  460.     char *name;
  461. {
  462.   register target_machine *t;
  463.   int i;
  464.  
  465.   for (i = 0; ; i++)
  466.     {
  467.       t = &target_machines[i];
  468.       if (t->name == 0)
  469.         {
  470.       break;
  471.     }
  472.       if (strcmp(t->name, name) == 0)
  473.         {
  474.       target_name = t->info->name;
  475.       target = t->info;
  476.       return;
  477.     }
  478.     }
  479. }
  480.  
  481. /* Create the vector `switches' and its contents.
  482.    Store its length in `n_switches'.  */
  483.  
  484. void
  485. process_command (argc, argv)
  486.      int argc;
  487.      char **argv;
  488. {
  489.   register int i;
  490.   n_switches = 0;
  491.   n_infiles = 0;
  492.  
  493.   /* Scan argv twice.  Here, the first time, just count how many switches
  494.      there will be in their vector, and how many input files in theirs.
  495.      Here we also parse the switches that cc itself uses (e.g. -v, -m).  */
  496.  
  497.   for (i = 1; i < argc; i++)
  498.     {
  499.       if (argv[i][0] == '-' && argv[i][1] != 'l')
  500.     {
  501.       register char *p = &argv[i][1];
  502.       register int c = *p;
  503.  
  504.       p++;
  505.       if (SWITCH_TAKES_ARG (c) && *p == 0)
  506.         {
  507.           i++;
  508.           p = argv[i];
  509.           if (p == NULL)
  510.             p = "";
  511.         }
  512.  
  513.       switch (c)
  514.         {
  515.         case 'B':
  516.           user_exec_prefix = p;
  517.           break;
  518.  
  519.         case 'v':    /* Print our subcommands and print versions.  */
  520.           vflag++;
  521.           n_switches++;
  522.           break;
  523.  
  524.         case 'm':
  525.           /*
  526.            * See if this switch identifies a known target machine.
  527.            * If so, use it to set the list of specs for the target.
  528.            */
  529.           set_target_machine(p);
  530.           n_switches++;
  531.           break;
  532.  
  533.         default:
  534.           n_switches++;
  535.  
  536.         }
  537.     }
  538.       else
  539.     n_infiles++;
  540.     }
  541.  
  542.   /* Then create the space for the vectors and scan again.  */
  543.  
  544.   switches = ((struct switchstr *)
  545.           xmalloc ((n_switches + 1) * sizeof (struct switchstr)));
  546.   infiles = (char **) xmalloc ((n_infiles + 1) * sizeof (char *));
  547.   n_switches = 0;
  548.   n_infiles = 0;
  549.  
  550.   /* This, time, copy the text of each switch and store a pointer
  551.      to the copy in the vector of switches.
  552.      Store all the infiles in their vector.  */
  553.  
  554.   for (i = 1; i < argc; i++)
  555.     {
  556.       if (argv[i][0] == '-' && argv[i][1] != 'l')
  557.     {
  558.       register char *p = &argv[i][1];
  559.       register int c = *p;
  560.  
  561.       switches[n_switches].part1 = p;
  562.       if (SWITCH_TAKES_ARG (c) && p[1] == 0)
  563.         switches[n_switches].part2 = argv[++i];
  564.       else
  565.         switches[n_switches].part2 = 0;
  566.       if (c != 'B')
  567.         n_switches++;
  568.     }
  569.       else
  570.     infiles[n_infiles++] = argv[i];
  571.     }
  572.  
  573.   switches[n_switches].part1 = 0;
  574.   infiles[n_infiles] = 0;
  575. }
  576.  
  577. /* Process a spec string, accumulating and running commands.  */
  578.  
  579. /* These variables describe the input file name.
  580.    input_file_number is the index on outfiles of this file,
  581.    so that the output file name can be stored for later use by %o.
  582.    input_basename is the start of the part of the input file
  583.    sans all directory names, and basename_length is the number
  584.    of characters starting there excluding the suffix .c or whatever.  */
  585.  
  586. char *input_filename;
  587. int input_file_number;
  588. int input_filename_length;
  589. int basename_length;
  590. char *input_basename;
  591.  
  592. /* These are variables used within do_spec and do_spec_1.  */
  593.  
  594. /* Nonzero if an arg has been started and not yet terminated
  595.    (with space, tab or newline).  */
  596. int arg_going;
  597.  
  598. /* Nonzero means %d or %g has been seen; the next arg to be terminated
  599.    is a temporary file name.  */
  600. int delete_this_arg;
  601.  
  602. /* Nonzero means %w has been seen; the next arg to be terminated
  603.    is the output file name of this compilation.  */
  604. int this_is_output_file;
  605.  
  606. /* Nonzero means %s has been seen; the next arg to be terminated
  607.    is the name of a library file and we should try the standard
  608.    search dirs for it.  */
  609. int this_is_library_file;
  610.  
  611. /* Process the spec SPEC and run the commands specified therein.
  612.    Returns 0 if the spec is successfully processed; -1 if failed.  */
  613.  
  614. int
  615. do_spec (spec)
  616.      char *spec;
  617. {
  618.   int value;
  619.  
  620.   clear_args ();
  621.   arg_going = 0;
  622.   delete_this_arg = 0;
  623.   this_is_output_file = 0;
  624.   this_is_library_file = 0;
  625.  
  626.   value = do_spec_1 (spec, 0);
  627.   if (value == 0)
  628.     value = do_spec_1 ("\n", 0);
  629.   return value;
  630. }
  631.  
  632. /* Process the sub-spec SPEC as a portion of a larger spec.
  633.    This is like processing a whole spec except that we do
  634.    not initialize at the beginning and we do not supply a
  635.    newline by default at the end.
  636.    INSWITCH nonzero means don't process %-sequences in SPEC;
  637.    in this case, % is treated as an ordinary character.
  638.    This is used while substituting switches.
  639.    INSWITCH nonzero also causes SPC not to terminate an argument.
  640.  
  641.    Value is zero unless a line was finished
  642.    and the command on that line reported an error.  */
  643.  
  644. int
  645. do_spec_1 (spec, inswitch)
  646.      char *spec;
  647.      int inswitch;
  648. {
  649.   register char *p = spec;
  650.   register int c;
  651.   char *string;
  652.  
  653.   while (c = *p++)
  654.     /* If substituting a switch, treat all chars like letters.
  655.        Otherwise, NL, SPC, TAB and % are special.  */
  656.     switch (inswitch ? 'a' : c)
  657.       {
  658.       case '\n':
  659.     /* End of line: finish any pending argument,
  660.        then run the pending command if one has been started.  */
  661.     if (arg_going)
  662.       {
  663.         obstack_1grow (&obstack, 0);
  664.         string = obstack_finish (&obstack);
  665.         if (this_is_library_file)
  666.           string = find_file (string);
  667.         store_arg (string, delete_this_arg);
  668.         if (this_is_output_file)
  669.           outfiles[input_file_number] = string;
  670.       }
  671.     arg_going = 0;
  672.     if (argbuf_index)
  673.       {
  674.         int value = execute ();
  675.         if (value)
  676.           return value;
  677.       }
  678.     /* Reinitialize for a new command, and for a new argument.  */
  679.     clear_args ();
  680.     arg_going = 0;
  681.     delete_this_arg = 0;
  682.     this_is_output_file = 0;
  683.     this_is_library_file = 0;
  684.     break;
  685.  
  686.       case '\t':
  687.       case ' ':
  688.     /* Space or tab ends an argument if one is pending.  */
  689.     if (arg_going)
  690.       {
  691.         obstack_1grow (&obstack, 0);
  692.         string = obstack_finish (&obstack);
  693.         if (this_is_library_file)
  694.           string = find_file (string);
  695.         store_arg (string, delete_this_arg);
  696.         if (this_is_output_file)
  697.           outfiles[input_file_number] = string;
  698.       }
  699.     /* Reinitialize for a new argument.  */
  700.     arg_going = 0;
  701.     delete_this_arg = 0;
  702.     this_is_output_file = 0;
  703.     this_is_library_file = 0;
  704.     break;
  705.  
  706.       case '%':
  707.     switch (c = *p++)
  708.       {
  709.       case 0:
  710.         fatal ("Invalid specification!  Bug in cc.");
  711.  
  712.       case 'i':
  713.         obstack_grow (&obstack, input_filename, input_filename_length);
  714.         arg_going = 1;
  715.         break;
  716.  
  717.       case 'b':
  718.         obstack_grow (&obstack, input_basename, basename_length);
  719.         arg_going = 1;
  720.         break;
  721.  
  722.       case 'p':
  723.         do_spec_1 (target->cpp_predefines, 0);
  724.         break;
  725.  
  726.       case 'g':
  727.         obstack_grow (&obstack, temp_filename, temp_filename_length);
  728.         delete_this_arg = 1;
  729.         arg_going = 1;
  730.         break;
  731.  
  732.       case 'm':
  733.           obstack_grow (&obstack, target_name, strlen (target_name));
  734.           arg_going = 1;
  735.           break;
  736.  
  737.       case 'd':
  738.         delete_this_arg = 2;
  739.         break;
  740.  
  741.       case 'w':
  742.         this_is_output_file = 1;
  743.         break;
  744.  
  745.       case 's':
  746.         this_is_library_file = 1;
  747.         break;
  748.  
  749.       case 'o':
  750.         {
  751.           register int f;
  752.           for (f = 0; f < n_infiles; f++)
  753.         store_arg (outfiles[f], 0);
  754.         }
  755.         break;
  756.  
  757.       case 'a':
  758.         do_spec_1 (target->asm_spec, 0);
  759.         break;
  760.  
  761.       case 'C':
  762.         do_spec_1 (target->compile_spec, 0);
  763.         break;
  764.  
  765.       case 'l':
  766.         do_spec_1 (target->link_spec, 0);
  767.         break;
  768.  
  769.       case 'L':
  770.         do_spec_1 (target->lib_spec, 0);
  771.         break;
  772.  
  773.       case 'S':
  774.         do_spec_1 (target->startfile_spec, 0);
  775.         break;
  776.  
  777.       case '{':
  778.         p = handle_braces (p);
  779.         if (p == 0)
  780.           return -1;
  781.         break;
  782.  
  783.       case '%':
  784.         obstack_1grow (&obstack, '%');
  785.         break;
  786.  
  787.       default:
  788.         fatal ("Invalid spec sequence \"%%%c\"!  Bug in cc.", c);
  789.       }
  790.     break;
  791.  
  792.       default:
  793.     /* Ordinary character: put it into the current argument.  */
  794.     obstack_1grow (&obstack, c);
  795.     arg_going = 1;
  796.       }
  797.  
  798.   return 0;        /* End of string */
  799. }
  800.  
  801. /* Return 0 if we call do_spec_1 and that returns -1.  */
  802.  
  803. char *
  804. handle_braces (p)
  805.      register char *p;
  806. {
  807.   register char *q;
  808.   int negate = *p == '!';
  809.   char *filter;
  810.  
  811.   if (negate) ++p;
  812.  
  813.   filter = p;
  814.   while (*p != ':' && *p != '}') p++;
  815.   if (*p != '}')
  816.     {
  817.       register int count = 1;
  818.       q = p + 1;
  819.       while (count > 0)
  820.     {
  821.       if (*q == '{')
  822.         count++;
  823.       else if (*q == '}')
  824.         count--;
  825.       else if (*q == 0)
  826.         abort ();
  827.       q++;
  828.     }
  829.     }
  830.   else
  831.     q = p + 1;
  832.  
  833.   if (p[-1] == '*' && p[0] == '}')
  834.     {
  835.       /* Substitute all matching switches as separate args.  */
  836.       register int i;
  837.       --p;
  838.       for (i = 0; i < n_switches; i++)
  839.     if (!strncmp (switches[i].part1, filter, p - filter))
  840.       {
  841.         give_switch (i);
  842.       }
  843.     }
  844.   else
  845.     {
  846.       /* Test for presence of the specified switch.  */
  847.       register int i;
  848.       int present = 0;
  849.  
  850.       /* If name specified ends in *, as in {x*:...},
  851.      check for presence of any switch name starting with x.  */
  852.       if (p[-1] == '*')
  853.     {
  854.       for (i = 0; i < n_switches; i++)
  855.         {
  856.           if (!strncmp (switches[i].part1, filter, p - filter - 1))
  857.         {
  858.           present = 1;
  859.           break;
  860.         }
  861.         }
  862.     }
  863.       /* Otherwise, check for presence of exact name specified.  */
  864.       else
  865.     {
  866.       for (i = 0; i < n_switches; i++)
  867.         {
  868.           if (!strncmp (switches[i].part1, filter, p - filter)
  869.           && switches[i].part1[p - filter] == 0)
  870.         {
  871.           present = 1;
  872.           break;
  873.         }
  874.         }
  875.     }
  876.  
  877.       /* If it is as desired (present for %{s...}, absent for %{-s...})
  878.      then substitute either the switch or the specified
  879.      conditional text.  */
  880.       if (present != negate)
  881.     {
  882.       if (*p == '}')
  883.         {
  884.           give_switch (i);
  885.         }
  886.       else
  887.         {
  888.           if (do_spec_1 (save_string (p + 1, q - p - 2), 0) < 0)
  889.         return 0;
  890.         }
  891.     }
  892.     }
  893.  
  894.   return q;
  895. }
  896.  
  897. /* Pass a switch to the current accumulating command
  898.    in the same form that we received it.
  899.    SWITCHNUM identifies the switch; it is an index into
  900.    the vector of switches gcc received, which is `switches'.
  901.    This cannot fail since it never finishes a command line.  */
  902.  
  903. give_switch (switchnum)
  904.      int switchnum;
  905. {
  906.   do_spec_1 ("-", 0);
  907.   do_spec_1 (switches[switchnum].part1, 1);
  908.   do_spec_1 (" ", 0);
  909.   if (switches[switchnum].part2 != 0)
  910.     {
  911.       do_spec_1 (switches[switchnum].part2, 1);
  912.       do_spec_1 (" ", 0);
  913.     }
  914. }
  915.  
  916. /* Search for a file named NAME trying various prefixes including the
  917.    user's -B prefix and some standard ones.
  918.    Return the absolute pathname found.  If nothing is found, return NAME.  */
  919.  
  920. char *
  921. find_file (name)
  922.      char *name;
  923. {
  924.   int size;
  925.   char *temp;
  926.   int win = 0;
  927.  
  928.   /* Compute maximum size of NAME plus any prefix we will try.  */
  929.  
  930.   size = strlen (standard_exec_prefix);
  931.   if (user_exec_prefix != 0 && strlen (user_exec_prefix) > size)
  932.     size = strlen (user_exec_prefix);
  933.   if (strlen (standard_exec_prefix_1) > size)
  934.     size = strlen (standard_exec_prefix_1);
  935.   if (strlen (standard_startfile_prefix) > size)
  936.     size = strlen (standard_startfile_prefix);
  937.   if (strlen (standard_startfile_prefix_1) > size)
  938.     size = strlen (standard_startfile_prefix_1);
  939.   size += strlen (name) + strlen(target_name) + 1;
  940.  
  941.   temp = (char *) alloca (size);
  942.  
  943.   if (user_exec_prefix)
  944.     {
  945.       sprintf (temp, user_exec_prefix, target_name);
  946.       strcat (temp, name);
  947.       win = (access (temp, R_OK) == 0);
  948.     }
  949.  
  950.   if (!win)
  951.     {
  952.       sprintf (temp, standard_exec_prefix, target_name);
  953.       strcat (temp, name);
  954.       win = (access (temp, R_OK) == 0);
  955.     }
  956.  
  957.   if (!win)
  958.     {
  959.       sprintf (temp, standard_exec_prefix_1, target_name);
  960.       strcat (temp, name);
  961.       win = (access (temp, R_OK) == 0);
  962.     }
  963.  
  964.   if (!win)
  965.     {
  966.       strcpy (temp, standard_startfile_prefix);
  967.       strcat (temp, name);
  968.       win = (access (temp, R_OK) == 0);
  969.     }
  970.  
  971.   if (!win)
  972.     {
  973.       strcpy (temp, standard_startfile_prefix_1);
  974.       strcat (temp, name);
  975.       win = (access (temp, R_OK) == 0);
  976.     }
  977.  
  978.   if (!win)
  979.     {
  980.       strcpy (temp, "./");
  981.       strcat (temp, name);
  982.       win = (access (temp, R_OK) == 0);
  983.     }
  984.  
  985.   if (win)
  986.     return save_string (temp, strlen (temp));
  987.   return name;
  988. }
  989.  
  990. /* Name with which this program was invoked.  */
  991.  
  992. char *programname;
  993.  
  994. /* On fatal signals, delete all the temporary files.  */
  995.  
  996. void
  997. fatal_error (signum)
  998.      int signum;
  999. {
  1000.   signal (signum, SIG_DFL);
  1001.   delete_temp_files (0);
  1002.   /* Get the same signal again, this time not handled,
  1003.      so its normal effect occurs.  */
  1004.   kill (getpid (), signum);
  1005. }
  1006.  
  1007. int
  1008. main (argc, argv)
  1009.      int argc;
  1010.      char **argv;
  1011. {
  1012.   register int i;
  1013.   int value;
  1014.   int nolink = 0;
  1015.   int error = 0;
  1016.   extern char *getenv();
  1017.  
  1018.   programname = argv[0];
  1019.  
  1020.   if (signal (SIGINT, SIG_IGN) != SIG_IGN)
  1021.     signal (SIGINT, fatal_error);
  1022.   if (signal (SIGHUP, SIG_IGN) != SIG_IGN)
  1023.     signal (SIGHUP, fatal_error);
  1024.   if (signal (SIGTERM, SIG_IGN) != SIG_IGN)
  1025.     signal (SIGTERM, fatal_error);
  1026.  
  1027.   argbuf_length = 10;
  1028.   argbuf = (char **) xmalloc (argbuf_length * sizeof (char *));
  1029.  
  1030.   obstack_init (&obstack);
  1031.  
  1032.   choose_temp_base ();
  1033.  
  1034.   /* Make a table of what switches there are (switches, n_switches).
  1035.      Make a table of specified input files (infiles, n_infiles).  */
  1036.  
  1037.   process_command (argc, argv);
  1038.  
  1039.   /* Figure out the machine type we're running on, using the $MACHINE
  1040.      environment variable. */
  1041.  
  1042.   machine = getenv("MACHINE");
  1043.   if (machine == NULL)
  1044.     {
  1045.       machine = "";
  1046.     }
  1047.   machine_length = strlen(machine);
  1048.  
  1049.   /* If the target machine wasn't specified in a switch, then get it from
  1050.      the TM environment variable, if it exists, or next from the MACHINE
  1051.      environment variable.  If none of these exists, pick a default. */
  1052.  
  1053.   if (target == NULL)
  1054.     {
  1055.       char *env;
  1056.  
  1057.       env = getenv ("TM");
  1058.       if (env == NULL)
  1059.         {
  1060.       env = getenv ("MACHINE");
  1061.       if (env == NULL)
  1062.         {
  1063.           env = target_machines[0].name;
  1064.         }
  1065.     }
  1066.       set_target_machine (env);
  1067.     }
  1068.  
  1069.   if (vflag)
  1070.     {
  1071.       extern char *version_string;
  1072.       printf ("gcc version %s\n", version_string);
  1073.       printf ("target machine is %s\n", target_name);
  1074.       if (n_infiles == 0)
  1075.     exit (0);
  1076.     }
  1077.  
  1078.   if (n_infiles == 0)
  1079.     fatal ("No source or object files specified.");
  1080.  
  1081.   /* Make a place to record the compiler output file names
  1082.      that correspond to the input files.  */
  1083.  
  1084.   outfiles = (char **) xmalloc (n_infiles * sizeof (char *));
  1085.   bzero (outfiles, n_infiles * sizeof (char *));
  1086.  
  1087.   for (i = 0; i < n_infiles; i++)
  1088.     {
  1089.       /* First figure out which compiler from the file's suffix.  */
  1090.       
  1091.       register struct compiler *cp;
  1092.  
  1093.       /* Tell do_spec what to substitute for %i.  */
  1094.  
  1095.       input_filename = infiles[i];
  1096.       input_filename_length = strlen (input_filename);
  1097.       input_file_number = i;
  1098.  
  1099.       /* Use the same thing in %o, unless cp->spec says otherwise.  */
  1100.  
  1101.       outfiles[i] = input_filename;
  1102.  
  1103.       for (cp = target->base_specs; cp->spec; cp++)
  1104.     {
  1105.       if (strlen (cp->suffix) < input_filename_length
  1106.           && !strcmp (cp->suffix,
  1107.               infiles[i] + input_filename_length
  1108.               - strlen (cp->suffix)))
  1109.         {
  1110.           /* Ok, we found an applicable compiler.  Run its spec.  */
  1111.           /* First say how much of input_filename to substitute for %b  */
  1112.           register char *p;
  1113.  
  1114.           input_basename = input_filename;
  1115.           for (p = input_filename; *p; p++)
  1116.         if (*p == '/')
  1117.           input_basename = p + 1;
  1118.           basename_length = (input_filename_length - strlen (cp->suffix)
  1119.                  - (input_basename - input_filename));
  1120.           value = do_spec (cp->spec);
  1121.           if (value < 0)
  1122.         error = 1;
  1123.           break;
  1124.         }
  1125.     }
  1126.  
  1127.       /* If this file's name does not contain a recognized suffix,
  1128.      don't do anything to it, but do feed it to the link spec
  1129.      since its name is in outfiles.  */
  1130.     }
  1131.  
  1132.   /* Run ld to link all the compiler output files.  */
  1133.  
  1134.   if (! nolink && error == 0)
  1135.     {
  1136.       value = do_spec (target->link_base_spec);
  1137.       if (value < 0)
  1138.     error = 1;
  1139.     }
  1140.  
  1141.   /* Delete some or all of the temporary files we made.  */
  1142.  
  1143.   delete_temp_files (error == 0);
  1144.  
  1145.   exit (error);
  1146. }
  1147.  
  1148. xmalloc (size)
  1149.      int size;
  1150. {
  1151.   register int value = malloc (size);
  1152.   if (value == 0)
  1153.     fatal ("Virtual memory full.");
  1154.   return value;
  1155. }
  1156.  
  1157. xrealloc (ptr, size)
  1158.      int ptr, size;
  1159. {
  1160.   register int value = realloc (ptr, size);
  1161.   if (value == 0)
  1162.     fatal ("Virtual memory full.");
  1163.   return value;
  1164. }
  1165.  
  1166. fatal (msg, arg1, arg2)
  1167.      char *msg, *arg1, *arg2;
  1168. {
  1169.   error (msg, arg1, arg2);
  1170.   delete_temp_files (0);
  1171.   exit (1);
  1172. }
  1173.  
  1174. error (msg, arg1, arg2)
  1175.      char *msg, *arg1, *arg2;
  1176. {
  1177.   fprintf (stderr, "%s: ", programname);
  1178.   fprintf (stderr, msg, arg1, arg2);
  1179.   fprintf (stderr, "\n");
  1180. }
  1181.  
  1182. /* Return a newly-allocated string whose contents concatenate those of s1, s2, s3.  */
  1183.  
  1184. char *
  1185. concat (s1, s2, s3)
  1186.      char *s1, *s2, *s3;
  1187. {
  1188.   int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
  1189.   char *result = (char *) xmalloc (len1 + len2 + len3 + 1);
  1190.  
  1191.   strcpy (result, s1);
  1192.   strcpy (result + len1, s2);
  1193.   strcpy (result + len1 + len2, s3);
  1194.   *(result + len1 + len2 + len3) = 0;
  1195.  
  1196.   return result;
  1197. }
  1198.  
  1199. char *
  1200. save_string (s, len)
  1201.      char *s;
  1202.      int len;
  1203. {
  1204.   register char *result = (char *) xmalloc (len + 1);
  1205.  
  1206.   bcopy (s, result, len);
  1207.   result[len] = 0;
  1208.   return result;
  1209. }
  1210.  
  1211. pfatal_with_name (name)
  1212.      char *name;
  1213. {
  1214.   extern int errno, sys_nerr;
  1215.   extern char *sys_errlist[];
  1216.   char *s;
  1217.  
  1218.   if (errno < sys_nerr)
  1219.     s = concat ("", sys_errlist[errno], " for %s");
  1220.   else
  1221.     s = "cannot open %s";
  1222.   fatal (s, name);
  1223. }
  1224.  
  1225. perror_with_name (name)
  1226.      char *name;
  1227. {
  1228.   extern int errno, sys_nerr;
  1229.   extern char *sys_errlist[];
  1230.   char *s;
  1231.  
  1232.   if (errno < sys_nerr)
  1233.     s = concat ("", sys_errlist[errno], " for %s");
  1234.   else
  1235.     s = "cannot open %s";
  1236.   error (s, name);
  1237. }
  1238.