Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20040902212606

Diff

Diff from 1.21 to:

Annotations

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

Annotated File View

ragge
1.21
1 /*      $Id: cc.c,v 1.21 2004/09/02 21:26:06 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.10
155                 case 'X':
156                         Xflag++;
157                         break;
ragge
1.15
158                 case 'W'/* Ignore (most of) W-flags */
159                         if (strncmp(argv[i], "-Wl,"4) == 0) {
160                                 /* options to the linker */
161                                 t = &argv[i][4];
162                                 while ((u = strchr(t','))) {
163                                         *u++ = 0;
164                                         llist[nl++] = t;
165                                         t = u;
166                                 }
167                                 llist[nl++] = t;
168                         }
169                         break;
170
ragge
1.6
171                 case 'f'/* Ignore -ffreestanding */
172                         break;
ragge
1.14
173
ragge
1.20
174                 case 'i':
175                         if (strcmp(argv[i], "-isystem") == 0) {
176                                 sysinc = argv[++i];
177                         } else
178                                 goto passa;
179                         break;
180
ragge
1.14
181                 case 'n'/* handle -n flags */
182                         if (strcmp(argv[i], "-nostdinc") == 0)
ragge
1.20
183                                 nostdinc++;
ragge
1.14
184                         else if (strcmp(argv[i], "-nostartfiles") == 0)
185                                 nostartfiles = 1;
186                         else
187                                 goto passa;
188                         break;
189
ragge
1.7
190                 case 'x':
191                         xflag++;
192                         i++; /* skip args */
193                         break;
194                 case 't':
195                         tflag++;
196                         break;
ragge
1.1
197                 case 'S':
198                         sflag++;
199                         cflag++;
ragge
1.3
200                         break;
ragge
1.1
201                 case 'o':
ragge
1.18
202                         if (outfile)
203                                 errorx("too many -o"""8);
204                         outfile = argv[++i];
ragge
1.3
205                         break;
ragge
1.1
206                 case 'O':
ragge
1.4
207                         Oflag++;
ragge
1.3
208                         break;
ragge
1.1
209                 case 'p':
210                         proflag++;
ragge
1.3
211                         break;
ragge
1.1
212                 case 'g':
ragge
1.3
213                         gflag++;
214                         break;
ragge
1.1
215                 case 'E':
ragge
1.8
216                         Eflag++;
217                         break;
ragge
1.1
218                 case 'P':
219                         pflag++;
ragge
1.3
220                         *pv++ = argv[i];
ragge
1.1
221                 case 'c':
222                         cflag++;
ragge
1.3
223                         break;
224
225                 case '2':
226                         if(argv[i][2] == '\0')
227                                 pref = "/lib/crt2.o";
228                         else {
229                                 pref = "/lib/crt20.o";
230                                 f20 = 1;
231                         }
232                         break;
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.3
294         if (noflflag)
295                 pref = proflag ? "/lib/fmcrt0.o" : "/lib/fcrt0.o";
296         else if (proflag)
297                 pref = "/lib/mcrt0.o";
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.7
344                 if (tflag)
ragge
1.20
345                         av[na++] = "-t";
ragge
1.7
346                 for(pv=ptemppv <pvtpv++)
347                         av[na++] = *pv;
ragge
1.5
348                 av[na++] = clist[i];
ragge
1.20
349                 if (!Eflag)
350                         av[na++] = tmp4;
ragge
1.3
351                 av[na++]=0;
352                 if (callsys(passpav))
353                         {exfail++; eflag++;}
ragge
1.11
354                 if (Eflag)
355                         dexit();
ragge
1.7
356                 if (xflag)
357                         goto assemble;
ragge
1.18
358
359                 /*
360                  * C compiler
361                  */
ragge
1.5
362                 av[0]= "ccom";
ragge
1.18
363                 av[1] = tmp4;
ragge
1.3
364                 tsp = savetsp;
365                 if (pflag || exfail)
366                         {
ragge
1.1
367                         cflag++;
368                         continue;
ragge
1.3
369                         }
ragge
1.16
370                 if(sflag) {
371                         if (outfile)
372                                 assource = tmp3 = outfile;
373                         else
374                                 assource = tmp3 = setsuf(clist[i], 's');
375                 }
ragge
1.3
376                 av[2] = tmp3;
377                 if (proflag) {
378                         av[3] = "-XP";
379                         av[4] = 0;
380                 } else
381                         av[3] = 0;
382                 if (gflag) {
383                         int i;
384                         i = av[3] ? 4 : 3;
385                         av[i++] = "-Xg";
386                         av[i] = 0;
ragge
1.1
387                 }
ragge
1.3
388                 if (callsys(pass0av)) {
ragge
1.1
389                         cflag++;
390                         eflag++;
391                         continue;
392                 }
393                 if (sflag)
394                         continue;
ragge
1.18
395
396                 /*
397                  * Assembler
398                  */
ragge
1.1
399         assemble:
ragge
1.3
400                 av[0] = "as";
401                 av[1] = "-o";
ragge
1.18
402                 if (outfile && cflag)
403                         av[2] = outfile;
404                 else
405                         av[2] = setsuf(clist[i], 'o');
ragge
1.7
406                 av[3] = xflag ? tmp4 : assource;
ragge
1.3
407                 if (dflag) {
408                         av[4] = alist;
409                         av[5] = 0;
410                 } else
411                         av[4] = 0;
412                 cunlink(tmp1);
413                 cunlink(tmp2);
ragge
1.21
414                 if (callsys("/bin/as"av)) {
ragge
1.1
415                         cflag++;
416                         eflag++;
ragge
1.7
417                         cunlink(tmp4);
ragge
1.1
418                         continue;
419                 }
ragge
1.7
420                 cunlink(tmp4);
ragge
1.1
421         }
ragge
1.18
422
423         /*
424          * Linker
425          */
ragge
1.1
426 nocom:
427         if (cflag==0 && nl!=0) {
428                 i = 0;
ragge
1.6
429                 j = 0;
430                 av[j++] = "ld";
431                 av[j++] = "-X";
432                 av[j++] = "-d";
433                 av[j++] = "-e";
434                 av[j++] = "__start";
ragge
1.14
435                 if (Bstatic == 0) { /* Dynamic linkage */
436                         av[j++] = "-dynamic-linker";
437                         av[j++] = dynlinker;
438                 }
ragge
1.1
439                 if (outfile) {
ragge
1.3
440                         av[j++] = "-o";
441                         av[j++] = outfile;
442                 }
ragge
1.14
443                 if (!nostartfiles) {
444                         av[j++] = pref;
445                         av[j++] = "/usr/lib/crti.o";
446                         av[j++] = "/usr/lib/crtbegin.o";
447                 }
448                 while(i<nl) {
ragge
1.3
449                         av[j++] = llist[i++];
ragge
1.14
450                         if (j >= MAXAV)
451                                 error("Too many ld options"0);
452                 }
ragge
1.11
453 #if 0
ragge
1.3
454                 if (gflag)
455                         av[j++] = "-lg";
ragge
1.11
456 #endif
ragge
1.6
457                 av[j++] = "-lc";
ragge
1.14
458                 if (!nostartfiles) {
459                         av[j++] = "/usr/lib/crtend.o";
460                         av[j++] = "/usr/lib/crtn.o";
461                 }
ragge
1.3
462                 if(f20)
463                         av[j++] = "-l2";
464                 av[j++] = 0;
465                 eflag |= callsys("/bin/ld"av);
ragge
1.1
466                 if (nc==1 && nxo==1 && eflag==0)
ragge
1.3
467                         cunlink(setsuf(clist[0], 'o'));
ragge
1.1
468         }
469         dexit();
ragge
1.4
470         return 0;
ragge
1.1
471 }
472
ragge
1.4
473 void
474 idexit(int arg)
ragge
1.1
475 {
476         eflag = 100;
477         dexit();
478 }
479
ragge
1.4
480 void
ragge
1.3
481 dexit()
ragge
1.1
482 {
ragge
1.10
483         if (!pflag && !Xflag) {
ragge
1.1
484                 cunlink(tmp1);
485                 cunlink(tmp2);
486                 if (sflag==0)
487                         cunlink(tmp3);
488                 cunlink(tmp4);
489                 cunlink(tmp5);
ragge
1.3
490                 cunlink(tmp0);
ragge
1.1
491         }
492         exit(eflag);
493 }
494
ragge
1.4
495 void
496 error(char *schar *x)
ragge
1.1
497 {
ragge
1.8
498         fprintf(Eflag?stderr:stdout , sx);
499         putc('\n'Eflagstderr : stdout);
ragge
1.1
500         exfail++;
501         cflag++;
502         eflag++;
503 }
504
ragge
1.18
505 void
506 errorx(char *schar *xint eval)
507 {
508         error(sx);
509         exit(eval);
510 }
ragge
1.3
511
ragge
1.4
512 int
ragge
1.3
513 getsuf(as)
514 char as[];
ragge
1.1
515 {
516         register char *s;
517
ragge
1.4
518         if ((s = strrchr(as'.')) && s[1] != '\0' && s[2] == '\0')
519                 return s[1];
ragge
1.3
520         return(0);
ragge
1.1
521 }
522
523 char *
ragge
1.3
524 setsuf(asch)
525 char as[];
ragge
1.1
526 {
527         register char *s, *s1;
528
ragge
1.3
529         s = s1 = copy(as);
530         while(*s)
ragge
1.1
531                 if (*s++ == '/')
532                         s1 = s;
533         s[-1] = ch;
ragge
1.3
534         return(s1);
ragge
1.1
535 }
536
ragge
1.4
537 int
ragge
1.3
538 callsys(fv)
539 char f[], *v[]; {
ragge
1.1
540         int tstatus;
ragge
1.4
541         char *s;
ragge
1.5
542
543         if (vflag) {
544                 for (t = 0v[t]; t++)
ragge
1.6
545                         fprintf(stderr"%s "v[t]);
546                 fprintf(stderr"\n");
ragge
1.5
547         }
ragge
1.1
548
ragge
1.3
549         if ((t=fork())==0) {
ragge
1.1
550                 execv(fv);
ragge
1.4
551                 if ((s = strrchr(f'/')))
552                         execvp(s+1v);
ragge
1.1
553                 printf("Can't find %s\n"f);
ragge
1.3
554                 exit(100);
555         } else
556                 if (t == -1) {
557                         printf("Try again\n");
558                         return(100);
559                 }
560         while(t!=wait(&status));
ragge
1.1
561         if ((t=(status&0377)) != 0 && t!=14) {
ragge
1.3
562                 if (t!=2)               /* interrupt */
563                         {
ragge
1.1
564                         printf("Fatal error in %s\n"f);
565                         eflag = 8;
ragge
1.3
566                         }
ragge
1.1
567                 dexit();
568         }
ragge
1.3
569         return((status>>8) & 0377);
ragge
1.1
570 }
571
ragge
1.3
572 char *
573 copy(as)
574 char as[];
575 {
576         register char *otsp, *s;
577
578         otsp = tsp;
579         s = as;
ragge
1.4
580         while((*tsp++ = *s++));
ragge
1.3
581         if (tsp >tsa+CHSPACE)
582                 {
ragge
1.4
583                 tsp = tsa = calloc(CHSPACE+50,1);
584                 if (tsa== 0){
585                         error("no space for file names"0);
586                         eflag = 8;
587                         dexit();
ragge
1.3
588                         }
589                 }
590         return(otsp);
591 }
592
ragge
1.4
593 int
ragge
1.3
594 nodup(los)
595 char **l, *os;
ragge
1.1
596 {
597         register char *t, *s;
598         register int c;
599
600         s = os;
601         if (getsuf(s) != 'o')
ragge
1.3
602                 return(1);
ragge
1.4
603         while((t = *l++)) {
604                 while((c = *s++))
ragge
1.1
605                         if (c != *t++)
606                                 break;
ragge
1.3
607                 if (*t=='\0' && c=='\0')
608                         return(0);
ragge
1.1
609                 s = os;
610         }
ragge
1.3
611         return(1);
ragge
1.1
612 }
613
ragge
1.4
614 int
ragge
1.3
615 cunlink(f)
616 char *f;
ragge
1.1
617 {
ragge
1.13
618         if (f==0 || Xflag)
ragge
1.3
619                 return(0);
620         return(unlink(f));
ragge
1.1
621 }
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-08-21 18:01 +0200