Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20081130211744

Diff

Diff from 1.130 to:

Annotations

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

Annotated File View

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