Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20030807223614

Diff

Diff from 1.7 to:

Annotations

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

Annotated File View

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