Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:pj:20041219211513

Diff

Diff from 1.28 to:

Annotations

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

Annotated File View

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