Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20041120105845

Diff

Diff from 1.24 to:

Annotations

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

Annotated File View

ragge
1.24
1 /*      $Id: cc.c,v 1.24 2004/11/20 10:58:45 ragge 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.1
124
ragge
1.4
125 int
ragge
1.3
126 main(argcargv)
127 char *argv[]; {
ragge
1.15
128         char *t, *u;
ragge
1.3
129         char *savetsp;
ragge
1.1
130         char *assource;
ragge
1.3
131         char **pv, *ptemp[MAXOPT], **pvt;
ragge
1.18
132         int ncnlijcf20nxona;
ragge
1.4
133         FILE *f;
ragge
1.3
134
135         i = nc = nl = f20 = nxo = 0;
136         pv = ptemp;
137         while(++i < argc) {
ragge
1.18
138                 if (argv[i][0] == '-')
139                 switch (argv[i][1]) {
ragge
1.3
140                 default:
141                         goto passa;
ragge
1.22
142
143                 case 'B'/* other search paths for binaries XXX support? */
144                         break;
145
ragge
1.10
146                 case 'X':
147                         Xflag++;
148                         break;
ragge
1.15
149                 case 'W'/* Ignore (most of) W-flags */
150                         if (strncmp(argv[i], "-Wl,"4) == 0) {
151                                 /* options to the linker */
152                                 t = &argv[i][4];
153                                 while ((u = strchr(t','))) {
154                                         *u++ = 0;
155                                         llist[nl++] = t;
156                                         t = u;
157                                 }
158                                 llist[nl++] = t;
159                         }
160                         break;
161
ragge
1.6
162                 case 'f'/* Ignore -ffreestanding */
163                         break;
ragge
1.14
164
ragge
1.20
165                 case 'i':
166                         if (strcmp(argv[i], "-isystem") == 0) {
167                                 sysinc = argv[++i];
168                         } else
169                                 goto passa;
170                         break;
171
ragge
1.14
172                 case 'n'/* handle -n flags */
173                         if (strcmp(argv[i], "-nostdinc") == 0)
ragge
1.20
174                                 nostdinc++;
ragge
1.14
175                         else if (strcmp(argv[i], "-nostartfiles") == 0)
176                                 nostartfiles = 1;
177                         else
178                                 goto passa;
179                         break;
180
ragge
1.7
181                 case 'x':
182                         xflag++;
183                         i++; /* skip args */
184                         break;
185                 case 't':
186                         tflag++;
187                         break;
ragge
1.1
188                 case 'S':
189                         sflag++;
190                         cflag++;
ragge
1.3
191                         break;
ragge
1.1
192                 case 'o':
ragge
1.18
193                         if (outfile)
194                                 errorx("too many -o"""8);
195                         outfile = argv[++i];
ragge
1.3
196                         break;
ragge
1.1
197                 case 'O':
ragge
1.4
198                         Oflag++;
ragge
1.3
199                         break;
ragge
1.1
200                 case 'p':
201                         proflag++;
ragge
1.3
202                         break;
ragge
1.1
203                 case 'g':
ragge
1.3
204                         gflag++;
205                         break;
ragge
1.1
206                 case 'E':
ragge
1.8
207                         Eflag++;
208                         break;
ragge
1.1
209                 case 'P':
210                         pflag++;
ragge
1.3
211                         *pv++ = argv[i];
ragge
1.1
212                 case 'c':
213                         cflag++;
ragge
1.3
214                         break;
215
ragge
1.24
216 #if 0
ragge
1.3
217                 case '2':
218                         if(argv[i][2] == '\0')
219                                 pref = "/lib/crt2.o";
220                         else {
221                                 pref = "/lib/crt20.o";
222                                 f20 = 1;
223                         }
224                         break;
ragge
1.24
225 #endif
ragge
1.1
226                 case 'D':
227                 case 'I':
228                 case 'U':
229                 case 'C':
ragge
1.20
230                         *pv++ = argv[i];
ragge
1.3
231                         if (pv >= ptemp+MAXOPT)
232                                 {
233                                 error("Too many DIUC options"0);
234                                 --pv;
235                                 }
236                         break;
237
ragge
1.1
238                 case 'd':
ragge
1.3
239                         dflag++;
ragge
1.4
240                         strncpy(alistargv[i], 19);
ragge
1.3
241                         break;
ragge
1.5
242                 case 'v':
243                         vflag++;
244                         break;
ragge
1.14
245
246                 case 's':
247                         if (strcmp(argv[i], "-static") == 0)
248                                 Bstatic = 1;
249                         else
250                                 goto passa;
251                         break;
ragge
1.3
252                 } else {
253                 passa:
254                         t = argv[i];
ragge
1.11
255                         if (*argv[i] == '-' && argv[i][1] == 'L')
256                                 ;
257                         else if((c=getsuf(t))=='c' || c=='s'|| Eflag) {
ragge
1.3
258                                 clist[nc++] = t;
259                                 if (nc>=MAXFIL)
260                                         {
261                                         error("Too many source files",0);
262                                         exit(1);
263                                         }
264                                 t = setsuf(t'o');
265                         }
266                         if (nodup(llistt)) {
267                                 llist[nl++] = t;
268                                 if (nl >= MAXLIB)
269                                         {
270                                         error("Too many object/library files",0);
271                                         exit(1);
272                                         }
273                                 if (getsuf(t)=='o')
274                                         nxo++;
ragge
1.1
275                         }
276                 }
277         }
ragge
1.18
278         /* Sanity checking */
279         if (nc == 0 && nl == 0)
280                 errorx("no input files"""8);
281         if (outfile && (cflag || sflag) && nc > 1)
282                 errorx("-o given with -c || -S and more than one file"""8);
ragge
1.19
283         if (outfile && clist[0] && strcmp(outfileclist[0]) == 0)
ragge
1.18
284                 errorx("output file will be clobbered"""8);
285
ragge
1.4
286         if (gflagOflag = 0;
ragge
1.24
287 #if 0
288         if (proflag)
ragge
1.3
289                 pref = "/lib/mcrt0.o";
ragge
1.24
290 #endif
ragge
1.3
291         if(nc==0)
292                 goto nocom;
293         if (pflag==0) {
294                 tmp0 = copy("/tmp/ctm0a");
ragge
1.4
295                 while((f=fopen(tmp0"r")) != NULL) {
296                         fclose(f);
ragge
1.3
297                         tmp0[9]++;
ragge
1.1
298                 }
ragge
1.3
299                 while((creat(tmp00400))<0)
300                         tmp0[9]++;
ragge
1.1
301         }
ragge
1.3
302         if (signal(SIGINTSIG_IGN) != SIG_IGN/* interrupt */
ragge
1.1
303                 signal(SIGINTidexit);
ragge
1.3
304         if (signal(SIGTERMSIG_IGN) != SIG_IGN)        /* terminate */
ragge
1.1
305                 signal(SIGTERMidexit);
ragge
1.3
306         (tmp1 = copy(tmp0))[8] = '1';
307         (tmp2 = copy(tmp0))[8] = '2';
308         (tmp3 = copy(tmp0))[8] = '3';
ragge
1.1
309         if (pflag==0)
ragge
1.3
310                 (tmp4 = copy(tmp0))[8] = '4';
311         pvt = pv;
ragge
1.1
312         for (i=0i<nci++) {
ragge
1.18
313                 /*
314                  * C preprocessor
315                  */
ragge
1.3
316                 if (nc>1)
ragge
1.1
317                         printf("%s:\n"clist[i]);
ragge
1.3
318                 if (getsuf(clist[i])=='s') {
ragge
1.1
319                         assource = clist[i];
ragge
1.7
320                         if (!xflag)
321                                 goto assemble;
ragge
1.1
322                 } else
323                         assource = tmp3;
324                 if (pflag)
325                         tmp4 = setsuf(clist[i], 'i');
ragge
1.3
326                 savetsp = tsp;
ragge
1.5
327                 na = 0;
328                 av[na++] = "cpp";
ragge
1.20
329                 av[na++] = "-D__PCC__=" MKS(PCC_MAJOR);
330                 av[na++] = "-D__PCC_MINOR__" MKS(PCC_MINOR);
331                 if (!nostdinc)
ragge
1.21
332                         av[na++] = "-S"av[na++] = STDINC;
ragge
1.20
333                 if (sysinc)
334                         av[na++] = "-S"av[na++] = sysinc;
ragge
1.5
335                 for (j = 0cppadd[j]; j++)
336                         av[na++] = cppadd[j];
ragge
1.24
337                 for (j = 0cppmdadd[j]; j++)
338                         av[na++] = cppmdadd[j];
ragge
1.7
339                 if (tflag)
ragge
1.20
340                         av[na++] = "-t";
ragge
1.7
341                 for(pv=ptemppv <pvtpv++)
342                         av[na++] = *pv;
ragge
1.5
343                 av[na++] = clist[i];
ragge
1.20
344                 if (!Eflag)
345                         av[na++] = tmp4;
ragge
1.3
346                 av[na++]=0;
347                 if (callsys(passpav))
348                         {exfail++; eflag++;}
ragge
1.11
349                 if (Eflag)
350                         dexit();
ragge
1.7
351                 if (xflag)
352                         goto assemble;
ragge
1.18
353
354                 /*
355                  * C compiler
356                  */
ragge
1.5
357                 av[0]= "ccom";
ragge
1.18
358                 av[1] = tmp4;
ragge
1.3
359                 tsp = savetsp;
360                 if (pflag || exfail)
361                         {
ragge
1.1
362                         cflag++;
363                         continue;
ragge
1.3
364                         }
ragge
1.16
365                 if(sflag) {
366                         if (outfile)
367                                 assource = tmp3 = outfile;
368                         else
369                                 assource = tmp3 = setsuf(clist[i], 's');
370                 }
ragge
1.3
371                 av[2] = tmp3;
372                 if (proflag) {
373                         av[3] = "-XP";
374                         av[4] = 0;
375                 } else
376                         av[3] = 0;
377                 if (gflag) {
378                         int i;
379                         i = av[3] ? 4 : 3;
380                         av[i++] = "-Xg";
381                         av[i] = 0;
ragge
1.1
382                 }
ragge
1.3
383                 if (callsys(pass0av)) {
ragge
1.1
384                         cflag++;
385                         eflag++;
386                         continue;
387                 }
388                 if (sflag)
389                         continue;
ragge
1.18
390
391                 /*
392                  * Assembler
393                  */
ragge
1.1
394         assemble:
ragge
1.3
395                 av[0] = "as";
396                 av[1] = "-o";
ragge
1.18
397                 if (outfile && cflag)
398                         av[2] = outfile;
399                 else
400                         av[2] = setsuf(clist[i], 'o');
ragge
1.7
401                 av[3] = xflag ? tmp4 : assource;
ragge
1.3
402                 if (dflag) {
403                         av[4] = alist;
404                         av[5] = 0;
405                 } else
406                         av[4] = 0;
407                 cunlink(tmp1);
408                 cunlink(tmp2);
ragge
1.21
409                 if (callsys("/bin/as"av)) {
ragge
1.1
410                         cflag++;
411                         eflag++;
ragge
1.7
412                         cunlink(tmp4);
ragge
1.1
413                         continue;
414                 }
ragge
1.7
415                 cunlink(tmp4);
ragge
1.1
416         }
ragge
1.18
417
418         /*
419          * Linker
420          */
ragge
1.1
421 nocom:
422         if (cflag==0 && nl!=0) {
ragge
1.6
423                 j = 0;
424                 av[j++] = "ld";
425                 av[j++] = "-X";
426                 av[j++] = "-d";
427                 av[j++] = "-e";
428                 av[j++] = "__start";
ragge
1.14
429                 if (Bstatic == 0) { /* Dynamic linkage */
ragge
1.24
430                         for (i = 0dynlinker[i]; i++)
431                                 av[j++] = dynlinker[i];
ragge
1.14
432                 }
ragge
1.1
433                 if (outfile) {
ragge
1.3
434                         av[j++] = "-o";
435                         av[j++] = outfile;
436                 }
ragge
1.14
437                 if (!nostartfiles) {
ragge
1.24
438                         av[j++] = crt0file;
439                         for (i = 0startfiles[i]; i++)
440                                 av[j++] = startfiles[i];
ragge
1.14
441                 }
ragge
1.24
442                 i = 0;
ragge
1.14
443                 while(i<nl) {
ragge
1.3
444                         av[j++] = llist[i++];
ragge
1.14
445                         if (j >= MAXAV)
446                                 error("Too many ld options"0);
447                 }
ragge
1.11
448 #if 0
ragge
1.3
449                 if (gflag)
450                         av[j++] = "-lg";
ragge
1.11
451 #endif
ragge
1.6
452                 av[j++] = "-lc";
ragge
1.14
453                 if (!nostartfiles) {
ragge
1.24
454                         for (i = 0endfiles[i]; i++)
455                                 av[j++] = endfiles[i];
ragge
1.14
456                 }
ragge
1.3
457                 if(f20)
458                         av[j++] = "-l2";
459                 av[j++] = 0;
460                 eflag |= callsys("/bin/ld"av);
ragge
1.1
461                 if (nc==1 && nxo==1 && eflag==0)
ragge
1.3
462                         cunlink(setsuf(clist[0], 'o'));
ragge
1.1
463         }
464         dexit();
ragge
1.4
465         return 0;
ragge
1.1
466 }
467
ragge
1.4
468 void
469 idexit(int arg)
ragge
1.1
470 {
471         eflag = 100;
472         dexit();
473 }
474
ragge
1.4
475 void
ragge
1.3
476 dexit()
ragge
1.1
477 {
ragge
1.10
478         if (!pflag && !Xflag) {
ragge
1.1
479                 cunlink(tmp1);
480                 cunlink(tmp2);
481                 if (sflag==0)
482                         cunlink(tmp3);
483                 cunlink(tmp4);
484                 cunlink(tmp5);
ragge
1.3
485                 cunlink(tmp0);
ragge
1.1
486         }
487         exit(eflag);
488 }
489
ragge
1.4
490 void
491 error(char *schar *x)
ragge
1.1
492 {
ragge
1.8
493         fprintf(Eflag?stderr:stdout , sx);
494         putc('\n'Eflagstderr : stdout);
ragge
1.1
495         exfail++;
496         cflag++;
497         eflag++;
498 }
499
ragge
1.18
500 void
501 errorx(char *schar *xint eval)
502 {
503         error(sx);
504         exit(eval);
505 }
ragge
1.3
506
ragge
1.4
507 int
ragge
1.3
508 getsuf(as)
509 char as[];
ragge
1.1
510 {
511         register char *s;
512
ragge
1.4
513         if ((s = strrchr(as'.')) && s[1] != '\0' && s[2] == '\0')
514                 return s[1];
ragge
1.3
515         return(0);
ragge
1.1
516 }
517
518 char *
ragge
1.3
519 setsuf(asch)
520 char as[];
ragge
1.1
521 {
522         register char *s, *s1;
523
ragge
1.3
524         s = s1 = copy(as);
525         while(*s)
ragge
1.1
526                 if (*s++ == '/')
527                         s1 = s;
528         s[-1] = ch;
ragge
1.3
529         return(s1);
ragge
1.1
530 }
531
ragge
1.4
532 int
ragge
1.3
533 callsys(fv)
534 char f[], *v[]; {
ragge
1.1
535         int tstatus;
ragge
1.4
536         char *s;
ragge
1.5
537
538         if (vflag) {
539                 for (t = 0v[t]; t++)
ragge
1.6
540                         fprintf(stderr"%s "v[t]);
541                 fprintf(stderr"\n");
ragge
1.5
542         }
ragge
1.1
543
ragge
1.3
544         if ((t=fork())==0) {
ragge
1.1
545                 execv(fv);
ragge
1.4
546                 if ((s = strrchr(f'/')))
547                         execvp(s+1v);
ragge
1.1
548                 printf("Can't find %s\n"f);
ragge
1.3
549                 exit(100);
550         } else
551                 if (t == -1) {
552                         printf("Try again\n");
553                         return(100);
554                 }
555         while(t!=wait(&status));
ragge
1.1
556         if ((t=(status&0377)) != 0 && t!=14) {
ragge
1.3
557                 if (t!=2)               /* interrupt */
558                         {
ragge
1.1
559                         printf("Fatal error in %s\n"f);
560                         eflag = 8;
ragge
1.3
561                         }
ragge
1.1
562                 dexit();
563         }
ragge
1.3
564         return((status>>8) & 0377);
ragge
1.1
565 }
566
ragge
1.3
567 char *
568 copy(as)
569 char as[];
570 {
571         register char *otsp, *s;
572
573         otsp = tsp;
574         s = as;
ragge
1.4
575         while((*tsp++ = *s++));
ragge
1.3
576         if (tsp >tsa+CHSPACE)
577                 {
ragge
1.4
578                 tsp = tsa = calloc(CHSPACE+50,1);
579                 if (tsa== 0){
580                         error("no space for file names"0);
581                         eflag = 8;
582                         dexit();
ragge
1.3
583                         }
584                 }
585         return(otsp);
586 }
587
ragge
1.4
588 int
ragge
1.3
589 nodup(los)
590 char **l, *os;
ragge
1.1
591 {
592         register char *t, *s;
593         register int c;
594
595         s = os;
596         if (getsuf(s) != 'o')
ragge
1.3
597                 return(1);
ragge
1.4
598         while((t = *l++)) {
599                 while((c = *s++))
ragge
1.1
600                         if (c != *t++)
601                                 break;
ragge
1.3
602                 if (*t=='\0' && c=='\0')
603                         return(0);
ragge
1.1
604                 s = os;
605         }
ragge
1.3
606         return(1);
ragge
1.1
607 }
608
ragge
1.4
609 int
ragge
1.3
610 cunlink(f)
611 char *f;
ragge
1.1
612 {
ragge
1.13
613         if (f==0 || Xflag)
ragge
1.3
614                 return(0);
615         return(unlink(f));
ragge
1.1
616 }
FishEye: Open Source License registered to PCC.
Your maintenance has expired. You can renew your license at http://www.atlassian.com/fisheye/renew
Atlassian FishEye, CVS analysis. (Version:1.6.3 Build:build-336 2008-11-04) - Administration - Page generated 2014-10-02 16:29 +0200