Oh GACK! src-clean doesn't quite work that easily since cleandist rebuilds the
[unix-history] / gnu / usr.bin / gcc1 / cc / cc.c
CommitLineData
15637ed4
RG
1/*-
2 * This code is derived from software copyrighted by the Free Software
3 * Foundation.
4 *
5 * Modified 1991 by Donn Seeley at UUNET Technologies, Inc.
6 */
7
8#ifndef lint
9static char sccsid[] = "@(#)cc.c 6.7 (Berkeley) 5/8/91";
10#endif /* not lint */
11
12/*-
13 Copyright (C) 1987,1989 Free Software Foundation, Inc.
14
15This file is part of GNU CC.
16
17GNU CC is free software; you can redistribute it and/or modify
18it under the terms of the GNU General Public License as published by
19the Free Software Foundation; either version 1, or (at your option)
20any later version.
21
22GNU CC is distributed in the hope that it will be useful,
23but WITHOUT ANY WARRANTY; without even the implied warranty of
24MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25GNU General Public License for more details.
26
27You should have received a copy of the GNU General Public License
28along with GNU CC; see the file COPYING. If not, write to
29the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
30
31This paragraph is here to try to keep Sun CC from dying.
32The number of chars here seems crucial!!!! */
33
34void record_temp_file ();
35\f
36/* This program is the user interface to the C compiler and possibly to
37other compilers. It is used because compilation is a complicated procedure
38which involves running several programs and passing temporary files between
39them, forwarding the users switches to those programs selectively,
40and deleting the temporary files at the end.
41
42CC recognizes how to compile each input file by suffixes in the file names.
43Once it knows which kind of compilation to perform, the procedure for
44compilation is specified by a string called a "spec".
45
46Specs are strings containing lines, each of which (if not blank)
47is made up of a program name, and arguments separated by spaces.
48The program name must be exact and start from root, since no path
49is searched and it is unreliable to depend on the current working directory.
50Redirection of input or output is not supported; the subprograms must
51accept filenames saying what files to read and write.
52
53In addition, the specs can contain %-sequences to substitute variable text
54or for conditional text. Here is a table of all defined %-sequences.
55Note that spaces are not generated automatically around the results of
56expanding these sequences; therefore, you can concatenate them together
57or with constant text in a single argument.
58
59 %% substitute one % into the program name or argument.
60 %i substitute the name of the input file being processed.
61 %b substitute the basename of the input file being processed.
62 This is the substring up to (and not including) the last period.
63 %g substitute the temporary-file-name-base. This is a string chosen
64 once per compilation. Different temporary file names are made by
65 concatenation of constant strings on the end, as in `%g.s'.
66 %g also has the same effect of %d.
67 %d marks the argument containing or following the %d as a
68 temporary file name, so that that file will be deleted if CC exits
69 successfully. Unlike %g, this contributes no text to the argument.
70 %w marks the argument containing or following the %w as the
71 "output file" of this compilation. This puts the argument
72 into the sequence of arguments that %o will substitute later.
73 %W{...}
74 like %{...} but mark last argument supplied within
75 as a file to be deleted on failure.
76 %o substitutes the names of all the output files, with spaces
77 automatically placed around them. You should write spaces
78 around the %o as well or the results are undefined.
79 %o is for use in the specs for running the linker.
80 Input files whose names have no recognized suffix are not compiled
81 at all, but they are included among the output files, so they will
82 be linked.
83 %p substitutes the standard macro predefinitions for the
84 current target machine. Use this when running cpp.
85 %P like %p, but puts `__' before and after the name of each macro.
86 This is for ANSI C.
87 %s current argument is the name of a library or startup file of some sort.
88 Search for that file in a standard list of directories
89 and substitute the full pathname found.
90 %eSTR Print STR as an error message. STR is terminated by a newline.
91 Use this when inconsistent options are detected.
92 %a process ASM_SPEC as a spec.
93 This allows config.h to specify part of the spec for running as.
94 %l process LINK_SPEC as a spec.
95 %L process LIB_SPEC as a spec.
96 %S process STARTFILE_SPEC as a spec. A capital S is actually used here.
97 %E process ENDFILE_SPEC as a spec. A capital E is actually used here.
98 %c process SIGNED_CHAR_SPEC as a spec.
99 %C process CPP_SPEC as a spec. A capital C is actually used here.
100 %1 process CC1_SPEC as a spec.
101 %{S} substitutes the -S switch, if that switch was given to CC.
102 If that switch was not specified, this substitutes nothing.
103 Here S is a metasyntactic variable.
104 %{S*} substitutes all the switches specified to CC whose names start
105 with -S. This is used for -o, -D, -I, etc; switches that take
106 arguments. CC considers `-o foo' as being one switch whose
107 name starts with `o'. %{o*} would substitute this text,
108 including the space; thus, two arguments would be generated.
109 %{S:X} substitutes X, but only if the -S switch was given to CC.
110 %{!S:X} substitutes X, but only if the -S switch was NOT given to CC.
111 %{|S:X} like %{S:X}, but if no S switch, substitute `-'.
112 %{|!S:X} like %{!S:X}, but if there is an S switch, substitute `-'.
113
114The conditional text X in a %{S:X} or %{!S:X} construct may contain
115other nested % constructs or spaces, or even newlines.
116They are processed as usual, as described above.
117
118The character | is used to indicate that a command should be piped to
119the following command, but only if -pipe is specified.
120
121Note that it is built into CC which switches take arguments and which
122do not. You might think it would be useful to generalize this to
123allow each compiler's spec to say which switches take arguments. But
124this cannot be done in a consistent fashion. CC cannot even decide
125which input files have been specified without knowing which switches
126take arguments, and it must know which input files to compile in order
127to tell which compilers to run.
128
129CC also knows implicitly that arguments starting in `-l' are to
130be treated as compiler output files, and passed to the linker in their proper
131position among the other output files.
132
133*/
134
135#include <stdio.h>
136#include <sys/types.h>
137#include <signal.h>
138#include <sys/file.h>
139
140#include "config.h"
141#include "obstack.h"
142#include "gvarargs.h"
143
144#ifdef USG
145#ifndef R_OK
146#define R_OK 4
147#define W_OK 2
148#define X_OK 1
149#endif
150
151#define vfork fork
152#endif /* USG */
153
154#define obstack_chunk_alloc xmalloc
155#define obstack_chunk_free free
156extern int xmalloc ();
157extern void free ();
158
159/* If a stage of compilation returns an exit status >= 1,
160 compilation of that file ceases. */
161
162#define MIN_FATAL_STATUS 1
163
164/* This is the obstack which we use to allocate many strings. */
165
166struct obstack obstack;
167
168char *handle_braces ();
169char *save_string ();
170char *concat ();
171int do_spec ();
172int do_spec_1 ();
173char *find_file ();
174static char *find_exec_file ();
175void validate_switches ();
176void validate_all_switches ();
177void fancy_abort ();
178
179/* config.h can define ASM_SPEC to provide extra args to the assembler
180 or extra switch-translations. */
181#ifndef ASM_SPEC
182#define ASM_SPEC ""
183#endif
184
185/* config.h can define CPP_SPEC to provide extra args to the C preprocessor
186 or extra switch-translations. */
187#ifndef CPP_SPEC
188#define CPP_SPEC ""
189#endif
190
191/* config.h can define CC1_SPEC to provide extra args to cc1
192 or extra switch-translations. */
193#ifndef CC1_SPEC
194#define CC1_SPEC ""
195#endif
196
197/* config.h can define LINK_SPEC to provide extra args to the linker
198 or extra switch-translations. */
199#ifndef LINK_SPEC
200#define LINK_SPEC ""
201#endif
202
203/* config.h can define LIB_SPEC to override the default libraries. */
204#ifndef LIB_SPEC
205#define LIB_SPEC "%{!p:%{!pg:-lc}}%{p:-lc_p}%{pg:-lc_p}"
206#endif
207
208/* config.h can define ENDFILE_SPEC to override the default crtn files. */
209#ifndef ENDFILE_SPEC
210#define ENDFILE_SPEC ""
211#endif
212
213/* config.h can define STARTFILE_SPEC to override the default crt0 files. */
214#ifndef STARTFILE_SPEC
215#define STARTFILE_SPEC \
216 "%{pg:gcrt0.o%s}%{!pg:%{p:mcrt0.o%s}%{!p:crt0.o%s}}"
217#endif
218
219/* This spec is used for telling cpp whether char is signed or not. */
220#define SIGNED_CHAR_SPEC \
221 (DEFAULT_SIGNED_CHAR ? "%{funsigned-char:-D__CHAR_UNSIGNED__}" \
222 : "%{!fsigned-char:-D__CHAR_UNSIGNED__}")
223
224/* This defines which switch letters take arguments. */
225
226#ifndef SWITCH_TAKES_ARG
227#define SWITCH_TAKES_ARG(CHAR) \
228 ((CHAR) == 'D' || (CHAR) == 'U' || (CHAR) == 'o' \
229 || (CHAR) == 'e' || (CHAR) == 'T' || (CHAR) == 'u' \
230 || (CHAR) == 'I' || (CHAR) == 'Y' || (CHAR) == 'm' \
231 || (CHAR) == 'L' || (CHAR) == 'i' || (CHAR) == 'A')
232#endif
233
234/* This defines which multi-letter switches take arguments. */
235
236#ifndef WORD_SWITCH_TAKES_ARG
237#define WORD_SWITCH_TAKES_ARG(STR) (!strcmp (STR, "Tdata"))
238#endif
239
240/* This structure says how to run one compiler, and when to do so. */
241
242struct compiler
243{
244 char *suffix; /* Use this compiler for input files
245 whose names end in this suffix. */
246 char *spec; /* To use this compiler, pass this spec
247 to do_spec. */
248};
249
250/* Here are the specs for compiling files with various known suffixes.
251 A file that does not end in any of these suffixes will be passed
252 unchanged to the loader and nothing else will be done to it. */
253
254struct compiler compilers[] =
255{
256 {".c",
257 "cpp %{nostdinc} %{C} %{v} %{D*} %{U*} %{I*} %{M*} %{i*} %{trigraphs} -undef \
258 -D__GNUC__ %{ansi:-trigraphs -$ -D__STRICT_ANSI__} %{!ansi:%p} %P\
259 %c %{O:-D__OPTIMIZE__} %{traditional} %{pedantic} %{P}\
260 %{Wcomment*} %{Wtrigraphs} %{Wall} %{w} %C\
261 %i %{!M*:%{!E:%{!pipe:%g.cpp}}}%{E:%W{o*}}%{M*:%W{o*}} |\n\
262 %{!M*:%{!E:cc1 %{!pipe:%g.cpp} %1 \
263 %{!Q:-quiet} -dumpbase %i %{Y*} %{d*} %{m*} %{f*} %{a}\
264 %{g} %{O} %{W*} %{w} %{pedantic} %{ansi} %{traditional}\
265 %{v:-version} %{gg:-symout %g.sym} %{pg:-p} %{p}\
266 %{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
267 %{S:%W{o*}%{!o*:-o %b.s}}%{!S:-o %{|!pipe:%g.s}} |\n\
268 %{!S:as %{R} %{j} %{J} %{h} %{d2} %a %{gg:-G %g.sym}\
269 %{c:%W{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%b.o}\
270 %{!pipe:%g.s}\n }}}"},
271 {".cc",
272 "cpp -+ %{nostdinc} %{C} %{v} %{D*} %{U*} %{I*} %{M*} %{i*} \
273 -undef -D__GNUC__ -D__GNUG__ %p %P\
274 %c %{O:-D__OPTIMIZE__} %{traditional} %{pedantic} %{P}\
275 %{Wcomment*} %{Wtrigraphs} %{Wall} %{w} %C\
276 %i %{!M*:%{!E:%{!pipe:%g.cpp}}}%{E:%W{o*}}%{M*:%W{o*}} |\n\
277 %{!M*:%{!E:cc1plus %{!pipe:%g.cpp} %1\
278 %{!Q:-quiet} -dumpbase %i %{Y*} %{d*} %{m*} %{f*} %{a}\
279 %{g} %{O} %{W*} %{w} %{pedantic} %{traditional}\
280 %{v:-version} %{gg:-symout %g.sym} %{pg:-p} %{p}\
281 %{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
282 %{S:%W{o*}%{!o*:-o %b.s}}%{!S:-o %{|!pipe:%g.s}} |\n\
283 %{!S:as %{R} %{j} %{J} %{h} %{d2} %a %{gg:-G %g.sym}\
284 %{c:%W{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%b.o}\
285 %{!pipe:%g.s}\n }}}"},
286 {".i",
287 "cc1 %i %1 %{!Q:-quiet} %{Y*} %{d*} %{m*} %{f*} %{a}\
288 %{g} %{O} %{W*} %{w} %{pedantic} %{ansi} %{traditional}\
289 %{v:-version} %{gg:-symout %g.sym} %{pg:-p} %{p}\
290 %{S:%W{o*}%{!o*:-o %b.s}}%{!S:-o %{|!pipe:%g.s}} |\n\
291 %{!S:as %{R} %{j} %{J} %{h} %{d2} %a %{gg:-G %g.sym}\
292 %{c:%W{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%b.o} %{!pipe:%g.s}\n }"},
293 {".s",
294 "%{!S:as %{R} %{j} %{J} %{h} %{d2} %a \
295 %{c:%W{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%b.o} %i\n }"},
296 {".S",
297 "cpp %{nostdinc} %{C} %{v} %{D*} %{U*} %{I*} %{M*} %{trigraphs} \
298 -undef -D__GNUC__ -$ %p %P\
299 %c %{O:-D__OPTIMIZE__} %{traditional} %{pedantic} %{P}\
300 %{Wcomment*} %{Wtrigraphs} %{Wall} %{w} %C\
301 %i %{!M*:%{!E:%{!pipe:%g.s}}}%{E:%W{o*}}%{M*:%W{o*}} |\n\
302 %{!M*:%{!E:%{!S:as %{R} %{j} %{J} %{h} %{d2} %a \
303 %{c:%W{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%b.o}\
304 %{!pipe:%g.s}\n }}}"},
305 /* Mark end of table */
306 {0, 0}
307};
308
309/* Here is the spec for running the linker, after compiling all files. */
310char *link_spec = "%{!c:%{!M*:%{!E:%{!S:ld %{o*} %l\
311 %{A} %{d} %{e*} %{N} %{n} %{r} %{s} %{S} %{T*} %{t} %{u*} %{X} %{x} %{z}\
312 %{y*} %{!A:%{!nostdlib:%S}} \
b7bc99a4 313 %{L*} %o %{!nostdlib:%L -lgnulib %L -lgnulib %{!A:%E}}\n }}}}";
15637ed4
RG
314\f
315/* Accumulate a command (program name and args), and run it. */
316
317/* Vector of pointers to arguments in the current line of specifications. */
318
319char **argbuf;
320
321/* Number of elements allocated in argbuf. */
322
323int argbuf_length;
324
325/* Number of elements in argbuf currently in use (containing args). */
326
327int argbuf_index;
328
329/* Number of commands executed so far. */
330
331int execution_count;
332
333/* Flag indicating whether we should print the command and arguments */
334
335unsigned char vflag;
336
337/* Name with which this program was invoked. */
338
339char *programname;
340
341/* User-specified -B prefix to attach to command names,
342 or 0 if none specified. */
343
344char *user_exec_prefix = 0;
345
346/* Environment-specified prefix to attach to command names,
347 or 0 if none specified. */
348
349char *env_exec_prefix = 0;
350
351/* Suffix to attach to directories searched for commands. */
352
353char *machine_suffix = 0;
354
355/* Default prefixes to attach to command names. */
356
357#ifndef STANDARD_EXEC_PREFIX
358#define STANDARD_EXEC_PREFIX "/usr/libexec/"
359#endif /* !defined STANDARD_EXEC_PREFIX */
360
361char *standard_exec_prefix = STANDARD_EXEC_PREFIX;
362char *standard_exec_prefix_1 = "/usr/libexec/gcc-";
363
364#ifndef STANDARD_STARTFILE_PREFIX
365#define STANDARD_STARTFILE_PREFIX "/usr/lib/"
366#endif /* !defined STANDARD_STARTFILE_PREFIX */
367
368char *standard_startfile_prefix = STANDARD_STARTFILE_PREFIX;
369char *standard_startfile_prefix_1 = "/usr/local/lib/";
370char *standard_startfile_prefix_2 = "/usr/lib/gcc-";
371
372/* Clear out the vector of arguments (after a command is executed). */
373
374void
375clear_args ()
376{
377 argbuf_index = 0;
378}
379
380/* Add one argument to the vector at the end.
381 This is done when a space is seen or at the end of the line.
382 If DELETE_ALWAYS is nonzero, the arg is a filename
383 and the file should be deleted eventually.
384 If DELETE_FAILURE is nonzero, the arg is a filename
385 and the file should be deleted if this compilation fails. */
386
387void
388store_arg (arg, delete_always, delete_failure)
389 char *arg;
390 int delete_always, delete_failure;
391{
392 if (argbuf_index + 1 == argbuf_length)
393 {
394 argbuf = (char **) realloc (argbuf, (argbuf_length *= 2) * sizeof (char *));
395 }
396
397 argbuf[argbuf_index++] = arg;
398 argbuf[argbuf_index] = 0;
399
400 if (delete_always || delete_failure)
401 record_temp_file (arg, delete_always, delete_failure);
402}
403\f
404/* Record the names of temporary files we tell compilers to write,
405 and delete them at the end of the run. */
406
407/* This is the common prefix we use to make temp file names.
408 It is chosen once for each run of this program.
409 It is substituted into a spec by %g.
410 Thus, all temp file names contain this prefix.
411 In practice, all temp file names start with this prefix.
412
413 This prefix comes from the envvar TMPDIR if it is defined;
414 otherwise, in /tmp. */
415
416char *temp_filename;
417
418/* Length of the prefix. */
419
420int temp_filename_length;
421
422/* Define the list of temporary files to delete. */
423
424struct temp_file
425{
426 char *name;
427 struct temp_file *next;
428};
429
430/* Queue of files to delete on success or failure of compilation. */
431struct temp_file *always_delete_queue;
432/* Queue of files to delete on failure of compilation. */
433struct temp_file *failure_delete_queue;
434
435/* Record FILENAME as a file to be deleted automatically.
436 ALWAYS_DELETE nonzero means delete it if all compilation succeeds;
437 otherwise delete it in any case.
438 FAIL_DELETE nonzero means delete it if a compilation step fails;
439 otherwise delete it in any case. */
440
441void
442record_temp_file (filename, always_delete, fail_delete)
443 char *filename;
444 int always_delete;
445 int fail_delete;
446{
447 register char *name;
448 name = (char *) xmalloc (strlen (filename) + 1);
449 strcpy (name, filename);
450
451 if (always_delete)
452 {
453 register struct temp_file *temp;
454 for (temp = always_delete_queue; temp; temp = temp->next)
455 if (! strcmp (name, temp->name))
456 goto already1;
457 temp = (struct temp_file *) xmalloc (sizeof (struct temp_file));
458 temp->next = always_delete_queue;
459 temp->name = name;
460 always_delete_queue = temp;
461 already1:;
462 }
463
464 if (fail_delete)
465 {
466 register struct temp_file *temp;
467 for (temp = failure_delete_queue; temp; temp = temp->next)
468 if (! strcmp (name, temp->name))
469 goto already2;
470 temp = (struct temp_file *) xmalloc (sizeof (struct temp_file));
471 temp->next = failure_delete_queue;
472 temp->name = name;
473 failure_delete_queue = temp;
474 already2:;
475 }
476}
477
478/* Delete all the temporary files whose names we previously recorded. */
479
480void
481delete_temp_files ()
482{
483 register struct temp_file *temp;
484
485 for (temp = always_delete_queue; temp; temp = temp->next)
486 {
487#ifdef DEBUG
488 int i;
489 printf ("Delete %s? (y or n) ", temp->name);
490 fflush (stdout);
491 i = getchar ();
492 if (i != '\n')
493 while (getchar () != '\n') ;
494 if (i == 'y' || i == 'Y')
495#endif /* DEBUG */
496 {
497 if (unlink (temp->name) < 0)
498 if (vflag)
499 perror_with_name (temp->name);
500 }
501 }
502
503 always_delete_queue = 0;
504}
505
506/* Delete all the files to be deleted on error. */
507
508void
509delete_failure_queue ()
510{
511 register struct temp_file *temp;
512
513 for (temp = failure_delete_queue; temp; temp = temp->next)
514 {
515#ifdef DEBUG
516 int i;
517 printf ("Delete %s? (y or n) ", temp->name);
518 fflush (stdout);
519 i = getchar ();
520 if (i != '\n')
521 while (getchar () != '\n') ;
522 if (i == 'y' || i == 'Y')
523#endif /* DEBUG */
524 {
525 if (unlink (temp->name) < 0)
526 if (vflag)
527 perror_with_name (temp->name);
528 }
529 }
530}
531
532void
533clear_failure_queue ()
534{
535 failure_delete_queue = 0;
536}
537
538/* Compute a string to use as the base of all temporary file names.
539 It is substituted for %g. */
540
541void
542choose_temp_base ()
543{
544 extern char *getenv ();
545 char *base = getenv ("TMPDIR");
546 int len;
547
548 if (base == (char *)0)
549 base = "/tmp/";
550
551 len = strlen (base);
552 temp_filename = (char *) xmalloc (len + sizeof("/ccXXXXXX"));
553 strcpy (temp_filename, base);
554 if (len > 0 && temp_filename[len-1] != '/')
555 temp_filename[len++] = '/';
556 strcpy (temp_filename + len, "ccXXXXXX");
557
558 mktemp (temp_filename);
559 temp_filename_length = strlen (temp_filename);
560}
561\f
562/* Search for an execute file through our search path.
563 Return 0 if not found, otherwise return its name, allocated with malloc. */
564
565static char *
566find_exec_file (prog)
567 char *prog;
568{
569 int win = 0;
570 char *temp;
571 int size;
572
573 size = strlen (standard_exec_prefix);
574 if (user_exec_prefix != 0 && strlen (user_exec_prefix) > size)
575 size = strlen (user_exec_prefix);
576 if (env_exec_prefix != 0 && strlen (env_exec_prefix) > size)
577 size = strlen (env_exec_prefix);
578 if (strlen (standard_exec_prefix_1) > size)
579 size = strlen (standard_exec_prefix_1);
580 size += strlen (prog) + 1;
581 if (machine_suffix)
582 size += strlen (machine_suffix) + 1;
583 temp = (char *) xmalloc (size);
584
585 /* Determine the filename to execute. */
586
587 if (user_exec_prefix)
588 {
589 if (machine_suffix)
590 {
591 strcpy (temp, user_exec_prefix);
592 strcat (temp, machine_suffix);
593 strcat (temp, prog);
594 win = (access (temp, X_OK) == 0);
595 }
596 if (!win)
597 {
598 strcpy (temp, user_exec_prefix);
599 strcat (temp, prog);
600 win = (access (temp, X_OK) == 0);
601 }
602 }
603
604 if (!win && env_exec_prefix)
605 {
606 if (machine_suffix)
607 {
608 strcpy (temp, env_exec_prefix);
609 strcat (temp, machine_suffix);
610 strcat (temp, prog);
611 win = (access (temp, X_OK) == 0);
612 }
613 if (!win)
614 {
615 strcpy (temp, env_exec_prefix);
616 strcat (temp, prog);
617 win = (access (temp, X_OK) == 0);
618 }
619 }
620
621 if (!win)
622 {
623 if (machine_suffix)
624 {
625 strcpy (temp, standard_exec_prefix);
626 strcat (temp, machine_suffix);
627 strcat (temp, prog);
628 win = (access (temp, X_OK) == 0);
629 }
630 if (!win)
631 {
632 strcpy (temp, standard_exec_prefix);
633 strcat (temp, prog);
634 win = (access (temp, X_OK) == 0);
635 }
636 }
637
638 if (!win)
639 {
640 if (machine_suffix)
641 {
642 strcpy (temp, standard_exec_prefix_1);
643 strcat (temp, machine_suffix);
644 strcat (temp, prog);
645 win = (access (temp, X_OK) == 0);
646 }
647 if (!win)
648 {
649 strcpy (temp, standard_exec_prefix_1);
650 strcat (temp, prog);
651 win = (access (temp, X_OK) == 0);
652 }
653 }
654
655 if (win)
656 return temp;
657 else
658 return 0;
659}
660\f
661/* stdin file number. */
662#define STDIN_FILE_NO 0
663
664/* stdout file number. */
665#define STDOUT_FILE_NO 1
666
667/* value of `pipe': port index for reading. */
668#define READ_PORT 0
669
670/* value of `pipe': port index for writing. */
671#define WRITE_PORT 1
672
673/* Pipe waiting from last process, to be used as input for the next one.
674 Value is STDIN_FILE_NO if no pipe is waiting
675 (i.e. the next command is the first of a group). */
676
677int last_pipe_input;
678
679/* Fork one piped subcommand. FUNC is the system call to use
680 (either execv or execvp). ARGV is the arg vector to use.
681 NOT_LAST is nonzero if this is not the last subcommand
682 (i.e. its output should be piped to the next one.) */
683
684static int
685pexecute (func, program, argv, not_last)
686 char *program;
687 int (*func)();
688 char *argv[];
689 int not_last;
690{
691 int pid;
692 int pdes[2];
693 int input_desc = last_pipe_input;
694 int output_desc = STDOUT_FILE_NO;
695
696 /* If this isn't the last process, make a pipe for its output,
697 and record it as waiting to be the input to the next process. */
698
699 if (not_last)
700 {
701 if (pipe (pdes) < 0)
702 pfatal_with_name ("pipe");
703 output_desc = pdes[WRITE_PORT];
704 last_pipe_input = pdes[READ_PORT];
705 }
706 else
707 last_pipe_input = STDIN_FILE_NO;
708
709 pid = vfork ();
710
711 switch (pid)
712 {
713 case -1:
714 pfatal_with_name ("vfork");
715 break;
716
717 case 0: /* child */
718 /* Move the input and output pipes into place, if nec. */
719 if (input_desc != STDIN_FILE_NO)
720 {
721 close (STDIN_FILE_NO);
722 dup (input_desc);
723 close (input_desc);
724 }
725 if (output_desc != STDOUT_FILE_NO)
726 {
727 close (STDOUT_FILE_NO);
728 dup (output_desc);
729 close (output_desc);
730 }
731
732 /* Close the parent's descs that aren't wanted here. */
733 if (last_pipe_input != STDIN_FILE_NO)
734 close (last_pipe_input);
735
736 /* Exec the program. */
737 (*func) (program, argv);
738 perror_exec (program);
739 exit (-1);
740 /* NOTREACHED */
741
742 default:
743 /* In the parent, after forking.
744 Close the descriptors that we made for this child. */
745 if (input_desc != STDIN_FILE_NO)
746 close (input_desc);
747 if (output_desc != STDOUT_FILE_NO)
748 close (output_desc);
749
750 /* Return child's process number. */
751 return pid;
752 }
753}
754\f
755/* Execute the command specified by the arguments on the current line of spec.
756 When using pipes, this includes several piped-together commands
757 with `|' between them.
758
759 Return 0 if successful, -1 if failed. */
760
761int
762execute ()
763{
764 int i, j;
765 int n_commands; /* # of command. */
766 char *string;
767 struct command
768 {
769 char *prog; /* program name. */
770 char **argv; /* vector of args. */
771 int pid; /* pid of process for this command. */
772 };
773
774 struct command *commands; /* each command buffer with above info. */
775
776 /* Count # of piped commands. */
777 for (n_commands = 1, i = 0; i < argbuf_index; i++)
778 if (strcmp (argbuf[i], "|") == 0)
779 n_commands++;
780
781 /* Get storage for each command. */
782 commands
783 = (struct command *) alloca (n_commands * sizeof (struct command));
784
785 /* Split argbuf into its separate piped processes,
786 and record info about each one.
787 Also search for the programs that are to be run. */
788
789 commands[0].prog = argbuf[0]; /* first command. */
790 commands[0].argv = &argbuf[0];
791 string = find_exec_file (commands[0].prog);
792 if (string)
793 commands[0].argv[0] = string;
794
795 for (n_commands = 1, i = 0; i < argbuf_index; i++)
796 if (strcmp (argbuf[i], "|") == 0)
797 { /* each command. */
798 argbuf[i] = 0; /* termination of command args. */
799 commands[n_commands].prog = argbuf[i + 1];
800 commands[n_commands].argv = &argbuf[i + 1];
801 string = find_exec_file (commands[n_commands].prog);
802 if (string)
803 commands[n_commands].argv[0] = string;
804 n_commands++;
805 }
806
807 argbuf[argbuf_index] = 0;
808
809 /* If -v, print what we are about to do, and maybe query. */
810
811 if (vflag)
812 {
813 /* Print each piped command as a separate line. */
814 for (i = 0; i < n_commands ; i++)
815 {
816 char **j;
817
818 for (j = commands[i].argv; *j; j++)
819 fprintf (stderr, " %s", *j);
820
821 /* Print a pipe symbol after all but the last command. */
822 if (i + 1 != n_commands)
823 fprintf (stderr, " |");
824 fprintf (stderr, "\n");
825 }
826 fflush (stderr);
827#ifdef DEBUG
828 fprintf (stderr, "\nGo ahead? (y or n) ");
829 fflush (stderr);
830 j = getchar ();
831 if (j != '\n')
832 while (getchar () != '\n') ;
833 if (j != 'y' && j != 'Y')
834 return 0;
835#endif /* DEBUG */
836 }
837
838 /* Run each piped subprocess. */
839
840 last_pipe_input = STDIN_FILE_NO;
841 for (i = 0; i < n_commands; i++)
842 {
843 extern int execv(), execvp();
844 char *string = commands[i].argv[0];
845
846 commands[i].pid = pexecute ((string != commands[i].prog ? execv : execvp),
847 string, commands[i].argv,
848 i + 1 < n_commands);
849
850 if (string != commands[i].prog)
851 free (string);
852 }
853
854 execution_count++;
855
856 /* Wait for all the subprocesses to finish.
857 We don't care what order they finish in;
858 we know that N_COMMANDS waits will get them all. */
859
860 {
861 int ret_code = 0;
862
863 for (i = 0; i < n_commands; i++)
864 {
865 int status;
866 int pid;
867 char *prog;
868
869 pid = wait (&status);
870 if (pid < 0)
871 abort ();
872
873 if (status != 0)
874 {
875 int j;
876 for (j = 0; j < n_commands; j++)
877 if (commands[j].pid == pid)
878 prog = commands[j].prog;
879
880 if ((status & 0x7F) != 0)
881 fatal ("Program %s got fatal signal %d.", prog, (status & 0x7F));
882 if (((status & 0xFF00) >> 8) >= MIN_FATAL_STATUS)
883 ret_code = -1;
884 }
885 }
886 return ret_code;
887 }
888}
889\f
890/* Find all the switches given to us
891 and make a vector describing them.
892 The elements of the vector a strings, one per switch given.
893 If a switch uses the following argument, then the `part1' field
894 is the switch itself and the `part2' field is the following argument.
895 The `valid' field is nonzero if any spec has looked at this switch;
896 if it remains zero at the end of the run, it must be meaningless. */
897
898struct switchstr
899{
900 char *part1;
901 char *part2;
902 int valid;
903};
904
905struct switchstr *switches;
906
907int n_switches;
908
909/* Also a vector of input files specified. */
910
911char **infiles;
912
913int n_infiles;
914
915/* And a vector of corresponding output files is made up later. */
916
917char **outfiles;
918
919/* Create the vector `switches' and its contents.
920 Store its length in `n_switches'. */
921
922void
923process_command (argc, argv)
924 int argc;
925 char **argv;
926{
927 extern char *getenv ();
928 register int i;
929 n_switches = 0;
930 n_infiles = 0;
931
932 env_exec_prefix = getenv ("GCC_EXEC_PREFIX");
933
934 /* Scan argv twice. Here, the first time, just count how many switches
935 there will be in their vector, and how many input files in theirs.
936 Here we also parse the switches that cc itself uses (e.g. -v). */
937
938 for (i = 1; i < argc; i++)
939 {
940 if (argv[i][0] == '-' && argv[i][1] != 'l')
941 {
942 register char *p = &argv[i][1];
943 register int c = *p;
944
945 switch (c)
946 {
947 case 'b':
948 machine_suffix = p + 1;
949 break;
950
951 case 'B':
952 user_exec_prefix = p + 1;
953 break;
954
955 case 'v': /* Print our subcommands and print versions. */
956 vflag++;
957 n_switches++;
958 break;
959
960 default:
961 n_switches++;
962
963 if (SWITCH_TAKES_ARG (c) && p[1] == 0)
964 i++;
965 else if (WORD_SWITCH_TAKES_ARG (p))
966 i++;
967 }
968 }
969 else
970 n_infiles++;
971 }
972
973 /* Then create the space for the vectors and scan again. */
974
975 switches = ((struct switchstr *)
976 xmalloc ((n_switches + 1) * sizeof (struct switchstr)));
977 infiles = (char **) xmalloc ((n_infiles + 1) * sizeof (char *));
978 n_switches = 0;
979 n_infiles = 0;
980
981 /* This, time, copy the text of each switch and store a pointer
982 to the copy in the vector of switches.
983 Store all the infiles in their vector. */
984
985 for (i = 1; i < argc; i++)
986 {
987 if (argv[i][0] == '-' && argv[i][1] != 'l')
988 {
989 register char *p = &argv[i][1];
990 register int c = *p;
991
992 if (c == 'B' || c == 'b')
993 continue;
994 switches[n_switches].part1 = p;
995 if ((SWITCH_TAKES_ARG (c) && p[1] == 0)
996 || WORD_SWITCH_TAKES_ARG (p))
997 switches[n_switches].part2 = argv[++i];
998 else if (c == 'o') {
999 /* On some systems, ld cannot handle -o without space.
1000 So split the -o and its argument. */
1001 switches[n_switches].part2 = (char *) xmalloc (strlen (p));
1002 strcpy (switches[n_switches].part2, &p[1]);
1003 p[1] = 0;
1004 } else
1005 switches[n_switches].part2 = 0;
1006 switches[n_switches].valid = 0;
1007 n_switches++;
1008 }
1009 else
1010 infiles[n_infiles++] = argv[i];
1011 }
1012
1013 switches[n_switches].part1 = 0;
1014 infiles[n_infiles] = 0;
1015}
1016\f
1017/* Process a spec string, accumulating and running commands. */
1018
1019/* These variables describe the input file name.
1020 input_file_number is the index on outfiles of this file,
1021 so that the output file name can be stored for later use by %o.
1022 input_basename is the start of the part of the input file
1023 sans all directory names, and basename_length is the number
1024 of characters starting there excluding the suffix .c or whatever. */
1025
1026char *input_filename;
1027int input_file_number;
1028int input_filename_length;
1029int basename_length;
1030char *input_basename;
1031
1032/* These are variables used within do_spec and do_spec_1. */
1033
1034/* Nonzero if an arg has been started and not yet terminated
1035 (with space, tab or newline). */
1036int arg_going;
1037
1038/* Nonzero means %d or %g has been seen; the next arg to be terminated
1039 is a temporary file name. */
1040int delete_this_arg;
1041
1042/* Nonzero means %w has been seen; the next arg to be terminated
1043 is the output file name of this compilation. */
1044int this_is_output_file;
1045
1046/* Nonzero means %s has been seen; the next arg to be terminated
1047 is the name of a library file and we should try the standard
1048 search dirs for it. */
1049int this_is_library_file;
1050
1051/* Process the spec SPEC and run the commands specified therein.
1052 Returns 0 if the spec is successfully processed; -1 if failed. */
1053
1054int
1055do_spec (spec)
1056 char *spec;
1057{
1058 int value;
1059
1060 clear_args ();
1061 arg_going = 0;
1062 delete_this_arg = 0;
1063 this_is_output_file = 0;
1064 this_is_library_file = 0;
1065
1066 value = do_spec_1 (spec, 0);
1067
1068 /* Force out any unfinished command.
1069 If -pipe, this forces out the last command if it ended in `|'. */
1070 if (value == 0)
1071 {
1072 if (argbuf_index > 0 && !strcmp (argbuf[argbuf_index - 1], "|"))
1073 argbuf_index--;
1074
1075 if (argbuf_index > 0)
1076 value = execute ();
1077 }
1078
1079 return value;
1080}
1081
1082/* Process the sub-spec SPEC as a portion of a larger spec.
1083 This is like processing a whole spec except that we do
1084 not initialize at the beginning and we do not supply a
1085 newline by default at the end.
1086 INSWITCH nonzero means don't process %-sequences in SPEC;
1087 in this case, % is treated as an ordinary character.
1088 This is used while substituting switches.
1089 INSWITCH nonzero also causes SPC not to terminate an argument.
1090
1091 Value is zero unless a line was finished
1092 and the command on that line reported an error. */
1093
1094int
1095do_spec_1 (spec, inswitch)
1096 char *spec;
1097 int inswitch;
1098{
1099 register char *p = spec;
1100 register int c;
1101 char *string;
1102
1103 while (c = *p++)
1104 /* If substituting a switch, treat all chars like letters.
1105 Otherwise, NL, SPC, TAB and % are special. */
1106 switch (inswitch ? 'a' : c)
1107 {
1108 case '\n':
1109 /* End of line: finish any pending argument,
1110 then run the pending command if one has been started. */
1111 if (arg_going)
1112 {
1113 obstack_1grow (&obstack, 0);
1114 string = obstack_finish (&obstack);
1115 if (this_is_library_file)
1116 string = find_file (string);
1117 store_arg (string, delete_this_arg, this_is_output_file);
1118 if (this_is_output_file)
1119 outfiles[input_file_number] = string;
1120 }
1121 arg_going = 0;
1122
1123 if (argbuf_index > 0 && !strcmp (argbuf[argbuf_index - 1], "|"))
1124 {
1125 int i;
1126 for (i = 0; i < n_switches; i++)
1127 if (!strcmp (switches[i].part1, "pipe"))
1128 break;
1129
1130 /* A `|' before the newline means use a pipe here,
1131 but only if -pipe was specified.
1132 Otherwise, execute now and don't pass the `|' as an arg. */
1133 if (i < n_switches)
1134 {
1135 switches[i].valid = 1;
1136 break;
1137 }
1138 else
1139 argbuf_index--;
1140 }
1141
1142 if (argbuf_index > 0)
1143 {
1144 int value = execute ();
1145 if (value)
1146 return value;
1147 }
1148 /* Reinitialize for a new command, and for a new argument. */
1149 clear_args ();
1150 arg_going = 0;
1151 delete_this_arg = 0;
1152 this_is_output_file = 0;
1153 this_is_library_file = 0;
1154 break;
1155
1156 case '|':
1157 /* End any pending argument. */
1158 if (arg_going)
1159 {
1160 obstack_1grow (&obstack, 0);
1161 string = obstack_finish (&obstack);
1162 if (this_is_library_file)
1163 string = find_file (string);
1164 store_arg (string, delete_this_arg, this_is_output_file);
1165 if (this_is_output_file)
1166 outfiles[input_file_number] = string;
1167 }
1168
1169 /* Use pipe */
1170 obstack_1grow (&obstack, c);
1171 arg_going = 1;
1172 break;
1173
1174 case '\t':
1175 case ' ':
1176 /* Space or tab ends an argument if one is pending. */
1177 if (arg_going)
1178 {
1179 obstack_1grow (&obstack, 0);
1180 string = obstack_finish (&obstack);
1181 if (this_is_library_file)
1182 string = find_file (string);
1183 store_arg (string, delete_this_arg, this_is_output_file);
1184 if (this_is_output_file)
1185 outfiles[input_file_number] = string;
1186 }
1187 /* Reinitialize for a new argument. */
1188 arg_going = 0;
1189 delete_this_arg = 0;
1190 this_is_output_file = 0;
1191 this_is_library_file = 0;
1192 break;
1193
1194 case '%':
1195 switch (c = *p++)
1196 {
1197 case 0:
1198 fatal ("Invalid specification! Bug in cc.");
1199
1200 case 'b':
1201 obstack_grow (&obstack, input_basename, basename_length);
1202 arg_going = 1;
1203 break;
1204
1205 case 'd':
1206 delete_this_arg = 2;
1207 break;
1208
1209 case 'e':
1210 /* {...:%efoo} means report an error with `foo' as error message
1211 and don't execute any more commands for this file. */
1212 {
1213 char *q = p;
1214 char *buf;
1215 while (*p != 0 && *p != '\n') p++;
1216 buf = (char *) alloca (p - q + 1);
1217 strncpy (buf, q, p - q);
1218 buf[p - q] = 0;
1219 error ("%s", buf);
1220 return -1;
1221 }
1222 break;
1223
1224 case 'g':
1225 obstack_grow (&obstack, temp_filename, temp_filename_length);
1226 delete_this_arg = 1;
1227 arg_going = 1;
1228 break;
1229
1230 case 'i':
1231 obstack_grow (&obstack, input_filename, input_filename_length);
1232 arg_going = 1;
1233 break;
1234
1235 case 'o':
1236 {
1237 register int f;
1238 for (f = 0; f < n_infiles; f++)
1239 store_arg (outfiles[f], 0, 0);
1240 }
1241 break;
1242
1243 case 's':
1244 this_is_library_file = 1;
1245 break;
1246
1247 case 'W':
1248 {
1249 int index = argbuf_index;
1250 /* Handle the {...} following the %W. */
1251 if (*p != '{')
1252 abort ();
1253 p = handle_braces (p + 1);
1254 if (p == 0)
1255 return -1;
1256 /* If any args were output, mark the last one for deletion
1257 on failure. */
1258 if (argbuf_index != index)
1259 record_temp_file (argbuf[argbuf_index - 1], 0, 1);
1260 break;
1261 }
1262
1263 case 'w':
1264 this_is_output_file = 1;
1265 break;
1266
1267 case '{':
1268 p = handle_braces (p);
1269 if (p == 0)
1270 return -1;
1271 break;
1272
1273 case '%':
1274 obstack_1grow (&obstack, '%');
1275 break;
1276
1277/*** The rest just process a certain constant string as a spec. */
1278
1279 case '1':
1280 do_spec_1 (CC1_SPEC, 0);
1281 break;
1282
1283 case 'a':
1284 do_spec_1 (ASM_SPEC, 0);
1285 break;
1286
1287 case 'c':
1288 do_spec_1 (SIGNED_CHAR_SPEC, 0);
1289 break;
1290
1291 case 'C':
1292 do_spec_1 (CPP_SPEC, 0);
1293 break;
1294
1295 case 'l':
1296 do_spec_1 (LINK_SPEC, 0);
1297 break;
1298
1299 case 'L':
1300 do_spec_1 (LIB_SPEC, 0);
1301 break;
1302
1303 case 'p':
1304 do_spec_1 (CPP_PREDEFINES, 0);
1305 break;
1306
1307 case 'P':
1308 {
1309 char *x = (char *) alloca (strlen (CPP_PREDEFINES) * 2 + 1);
1310 char *buf = x;
1311 char *y = CPP_PREDEFINES;
1312
1313 /* Copy all of CPP_PREDEFINES into BUF,
1314 but put __ after every -D and at the end of each arg, */
1315 while (1)
1316 {
1317 if (! strncmp (y, "-D", 2))
1318 {
1319 *x++ = '-';
1320 *x++ = 'D';
1321 *x++ = '_';
1322 *x++ = '_';
1323 y += 2;
1324 }
1325 else if (*y == ' ' || *y == 0)
1326 {
1327 *x++ = '_';
1328 *x++ = '_';
1329 if (*y == 0)
1330 break;
1331 else
1332 *x++ = *y++;
1333 }
1334 else
1335 *x++ = *y++;
1336 }
1337 *x = 0;
1338
1339 do_spec_1 (buf, 0);
1340 }
1341 break;
1342
1343 case 'S':
1344 do_spec_1 (STARTFILE_SPEC, 0);
1345 break;
1346
1347 case 'E':
1348 do_spec_1 (ENDFILE_SPEC, 0);
1349 break;
1350
1351 default:
1352 abort ();
1353 }
1354 break;
1355
1356 default:
1357 /* Ordinary character: put it into the current argument. */
1358 obstack_1grow (&obstack, c);
1359 arg_going = 1;
1360 }
1361
1362 return 0; /* End of string */
1363}
1364
1365/* Return 0 if we call do_spec_1 and that returns -1. */
1366
1367char *
1368handle_braces (p)
1369 register char *p;
1370{
1371 register char *q;
1372 char *filter;
1373 int pipe = 0;
1374 int negate = 0;
1375
1376 if (*p == '|')
1377 /* A `|' after the open-brace means,
1378 if the test fails, output a single minus sign rather than nothing.
1379 This is used in %{|!pipe:...}. */
1380 pipe = 1, ++p;
1381
1382 if (*p == '!')
1383 /* A `!' after the open-brace negates the condition:
1384 succeed if the specified switch is not present. */
1385 negate = 1, ++p;
1386
1387 filter = p;
1388 while (*p != ':' && *p != '}') p++;
1389 if (*p != '}')
1390 {
1391 register int count = 1;
1392 q = p + 1;
1393 while (count > 0)
1394 {
1395 if (*q == '{')
1396 count++;
1397 else if (*q == '}')
1398 count--;
1399 else if (*q == 0)
1400 abort ();
1401 q++;
1402 }
1403 }
1404 else
1405 q = p + 1;
1406
1407 if (p[-1] == '*' && p[0] == '}')
1408 {
1409 /* Substitute all matching switches as separate args. */
1410 register int i;
1411 --p;
1412 for (i = 0; i < n_switches; i++)
1413 if (!strncmp (switches[i].part1, filter, p - filter))
1414 give_switch (i);
1415 }
1416 else
1417 {
1418 /* Test for presence of the specified switch. */
1419 register int i;
1420 int present = 0;
1421
1422 /* If name specified ends in *, as in {x*:...},
1423 check for presence of any switch name starting with x. */
1424 if (p[-1] == '*')
1425 {
1426 for (i = 0; i < n_switches; i++)
1427 {
1428 if (!strncmp (switches[i].part1, filter, p - filter - 1))
1429 {
1430 switches[i].valid = 1;
1431 present = 1;
1432 }
1433 }
1434 }
1435 /* Otherwise, check for presence of exact name specified. */
1436 else
1437 {
1438 for (i = 0; i < n_switches; i++)
1439 {
1440 if (!strncmp (switches[i].part1, filter, p - filter)
1441 && switches[i].part1[p - filter] == 0)
1442 {
1443 switches[i].valid = 1;
1444 present = 1;
1445 break;
1446 }
1447 }
1448 }
1449
1450 /* If it is as desired (present for %{s...}, absent for %{-s...})
1451 then substitute either the switch or the specified
1452 conditional text. */
1453 if (present != negate)
1454 {
1455 if (*p == '}')
1456 {
1457 give_switch (i);
1458 }
1459 else
1460 {
1461 if (do_spec_1 (save_string (p + 1, q - p - 2), 0) < 0)
1462 return 0;
1463 }
1464 }
1465 else if (pipe)
1466 {
1467 /* Here if a %{|...} conditional fails: output a minus sign,
1468 which means "standard output" or "standard input". */
1469 do_spec_1 ("-", 0);
1470 }
1471 }
1472
1473 return q;
1474}
1475
1476/* Pass a switch to the current accumulating command
1477 in the same form that we received it.
1478 SWITCHNUM identifies the switch; it is an index into
1479 the vector of switches gcc received, which is `switches'.
1480 This cannot fail since it never finishes a command line. */
1481
1482give_switch (switchnum)
1483 int switchnum;
1484{
1485 do_spec_1 ("-", 0);
1486 do_spec_1 (switches[switchnum].part1, 1);
1487 do_spec_1 (" ", 0);
1488 if (switches[switchnum].part2 != 0)
1489 {
1490 do_spec_1 (switches[switchnum].part2, 1);
1491 do_spec_1 (" ", 0);
1492 }
1493 switches[switchnum].valid = 1;
1494}
1495\f
1496/* Search for a file named NAME trying various prefixes including the
1497 user's -B prefix and some standard ones.
1498 Return the absolute pathname found. If nothing is found, return NAME. */
1499
1500char *
1501find_file (name)
1502 char *name;
1503{
1504 int size;
1505 char *temp;
1506 int win = 0;
1507
1508 /* Compute maximum size of NAME plus any prefix we will try. */
1509
1510 size = strlen (standard_exec_prefix);
1511 if (user_exec_prefix != 0 && strlen (user_exec_prefix) > size)
1512 size = strlen (user_exec_prefix);
1513 if (env_exec_prefix != 0 && strlen (env_exec_prefix) > size)
1514 size = strlen (env_exec_prefix);
1515 if (strlen (standard_exec_prefix) > size)
1516 size = strlen (standard_exec_prefix);
1517 if (strlen (standard_exec_prefix_1) > size)
1518 size = strlen (standard_exec_prefix_1);
1519 if (strlen (standard_startfile_prefix) > size)
1520 size = strlen (standard_startfile_prefix);
1521 if (strlen (standard_startfile_prefix_1) > size)
1522 size = strlen (standard_startfile_prefix_1);
1523 if (strlen (standard_startfile_prefix_2) > size)
1524 size = strlen (standard_startfile_prefix_2);
1525 if (machine_suffix)
1526 size += strlen (machine_suffix) + 1;
1527 size += strlen (name) + 1;
1528
1529 temp = (char *) alloca (size);
1530
1531 if (user_exec_prefix)
1532 {
1533 if (machine_suffix)
1534 {
1535 strcpy (temp, user_exec_prefix);
1536 strcat (temp, machine_suffix);
1537 strcat (temp, name);
1538 win = (access (temp, R_OK) == 0);
1539 }
1540 if (!win)
1541 {
1542 strcpy (temp, user_exec_prefix);
1543 strcat (temp, name);
1544 win = (access (temp, R_OK) == 0);
1545 }
1546 }
1547
1548 if (!win && env_exec_prefix)
1549 {
1550 if (machine_suffix)
1551 {
1552 strcpy (temp, env_exec_prefix);
1553 strcat (temp, machine_suffix);
1554 strcat (temp, name);
1555 win = (access (temp, R_OK) == 0);
1556 }
1557 if (!win)
1558 {
1559 strcpy (temp, env_exec_prefix);
1560 strcat (temp, name);
1561 win = (access (temp, R_OK) == 0);
1562 }
1563 }
1564
1565 if (!win)
1566 {
1567 if (machine_suffix)
1568 {
1569 strcpy (temp, standard_exec_prefix);
1570 strcat (temp, machine_suffix);
1571 strcat (temp, name);
1572 win = (access (temp, R_OK) == 0);
1573 }
1574 if (!win)
1575 {
1576 strcpy (temp, standard_exec_prefix);
1577 strcat (temp, name);
1578 win = (access (temp, R_OK) == 0);
1579 }
1580 }
1581
1582 if (!win)
1583 {
1584 if (machine_suffix)
1585 {
1586 strcpy (temp, standard_exec_prefix_1);
1587 strcat (temp, machine_suffix);
1588 strcat (temp, name);
1589 win = (access (temp, R_OK) == 0);
1590 }
1591 if (!win)
1592 {
1593 strcpy (temp, standard_exec_prefix_1);
1594 strcat (temp, name);
1595 win = (access (temp, R_OK) == 0);
1596 }
1597 }
1598
1599 if (!win)
1600 {
1601 if (machine_suffix)
1602 {
1603 strcpy (temp, standard_startfile_prefix);
1604 strcat (temp, machine_suffix);
1605 strcat (temp, name);
1606 win = (access (temp, R_OK) == 0);
1607 }
1608 if (!win)
1609 {
1610 strcpy (temp, standard_startfile_prefix);
1611 strcat (temp, name);
1612 win = (access (temp, R_OK) == 0);
1613 }
1614 }
1615
1616 if (!win)
1617 {
1618 if (machine_suffix)
1619 {
1620 strcpy (temp, standard_startfile_prefix_1);
1621 strcat (temp, machine_suffix);
1622 strcat (temp, name);
1623 win = (access (temp, R_OK) == 0);
1624 }
1625 if (!win)
1626 {
1627 strcpy (temp, standard_startfile_prefix_1);
1628 strcat (temp, name);
1629 win = (access (temp, R_OK) == 0);
1630 }
1631 }
1632
1633 if (!win)
1634 {
1635 if (machine_suffix)
1636 {
1637 strcpy (temp, standard_startfile_prefix_2);
1638 strcat (temp, machine_suffix);
1639 strcat (temp, name);
1640 win = (access (temp, R_OK) == 0);
1641 }
1642 if (!win)
1643 {
1644 strcpy (temp, standard_startfile_prefix_2);
1645 strcat (temp, name);
1646 win = (access (temp, R_OK) == 0);
1647 }
1648 }
1649
1650 if (!win)
1651 {
1652 if (machine_suffix)
1653 {
1654 strcpy (temp, "./");
1655 strcat (temp, machine_suffix);
1656 strcat (temp, name);
1657 win = (access (temp, R_OK) == 0);
1658 }
1659 if (!win)
1660 {
1661 strcpy (temp, "./");
1662 strcat (temp, name);
1663 win = (access (temp, R_OK) == 0);
1664 }
1665 }
1666
1667 if (win)
1668 return save_string (temp, strlen (temp));
1669 return name;
1670}
1671\f
1672/* On fatal signals, delete all the temporary files. */
1673
1674void
1675fatal_error (signum)
1676 int signum;
1677{
1678 signal (signum, SIG_DFL);
1679 delete_failure_queue ();
1680 delete_temp_files ();
1681 /* Get the same signal again, this time not handled,
1682 so its normal effect occurs. */
1683 kill (getpid (), signum);
1684}
1685
1686int
1687main (argc, argv)
1688 int argc;
1689 char **argv;
1690{
1691 register int i;
1692 int value;
1693 int error_count = 0;
1694 int linker_was_run = 0;
1695 char *explicit_link_files;
1696
1697 programname = argv[0];
1698
1699 if (signal (SIGINT, SIG_IGN) != SIG_IGN)
1700 signal (SIGINT, fatal_error);
1701 if (signal (SIGHUP, SIG_IGN) != SIG_IGN)
1702 signal (SIGHUP, fatal_error);
1703 if (signal (SIGTERM, SIG_IGN) != SIG_IGN)
1704 signal (SIGTERM, fatal_error);
1705 if (signal (SIGPIPE, SIG_IGN) != SIG_IGN)
1706 signal (SIGPIPE, fatal_error);
1707
1708 argbuf_length = 10;
1709 argbuf = (char **) xmalloc (argbuf_length * sizeof (char *));
1710
1711 obstack_init (&obstack);
1712
1713 choose_temp_base ();
1714
1715 /* Make a table of what switches there are (switches, n_switches).
1716 Make a table of specified input files (infiles, n_infiles). */
1717
1718 process_command (argc, argv);
1719
1720 if (vflag)
1721 {
1722 extern char *version_string;
1723 fprintf (stderr, "gcc version %s\n", version_string);
1724 if (n_infiles == 0)
1725 exit (0);
1726 }
1727
1728 if (n_infiles == 0)
1729 fatal ("No input files specified.");
1730
1731 /* Make a place to record the compiler output file names
1732 that correspond to the input files. */
1733
1734 outfiles = (char **) xmalloc (n_infiles * sizeof (char *));
1735 bzero (outfiles, n_infiles * sizeof (char *));
1736
1737 /* Record which files were specified explicitly as link input. */
1738
1739 explicit_link_files = (char *) xmalloc (n_infiles);
1740 bzero (explicit_link_files, n_infiles);
1741
1742 for (i = 0; i < n_infiles; i++)
1743 {
1744 register struct compiler *cp;
1745 int this_file_error = 0;
1746
1747 /* Tell do_spec what to substitute for %i. */
1748
1749 input_filename = infiles[i];
1750 input_filename_length = strlen (input_filename);
1751 input_file_number = i;
1752
1753 /* Use the same thing in %o, unless cp->spec says otherwise. */
1754
1755 outfiles[i] = input_filename;
1756
1757 /* Figure out which compiler from the file's suffix. */
1758
1759 for (cp = compilers; cp->spec; cp++)
1760 {
1761 if (strlen (cp->suffix) < input_filename_length
1762 && !strcmp (cp->suffix,
1763 infiles[i] + input_filename_length
1764 - strlen (cp->suffix)))
1765 {
1766 /* Ok, we found an applicable compiler. Run its spec. */
1767 /* First say how much of input_filename to substitute for %b */
1768 register char *p;
1769
1770 input_basename = input_filename;
1771 for (p = input_filename; *p; p++)
1772 if (*p == '/')
1773 input_basename = p + 1;
1774 basename_length = (input_filename_length - strlen (cp->suffix)
1775 - (input_basename - input_filename));
1776 value = do_spec (cp->spec);
1777 if (value < 0)
1778 this_file_error = 1;
1779 break;
1780 }
1781 }
1782
1783 /* If this file's name does not contain a recognized suffix,
1784 record it as explicit linker input. */
1785
1786 if (! cp->spec)
1787 explicit_link_files[i] = 1;
1788
1789 /* Clear the delete-on-failure queue, deleting the files in it
1790 if this compilation failed. */
1791
1792 if (this_file_error)
1793 {
1794 delete_failure_queue ();
1795 error_count++;
1796 }
1797 /* If this compilation succeeded, don't delete those files later. */
1798 clear_failure_queue ();
1799 }
1800
1801 /* Run ld to link all the compiler output files. */
1802
1803 if (error_count == 0)
1804 {
1805 int tmp = execution_count;
1806 value = do_spec (link_spec);
1807 if (value < 0)
1808 error_count = 1;
1809 linker_was_run = (tmp != execution_count);
1810 }
1811
1812 /* If options said don't run linker,
1813 complain about input files to be given to the linker. */
1814
1815 if (! linker_was_run && error_count == 0)
1816 for (i = 0; i < n_infiles; i++)
1817 if (explicit_link_files[i])
1818 error ("%s: linker input file unused since linking not done",
1819 outfiles[i]);
1820
1821 /* Set the `valid' bits for switches that match anything in any spec. */
1822
1823 validate_all_switches ();
1824
1825 /* Warn about any switches that no pass was interested in. */
1826
1827 for (i = 0; i < n_switches; i++)
1828 if (! switches[i].valid)
1829 error ("unrecognized option `-%s'", switches[i].part1);
1830
1831 /* Delete some or all of the temporary files we made. */
1832
1833 if (error_count)
1834 delete_failure_queue ();
1835 delete_temp_files ();
1836
1837 exit (error_count);
1838}
1839\f
1840xmalloc (size)
1841 int size;
1842{
1843 register int value = malloc (size);
1844 if (value == 0)
1845 fatal ("Virtual memory full.");
1846 return value;
1847}
1848
1849xrealloc (ptr, size)
1850 int ptr, size;
1851{
1852 register int value = realloc (ptr, size);
1853 if (value == 0)
1854 fatal ("Virtual memory full.");
1855 return value;
1856}
1857
1858/* Return a newly-allocated string whose contents concatenate those of s1, s2, s3. */
1859
1860char *
1861concat (s1, s2, s3)
1862 char *s1, *s2, *s3;
1863{
1864 int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
1865 char *result = (char *) xmalloc (len1 + len2 + len3 + 1);
1866
1867 strcpy (result, s1);
1868 strcpy (result + len1, s2);
1869 strcpy (result + len1 + len2, s3);
1870 *(result + len1 + len2 + len3) = 0;
1871
1872 return result;
1873}
1874
1875char *
1876save_string (s, len)
1877 char *s;
1878 int len;
1879{
1880 register char *result = (char *) xmalloc (len + 1);
1881
1882 bcopy (s, result, len);
1883 result[len] = 0;
1884 return result;
1885}
1886
1887pfatal_with_name (name)
1888 char *name;
1889{
1890 extern int errno, sys_nerr;
1891 extern char *sys_errlist[];
1892 char *s;
1893
1894 if (errno < sys_nerr)
1895 s = concat ("%s: ", sys_errlist[errno], "");
1896 else
1897 s = "cannot open %s";
1898 fatal (s, name);
1899}
1900
1901perror_with_name (name)
1902 char *name;
1903{
1904 extern int errno, sys_nerr;
1905 extern char *sys_errlist[];
1906 char *s;
1907
1908 if (errno < sys_nerr)
1909 s = concat ("%s: ", sys_errlist[errno], "");
1910 else
1911 s = "cannot open %s";
1912 error (s, name);
1913}
1914
1915perror_exec (name)
1916 char *name;
1917{
1918 extern int errno, sys_nerr;
1919 extern char *sys_errlist[];
1920 char *s;
1921
1922 if (errno < sys_nerr)
1923 s = concat ("installation problem, cannot exec %s: ",
1924 sys_errlist[errno], "");
1925 else
1926 s = "installation problem, cannot exec %s";
1927 error (s, name);
1928}
1929
1930/* More 'friendly' abort that prints the line and file.
1931 config.h can #define abort fancy_abort if you like that sort of thing. */
1932
1933void
1934fancy_abort ()
1935{
1936 fatal ("Internal gcc abort.");
1937}
1938\f
1939#ifdef HAVE_VPRINTF
1940
1941/* Output an error message and exit */
1942
1943int
1944fatal (va_alist)
1945 va_dcl
1946{
1947 va_list ap;
1948 char *format;
1949
1950 va_start(ap);
1951 format = va_arg (ap, char *);
1952 vfprintf (stderr, format, ap);
1953 va_end (ap);
1954 fprintf (stderr, "\n");
1955 delete_temp_files ();
1956 exit (1);
1957}
1958
1959error (va_alist)
1960 va_dcl
1961{
1962 va_list ap;
1963 char *format;
1964
1965 va_start(ap);
1966 format = va_arg (ap, char *);
1967 fprintf (stderr, "%s: ", programname);
1968 vfprintf (stderr, format, ap);
1969 va_end (ap);
1970
1971 fprintf (stderr, "\n");
1972}
1973
1974#else /* not HAVE_VPRINTF */
1975
1976fatal (msg, arg1, arg2)
1977 char *msg, *arg1, *arg2;
1978{
1979 error (msg, arg1, arg2);
1980 delete_temp_files (0);
1981 exit (1);
1982}
1983
1984error (msg, arg1, arg2)
1985 char *msg, *arg1, *arg2;
1986{
1987 fprintf (stderr, "%s: ", programname);
1988 fprintf (stderr, msg, arg1, arg2);
1989 fprintf (stderr, "\n");
1990}
1991
1992#endif /* not HAVE_VPRINTF */
1993
1994\f
1995void
1996validate_all_switches ()
1997{
1998 struct compiler *comp;
1999 register char *p;
2000 register char c;
2001
2002 for (comp = compilers; comp->spec; comp++)
2003 {
2004 p = comp->spec;
2005 while (c = *p++)
2006 if (c == '%' && *p == '{')
2007 /* We have a switch spec. */
2008 validate_switches (p + 1);
2009 }
2010
2011 p = link_spec;
2012 while (c = *p++)
2013 if (c == '%' && *p == '{')
2014 /* We have a switch spec. */
2015 validate_switches (p + 1);
2016
2017 /* Now notice switches mentioned in the machine-specific specs. */
2018
2019#ifdef ASM_SPEC
2020 p = ASM_SPEC;
2021 while (c = *p++)
2022 if (c == '%' && *p == '{')
2023 /* We have a switch spec. */
2024 validate_switches (p + 1);
2025#endif
2026
2027#ifdef CPP_SPEC
2028 p = CPP_SPEC;
2029 while (c = *p++)
2030 if (c == '%' && *p == '{')
2031 /* We have a switch spec. */
2032 validate_switches (p + 1);
2033#endif
2034
2035#ifdef SIGNED_CHAR_SPEC
2036 p = SIGNED_CHAR_SPEC;
2037 while (c = *p++)
2038 if (c == '%' && *p == '{')
2039 /* We have a switch spec. */
2040 validate_switches (p + 1);
2041#endif
2042
2043#ifdef CC1_SPEC
2044 p = CC1_SPEC;
2045 while (c = *p++)
2046 if (c == '%' && *p == '{')
2047 /* We have a switch spec. */
2048 validate_switches (p + 1);
2049#endif
2050
2051#ifdef LINK_SPEC
2052 p = LINK_SPEC;
2053 while (c = *p++)
2054 if (c == '%' && *p == '{')
2055 /* We have a switch spec. */
2056 validate_switches (p + 1);
2057#endif
2058
2059#ifdef LIB_SPEC
2060 p = LIB_SPEC;
2061 while (c = *p++)
2062 if (c == '%' && *p == '{')
2063 /* We have a switch spec. */
2064 validate_switches (p + 1);
2065#endif
2066
2067#ifdef STARTFILE_SPEC
2068 p = STARTFILE_SPEC;
2069 while (c = *p++)
2070 if (c == '%' && *p == '{')
2071 /* We have a switch spec. */
2072 validate_switches (p + 1);
2073#endif
2074}
2075
2076/* Look at the switch-name that comes after START
2077 and mark as valid all supplied switches that match it. */
2078
2079void
2080validate_switches (start)
2081 char *start;
2082{
2083 register char *p = start;
2084 char *filter;
2085 register int i;
2086
2087 if (*p == '|')
2088 ++p;
2089
2090 if (*p == '!')
2091 ++p;
2092
2093 filter = p;
2094 while (*p != ':' && *p != '}') p++;
2095
2096 if (p[-1] == '*')
2097 {
2098 /* Mark all matching switches as valid. */
2099 --p;
2100 for (i = 0; i < n_switches; i++)
2101 if (!strncmp (switches[i].part1, filter, p - filter))
2102 switches[i].valid = 1;
2103 }
2104 else
2105 {
2106 /* Mark an exact matching switch as valid. */
2107 for (i = 0; i < n_switches; i++)
2108 {
2109 if (!strncmp (switches[i].part1, filter, p - filter)
2110 && switches[i].part1[p - filter] == 0)
2111 switches[i].valid = 1;
2112 }
2113 }
2114}