gcc.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963
  1. /* Compiler driver program that can handle many languages.
  2. Copyright (C) 1987 Free Software Foundation, Inc.
  3. This file is part of GNU CC.
  4. GNU CC is distributed in the hope that it will be useful,
  5. but WITHOUT ANY WARRANTY. No author or distributor
  6. accepts responsibility to anyone for the consequences of using it
  7. or for whether it serves any particular purpose or works at all,
  8. unless he says so in writing. Refer to the GNU CC General Public
  9. License for full details.
  10. Everyone is granted permission to copy, modify and redistribute
  11. GNU CC, but only under the conditions described in the
  12. GNU CC General Public License. A copy of this license is
  13. supposed to have been given to you along with GNU CC so you
  14. can know your rights and responsibilities. It should be in a
  15. file named COPYING. Among other things, the copyright notice
  16. and this notice must be preserved on all copies. */
  17. /* This program is the user interface to the C compiler and possibly to
  18. other compilers. It is used because compilation is a complicated procedure
  19. which involves running several programs and passing temporary files between
  20. them, forwarding the users switches to those programs selectively,
  21. and deleting the temporary files at the end.
  22. CC recognizes how to compile each input file by suffixes in the file names.
  23. Once it knows which kind of compilation to perform, the procedure for
  24. compilation is specified by a string called a "spec".
  25. Specs are strings containing lines, each of which (if not blank)
  26. is made up of a program name, and arguments separated by spaces.
  27. The program name must be exact and start from root, since no path
  28. is searched and it is unreliable to depend on the current working directory.
  29. Redirection of input or output is not supported; the subprograms must
  30. accept filenames saying what files to read and write.
  31. In addition, the specs can contain %-sequences to substitute variable text
  32. or for conditional text. Here is a table of all defined %-sequences.
  33. Note that spaces are not generated automatically around the results of
  34. expanding these sequences; therefore, you can concatenate them together
  35. or with constant text in a single argument.
  36. %% substitute one % into the program name or argument.
  37. %i substitute the name of the input file being processed.
  38. %b substitute the basename of the input file being processed.
  39. This is the substring up to (and not including) the last period.
  40. %g substitute the temporary-file-name-base. This is a string chosen
  41. once per compilation. Different temporary file names are made by
  42. concatenation of constant strings on the end, as in `%g.s'.
  43. %g also has the same effect of %d.
  44. %d marks the argument containing or following the %d as a
  45. temporary file name, so that that file will be deleted at the
  46. end of CC. Unlike %g, this contributes no text to the argument.
  47. %w marks the argument containing or following the %w as the
  48. "output file" of this compilation. This puts the argument
  49. into the sequence of arguments that %o will substitute later.
  50. %o substitutes the names of all the output files, with spaces
  51. automatically placed around them. You should write spaces
  52. around the %o as well or the results are undefined.
  53. %o is for use in the specs for running the linker.
  54. Input files whose names have no recognized suffix are not compiled
  55. at all, but they are included among the output files, so they will
  56. be linked.
  57. %p substitutes the standard macro predefinitions for the
  58. current target machine. Use this when running cpp.
  59. %{S} substitutes the -S switch, if that switch was given to CC.
  60. If that switch was not specified, this substitutes nothing.
  61. %{S*} substitutes all the switches specified to CC whose names start
  62. with -S. This is used for -o, -D, -I, etc; switches that take
  63. arguments. CC considers `-o foo' as being one switch whose
  64. name starts with `o'. %{o*} would substitute this text,
  65. including the space; thus, two arguments would be generated.
  66. %{S:X} substitutes X, but only if the -S switch was given to CC.
  67. %{!S:X} substitutes X, but only if the -S switch was NOT given to CC.
  68. The conditional text X in a %{S:X} or %{!S:X} construct may contain
  69. other nested % constructs or spaces, or even newlines.
  70. They are processed as usual, as described above.
  71. Note that it is built into CC which switches take arguments and which
  72. do not. You might think it would be useful to generalize this to
  73. allow each compiler's spec to say which switches take arguments. But
  74. this cannot be done in a consistent fashion. CC cannot even decide
  75. which input files have been specified without knowing which switches
  76. take arguments, and it must know which input files to compile in order
  77. to tell which compilers to run.
  78. CC also knows implicitly that arguments starting in `-l' are to
  79. be treated as output files, and passed to the linker in their proper
  80. position among the other output files.
  81. */
  82. /* This defines which switches take arguments. */
  83. #define SWITCH_TAKES_ARG(CHAR) \
  84. (CHAR == 'D' || CHAR == 'U' || CHAR == 'o' || CHAR == 'e' || CHAR == 'T' \
  85. || CHAR == 'u' || CHAR == 'I' || CHAR == 'Y' || CHAR == 'd' || CHAR == 'm')
  86. #include <stdio.h>
  87. #include <sys/wait.h>
  88. #include <signal.h>
  89. #include <sys/file.h>
  90. #include "obstack.h"
  91. #include "config.h"
  92. #define obstack_chunk_alloc xmalloc
  93. #define obstack_chunk_free free
  94. extern int xmalloc ();
  95. extern void free ();
  96. /* If a stage of compilation returns an exit status >= 32,
  97. compilation of that file ceases. */
  98. #define MIN_FATAL_STATUS 32
  99. /* This is the obstack which we use to allocate many strings. */
  100. struct obstack obstack;
  101. char *handle_braces ();
  102. char *save_string ();
  103. char *concat ();
  104. int do_spec ();
  105. int do_spec_1 ();
  106. int give_string ();
  107. /* This structure says how to run one compiler, and when to do so. */
  108. struct compiler
  109. {
  110. char *suffix; /* Use this compiler for input files
  111. whose names end in this suffix. */
  112. char *spec; /* To use this compiler, pass this spec
  113. to do_spec. */
  114. };
  115. /* Here are the specs for compiling files with various known suffixes.
  116. A file that does not end in any of these suffixes will be passed
  117. unchanged to the loader, but that is all. */
  118. struct compiler compilers[] =
  119. {
  120. /* Note that we use the "cc1" from $PATH. */
  121. {".c",
  122. "cpp %{C} %p %{pedantic} %{D*} %{U*} %{I*} %i %{!E:%g.cpp}\n\
  123. %{!E:cc1 %g.cpp -quiet -dumpbase %i %{Y*} %{d*} %{m*} %{w} %{pedantic}\
  124. %{O:-opt}%{!O:-noreg}\
  125. %{g:-G}\
  126. -o %{S:%b}%{!S:%g}.s\n\
  127. %{!S:as %{R} -o %{!c:%d}%w%b.o %g.s\n }}"},
  128. {".s",
  129. "%{!S:as %{R} %i -o %{!c:%d}%w%b.o\n }"},
  130. /* Mark end of table */
  131. {0, 0}
  132. };
  133. /* Here is the spec for running the linker, after compiling all files. */
  134. char *link_spec = "%{!c:%{!E:%{!S:ld %{o*}\
  135. %{A} %{d} %{e*} %{M} %{N} %{n} %{r} %{s} %{S} %{T*} %{t} %{u*} %{X} %{x} %{z}\
  136. /lib/crt0.o %o %{g:-lg} -lc\n }}}";
  137. /* Record the names of temporary files we tell compilers to write,
  138. and delete them at the end of the run. */
  139. /* This is the common prefix we use to make temp file names.
  140. It is chosen once for each run of this program.
  141. It is substituted into a spec by %g.
  142. Thus, all temp file names contain this prefix.
  143. In practice, all temp file names start with this prefix.
  144. The prefix starts with `/tmp'. */
  145. char *temp_filename;
  146. /* Length of the prefix. */
  147. int temp_filename_length;
  148. /* Define the list of temporary files to delete. */
  149. struct temp_file
  150. {
  151. char *name;
  152. struct temp_file *next;
  153. };
  154. struct temp_file *temp_file_queue;
  155. /* Record FILENAME as a file to be deleted automatically. */
  156. void
  157. record_temp_file (filename)
  158. char *filename;
  159. {
  160. register struct temp_file *temp;
  161. register char *name;
  162. temp = (struct temp_file *) xmalloc (sizeof (struct temp_file));
  163. name = (char *) xmalloc (strlen (filename) + 1);
  164. strcpy (name, filename);
  165. temp->next = temp_file_queue;
  166. temp->name = name;
  167. temp_file_queue = temp;
  168. }
  169. /* Delete all the temporary files whose names we previously recorded. */
  170. void
  171. delete_temp_files ()
  172. {
  173. register struct temp_file *temp;
  174. for (temp = temp_file_queue; temp; temp = temp->next)
  175. {
  176. #ifdef DEBUG
  177. int i;
  178. printf ("Delete %s? (y or n) ", temp->name);
  179. fflush (stdout);
  180. i = getchar ();
  181. if (i != '\n')
  182. while (getchar () != '\n') ;
  183. if (i == 'y' || i == 'Y')
  184. #endif /* DEBUG */
  185. unlink (temp->name);
  186. }
  187. temp_file_queue = 0;
  188. }
  189. /* Compute a string to use as the base of all temporary file names.
  190. It is substituted for %g. */
  191. void
  192. choose_temp_base ()
  193. {
  194. register char *foo = "/tmp/ccXXXXXX";
  195. temp_filename = (char *) xmalloc (strlen (foo) + 1);
  196. strcpy (temp_filename, foo);
  197. mktemp (temp_filename);
  198. temp_filename_length = strlen (temp_filename);
  199. }
  200. /* Accumulate a command (program name and args), and run it. */
  201. /* Vector of pointers to arguments in the current line of specifications. */
  202. char **argbuf;
  203. /* Number of elements allocated in argbuf. */
  204. int argbuf_length;
  205. /* Number of elements in argbuf currently in use (containing args). */
  206. int argbuf_index;
  207. /* Flag indicating whether we should print the command and arguments */
  208. unsigned char vflag;
  209. /* User-specified prefix to attach to command names,
  210. or 0 if none specified. */
  211. char *user_exec_prefix = 0;
  212. /* Default prefixes to attach to command names. */
  213. char *standard_exec_prefix = "/usr/local/lib/gcc-";
  214. char *standard_exec_prefix_1 = "/usr/lib/gcc-";
  215. /* Clear out the vector of arguments (after a command is executed). */
  216. void
  217. clear_args ()
  218. {
  219. argbuf_index = 0;
  220. }
  221. /* Add one argument to the vector at the end.
  222. This is done when a space is seen or at the end of the line. */
  223. void
  224. store_arg (arg, tempnamep)
  225. char *arg;
  226. int tempnamep;
  227. {
  228. if (argbuf_index + 1 == argbuf_length)
  229. {
  230. argbuf = (char **) realloc (argbuf, (argbuf_length *= 2) * sizeof (char *));
  231. }
  232. argbuf[argbuf_index++] = arg;
  233. argbuf[argbuf_index] = 0;
  234. if (tempnamep)
  235. record_temp_file (arg);
  236. }
  237. /* Execute the command specified by the arguments on the current line of spec.
  238. Returns 0 if successful, -1 if failed. */
  239. int
  240. execute ()
  241. {
  242. int pid;
  243. union wait status;
  244. int size;
  245. char *temp;
  246. int win = 0;
  247. size = strlen (standard_exec_prefix);
  248. if (user_exec_prefix != 0 && strlen (user_exec_prefix) > size)
  249. size = strlen (user_exec_prefix);
  250. if (strlen (standard_exec_prefix_1) > size)
  251. size = strlen (standard_exec_prefix_1);
  252. size += strlen (argbuf[0]) + 1;
  253. temp = (char *) alloca (size);
  254. /* Determine the filename to execute. */
  255. if (user_exec_prefix)
  256. {
  257. strcpy (temp, user_exec_prefix);
  258. strcat (temp, argbuf[0]);
  259. win = (access (temp, X_OK) == 0);
  260. }
  261. if (!win)
  262. {
  263. strcpy (temp, standard_exec_prefix);
  264. strcat (temp, argbuf[0]);
  265. win = (access (temp, X_OK) == 0);
  266. }
  267. if (!win)
  268. {
  269. strcpy (temp, standard_exec_prefix_1);
  270. strcat (temp, argbuf[0]);
  271. win = (access (temp, X_OK) == 0);
  272. }
  273. if (vflag)
  274. {
  275. int i;
  276. for (i = 0; argbuf[i]; i++)
  277. {
  278. if (i == 0 && win)
  279. fprintf (stderr, " %s", temp);
  280. else
  281. fprintf (stderr, " %s", argbuf[i]);
  282. }
  283. fprintf (stderr, "\n");
  284. #ifdef DEBUG
  285. fprintf (stderr, "\nGo ahead? (y or n) ");
  286. fflush (stderr);
  287. i = getchar ();
  288. if (i != '\n')
  289. while (getchar () != '\n') ;
  290. if (i != 'y' && i != 'Y')
  291. return 0;
  292. #endif /* DEBUG */
  293. }
  294. pid = vfork ();
  295. if (pid < 0)
  296. pfatal_with_name ("vfork");
  297. if (pid == 0)
  298. {
  299. if (win)
  300. execv (temp, argbuf);
  301. else
  302. execvp (argbuf[0], argbuf);
  303. perror_with_name (argbuf[0]);
  304. _exit (65);
  305. }
  306. wait (&status);
  307. if (WIFSIGNALED (status))
  308. fatal ("Program %s got fatal signal %d.", argbuf[0], status.w_termsig);
  309. if ((WIFEXITED (status) && status.w_retcode >= MIN_FATAL_STATUS)
  310. || WIFSIGNALED (status))
  311. return -1;
  312. return 0;
  313. }
  314. /* Find all the switches given to us
  315. and make a vector describing them.
  316. The elements of the vector a strings, one per switch given.
  317. The string includes a minus sign, the switch name,
  318. and possibly an argument after that, with a space between them
  319. if that the user gave the switch its argument as two args. */
  320. char **switches;
  321. int n_switches;
  322. /* Also a vector of input files specified. */
  323. char **infiles;
  324. int n_infiles;
  325. /* And a vector of corresponding output files is made up later. */
  326. char **outfiles;
  327. char *
  328. make_switch (p1, s1, p2, s2)
  329. char *p1;
  330. int s1;
  331. char *p2;
  332. int s2;
  333. {
  334. register char *new;
  335. if (p2 && s2 == 0)
  336. s2 = strlen (p2);
  337. new = (char *) xmalloc (s1 + s2 + 2);
  338. bcopy (p1, new, s1);
  339. if (p2)
  340. {
  341. new[s1++] = ' ';
  342. bcopy (p2, new + s1, s2);
  343. }
  344. new[s1 + s2] = 0;
  345. return new;
  346. }
  347. /* Create the vector `switches' and its contents.
  348. Store its length in `n_switches'. */
  349. void
  350. process_command (argc, argv)
  351. int argc;
  352. char **argv;
  353. {
  354. register int i;
  355. n_switches = 0;
  356. n_infiles = 0;
  357. /* Scan argv twice. Here, the first time, just count how many switches
  358. there will be in their vector, and how many input files in theirs.
  359. Here we also parse the switches that cc itself uses (e.g. -v). */
  360. for (i = 1; i < argc; i++)
  361. {
  362. if (argv[i][0] == '-' && argv[i][1] != 'l')
  363. {
  364. register char *p = &argv[i][1];
  365. register int c = *p;
  366. switch (c)
  367. {
  368. case 'B':
  369. user_exec_prefix = p + 1;
  370. break;
  371. case 'v': /* Print commands as we execute them */
  372. vflag++;
  373. default:
  374. n_switches++;
  375. if (SWITCH_TAKES_ARG (c) && p[1] == 0)
  376. i++;
  377. }
  378. }
  379. else
  380. n_infiles++;
  381. }
  382. /* Then create the space for the vectors and scan again. */
  383. switches = (char **) xmalloc ((n_switches + 1) * sizeof (char *));
  384. infiles = (char **) xmalloc ((n_infiles + 1) * sizeof (char *));
  385. n_switches = 0;
  386. n_infiles = 0;
  387. /* This, time, copy the text of each switch and store a pointer
  388. to the copy in the vector of switches.
  389. Store all the infiles in their vector. */
  390. for (i = 1; i < argc; i++)
  391. {
  392. if (argv[i][0] == '-' && argv[i][1] != 'l')
  393. {
  394. register char *p = &argv[i][1];
  395. register int c = *p;
  396. if (c == 'B')
  397. continue;
  398. if (SWITCH_TAKES_ARG (c) && p[1] == 0)
  399. switches[n_switches++] = make_switch (p, 1, argv[++i], 0);
  400. else
  401. switches[n_switches++] = make_switch (p, strlen (p),
  402. (char *)0, 0);
  403. }
  404. else
  405. infiles[n_infiles++] = argv[i];
  406. }
  407. switches[n_switches] = 0;
  408. infiles[n_infiles] = 0;
  409. }
  410. /* Process a spec string, accumulating and running commands. */
  411. /* These variables describe the input file name.
  412. input_file_number is the index on outfiles of this file,
  413. so that the output file name can be stored for later use by %o.
  414. input_basename is the start of the part of the input file
  415. sans all directory names, and basename_length is the number
  416. of characters starting there excluding the suffix .c or whatever. */
  417. char *input_filename;
  418. int input_file_number;
  419. int input_filename_length;
  420. int basename_length;
  421. char *input_basename;
  422. /* These are variables used within do_spec and do_spec_1. */
  423. /* Nonzero if an arg has been started and not yet terminated
  424. (with space, tab or newline). */
  425. int arg_going;
  426. /* Nonzero means %d or %g has been seen; the next arg to be terminated
  427. is a temporary file name. */
  428. int delete_this_arg;
  429. /* Nonzero means %w has been seen; the next arg to be terminated
  430. is the output file name of this compilation. */
  431. int this_is_output_file;
  432. /* Process the spec SPEC and run the commands specified therein.
  433. Returns 0 if the spec is successfully processed; -1 if failed. */
  434. int
  435. do_spec (spec)
  436. char *spec;
  437. {
  438. int value;
  439. clear_args ();
  440. arg_going = 0;
  441. delete_this_arg = 0;
  442. this_is_output_file = 0;
  443. value = do_spec_1 (spec, 0);
  444. if (value == 0)
  445. value = do_spec_1 ("\n", 0);
  446. return value;
  447. }
  448. /* Process the sub-spec SPEC as a portion of a larger spec.
  449. This is like processing a whole spec except that we do
  450. not initialize at the beginning and we do not supply a
  451. newline by default at the end.
  452. NOPERCENT nonzero means don't process %-sequences in SPEC;
  453. in this case, % is treated as an ordinary character.
  454. This is used while substituting switches. */
  455. int
  456. do_spec_1 (spec, nopercent)
  457. char *spec;
  458. int nopercent;
  459. {
  460. register char *p = spec;
  461. register int c;
  462. char *string;
  463. while (c = *p++)
  464. switch (c)
  465. {
  466. case '\n':
  467. /* End of line: finish any pending argument,
  468. then run the pending command if one has been started. */
  469. if (arg_going)
  470. {
  471. obstack_1grow (&obstack, 0);
  472. string = obstack_finish (&obstack);
  473. store_arg (string, delete_this_arg);
  474. if (this_is_output_file)
  475. outfiles[input_file_number] = string;
  476. }
  477. arg_going = 0;
  478. if (argbuf_index)
  479. {
  480. int value = execute ();
  481. if (value)
  482. return value;
  483. }
  484. /* Reinitialize for a new command, and for a new argument. */
  485. clear_args ();
  486. arg_going = 0;
  487. delete_this_arg = 0;
  488. this_is_output_file = 0;
  489. break;
  490. case '\t':
  491. case ' ':
  492. /* Space or tab ends an argument if one is pending. */
  493. if (arg_going)
  494. {
  495. obstack_1grow (&obstack, 0);
  496. string = obstack_finish (&obstack);
  497. store_arg (string, delete_this_arg);
  498. if (this_is_output_file)
  499. outfiles[input_file_number] = string;
  500. }
  501. /* Reinitialize for a new argument. */
  502. arg_going = 0;
  503. delete_this_arg = 0;
  504. this_is_output_file = 0;
  505. break;
  506. case '%':
  507. if (! nopercent)
  508. switch (c = *p++)
  509. {
  510. case 0:
  511. fatal ("Invalid specification! Bug in cc.");
  512. case 'i':
  513. obstack_grow (&obstack, input_filename, input_filename_length);
  514. arg_going = 1;
  515. break;
  516. case 'b':
  517. obstack_grow (&obstack, input_basename, basename_length);
  518. arg_going = 1;
  519. break;
  520. case 'p':
  521. do_spec_1 (CPP_PREDEFINES, 1);
  522. break;
  523. case 'g':
  524. obstack_grow (&obstack, temp_filename, temp_filename_length);
  525. delete_this_arg = 1;
  526. arg_going = 1;
  527. break;
  528. case 'd':
  529. delete_this_arg = 1;
  530. break;
  531. case 'w':
  532. this_is_output_file = 1;
  533. break;
  534. case 'o':
  535. {
  536. register int f;
  537. for (f = 0; f < n_infiles; f++)
  538. store_arg (outfiles[f], 0);
  539. }
  540. break;
  541. case '{':
  542. p = handle_braces (p);
  543. if (p == 0)
  544. return -1;
  545. break;
  546. case '%':
  547. obstack_1grow (&obstack, '%');
  548. break;
  549. default:
  550. abort ();
  551. }
  552. else
  553. {
  554. obstack_1grow (&obstack, c);
  555. arg_going = 1;
  556. }
  557. break;
  558. default:
  559. /* Ordinary character: put it into the current argument. */
  560. obstack_1grow (&obstack, c);
  561. arg_going = 1;
  562. }
  563. return 0; /* End of string */
  564. }
  565. /* Return 0 if we call do_spec_1 and that returns -1. */
  566. char *
  567. handle_braces (p)
  568. register char *p;
  569. {
  570. register char *q;
  571. int negate = *p == '!';
  572. char *filter;
  573. if (negate) ++p;
  574. filter = p;
  575. while (*p != ':' && *p != '}') p++;
  576. if (*p != '}')
  577. {
  578. register int count = 1;
  579. q = p + 1;
  580. while (count > 0)
  581. {
  582. if (*q == '{')
  583. count++;
  584. else if (*q == '}')
  585. count--;
  586. else if (*q == 0)
  587. abort ();
  588. q++;
  589. }
  590. }
  591. else
  592. q = p + 1;
  593. if (p[-1] == '*')
  594. {
  595. /* Substitute all matching switches as separate args. */
  596. register int i;
  597. --p;
  598. for (i = 0; i < n_switches; i++)
  599. if (!strncmp (switches[i], filter, p - filter))
  600. {
  601. if (give_switch (i) < 0)
  602. return 0;
  603. }
  604. }
  605. else
  606. {
  607. /* Test for presence of the specified switch. */
  608. register int i;
  609. int present = 0;
  610. for (i = 0; i < n_switches; i++)
  611. {
  612. if (!strncmp (switches[i], filter, p - filter))
  613. {
  614. present = 1;
  615. break;
  616. }
  617. }
  618. /* If it is as desired (present for %{s...}, absent for %{-s...})
  619. then substitute either the switch or the specified
  620. conditional text. */
  621. if (present != negate)
  622. {
  623. if (*p == '}')
  624. {
  625. if (give_switch (i) < 0)
  626. return 0;
  627. }
  628. else
  629. {
  630. if (do_spec_1 (save_string (p + 1, q - p - 2), 0) < 0)
  631. return 0;
  632. }
  633. }
  634. }
  635. return q;
  636. }
  637. int
  638. give_switch (switchnum)
  639. int switchnum;
  640. {
  641. do_spec_1 ("-", 0);
  642. do_spec_1 (switches[switchnum], 0);
  643. do_spec_1 (" ", 0);
  644. }
  645. /* On fatal signals, delete all the temporary files. */
  646. void
  647. fatal_error (signum)
  648. int signum;
  649. {
  650. signal (signum, SIG_DFL);
  651. delete_temp_files ();
  652. /* Get the same signal again, this time not handled,
  653. so its normal effect occurs. */
  654. kill (getpid (), signum);
  655. }
  656. int
  657. main (argc, argv)
  658. int argc;
  659. char **argv;
  660. {
  661. register int i;
  662. int value;
  663. int nolink = 0;
  664. signal (SIGINT, fatal_error);
  665. signal (SIGKILL, fatal_error);
  666. argbuf_length = 10;
  667. argbuf = (char **) xmalloc (argbuf_length * sizeof (char *));
  668. obstack_init (&obstack);
  669. choose_temp_base ();
  670. /* Make a table of what switches there are (switches, n_switches).
  671. Make a table of specified input files (infiles, n_infiles). */
  672. process_command (argc, argv);
  673. /* Make a place to record the compiler output file names
  674. that correspond to the input files. */
  675. outfiles = (char **) xmalloc (n_infiles * sizeof (char *));
  676. bzero (outfiles, n_infiles * sizeof (char *));
  677. for (i = 0; i < n_infiles; i++)
  678. {
  679. /* First figure out which compiler from the file's suffix. */
  680. register struct compiler *cp;
  681. /* Tell do_spec what to substitute for %i. */
  682. input_filename = infiles[i];
  683. input_filename_length = strlen (input_filename);
  684. input_file_number = i;
  685. /* Use the same thing in %o, unless cp->spec says otherwise. */
  686. outfiles[i] = input_filename;
  687. for (cp = compilers; cp->spec; cp++)
  688. {
  689. if (strlen (cp->suffix) < input_filename_length
  690. && !strcmp (cp->suffix,
  691. infiles[i] + input_filename_length
  692. - strlen (cp->suffix)))
  693. {
  694. /* Ok, we found an applicable compiler. Run its spec. */
  695. /* First say how much of input_filename to substitute for %b */
  696. register char *p;
  697. input_basename = input_filename;
  698. for (p = input_filename; *p; p++)
  699. if (*p == '/')
  700. input_basename = p + 1;
  701. basename_length = (input_filename_length - strlen (cp->suffix)
  702. - (input_basename - input_filename));
  703. value = do_spec (cp->spec);
  704. if (value < 0)
  705. nolink = 1;
  706. break;
  707. }
  708. }
  709. /* If this file's name does not contain a recognized suffix,
  710. don't do anything to it, but do feed it to the link spec
  711. since its name is in outfiles. */
  712. }
  713. /* Run ld to link all the compiler output files. */
  714. if (! nolink)
  715. do_spec (link_spec);
  716. /* Delete all the temporary files we made. */
  717. delete_temp_files ();
  718. return 0;
  719. }
  720. xmalloc (size)
  721. int size;
  722. {
  723. register int value = malloc (size);
  724. if (value == 0)
  725. fatal ("Virtual memory full.");
  726. return value;
  727. }
  728. xrealloc (ptr, size)
  729. int ptr, size;
  730. {
  731. register int value = realloc (ptr, size);
  732. if (value == 0)
  733. fatal ("Virtual memory full.");
  734. return value;
  735. }
  736. fatal (msg, arg1, arg2)
  737. char *msg, *arg1, *arg2;
  738. {
  739. error (msg, arg1, arg2);
  740. delete_temp_files ();
  741. exit (1);
  742. }
  743. error (msg, arg1, arg2)
  744. char *msg, *arg1, *arg2;
  745. {
  746. fprintf (stderr, "cc: ");
  747. fprintf (stderr, msg, arg1, arg2);
  748. fprintf (stderr, "\n");
  749. }
  750. /* Return a newly-allocated string whose contents concatenate those of s1, s2, s3. */
  751. char *
  752. concat (s1, s2, s3)
  753. char *s1, *s2, *s3;
  754. {
  755. int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
  756. char *result = (char *) xmalloc (len1 + len2 + len3 + 1);
  757. strcpy (result, s1);
  758. strcpy (result + len1, s2);
  759. strcpy (result + len1 + len2, s3);
  760. *(result + len1 + len2 + len3) = 0;
  761. return result;
  762. }
  763. char *
  764. save_string (s, len)
  765. char *s;
  766. int len;
  767. {
  768. register char *result = (char *) xmalloc (len + 1);
  769. bcopy (s, result, len);
  770. result[len] = 0;
  771. return result;
  772. }
  773. pfatal_with_name (name)
  774. char *name;
  775. {
  776. extern int errno, sys_nerr;
  777. extern char *sys_errlist[];
  778. char *s;
  779. if (errno < sys_nerr)
  780. s = concat ("", sys_errlist[errno], " for %s");
  781. else
  782. s = "cannot open %s";
  783. fatal (s, name);
  784. }
  785. perror_with_name (name)
  786. char *name;
  787. {
  788. extern int errno, sys_nerr;
  789. extern char *sys_errlist[];
  790. char *s;
  791. if (errno < sys_nerr)
  792. s = concat ("", sys_errlist[errno], " for %s");
  793. else
  794. s = "cannot open %s";
  795. error (s, name);
  796. }