Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:pj:20050219134402

Diff

Diff from 1.38 to:

Annotations

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

Annotated File View

pj
1.38
1 /*      $Id: cc.c,v 1.38 2005/02/19 13:44:02 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.36
50 #include <sys/wait.h>
ragge
1.2
51
ragge
1.1
52 #include <stdio.h>
53 #include <ctype.h>
54 #include <signal.h>
ragge
1.4
55 #include <string.h>
56 #include <fcntl.h>
57 #include <unistd.h>
58 #include <stdlib.h>
ragge
1.36
59 #include <stdarg.h>
60 #include <libgen.h>
pj
1.38
61 #include <errno.h>
ragge
1.24
62
63 #include "ccconfig.h"
ragge
1.3
64 /* C command */
ragge
1.1
65
ragge
1.20
66 #define MKS(x) _MKS(x)
67 #define _MKS(x) #x
68
69 /*
70  * Many specific definitions, should be declared elsewhere.
71  */
72 #define PCC_MINOR 0
73 #define PCC_MAJOR 1
74 #define STDINC    "/usr/include"
75
ragge
1.3
76 #define SBSIZE 10000
ragge
1.12
77 #define MAXINC 100
ragge
1.3
78 #define MAXFIL 100
ragge
1.12
79 #define MAXLIB 10000
ragge
1.14
80 #define MAXAV  10000
ragge
1.3
81 #define MAXOPT 100
82 char    *tmp3;
83 char    *tmp4;
ragge
1.1
84 char    *outfile;
pj
1.31
85 char *copy(char *as),*setsuf(char *aschar ch);
ragge
1.4
86 int getsuf(char []);
87 int main(intchar *[]);
ragge
1.36
88 void error(char *, ...);
89 void errorx(int evalchar *, ...);
ragge
1.4
90 int nodup(char **, char *);
91 int callsys(char [], char *[]);
92 int cunlink(char *);
ragge
1.36
93 void dexit(int eval);
ragge
1.4
94 void idexit(int);
pj
1.30
95 char *gettmp();
ragge
1.14
96 char    *av[MAXAV];
ragge
1.3
97 char    *clist[MAXFIL];
98 char    *llist[MAXLIB];
ragge
1.4
99 char    alist[20];
ragge
1.3
100 int dflag;
ragge
1.7
101 int     xflag;
ragge
1.3
102 int     pflag;
103 int     sflag;
104 int     cflag;
105 int     eflag;
106 int     gflag;
ragge
1.5
107 int     vflag;
ragge
1.7
108 int     tflag;
ragge
1.8
109 int     Eflag;
ragge
1.4
110 int     Oflag;
ragge
1.3
111 int     proflag;
ragge
1.1
112 int     exfail;
ragge
1.10
113 int     Xflag;
ragge
1.14
114 int     nostartfilesBstatic;
ragge
1.20
115 int     nostdinc;
ragge
1.14
116
ragge
1.23
117 char    *pass0 = LIBEXECDIR "/ccom";
118 char    *passp = LIBEXECDIR "/cpp";
ragge
1.20
119 char    *sysinc;
ragge
1.25
120 char *cppadd[] = CPPADD;
121 char *dynlinker[] = DYNLINKER;
122 char *crt0file = CRT0FILE;
123 char *startfiles[] = STARTFILES;
124 char *endfiles[] = ENDFILES;
125 char *cppmdadd[] = CPPMDADD;
ragge
1.1
126
ragge
1.4
127 int
pj
1.26
128 main(int argcchar *argv[])
129 {
ragge
1.15
130         char *t, *u;
ragge
1.1
131         char *assource;
ragge
1.3
132         char **pv, *ptemp[MAXOPT], **pvt;
ragge
1.36
133         int ncnlijcnxona;
ragge
1.3
134
ragge
1.36
135         i = nc = nl = nxo = 0;
ragge
1.3
136         pv = ptemp;
137         while(++i < argc) {
ragge
1.18
138                 if (argv[i][0] == '-')
139                 switch (argv[i][1]) {
ragge
1.3
140                 default:
141                         goto passa;
ragge
1.22
142
143                 case 'B'/* other search paths for binaries XXX support? */
144                         break;
145
ragge
1.10
146                 case 'X':
147                         Xflag++;
148                         break;
ragge
1.15
149                 case 'W'/* Ignore (most of) W-flags */
150                         if (strncmp(argv[i], "-Wl,"4) == 0) {
151                                 /* options to the linker */
152                                 t = &argv[i][4];
153                                 while ((u = strchr(t','))) {
154                                         *u++ = 0;
155                                         llist[nl++] = t;
156                                         t = u;
157                                 }
158                                 llist[nl++] = t;
159                         }
160                         break;
161
ragge
1.6
162                 case 'f'/* Ignore -ffreestanding */
163                         break;
ragge
1.14
164
ragge
1.25
165                 case 'g'/* create debug output */
166                         gflag++;
167                         break;
168
ragge
1.20
169                 case 'i':
170                         if (strcmp(argv[i], "-isystem") == 0) {
171                                 sysinc = argv[++i];
172                         } else
173                                 goto passa;
174                         break;
175
ragge
1.14
176                 case 'n'/* handle -n flags */
177                         if (strcmp(argv[i], "-nostdinc") == 0)
ragge
1.20
178                                 nostdinc++;
ragge
1.14
179                         else if (strcmp(argv[i], "-nostartfiles") == 0)
180                                 nostartfiles = 1;
181                         else
182                                 goto passa;
183                         break;
184
ragge
1.7
185                 case 'x':
186                         xflag++;
187                         i++; /* skip args */
188                         break;
189                 case 't':
190                         tflag++;
191                         break;
ragge
1.1
192                 case 'S':
193                         sflag++;
194                         cflag++;
ragge
1.3
195                         break;
ragge
1.1
196                 case 'o':
ragge
1.18
197                         if (outfile)
ragge
1.36
198                                 errorx(8"too many -o");
ragge
1.18
199                         outfile = argv[++i];
ragge
1.3
200                         break;
ragge
1.1
201                 case 'O':
ragge
1.4
202                         Oflag++;
ragge
1.3
203                         break;
ragge
1.1
204                 case 'p':
205                         proflag++;
ragge
1.3
206                         break;
ragge
1.1
207                 case 'E':
ragge
1.8
208                         Eflag++;
209                         break;
ragge
1.1
210                 case 'P':
211                         pflag++;
ragge
1.3
212                         *pv++ = argv[i];
ragge
1.1
213                 case 'c':
214                         cflag++;
ragge
1.3
215                         break;
216
ragge
1.24
217 #if 0
ragge
1.3
218                 case '2':
219                         if(argv[i][2] == '\0')
220                                 pref = "/lib/crt2.o";
221                         else {
222                                 pref = "/lib/crt20.o";
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                                 {
ragge
1.36
233                                 error("Too many DIUC options");
ragge
1.3
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                                         {
ragge
1.36
261                                         error("Too many source files");
ragge
1.3
262                                         exit(1);
263                                         }
264                                 t = setsuf(t'o');
265                         }
266                         if (nodup(llistt)) {
267                                 llist[nl++] = t;
268                                 if (nl >= MAXLIB)
269                                         {
ragge
1.36
270                                         error("Too many object/library files");
ragge
1.3
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)
ragge
1.36
280                 errorx(8"no input files");
ragge
1.18
281         if (outfile && (cflag || sflag) && nc > 1)
ragge
1.36
282                 errorx(8"-o given with -c || -S and more than one file");
ragge
1.19
283         if (outfile && clist[0] && strcmp(outfileclist[0]) == 0)
ragge
1.36
284                 errorx(8"output file will be clobbered");
ragge
1.18
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.37
306                 if (nc>1 && !Eflag)
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)
ragge
1.37
339                         continue;
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
ragge
1.37
404         if (Eflag)
405                 dexit(eflag);
406
ragge
1.18
407         /*
408          * Linker
409          */
ragge
1.1
410 nocom:
411         if (cflag==0 && nl!=0) {
ragge
1.6
412                 j = 0;
413                 av[j++] = "ld";
414                 av[j++] = "-X";
415                 av[j++] = "-d";
416                 av[j++] = "-e";
417                 av[j++] = "__start";
ragge
1.14
418                 if (Bstatic == 0) { /* Dynamic linkage */
ragge
1.24
419                         for (i = 0dynlinker[i]; i++)
420                                 av[j++] = dynlinker[i];
ragge
1.14
421                 }
ragge
1.1
422                 if (outfile) {
ragge
1.3
423                         av[j++] = "-o";
424                         av[j++] = outfile;
425                 }
ragge
1.14
426                 if (!nostartfiles) {
ragge
1.24
427                         av[j++] = crt0file;
428                         for (i = 0startfiles[i]; i++)
429                                 av[j++] = startfiles[i];
ragge
1.14
430                 }
ragge
1.24
431                 i = 0;
ragge
1.14
432                 while(i<nl) {
ragge
1.3
433                         av[j++] = llist[i++];
ragge
1.14
434                         if (j >= MAXAV)
ragge
1.36
435                                 error("Too many ld options");
ragge
1.14
436                 }
ragge
1.11
437 #if 0
ragge
1.3
438                 if (gflag)
439                         av[j++] = "-lg";
ragge
1.11
440 #endif
ragge
1.6
441                 av[j++] = "-lc";
ragge
1.14
442                 if (!nostartfiles) {
ragge
1.24
443                         for (i = 0endfiles[i]; i++)
444                                 av[j++] = endfiles[i];
ragge
1.14
445                 }
ragge
1.3
446                 av[j++] = 0;
447                 eflag |= callsys("/bin/ld"av);
ragge
1.1
448                 if (nc==1 && nxo==1 && eflag==0)
ragge
1.3
449                         cunlink(setsuf(clist[0], 'o'));
ragge
1.1
450         }
ragge
1.36
451         dexit(eflag);
ragge
1.4
452         return 0;
ragge
1.1
453 }
454
ragge
1.36
455 /*
456  * exit and cleanup after interrupt.
457  */
ragge
1.4
458 void
459 idexit(int arg)
ragge
1.1
460 {
ragge
1.36
461         dexit(100);
ragge
1.1
462 }
463
ragge
1.36
464 /*
465  * exit and cleanup.
466  */
ragge
1.4
467 void
ragge
1.36
468 dexit(int eval)
ragge
1.1
469 {
ragge
1.10
470         if (!pflag && !Xflag) {
ragge
1.1
471                 if (sflag==0)
472                         cunlink(tmp3);
473                 cunlink(tmp4);
474         }
ragge
1.36
475         exit(eval);
ragge
1.1
476 }
477
ragge
1.36
478 static void
479 ccerror(char *sva_list ap)
ragge
1.1
480 {
ragge
1.36
481         vfprintf(Eflag ? stderr : stdoutsap);
ragge
1.8
482         putc('\n'Eflagstderr : stdout);
ragge
1.1
483         exfail++;
484         cflag++;
485         eflag++;
486 }
487
ragge
1.36
488 /*
489  * complain a bit.
490  */
491 void
492 error(char *s, ...)
493 {
494         va_list ap;
495
496         va_start(aps);
497         ccerror(sap);
498         va_end(ap);
499 }
500
501 /*
502  * complain a bit and then exit.
503  */
ragge
1.18
504 void
ragge
1.36
505 errorx(int evalchar *s, ...)
ragge
1.18
506 {
ragge
1.36
507         va_list ap;
508
509         va_start(aps);
510         ccerror(sap);
511         va_end(ap);
512         dexit(eval);
ragge
1.18
513 }
ragge
1.3
514
ragge
1.4
515 int
ragge
1.3
516 getsuf(as)
517 char as[];
ragge
1.1
518 {
519         register char *s;
520
ragge
1.4
521         if ((s = strrchr(as'.')) && s[1] != '\0' && s[2] == '\0')
522                 return s[1];
ragge
1.3
523         return(0);
ragge
1.1
524 }
525
ragge
1.36
526 /*
527  * Get basename of string s and change its suffix to ch.
528  */
ragge
1.1
529 char *
ragge
1.36
530 setsuf(char *schar ch)
ragge
1.1
531 {
ragge
1.36
532         s = copy(basename(s));
pj
1.31
533         s[strlen(s) - 1] = ch;
ragge
1.36
534         return(s);
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 */
ragge
1.36
563                         errorx(8"Fatal error in %s"f);
564                 dexit(eflag);
ragge
1.1
565         }
ragge
1.3
566         return((status>>8) & 0377);
ragge
1.1
567 }
568
ragge
1.3
569 char *
pj
1.28
570 copy(char *as)
ragge
1.3
571 {
pj
1.28
572         char *p;
573
ragge
1.36
574         if ((p = strdup(as)) == NULL)
575                 errorx(8"no space for file names");
ragge
1.3
576
pj
1.28
577         return p;
ragge
1.3
578 }
579
ragge
1.4
580 int
ragge
1.3
581 nodup(los)
582 char **l, *os;
ragge
1.1
583 {
584         register char *t, *s;
585         register int c;
586
587         s = os;
588         if (getsuf(s) != 'o')
ragge
1.3
589                 return(1);
ragge
1.4
590         while((t = *l++)) {
591                 while((c = *s++))
ragge
1.1
592                         if (c != *t++)
593                                 break;
ragge
1.3
594                 if (*t=='\0' && c=='\0')
595                         return(0);
ragge
1.1
596                 s = os;
597         }
ragge
1.3
598         return(1);
ragge
1.1
599 }
600
ragge
1.4
601 int
ragge
1.3
602 cunlink(f)
603 char *f;
ragge
1.1
604 {
ragge
1.13
605         if (f==0 || Xflag)
ragge
1.3
606                 return(0);
607         return(unlink(f));
ragge
1.1
608 }
pj
1.30
609
610 char *
611 gettmp()
612 {
613         char *sfn = strdup("/tmp/ctm.XXXXXX");
614         int fd = -1;
615
616         if ((fd = mkstemp(sfn)) == -1) {
617                 fprintf(stderr"%s: %s\n"sfnstrerror(errno));
618                 exit(8);
619         }
620         close(fd);
621
622         return sfn;
623 }
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-02 18:43 +0200