Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20040904102208

Diff

Diff from 1.22 to:

Annotations

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

Annotated File View

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