Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20040622121245

Diff

Diff from 1.18 to:

Annotations

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

Annotated File View

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