-
Notifications
You must be signed in to change notification settings - Fork 0
/
gcc-save.c
2107 lines (1770 loc) · 52 KB
/
gcc-save.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Compiler driver program that can handle many languages.
Copyright (C) 1987,1989 Free Software Foundation, Inc.
This file is part of GNU CC.
GNU CC is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 1, or (at your option)
any later version.
GNU CC is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU CC; see the file COPYING. If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
This paragraph is here to try to keep Sun CC from dying.
The number of chars here seems crucial!!!! */
void record_temp_file ();
/* This program is the user interface to the C compiler and possibly to
other compilers. It is used because compilation is a complicated procedure
which involves running several programs and passing temporary files between
them, forwarding the users switches to those programs selectively,
and deleting the temporary files at the end.
CC recognizes how to compile each input file by suffixes in the file names.
Once it knows which kind of compilation to perform, the procedure for
compilation is specified by a string called a "spec".
Specs are strings containing lines, each of which (if not blank)
is made up of a program name, and arguments separated by spaces.
The program name must be exact and start from root, since no path
is searched and it is unreliable to depend on the current working directory.
Redirection of input or output is not supported; the subprograms must
accept filenames saying what files to read and write.
In addition, the specs can contain %-sequences to substitute variable text
or for conditional text. Here is a table of all defined %-sequences.
Note that spaces are not generated automatically around the results of
expanding these sequences; therefore, you can concatenate them together
or with constant text in a single argument.
%% substitute one % into the program name or argument.
%i substitute the name of the input file being processed.
%b substitute the basename of the input file being processed.
This is the substring up to (and not including) the last period.
%g substitute the temporary-file-name-base. This is a string chosen
once per compilation. Different temporary file names are made by
concatenation of constant strings on the end, as in `%g.s'.
%g also has the same effect of %d.
%d marks the argument containing or following the %d as a
temporary file name, so that that file will be deleted if CC exits
successfully. Unlike %g, this contributes no text to the argument.
%w marks the argument containing or following the %w as the
"output file" of this compilation. This puts the argument
into the sequence of arguments that %o will substitute later.
%o substitutes the names of all the output files, with spaces
automatically placed around them. You should write spaces
around the %o as well or the results are undefined.
%o is for use in the specs for running the linker.
Input files whose names have no recognized suffix are not compiled
at all, but they are included among the output files, so they will
be linked.
%p substitutes the standard macro predefinitions for the
current target machine. Use this when running cpp.
%P like %p, but puts `__' before and after the name of each macro.
This is for ANSI C.
%s current argument is the name of a library or startup file of some sort.
Search for that file in a standard list of directories
and substitute the full pathname found.
%eSTR Print STR as an error message. STR is terminated by a newline.
Use this when inconsistent options are detected.
%a process ASM_SPEC as a spec.
This allows config.h to specify part of the spec for running as.
%l process LINK_SPEC as a spec.
%L process LIB_SPEC as a spec.
%S process STARTFILE_SPEC as a spec. A capital S is actually used here.
%c process SIGNED_CHAR_SPEC as a spec.
%C process CPP_SPEC as a spec. A capital C is actually used here.
%1 process CC1_SPEC as a spec.
%{S} substitutes the -S switch, if that switch was given to CC.
If that switch was not specified, this substitutes nothing.
Here S is a metasyntactic variable.
%{S*} substitutes all the switches specified to CC whose names start
with -S. This is used for -o, -D, -I, etc; switches that take
arguments. CC considers `-o foo' as being one switch whose
name starts with `o'. %{o*} would substitute this text,
including the space; thus, two arguments would be generated.
%{S:X} substitutes X, but only if the -S switch was given to CC.
%{!S:X} substitutes X, but only if the -S switch was NOT given to CC.
%{|S:X} like %{S:X}, but if no S switch, substitute `-'.
%{|!S:X} like %{!S:X}, but if there is an S switch, substitute `-'.
The conditional text X in a %{S:X} or %{!S:X} construct may contain
other nested % constructs or spaces, or even newlines.
They are processed as usual, as described above.
The character | is used to indicate that a command should be piped to
the following command, but only if -pipe is specified.
Note that it is built into CC which switches take arguments and which
do not. You might think it would be useful to generalize this to
allow each compiler's spec to say which switches take arguments. But
this cannot be done in a consistent fashion. CC cannot even decide
which input files have been specified without knowing which switches
take arguments, and it must know which input files to compile in order
to tell which compilers to run.
CC also knows implicitly that arguments starting in `-l' are to
be treated as compiler output files, and passed to the linker in their proper
position among the other output files.
*/
#include <stdio.h>
#include <sys/types.h>
#include <signal.h>
#include <sys/file.h>
#include "config.h"
#include "obstack.h"
#include "gvarargs.h"
#ifdef USG
#define R_OK 4
#define W_OK 2
#define X_OK 1
#define vfork fork
#endif /* USG */
#define obstack_chunk_alloc xmalloc
#define obstack_chunk_free free
extern int xmalloc ();
extern void free ();
/* If a stage of compilation returns an exit status >= 1,
compilation of that file ceases. */
#define MIN_FATAL_STATUS 1
/* This is the obstack which we use to allocate many strings. */
struct obstack obstack;
char *handle_braces ();
char *save_string ();
char *concat ();
int do_spec ();
int do_spec_1 ();
char *find_file ();
static char *find_exec_file ();
void validate_switches ();
void validate_all_switches ();
void fancy_abort ();
/* config.h can define ASM_SPEC to provide extra args to the assembler
or extra switch-translations. */
#ifndef ASM_SPEC
#define ASM_SPEC ""
#endif
/* config.h can define CPP_SPEC to provide extra args to the C preprocessor
or extra switch-translations. */
#ifndef CPP_SPEC
#define CPP_SPEC ""
#endif
/* config.h can define CC1_SPEC to provide extra args to cc1
or extra switch-translations. */
#ifndef CC1_SPEC
#define CC1_SPEC ""
#endif
/* config.h can define LINK_SPEC to provide extra args to the linker
or extra switch-translations. */
#ifndef LINK_SPEC
#define LINK_SPEC ""
#endif
/* config.h can define LIB_SPEC to override the default libraries. */
#ifndef LIB_SPEC
#define LIB_SPEC "%{!p:%{!pg:-lc}}%{p:-lc_p}%{pg:-lc_p}"
#endif
/* config.h can define STARTFILE_SPEC to override the default crt0 files. */
#ifndef STARTFILE_SPEC
#define STARTFILE_SPEC \
"%{pg:gcrt0.o%s}%{!pg:%{p:mcrt0.o%s}%{!p:crt0.o%s}}"
#endif
/* This spec is used for telling cpp whether char is signed or not. */
#define SIGNED_CHAR_SPEC \
(DEFAULT_SIGNED_CHAR ? "%{funsigned-char:-D__CHAR_UNSIGNED__}" \
: "%{!fsigned-char:-D__CHAR_UNSIGNED__}")
/* This defines which switch letters take arguments. */
#ifndef SWITCH_TAKES_ARG
#define SWITCH_TAKES_ARG(CHAR) \
((CHAR) == 'D' || (CHAR) == 'U' || (CHAR) == 'o' \
|| (CHAR) == 'e' || (CHAR) == 'T' || (CHAR) == 'u' \
|| (CHAR) == 'I' || (CHAR) == 'Y' || (CHAR) == 'm' \
|| (CHAR) == 'L' || (CHAR) == 'i' || (CHAR) == 'A')
#endif
/* This defines which multi-letter switches take arguments. */
#ifndef WORD_SWITCH_TAKES_ARG
#define WORD_SWITCH_TAKES_ARG(STR) (!strcmp (STR, "Tdata"))
#endif
/* This structure says how to run one compiler, and when to do so. */
struct compiler
{
char *suffix; /* Use this compiler for input files
whose names end in this suffix. */
char *spec; /* To use this compiler, pass this spec
to do_spec. */
};
/* Here are the specs for compiling files with various known suffixes.
A file that does not end in any of these suffixes will be passed
unchanged to the loader and nothing else will be done to it. */
struct compiler compilers[] =
{
{".c",
"cpp %{nostdinc} %{C} %{v} %{g} %{D*} %{U*} %{I*} %{M*} %{i*} %{trigraphs} -undef \
-D__GNUC__ %{ansi:-trigraphs -$ -D__STRICT_ANSI__} %{!ansi:%p} %P\
%c %{!NOOP:%{O:-D__OPTIMIZE__}} %{traditional} %{pedantic}\
%{Wcomment*} %{Wtrigraphs} %{Wall} %C\
%i %{!M*:%{!E:%{!pipe:%g.cpp}}}%{E:%{o*}}%{M*:%{o*}} |\n\
%{!M*:%{!E:cc1 %{!pipe:%g.cpp} %1 \
%{!Q:-quiet} -dumpbase %i %{Y*} %{d*} %{m*} %{f*} %{a}\
%{g} %{!NOOP:%{O}} %{W*} %{w} %{pedantic} %{ansi} %{traditional}\
%{v:-version} %{gg:-symout %g.sym} %{pg:-p} %{p}\
%{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
%{S:%{o*}%{!o*:-o %b.s}}%{!S:-o %{|!pipe:%g.s}} |\n\
%{!S:as %{coff} %{j} %{J} %{h} %{d2} %a %{gg:-G %g.sym}\
%{c:%{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%b.o}\
%{!pipe:%g.s}\n }}}"},
{".cc",
"cpp -+ %{nostdinc} %{C} %{v} %{D*} %{U*} %{I*} %{M*} %{i*} \
-undef -D__GNUC__ %p %P\
%c %{!NOOP:%{O:-D__OPTIMIZE__}} %{traditional} %{pedantic}\
%{Wcomment*} %{Wtrigraphs} %{Wall} %C\
%i %{!M*:%{!E:%{!pipe:%g.cpp}}}%{E:%{o*}}%{M*:%{o*}} |\n\
%{!M*:%{!E:cc1plus %{!pipe:%g.cpp} %1\
%{!Q:-quiet} -dumpbase %i %{Y*} %{d*} %{m*} %{f*} %{a}\
%{g} %{!NOOP:%{O}} %{W*} %{w} %{pedantic} %{traditional}\
%{v:-version} %{gg:-symout %g.sym} %{pg:-p} %{p}\
%{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
%{S:%{o*}%{!o*:-o %b.s}}%{!S:-o %{|!pipe:%g.s}} |\n\
%{!S:as %{coff} %{j} %{J} %{h} %{d2} %a %{gg:-G %g.sym}\
%{c:%{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%b.o}\
%{!pipe:%g.s}\n }}}"},
{".i",
"cc1 %i %1 %{!Q:-quiet} %{Y*} %{d*} %{m*} %{f*} %{a}\
%{g} %{!NOOP:%{O}} %{W*} %{w} %{pedantic} %{ansi} %{traditional}\
%{v:-version} %{gg:-symout %g.sym} %{pg:-p} %{p}\
%{S:%{o*}%{!o*:-o %b.s}}%{!S:-o %{|!pipe:%g.s}} |\n\
%{!S:as %{coff} %{j} %{J} %{h} %{d2} %a %{gg:-G %g.sym}\
%{c:%{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%b.o} %{!pipe:%g.s}\n }"},
{".s",
"%{!S:as %{coff} %{j} %{J} %{h} %{d2} %a \
%{c:%{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%b.o} %i\n }"},
{".S",
"cpp %{nostdinc} %{C} %{v} %{D*} %{U*} %{I*} %{M*} %{trigraphs} \
-undef -D__GNUC__ -$ %p %P\
%c %{!NOOP:%{O:-D__OPTIMIZE__}} %{traditional} %{pedantic}\
%{Wcomment*} %{Wtrigraphs} %{Wall} %C\
%i %{!M*:%{!E:%{!pipe:%g.cpp}}}%{E:%{o*}}%{M*:%{o*}} |\n\
%{!M*:%{!E:%{!S:as %{j} %{J} %{h} %{d2} %a \
%{c:%{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%b.o}\
%{!pipe:%g.s}\n }}}"},
/* Mark end of table */
{0, 0}
};
/* Here is the spec for running the linker, after compiling all files. */
char *link_spec = "%{!c:%{!M*:%{!E:%{!coff:%{!S:ld %{o*} %l\
%{A} %{d} %{e*} %{N} %{n} %{r} %{s} %{S} %{T*} %{t} %{u*} %{X} %{x} %{z}\
%{y*} %{!nostdlib:%S} \
%{L*} %o %{!nostdlib: %{g:-lg} %L}\n }} \
%{coff:%{!S:/bin/ld /lib/crt0.o %{o*} %l\
%{A} %{d} %{e*} %{N} %{n} %{r} %{s} %{S} %{T*} %{t} %{u*} %{X} %{x} %{z}\
%{y*} \
%{L*} %o %L \n }} \
}}}";
/* Accumulate a command (program name and args), and run it. */
/* Vector of pointers to arguments in the current line of specifications. */
char **argbuf;
/* Number of elements allocated in argbuf. */
int argbuf_length;
/* Number of elements in argbuf currently in use (containing args). */
int argbuf_index;
/* Number of commands executed so far. */
int execution_count;
/* Flag indicating whether we should print the command and arguments */
unsigned char vflag;
/* Name with which this program was invoked. */
char *programname;
/* User-specified -B prefix to attach to command names,
or 0 if none specified. */
char *user_exec_prefix = 0;
/* Environment-specified prefix to attach to command names,
or 0 if none specified. */
char *env_exec_prefix = 0;
/* Suffix to attach to directories searched for commands. */
char *machine_suffix = 0;
/* Default prefixes to attach to command names. */
#ifndef STANDARD_EXEC_PREFIX
#define STANDARD_EXEC_PREFIX "/usr/local/lib/gcc-"
#endif /* !defined STANDARD_EXEC_PREFIX */
char *standard_exec_prefix = STANDARD_EXEC_PREFIX;
char *standard_exec_prefix_1 = "/usr/lib/gcc-";
#ifndef STANDARD_STARTFILE_PREFIX
#define STANDARD_STARTFILE_PREFIX "/usr/local/lib/"
#endif /* !defined STANDARD_STARTFILE_PREFIX */
char *standard_startfile_prefix = STANDARD_STARTFILE_PREFIX;
char *standard_startfile_prefix_1 = "/lib/";
char *standard_startfile_prefix_2 = "/usr/lib/";
/* Clear out the vector of arguments (after a command is executed). */
void
clear_args ()
{
argbuf_index = 0;
}
/* Add one argument to the vector at the end.
This is done when a space is seen or at the end of the line.
If DELETE_ALWAYS is nonzero, the arg is a filename
and the file should be deleted eventually.
If DELETE_FAILURE is nonzero, the arg is a filename
and the file should be deleted if this compilation fails. */
void
store_arg (arg, delete_always, delete_failure)
char *arg;
int delete_always, delete_failure;
{
if (argbuf_index + 1 == argbuf_length)
{
argbuf = (char **) realloc (argbuf, (argbuf_length *= 2) * sizeof (char *));
}
argbuf[argbuf_index++] = arg;
argbuf[argbuf_index] = 0;
if (delete_always || delete_failure)
record_temp_file (arg, delete_always, delete_failure);
}
/* Record the names of temporary files we tell compilers to write,
and delete them at the end of the run. */
/* This is the common prefix we use to make temp file names.
It is chosen once for each run of this program.
It is substituted into a spec by %g.
Thus, all temp file names contain this prefix.
In practice, all temp file names start with this prefix.
This prefix comes from the envvar TMPDIR if it is defined;
otherwise, from the P_tmpdir macro if that is defined;
otherwise, in /usr/tmp or /tmp. */
char *temp_filename;
/* Length of the prefix. */
int temp_filename_length;
/* Define the list of temporary files to delete. */
struct temp_file
{
char *name;
struct temp_file *next;
};
/* Queue of files to delete on success or failure of compilation. */
struct temp_file *always_delete_queue;
/* Queue of files to delete on failure of compilation. */
struct temp_file *failure_delete_queue;
/* Record FILENAME as a file to be deleted automatically.
ALWAYS_DELETE nonzero means delete it if all compilation succeeds;
otherwise delete it in any case.
FAIL_DELETE nonzero means delete it if a compilation step fails;
otherwise delete it in any case. */
void
record_temp_file (filename, always_delete, fail_delete)
char *filename;
int always_delete;
int fail_delete;
{
register char *name;
name = (char *) xmalloc (strlen (filename) + 1);
strcpy (name, filename);
if (always_delete)
{
register struct temp_file *temp;
for (temp = always_delete_queue; temp; temp = temp->next)
if (! strcmp (name, temp->name))
goto already1;
temp = (struct temp_file *) xmalloc (sizeof (struct temp_file));
temp->next = always_delete_queue;
temp->name = name;
always_delete_queue = temp;
already1:;
}
if (fail_delete)
{
register struct temp_file *temp;
for (temp = failure_delete_queue; temp; temp = temp->next)
if (! strcmp (name, temp->name))
goto already2;
temp = (struct temp_file *) xmalloc (sizeof (struct temp_file));
temp->next = failure_delete_queue;
temp->name = name;
failure_delete_queue = temp;
already2:;
}
}
/* Delete all the temporary files whose names we previously recorded. */
void
delete_temp_files ()
{
register struct temp_file *temp;
for (temp = always_delete_queue; temp; temp = temp->next)
{
#ifdef DEBUG
int i;
printf ("Delete %s? (y or n) ", temp->name);
fflush (stdout);
i = getchar ();
if (i != '\n')
while (getchar () != '\n') ;
if (i == 'y' || i == 'Y')
#endif /* DEBUG */
{
if (unlink (temp->name) < 0)
if (vflag)
perror_with_name (temp->name);
}
}
always_delete_queue = 0;
}
/* Delete all the files to be deleted on error. */
void
delete_failure_queue ()
{
register struct temp_file *temp;
for (temp = failure_delete_queue; temp; temp = temp->next)
{
#ifdef DEBUG
int i;
printf ("Delete %s? (y or n) ", temp->name);
fflush (stdout);
i = getchar ();
if (i != '\n')
while (getchar () != '\n') ;
if (i == 'y' || i == 'Y')
#endif /* DEBUG */
{
if (unlink (temp->name) < 0)
if (vflag)
perror_with_name (temp->name);
}
}
}
void
clear_failure_queue ()
{
failure_delete_queue = 0;
}
/* Compute a string to use as the base of all temporary file names.
It is substituted for %g. */
void
choose_temp_base ()
{
extern char *getenv ();
char *base = getenv ("TMPDIR");
int len;
if (base == (char *)0)
{
#ifdef P_tmpdir
if (access (P_tmpdir, R_OK | W_OK) == 0)
base = P_tmpdir;
#endif
if (base == (char *)0)
{
if (access ("/usr/tmp", R_OK | W_OK) == 0)
base = "/usr/tmp/";
else
base = "/tmp/";
}
}
len = strlen (base);
temp_filename = (char *) xmalloc (len + sizeof("/ccXXXXXX"));
strcpy (temp_filename, base);
if (len > 0 && temp_filename[len-1] != '/')
temp_filename[len++] = '/';
strcpy (temp_filename + len, "ccXXXXXX");
mktemp (temp_filename);
temp_filename_length = strlen (temp_filename);
}
/* Search for an execute file through our search path.
Return 0 if not found, otherwise return its name, allocated with malloc. */
static char *
find_exec_file (prog)
char *prog;
{
int win = 0;
char *temp;
int size;
size = strlen (standard_exec_prefix);
if (user_exec_prefix != 0 && strlen (user_exec_prefix) > size)
size = strlen (user_exec_prefix);
if (env_exec_prefix != 0 && strlen (env_exec_prefix) > size)
size = strlen (env_exec_prefix);
if (strlen (standard_exec_prefix_1) > size)
size = strlen (standard_exec_prefix_1);
size += strlen (prog) + 1;
if (machine_suffix)
size += strlen (machine_suffix) + 1;
temp = (char *) xmalloc (size);
/* Determine the filename to execute. */
if (user_exec_prefix)
{
if (machine_suffix)
{
strcpy (temp, user_exec_prefix);
strcat (temp, machine_suffix);
strcat (temp, prog);
win = (access (temp, X_OK) == 0);
}
if (!win)
{
strcpy (temp, user_exec_prefix);
strcat (temp, prog);
win = (access (temp, X_OK) == 0);
}
}
if (!win && env_exec_prefix)
{
if (machine_suffix)
{
strcpy (temp, env_exec_prefix);
strcat (temp, machine_suffix);
strcat (temp, prog);
win = (access (temp, X_OK) == 0);
}
if (!win)
{
strcpy (temp, env_exec_prefix);
strcat (temp, prog);
win = (access (temp, X_OK) == 0);
}
}
if (!win)
{
if (machine_suffix)
{
strcpy (temp, standard_exec_prefix);
strcat (temp, machine_suffix);
strcat (temp, prog);
win = (access (temp, X_OK) == 0);
}
if (!win)
{
strcpy (temp, standard_exec_prefix);
strcat (temp, prog);
win = (access (temp, X_OK) == 0);
}
}
if (!win)
{
if (machine_suffix)
{
strcpy (temp, standard_exec_prefix_1);
strcat (temp, machine_suffix);
strcat (temp, prog);
win = (access (temp, X_OK) == 0);
}
if (!win)
{
strcpy (temp, standard_exec_prefix_1);
strcat (temp, prog);
win = (access (temp, X_OK) == 0);
}
}
if (win)
return temp;
else
return 0;
}
/* stdin file number. */
#define STDIN_FILE_NO 0
/* stdout file number. */
#define STDOUT_FILE_NO 1
/* value of `pipe': port index for reading. */
#define READ_PORT 0
/* value of `pipe': port index for writing. */
#define WRITE_PORT 1
/* Pipe waiting from last process, to be used as input for the next one.
Value is STDIN_FILE_NO if no pipe is waiting
(i.e. the next command is the first of a group). */
int last_pipe_input;
/* Fork one piped subcommand. FUNC is the system call to use
(either execv or execvp). ARGV is the arg vector to use.
NOT_LAST is nonzero if this is not the last subcommand
(i.e. its output should be piped to the next one.) */
static int
pexecute (func, program, argv, not_last)
char *program;
int (*func)();
char *argv[];
int not_last;
{
int pid;
int pdes[2];
int input_desc = last_pipe_input;
int output_desc = STDOUT_FILE_NO;
/* If this isn't the last process, make a pipe for its output,
and record it as waiting to be the input to the next process. */
if (not_last)
{
if (pipe (pdes) < 0)
pfatal_with_name ("pipe");
output_desc = pdes[WRITE_PORT];
last_pipe_input = pdes[READ_PORT];
}
else
last_pipe_input = STDIN_FILE_NO;
pid = vfork ();
switch (pid)
{
case -1:
pfatal_with_name ("vfork");
break;
case 0: /* child */
/* Move the input and output pipes into place, if nec. */
if (input_desc != STDIN_FILE_NO)
{
close (STDIN_FILE_NO);
dup (input_desc);
close (input_desc);
}
if (output_desc != STDOUT_FILE_NO)
{
close (STDOUT_FILE_NO);
dup (output_desc);
close (output_desc);
}
/* Close the parent's descs that aren't wanted here. */
if (last_pipe_input != STDIN_FILE_NO)
close (last_pipe_input);
/* Exec the program. */
(*func) (program, argv);
perror_exec (program);
exit (-1);
/* NOTREACHED */
default:
/* In the parent, after forking.
Close the descriptors that we made for this child. */
if (input_desc != STDIN_FILE_NO)
close (input_desc);
if (output_desc != STDOUT_FILE_NO)
close (output_desc);
/* Return child's process number. */
return pid;
}
}
/* Execute the command specified by the arguments on the current line of spec.
When using pipes, this includes several piped-together commands
with `|' between them.
Return 0 if successful, -1 if failed. */
int
execute ()
{
int i, j;
int n_commands; /* # of command. */
char *string;
struct command
{
char *prog; /* program name. */
char **argv; /* vector of args. */
int pid; /* pid of process for this command. */
};
struct command *commands; /* each command buffer with above info. */
/* Count # of piped commands. */
for (n_commands = 1, i = 0; i < argbuf_index; i++)
if (strcmp (argbuf[i], "|") == 0)
n_commands++;
/* Get storage for each command. */
commands
= (struct command *) alloca (n_commands * sizeof (struct command));
/* Split argbuf into its separate piped processes,
and record info about each one.
Also search for the programs that are to be run. */
commands[0].prog = argbuf[0]; /* first command. */
commands[0].argv = &argbuf[0];
string = find_exec_file (commands[0].prog);
if (string)
commands[0].argv[0] = string;
for (n_commands = 1, i = 0; i < argbuf_index; i++)
if (strcmp (argbuf[i], "|") == 0)
{ /* each command. */
argbuf[i] = 0; /* termination of command args. */
commands[n_commands].prog = argbuf[i + 1];
commands[n_commands].argv = &argbuf[i + 1];
string = find_exec_file (commands[n_commands].prog);
if (string)
commands[n_commands].argv[0] = string;
n_commands++;
}
argbuf[argbuf_index] = 0;
/* If -v, print what we are about to do, and maybe query. */
if (vflag)
{
/* Print each piped command as a separate line. */
for (i = 0; i < n_commands ; i++)
{
char **j;
for (j = commands[i].argv; *j; j++)
fprintf (stderr, " %s", *j);
/* Print a pipe symbol after all but the last command. */
if (i + 1 != n_commands)
fprintf (stderr, " |");
fprintf (stderr, "\n");
}
fflush (stderr);
#ifdef DEBUG
fprintf (stderr, "\nGo ahead? (y or n) ");
fflush (stderr);
j = getchar ();
if (j != '\n')
while (getchar () != '\n') ;
if (j != 'y' && j != 'Y')
return 0;
#endif /* DEBUG */
}
/* Run each piped subprocess. */
last_pipe_input = STDIN_FILE_NO;
for (i = 0; i < n_commands; i++)
{
extern int execv(), execvp();
char *string = commands[i].argv[0];
commands[i].pid = pexecute ((string != commands[i].prog ? execv : execvp),
string, commands[i].argv,
i + 1 < n_commands);
if (string != commands[i].prog)
free (string);
}
execution_count++;
/* Wait for all the subprocesses to finish.
We don't care what order they finish in;
we know that N_COMMANDS waits will get them all. */
{
int ret_code = 0;
for (i = 0; i < n_commands; i++)
{
int status;
int pid;
char *prog;
pid = wait (&status);
if (pid < 0)
abort ();
if (status != 0)
{
int j;
for (j = 0; j < n_commands; j++)
if (commands[j].pid == pid)
prog = commands[j].prog;
if ((status & 0x7F) != 0)
fatal ("Program %s got fatal signal %d.", prog, (status & 0x7F));
if (((status & 0xFF00) >> 8) >= MIN_FATAL_STATUS)
ret_code = -1;
}
}
return ret_code;
}
}
/* Find all the switches given to us
and make a vector describing them.
The elements of the vector a strings, one per switch given.
If a switch uses the following argument, then the `part1' field
is the switch itself and the `part2' field is the following argument.
The `valid' field is nonzero if any spec has looked at this switch;
if it remains zero at the end of the run, it must be meaningless. */
struct switchstr
{
char *part1;
char *part2;
int valid;
};
struct switchstr *switches;
int n_switches;
/* Also a vector of input files specified. */
char **infiles;
int n_infiles;
/* And a vector of corresponding output files is made up later. */
char **outfiles;
/* Create the vector `switches' and its contents.
Store its length in `n_switches'. */
void
process_command (argc, argv)
int argc;
char **argv;
{
extern char *getenv ();
register int i;
n_switches = 0;
n_infiles = 0;
env_exec_prefix = getenv ("GCC_EXEC_PREFIX");
/* Scan argv twice. Here, the first time, just count how many switches
there will be in their vector, and how many input files in theirs.
Here we also parse the switches that cc itself uses (e.g. -v). */
for (i = 1; i < argc; i++)
{
if (argv[i][0] == '-' && argv[i][1] != 'l')
{
register char *p = &argv[i][1];
register int c = *p;
switch (c)
{
case 'b':
machine_suffix = p + 1;
break;
case 'B':
user_exec_prefix = p + 1;
break;
case 'v': /* Print our subcommands and print versions. */
vflag++;
n_switches++;
break;
default:
n_switches++;
if (SWITCH_TAKES_ARG (c) && p[1] == 0)
i++;
else if (WORD_SWITCH_TAKES_ARG (p))
i++;
}
}
else
n_infiles++;
}
/* Then create the space for the vectors and scan again. */
switches = ((struct switchstr *)
xmalloc ((n_switches + 1) * sizeof (struct switchstr)));
infiles = (char **) xmalloc ((n_infiles + 1) * sizeof (char *));
n_switches = 0;
n_infiles = 0;
/* This, time, copy the text of each switch and store a pointer
to the copy in the vector of switches.
Store all the infiles in their vector. */
for (i = 1; i < argc; i++)
{
if (argv[i][0] == '-' && argv[i][1] != 'l')
{
register char *p = &argv[i][1];
register int c = *p;
if (c == 'B' || c == 'b')
continue;
switches[n_switches].part1 = p;
if ((SWITCH_TAKES_ARG (c) && p[1] == 0)
|| WORD_SWITCH_TAKES_ARG (p))
switches[n_switches].part2 = argv[++i];
else
switches[n_switches].part2 = 0;
switches[n_switches].valid = 0;
n_switches++;
}
else
infiles[n_infiles++] = argv[i];
}