Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:pj:20041219211644

Diff

Diff from 1.29 to:

Annotations

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

Annotated File View

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