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
|
/* $OpenBSD: cc.c,v 1.9 2007/11/01 10:49:22 stefan Exp $ */
/*
* Copyright(C) Caldera International Inc. 2001-2002. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* Redistributions of source code and documentation must retain the above
* copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditionsand the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed or owned by Caldera
* International, Inc.
* Neither the name of Caldera International, Inc. nor the names of other
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA
* INTERNATIONAL, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE LIABLE
* FOR ANY DIRECT, INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OFLIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*
* Front-end to the C compiler.
*
* Brief description of its syntax:
* - Files that end with .c are passed via cpp->ccom->as->ld
* - Files that end with .i are passed via ccom->as->ld
* - Files that end with .s are passed as->ld
* - Files that end with .o are passed directly to ld
* - Multiple files may be given on the command line.
* - Unrecognized options are all sent directly to ld.
* -c or -S cannot be combined with -o if multiple files are given.
*
* This file should be rewritten readable.
*/
#include <sys/types.h>
#include <sys/wait.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <libgen.h>
#include <signal.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "../config.h"
#include "ccconfig.h"
/* C command */
#define MKS(x) _MKS(x)
#define _MKS(x) #x
/*
* Many specific definitions, should be declared elsewhere.
*/
#define STDINC "/usr/include"
#define MAXFIL 10000
#define MAXLIB 10000
#define MAXAV 10000
#define MAXOPT 100
char *tmp3;
char *tmp4;
char *outfile;
char *copy(char *),*setsuf(char *, char);
int getsuf(char *);
int main(int, char *[]);
void error(char *, ...);
void errorx(int, char *, ...);
int nodup(char **, char *);
int callsys(char [], char *[]);
int cunlink(char *);
void dexit(int);
void idexit(int);
char *gettmp();
char *av[MAXAV];
char *clist[MAXFIL];
char *llist[MAXLIB];
char alist[20];
char *xlist[100];
int xnum;
int Cflag;
int dflag;
int pflag;
int sflag;
int cflag;
int eflag;
int gflag;
int vflag;
int tflag;
int Eflag;
int Oflag;
int kflag; /* generate PIC/pic code */
#define F_PIC 1
#define F_pic 2
int Mflag; /* dependencies only */
int pgflag;
int exfail;
int Xflag;
int nostartfiles, Bstatic;
int nostdinc, nostdlib;
int onlyas;
int pthreads;
char *pass0;
char *passp = LIBEXECDIR "/cpp";
char *Bflag;
char *cppadd[] = CPPADD;
char *dynlinker[] = DYNLINKER;
char *crt0file = CRT0FILE;
char *startfiles[] = STARTFILES;
char *endfiles[] = ENDFILES;
char *mach = DEFMACH;
struct cppmd {
char *mach;
char *cppmdadd[MAXCPPMDARGS];
};
struct cppmd cppmds[] = CPPMDADDS;
#ifdef LIBCLIBS
char *libclibs[] = LIBCLIBS;
#else
char *libclibs[] = { "-lc", NULL };
#endif
#ifndef STARTLABEL
#define STARTLABEL "__start"
#endif
int
main(int argc, char *argv[])
{
char *t, *u;
char *assource;
char **pv, *ptemp[MAXOPT], **pvt;
int nc, nl, i, j, k, c, nxo, na;
i = nc = nl = nxo = 0;
pv = ptemp;
while(++i < argc) {
if (argv[i][0] == '-') {
switch (argv[i][1]) {
default:
goto passa;
case 'B': /* other search paths for binaries */
Bflag = &argv[i][2];
break;
case 'b':
t = &argv[i][2];
if (*t == '\0' && i + 1 < argc) {
t = argv[i+1];
i++;
}
if (strncmp(t, "?", 1) == 0) {
/* show machine targets */
printf("Available machine targets:");
for (j=0; cppmds[j].mach; j++)
printf(" %s",cppmds[j].mach);
printf("\n");
exit(0);
}
for (j=0; cppmds[j].mach; j++)
if (strcmp(t, cppmds[j].mach) == 0) {
mach = cppmds[j].mach;
break;
}
if (cppmds[j].mach == NULL)
errorx(1, "unknown target arch %s", t);
break;
case 'X':
Xflag++;
break;
case 'W': /* Ignore (most of) W-flags */
if (strncmp(argv[i], "-Wl,", 4) == 0) {
/* options to the linker */
t = &argv[i][4];
while ((u = strchr(t, ','))) {
*u++ = 0;
llist[nl++] = t;
t = u;
}
llist[nl++] = t;
}
break;
case 'f': /* GCC compatibility flags */
if (strcmp(argv[i], "-fPIC") == 0)
kflag = F_PIC;
if (strcmp(argv[i], "-fpic") == 0)
kflag = F_pic;
/* silently ignore the rest */
break;
case 'g': /* create debug output */
gflag++;
break;
case 'i':
if (strcmp(argv[i], "-isystem") == 0) {
*pv++ = "-S";
*pv++ = argv[++i];
} else if (strcmp(argv[i], "-include") == 0) {
*pv++ = "-i";
*pv++ = argv[++i];
} else
goto passa;
break;
case 'k': /* generate PIC code */
kflag = F_pic;
break;
case 'n': /* handle -n flags */
if (strcmp(argv[i], "-nostdinc") == 0)
nostdinc++;
else if (strcmp(argv[i], "-nostdlib") == 0) {
nostdlib++;
nostartfiles++;
} else if (strcmp(argv[i], "-nostartfiles") == 0)
nostartfiles = 1;
else
goto passa;
break;
case 'p':
if (strcmp(argv[i], "-pg") == 0)
pgflag++;
else if (strcmp(argv[i], "-pthread") == 0)
pthreads++;
else if (strcmp(argv[i], "-pipe") == 0)
/* NOTHING YET */;
else
errorx(1, "unknown option %s", argv[i]);
break;
case 'x':
xlist[xnum++] = argv[i];
break;
case 't':
tflag++;
break;
case 'S':
sflag++;
cflag++;
break;
case 'o':
if (outfile)
errorx(8, "too many -o");
outfile = argv[++i];
break;
case 'O':
Oflag++;
break;
case 'E':
Eflag++;
break;
case 'P':
pflag++;
*pv++ = argv[i];
case 'c':
cflag++;
break;
#if 0
case '2':
if(argv[i][2] == '\0')
pref = "/lib/crt2.o";
else {
pref = "/lib/crt20.o";
}
break;
#endif
case 'C':
Cflag = 1;
break;
case 'D':
case 'I':
case 'U':
*pv++ = argv[i];
if (argv[i][2] == 0)
*pv++ = argv[++i];
if (pv >= ptemp+MAXOPT)
{
error("Too many DIU options");
--pv;
}
break;
case 'M':
Mflag++;
break;
case 'd':
dflag++;
strlcpy(alist, argv[i], sizeof (alist));
break;
case 'v':
printf("%s\n", VERSSTR);
vflag++;
break;
case 's':
if (strcmp(argv[i], "-static") == 0)
Bstatic = 1;
else
goto passa;
break;
}
} else {
passa:
t = argv[i];
if (*argv[i] == '-' && argv[i][1] == 'L')
;
else if((c=getsuf(t))=='c' || c=='S' || c=='i' ||
c=='s'|| Eflag) {
clist[nc++] = t;
if (nc>=MAXFIL)
{
error("Too many source files");
exit(1);
}
t = setsuf(t, 'o');
}
if (nodup(llist, t)) {
llist[nl++] = t;
if (nl >= MAXLIB)
{
error("Too many object/library files");
exit(1);
}
if (getsuf(t)=='o')
nxo++;
}
}
}
/* Sanity checking */
if (nc == 0 && nl == 0)
errorx(8, "no input files");
if (outfile && (cflag || sflag || Eflag) && nc > 1)
errorx(8, "-o given with -c || -E || -S and more than one file");
if (outfile && clist[0] && strcmp(outfile, clist[0]) == 0)
errorx(8, "output file will be clobbered");
#if 0
for(i=0, j=0; i<nc; i++) {
if((c=getsuf(clist[i]))=='c' || c=='S') {
j++;
break;
}
}
if (j==0 && Eflag)
errorx(8, "no file to be preprocessed");
#endif
if (gflag) Oflag = 0;
#if 0
if (proflag)
pref = "/lib/mcrt0.o";
#endif
if(nc==0)
goto nocom;
if (pflag==0) {
if (!sflag)
tmp3 = gettmp();
tmp4 = gettmp();
}
if (signal(SIGINT, SIG_IGN) != SIG_IGN) /* interrupt */
signal(SIGINT, idexit);
if (signal(SIGTERM, SIG_IGN) != SIG_IGN) /* terminate */
signal(SIGTERM, idexit);
asprintf(&pass0, "%s%s%s", LIBEXECDIR, "/ccom_", mach);
pvt = pv;
for (i=0; i<nc; i++) {
/*
* C preprocessor
*/
if (nc>1 && !Eflag)
printf("%s:\n", clist[i]);
onlyas = 0;
assource = tmp3;
if (getsuf(clist[i])=='i') {
if(Eflag)
continue;
goto com;
} else if (getsuf(clist[i])=='s') {
assource = clist[i];
goto assemble;
} else if (getsuf(clist[i])=='S')
onlyas = 1;
if (pflag)
tmp4 = setsuf(clist[i], 'i');
na = 0;
av[na++] = "cpp";
if (vflag)
av[na++] = "-v";
av[na++] = "-D__PCC__=" MKS(PCC_MAJOR);
av[na++] = "-D__PCC_MINOR__=" MKS(PCC_MINOR);
av[na++] = "-D__PCC_MINORMINOR__=" MKS(PCC_MINORMINOR);
if (getsuf(clist[i])=='S')
av[na++] = "-D__ASSEMBLER__";
if (pthreads)
av[na++] = "-D_PTHREADS";
if (Cflag)
av[na++] = "-C";
if (Mflag)
av[na++] = "-M";
if (dflag)
av[na++] = alist;
for (j = 0; cppadd[j]; j++)
av[na++] = cppadd[j];
for (k = 0; cppmds[k].mach; k++) {
if (strcmp(cppmds[k].mach, mach) != 0)
continue;
for (j = 0; cppmds[k].cppmdadd[j]; j++)
av[na++] = cppmds[k].cppmdadd[j];
break;
}
if (tflag)
av[na++] = "-t";
for(pv=ptemp; pv <pvt; pv++)
av[na++] = *pv;
if (!nostdinc)
av[na++] = "-S", av[na++] = STDINC;
av[na++] = clist[i];
if (!Eflag && !Mflag)
av[na++] = tmp4;
if (Eflag && outfile)
av[na++] = outfile;
av[na++]=0;
if (callsys(passp, av))
{exfail++; eflag++;}
if (Eflag || Mflag)
continue;
if (onlyas) {
assource = tmp4;
goto assemble;
}
/*
* C compiler
*/
com:
na = 0;
av[na++]= "ccom";
if (vflag)
av[na++] = "-v";
if (gflag)
av[na++] = "-g";
if (kflag)
av[na++] = "-k";
if (Oflag) {
av[na++] = "-xtemps";
av[na++] = "-xdeljumps";
}
for (j = 0; j < xnum; j++)
av[na++] = xlist[j];
if (getsuf(clist[i])=='i')
av[na++] = clist[i];
else
av[na++] = tmp4; /* created by cpp */
if (pflag || exfail)
{
cflag++;
continue;
}
if(sflag) {
if (outfile)
assource = tmp3 = outfile;
else
assource = tmp3 = setsuf(clist[i], 's');
}
av[na++] = tmp3;
#if 0
if (proflag) {
av[3] = "-XP";
av[4] = 0;
} else
av[3] = 0;
#endif
av[na++] = NULL;
if (callsys(pass0, av)) {
cflag++;
eflag++;
continue;
}
if (sflag)
continue;
/*
* Assembler
*/
assemble:
na = 0;
av[na++] = "as";
if (vflag)
av[na++] = "-v";
if (kflag)
av[na++] = "-k";
av[na++] = "-o";
if (outfile && cflag)
av[na++] = outfile;
else
av[na++] = setsuf(clist[i], 'o');
av[na++] = assource;
if (dflag)
av[na++] = alist;
av[na++] = 0;
if (callsys("/bin/as", av)) {
cflag++;
eflag++;
cunlink(tmp4);
continue;
}
cunlink(tmp4);
}
if (Eflag || Mflag)
dexit(eflag);
/*
* Linker
*/
nocom:
if (cflag==0 && nl!=0) {
j = 0;
av[j++] = "ld";
if (vflag)
av[j++] = "-v";
av[j++] = "-X";
av[j++] = "-d";
av[j++] = "-e";
av[j++] = STARTLABEL;
if (Bstatic == 0) { /* Dynamic linkage */
for (i = 0; dynlinker[i]; i++)
av[j++] = dynlinker[i];
} else
av[j++] = "-Bstatic";
if (outfile) {
av[j++] = "-o";
av[j++] = outfile;
}
if (!nostartfiles) {
av[j++] = crt0file;
for (i = 0; startfiles[i]; i++)
av[j++] = startfiles[i];
}
i = 0;
while(i<nl) {
av[j++] = llist[i++];
if (j >= MAXAV)
error("Too many ld options");
}
if (gflag)
av[j++] = "-g";
#if 0
if (gflag)
av[j++] = "-lg";
#endif
if (pthreads)
av[j++] = "-lpthread";
if (!nostdlib)
for (i = 0; libclibs[i]; i++)
av[j++] = libclibs[i];
if (!nostartfiles) {
for (i = 0; endfiles[i]; i++)
av[j++] = endfiles[i];
}
av[j++] = 0;
eflag |= callsys("/bin/ld", av);
if (nc==1 && nxo==1 && eflag==0)
cunlink(setsuf(clist[0], 'o'));
else if (nc > 0 && eflag == 0) {
/* remove .o files XXX ugly */
for (i = 0; i < nc; i++)
cunlink(setsuf(clist[i], 'o'));
}
}
dexit(eflag);
return 0;
}
/*
* exit and cleanup after interrupt.
*/
void
idexit(int arg)
{
dexit(100);
}
/*
* exit and cleanup.
*/
void
dexit(int eval)
{
if (!pflag && !Xflag) {
if (sflag==0)
cunlink(tmp3);
cunlink(tmp4);
}
if (eval == 100)
_exit(eval);
exit(eval);
}
static void
ccerror(char *s, va_list ap)
{
vfprintf(Eflag ? stderr : stdout, s, ap);
putc('\n', Eflag? stderr : stdout);
exfail++;
cflag++;
eflag++;
}
/*
* complain a bit.
*/
void
error(char *s, ...)
{
va_list ap;
va_start(ap, s);
ccerror(s, ap);
va_end(ap);
}
/*
* complain a bit and then exit.
*/
void
errorx(int eval, char *s, ...)
{
va_list ap;
va_start(ap, s);
ccerror(s, ap);
va_end(ap);
dexit(eval);
}
int
getsuf(char *as)
{
register char *s;
if ((s = strrchr(as, '.')) && s[1] != '\0' && s[2] == '\0')
return s[1];
return(0);
}
/*
* Get basename of string s and change its suffix to ch.
*/
char *
setsuf(char *s, char ch)
{
s = copy(basename(s));
s[strlen(s) - 1] = ch;
return(s);
}
int
callsys(char f[], char *v[])
{
int status;
pid_t t;
char *s;
if (vflag) {
fprintf(stderr, "%s ", f);
for (t = 1; v[t]; t++)
fprintf(stderr, "%s ", v[t]);
fprintf(stderr, "\n");
}
if ((t=fork())==0) {
if (Bflag) {
size_t len = strlen(Bflag) + 8;
char *a = malloc(len);
if (a == NULL) {
error("callsys: malloc failed");
exit(1);
}
if ((s = strrchr(f, '/'))) {
strlcpy(a, Bflag, len);
strlcat(a, s, len);
execv(a, v);
}
}
execv(f, v);
if ((s = strrchr(f, '/')))
execvp(s+1, v);
printf("Can't find %s\n", f);
exit(100);
} else
if (t == -1) {
printf("Try again\n");
return(100);
}
while(t!=wait(&status));
if ((t=(status&0377)) != 0 && t!=14) {
if (t!=2) /* interrupt */
errorx(8, "Fatal error in %s", f);
dexit(eflag);
}
return((status>>8) & 0377);
}
char *
copy(char *as)
{
char *p;
if ((p = strdup(as)) == NULL)
errorx(8, "no space for file names");
return p;
}
int
nodup(char **l, char *os)
{
register char *t, *s;
register int c;
s = os;
if (getsuf(s) != 'o')
return(1);
while((t = *l++)) {
while((c = *s++))
if (c != *t++)
break;
if (*t=='\0' && c=='\0')
return(0);
s = os;
}
return(1);
}
int
cunlink(char *f)
{
if (f==0 || Xflag)
return(0);
return(unlink(f));
}
char *
gettmp(void)
{
char *sfn = copy("/tmp/ctm.XXXXXX");
int fd = -1;
if ((fd = mkstemp(sfn)) == -1) {
fprintf(stderr, "%s: %s\n", sfn, strerror(errno));
exit(8);
}
close(fd);
return sfn;
}
|