Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:pj:20041220141251

Diff

Diff from 1.32 to:

Annotations

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

Annotated File View

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