Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:pj:20041220141650

Diff

Diff from 1.35 to:

Annotations

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

Annotated File View

pj
1.35
1 /*      $Id: cc.c,v 1.35 2004/12/20 14:16:50 pj 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.18
38  *
39  * Brief description of its syntax:
40  * - Files that end with .c are passed via cpp->ccom->as->ld
41  * - Files that end with .s are passed as->ld
42  * - Files that end with .o are passed directly to ld
43  * - Multiple files may be given on the command line.
44  * - Unrecognized options are all sent directly to ld.
45  * -c or -S cannot be combined with -o if multiple files are given.
46  *
47  * This file should be rewritten readable.
ragge
1.1
48  */
ragge
1.9
49 #include <sys/types.h>
ragge
1.2
50
ragge
1.1
51 #include <stdio.h>
52 #include <ctype.h>
53 #include <signal.h>
ragge
1.4
54 #include <string.h>
55 #include <fcntl.h>
56 #include <unistd.h>
57 #include <stdlib.h>
58 #include <sys/wait.h>
ragge
1.24
59
60 #include "ccconfig.h"
ragge
1.3
61 /* C command */
ragge
1.1
62
ragge
1.20
63 #define MKS(x) _MKS(x)
64 #define _MKS(x) #x
65
66 /*
67  * Many specific definitions, should be declared elsewhere.
68  */
69 #define PCC_MINOR 0
70 #define PCC_MAJOR 1
71 #define STDINC    "/usr/include"
72
ragge
1.3
73 #define SBSIZE 10000
ragge
1.12
74 #define MAXINC 100
ragge
1.3
75 #define MAXFIL 100
ragge
1.12
76 #define MAXLIB 10000
ragge
1.14
77 #define MAXAV  10000
ragge
1.3
78 #define MAXOPT 100
79 char    *tmp3;
80 char    *tmp4;
ragge
1.1
81 char    *outfile;
pj
1.31
82 char *copy(char *as),*setsuf(char *aschar ch);
ragge
1.4
83 int getsuf(char []);
84 int main(intchar *[]);
85 void error(char *, char *);
ragge
1.18
86 void errorx(char *, char *, int);
ragge
1.4
87 int nodup(char **, char *);
88 int callsys(char [], char *[]);
89 int cunlink(char *);
90 void dexit(void);
91 void idexit(int);
pj
1.30
92 char *gettmp();
ragge
1.14
93 char    *av[MAXAV];
ragge
1.3
94 char    *clist[MAXFIL];
95 char    *llist[MAXLIB];
ragge
1.4
96 char    alist[20];
ragge
1.3
97 int dflag;
ragge
1.7
98 int     xflag;
ragge
1.3
99 int     pflag;
100 int     sflag;
101 int     cflag;
102 int     eflag;
103 int     gflag;
ragge
1.5
104 int     vflag;
ragge
1.7
105 int     tflag;
ragge
1.8
106 int     Eflag;
ragge
1.4
107 int     Oflag;
ragge
1.3
108 int     proflag;
ragge
1.1
109 int     exfail;
ragge
1.10
110 int     Xflag;
ragge
1.14
111 int     nostartfilesBstatic;
ragge
1.20
112 int     nostdinc;
ragge
1.14
113
ragge
1.23
114 char    *pass0 = LIBEXECDIR "/ccom";
115 char    *passp = LIBEXECDIR "/cpp";
ragge
1.20
116 char    *sysinc;
ragge
1.25
117 char *cppadd[] = CPPADD;
118 char *dynlinker[] = DYNLINKER;
119 char *crt0file = CRT0FILE;
120 char *startfiles[] = STARTFILES;
121 char *endfiles[] = ENDFILES;
122 char *cppmdadd[] = CPPMDADD;
ragge
1.1
123
ragge
1.4
124 int
pj
1.26
125 main(int argcchar *argv[])
126 {
ragge
1.15
127         char *t, *u;
ragge
1.1
128         char *assource;
ragge
1.3
129         char **pv, *ptemp[MAXOPT], **pvt;
ragge
1.18
130         int ncnlijcf20nxona;
ragge
1.3
131
132         i = nc = nl = f20 = nxo = 0;
133         pv = ptemp;
134         while(++i < argc) {
ragge
1.18
135                 if (argv[i][0] == '-')
136                 switch (argv[i][1]) {
ragge
1.3
137                 default:
138                         goto passa;
ragge
1.22
139
140                 case 'B'/* other search paths for binaries XXX support? */
141                         break;
142
ragge
1.10
143                 case 'X':
144                         Xflag++;
145                         break;
ragge
1.15
146                 case 'W'/* Ignore (most of) W-flags */
147                         if (strncmp(argv[i], "-Wl,"4) == 0) {
148                                 /* options to the linker */
149                                 t = &argv[i][4];
150                                 while ((u = strchr(t','))) {
151                                         *u++ = 0;
152                                         llist[nl++] = t;
153                                         t = u;
154                                 }
155                                 llist[nl++] = t;
156                         }
157                         break;
158
ragge
1.6
159                 case 'f'/* Ignore -ffreestanding */
160                         break;
ragge
1.14
161
ragge
1.25
162                 case 'g'/* create debug output */
163                         gflag++;
164                         break;
165
ragge
1.20
166                 case 'i':
167                         if (strcmp(argv[i], "-isystem") == 0) {
168                                 sysinc = argv[++i];
169                         } else
170                                 goto passa;
171                         break;
172
ragge
1.14
173                 case 'n'/* handle -n flags */
174                         if (strcmp(argv[i], "-nostdinc") == 0)
ragge
1.20
175                                 nostdinc++;
ragge
1.14
176                         else if (strcmp(argv[i], "-nostartfiles") == 0)
177                                 nostartfiles = 1;
178                         else
179                                 goto passa;
180                         break;
181
ragge
1.7
182                 case 'x':
183                         xflag++;
184                         i++; /* skip args */
185                         break;
186                 case 't':
187                         tflag++;
188                         break;
ragge
1.1
189                 case 'S':
190                         sflag++;
191                         cflag++;
ragge
1.3
192                         break;
ragge
1.1
193                 case 'o':
ragge
1.18
194                         if (outfile)
195                                 errorx("too many -o"""8);
196                         outfile = argv[++i];
ragge
1.3
197                         break;
ragge
1.1
198                 case 'O':
ragge
1.4
199                         Oflag++;
ragge
1.3
200                         break;
ragge
1.1
201                 case 'p':
202                         proflag++;
ragge
1.3
203                         break;
ragge
1.1
204                 case 'E':
ragge
1.8
205                         Eflag++;
206                         break;
ragge
1.1
207                 case 'P':
208                         pflag++;
ragge
1.3
209                         *pv++ = argv[i];
ragge
1.1
210                 case 'c':
211                         cflag++;
ragge
1.3
212                         break;
213
ragge
1.24
214 #if 0
ragge
1.3
215                 case '2':
216                         if(argv[i][2] == '\0')
217                                 pref = "/lib/crt2.o";
218                         else {
219                                 pref = "/lib/crt20.o";
220                                 f20 = 1;
221                         }
222                         break;
ragge
1.24
223 #endif
ragge
1.1
224                 case 'D':
225                 case 'I':
226                 case 'U':
227                 case 'C':
ragge
1.20
228                         *pv++ = argv[i];
ragge
1.3
229                         if (pv >= ptemp+MAXOPT)
230                                 {
231                                 error("Too many DIUC options"0);
232                                 --pv;
233                                 }
234                         break;
235
ragge
1.1
236                 case 'd':
ragge
1.3
237                         dflag++;
ragge
1.4
238                         strncpy(alistargv[i], 19);
ragge
1.3
239                         break;
ragge
1.5
240                 case 'v':
241                         vflag++;
242                         break;
ragge
1.14
243
244                 case 's':
245                         if (strcmp(argv[i], "-static") == 0)
246                                 Bstatic = 1;
247                         else
248                                 goto passa;
249                         break;
ragge
1.3
250                 } else {
251                 passa:
252                         t = argv[i];
ragge
1.11
253                         if (*argv[i] == '-' && argv[i][1] == 'L')
254                                 ;
255                         else if((c=getsuf(t))=='c' || c=='s'|| Eflag) {
ragge
1.3
256                                 clist[nc++] = t;
257                                 if (nc>=MAXFIL)
258                                         {
259                                         error("Too many source files",0);
260                                         exit(1);
261                                         }
262                                 t = setsuf(t'o');
263                         }
264                         if (nodup(llistt)) {
265                                 llist[nl++] = t;
266                                 if (nl >= MAXLIB)
267                                         {
268                                         error("Too many object/library files",0);
269                                         exit(1);
270                                         }
271                                 if (getsuf(t)=='o')
272                                         nxo++;
ragge
1.1
273                         }
274                 }
275         }
ragge
1.18
276         /* Sanity checking */
277         if (nc == 0 && nl == 0)
278                 errorx("no input files"""8);
279         if (outfile && (cflag || sflag) && nc > 1)
280                 errorx("-o given with -c || -S and more than one file"""8);
ragge
1.19
281         if (outfile && clist[0] && strcmp(outfileclist[0]) == 0)
ragge
1.18
282                 errorx("output file will be clobbered"""8);
283
ragge
1.4
284         if (gflagOflag = 0;
ragge
1.24
285 #if 0
286         if (proflag)
ragge
1.3
287                 pref = "/lib/mcrt0.o";
ragge
1.24
288 #endif
ragge
1.3
289         if(nc==0)
290                 goto nocom;
291         if (pflag==0) {
pj
1.30
292                 tmp3 = gettmp();
293                 tmp4 = gettmp();
ragge
1.1
294         }
ragge
1.3
295         if (signal(SIGINTSIG_IGN) != SIG_IGN/* interrupt */
ragge
1.1
296                 signal(SIGINTidexit);
ragge
1.3
297         if (signal(SIGTERMSIG_IGN) != SIG_IGN)        /* terminate */
ragge
1.1
298                 signal(SIGTERMidexit);
ragge
1.3
299         pvt = pv;
ragge
1.1
300         for (i=0i<nci++) {
ragge
1.18
301                 /*
302                  * C preprocessor
303                  */
ragge
1.3
304                 if (nc>1)
ragge
1.1
305                         printf("%s:\n"clist[i]);
pj
1.32
306                 assource = tmp3;
ragge
1.3
307                 if (getsuf(clist[i])=='s') {
ragge
1.1
308                         assource = clist[i];
ragge
1.7
309                         if (!xflag)
310                                 goto assemble;
pj
1.32
311                 }
ragge
1.1
312                 if (pflag)
313                         tmp4 = setsuf(clist[i], 'i');
ragge
1.5
314                 na = 0;
315                 av[na++] = "cpp";
ragge
1.20
316                 av[na++] = "-D__PCC__=" MKS(PCC_MAJOR);
317                 av[na++] = "-D__PCC_MINOR__" MKS(PCC_MINOR);
318                 if (!nostdinc)
ragge
1.21
319                         av[na++] = "-S"av[na++] = STDINC;
ragge
1.20
320                 if (sysinc)
321                         av[na++] = "-S"av[na++] = sysinc;
ragge
1.5
322                 for (j = 0cppadd[j]; j++)
323                         av[na++] = cppadd[j];
ragge
1.24
324                 for (j = 0cppmdadd[j]; j++)
325                         av[na++] = cppmdadd[j];
ragge
1.7
326                 if (tflag)
ragge
1.20
327                         av[na++] = "-t";
ragge
1.7
328                 for(pv=ptemppv <pvtpv++)
329                         av[na++] = *pv;
ragge
1.5
330                 av[na++] = clist[i];
ragge
1.20
331                 if (!Eflag)
332                         av[na++] = tmp4;
ragge
1.3
333                 av[na++]=0;
334                 if (callsys(passpav))
335                         {exfail++; eflag++;}
ragge
1.11
336                 if (Eflag)
337                         dexit();
ragge
1.7
338                 if (xflag)
339                         goto assemble;
ragge
1.18
340
341                 /*
342                  * C compiler
343                  */
ragge
1.25
344                 na = 0;
345                 av[na++]= "ccom";
346                 if (gflag)
347                         av[na++] = "-g";
348                 av[na++] = tmp4;
ragge
1.3
349                 if (pflag || exfail)
350                         {
ragge
1.1
351                         cflag++;
352                         continue;
ragge
1.3
353                         }
ragge
1.16
354                 if(sflag) {
355                         if (outfile)
356                                 assource = tmp3 = outfile;
357                         else
358                                 assource = tmp3 = setsuf(clist[i], 's');
359                 }
ragge
1.25
360                 av[na++] = tmp3;
361 #if 0
ragge
1.3
362                 if (proflag) {
363                         av[3] = "-XP";
364                         av[4] = 0;
365                 } else
366                         av[3] = 0;
ragge
1.25
367 #endif
368                 av[na++] = NULL;
ragge
1.3
369                 if (callsys(pass0av)) {
ragge
1.1
370                         cflag++;
371                         eflag++;
372                         continue;
373                 }
374                 if (sflag)
375                         continue;
ragge
1.18
376
377                 /*
378                  * Assembler
379                  */
ragge
1.1
380         assemble:
ragge
1.3
381                 av[0] = "as";
382                 av[1] = "-o";
ragge
1.18
383                 if (outfile && cflag)
384                         av[2] = outfile;
385                 else
386                         av[2] = setsuf(clist[i], 'o');
ragge
1.7
387                 av[3] = xflag ? tmp4 : assource;
ragge
1.3
388                 if (dflag) {
389                         av[4] = alist;
390                         av[5] = 0;
391                 } else
392                         av[4] = 0;
ragge
1.21
393                 if (callsys("/bin/as"av)) {
ragge
1.1
394                         cflag++;
395                         eflag++;
ragge
1.7
396                         cunlink(tmp4);
ragge
1.1
397                         continue;
398                 }
ragge
1.7
399                 cunlink(tmp4);
ragge
1.1
400         }
ragge
1.18
401
402         /*
403          * Linker
404          */
ragge
1.1
405 nocom:
406         if (cflag==0 && nl!=0) {
ragge
1.6
407                 j = 0;
408                 av[j++] = "ld";
409                 av[j++] = "-X";
410                 av[j++] = "-d";
411                 av[j++] = "-e";
412                 av[j++] = "__start";
ragge
1.14
413                 if (Bstatic == 0) { /* Dynamic linkage */
ragge
1.24
414                         for (i = 0dynlinker[i]; i++)
415                                 av[j++] = dynlinker[i];
ragge
1.14
416                 }
ragge
1.1
417                 if (outfile) {
ragge
1.3
418                         av[j++] = "-o";
419                         av[j++] = outfile;
420                 }
ragge
1.14
421                 if (!nostartfiles) {
ragge
1.24
422                         av[j++] = crt0file;
423                         for (i = 0startfiles[i]; i++)
424                                 av[j++] = startfiles[i];
ragge
1.14
425                 }
ragge
1.24
426                 i = 0;
ragge
1.14
427                 while(i<nl) {
ragge
1.3
428                         av[j++] = llist[i++];
ragge
1.14
429                         if (j >= MAXAV)
430                                 error("Too many ld options"0);
431                 }
ragge
1.11
432 #if 0
ragge
1.3
433                 if (gflag)
434                         av[j++] = "-lg";
ragge
1.11
435 #endif
ragge
1.6
436                 av[j++] = "-lc";
ragge
1.14
437                 if (!nostartfiles) {
ragge
1.24
438                         for (i = 0endfiles[i]; i++)
439                                 av[j++] = endfiles[i];
ragge
1.14
440                 }
ragge
1.3
441                 if(f20)
442                         av[j++] = "-l2";
443                 av[j++] = 0;
444                 eflag |= callsys("/bin/ld"av);
ragge
1.1
445                 if (nc==1 && nxo==1 && eflag==0)
ragge
1.3
446                         cunlink(setsuf(clist[0], 'o'));
ragge
1.1
447         }
448         dexit();
ragge
1.4
449         return 0;
ragge
1.1
450 }
451
ragge
1.4
452 void
453 idexit(int arg)
ragge
1.1
454 {
455         eflag = 100;
456         dexit();
457 }
458
ragge
1.4
459 void
ragge
1.3
460 dexit()
ragge
1.1
461 {
ragge
1.10
462         if (!pflag && !Xflag) {
ragge
1.1
463                 if (sflag==0)
464                         cunlink(tmp3);
465                 cunlink(tmp4);
466         }
467         exit(eflag);
468 }
469
ragge
1.4
470 void
471 error(char *schar *x)
ragge
1.1
472 {
ragge
1.8
473         fprintf(Eflag?stderr:stdout , sx);
474         putc('\n'Eflagstderr : stdout);
ragge
1.1
475         exfail++;
476         cflag++;
477         eflag++;
478 }
479
ragge
1.18
480 void
481 errorx(char *schar *xint eval)
482 {
483         error(sx);
484         exit(eval);
485 }
ragge
1.3
486
ragge
1.4
487 int
ragge
1.3
488 getsuf(as)
489 char as[];
ragge
1.1
490 {
491         register char *s;
492
ragge
1.4
493         if ((s = strrchr(as'.')) && s[1] != '\0' && s[2] == '\0')
494                 return s[1];
ragge
1.3
495         return(0);
ragge
1.1
496 }
497
498 char *
pj
1.31
499 setsuf(char *aschar ch)
ragge
1.1
500 {
pj
1.31
501         char *s, *s1;
ragge
1.1
502
pj
1.31
503         s = s1 = as
ragge
1.3
504         while(*s)
ragge
1.1
505                 if (*s++ == '/')
506                         s1 = s;
pj
1.31
507
508         s = copy(s1);
509         s[strlen(s) - 1] = ch;
ragge
1.3
510         return(s1);
ragge
1.1
511 }
512
ragge
1.4
513 int
ragge
1.3
514 callsys(fv)
515 char f[], *v[]; {
ragge
1.1
516         int tstatus;
ragge
1.4
517         char *s;
ragge
1.5
518
519         if (vflag) {
520                 for (t = 0v[t]; t++)
ragge
1.6
521                         fprintf(stderr"%s "v[t]);
522                 fprintf(stderr"\n");
ragge
1.5
523         }
ragge
1.1
524
ragge
1.3
525         if ((t=fork())==0) {
ragge
1.1
526                 execv(fv);
ragge
1.4
527                 if ((s = strrchr(f'/')))
528                         execvp(s+1v);
ragge
1.1
529                 printf("Can't find %s\n"f);
ragge
1.3
530                 exit(100);
531         } else
532                 if (t == -1) {
533                         printf("Try again\n");
534                         return(100);
535                 }
536         while(t!=wait(&status));
ragge
1.1
537         if ((t=(status&0377)) != 0 && t!=14) {
ragge
1.3
538                 if (t!=2)               /* interrupt */
539                         {
ragge
1.1
540                         printf("Fatal error in %s\n"f);
541                         eflag = 8;
ragge
1.3
542                         }
ragge
1.1
543                 dexit();
544         }
ragge
1.3
545         return((status>>8) & 0377);
ragge
1.1
546 }
547
ragge
1.3
548 char *
pj
1.28
549 copy(char *as)
ragge
1.3
550 {
pj
1.28
551         char *p;
552
553         if ((p = strdup(as)) == NULL) {
554                 error("no space for file names"0);
555                 eflag = 8;
556                 dexit();
557         }
ragge
1.3
558
pj
1.28
559         return p;
ragge
1.3
560 }
561
ragge
1.4
562 int
ragge
1.3
563 nodup(los)
564 char **l, *os;
ragge
1.1
565 {
566         register char *t, *s;
567         register int c;
568
569         s = os;
570         if (getsuf(s) != 'o')
ragge
1.3
571                 return(1);
ragge
1.4
572         while((t = *l++)) {
573                 while((c = *s++))
ragge
1.1
574                         if (c != *t++)
575                                 break;
ragge
1.3
576                 if (*t=='\0' && c=='\0')
577                         return(0);
ragge
1.1
578                 s = os;
579         }
ragge
1.3
580         return(1);
ragge
1.1
581 }
582
ragge
1.4
583 int
ragge
1.3
584 cunlink(f)
585 char *f;
ragge
1.1
586 {
ragge
1.13
587         if (f==0 || Xflag)
ragge
1.3
588                 return(0);
589         return(unlink(f));
ragge
1.1
590 }
pj
1.30
591
592 char *
593 gettmp()
594 {
595         char *sfn = strdup("/tmp/ctm.XXXXXX");
596         int fd = -1;
597
598         if ((fd = mkstemp(sfn)) == -1) {
599                 fprintf(stderr"%s: %s\n"sfnstrerror(errno));
600                 exit(8);
601         }
602         close(fd);
603
604         return sfn;
605 }
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-30 15:30 +0100