1 static char sccsid[] = "@(#)pc.c 3.14 07/19/82"; 2 3 #include <stdio.h> 4 #include <signal.h> 5 #include <wait.h> 6 7 /* 8 * Pc - front end for Pascal compiler. 9 */ 10 char *pc0 = "/usr/lib/pc0"; 11 char *pc1 = "/lib/f1"; 12 char *pc2 = "/usr/lib/pc2"; 13 char *c2 = "/lib/c2"; 14 char *pc3 = "/usr/lib/pc3"; 15 char *ld = "/bin/ld"; 16 char *as = "/bin/as"; 17 char *lpc = "-lpc"; 18 char *crt0 = "/lib/crt0.o"; 19 char *mcrt0 = "/lib/mcrt0.o"; 20 char *gcrt0 = "/usr/lib/gcrt0.o"; 21 22 char *mktemp(); 23 char *tname[2]; 24 char *tfile[2]; 25 26 char *setsuf(), *savestr(); 27 28 int Jflag, Sflag, Oflag, Tlflag, cflag, gflag, pflag; 29 int debug; 30 31 #define NARGS 512 32 int ldargx = 3; 33 int pc0argx = 3; 34 char *pc0args[NARGS] = { "pc0", "-o", "XXX" }; 35 char *pc1args[3] = { "pc1", 0, }; 36 char *pc2args[2] = { "pc2", 0 }; 37 char *c2args[4] = { "c2", 0, 0, 0 }; 38 int pc3argx = 2; 39 #define pc3args pc0args 40 #define ldargs pc0args 41 /* char *pc3args[NARGS] = { "pc3", /usr/lib/pcexterns.o, 0 }; */ 42 /* char *ldargs[NARGS] = { "ld", "-X", "/lib/crt0.o", 0, }; */ 43 int asargx; 44 char *asargs[6] = { "as", 0, }; 45 46 /* 47 * If the number of .p arguments (np) is 1, and the number of .o arguments 48 * (nxo) is 0, and we successfully create an ``a.out'', then we remove 49 * the one .ps .o file (onepso). 50 */ 51 int np, nxo; 52 char *onepso; 53 int errs; 54 55 int onintr(); 56 57 main(argc, argv) 58 int argc; 59 char **argv; 60 { 61 register char *argp; 62 register int i; 63 int savargx; 64 char *t, c; 65 int j; 66 67 argc--, argv++; 68 if (argc == 0) { 69 execl("/bin/cat", "cat", "/usr/lib/how_pc"); 70 exit(1); 71 } 72 if (signal(SIGINT, SIG_IGN) != SIG_IGN) { 73 signal(SIGINT, onintr); 74 signal(SIGTERM, onintr); 75 } 76 for (i = 0; i < argc; i++) { 77 argp = argv[i]; 78 if (argp[0] != '-') 79 continue; 80 switch (argp[1]) { 81 82 case 'd': 83 if (argp[2] == 0) 84 debug++; 85 continue; 86 case 'i': 87 pc0args[pc0argx++] = "-i"; 88 while (i+1 < argc && argv[i+1][0] != '-' && 89 getsuf(argv[i+1]) != 'p') { 90 pc0args[pc0argx++] = argv[i+1]; 91 i++; 92 } 93 if (i+1 == argc) { 94 fprintf(stderr, "pc: bad -i construction\n"); 95 exit(1); 96 } 97 continue; 98 case 'o': 99 i++; 100 if (i == argc) { 101 fprintf(stderr, "pc: -o must specify file\n"); 102 exit(1); 103 } 104 c = getsuf(argv[i]); 105 if (c == 'o' || c == 'p' || c == 'c') { 106 fprintf(stderr, "pc: -o would overwrite %s\n", 107 argv[i]); 108 exit(1); 109 } 110 continue; 111 case 'O': 112 Oflag = 1; 113 continue; 114 case 'S': 115 Sflag = 1; 116 continue; 117 case 'J': 118 Jflag = 1; 119 continue; 120 case 'T': 121 switch (argp[2]) { 122 123 case '0': 124 pc0 = "/usr/src/cmd/pc0/a.out"; 125 if (argp[3] != '\0') { 126 pc0 = &argp[3]; 127 } 128 continue; 129 case '1': 130 pc1 = "/usr/src/cmd/pcc/pc1"; 131 if (argp[3] != '\0') { 132 pc1 = &argp[3]; 133 } 134 continue; 135 case '2': 136 pc2 = "/usr/src/cmd/pascal/pc2"; 137 if (argp[3] != '\0') { 138 pc2 = &argp[3]; 139 } 140 continue; 141 case '3': 142 pc3 = "/usr/src/cmd/pascal/pc3"; 143 if (argp[3] != '\0') { 144 pc3 = &argp[3]; 145 } 146 continue; 147 case 'l': 148 Tlflag = 1; 149 lpc = "/usr/src/lib/libpc/libpc"; 150 if (argp[3] != '\0') { 151 lpc = &argp[3]; 152 } 153 continue; 154 } 155 continue; 156 case 'c': 157 cflag = 1; 158 continue; 159 case 'l': 160 if (argp[2]) 161 continue; 162 /* fall into ... */ 163 case 'b': 164 case 'g': 165 case 's': 166 case 'w': 167 case 'z': 168 case 'C': 169 pc0args[pc0argx++] = argp; 170 if (argp[1] == 'g') 171 gflag = 1; 172 continue; 173 case 't': 174 fprintf(stderr, "pc: -t is default; -C for checking\n"); 175 continue; 176 case 'p': 177 if (argp[2] == 'g') 178 crt0 = gcrt0; 179 else 180 crt0 = mcrt0; 181 if (!Tlflag) 182 lpc = "-lpc_p"; 183 pflag = 1; 184 continue; 185 } 186 } 187 if (gflag && Oflag) { 188 fprintf(stderr, "pc: warning: -g overrides -O\n"); 189 Oflag = 0; 190 } 191 tname[0] = mktemp("/tmp/p0XXXXXX"); 192 tname[1] = mktemp("/tmp/p1XXXXXX"); 193 savargx = pc0argx; 194 for (i = 0; i < argc; i++) { 195 argp = argv[i]; 196 if (argp[0] == '-') 197 continue; 198 if (suffix(argp) == 's') { 199 asargx = 1; 200 if (Jflag) 201 asargs[asargx++] = "-J"; 202 asargs[asargx++] = argp; 203 asargs[asargx++] = "-o"; 204 tfile[1] = setsuf(argp, 'o'); 205 asargs[asargx++] = tfile[1]; 206 asargs[asargx] = 0; 207 if (dosys(as, asargs, 0, 0)) 208 continue; 209 tfile[1] = 0; 210 continue; 211 } 212 if (suffix(argp) != 'p') 213 continue; 214 tfile[0] = tname[0]; 215 pc0args[2] = tfile[0]; 216 pc0argx = savargx; 217 if (pflag) 218 pc0args[pc0argx++] = "-p"; 219 pc0args[pc0argx++] = argp; 220 pc0args[pc0argx] = 0; 221 if (dosys(pc0, pc0args, 0, 0)) 222 continue; 223 pc1args[1] = tfile[0]; 224 tfile[1] = tname[1]; 225 if (dosys(pc1, pc1args, 0, tfile[1])) 226 continue; 227 unlink(tfile[0]); 228 tfile[0] = tname[0]; 229 if (Oflag) { 230 if (dosys(c2, c2args, tfile[1], tfile[0])) 231 continue; 232 unlink(tfile[1]); 233 tfile[1] = tfile[0]; 234 tfile[0] = tname[1]; 235 } 236 if (Sflag) 237 tfile[0] = setsuf(argp, 's'); 238 if (dosys(pc2, pc2args, tfile[1], tfile[0])) 239 continue; 240 unlink(tfile[1]); 241 tfile[1] = 0; 242 if (Sflag) { 243 tfile[0] = 0; 244 continue; 245 } 246 asargx = 1; 247 if (Jflag) 248 asargs[asargx++] = "-J"; 249 asargs[asargx++] = tfile[0]; 250 asargs[asargx++] = "-o"; 251 tfile[1] = setsuf(argp, 'o'); 252 asargs[asargx++] = tfile[1]; 253 asargs[asargx] = 0; 254 if (dosys(as, asargs, 0, 0)) 255 continue; 256 tfile[1] = 0; 257 remove(); 258 } 259 if (errs || cflag || Sflag) 260 done(); 261 /* char *pc3args[NARGS] = { "pc3", "/usr/lib/pcexterns.o", 0 }; */ 262 pc3args[0] = "pc3"; 263 pc3args[1] = "/usr/lib/pcexterns.o"; 264 for (i = 0; i < argc; i++) { 265 argp = argv[i]; 266 if (!strcmp(argp, "-o")) 267 i++; 268 if (argp[0] == '-') 269 continue; 270 switch (getsuf(argp)) { 271 272 case 'o': 273 pc3args[pc3argx++] = argp; 274 nxo++; 275 continue; 276 case 's': 277 case 'p': 278 onepso = pc3args[pc3argx++] = 279 savestr(setsuf(argp, 'o')); 280 np++; 281 continue; 282 } 283 } 284 pc3args[pc3argx] = 0; 285 if (dosys(pc3, pc3args, 0, 0)) 286 done(); 287 /* char *ldargs[NARGS] = { "ld", "-X", "/lib/crt0.o", 0, }; */ 288 ldargs[0] = "ld"; 289 ldargs[1] = "-X"; 290 ldargs[2] = crt0; 291 for (i = 0; i < argc; i++) { 292 argp = argv[i]; 293 if (argp[0] != '-') { 294 switch (getsuf(argp)) { 295 296 case 'p': 297 case 's': 298 ldargs[ldargx] = savestr(setsuf(argp, 'o')); 299 break; 300 default: 301 ldargs[ldargx] = argp; 302 break; 303 } 304 if (getsuf(ldargs[ldargx]) == 'o') 305 for (j = 0; j < ldargx; j++) 306 if (!strcmp(ldargs[j], ldargs[ldargx])) 307 goto duplicate; 308 ldargx++; 309 duplicate: 310 continue; 311 } 312 switch (argp[1]) { 313 314 case 'i': 315 while (i+1 < argc && argv[i+1][0] != '-' && 316 getsuf(argv[i+1]) != 'p') 317 i++; 318 continue; 319 case 'd': 320 if (argp[2] == 0) 321 continue; 322 ldargs[ldargx++] = argp; 323 continue; 324 case 'o': 325 ldargs[ldargx++] = argp; 326 i++; 327 ldargs[ldargx++] = argv[i]; 328 continue; 329 case 'l': 330 if (argp[2]) 331 ldargs[ldargx++] = argp; 332 continue; 333 case 'c': 334 case 'g': 335 case 'w': 336 case 'p': 337 case 'S': 338 case 'J': 339 case 'T': 340 case 'O': 341 case 'C': 342 case 'b': 343 case 's': 344 case 'z': 345 continue; 346 default: 347 ldargs[ldargx++] = argp; 348 continue; 349 } 350 } 351 ldargs[ldargx++] = lpc; 352 if (gflag) 353 ldargs[ldargx++] = "-lg"; 354 if (pflag) { 355 ldargs[ldargx++] = "-lm_p"; 356 ldargs[ldargx++] = "-lc_p"; 357 } else { 358 ldargs[ldargx++] = "-lm"; 359 ldargs[ldargx++] = "-lc"; 360 } 361 ldargs[ldargx] = 0; 362 if (dosys(ld, ldargs, 0, 0)==0 && np == 1 && nxo == 0) 363 unlink(onepso); 364 done(); 365 } 366 367 dosys(cmd, argv, in, out) 368 char *cmd, **argv, *in, *out; 369 { 370 union wait status; 371 int pid; 372 373 if (debug) { 374 int i; 375 printf("%s:", cmd); 376 for (i = 0; argv[i]; i++) 377 printf(" %s", argv[i]); 378 if (in) 379 printf(" <%s", in); 380 if (out) 381 printf(" >%s", out); 382 printf("\n"); 383 } 384 pid = vfork(); 385 if (pid < 0) { 386 fprintf(stderr, "pc: No more processes\n"); 387 done(); 388 } 389 if (pid == 0) { 390 if (in) { 391 close(0); 392 if (open(in, 0) != 0) { 393 perror(in); 394 exit(1); 395 } 396 } 397 if (out) { 398 close(1); 399 unlink(out); 400 if (creat(out, 0666) != 1) { 401 perror(out); 402 exit(1); 403 } 404 } 405 signal(SIGINT, SIG_DFL); 406 execv(cmd, argv); 407 perror(cmd); 408 exit(1); 409 } 410 while (wait(&status) != pid) 411 ; 412 if (WIFSIGNALED(status)) { 413 if (status.w_termsig != SIGINT) 414 fprintf(stderr, "Fatal error in %s\n", cmd); 415 errs = 100; 416 done(); 417 /*NOTREACHED*/ 418 } 419 if (status.w_retcode) { 420 errs = 1; 421 remove(); 422 } 423 return (status.w_retcode); 424 } 425 426 done() 427 { 428 429 remove(); 430 exit(errs); 431 } 432 433 remove() 434 { 435 436 if (tfile[0]) 437 unlink(tfile[0]); 438 if (tfile[1]) 439 unlink(tfile[1]); 440 } 441 442 onintr() 443 { 444 445 errs = 1; 446 done(); 447 } 448 449 getsuf(cp) 450 char *cp; 451 { 452 453 if (*cp == 0) 454 return; 455 while (cp[1]) 456 cp++; 457 if (cp[-1] != '.') 458 return (0); 459 return (*cp); 460 } 461 462 char * 463 setsuf(as, ch) 464 char *as; 465 { 466 register char *s, *s1; 467 468 s = s1 = savestr(as); 469 while (*s) 470 if (*s++ == '/') 471 s1 = s; 472 s[-1] = ch; 473 return (s1); 474 } 475 476 #define NSAVETAB 512 477 char *savetab; 478 int saveleft; 479 480 char * 481 savestr(cp) 482 register char *cp; 483 { 484 register int len; 485 486 len = strlen(cp) + 1; 487 if (len > saveleft) { 488 saveleft = NSAVETAB; 489 if (len > saveleft) 490 saveleft = len; 491 savetab = (char *)malloc(saveleft); 492 if (savetab == 0) { 493 fprintf(stderr, "ran out of memory (savestr)\n"); 494 exit(1); 495 } 496 } 497 strncpy(savetab, cp, len); 498 cp = savetab; 499 savetab += len; 500 return (cp); 501 } 502 503 suffix(cp) 504 char *cp; 505 { 506 507 if (cp[0] == 0 || cp[1] == 0) 508 return (0); 509 while (cp[1]) 510 cp++; 511 if (cp[-1] == '.') 512 return (*cp); 513 return (0); 514 } 515