Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20040612140703

Diff

Diff from 1.14 to:

Annotations

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

Annotated File View

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