Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20040515155230

Diff

Diff from 1.9 to:

Annotations

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

Annotated File View

ragge
1.9
1 /*      $Id: cc.c,v 1.9 2004/05/15 15:52:30 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)",
56         "-Amachine(i386)""-D__i386__""-D__OPTIMIZE__""-Di386",
ragge
1.9
57         "-nostdinc""-I/usr/include"NULL,
ragge
1.5
58 };
59 #endif
60
ragge
1.3
61 #define SBSIZE 10000
62 #define MAXINC 10
63 #define MAXFIL 100
64 #define MAXLIB 100
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;
86 char    *av[50];
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.4
104 char    *pass0 = "/lib/ccom";
ragge
1.5
105 char    *passp = "/usr/libexec/cpp0";
ragge
1.4
106 char    *pref = "/usr/lib/crt0.o";
ragge
1.1
107
ragge
1.4
108 int
ragge
1.3
109 main(argcargv)
110 char *argv[]; {
ragge
1.1
111         char *t;
ragge
1.3
112         char *savetsp;
ragge
1.1
113         char *assource;
ragge
1.3
114         char **pv, *ptemp[MAXOPT], **pvt;
ragge
1.4
115         int ncnlijcf20nxonatt;
116         FILE *f;
ragge
1.3
117
118         i = nc = nl = f20 = nxo = 0;
119         pv = ptemp;
120         while(++i < argc) {
121                 if(*argv[i] == '-'switch (argv[i][1]) {
122                 default:
123                         goto passa;
ragge
1.6
124                 case 'W'/* Ignore W-flags */
125                 case 'f'/* Ignore -ffreestanding */
126                 case 'n'/* Ignore -nostdinc */
127                         break;
ragge
1.7
128                 case 'x':
129                         xflag++;
130                         i++; /* skip args */
131                         break;
132                 case 't':
133                         tflag++;
134                         break;
ragge
1.1
135                 case 'S':
136                         sflag++;
137                         cflag++;
ragge
1.3
138                         break;
ragge
1.1
139                 case 'o':
140                         if (++i < argc) {
141                                 outfile = argv[i];
ragge
1.4
142                                 if ((tt=getsuf(outfile))=='c'||tt=='o') {
ragge
1.3
143                                         error("Would overwrite %s"outfile);
ragge
1.1
144                                         exit(8);
145                                 }
146                         }
ragge
1.3
147                         break;
ragge
1.1
148                 case 'O':
ragge
1.4
149                         Oflag++;
ragge
1.3
150                         break;
ragge
1.1
151                 case 'p':
152                         proflag++;
ragge
1.3
153                         break;
ragge
1.1
154                 case 'g':
ragge
1.3
155                         gflag++;
156                         break;
ragge
1.1
157                 case 'E':
ragge
1.8
158                         Eflag++;
159                         break;
ragge
1.1
160                 case 'P':
161                         pflag++;
ragge
1.3
162                         *pv++ = argv[i];
ragge
1.1
163                 case 'c':
164                         cflag++;
ragge
1.3
165                         break;
166
167                 case '2':
168                         if(argv[i][2] == '\0')
169                                 pref = "/lib/crt2.o";
170                         else {
171                                 pref = "/lib/crt20.o";
172                                 f20 = 1;
173                         }
174                         break;
ragge
1.1
175                 case 'D':
176                 case 'I':
177                 case 'U':
178                 case 'C':
ragge
1.3
179                         *pv++ = argv[i];
180                         if (pv >= ptemp+MAXOPT)
181                                 {
182                                 error("Too many DIUC options"0);
183                                 --pv;
184                                 }
185                         break;
186
ragge
1.1
187                 case 'd':
ragge
1.3
188                         dflag++;
ragge
1.4
189                         strncpy(alistargv[i], 19);
ragge
1.3
190                         break;
ragge
1.5
191                 case 'v':
192                         vflag++;
193                         break;
ragge
1.3
194                 } else {
195                 passa:
196                         t = argv[i];
ragge
1.8
197                         if((c=getsuf(t))=='c' || c=='s'|| Eflag) {
ragge
1.3
198                                 clist[nc++] = t;
199                                 if (nc>=MAXFIL)
200                                         {
201                                         error("Too many source files",0);
202                                         exit(1);
203                                         }
204                                 t = setsuf(t'o');
205                         }
206                         if (nodup(llistt)) {
207                                 llist[nl++] = t;
208                                 if (nl >= MAXLIB)
209                                         {
210                                         error("Too many object/library files",0);
211                                         exit(1);
212                                         }
213                                 if (getsuf(t)=='o')
214                                         nxo++;
ragge
1.1
215                         }
216                 }
217         }
ragge
1.4
218         if (gflagOflag = 0;
ragge
1.3
219         if (noflflag)
220                 pref = proflag ? "/lib/fmcrt0.o" : "/lib/fcrt0.o";
221         else if (proflag)
222                 pref = "/lib/mcrt0.o";
223         if(nc==0)
224                 goto nocom;
225         if (pflag==0) {
226                 tmp0 = copy("/tmp/ctm0a");
ragge
1.4
227                 while((f=fopen(tmp0"r")) != NULL) {
228                         fclose(f);
ragge
1.3
229                         tmp0[9]++;
ragge
1.1
230                 }
ragge
1.3
231                 while((creat(tmp00400))<0)
232                         tmp0[9]++;
ragge
1.1
233         }
ragge
1.3
234         if (signal(SIGINTSIG_IGN) != SIG_IGN/* interrupt */
ragge
1.1
235                 signal(SIGINTidexit);
ragge
1.3
236         if (signal(SIGTERMSIG_IGN) != SIG_IGN)        /* terminate */
ragge
1.1
237                 signal(SIGTERMidexit);
ragge
1.3
238         (tmp1 = copy(tmp0))[8] = '1';
239         (tmp2 = copy(tmp0))[8] = '2';
240         (tmp3 = copy(tmp0))[8] = '3';
ragge
1.1
241         if (pflag==0)
ragge
1.3
242                 (tmp4 = copy(tmp0))[8] = '4';
243         pvt = pv;
ragge
1.1
244         for (i=0i<nci++) {
ragge
1.3
245                 if (nc>1)
ragge
1.1
246                         printf("%s:\n"clist[i]);
ragge
1.3
247                 if (getsuf(clist[i])=='s') {
ragge
1.1
248                         assource = clist[i];
ragge
1.7
249                         if (!xflag)
250                                 goto assemble;
ragge
1.1
251                 } else
252                         assource = tmp3;
253                 if (pflag)
254                         tmp4 = setsuf(clist[i], 'i');
ragge
1.3
255                 savetsp = tsp;
ragge
1.5
256                 na = 0;
257                 av[na++] = "cpp";
ragge
1.7
258                 av[na++] = xflag ? "-lang-asm" : "-lang-c";
259                 av[na++] = "-$";
ragge
1.5
260                 for (j = 0cppadd[j]; j++)
261                         av[na++] = cppadd[j];
ragge
1.7
262                 if (tflag)
263                         av[na++] = "-traditional";
264                 for(pv=ptemppv <pvtpv++)
265                         av[na++] = *pv;
ragge
1.5
266                 av[na++] = clist[i];
ragge
1.8
267                 av[na++] = Eflag ? "-" : tmp4;
ragge
1.3
268                 av[na++]=0;
269                 if (callsys(passpav))
270                         {exfail++; eflag++;}
ragge
1.7
271                 if (xflag)
272                         goto assemble;
ragge
1.5
273                 av[0]= "ccom";
ragge
1.3
274                 av[1] =tmp4;
275                 tsp = savetsp;
276                 if (pflag || exfail)
277                         {
ragge
1.1
278                         cflag++;
279                         continue;
ragge
1.3
280                         }
281                 if(sflag)
282                         assource = tmp3 = setsuf(clist[i], 's');
283                 av[2] = tmp3;
284                 if (proflag) {
285                         av[3] = "-XP";
286                         av[4] = 0;
287                 } else
288                         av[3] = 0;
289                 if (gflag) {
290                         int i;
291                         i = av[3] ? 4 : 3;
292                         av[i++] = "-Xg";
293                         av[i] = 0;
ragge
1.1
294                 }
ragge
1.3
295                 if (callsys(pass0av)) {
ragge
1.1
296                         cflag++;
297                         eflag++;
298                         continue;
299                 }
300                 if (sflag)
301                         continue;
302         assemble:
ragge
1.3
303                 av[0] = "as";
304                 av[1] = "-o";
305                 av[2] = setsuf(clist[i], 'o');
ragge
1.7
306                 av[3] = xflag ? tmp4 : assource;
ragge
1.3
307                 if (dflag) {
308                         av[4] = alist;
309                         av[5] = 0;
310                 } else
311                         av[4] = 0;
312                 cunlink(tmp1);
313                 cunlink(tmp2);
314                 if (callsys("/bin/as"av) > 1) {
ragge
1.1
315                         cflag++;
316                         eflag++;
ragge
1.7
317                         cunlink(tmp4);
ragge
1.1
318                         continue;
319                 }
ragge
1.7
320                 cunlink(tmp4);
ragge
1.1
321         }
322 nocom:
323         if (cflag==0 && nl!=0) {
324                 i = 0;
ragge
1.6
325                 j = 0;
326                 av[j++] = "ld";
327                 av[j++] = "-X";
328                 av[j++] = "-d";
329                 av[j++] = "-e";
330                 av[j++] = "__start";
331                 av[j++] = "-static";
ragge
1.1
332                 if (outfile) {
ragge
1.3
333                         av[j++] = "-o";
334                         av[j++] = outfile;
335                 }
ragge
1.6
336                 av[j++] = pref;
337                 av[j++] = "/usr/lib/crti.o";
338                 av[j++] = "/usr/lib/crtbegin.o";
ragge
1.3
339                 while(i<nl)
340                         av[j++] = llist[i++];
341                 if (gflag)
342                         av[j++] = "-lg";
ragge
1.6
343                 av[j++] = "-lc";
344                 av[j++] = "/usr/lib/crtend.o";
345                 av[j++] = "/usr/lib/crtn.o";
ragge
1.3
346                 if(f20)
347                         av[j++] = "-l2";
348                 av[j++] = 0;
349                 eflag |= callsys("/bin/ld"av);
ragge
1.1
350                 if (nc==1 && nxo==1 && eflag==0)
ragge
1.3
351                         cunlink(setsuf(clist[0], 'o'));
ragge
1.1
352         }
353         dexit();
ragge
1.4
354         return 0;
ragge
1.1
355 }
356
ragge
1.4
357 void
358 idexit(int arg)
ragge
1.1
359 {
360         eflag = 100;
361         dexit();
362 }
363
ragge
1.4
364 void
ragge
1.3
365 dexit()
ragge
1.1
366 {
367         if (!pflag) {
368                 cunlink(tmp1);
369                 cunlink(tmp2);
370                 if (sflag==0)
371                         cunlink(tmp3);
372                 cunlink(tmp4);
373                 cunlink(tmp5);
ragge
1.3
374                 cunlink(tmp0);
ragge
1.1
375         }
376         exit(eflag);
377 }
378
ragge
1.4
379 void
380 error(char *schar *x)
ragge
1.1
381 {
ragge
1.8
382         fprintf(Eflag?stderr:stdout , sx);
383         putc('\n'Eflagstderr : stdout);
ragge
1.1
384         exfail++;
385         cflag++;
386         eflag++;
387 }
388
ragge
1.3
389
390
391
ragge
1.4
392 int
ragge
1.3
393 getsuf(as)
394 char as[];
ragge
1.1
395 {
396         register char *s;
397
ragge
1.4
398         if ((s = strrchr(as'.')) && s[1] != '\0' && s[2] == '\0')
399                 return s[1];
ragge
1.3
400         return(0);
ragge
1.1
401 }
402
403 char *
ragge
1.3
404 setsuf(asch)
405 char as[];
ragge
1.1
406 {
407         register char *s, *s1;
408
ragge
1.3
409         s = s1 = copy(as);
410         while(*s)
ragge
1.1
411                 if (*s++ == '/')
412                         s1 = s;
413         s[-1] = ch;
ragge
1.3
414         return(s1);
ragge
1.1
415 }
416
ragge
1.4
417 int
ragge
1.3
418 callsys(fv)
419 char f[], *v[]; {
ragge
1.1
420         int tstatus;
ragge
1.4
421         char *s;
ragge
1.5
422
423         if (vflag) {
424                 for (t = 0v[t]; t++)
ragge
1.6
425                         fprintf(stderr"%s "v[t]);
426                 fprintf(stderr"\n");
ragge
1.5
427         }
ragge
1.1
428
ragge
1.3
429         if ((t=fork())==0) {
ragge
1.1
430                 execv(fv);
ragge
1.4
431                 if ((s = strrchr(f'/')))
432                         execvp(s+1v);
ragge
1.1
433                 printf("Can't find %s\n"f);
ragge
1.3
434                 exit(100);
435         } else
436                 if (t == -1) {
437                         printf("Try again\n");
438                         return(100);
439                 }
440         while(t!=wait(&status));
ragge
1.1
441         if ((t=(status&0377)) != 0 && t!=14) {
ragge
1.3
442                 if (t!=2)               /* interrupt */
443                         {
ragge
1.1
444                         printf("Fatal error in %s\n"f);
445                         eflag = 8;
ragge
1.3
446                         }
ragge
1.1
447                 dexit();
448         }
ragge
1.3
449         return((status>>8) & 0377);
ragge
1.1
450 }
451
ragge
1.3
452 char *
453 copy(as)
454 char as[];
455 {
456         register char *otsp, *s;
457
458         otsp = tsp;
459         s = as;
ragge
1.4
460         while((*tsp++ = *s++));
ragge
1.3
461         if (tsp >tsa+CHSPACE)
462                 {
ragge
1.4
463                 tsp = tsa = calloc(CHSPACE+50,1);
464                 if (tsa== 0){
465                         error("no space for file names"0);
466                         eflag = 8;
467                         dexit();
ragge
1.3
468                         }
469                 }
470         return(otsp);
471 }
472
ragge
1.4
473 int
ragge
1.3
474 nodup(los)
475 char **l, *os;
ragge
1.1
476 {
477         register char *t, *s;
478         register int c;
479
480         s = os;
481         if (getsuf(s) != 'o')
ragge
1.3
482                 return(1);
ragge
1.4
483         while((t = *l++)) {
484                 while((c = *s++))
ragge
1.1
485                         if (c != *t++)
486                                 break;
ragge
1.3
487                 if (*t=='\0' && c=='\0')
488                         return(0);
ragge
1.1
489                 s = os;
490         }
ragge
1.3
491         return(1);
ragge
1.1
492 }
493
ragge
1.4
494 int
ragge
1.3
495 cunlink(f)
496 char *f;
ragge
1.1
497 {
ragge
1.3
498         if (f==0)
499                 return(0);
500         return(unlink(f));
ragge
1.1
501 }
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 05:05 +0100