Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20040516153118

Diff

Diff from 1.10 to:

Annotations

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

Annotated File View

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