Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20041230152313

Diff

Diff from 1.36 to:

Annotations

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

Annotated File View

ragge
1.36
1 /*      $Id: cc.c,v 1.36 2004/12/30 15:23:13 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.3
305                 if (nc>1)
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.36
338                         dexit(eflag);
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
403         /*
404          * Linker
405          */
ragge
1.1
406 nocom:
407         if (cflag==0 && nl!=0) {
ragge
1.6
408                 j = 0;
409                 av[j++] = "ld";
410                 av[j++] = "-X";
411                 av[j++] = "-d";
412                 av[j++] = "-e";
413                 av[j++] = "__start";
ragge
1.14
414                 if (Bstatic == 0) { /* Dynamic linkage */
ragge
1.24
415                         for (i = 0dynlinker[i]; i++)
416                                 av[j++] = dynlinker[i];
ragge
1.14
417                 }
ragge
1.1
418                 if (outfile) {
ragge
1.3
419                         av[j++] = "-o";
420                         av[j++] = outfile;
421                 }
ragge
1.14
422                 if (!nostartfiles) {
ragge
1.24
423                         av[j++] = crt0file;
424                         for (i = 0startfiles[i]; i++)
425                                 av[j++] = startfiles[i];
ragge
1.14
426                 }
ragge
1.24
427                 i = 0;
ragge
1.14
428                 while(i<nl) {
ragge
1.3
429                         av[j++] = llist[i++];
ragge
1.14
430                         if (j >= MAXAV)
ragge
1.36
431                                 error("Too many ld options");
ragge
1.14
432                 }
ragge
1.11
433 #if 0
ragge
1.3
434                 if (gflag)
435                         av[j++] = "-lg";
ragge
1.11
436 #endif
ragge
1.6
437                 av[j++] = "-lc";
ragge
1.14
438                 if (!nostartfiles) {
ragge
1.24
439                         for (i = 0endfiles[i]; i++)
440                                 av[j++] = endfiles[i];
ragge
1.14
441                 }
ragge
1.3
442                 av[j++] = 0;
443                 eflag |= callsys("/bin/ld"av);
ragge
1.1
444                 if (nc==1 && nxo==1 && eflag==0)
ragge
1.3
445                         cunlink(setsuf(clist[0], 'o'));
ragge
1.1
446         }
ragge
1.36
447         dexit(eflag);
ragge
1.4
448         return 0;
ragge
1.1
449 }
450
ragge
1.36
451 /*
452  * exit and cleanup after interrupt.
453  */
ragge
1.4
454 void
455 idexit(int arg)
ragge
1.1
456 {
ragge
1.36
457         dexit(100);
ragge
1.1
458 }
459
ragge
1.36
460 /*
461  * exit and cleanup.
462  */
ragge
1.4
463 void
ragge
1.36
464 dexit(int eval)
ragge
1.1
465 {
ragge
1.10
466         if (!pflag && !Xflag) {
ragge
1.1
467                 if (sflag==0)
468                         cunlink(tmp3);
469                 cunlink(tmp4);
470         }
ragge
1.36
471         exit(eval);
ragge
1.1
472 }
473
ragge
1.36
474 static void
475 ccerror(char *sva_list ap)
ragge
1.1
476 {
ragge
1.36
477         vfprintf(Eflag ? stderr : stdoutsap);
ragge
1.8
478         putc('\n'Eflagstderr : stdout);
ragge
1.1
479         exfail++;
480         cflag++;
481         eflag++;
482 }
483
ragge
1.36
484 /*
485  * complain a bit.
486  */
487 void
488 error(char *s, ...)
489 {
490         va_list ap;
491
492         va_start(aps);
493         ccerror(sap);
494         va_end(ap);
495 }
496
497 /*
498  * complain a bit and then exit.
499  */
ragge
1.18
500 void
ragge
1.36
501 errorx(int evalchar *s, ...)
ragge
1.18
502 {
ragge
1.36
503         va_list ap;
504
505         va_start(aps);
506         ccerror(sap);
507         va_end(ap);
508         dexit(eval);
ragge
1.18
509 }
ragge
1.3
510
ragge
1.4
511 int
ragge
1.3
512 getsuf(as)
513 char as[];
ragge
1.1
514 {
515         register char *s;
516
ragge
1.4
517         if ((s = strrchr(as'.')) && s[1] != '\0' && s[2] == '\0')
518                 return s[1];
ragge
1.3
519         return(0);
ragge
1.1
520 }
521
ragge
1.36
522 /*
523  * Get basename of string s and change its suffix to ch.
524  */
ragge
1.1
525 char *
ragge
1.36
526 setsuf(char *schar ch)
ragge
1.1
527 {
ragge
1.36
528         s = copy(basename(s));
pj
1.31
529         s[strlen(s) - 1] = ch;
ragge
1.36
530         return(s);
ragge
1.1
531 }
532
ragge
1.4
533 int
ragge
1.3
534 callsys(fv)
535 char f[], *v[]; {
ragge
1.1
536         int tstatus;
ragge
1.4
537         char *s;
ragge
1.5
538
539         if (vflag) {
540                 for (t = 0v[t]; t++)
ragge
1.6
541                         fprintf(stderr"%s "v[t]);
542                 fprintf(stderr"\n");
ragge
1.5
543         }
ragge
1.1
544
ragge
1.3
545         if ((t=fork())==0) {
ragge
1.1
546                 execv(fv);
ragge
1.4
547                 if ((s = strrchr(f'/')))
548                         execvp(s+1v);
ragge
1.1
549                 printf("Can't find %s\n"f);
ragge
1.3
550                 exit(100);
551         } else
552                 if (t == -1) {
553                         printf("Try again\n");
554                         return(100);
555                 }
556         while(t!=wait(&status));
ragge
1.1
557         if ((t=(status&0377)) != 0 && t!=14) {
ragge
1.3
558                 if (t!=2)               /* interrupt */
ragge
1.36
559                         errorx(8"Fatal error in %s"f);
560                 dexit(eflag);
ragge
1.1
561         }
ragge
1.3
562         return((status>>8) & 0377);
ragge
1.1
563 }
564
ragge
1.3
565 char *
pj
1.28
566 copy(char *as)
ragge
1.3
567 {
pj
1.28
568         char *p;
569
ragge
1.36
570         if ((p = strdup(as)) == NULL)
571                 errorx(8"no space for file names");
ragge
1.3
572
pj
1.28
573         return p;
ragge
1.3
574 }
575
ragge
1.4
576 int
ragge
1.3
577 nodup(los)
578 char **l, *os;
ragge
1.1
579 {
580         register char *t, *s;
581         register int c;
582
583         s = os;
584         if (getsuf(s) != 'o')
ragge
1.3
585                 return(1);
ragge
1.4
586         while((t = *l++)) {
587                 while((c = *s++))
ragge
1.1
588                         if (c != *t++)
589                                 break;
ragge
1.3
590                 if (*t=='\0' && c=='\0')
591                         return(0);
ragge
1.1
592                 s = os;
593         }
ragge
1.3
594         return(1);
ragge
1.1
595 }
596
ragge
1.4
597 int
ragge
1.3
598 cunlink(f)
599 char *f;
ragge
1.1
600 {
ragge
1.13
601         if (f==0 || Xflag)
ragge
1.3
602                 return(0);
603         return(unlink(f));
ragge
1.1
604 }
pj
1.30
605
606 char *
607 gettmp()
608 {
609         char *sfn = strdup("/tmp/ctm.XXXXXX");
610         int fd = -1;
611
612         if ((fd = mkstemp(sfn)) == -1) {
613                 fprintf(stderr"%s: %s\n"sfnstrerror(errno));
614                 exit(8);
615         }
616         close(fd);
617
618         return sfn;
619 }
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-10-31 10:10 +0100