Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:pj:20041220141437

Diff

Diff from 1.33 to:

Annotations

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

Annotated File View

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