Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20040621081946

Diff

Diff from 1.16 to:

Annotations

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

Annotated File View

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