1: # 2: # include "../mcons.h" 3: # include "../ccmn.h" 4: /*int mbuf[1024]; /*INSTR*/ 5: /*int tbuf[36]; /*INSTR*/ 6: 7: int (*acts[])() {0, 8: &coll, 9: &save, 10: &out, 11: &asym, 12: &asw, 13: &csym, 14: &csw, 15: &incl, 16: &decl, 17: &sk2, 18: &sk, 19: &tabs, 20: &semi 21: }; 22: 23: char *tmp[5] {"/tmp/crt0a", 24: "/tmp/crt1a", 25: "/tmp/crt2a", 26: "/tmp/crt3a", 27: "/tmp/crt4a" 28: }; 29: 30: char *ignonl "/usr/lib/aign"; 31: char *gtab "/usr/lib/atab"; 32: 33: main(argc,argv) 34: char *argv[]; 35: { 36: auto i,j,tm1,tm2,tm3; 37: char *fn,*av[8]; 38: /* extern etext; /*INSTR*/ 39: 40: /* monitor(&main,&etext,&mbuf,1024); /*INSTR*/ 41: if(argc < 2) { 42: printf("Usage: cref [-aceilosux] file1 ...\n"); 43: exit(); 44: } 45: 46: lbuf[4] = '\t'; 47: if(*argv[1] == '-') { 48: j = flags(argv); 49: argv =+ j; 50: argc =- j; 51: } 52: 53: init(); 54: 55: i = 0; 56: while(++i < argc) { 57: curs[0] = '_'; 58: curs[1] = '\t'; 59: curs[4] = '\t'; 60: cursl = 2; 61: 62: if(fopen(argv[i],ibuf1) < 0) { 63: printf("Can't open %s\n",argv[i]); 64: dexit(); 65: } 66: ibuf = ibuf1; 67: 68: curf[0] = '\t'; 69: curfl = 1; 70: while((curf[curfl] = *argv[i]++) != 0 && curfl <= 8) 71: if(curf[curfl++] == '/') curfl = 1; 72: 73: curf[curfl++] = '\t'; 74: if(curfl == 10) curf[9] = -1; 75: /* printf("%s %d\n",curf,curfl);/*DEBUG*/ 76: 77: lno = 1; 78: 79: driver(); 80: 81: close(file); 82: } 83: for(j = 0; j < 4;) { 84: flsh(j,0); 85: close(tp[j++]); 86: } 87: 88: /* monitor(0); /*INSTR*/ 89: /* dexit(); /*INSTR*/ 90: 91: /* times(tbuf); /*INSTR*/ 92: /* tm1 = tbuf[15]/6; /*INSTR*/ 93: /* tm2 = tbuf[19]/6; /*INSTR*/ 94: /* tm3 = tbuf[23]/6; /*INSTR*/ 95: /* printf("Prep: %d %d %d\n",tm1,tm2,tm3); /*INSTR*/ 96: 97: if(utmp) exit(); 98: fn = "/bin/sort"; 99: av[0] = "sort"; 100: av[1] = "-a"; 101: av[3] = "-o"; 102: av[5] = 0; 103: for(i = 0; i < 4; i++) { 104: av[4] = av[2] = tmp[i]; 105: callsys(fn,av); 106: if(utmp) break; 107: } 108: 109: /* times(tbuf); /*INSTR*/ 110: /* tm1 = tbuf[27]/6; /*INSTR*/ 111: /* tm2 = tbuf[31]/6; /*INSTR*/ 112: /* tm3 = tbuf[35]/6; /*INSTR*/ 113: /* printf("Sort: %d %d %d\n",tm1,tm2,tm3); /*INSTR*/ 114: 115: if(usw) { 116: fn = "/usr/bin/upost"; 117: av[0] = "upost"; 118: i = 0; 119: } else { 120: fn = "/usr/bin/crpost"; 121: av[0] = "crpost"; 122: av[1] = cross? "-4x": "-3"; 123: i = 1; 124: } 125: j = -1; 126: while(++j < 4) { 127: av[++i] = tmp[j]; 128: if(utmp) break; 129: } 130: av[++i] = 0; 131: 132: callsys(fn,av); 133: 134: /* times(tbuf); /*INSTR*/ 135: /* tm1 = tbuf[27]/6 - tm1; /*INSTR*/ 136: /* tm2 = tbuf[31]/6 - tm2; /*INSTR*/ 137: /* tm3 = tbuf[35]/6 - tm3; /*INSTR*/ 138: /* printf("Post: %d %d %d\n",tm1,tm2,tm3); /*INSTR*/ 139: 140: dexit(); 141: } 142: 143: driver() 144: { 145: auto p; 146: 147: top: 148: l = -1; 149: while((c = line[++l] = getc(ibuf)) != -1) { 150: if(l >= 131) { 151: printf("Line too long: %d %s\n",lno,curf); 152: dexit(); 153: } 154: 155: if(c & 0200) { 156: printf("Illegal character: %o line %d\n",c,lno); 157: dexit(); 158: } 159: 160: if(fl) { 161: if((*flag[fl])()) 162: continue; 163: } 164: 165: /*printf("cs = %d cc = %c ca = %d\n",cs,c,tab[cs].cl[c]); /*DEBUG*/ 166: 167: if(p = tab[cs].cl[c]) 168: (*acts[p])(); 169: continue; 170: } 171: if(ibuf == ibuf1) return; 172: ibuf = ibuf1; 173: goto top; 174: 175: } 176: 177: init() 178: { 179: int b[3]; 180: auto fi,i; 181: extern coll(),save(),out(),asym(),asw(),csym(),csw(); 182: extern incl(),decl(),sk(),sk2(); 183: extern dexit(); 184: 185: ibuf1 = &ib1; 186: ibuf2 = &ib2; 187: 188: xtab.hptr = &xpsp; 189: xtab.symt = &xssp; 190: xtab.hsiz = PTRX; 191: xtab.ssiz = CHARX; 192: xtab.nsym = 0; 193: xtab.curb = 1; 194: 195: itab.hptr = &ipsp; 196: itab.symt = &issp; 197: itab.hsiz = PTRI; 198: itab.ssiz = CHARI; 199: itab.nsym = 0; 200: itab.curb = 1; 201: 202: if((fi = open(gtab,0)) < 0) { 203: printf("Cannot open grammar table; see lem\n"); 204: dexit(); 205: } 206: 207: i = -1; 208: while(++i < NUMS) 209: if(read(fi,tab[i].cl,256) < 256) { 210: printf("Bad grammar table.\n"); 211: dexit(); 212: } 213: 214: close(fi); 215: 216: 217: if((fi = open(ignonl,0)) < 0) { 218: printf("Cannot open ignore/only file: %s\n",ignonl); 219: dexit(); 220: } 221: if((read(fi,b,6) == 6) && (b[0] == 0100200)) { 222: if(read(fi,itab.hptr,b[1]) < b[1]) { 223: printf("Cannot read ignore/only file: %s\n",ignonl); 224: dexit(); 225: } 226: if(read(fi,itab.symt,b[2]) < b[2]) { 227: printf("Cannot read ignore/only file: %s\n",ignonl); 228: dexit(); 229: } 230: close(fi); 231: } else { 232: close(fi); 233: compile(); 234: } 235: 236: if((signal(1,1) & 1) == 0) signal(1,&dexit); 237: if((signal(2,1) & 1) == 0) signal(2,&dexit); 238: if((signal(3,1) & 1) == 0) signal(3,&dexit); 239: /* signal(4,&dexit); 240: signal(5,&dexit); 241: signal(6,&dexit); 242: signal(7,&dexit); 243: signal(8,&dexit); 244: signal(10,&dexit); 245: signal(11,&dexit); 246: signal(12,&dexit); 247: */ 248: if(utmp == 0) { 249: while((tp[4] = creat(tmp[4],0)) < 0) 250: tmp[4][9]++; 251: close(tp[4]); 252: tmp[0][9] = tmp[4][9]; 253: tmp[1][9] = tmp[4][9]; 254: tmp[2][9] = tmp[4][9]; 255: tmp[3][9] = tmp[4][9]; 256: tp[0] = creat(tmp[0],CREATC); 257: tp[1] = creat(tmp[1],CREATC); 258: tp[2] = creat(tmp[2],CREATC); 259: tp[3] = creat(tmp[3],CREATC); 260: } else { 261: if((tp[0] = creat(utmp,CREATC)) < 0) { 262: printf("Can't create user's temp file.\n"); 263: exit(); 264: } 265: } 266: 267: return; 268: } 269: 270: error(a) 271: { 272: printf("Error %d\n",a); 273: dexit(); 274: } 275: 276: dexit() 277: { 278: extern nflush; 279: 280: /* printf("nflush = %d\n",nflush); /*DEBUG*/ 281: if(tp[0] > 0 && utmp == 0) { 282: unlink(tmp[0]); 283: unlink(tmp[1]); 284: unlink(tmp[2]); 285: unlink(tmp[3]); 286: unlink(tmp[4]); 287: } 288: exit(); 289: } 290: 291: callsys(f,v) 292: char f[],*v[]; 293: { 294: int t,status,i; 295: 296: if((t = fork()) == 0) { 297: execv(f,v); 298: printf("Can't find %s\n",f); 299: exit(1); 300: } else { 301: if(t == -1) { 302: printf("Try again\n"); 303: return(1); 304: } 305: } 306: 307: while(t != wait(&status)); 308: /* printf("Status = %o, %s\n",status,f); /*DEBUG*/ 309: if((t = (status & 0377)) != 0) { 310: if(t != 2) { 311: printf("Fatal error in %s\n",f); 312: printf("t = %d\n",t); 313: } 314: dexit(); 315: } 316: return((status>>8) & 0377); 317: } 318: 319: flags(argv) 320: char *argv[]; 321: { 322: int j,xx; 323: char *ap; 324: 325: j = 1; 326: ap = argv[1]; 327: while(*++ap != '\0') { 328: switch(*ap) { 329: 330: default: 331: printf("Unrecognized flag: %c\n",*ap); 332: dexit(); 333: 334: case '1': /* Symbol first */ 335: order = 1; 336: continue; 337: 338: case '2': /* Current file first */ 339: order = 2; 340: continue; 341: 342: case '3': /* Current symbol first */ 343: order = 3; 344: continue; 345: 346: case 'a': /* Assembler */ 347: cflag = 0; 348: continue; 349: 350: case 'c': /* C */ 351: gtab = "/usr/lib/ctab"; 352: if(!xx) 353: ignonl = "/usr/lib/cign"; 354: cflag = 1; 355: continue; 356: 357: case 'e': /* English */ 358: gtab = "/usr/lib/etab"; 359: if(!xx) 360: ignonl = "/usr/lib/eign"; 361: continue; 362: 363: case 'i': /* Ignore file */ 364: if(!xx) { 365: xx = 1; 366: only = 0; 367: ignonl = argv[++j]; 368: } 369: continue; 370: 371: case 'l': /* Line numbers in col. 3 */ 372: cross = 0; 373: continue; 374: 375: case 'o': /* Only file */ 376: if(!xx) { 377: xx = 1; 378: only = 1; 379: ignonl = argv[++j]; 380: } 381: continue; 382: 383: case 's': /* Symbols in col. 3 */ 384: cross = 1; 385: continue; 386: 387: case 't': 388: utmp = argv[++j]; 389: tmp[0] = argv[j]; 390: continue; 391: 392: case 'u': /* Unique symbols only */ 393: usw = 1; 394: continue; 395: 396: case 'x': /* C externals */ 397: xsw = 1; 398: gtab = "/usr/lib/ctab"; 399: if(!xx) 400: ignonl = "/usr/lib/cign"; 401: cflag = 1; 402: continue; 403: } 404: } 405: return(j); 406: } 407: 408: 409: compile() 410: { 411: char buf[40],*b; 412: int i,v; 413: 414: fopen(ignonl,ibuf1); 415: 416: b = buf - 1; 417: while((*++b = getc(ibuf1)) != -1) { 418: if(*b == '\n') { 419: *b = '\0'; 420: search(buf,b - buf,&itab,1); 421: b = buf - 1; 422: } else { 423: if(*b == '\t') { 424: v = 0; 425: while((i = getc(ibuf1)) != -1) { 426: if(i == '\n') break; 427: v = v*10 + (i - '0'); 428: } 429: search(buf,b - buf,&itab,v); 430: b = buf - 1; 431: } else { 432: if((b - buf) > 39) { 433: printf("Ignore/only symbol too long"); 434: dexit(); 435: } 436: } 437: } 438: } 439: close(ibuf1); 440: return; 441: }