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