Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20040614163324

Diff

Diff from 1.15 to:

Annotations

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

Annotated File View

ragge
1.15
1 /*      $Id: cc.c,v 1.15 2004/06/14 16:33:24 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                         }
321                 if(sflag)
322                         assource = tmp3 = setsuf(clist[i], 's');
323                 av[2] = tmp3;
324                 if (proflag) {
325                         av[3] = "-XP";
326                         av[4] = 0;
327                 } else
328                         av[3] = 0;
329                 if (gflag) {
330                         int i;
331                         i = av[3] ? 4 : 3;
332                         av[i++] = "-Xg";
333                         av[i] = 0;
ragge
1.1
334                 }
ragge
1.3
335                 if (callsys(pass0av)) {
ragge
1.1
336                         cflag++;
337                         eflag++;
338                         continue;
339                 }
340                 if (sflag)
341                         continue;
342         assemble:
ragge
1.3
343                 av[0] = "as";
344                 av[1] = "-o";
345                 av[2] = setsuf(clist[i], 'o');
ragge
1.7
346                 av[3] = xflag ? tmp4 : assource;
ragge
1.3
347                 if (dflag) {
348                         av[4] = alist;
349                         av[5] = 0;
350                 } else
351                         av[4] = 0;
352                 cunlink(tmp1);
353                 cunlink(tmp2);
354                 if (callsys("/bin/as"av) > 1) {
ragge
1.1
355                         cflag++;
356                         eflag++;
ragge
1.7
357                         cunlink(tmp4);
ragge
1.1
358                         continue;
359                 }
ragge
1.7
360                 cunlink(tmp4);
ragge
1.1
361         }
362 nocom:
363         if (cflag==0 && nl!=0) {
364                 i = 0;
ragge
1.6
365                 j = 0;
366                 av[j++] = "ld";
367                 av[j++] = "-X";
368                 av[j++] = "-d";
369                 av[j++] = "-e";
370                 av[j++] = "__start";
ragge
1.14
371                 if (Bstatic == 0) { /* Dynamic linkage */
372                         av[j++] = "-dynamic-linker";
373                         av[j++] = dynlinker;
374                 }
ragge
1.1
375                 if (outfile) {
ragge
1.3
376                         av[j++] = "-o";
377                         av[j++] = outfile;
378                 }
ragge
1.14
379                 if (!nostartfiles) {
380                         av[j++] = pref;
381                         av[j++] = "/usr/lib/crti.o";
382                         av[j++] = "/usr/lib/crtbegin.o";
383                 }
384                 while(i<nl) {
ragge
1.3
385                         av[j++] = llist[i++];
ragge
1.14
386                         if (j >= MAXAV)
387                                 error("Too many ld options"0);
388                 }
ragge
1.11
389 #if 0
ragge
1.3
390                 if (gflag)
391                         av[j++] = "-lg";
ragge
1.11
392 #endif
ragge
1.6
393                 av[j++] = "-lc";
ragge
1.14
394                 if (!nostartfiles) {
395                         av[j++] = "/usr/lib/crtend.o";
396                         av[j++] = "/usr/lib/crtn.o";
397                 }
ragge
1.3
398                 if(f20)
399                         av[j++] = "-l2";
400                 av[j++] = 0;
401                 eflag |= callsys("/bin/ld"av);
ragge
1.1
402                 if (nc==1 && nxo==1 && eflag==0)
ragge
1.3
403                         cunlink(setsuf(clist[0], 'o'));
ragge
1.1
404         }
405         dexit();
ragge
1.4
406         return 0;
ragge
1.1
407 }
408
ragge
1.4
409 void
410 idexit(int arg)
ragge
1.1
411 {
412         eflag = 100;
413         dexit();
414 }
415
ragge
1.4
416 void
ragge
1.3
417 dexit()
ragge
1.1
418 {
ragge
1.10
419         if (!pflag && !Xflag) {
ragge
1.1
420                 cunlink(tmp1);
421                 cunlink(tmp2);
422                 if (sflag==0)
423                         cunlink(tmp3);
424                 cunlink(tmp4);
425                 cunlink(tmp5);
ragge
1.3
426                 cunlink(tmp0);
ragge
1.1
427         }
428         exit(eflag);
429 }
430
ragge
1.4
431 void
432 error(char *schar *x)
ragge
1.1
433 {
ragge
1.8
434         fprintf(Eflag?stderr:stdout , sx);
435         putc('\n'Eflagstderr : stdout);
ragge
1.1
436         exfail++;
437         cflag++;
438         eflag++;
439 }
440
ragge
1.3
441
442
443
ragge
1.4
444 int
ragge
1.3
445 getsuf(as)
446 char as[];
ragge
1.1
447 {
448         register char *s;
449
ragge
1.4
450         if ((s = strrchr(as'.')) && s[1] != '\0' && s[2] == '\0')
451                 return s[1];
ragge
1.3
452         return(0);
ragge
1.1
453 }
454
455 char *
ragge
1.3
456 setsuf(asch)
457 char as[];
ragge
1.1
458 {
459         register char *s, *s1;
460
ragge
1.3
461         s = s1 = copy(as);
462         while(*s)
ragge
1.1
463                 if (*s++ == '/')
464                         s1 = s;
465         s[-1] = ch;
ragge
1.3
466         return(s1);
ragge
1.1
467 }
468
ragge
1.4
469 int
ragge
1.3
470 callsys(fv)
471 char f[], *v[]; {
ragge
1.1
472         int tstatus;
ragge
1.4
473         char *s;
ragge
1.5
474
475         if (vflag) {
476                 for (t = 0v[t]; t++)
ragge
1.6
477                         fprintf(stderr"%s "v[t]);
478                 fprintf(stderr"\n");
ragge
1.5
479         }
ragge
1.1
480
ragge
1.3
481         if ((t=fork())==0) {
ragge
1.1
482                 execv(fv);
ragge
1.4
483                 if ((s = strrchr(f'/')))
484                         execvp(s+1v);
ragge
1.1
485                 printf("Can't find %s\n"f);
ragge
1.3
486                 exit(100);
487         } else
488                 if (t == -1) {
489                         printf("Try again\n");
490                         return(100);
491                 }
492         while(t!=wait(&status));
ragge
1.1
493         if ((t=(status&0377)) != 0 && t!=14) {
ragge
1.3
494                 if (t!=2)               /* interrupt */
495                         {
ragge
1.1
496                         printf("Fatal error in %s\n"f);
497                         eflag = 8;
ragge
1.3
498                         }
ragge
1.1
499                 dexit();
500         }
ragge
1.3
501         return((status>>8) & 0377);
ragge
1.1
502 }
503
ragge
1.3
504 char *
505 copy(as)
506 char as[];
507 {
508         register char *otsp, *s;
509
510         otsp = tsp;
511         s = as;
ragge
1.4
512         while((*tsp++ = *s++));
ragge
1.3
513         if (tsp >tsa+CHSPACE)
514                 {
ragge
1.4
515                 tsp = tsa = calloc(CHSPACE+50,1);
516                 if (tsa== 0){
517                         error("no space for file names"0);
518                         eflag = 8;
519                         dexit();
ragge
1.3
520                         }
521                 }
522         return(otsp);
523 }
524
ragge
1.4
525 int
ragge
1.3
526 nodup(los)
527 char **l, *os;
ragge
1.1
528 {
529         register char *t, *s;
530         register int c;
531
532         s = os;
533         if (getsuf(s) != 'o')
ragge
1.3
534                 return(1);
ragge
1.4
535         while((t = *l++)) {
536                 while((c = *s++))
ragge
1.1
537                         if (c != *t++)
538                                 break;
ragge
1.3
539                 if (*t=='\0' && c=='\0')
540                         return(0);
ragge
1.1
541                 s = os;
542         }
ragge
1.3
543         return(1);
ragge
1.1
544 }
545
ragge
1.4
546 int
ragge
1.3
547 cunlink(f)
548 char *f;
ragge
1.1
549 {
ragge
1.13
550         if (f==0 || Xflag)
ragge
1.3
551                 return(0);
552         return(unlink(f));
ragge
1.1
553 }
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-30 14:03 +0100