Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20050102175443

Diff

Diff from 1.37 to:

Annotations

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

Annotated File View

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