Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20040608210724

Diff

Diff from 1.12 to:

Annotations

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

Annotated File View

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