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