Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:pj:20041220141545

Diff

Diff from 1.34 to:

Annotations

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

Annotated File View

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