Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:pj:20041219194358

Diff

Diff from 1.26 to:

Annotations

Annotate by Age | Author | Mixed | None
/fisheye/browse/pcc/pcc/cc/cc/cc.c

Annotated File View

pj
1.26
1 /*      $Id: cc.c,v 1.26 2004/12/19 19:43:58 pj Exp $   */
ragge
1.3
2 /*
3  * Copyright(C) Caldera International Inc. 2001-2002. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * Redistributions of source code and documentation must retain the above
10  * copyright notice, this list of conditions and the following disclaimer.
11  * Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditionsand the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  * All advertising materials mentioning features or use of this software
15  * must display the following acknowledgement:
16  *      This product includes software developed or owned by Caldera
17  *      International, Inc.
18  * Neither the name of Caldera International, Inc. nor the names of other
19  * contributors may be used to endorse or promote products derived from
20  * this software without specific prior written permission.
21  *
22  * USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA
23  * INTERNATIONAL, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26  * DISCLAIMED.  IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE LIABLE
27  * FOR ANY DIRECT, INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OFLIABILITY, WHETHER IN CONTRACT,
31  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
32  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
33  * POSSIBILITY OF SUCH DAMAGE.
34  */
ragge
1.2
35
ragge
1.1
36 /*
ragge
1.3
37  * Front-end to the C compiler.
ragge
1.18
38  *
39  * Brief description of its syntax:
40  * - Files that end with .c are passed via cpp->ccom->as->ld
41  * - Files that end with .s are passed as->ld
42  * - Files that end with .o are passed directly to ld
43  * - Multiple files may be given on the command line.
44  * - Unrecognized options are all sent directly to ld.
45  * -c or -S cannot be combined with -o if multiple files are given.
46  *
47  * This file should be rewritten readable.
ragge
1.1
48  */
ragge
1.9
49 #include <sys/types.h>
ragge
1.2
50
ragge
1.1
51 #include <stdio.h>
52 #include <ctype.h>
53 #include <signal.h>
ragge
1.4
54 #include <string.h>
55 #include <fcntl.h>
56 #include <unistd.h>
57 #include <stdlib.h>
58 #include <sys/wait.h>
ragge
1.24
59
60 #include "ccconfig.h"
ragge
1.3
61 /* C command */
ragge
1.1
62
ragge
1.20
63 #define MKS(x) _MKS(x)
64 #define _MKS(x) #x
65
66 /*
67  * Many specific definitions, should be declared elsewhere.
68  */
69 #define PCC_MINOR 0
70 #define PCC_MAJOR 1
71 #define STDINC    "/usr/include"
72
ragge
1.3
73 #define SBSIZE 10000
ragge
1.12
74 #define MAXINC 100
ragge
1.3
75 #define MAXFIL 100
ragge
1.12
76 #define MAXLIB 10000
ragge
1.14
77 #define MAXAV  10000
ragge
1.3
78 #define MAXOPT 100
79 char    *tmp0;
80 char    *tmp1;
81 char    *tmp2;
82 char    *tmp3;
83 char    *tmp4;
84 char    *tmp5;
ragge
1.1
85 char    *outfile;
ragge
1.4
86 char *copy(char []),*setsuf(char [], int);
87 int getsuf(char []);
88 int main(intchar *[]);
89 void error(char *, char *);
ragge
1.18
90 void errorx(char *, char *, int);
ragge
1.4
91 int nodup(char **, char *);
92 int callsys(char [], char *[]);
93 int cunlink(char *);
94 void dexit(void);
95 void idexit(int);
ragge
1.3
96 # define CHSPACE 1000
97 char    ts[CHSPACE+50];
98 char    *tsa = ts;
99 char    *tsp = ts;
ragge
1.14
100 char    *av[MAXAV];
ragge
1.3
101 char    *clist[MAXFIL];
102 char    *llist[MAXLIB];
ragge
1.4
103 char    alist[20];
ragge
1.3
104 int dflag;
ragge
1.7
105 int     xflag;
ragge
1.3
106 int     pflag;
107 int     sflag;
108 int     cflag;
109 int     eflag;
110 int     gflag;
ragge
1.5
111 int     vflag;
ragge
1.7
112 int     tflag;
ragge
1.8
113 int     Eflag;
ragge
1.4
114 int     Oflag;
ragge
1.3
115 int     proflag;
ragge
1.1
116 int     exfail;
ragge
1.10
117 int     Xflag;
ragge
1.14
118 int     nostartfilesBstatic;
ragge
1.20
119 int     nostdinc;
ragge
1.14
120
ragge
1.23
121 char    *pass0 = LIBEXECDIR "/ccom";
122 char    *passp = LIBEXECDIR "/cpp";
ragge
1.20
123 char    *sysinc;
ragge
1.25
124 char *cppadd[] = CPPADD;
125 char *dynlinker[] = DYNLINKER;
126 char *crt0file = CRT0FILE;
127 char *startfiles[] = STARTFILES;
128 char *endfiles[] = ENDFILES;
129 char *cppmdadd[] = CPPMDADD;
ragge
1.1
130
ragge
1.4
131 int
pj
1.26
132 main(int argcchar *argv[])
133 {
ragge
1.15
134         char *t, *u;
ragge
1.3
135         char *savetsp;
ragge
1.1
136         char *assource;
ragge
1.3
137         char **pv, *ptemp[MAXOPT], **pvt;
ragge
1.18
138         int ncnlijcf20nxona;
ragge
1.4
139         FILE *f;
ragge
1.3
140
141         i = nc = nl = f20 = nxo = 0;
142         pv = ptemp;
143         while(++i < argc) {
ragge
1.18
144                 if (argv[i][0] == '-')
145                 switch (argv[i][1]) {
ragge
1.3
146                 default:
147                         goto passa;
ragge
1.22
148
149                 case 'B'/* other search paths for binaries XXX support? */
150                         break;
151
ragge
1.10
152                 case 'X':
153                         Xflag++;
154                         break;
ragge
1.15
155                 case 'W'/* Ignore (most of) W-flags */
156                         if (strncmp(argv[i], "-Wl,"4) == 0) {
157                                 /* options to the linker */
158                                 t = &argv[i][4];
159                                 while ((u = strchr(t','))) {
160                                         *u++ = 0;
161                                         llist[nl++] = t;
162                                         t = u;
163                                 }
164                                 llist[nl++] = t;
165                         }
166                         break;
167
ragge
1.6
168                 case 'f'/* Ignore -ffreestanding */
169                         break;
ragge
1.14
170
ragge
1.25
171                 case 'g'/* create debug output */
172                         gflag++;
173                         break;
174
ragge
1.20
175                 case 'i':
176                         if (strcmp(argv[i], "-isystem") == 0) {
177                                 sysinc = argv[++i];
178                         } else
179                                 goto passa;
180                         break;
181
ragge
1.14
182                 case 'n'/* handle -n flags */
183                         if (strcmp(argv[i], "-nostdinc") == 0)
ragge
1.20
184                                 nostdinc++;
ragge
1.14
185                         else if (strcmp(argv[i], "-nostartfiles") == 0)
186                                 nostartfiles = 1;
187                         else
188                                 goto passa;
189                         break;
190
ragge
1.7
191                 case 'x':
192                         xflag++;
193                         i++; /* skip args */
194                         break;
195                 case 't':
196                         tflag++;
197                         break;
ragge
1.1
198                 case 'S':
199                         sflag++;
200                         cflag++;
ragge
1.3
201                         break;
ragge
1.1
202                 case 'o':
ragge
1.18
203                         if (outfile)
204                                 errorx("too many -o"""8);
205                         outfile = argv[++i];
ragge
1.3
206                         break;
ragge
1.1
207                 case 'O':
ragge
1.4
208                         Oflag++;
ragge
1.3
209                         break;
ragge
1.1
210                 case 'p':
211                         proflag++;
ragge
1.3
212                         break;
ragge
1.1
213                 case 'E':
ragge
1.8
214                         Eflag++;
215                         break;
ragge
1.1
216                 case 'P':
217                         pflag++;
ragge
1.3
218                         *pv++ = argv[i];
ragge
1.1
219                 case 'c':
220                         cflag++;
ragge
1.3
221                         break;
222
ragge
1.24
223 #if 0
ragge
1.3
224                 case '2':
225                         if(argv[i][2] == '\0')
226                                 pref = "/lib/crt2.o";
227                         else {
228                                 pref = "/lib/crt20.o";
229                                 f20 = 1;
230                         }
231                         break;
ragge
1.24
232 #endif
ragge
1.1
233                 case 'D':
234                 case 'I':
235                 case 'U':
236                 case 'C':
ragge
1.20
237                         *pv++ = argv[i];
ragge
1.3
238                         if (pv >= ptemp+MAXOPT)
239                                 {
240                                 error("Too many DIUC options"0);
241                                 --pv;
242                                 }
243                         break;
244
ragge
1.1
245                 case 'd':
ragge
1.3
246                         dflag++;
ragge
1.4
247                         strncpy(alistargv[i], 19);
ragge
1.3
248                         break;
ragge
1.5
249                 case 'v':
250                         vflag++;
251                         break;
ragge
1.14
252
253                 case 's':
254                         if (strcmp(argv[i], "-static") == 0)
255                                 Bstatic = 1;
256                         else
257                                 goto passa;
258                         break;
ragge
1.3
259                 } else {
260                 passa:
261                         t = argv[i];
ragge
1.11
262                         if (*argv[i] == '-' && argv[i][1] == 'L')
263                                 ;
264                         else if((c=getsuf(t))=='c' || c=='s'|| Eflag) {
ragge
1.3
265                                 clist[nc++] = t;
266                                 if (nc>=MAXFIL)
267                                         {
268                                         error("Too many source files",0);
269                                         exit(1);
270                                         }
271                                 t = setsuf(t'o');
272                         }
273                         if (nodup(llistt)) {
274                                 llist[nl++] = t;
275                                 if (nl >= MAXLIB)
276                                         {
277                                         error("Too many object/library files",0);
278                                         exit(1);
279                                         }
280                                 if (getsuf(t)=='o')
281                                         nxo++;
ragge
1.1
282                         }
283                 }
284         }
ragge
1.18
285         /* Sanity checking */
286         if (nc == 0 && nl == 0)
287                 errorx("no input files"""8);
288         if (outfile && (cflag || sflag) && nc > 1)
289                 errorx("-o given with -c || -S and more than one file"""8);
ragge
1.19
290         if (outfile && clist[0] && strcmp(outfileclist[0]) == 0)
ragge
1.18
291                 errorx("output file will be clobbered"""8);
292
ragge
1.4
293         if (gflagOflag = 0;
ragge
1.24
294 #if 0
295         if (proflag)
ragge
1.3
296                 pref = "/lib/mcrt0.o";
ragge
1.24
297 #endif
ragge
1.3
298         if(nc==0)
299                 goto nocom;
300         if (pflag==0) {
301                 tmp0 = copy("/tmp/ctm0a");
ragge
1.4
302                 while((f=fopen(tmp0"r")) != NULL) {
303                         fclose(f);
ragge
1.3
304                         tmp0[9]++;
ragge
1.1
305                 }
ragge
1.3
306                 while((creat(tmp00400))<0)
307                         tmp0[9]++;
ragge
1.1
308         }
ragge
1.3
309         if (signal(SIGINTSIG_IGN) != SIG_IGN/* interrupt */
ragge
1.1
310                 signal(SIGINTidexit);
ragge
1.3
311         if (signal(SIGTERMSIG_IGN) != SIG_IGN)        /* terminate */
ragge
1.1
312                 signal(SIGTERMidexit);
ragge
1.3
313         (tmp1 = copy(tmp0))[8] = '1';
314         (tmp2 = copy(tmp0))[8] = '2';
315         (tmp3 = copy(tmp0))[8] = '3';
ragge
1.1
316         if (pflag==0)
ragge
1.3
317                 (tmp4 = copy(tmp0))[8] = '4';
318         pvt = pv;
ragge
1.1
319         for (i=0i<nci++) {
ragge
1.18
320                 /*
321                  * C preprocessor
322                  */
ragge
1.3
323                 if (nc>1)
ragge
1.1
324                         printf("%s:\n"clist[i]);
ragge
1.3
325                 if (getsuf(clist[i])=='s') {
ragge
1.1
326                         assource = clist[i];
ragge
1.7
327                         if (!xflag)
328                                 goto assemble;
ragge
1.1
329                 } else
330                         assource = tmp3;
331                 if (pflag)
332                         tmp4 = setsuf(clist[i], 'i');
ragge
1.3
333                 savetsp = tsp;
ragge
1.5
334                 na = 0;
335                 av[na++] = "cpp";
ragge
1.20
336                 av[na++] = "-D__PCC__=" MKS(PCC_MAJOR);
337                 av[na++] = "-D__PCC_MINOR__" MKS(PCC_MINOR);
338                 if (!nostdinc)
ragge
1.21
339                         av[na++] = "-S"av[na++] = STDINC;
ragge
1.20
340                 if (sysinc)
341                         av[na++] = "-S"av[na++] = sysinc;
ragge
1.5
342                 for (j = 0cppadd[j]; j++)
343                         av[na++] = cppadd[j];
ragge
1.24
344                 for (j = 0cppmdadd[j]; j++)
345                         av[na++] = cppmdadd[j];
ragge
1.7
346                 if (tflag)
ragge
1.20
347                         av[na++] = "-t";
ragge
1.7
348                 for(pv=ptemppv <pvtpv++)
349                         av[na++] = *pv;
ragge
1.5
350                 av[na++] = clist[i];
ragge
1.20
351                 if (!Eflag)
352                         av[na++] = tmp4;
ragge
1.3
353                 av[na++]=0;
354                 if (callsys(passpav))
355                         {exfail++; eflag++;}
ragge
1.11
356                 if (Eflag)
357                         dexit();
ragge
1.7
358                 if (xflag)
359                         goto assemble;
ragge
1.18
360
361                 /*
362                  * C compiler
363                  */
ragge
1.25
364                 na = 0;
365                 av[na++]= "ccom";
366                 if (gflag)
367                         av[na++] = "-g";
368                 av[na++] = tmp4;
ragge
1.3
369                 tsp = savetsp;
370                 if (pflag || exfail)
371                         {
ragge
1.1
372                         cflag++;
373                         continue;
ragge
1.3
374                         }
ragge
1.16
375                 if(sflag) {
376                         if (outfile)
377                                 assource = tmp3 = outfile;
378                         else
379                                 assource = tmp3 = setsuf(clist[i], 's');
380                 }
ragge
1.25
381                 av[na++] = tmp3;
382 #if 0
ragge
1.3
383                 if (proflag) {
384                         av[3] = "-XP";
385                         av[4] = 0;
386                 } else
387                         av[3] = 0;
ragge
1.25
388 #endif
389                 av[na++] = NULL;
ragge
1.3
390                 if (callsys(pass0av)) {
ragge
1.1
391                         cflag++;
392                         eflag++;
393                         continue;
394                 }
395                 if (sflag)
396                         continue;
ragge
1.18
397
398                 /*
399                  * Assembler
400                  */
ragge
1.1
401         assemble:
ragge
1.3
402                 av[0] = "as";
403                 av[1] = "-o";
ragge
1.18
404                 if (outfile && cflag)
405                         av[2] = outfile;
406                 else
407                         av[2] = setsuf(clist[i], 'o');
ragge
1.7
408                 av[3] = xflag ? tmp4 : assource;
ragge
1.3
409                 if (dflag) {
410                         av[4] = alist;
411                         av[5] = 0;
412                 } else
413                         av[4] = 0;
414                 cunlink(tmp1);
415                 cunlink(tmp2);
ragge
1.21
416                 if (callsys("/bin/as"av)) {
ragge
1.1
417                         cflag++;
418                         eflag++;
ragge
1.7
419                         cunlink(tmp4);
ragge
1.1
420                         continue;
421                 }
ragge
1.7
422                 cunlink(tmp4);
ragge
1.1
423         }
ragge
1.18
424
425         /*
426          * Linker
427          */
ragge
1.1
428 nocom:
429         if (cflag==0 && nl!=0) {
ragge
1.6
430                 j = 0;
431                 av[j++] = "ld";
432                 av[j++] = "-X";
433                 av[j++] = "-d";
434                 av[j++] = "-e";
435                 av[j++] = "__start";
ragge
1.14
436                 if (Bstatic == 0) { /* Dynamic linkage */
ragge
1.24
437                         for (i = 0dynlinker[i]; i++)
438                                 av[j++] = dynlinker[i];
ragge
1.14
439                 }
ragge
1.1
440                 if (outfile) {
ragge
1.3
441                         av[j++] = "-o";
442                         av[j++] = outfile;
443                 }
ragge
1.14
444                 if (!nostartfiles) {
ragge
1.24
445                         av[j++] = crt0file;
446                         for (i = 0startfiles[i]; i++)
447                                 av[j++] = startfiles[i];
ragge
1.14
448                 }
ragge
1.24
449                 i = 0;
ragge
1.14
450                 while(i<nl) {
ragge
1.3
451                         av[j++] = llist[i++];
ragge
1.14
452                         if (j >= MAXAV)
453                                 error("Too many ld options"0);
454                 }
ragge
1.11
455 #if 0
ragge
1.3
456                 if (gflag)
457                         av[j++] = "-lg";
ragge
1.11
458 #endif
ragge
1.6
459                 av[j++] = "-lc";
ragge
1.14
460                 if (!nostartfiles) {
ragge
1.24
461                         for (i = 0endfiles[i]; i++)
462                                 av[j++] = endfiles[i];
ragge
1.14
463                 }
ragge
1.3
464                 if(f20)
465                         av[j++] = "-l2";
466                 av[j++] = 0;
467                 eflag |= callsys("/bin/ld"av);
ragge
1.1
468                 if (nc==1 && nxo==1 && eflag==0)
ragge
1.3
469                         cunlink(setsuf(clist[0], 'o'));
ragge
1.1
470         }
471         dexit();
ragge
1.4
472         return 0;
ragge
1.1
473 }
474
ragge
1.4
475 void
476 idexit(int arg)
ragge
1.1
477 {
478         eflag = 100;
479         dexit();
480 }
481
ragge
1.4
482 void
ragge
1.3
483 dexit()
ragge
1.1
484 {
ragge
1.10
485         if (!pflag && !Xflag) {
ragge
1.1
486                 cunlink(tmp1);
487                 cunlink(tmp2);
488                 if (sflag==0)
489                         cunlink(tmp3);
490                 cunlink(tmp4);
491                 cunlink(tmp5);
ragge
1.3
492                 cunlink(tmp0);
ragge
1.1
493         }
494         exit(eflag);
495 }
496
ragge
1.4
497 void
498 error(char *schar *x)
ragge
1.1
499 {
ragge
1.8
500         fprintf(Eflag?stderr:stdout , sx);
501         putc('\n'Eflagstderr : stdout);
ragge
1.1
502         exfail++;
503         cflag++;
504         eflag++;
505 }
506
ragge
1.18
507 void
508 errorx(char *schar *xint eval)
509 {
510         error(sx);
511         exit(eval);
512 }
ragge
1.3
513
ragge
1.4
514 int
ragge
1.3
515 getsuf(as)
516 char as[];
ragge
1.1
517 {
518         register char *s;
519
ragge
1.4
520         if ((s = strrchr(as'.')) && s[1] != '\0' && s[2] == '\0')
521                 return s[1];
ragge
1.3
522         return(0);
ragge
1.1
523 }
524
525 char *
ragge
1.3
526 setsuf(asch)
527 char as[];
ragge
1.1
528 {
529         register char *s, *s1;
530
ragge
1.3
531         s = s1 = copy(as);
532         while(*s)
ragge
1.1
533                 if (*s++ == '/')
534                         s1 = s;
535         s[-1] = ch;
ragge
1.3
536         return(s1);
ragge
1.1
537 }
538
ragge
1.4
539 int
ragge
1.3
540 callsys(fv)
541 char f[], *v[]; {
ragge
1.1
542         int tstatus;
ragge
1.4
543         char *s;
ragge
1.5
544
545         if (vflag) {
546                 for (t = 0v[t]; t++)
ragge
1.6
547                         fprintf(stderr"%s "v[t]);
548                 fprintf(stderr"\n");
ragge
1.5
549         }
ragge
1.1
550
ragge
1.3
551         if ((t=fork())==0) {
ragge
1.1
552                 execv(fv);
ragge
1.4
553                 if ((s = strrchr(f'/')))
554                         execvp(s+1v);
ragge
1.1
555                 printf("Can't find %s\n"f);
ragge
1.3
556                 exit(100);
557         } else
558                 if (t == -1) {
559                         printf("Try again\n");
560                         return(100);
561                 }
562         while(t!=wait(&status));
ragge
1.1
563         if ((t=(status&0377)) != 0 && t!=14) {
ragge
1.3
564                 if (t!=2)               /* interrupt */
565                         {
ragge
1.1
566                         printf("Fatal error in %s\n"f);
567                         eflag = 8;
ragge
1.3
568                         }
ragge
1.1
569                 dexit();
570         }
ragge
1.3
571         return((status>>8) & 0377);
ragge
1.1
572 }
573
ragge
1.3
574 char *
575 copy(as)
576 char as[];
577 {
578         register char *otsp, *s;
579
580         otsp = tsp;
581         s = as;
ragge
1.4
582         while((*tsp++ = *s++));
ragge
1.3
583         if (tsp >tsa+CHSPACE)
584                 {
ragge
1.4
585                 tsp = tsa = calloc(CHSPACE+50,1);
586                 if (tsa== 0){
587                         error("no space for file names"0);
588                         eflag = 8;
589                         dexit();
ragge
1.3
590                         }
591                 }
592         return(otsp);
593 }
594
ragge
1.4
595 int
ragge
1.3
596 nodup(los)
597 char **l, *os;
ragge
1.1
598 {
599         register char *t, *s;
600         register int c;
601
602         s = os;
603         if (getsuf(s) != 'o')
ragge
1.3
604                 return(1);
ragge
1.4
605         while((t = *l++)) {
606                 while((c = *s++))
ragge
1.1
607                         if (c != *t++)
608                                 break;
ragge
1.3
609                 if (*t=='\0' && c=='\0')
610                         return(0);
ragge
1.1
611                 s = os;
612         }
ragge
1.3
613         return(1);
ragge
1.1
614 }
615
ragge
1.4
616 int
ragge
1.3
617 cunlink(f)
618 char *f;
ragge
1.1
619 {
ragge
1.13
620         if (f==0 || Xflag)
ragge
1.3
621                 return(0);
622         return(unlink(f));
ragge
1.1
623 }
FishEye: Open Source License registered to PCC.
Atlassian FishEye, CVS analysis. (Version:1.6.3 Build:build-336 2008-11-04) - Administration - Page generated 2014-12-25 20:56 +0100