Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20040621183026

Diff

Diff from 1.17 to:

Annotations

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

Annotated File View

ragge
1.17
1 /*      $Id: cc.c,v 1.17 2004/06/21 18:30:26 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.17
170                                 if ((tt=getsuf(outfile))=='c'||
171                                     (tt=='o' && !cflag)) {
ragge
1.3
172                                         error("Would overwrite %s"outfile);
ragge
1.1
173                                         exit(8);
174                                 }
175                         }
ragge
1.3
176                         break;
ragge
1.1
177                 case 'O':
ragge
1.4
178                         Oflag++;
ragge
1.3
179                         break;
ragge
1.1
180                 case 'p':
181                         proflag++;
ragge
1.3
182                         break;
ragge
1.1
183                 case 'g':
ragge
1.3
184                         gflag++;
185                         break;
ragge
1.1
186                 case 'E':
ragge
1.8
187                         Eflag++;
188                         break;
ragge
1.1
189                 case 'P':
190                         pflag++;
ragge
1.3
191                         *pv++ = argv[i];
ragge
1.1
192                 case 'c':
193                         cflag++;
ragge
1.3
194                         break;
195
196                 case '2':
197                         if(argv[i][2] == '\0')
198                                 pref = "/lib/crt2.o";
199                         else {
200                                 pref = "/lib/crt20.o";
201                                 f20 = 1;
202                         }
203                         break;
ragge
1.1
204                 case 'D':
205                 case 'I':
206                 case 'U':
207                 case 'C':
ragge
1.14
208 diuc:                   *pv++ = argv[i];
ragge
1.3
209                         if (pv >= ptemp+MAXOPT)
210                                 {
211                                 error("Too many DIUC options"0);
212                                 --pv;
213                                 }
214                         break;
215
ragge
1.1
216                 case 'd':
ragge
1.3
217                         dflag++;
ragge
1.4
218                         strncpy(alistargv[i], 19);
ragge
1.3
219                         break;
ragge
1.5
220                 case 'v':
221                         vflag++;
222                         break;
ragge
1.14
223
224                 case 's':
225                         if (strcmp(argv[i], "-static") == 0)
226                                 Bstatic = 1;
227                         else
228                                 goto passa;
229                         break;
230
ragge
1.3
231                 } else {
232                 passa:
233                         t = argv[i];
ragge
1.11
234                         if (*argv[i] == '-' && argv[i][1] == 'L')
235                                 ;
236                         else if((c=getsuf(t))=='c' || c=='s'|| Eflag) {
ragge
1.3
237                                 clist[nc++] = t;
238                                 if (nc>=MAXFIL)
239                                         {
240                                         error("Too many source files",0);
241                                         exit(1);
242                                         }
243                                 t = setsuf(t'o');
244                         }
245                         if (nodup(llistt)) {
246                                 llist[nl++] = t;
247                                 if (nl >= MAXLIB)
248                                         {
249                                         error("Too many object/library files",0);
250                                         exit(1);
251                                         }
252                                 if (getsuf(t)=='o')
253                                         nxo++;
ragge
1.1
254                         }
255                 }
256         }
ragge
1.4
257         if (gflagOflag = 0;
ragge
1.3
258         if (noflflag)
259                 pref = proflag ? "/lib/fmcrt0.o" : "/lib/fcrt0.o";
260         else if (proflag)
261                 pref = "/lib/mcrt0.o";
262         if(nc==0)
263                 goto nocom;
264         if (pflag==0) {
265                 tmp0 = copy("/tmp/ctm0a");
ragge
1.4
266                 while((f=fopen(tmp0"r")) != NULL) {
267                         fclose(f);
ragge
1.3
268                         tmp0[9]++;
ragge
1.1
269                 }
ragge
1.3
270                 while((creat(tmp00400))<0)
271                         tmp0[9]++;
ragge
1.1
272         }
ragge
1.3
273         if (signal(SIGINTSIG_IGN) != SIG_IGN/* interrupt */
ragge
1.1
274                 signal(SIGINTidexit);
ragge
1.3
275         if (signal(SIGTERMSIG_IGN) != SIG_IGN)        /* terminate */
ragge
1.1
276                 signal(SIGTERMidexit);
ragge
1.3
277         (tmp1 = copy(tmp0))[8] = '1';
278         (tmp2 = copy(tmp0))[8] = '2';
279         (tmp3 = copy(tmp0))[8] = '3';
ragge
1.1
280         if (pflag==0)
ragge
1.3
281                 (tmp4 = copy(tmp0))[8] = '4';
282         pvt = pv;
ragge
1.1
283         for (i=0i<nci++) {
ragge
1.3
284                 if (nc>1)
ragge
1.1
285                         printf("%s:\n"clist[i]);
ragge
1.3
286                 if (getsuf(clist[i])=='s') {
ragge
1.1
287                         assource = clist[i];
ragge
1.7
288                         if (!xflag)
289                                 goto assemble;
ragge
1.1
290                 } else
291                         assource = tmp3;
292                 if (pflag)
293                         tmp4 = setsuf(clist[i], 'i');
ragge
1.3
294                 savetsp = tsp;
ragge
1.5
295                 na = 0;
296                 av[na++] = "cpp";
ragge
1.7
297                 av[na++] = xflag ? "-lang-asm" : "-lang-c";
298                 av[na++] = "-$";
ragge
1.5
299                 for (j = 0cppadd[j]; j++)
300                         av[na++] = cppadd[j];
ragge
1.7
301                 if (tflag)
302                         av[na++] = "-traditional";
303                 for(pv=ptemppv <pvtpv++)
304                         av[na++] = *pv;
ragge
1.5
305                 av[na++] = clist[i];
ragge
1.8
306                 av[na++] = Eflag ? "-" : tmp4;
ragge
1.3
307                 av[na++]=0;
308                 if (callsys(passpav))
309                         {exfail++; eflag++;}
ragge
1.11
310                 if (Eflag)
311                         dexit();
ragge
1.7
312                 if (xflag)
313                         goto assemble;
ragge
1.5
314                 av[0]= "ccom";
ragge
1.3
315                 av[1] =tmp4;
316                 tsp = savetsp;
317                 if (pflag || exfail)
318                         {
ragge
1.1
319                         cflag++;
320                         continue;
ragge
1.3
321                         }
ragge
1.16
322                 if(sflag) {
323                         if (outfile)
324                                 assource = tmp3 = outfile;
325                         else
326                                 assource = tmp3 = setsuf(clist[i], 's');
327                 }
ragge
1.3
328                 av[2] = tmp3;
329                 if (proflag) {
330                         av[3] = "-XP";
331                         av[4] = 0;
332                 } else
333                         av[3] = 0;
334                 if (gflag) {
335                         int i;
336                         i = av[3] ? 4 : 3;
337                         av[i++] = "-Xg";
338                         av[i] = 0;
ragge
1.1
339                 }
ragge
1.3
340                 if (callsys(pass0av)) {
ragge
1.1
341                         cflag++;
342                         eflag++;
343                         continue;
344                 }
345                 if (sflag)
346                         continue;
347         assemble:
ragge
1.3
348                 av[0] = "as";
349                 av[1] = "-o";
350                 av[2] = setsuf(clist[i], 'o');
ragge
1.7
351                 av[3] = xflag ? tmp4 : assource;
ragge
1.3
352                 if (dflag) {
353                         av[4] = alist;
354                         av[5] = 0;
355                 } else
356                         av[4] = 0;
357                 cunlink(tmp1);
358                 cunlink(tmp2);
359                 if (callsys("/bin/as"av) > 1) {
ragge
1.1
360                         cflag++;
361                         eflag++;
ragge
1.7
362                         cunlink(tmp4);
ragge
1.1
363                         continue;
364                 }
ragge
1.7
365                 cunlink(tmp4);
ragge
1.1
366         }
367 nocom:
368         if (cflag==0 && nl!=0) {
369                 i = 0;
ragge
1.6
370                 j = 0;
371                 av[j++] = "ld";
372                 av[j++] = "-X";
373                 av[j++] = "-d";
374                 av[j++] = "-e";
375                 av[j++] = "__start";
ragge
1.14
376                 if (Bstatic == 0) { /* Dynamic linkage */
377                         av[j++] = "-dynamic-linker";
378                         av[j++] = dynlinker;
379                 }
ragge
1.1
380                 if (outfile) {
ragge
1.3
381                         av[j++] = "-o";
382                         av[j++] = outfile;
383                 }
ragge
1.14
384                 if (!nostartfiles) {
385                         av[j++] = pref;
386                         av[j++] = "/usr/lib/crti.o";
387                         av[j++] = "/usr/lib/crtbegin.o";
388                 }
389                 while(i<nl) {
ragge
1.3
390                         av[j++] = llist[i++];
ragge
1.14
391                         if (j >= MAXAV)
392                                 error("Too many ld options"0);
393                 }
ragge
1.11
394 #if 0
ragge
1.3
395                 if (gflag)
396                         av[j++] = "-lg";
ragge
1.11
397 #endif
ragge
1.6
398                 av[j++] = "-lc";
ragge
1.14
399                 if (!nostartfiles) {
400                         av[j++] = "/usr/lib/crtend.o";
401                         av[j++] = "/usr/lib/crtn.o";
402                 }
ragge
1.3
403                 if(f20)
404                         av[j++] = "-l2";
405                 av[j++] = 0;
406                 eflag |= callsys("/bin/ld"av);
ragge
1.1
407                 if (nc==1 && nxo==1 && eflag==0)
ragge
1.3
408                         cunlink(setsuf(clist[0], 'o'));
ragge
1.1
409         }
410         dexit();
ragge
1.4
411         return 0;
ragge
1.1
412 }
413
ragge
1.4
414 void
415 idexit(int arg)
ragge
1.1
416 {
417         eflag = 100;
418         dexit();
419 }
420
ragge
1.4
421 void
ragge
1.3
422 dexit()
ragge
1.1
423 {
ragge
1.10
424         if (!pflag && !Xflag) {
ragge
1.1
425                 cunlink(tmp1);
426                 cunlink(tmp2);
427                 if (sflag==0)
428                         cunlink(tmp3);
429                 cunlink(tmp4);
430                 cunlink(tmp5);
ragge
1.3
431                 cunlink(tmp0);
ragge
1.1
432         }
433         exit(eflag);
434 }
435
ragge
1.4
436 void
437 error(char *schar *x)
ragge
1.1
438 {
ragge
1.8
439         fprintf(Eflag?stderr:stdout , sx);
440         putc('\n'Eflagstderr : stdout);
ragge
1.1
441         exfail++;
442         cflag++;
443         eflag++;
444 }
445
ragge
1.3
446
447
448
ragge
1.4
449 int
ragge
1.3
450 getsuf(as)
451 char as[];
ragge
1.1
452 {
453         register char *s;
454
ragge
1.4
455         if ((s = strrchr(as'.')) && s[1] != '\0' && s[2] == '\0')
456                 return s[1];
ragge
1.3
457         return(0);
ragge
1.1
458 }
459
460 char *
ragge
1.3
461 setsuf(asch)
462 char as[];
ragge
1.1
463 {
464         register char *s, *s1;
465
ragge
1.3
466         s = s1 = copy(as);
467         while(*s)
ragge
1.1
468                 if (*s++ == '/')
469                         s1 = s;
470         s[-1] = ch;
ragge
1.3
471         return(s1);
ragge
1.1
472 }
473
ragge
1.4
474 int
ragge
1.3
475 callsys(fv)
476 char f[], *v[]; {
ragge
1.1
477         int tstatus;
ragge
1.4
478         char *s;
ragge
1.5
479
480         if (vflag) {
481                 for (t = 0v[t]; t++)
ragge
1.6
482                         fprintf(stderr"%s "v[t]);
483                 fprintf(stderr"\n");
ragge
1.5
484         }
ragge
1.1
485
ragge
1.3
486         if ((t=fork())==0) {
ragge
1.1
487                 execv(fv);
ragge
1.4
488                 if ((s = strrchr(f'/')))
489                         execvp(s+1v);
ragge
1.1
490                 printf("Can't find %s\n"f);
ragge
1.3
491                 exit(100);
492         } else
493                 if (t == -1) {
494                         printf("Try again\n");
495                         return(100);
496                 }
497         while(t!=wait(&status));
ragge
1.1
498         if ((t=(status&0377)) != 0 && t!=14) {
ragge
1.3
499                 if (t!=2)               /* interrupt */
500                         {
ragge
1.1
501                         printf("Fatal error in %s\n"f);
502                         eflag = 8;
ragge
1.3
503                         }
ragge
1.1
504                 dexit();
505         }
ragge
1.3
506         return((status>>8) & 0377);
ragge
1.1
507 }
508
ragge
1.3
509 char *
510 copy(as)
511 char as[];
512 {
513         register char *otsp, *s;
514
515         otsp = tsp;
516         s = as;
ragge
1.4
517         while((*tsp++ = *s++));
ragge
1.3
518         if (tsp >tsa+CHSPACE)
519                 {
ragge
1.4
520                 tsp = tsa = calloc(CHSPACE+50,1);
521                 if (tsa== 0){
522                         error("no space for file names"0);
523                         eflag = 8;
524                         dexit();
ragge
1.3
525                         }
526                 }
527         return(otsp);
528 }
529
ragge
1.4
530 int
ragge
1.3
531 nodup(los)
532 char **l, *os;
ragge
1.1
533 {
534         register char *t, *s;
535         register int c;
536
537         s = os;
538         if (getsuf(s) != 'o')
ragge
1.3
539                 return(1);
ragge
1.4
540         while((t = *l++)) {
541                 while((c = *s++))
ragge
1.1
542                         if (c != *t++)
543                                 break;
ragge
1.3
544                 if (*t=='\0' && c=='\0')
545                         return(0);
ragge
1.1
546                 s = os;
547         }
ragge
1.3
548         return(1);
ragge
1.1
549 }
550
ragge
1.4
551 int
ragge
1.3
552 cunlink(f)
553 char *f;
ragge
1.1
554 {
ragge
1.13
555         if (f==0 || Xflag)
ragge
1.3
556                 return(0);
557         return(unlink(f));
ragge
1.1
558 }
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-01 13:26 +0200