Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20020324112007

Diff

Diff from 1.2 to:

Annotations

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

Annotated File View

ragge
1.2
1 /*      $Id: cc.c,v 1.2 2002/03/24 11:20:07 ragge Exp $ */
2
3 #if 0
ragge
1.1
4 static  char sccsid[] = "@(#)cc.c 4.21 6/30/90";
ragge
1.2
5 #endif
ragge
1.1
6 /*
7  * cc - front end for C compiler
8  */
9 #include <sys/param.h>
ragge
1.2
10 #include <sys/dir.h>
11 #include <sys/wait.h>
12
ragge
1.1
13 #include <stdio.h>
14 #include <ctype.h>
15 #include <signal.h>
ragge
1.2
16 #include <stdlib.h>
17 #include <unistd.h>
18 #include <string.h>
19
ragge
1.1
20 #include "pathnames.h"
21
22 char    *cpp = _PATH_CPP;
23 char    *ccom = _PATH_CCOM;
24 char    *c2 = _PATH_C2;
25 char    *as = _PATH_AS;
26 char    *ld = _PATH_LD;
27 char    *crt0 = _PATH_CRT0;
28
29 char    tmp0[MAXPATHLEN];
30 char    *tmp1, *tmp2, *tmp3, *tmp4, *tmp5;
31 char    *outfile;
ragge
1.2
32 char    *savestr(char *cp), *strspl(char *leftchar *right);
33 char    *setsuf(char *asint ch);
34 int     getsuf(char as[]), nodup(char **lchar *os);
35 int     callsys(char *fchar **v);
36 void    idexit(int a), error(char *schar *x), dexit(void);
ragge
1.1
37 char    **av, **clist, **llist, **plist;
38 int     cflageflagoflagpflagsflagwflagRflagexflagproflag;
ragge
1.2
39 int     gflagGflagMflagdebug;
ragge
1.1
40 char    *dflag;
41 int     exfail;
42 char    *chpass;
43 char    *npassname;
44
45 int     ncnlnpnxona;
46
47 #define cunlink(s)      if (s) unlink(s)
48
ragge
1.2
49 int
50 main(int argcchar **argv)
ragge
1.1
51 {
52         char *t;
53         char *assource;
54         int ijc;
55
56         /* ld currently adds upto 5 args; 10 is room to spare */
57         av = (char **)calloc(argc+10sizeof (char **));
58         clist = (char **)calloc(argcsizeof (char **));
59         llist = (char **)calloc(argcsizeof (char **));
60         plist = (char **)calloc(argcsizeof (char **));
61         for (i = 1i < argci++) {
62                 if (*argv[i] == '-'switch (argv[i][1]) {
63
64                 case 'S':
65                         sflag++;
66                         cflag++;
67                         continue;
68                 case 'o':
69                         if (++i < argc) {
70                                 outfile = argv[i];
71                                 switch (getsuf(outfile)) {
72
73                                 case 'c':
74                                         error("-o would overwrite %s",
75                                             outfile);
76                                         exit(8);
77                                 }
78                         }
79                         continue;
80                 case 'R':
81                         Rflag++;
82                         continue;
83                 case 'O':
84                         oflag++;
85                         continue;
86                 case 'p':
87                         proflag++;
88                         crt0 = _PATH_MCRT0;
89                         if (argv[i][2] == 'g')
90                                 crt0 = _PATH_GCRT0;
91                         continue;
92                 case 'g':
93                         if (argv[i][2] == 'o') {
94                             Gflag++;    /* old format for -go */
95                         } else {
96                             gflag++;    /* new format for -g */
97                         }
98                         continue;
99                 case 'w':
100                         wflag++;
101                         continue;
102                 case 'E':
103                         exflag++;
104                 case 'P':
105                         pflag++;
106                         if (argv[i][1]=='P')
107                                 fprintf(stderr,
108         "cc: warning: -P option obsolete; you should use -E instead\n");
109                         plist[np++] = argv[i];
110                 case 'c':
111                         cflag++;
112                         continue;
113                 case 'M':
114                         exflag++;
115                         pflag++;
116                         Mflag++;
117                         /* and fall through */
118                 case 'D':
119                 case 'I':
120                 case 'U':
121                 case 'C':
122                         plist[np++] = argv[i];
123                         continue;
124                 case 'L':
125                         llist[nl++] = argv[i];
126                         continue;
127                 case 't':
128                         if (chpass)
129                                 error("-t overwrites earlier option"0);
130                         chpass = argv[i]+2;
131                         if (chpass[0]==0)
132                                 chpass = "012p";
133                         continue;
134                 case 'B':
135                         if (npassname)
136                                 error("-B overwrites earlier option"0);
137                         npassname = argv[i]+2;
138                         if (npassname[0]==0)
139                                 error("-B requires an argument"0);
140                         continue;
141                 case 'd':
142                         if (argv[i][2] == '\0') {
143                                 debug++;
144                                 continue;
145                         }
146                         dflag = argv[i];
147                         continue;
148                 }
149                 t = argv[i];
150                 c = getsuf(t);
151                 if (c=='c' || c=='s' || exflag) {
152                         clist[nc++] = t;
153                         t = setsuf(t'o');
154                 }
155                 if (nodup(llistt)) {
156                         llist[nl++] = t;
157                         if (getsuf(t)=='o')
158                                 nxo++;
159                 }
160         }
161         if (gflag || Gflag) {
162                 if (oflag)
163                         fprintf(stderr"cc: warning: -g disables -O\n");
164                 oflag = 0;
165         }
166         if (npassname && chpass ==0)
167                 chpass = "012p";
168         if (chpass && npassname==0)
169                 npassname = _PATH_USRNEW;
170         if (chpass)
171         for (t=chpass; *tt++) {
172                 switch (*t) {
173
174                 case '0':
ragge
1.2
175                         ccom = strspl(npassname"ccom");
ragge
1.1
176                         continue;
177                 case '2':
178                         c2 = strspl(npassname"c2");
179                         continue;
180                 case 'p':
181                         cpp = strspl(npassname"cpp");
182                         continue;
183                 }
184         }
185         if (nc==0)
186                 goto nocom;
187         if (signal(SIGINTSIG_IGN) != SIG_IGN)
188                 signal(SIGINTidexit);
189         if (signal(SIGTERMSIG_IGN) != SIG_IGN)
190                 signal(SIGTERMidexit);
191         if (signal(SIGHUPSIG_IGN) != SIG_IGN)
192                 signal(SIGHUPidexit);
193         if (pflag==0)
ragge
1.2
194                 (void)sprintf(tmp0"%s/ctm%5.5d"_PATH_TMPgetpid());
ragge
1.1
195         tmp1 = strspl(tmp0"1");
196         tmp2 = strspl(tmp0"2");
197         tmp3 = strspl(tmp0"3");
198         if (pflag==0)
199                 tmp4 = strspl(tmp0"4");
200         if (oflag)
201                 tmp5 = strspl(tmp0"5");
202         for (i=0i<nci++) {
203                 if (nc > 1 && !Mflag) {
204                         printf("%s:\n"clist[i]);
205                         fflush(stdout);
206                 }
207                 if (!Mflag && getsuf(clist[i]) == 's') {
208                         assource = clist[i];
209                         goto assemble;
210                 } else
211                         assource = tmp3;
212                 if (pflag)
213                         tmp4 = setsuf(clist[i], 'i');
214                 av[0] = "cpp"av[1] = clist[i];
215                 na = 2;
216                 if (!exflag)
217                         av[na++] = tmp4;
218                 for (j = 0j < npj++)
219                         av[na++] = plist[j];
220                 av[na++] = 0;
221                 if (callsys(cppav)) {
222                         exfail++;
223                         eflag++;
224                         cflag++;
225                         continue;
226                 }
227                 if (pflag) {
228                         cflag++;
229                         continue;
230                 }
231                 if (sflag) {
232                         if (nc==1 && outfile)
233                                 tmp3 = outfile;
234                         else
235                                 tmp3 = setsuf(clist[i], 's');
236                         assource = tmp3;
237                 }
ragge
1.2
238                 av[0] = "ccom";
ragge
1.1
239                 av[1] = tmp4av[2] = oflag?tmp5:tmp3na = 3;
240                 if (proflag)
241                         av[na++] = "-XP";
242                 if (gflag) {
243                         av[na++] = "-Xg";
244                 } else if (Gflag) {
245                         av[na++] = "-XG";
246                 }
247                 if (wflag)
248                         av[na++] = "-w";
249                 av[na] = 0;
ragge
1.2
250                 if (callsys(ccomav)) {
ragge
1.1
251                         cflag++;
252                         eflag++;
253                         continue;
254                 }
255                 if (oflag) {
256                         av[0] = "c2"av[1] = tmp5av[2] = tmp3av[3] = 0;
257                         if (callsys(c2av)) {
258                                 unlink(tmp3);
259                                 tmp3 = assource = tmp5;
260                         } else
261                                 unlink(tmp5);
262                 }
263                 if (sflag)
264                         continue;
265         assemble:
266                 cunlink(tmp1); cunlink(tmp2); cunlink(tmp4);
267                 av[0] = "as"av[1] = "-o";
268                 if (cflag && nc==1 && outfile)
269                         av[2] = outfile;
270                 else
271                         av[2] = setsuf(clist[i], 'o');
272                 na = 3;
273                 if (Rflag)
274                         av[na++] = "-R";
275                 if (dflag)
276                         av[na++] = dflag;
277                 av[na++] = assource;
278                 av[na] = 0;
279                 if (callsys(asav) > 1) {
280                         cflag++;
281                         eflag++;
282                         continue;
283                 }
284         }
285 nocom:
286         if (cflag==0 && nl!=0) {
287                 i = 0;
288                 av[0] = "ld"av[1] = "-X"av[2] = crt0na = 3;
289                 if (outfile) {
290                         av[na++] = "-o";
291                         av[na++] = outfile;
292                 }
293                 while (i < nl)
294                         av[na++] = llist[i++];
295                 if (proflag)
296                         av[na++] = "-lc_p";
297                 else
298                         av[na++] = "-lc";
299                 av[na++] = 0;
300                 eflag |= callsys(ldav);
301                 if (nc==1 && nxo==1 && eflag==0)
302                         unlink(setsuf(clist[0], 'o'));
303         }
304         dexit();
ragge
1.2
305         return 0/* not reached */
ragge
1.1
306 }
307
ragge
1.2
308 void
309 idexit(int a)
ragge
1.1
310 {
311
312         eflag = 100;
313         dexit();
314 }
315
ragge
1.2
316 void
317 dexit(void)
ragge
1.1
318 {
319
320         if (!pflag) {
321                 cunlink(tmp1);
322                 cunlink(tmp2);
323                 if (sflag==0)
324                         cunlink(tmp3);
325                 cunlink(tmp4);
326                 cunlink(tmp5);
327         }
328         exit(eflag);
329 }
330
ragge
1.2
331 void
332 error(char *schar *x)
ragge
1.1
333 {
334         FILE *diag = exflag ? stderr : stdout;
335
336         fprintf(diag"cc: ");
337         fprintf(diagsx);
338         putc('\n'diag);
339         exfail++;
340         cflag++;
341         eflag++;
342 }
343
ragge
1.2
344 int
345 getsuf(char as[])
ragge
1.1
346 {
347         register int c;
348         register char *s;
349         register int t;
350
351         s = as;
352         c = 0;
ragge
1.2
353         while ((t = *s++))
ragge
1.1
354                 if (t=='/')
355                         c = 0;
356                 else
357                         c++;
358         s -= 3;
359         if (c <= MAXNAMLEN && c > 2 && *s++ == '.')
360                 return (*s);
361         return (0);
362 }
363
364 char *
ragge
1.2
365 setsuf(char *asint ch)
ragge
1.1
366 {
367         register char *s, *s1;
368
369         s = s1 = savestr(as);
370         while (*s)
371                 if (*s++ == '/')
372                         s1 = s;
373         s[-1] = ch;
374         return (s1);
375 }
376
ragge
1.2
377 int
378 callsys(char *fchar **v)
ragge
1.1
379 {
380         int tstatus;
381         char **cpp;
382
383         if (debug) {
384                 fprintf(stderr"%s:"f);
385                 for (cpp = v; *cpp != 0cpp++)
386                         fprintf(stderr" %s", *cpp);
387                 fprintf(stderr"\n");
388         }
389         t = vfork();
390         if (t == -1) {
391                 printf("No more processes\n");
392                 return (100);
393         }
394         if (t == 0) {
395                 execv(fv);
396                 printf("Can't find %s\n"f);
397                 fflush(stdout);
398                 _exit(100);
399         }
400         while (t != wait(&status))
401                 ;
402         if ((t=(status&0377)) != 0 && t!=14) {
403                 if (t!=2) {
404                         printf("Fatal error in %s\n"f);
405                         eflag = 8;
406                 }
407                 dexit();
408         }
409         return ((status>>8) & 0377);
410 }
411
ragge
1.2
412 int
413 nodup(char **lchar *os)
ragge
1.1
414 {
415         register char *t, *s;
416         register int c;
417
418         s = os;
419         if (getsuf(s) != 'o')
420                 return (1);
ragge
1.2
421         while ((t = *l++)) {
422                 while ((c = *s++))
ragge
1.1
423                         if (c != *t++)
424                                 break;
425                 if (*t==0 && c==0)
426                         return (0);
427                 s = os;
428         }
429         return (1);
430 }
431
432 #define NSAVETAB        1024
433 char    *savetab;
434 int     saveleft;
435
436 char *
ragge
1.2
437 savestr(char *cp)
ragge
1.1
438 {
439         register int len;
440
441         len = strlen(cp) + 1;
442         if (len > saveleft) {
443                 saveleft = NSAVETAB;
444                 if (len > saveleft)
445                         saveleft = len;
446                 savetab = (char *)malloc(saveleft);
447                 if (savetab == 0) {
448                         fprintf(stderr"ran out of memory (savestr)\n");
449                         exit(1);
450                 }
451         }
452         strncpy(savetabcplen);
453         cp = savetab;
454         savetab += len;
455         saveleft -= len;
456         return (cp);
457 }
458
459 char *
ragge
1.2
460 strspl(char *leftchar *right)
ragge
1.1
461 {
462         char buf[BUFSIZ];
463
464         strcpy(bufleft);
465         strcat(bufright);
466         return (savestr(buf));
467 }
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 18:55 +0100