Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:gmcgarry:20081222020411

Diff

Diff from 1.136 to:

Annotations

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

Annotated File View

gmcgarry
1.136
1 /*      $Id: cc.c,v 1.136 2008/12/22 02:04:11 gmcgarry Exp $    */
ragge
1.128
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  */
35
36 /*
37  * Front-end to the C compiler.
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 .i are passed via ccom->as->ld
42  * - Files that end with .s are passed as->ld
43  * - Files that end with .o are passed directly to ld
44  * - Multiple files may be given on the command line.
45  * - Unrecognized options are all sent directly to ld.
46  * -c or -S cannot be combined with -o if multiple files are given.
47  *
48  * This file should be rewritten readable.
49  */
50 #include "config.h"
51
52 #include <sys/types.h>
53 #ifdef HAVE_SYS_WAIT_H
54 #include <sys/wait.h>
55 #endif
56
57 #include <ctype.h>
58 #include <errno.h>
59 #include <fcntl.h>
60 #ifdef HAVE_LIBGEN_H
61 #include <libgen.h>
62 #endif
63 #include <signal.h>
64 #include <stdarg.h>
65 #include <stdio.h>
66 #include <stdlib.h>
67 #include <string.h>
68 #ifdef HAVE_UNISTD_H
69 #include <unistd.h>
70 #endif
71
72 #ifdef WIN32
73 #include <windows.h>
74 #include <process.h>
75 #include <io.h>
76 #endif
77
78 #include "compat.h"
79
80 #include "ccconfig.h"
81 /* C command */
82
83 #define MKS(x) _MKS(x)
84 #define _MKS(x) #x
85
86 /*
87  * Many specific definitions, should be declared elsewhere.
88  */
89
90 #ifndef STDINC
91 #define STDINC          "/usr/include/"
92 #endif
93
94 #ifndef LIBDIR
95 #define LIBDIR          "/usr/lib/"
96 #endif
97
98 #ifndef PREPROCESSOR
99 #define PREPROCESSOR    "cpp"
100 #endif
101
102 #ifndef COMPILER
103 #define COMPILER        "ccom";
104 #endif
105
106 #ifndef ASSEMBLER
107 #define ASSEMBLER       "as"
108 #endif
109
110 #ifndef LINKER
111 #define LINKER          "ld"
112 #endif
113
114 #define OS MKS(TARGOS)
115 #define MACH MKS(TARGMACH)
116 #ifndef PCCINCDIR
117 #define PCCINCDIR       LIBDIR "pcc/" MACH "-" OS "/" PACKAGE_VERSION "/include"
118 #endif
119 #ifndef PCCLIBDIR
120 #define PCCLIBDIR       LIBDIR "pcc/" MACH "-" OS "/" PACKAGE_VERSION "/lib"
121 #endif
122
123 #define MAXFIL 10000
124 #define MAXLIB 10000
125 #define MAXAV  10000
126 #define MAXOPT 100
127 char    *tmp3;
128 char    *tmp4;
129 char    *outfile, *ermfile;
130 char *Bprefix(char *);
131 char *copy(char *, int),*setsuf(char *, char);
132 int getsuf(char *);
133 int main(intchar *[]);
134 void error(char *, ...);
135 void errorx(intchar *, ...);
136 int callsys(char [], char *[]);
137 int cunlink(char *);
138 void dexit(int);
139 void idexit(int);
140 char *gettmp(void);
141 void *ccmalloc(int size);
142 #ifdef WIN32
143 char *win32pathsubst(char *);
144 #endif
145 char    *av[MAXAV];
146 char    *clist[MAXFIL];
147 char    *llist[MAXLIB];
148 char    *aslist[MAXAV];
149 char    alist[20];
150 char    *xlist[100];
151 int     xnum;
152 char    *mlist[100];
153 char    *flist[100];
154 char    *wlist[100];
155 char    *idirafter;
156 int     nm;
157 int     nf;
158 int     nw;
159 int     sspflag;
160 int     Cflag;
gmcgarry
1.134
161 int     Vflag;
ragge
1.128
162 int     dflag;
163 int     pflag;
164 int     sflag;
165 int     cflag;
166 int     eflag;
167 int     gflag;
168 int     vflag;
169 int     tflag;
170 int     Eflag;
171 int     Oflag;
172 int     kflag;  /* generate PIC/pic code */
173 #define F_PIC   1
174 #define F_pic   2
175 int     Mflag;  /* dependencies only */
176 int     pgflag;
177 int     exfail;
178 int     Xflag;
179 int     Wallflag;
180 int     Wflag;
181 int     nostartfilesBstaticshared;
182 int     nostdincnostdlib;
183 int     onlyas;
184 int     pthreads;
185 int     xcflag;
186 int     ascpp;
187
188 char    *passp = LIBEXECDIR PREPROCESSOR;
189 char    *pass0 = LIBEXECDIR COMPILER;
190 char    *as = ASSEMBLER;
191 char    *ld = LINKER;
192 char    *Bflag;
193 char *cppadd[] = CPPADD;
194 #ifdef DYNLINKER
195 char *dynlinker[] = DYNLINKER;
196 #endif
197 #ifdef CRT0FILE
198 char *crt0file = CRT0FILE;
199 #endif
200 #ifdef CRT0FILE_PROFILE
201 char *crt0file_profile = CRT0FILE_PROFILE;
202 #endif
203 #ifdef STARTFILES
204 char *startfiles[] = STARTFILES;
205 char *endfiles[] = ENDFILES;
206 #endif
207 #ifdef STARTFILES_T
208 char *startfiles_T[] = STARTFILES_T;
209 char *endfiles_T[] = ENDFILES_T;
210 #endif
211 #ifdef STARTFILES_S
212 char *startfiles_S[] = STARTFILES_S;
213 char *endfiles_S[] = ENDFILES_S;
214 #endif
215 #ifdef MULTITARGET
216 char *mach = DEFMACH;
217 struct cppmd {
218         char *mach;
219         char *cppmdadd[MAXCPPMDARGS];
220 };
221
222 struct cppmd cppmds[] = CPPMDADDS;
223 #else
224 char *cppmdadd[] = CPPMDADD;
225 #endif
226 #ifdef LIBCLIBS
227 char *libclibs[] = LIBCLIBS;
228 #else
229 char *libclibs[] = { "-lc"NULL };
230 #endif
231 #ifdef LIBCLIBS_PROFILE
232 char *libclibs_profile[] = LIBCLIBS_PROFILE;
233 #else
234 char *libclibs_profile[] = { "-lc_p"NULL };
235 #endif
236 #ifndef STARTLABEL
237 #define STARTLABEL "__start"
238 #endif
239 char *incdir = STDINC;
gmcgarry
1.136
240 char *libdir = LIBDIR;
ragge
1.128
241 char *pccincdir = PCCINCDIR;
242 char *pcclibdir = PCCLIBDIR;
243
244 /* handle gcc warning emulations */
245 struct Wflags {
246         char *name;
247         int flags;
248 #define INWALL          1
249 #define NEGATIVE        2
250 Wflags[] = {
251         { "-Werror"0 },
252         { "-Wshadow"0 },
253         { "-Wno-shadow"NEGATIVE },
254         { "-Wpointer-sign"INWALL },
255         { "-Wno-pointer-sign"NEGATIVE },
256         { "-Wsign-compare"0 },
257         { "-Wno-sign-compare"NEGATIVE },
258         { "-Wunknown-pragmas"INWALL },
259         { "-Wno-unknown-pragmas"NEGATIVE },
260         { "-Wunreachable-code"0 },
261         { "-Wno-unreachable-code"NEGATIVE },
262         { 00 },
263 };
264
265 #define SZWFL   (sizeof(Wflags)/sizeof(Wflags[0]))
266
267 int
268 main(int argcchar *argv[])
269 {
270         struct Wflags *Wf;
271         char *t, *u;
272         char *assource;
273         char **pv, *ptemp[MAXOPT], **pvt;
274         int ncnlnasijcnxona;
275 #ifdef MULTITARGET
276         int k;
277 #endif
278
279 #ifdef WIN32
280         /* have to prefix path early.  -B may override */
281         incdir = win32pathsubst(incdir);
gmcgarry
1.136
282         libdir = win32pathsubst(libdir);
ragge
1.128
283         pccincdir = win32pathsubst(pccincdir);
284         pcclibdir = win32pathsubst(pcclibdir);
285         passp = win32pathsubst(passp);
286         pass0 = win32pathsubst(pass0);
287 #endif
288
289         i = nc = nl = nas = nxo = 0;
290         pv = ptemp;
291         while(++i < argc) {
292                 if (argv[i][0] == '-') {
293                         switch (argv[i][1]) {
294                         default:
295                                 goto passa;
296 #ifdef notyet
297         /* must add library options first (-L/-l/...) */
298                                 error("unrecognized option `-%c'"argv[i][1]);
299                                 break;
300 #endif
301
302                         case '-'/* double -'s */
ragge
1.130
303                                 if (strcmp(argv[i], "--version") == 0) {
ragge
1.128
304                                         printf("%s\n"VERSSTR);
ragge
1.130
305                                         return 0;
306                                 } else if (strcmp(argv[i], "--param") == 0)
ragge
1.128
307                                         /* NOTHING YET */;
308                                 else
309                                         error("unrecognized option %s"argv[i]);
310                                 break;
311
312                         case 'B'/* other search paths for binaries */
313                                 Bflag = &argv[i][2];
314                                 break;
315
316 #ifdef MULTITARGET
317                         case 'b':
318                                 t = &argv[i][2];
319                                 if (*t == '\0' && i + 1 < argc) {
320                                         t = argv[i+1];
321                                         i++;
322                                 }
323                                 if (strncmp(t"?"1) == 0) {
324                                         /* show machine targets */
325                                         printf("Available machine targets:");
326                                         for (j=0cppmds[j].machj++)
327                                                 printf(" %s",cppmds[j].mach);
328                                         printf("\n");
329                                         exit(0);
330                                 }
331                                 for (j=0cppmds[j].machj++)
332                                         if (strcmp(tcppmds[j].mach) == 0) {
333                                                 mach = cppmds[j].mach;
334                                                 break;
335                                         }
336                                 if (cppmds[j].mach == NULL)
337                                         errorx(1"unknown target arch %s"t);
338                                 break;
339 #endif
340
341                         case 'X':
342                                 Xflag++;
343                                 break;
344                         case 'W'/* Ignore (most of) W-flags */
345                                 if (strncmp(argv[i], "-Wl,"4) == 0) {
346                                         /* options to the linker */
347                                         t = &argv[i][4];
348                                         while ((u = strchr(t','))) {
349                                                 *u++ = 0;
350                                                 llist[nl++] = t;
351                                                 t = u;
352                                         }
353                                         llist[nl++] = t;
354                                 } else if (strncmp(argv[i], "-Wa,"4) == 0) {
355                                         /* options to the assembler */
356                                         t = &argv[i][4];
357                                         while ((u = strchr(t','))) {
358                                                 *u++ = 0;
359                                                 aslist[nas++] = t;
360                                                 t = u;
361                                         }
362                                         aslist[nas++] = t;
363                                 } else if (strncmp(argv[i], "-Wp,"4) == 0) {
364                                         /* preprocessor */
365                                         if (!strncmp(argv[i], "-Wp,-C"6))
366                                                 Cflag++;
gmcgarry
1.134
367                                         else if (!strncmp(argv[i], "-Wp,-V"6))
368                                                 Vflag++;
ragge
1.128
369                                 } else if (strcmp(argv[i], "-Wall") == 0) {
370                                         Wallflag = 1;
371                                 } else if (strcmp(argv[i], "-WW") == 0) {
372                                         Wflag = 1;
373                                 } else {
374                                         /* check and set if available */
375                                         for (Wf = WflagsWf->nameWf++) {
376                                                 if (strcmp(argv[i], Wf->name))
377                                                         continue;
378                                                 wlist[nw++] = Wf->name;
379                                         }
380                                 }
381                                 break;
382
383                         case 'f'/* GCC compatibility flags */
384                                 if (strcmp(argv[i], "-fPIC") == 0)
385                                         kflag = F_PIC;
386                                 else if (strcmp(argv[i], "-fpic") == 0)
387                                         kflag = F_pic;
388                                 else if (strcmp(argv[i],
389                                     "-fsigned-char") == 0)
390                                         flist[nf++] = argv[i];
391                                 else if (strcmp(argv[i],
392                                     "-fno-signed-char") == 0)
393                                         flist[nf++] = argv[i];
394                                 else if (strcmp(argv[i],
395                                     "-funsigned-char") == 0)
396                                         flist[nf++] = argv[i];
397                                 else if (strcmp(argv[i],
398                                     "-fno-unsigned-char") == 0)
399                                         flist[nf++] = argv[i];
400                                 else if (strcmp(argv[i],
401                                     "-fstack-protector") == 0) {
402                                         flist[nf++] = argv[i];
403                                         sspflag++;
404                                 } else if (strcmp(argv[i],
405                                     "-fstack-protector-all") == 0) {
406                                         flist[nf++] = argv[i];
407                                         sspflag++;
408                                 } else if (strcmp(argv[i],
409                                     "-fno-stack-protector") == 0) {
410                                         flist[nf++] = argv[i];
411                                         sspflag = 0;
412                                 } else if (strcmp(argv[i],
413                                     "-fno-stack-protector-all") == 0) {
414                                         flist[nf++] = argv[i];
415                                         sspflag = 0;
416                                 }
417                                 /* silently ignore the rest */
418                                 break;
419
420                         case 'g'/* create debug output */
421                                 gflag++;
422                                 break;
423
424                         case 'i':
425                                 if (strcmp(argv[i], "-isystem") == 0) {
426                                         *pv++ = "-S";
427                                         *pv++ = argv[++i];
428                                 } else if (strcmp(argv[i], "-include") == 0) {
429                                         *pv++ = "-i";
430                                         *pv++ = argv[++i];
431                                 } else if (strcmp(argv[i], "-idirafter") == 0) {
432                                         idirafter = argv[++i];
433                                 } else
434                                         goto passa;
435                                 break;
436
437                         case 'k'/* generate PIC code */
438                                 kflag = F_pic;
439                                 break;
440
441                         case 'm'/* target-dependent options */
442                                 mlist[nm++] = argv[i];
443                                 break;
444
445                         case 'n'/* handle -n flags */
446                                 if (strcmp(argv[i], "-nostdinc") == 0)
447                                         nostdinc++;
448                                 else if (strcmp(argv[i], "-nostdlib") == 0) {
449                                         nostdlib++;
450                                         nostartfiles++;
451                                 } else if (strcmp(argv[i], "-nostartfiles") == 0)
452                                         nostartfiles = 1;
453                                 else
454                                         goto passa;
455                                 break;
456
457                         case 'p':
458                                 if (strcmp(argv[i], "-pg") == 0 ||
459                                     strcmp(argv[i], "-p") == 0)
460                                         pgflag++;
461                                 else if (strcmp(argv[i], "-pthread") == 0)
462                                         pthreads++;
463                                 else if (strcmp(argv[i], "-pipe") == 0)
464                                         /* NOTHING YET */;
465                                 else
466                                         errorx(1"unknown option %s"argv[i]);
467                                 break;
468
469                         case 'x':
470                                 t = &argv[i][2];
471                                 if (*t == 0)
472                                         t = argv[++i];
473                                 if (strcmp(t"c") == 0)
474                                         xcflag = 1/* default */
475                                 else if (strcmp(t"assembler-with-cpp") == 0)
476                                         ascpp = 1;
477 #ifdef notyet
478                                 else if (strcmp(t"c++") == 0)
479                                         cxxflag++;
480 #endif
481                                 else
482                                         xlist[xnum++] = argv[i];
483                                 break;
484                         case 't':
485                                 tflag++;
486                                 break;
487                         case 'S':
488                                 sflag++;
489                                 cflag++;
490                                 break;
491                         case 'o':
492                                 if (outfile)
493                                         errorx(8"too many -o");
494                                 outfile = argv[++i];
495                                 break;
496                         case 'O':
497                                 if (argv[i][2] == '0')
498                                         Oflag = 0;
499                                 else
500                                         Oflag++;
501                                 break;
502                         case 'E':
503                                 Eflag++;
504                                 break;
505                         case 'P':
506                                 pflag++;
507                                 *pv++ = argv[i];
508                         case 'c':
509                                 cflag++;
510                                 break;
511
512 #if 0
513                         case '2':
514                                 if(argv[i][2] == '\0')
515                                         pref = "/lib/crt2.o";
516                                 else {
517                                         pref = "/lib/crt20.o";
518                                 }
519                                 break;
520 #endif
521                         case 'C':
522                                 Cflag = 1;
523                                 break;
524                         case 'D':
525                         case 'I':
526                         case 'U':
527                                 *pv++ = argv[i];
528                                 if (argv[i][2] == 0)
529                                         *pv++ = argv[++i];
530                                 if (pv >= ptemp+MAXOPT) {
531                                         error("Too many DIU options");
532                                         --pv;
533                                 }
534                                 break;
535
536                         case 'M':
537                                 Mflag++;
538                                 break;
539
540                         case 'd':
541                                 dflag++;
542                                 strlcpy(alistargv[i], sizeof (alist));
543                                 break;
544                         case 'v':
545                                 printf("%s\n"VERSSTR);
546                                 vflag++;
547                                 break;
548
549                         case 's':
550                                 if (strcmp(argv[i], "-static") == 0)
551                                         Bstatic = 1;
552                                 else if (strcmp(argv[i], "-shared") == 0) {
553                                         shared = 1;
554 #ifndef os_win32
555                                         nostdlib = 1;
556 #endif
557                                 } else if (strncmp(argv[i], "-std"4) == 0) {
558                                         /* ignore gcc -std= */;
559                                 } else
560                                         goto passa;
561                                 break;
562                         }
563                 } else {
564                 passa:
565                         t = argv[i];
566                         if (*argv[i] == '-' && argv[i][1] == 'L')
567                                 ;
568                         else if((c=getsuf(t))=='c' || c=='S' || c=='i' ||
569                             c=='s'|| Eflag || xcflag) {
570                                 clist[nc++] = t;
571                                 if (nc>=MAXFIL) {
572                                         error("Too many source files");
573                                         exit(1);
574                                 }
575                                 t = setsuf(t'o');
576                         }
577
578                         /* Check for duplicate .o files. */
579                         for (j = getsuf(t) == 'o' ? 0 : nlj < nlj++) {
580                                 if (strcmp(llist[j], t) == 0)
581                                         break;
582                         }
583                         if (j == nl) {
584                                 llist[nl++] = t;
585                                 if (nl >= MAXLIB) {
586                                         error("Too many object/library files");
587                                         exit(1);
588                                 }
589                                 if (getsuf(t)=='o')
590                                         nxo++;
591                         }
592                 }
593         }
594         /* Sanity checking */
595         if (nc == 0 && nl == 0)
596                 errorx(8"no input files");
597         if (outfile && (cflag || sflag || Eflag) && nc > 1)
598                 errorx(8"-o given with -c || -E || -S and more than one file");
599         if (outfile && clist[0] && strcmp(outfileclist[0]) == 0)
600                 errorx(8"output file will be clobbered");
601 #if 0
602         if (proflag)
603                 pref = "/lib/mcrt0.o";
604 #endif
605         if(nc==0)
606                 goto nocom;
607         if (pflag==0) {
608                 if (!sflag)
609                         tmp3 = gettmp();
610                 tmp4 = gettmp();
611         }
612         if (Bflag) {
613                 incdir = Bflag;
gmcgarry
1.136
614                 libdir = Bflag;
ragge
1.128
615                 pccincdir = Bflag;
616                 pcclibdir = Bflag;
617         }
618         if (signal(SIGINTSIG_IGN) != SIG_IGN/* interrupt */
619                 signal(SIGINTidexit);
620         if (signal(SIGTERMSIG_IGN) != SIG_IGN)        /* terminate */
621                 signal(SIGTERMidexit);
622 #ifdef MULTITARGET
ragge
1.133
623         pass0 = copy(LIBEXECDIR "/ccom_"k = strlen(mach));
624         strlcat(pass0machsizeof(LIBEXECDIR "/ccom_") + k);
ragge
1.128
625 #endif
626         pvt = pv;
627         for (i=0i<nci++) {
628                 /*
629                  * C preprocessor
630                  */
631                 if (nc>1 && !Eflag)
632                         printf("%s:\n"clist[i]);
633                 onlyas = 0;
634                 assource = tmp3;
635                 if (getsuf(clist[i])=='S')
636                         ascpp = 1;
637                 if (getsuf(clist[i])=='i') {
638                         if(Eflag)
639                                 continue;
640                         goto com;
641                 } else if (ascpp) {
642                         onlyas = 1;
643                 } else if (getsuf(clist[i])=='s') {
644                         assource = clist[i];
645                         goto assemble;
646                 }
647                 if (pflag)
648                         tmp4 = setsuf(clist[i], 'i');
649                 na = 0;
650                 av[na++] = "cpp";
651                 if (vflag)
652                         av[na++] = "-v";
653                 av[na++] = "-D__PCC__=" MKS(PCC_MAJOR);
654                 av[na++] = "-D__PCC_MINOR__=" MKS(PCC_MINOR);
655                 av[na++] = "-D__PCC_MINORMINOR__=" MKS(PCC_MINORMINOR);
656                 if (ascpp)
657                         av[na++] = "-D__ASSEMBLER__";
658                 if (sspflag)
659                         av[na++] = "-D__SSP__=1";
660                 if (pthreads)
661                         av[na++] = "-D_PTHREADS";
662                 if (Cflag)
663                         av[na++] = "-C";
664                 if (Mflag)
665                         av[na++] = "-M";
gmcgarry
1.134
666                 if (Vflag)
667                         av[na++] = "-V";
ragge
1.128
668                 if (dflag)
669                         av[na++] = alist;
670                 for (j = 0cppadd[j]; j++)
671                         av[na++] = cppadd[j];
672 #ifdef MULTITARGET
673                 for (k = 0cppmds[k].machk++) {
674                         if (strcmp(cppmds[k].machmach) != 0)
675                                 continue;
676                         for (j = 0cppmds[k].cppmdadd[j]; j++)
677                                 av[na++] = cppmds[k].cppmdadd[j];
678                         break;
679                 }
680 #else
681                 for (j = 0cppmdadd[j]; j++)
682                         av[na++] = cppmdadd[j];
683 #endif
684                 if (tflag)
685                         av[na++] = "-t";
686                 for(pv=ptemppv <pvtpv++)
687                         av[na++] = *pv;
688                 if (!nostdinc)
689                         av[na++] = "-S"av[na++] = incdir;
690                 av[na++] = "-I"av[na++] = pccincdir;
691                 if (idirafter) {
692                         av[na++] = "-I";
693                         av[na++] = idirafter;
694                 }
695                 av[na++] = clist[i];
696                 if (!Eflag && !Mflag)
697                         av[na++] = tmp4;
698                 if (Eflag && outfile)
699                          ermfile = av[na++] = outfile;
700                 av[na++]=0;
701                 if (callsys(passpav))
702                         {exfail++; eflag++;}
703                 if (Eflag || Mflag)
704                         continue;
705                 if (onlyas) {
706                         assource = tmp4;
707                         goto assemble;
708                 }
709
710                 /*
711                  * C compiler
712                  */
713         com:
714                 na = 0;
715                 av[na++]= "ccom";
716                 if (Wallflag) {
717                         /* Set only the same flags as gcc */
718                         for (Wf = WflagsWf->nameWf++) {
719                                 if (Wf->flags != INWALL)
720                                         continue;
721                                 av[na++] = Wf->name;
722                         }
723                 }
724                 if (Wflag) {
725                         /* set all positive flags */
726                         for (Wf = WflagsWf->nameWf++) {
727                                 if (Wf->flags == NEGATIVE)
728                                         continue;
729                                 av[na++] = Wf->name;
730                         }
731                 }
732                 for (j = 0j < nwj++)
733                         av[na++] = wlist[j];
734                 for (j = 0j < nfj++)
735                         av[na++] = flist[j];
736                 if (vflag)
737                         av[na++] = "-v";
738                 if (pgflag)
739                         av[na++] = "-p";
740                 if (gflag)
741                         av[na++] = "-g";
742 #ifdef os_darwin
743                 /* darwin always wants PIC compilation */
744                 if (!Bstatic)
745                         av[na++] = "-k";
746 #else
747                 if (kflag)
748                         av[na++] = "-k";
749 #endif
750                 if (Oflag) {
751                         av[na++] = "-xtemps";
752                         av[na++] = "-xdeljumps";
ragge
1.133
753                         av[na++] = "-xinline";
ragge
1.128
754                 }
755                 for (j = 0j < xnumj++)
756                         av[na++] = xlist[j];
757                 for (j = 0j < nmj++)
758                         av[na++] = mlist[j];
759                 if (getsuf(clist[i])=='i')
760                         av[na++] = clist[i];
761                 else
762                         av[na++] = tmp4/* created by cpp */
763                 if (pflag || exfail)
764                         {
765                         cflag++;
766                         continue;
767                         }
768                 if(sflag) {
769                         if (outfile)
770                                 tmp3 = outfile;
771                         else
772                                 tmp3 = setsuf(clist[i], 's');
773                 }
774                 ermfile = av[na++] = tmp3;
775 #if 0
776                 if (proflag) {
777                         av[3] = "-XP";
778                         av[4] = 0;
779                 } else
780                         av[3] = 0;
781 #endif
782                 av[na++] = NULL;
783                 if (callsys(pass0av)) {
784                         cflag++;
785                         eflag++;
786                         continue;
787                 }
788                 if (sflag)
789                         continue;
790
791                 /*
792                  * Assembler
793                  */
794         assemble:
795                 na = 0;
796                 av[na++] = as;
797                 for (j = 0j < nasj++)
798                         av[na++] = aslist[j];
799 #if defined(os_sunos) && defined(mach_sparc64)
800                 av[na++] = "-m64";
801 #endif
802 #if defined(os_darwin)
803                 if (Bstatic)
804                         av[na++] = "-static";
805 #endif
806                 if (vflag)
807                         av[na++] = "-v";
808                 if (kflag)
809                         av[na++] = "-k";
810                 av[na++] = "-o";
811                 if (outfile && cflag)
812                         ermfile = av[na++] = outfile;
813                 else
814                         ermfile = av[na++] = setsuf(clist[i], 'o');
815                 av[na++] = assource;
816                 if (dflag)
817                         av[na++] = alist;
818                 av[na++] = 0;
819                 if (callsys(asav)) {
820                         cflag++;
821                         eflag++;
822                         cunlink(tmp4);
823                         continue;
824                 }
825                 cunlink(tmp4);
826         }
827
828         if (Eflag || Mflag)
829                 dexit(eflag);
830
831         /*
832          * Linker
833          */
834 nocom:
835         if (cflag==0 && nl!=0) {
836                 j = 0;
837                 av[j++] = ld;
838 #ifndef MSLINKER
839                 if (vflag)
840                         av[j++] = "-v";
841 #endif
gmcgarry
1.131
842 #if !defined(os_sunos) && !defined(os_win32) && !defined(os_darwin)
ragge
1.128
843                 av[j++] = "-X";
844 #endif
845                 if (shared) {
846                         av[j++] = "-shared";
847 #ifdef os_win32
848                         av[j++] = "-Bdynamic";
849 #endif
850 #ifndef os_sunos
851                 } else {
852 #ifndef os_win32
853 #ifndef os_darwin
854                         av[j++] = "-d";
855 #endif
856                         av[j++] = "-e";
857                         av[j++] = STARTLABEL;
858 #endif
859 #endif
860                         if (Bstatic == 0) { /* Dynamic linkage */
861 #ifdef DYNLINKER
862                                 for (i = 0dynlinker[i]; i++)
863                                         av[j++] = dynlinker[i];
864 #endif
865                         } else {
866 #ifdef os_darwin
867                                 av[j++] = "-static";
868 #else
869                                 av[j++] = "-Bstatic";
870 #endif
871                         }
872                 }
873                 if (outfile) {
874 #ifdef MSLINKER
gmcgarry
1.135
875 #define OUTSTR  "/OUT:"
876                         char *s = copy(OUTSTRi = strlen(outfile));
877                         strlcat(soutfilesizeof(OUTSTR) + i);
ragge
1.128
878                         av[j++] = s;
879 #else
880                         av[j++] = "-o";
881                         av[j++] = outfile;
882 #endif
883                 }
884 #ifdef STARTFILES_S
885                 if (shared) {
886                         if (!nostartfiles) {
887                                 for (i = 0startfiles_S[i]; i++)
888                                         av[j++] = Bprefix(startfiles_S[i]);
889                         }
890                 } else
891 #endif
892                 {
893                         if (!nostartfiles) {
894 #ifdef CRT0FILE_PROFILE
895                                 if (pgflag) {
896                                         av[j++] = Bprefix(crt0file_profile);
897                                 } else
898 #endif
899                                 {
900 #ifdef CRT0FILE
901                                         av[j++] = Bprefix(crt0file);
902 #endif
903                                 }
904 #ifdef STARTFILES_T
905                                 if (Bstatic) {
906                                         for (i = 0startfiles_T[i]; i++)
ragge
1.129
907                                                 av[j++] = Bprefix(startfiles_T[i]);
ragge
1.128
908                                 } else
909 #endif
910                                 {
911 #ifdef STARTFILES
912                                         for (i = 0startfiles[i]; i++)
913                                                 av[j++] = Bprefix(startfiles[i]);
914 #endif
915                                 }
916                         }
917                 }
918                 i = 0;
919                 while(i<nl) {
920                         av[j++] = llist[i++];
921                         if (j >= MAXAV)
922                                 error("Too many ld options");
923                 }
924 #ifndef MACHOABI
925                 /* darwin assembler doesn't want -g */
926                 if (gflag)
927                         av[j++] = "-g";
928 #endif
929 #if 0
930                 if (gflag)
931                         av[j++] = "-lg";
932 #endif
933                 if (pthreads)
934                         av[j++] = "-lpthread";
935                 if (!nostdlib) {
936 #ifdef MSLINKER
ragge
1.133
937 #define DL      "/LIBPATH:"
ragge
1.128
938 #else
ragge
1.133
939 #define DL      "-L"
ragge
1.128
940 #endif
ragge
1.133
941                         char *s = copy(DLi = strlen(pcclibdir));
gmcgarry
1.136
942                         strlcat(spcclibdirsizeof(DL) + i);
ragge
1.128
943                         av[j++] = s;
gmcgarry
1.136
944 #ifdef os_win32
945                         s = copy(DLi = strlen(libdir));
946                         strlcat(slibdirsizeof(DL) + i);
947                         av[j++] = s;
948 #endif
ragge
1.128
949                         if (pgflag) {
950                                 for (i = 0libclibs_profile[i]; i++)
951                                         av[j++] = Bprefix(libclibs_profile[i]);
952                         } else {
953                                 for (i = 0libclibs[i]; i++)
954                                         av[j++] = Bprefix(libclibs[i]);
955                         }
956                 }
957                 if (!nostartfiles) {
958 #ifdef STARTFILES_S
959                         if (shared) {
960                                 for (i = 0endfiles_S[i]; i++)
961                                         av[j++] = Bprefix(endfiles_S[i]);
962                         } else 
963 #endif
964                         {
965 #ifdef STARTFILES_T
966                                 if (Bstatic) {
967                                         for (i = 0endfiles_T[i]; i++)
968                                                 av[j++] = Bprefix(endfiles_T[i]);
969                                 } else
970 #endif
971                                 {
972 #ifdef STARTFILES
973                                         for (i = 0endfiles[i]; i++)
974                                                 av[j++] = Bprefix(endfiles[i]);
975 #endif
976                                 }
977                         }
978                 }
979                 av[j++] = 0;
980                 eflag |= callsys(ldav);
981                 if (nc==1 && nxo==1 && eflag==0)
982                         cunlink(setsuf(clist[0], 'o'));
983                 else if (nc > 0 && eflag == 0) {
984                         /* remove .o files XXX ugly */
985                         for (i = 0i < nci++)
986                                 cunlink(setsuf(clist[i], 'o'));
987                 }
988         }
989         dexit(eflag);
990         return 0;
991 }
992
993 /*
994  * exit and cleanup after interrupt.
995  */
996 void
997 idexit(int arg)
998 {
999         dexit(100);
1000 }
1001
1002 /*
1003  * exit and cleanup.
1004  */
1005 void
1006 dexit(int eval)
1007 {
1008         if (!pflag && !Xflag) {
1009                 if (sflag==0)
1010                         cunlink(tmp3);
1011                 cunlink(tmp4);
1012         }
1013         if (exfail || eflag)
1014                 cunlink(ermfile);
1015         if (eval == 100)
1016                 _exit(eval);
1017         exit(eval);
1018 }
1019
1020 static void
1021 ccerror(char *sva_list ap)
1022 {
1023         vfprintf(Eflag ? stderr : stdoutsap);
1024         putc('\n'Eflagstderr : stdout);
1025         exfail++;
1026         cflag++;
1027         eflag++;
1028 }
1029
1030 /*
1031  * complain a bit.
1032  */
1033 void
1034 error(char *s, ...)
1035 {
1036         va_list ap;
1037
1038         va_start(aps);
1039         ccerror(sap);
1040         va_end(ap);
1041 }
1042
1043 /*
1044  * complain a bit and then exit.
1045  */
1046 void
1047 errorx(int evalchar *s, ...)
1048 {
1049         va_list ap;
1050
1051         va_start(aps);
1052         ccerror(sap);
1053         va_end(ap);
1054         dexit(eval);
1055 }
1056
1057 char *
1058 Bprefix(char *s)
1059 {
1060         char *suffix;
1061         char *str;
ragge
1.133
1062         int i;
ragge
1.128
1063
gmcgarry
1.136
1064 #ifdef os_win32
1065
1066         /*  put here to save sprinkling it ~everywhere  */
1067         s =  win32pathsubst(s);
1068
1069         if (Bflag == NULL)
1070                 return s;
1071         suffix = strrchr(s'/');
1072         if (suffix == NULL)
1073                 suffix = strrchr(s'\\');
1074
1075 #else
1076
ragge
1.128
1077         if (Bflag == NULL || s[0] != '/')
1078                 return s;
gmcgarry
1.136
1079         suffix = strrchr(s'/');
1080
1081 #endif
ragge
1.128
1082
1083         if (suffix == NULL)
1084                 suffix = s;
1085
ragge
1.133
1086         str = copy(Bflagi = strlen(suffix));
1087         strlcat(strsuffixstrlen(Bflag) + i + 1);
ragge
1.128
1088         return str;
1089 }
1090
1091 int
1092 getsuf(char *s)
1093 {
1094         register char *p;
1095
1096         if ((p = strrchr(s'.')) && p[1] != '\0' && p[2] == '\0')
1097                 return p[1];
1098         return(0);
1099 }
1100
1101 /*
1102  * Get basename of string s and change its suffix to ch.
1103  */
1104 char *
1105 setsuf(char *schar ch)
1106 {
1107         char *p;
1108
1109         s = copy(basename(s), 2);
1110         if ((p = strrchr(s'.')) == NULL) {
1111                 p = s + strlen(s);