Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20050515095500

Diff

Diff from 1.39 to:

Annotations

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

Annotated File View

ragge
1.39
1 /*      $Id: cc.c,v 1.39 2005/05/15 09:55:02 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>
pj
1.38
61 #include <errno.h>
ragge
1.24
62
ragge
1.39
63 #include "../../config.h"
64
ragge
1.24
65 #include "ccconfig.h"
ragge
1.3
66 /* C command */
ragge
1.1
67
ragge
1.20
68 #define MKS(x) _MKS(x)
69 #define _MKS(x) #x
70
71 /*
72  * Many specific definitions, should be declared elsewhere.
73  */
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':
ragge
1.39
243                         printf("%s\n"VERSSTR);
ragge
1.5
244                         vflag++;
245                         break;
ragge
1.14
246
247                 case 's':
248                         if (strcmp(argv[i], "-static") == 0)
249                                 Bstatic = 1;
250                         else
251                                 goto passa;
252                         break;
ragge
1.3
253                 } else {
254                 passa:
255                         t = argv[i];
ragge
1.11
256                         if (*argv[i] == '-' && argv[i][1] == 'L')
257                                 ;
258                         else if((c=getsuf(t))=='c' || c=='s'|| Eflag) {
ragge
1.3
259                                 clist[nc++] = t;
260                                 if (nc>=MAXFIL)
261                                         {
ragge
1.36
262                                         error("Too many source files");
ragge
1.3
263                                         exit(1);
264                                         }
265                                 t = setsuf(t'o');
266                         }
267                         if (nodup(llistt)) {
268                                 llist[nl++] = t;
269                                 if (nl >= MAXLIB)
270                                         {
ragge
1.36
271                                         error("Too many object/library files");
ragge
1.3
272                                         exit(1);
273                                         }
274                                 if (getsuf(t)=='o')
275                                         nxo++;
ragge
1.1
276                         }
277                 }
278         }
ragge
1.18
279         /* Sanity checking */
280         if (nc == 0 && nl == 0)
ragge
1.36
281                 errorx(8"no input files");
ragge
1.18
282         if (outfile && (cflag || sflag) && nc > 1)
ragge
1.36
283                 errorx(8"-o given with -c || -S and more than one file");
ragge
1.19
284         if (outfile && clist[0] && strcmp(outfileclist[0]) == 0)
ragge
1.36
285                 errorx(8"output file will be clobbered");
ragge
1.18
286
ragge
1.4
287         if (gflagOflag = 0;
ragge
1.24
288 #if 0
289         if (proflag)
ragge
1.3
290                 pref = "/lib/mcrt0.o";
ragge
1.24
291 #endif
ragge
1.3
292         if(nc==0)
293                 goto nocom;
294         if (pflag==0) {
pj
1.30
295                 tmp3 = gettmp();
296                 tmp4 = gettmp();
ragge
1.1
297         }
ragge
1.3
298         if (signal(SIGINTSIG_IGN) != SIG_IGN/* interrupt */
ragge
1.1
299                 signal(SIGINTidexit);
ragge
1.3
300         if (signal(SIGTERMSIG_IGN) != SIG_IGN)        /* terminate */
ragge
1.1
301                 signal(SIGTERMidexit);
ragge
1.3
302         pvt = pv;
ragge
1.1
303         for (i=0i<nci++) {
ragge
1.18
304                 /*
305                  * C preprocessor
306                  */
ragge
1.37
307                 if (nc>1 && !Eflag)
ragge
1.1
308                         printf("%s:\n"clist[i]);
pj
1.32
309                 assource = tmp3;
ragge
1.3
310                 if (getsuf(clist[i])=='s') {
ragge
1.1
311                         assource = clist[i];
ragge
1.7
312                         if (!xflag)
313                                 goto assemble;
pj
1.32
314                 }
ragge
1.1
315                 if (pflag)
316                         tmp4 = setsuf(clist[i], 'i');
ragge
1.5
317                 na = 0;
318                 av[na++] = "cpp";
ragge
1.20
319                 av[na++] = "-D__PCC__=" MKS(PCC_MAJOR);
320                 av[na++] = "-D__PCC_MINOR__" MKS(PCC_MINOR);
ragge
1.39
321                 av[na++] = "-D__PCC_MINORMINOR__" MKS(PCC_MINORMINOR);
ragge
1.20
322                 if (!nostdinc)
ragge
1.21
323                         av[na++] = "-S"av[na++] = STDINC;
ragge
1.20
324                 if (sysinc)
325                         av[na++] = "-S"av[na++] = sysinc;
ragge
1.5
326                 for (j = 0cppadd[j]; j++)
327                         av[na++] = cppadd[j];
ragge
1.24
328                 for (j = 0cppmdadd[j]; j++)
329                         av[na++] = cppmdadd[j];
ragge
1.7
330                 if (tflag)
ragge
1.20
331                         av[na++] = "-t";
ragge
1.7
332                 for(pv=ptemppv <pvtpv++)
333                         av[na++] = *pv;
ragge
1.5
334                 av[na++] = clist[i];
ragge
1.20
335                 if (!Eflag)
336                         av[na++] = tmp4;
ragge
1.3
337                 av[na++]=0;
338                 if (callsys(passpav))
339                         {exfail++; eflag++;}
ragge
1.11
340                 if (Eflag)
ragge
1.37
341                         continue;
ragge
1.7
342                 if (xflag)
343                         goto assemble;
ragge
1.18
344
345                 /*
346                  * C compiler
347                  */
ragge
1.25
348                 na = 0;
349                 av[na++]= "ccom";
350                 if (gflag)
351                         av[na++] = "-g";
352                 av[na++] = tmp4;
ragge
1.3
353                 if (pflag || exfail)
354                         {
ragge
1.1
355                         cflag++;
356                         continue;
ragge
1.3
357                         }
ragge
1.16
358                 if(sflag) {
359                         if (outfile)
360                                 assource = tmp3 = outfile;
361                         else
362                                 assource = tmp3 = setsuf(clist[i], 's');
363                 }
ragge
1.25
364                 av[na++] = tmp3;
365 #if 0
ragge
1.3
366                 if (proflag) {
367                         av[3] = "-XP";
368                         av[4] = 0;
369                 } else
370                         av[3] = 0;
ragge
1.25
371 #endif
372                 av[na++] = NULL;
ragge
1.3
373                 if (callsys(pass0av)) {
ragge
1.1
374                         cflag++;
375                         eflag++;
376                         continue;
377                 }
378                 if (sflag)
379                         continue;
ragge
1.18
380
381                 /*
382                  * Assembler
383                  */
ragge
1.1
384         assemble:
ragge
1.3
385                 av[0] = "as";
386                 av[1] = "-o";
ragge
1.18
387                 if (outfile && cflag)
388                         av[2] = outfile;
389                 else
390                         av[2] = setsuf(clist[i], 'o');
ragge
1.7
391                 av[3] = xflag ? tmp4 : assource;
ragge
1.3
392                 if (dflag) {
393                         av[4] = alist;
394                         av[5] = 0;
395                 } else
396                         av[4] = 0;
ragge
1.21
397                 if (callsys("/bin/as"av)) {
ragge
1.1
398                         cflag++;
399                         eflag++;
ragge
1.7
400                         cunlink(tmp4);
ragge
1.1
401                         continue;
402                 }
ragge
1.7
403                 cunlink(tmp4);
ragge
1.1
404         }
ragge
1.18
405
ragge
1.37
406         if (Eflag)
407                 dexit(eflag);
408
ragge
1.18
409         /*
410          * Linker
411          */
ragge
1.1
412 nocom:
413         if (cflag==0 && nl!=0) {
ragge
1.6
414                 j = 0;
415                 av[j++] = "ld";
416                 av[j++] = "-X";
417                 av[j++] = "-d";
418                 av[j++] = "-e";
419                 av[j++] = "__start";
ragge
1.14
420                 if (Bstatic == 0) { /* Dynamic linkage */
ragge
1.24
421                         for (i = 0dynlinker[i]; i++)
422                                 av[j++] = dynlinker[i];
ragge
1.14
423                 }
ragge
1.1
424                 if (outfile) {
ragge
1.3
425                         av[j++] = "-o";
426                         av[j++] = outfile;
427                 }
ragge
1.14
428                 if (!nostartfiles) {
ragge
1.24
429                         av[j++] = crt0file;
430                         for (i = 0startfiles[i]; i++)
431                                 av[j++] = startfiles[i];
ragge
1.14
432                 }
ragge
1.24
433                 i = 0;
ragge
1.14
434                 while(i<nl) {
ragge
1.3
435                         av[j++] = llist[i++];
ragge
1.14
436                         if (j >= MAXAV)
ragge
1.36
437                                 error("Too many ld options");
ragge
1.14
438                 }
ragge
1.11
439 #if 0
ragge
1.3
440                 if (gflag)
441                         av[j++] = "-lg";
ragge
1.11
442 #endif
ragge
1.6
443                 av[j++] = "-lc";
ragge
1.14
444                 if (!nostartfiles) {
ragge
1.24
445                         for (i = 0endfiles[i]; i++)
446                                 av[j++] = endfiles[i];
ragge
1.14
447                 }
ragge
1.3
448                 av[j++] = 0;
449                 eflag |= callsys("/bin/ld"av);
ragge
1.1
450                 if (nc==1 && nxo==1 && eflag==0)
ragge
1.3
451                         cunlink(setsuf(clist[0], 'o'));
ragge
1.1
452         }
ragge
1.36
453         dexit(eflag);
ragge
1.4
454         return 0;
ragge
1.1
455 }
456
ragge
1.36
457 /*
458  * exit and cleanup after interrupt.
459  */
ragge
1.4
460 void
461 idexit(int arg)
ragge
1.1
462 {
ragge
1.36
463         dexit(100);
ragge
1.1
464 }
465
ragge
1.36
466 /*
467  * exit and cleanup.
468  */
ragge
1.4
469 void
ragge
1.36
470 dexit(int eval)
ragge
1.1
471 {
ragge
1.10
472         if (!pflag && !Xflag) {
ragge
1.1
473                 if (sflag==0)
474                         cunlink(tmp3);
475                 cunlink(tmp4);
476         }
ragge
1.36
477         exit(eval);
ragge
1.1
478 }
479
ragge
1.36
480 static void
481 ccerror(char *sva_list ap)
ragge
1.1
482 {
ragge
1.36
483         vfprintf(Eflag ? stderr : stdoutsap);
ragge
1.8
484         putc('\n'Eflagstderr : stdout);
ragge
1.1
485         exfail++;
486         cflag++;
487         eflag++;
488 }
489
ragge
1.36
490 /*
491  * complain a bit.
492  */
493 void
494 error(char *s, ...)
495 {
496         va_list ap;
497
498         va_start(aps);
499         ccerror(sap);
500         va_end(ap);
501 }
502
503 /*
504  * complain a bit and then exit.
505  */
ragge
1.18
506 void
ragge
1.36
507 errorx(int evalchar *s, ...)
ragge
1.18
508 {
ragge
1.36
509         va_list ap;
510
511         va_start(aps);
512         ccerror(sap);
513         va_end(ap);
514         dexit(eval);
ragge
1.18
515 }
ragge
1.3
516
ragge
1.4
517 int
ragge
1.3
518 getsuf(as)
519 char as[];
ragge
1.1
520 {
521         register char *s;
522
ragge
1.4
523         if ((s = strrchr(as'.')) && s[1] != '\0' && s[2] == '\0')
524                 return s[1];
ragge
1.3
525         return(0);
ragge
1.1
526 }
527
ragge
1.36
528 /*
529  * Get basename of string s and change its suffix to ch.
530  */
ragge
1.1
531 char *
ragge
1.36
532 setsuf(char *schar ch)
ragge
1.1
533 {
ragge
1.36
534         s = copy(basename(s));
pj
1.31
535         s[strlen(s) - 1] = ch;
ragge
1.36
536         return(s);
ragge
1.1
537 }
538
ragge
1.4
539 int
ragge
1.3
540 callsys(fv)
541 char f[], *v[]; {
ragge
1.1
542         int tstatus;
ragge
1.4
543         char *s;
ragge
1.5
544
545         if (vflag) {
546                 for (t = 0v[t]; t++)
ragge
1.6
547                         fprintf(stderr"%s "v[t]);
548                 fprintf(stderr"\n");
ragge
1.5
549         }
ragge
1.1
550
ragge
1.3
551         if ((t=fork())==0) {
ragge
1.1
552                 execv(fv);
ragge
1.4
553                 if ((s = strrchr(f'/')))
554                         execvp(s+1v);
ragge
1.1
555                 printf("Can't find %s\n"f);
ragge
1.3
556                 exit(100);
557         } else
558                 if (t == -1) {
559                         printf("Try again\n");
560                         return(100);
561                 }
562         while(t!=wait(&status));
ragge
1.1
563         if ((t=(status&0377)) != 0 && t!=14) {
ragge
1.3
564                 if (t!=2)               /* interrupt */
ragge
1.36
565                         errorx(8"Fatal error in %s"f);
566                 dexit(eflag);
ragge
1.1
567         }
ragge
1.3
568         return((status>>8) & 0377);
ragge
1.1
569 }
570
ragge
1.3
571 char *
pj
1.28
572 copy(char *as)
ragge
1.3
573 {
pj
1.28
574         char *p;
575
ragge
1.36
576         if ((p = strdup(as)) == NULL)
577                 errorx(8"no space for file names");
ragge
1.3
578
pj
1.28
579         return p;
ragge
1.3
580 }
581
ragge
1.4
582 int
ragge
1.3
583 nodup(los)
584 char **l, *os;
ragge
1.1
585 {
586         register char *t, *s;
587         register int c;
588
589         s = os;
590         if (getsuf(s) != 'o')
ragge
1.3
591                 return(1);
ragge
1.4
592         while((t = *l++)) {
593                 while((c = *s++))
ragge
1.1
594                         if (c != *t++)
595                                 break;
ragge
1.3
596                 if (*t=='\0' && c=='\0')
597                         return(0);
ragge
1.1
598                 s = os;
599         }
ragge
1.3
600         return(1);
ragge
1.1
601 }
602
ragge
1.4
603 int
ragge
1.3
604 cunlink(f)
605 char *f;
ragge
1.1
606 {
ragge
1.13
607         if (f==0 || Xflag)
ragge
1.3
608                 return(0);
609         return(unlink(f));
ragge
1.1
610 }
pj
1.30
611
612 char *
613 gettmp()
614 {
615         char *sfn = strdup("/tmp/ctm.XXXXXX");
616         int fd = -1;
617
618         if ((fd = mkstemp(sfn)) == -1) {
619                 fprintf(stderr"%s: %s\n"sfnstrerror(errno));
620                 exit(8);
621         }
622         close(fd);
623
624         return sfn;
625 }
FishEye: Open Source License registered to PCC.
Atlassian FishEye, CVS analysis. (Version:1.6.3 Build:build-336 2008-11-04) - Administration - Page generated 2014-12-20 21:10 +0100