110927Srrh #ifndef lint 2*27447Slepreau static char sccsid[] = "@(#)dc.c 4.3 (Berkeley) 04/26/86"; 310927Srrh #endif not lint 410927Srrh 510927Srrh #include <stdio.h> 610927Srrh #include <signal.h> 710927Srrh #include "dc.h" 810927Srrh main(argc,argv) 910927Srrh int argc; 1010927Srrh char *argv[]; 1110927Srrh { 1210927Srrh init(argc,argv); 1310927Srrh commnds(); 1410927Srrh } 1510927Srrh commnds(){ 1610927Srrh register int c; 1710927Srrh register struct blk *p,*q; 1810927Srrh long l; 1910927Srrh int sign; 2010927Srrh struct blk **ptr,*s,*t; 2110927Srrh struct sym *sp; 2210927Srrh int sk,sk1,sk2; 2310927Srrh int n,d; 2410927Srrh 2510927Srrh while(1){ 2610927Srrh if(((c = readc())>='0' && c <= '9')|| (c>='A' && c <='F') || c == '.'){ 2710927Srrh unreadc(c); 2810927Srrh p = readin(); 2910927Srrh pushp(p); 3010927Srrh continue; 3110927Srrh } 3210927Srrh switch(c){ 3310927Srrh case ' ': 3410927Srrh case '\n': 3510927Srrh case 0377: 3610927Srrh case EOF: 3710927Srrh continue; 3810927Srrh case 'Y': 3910927Srrh sdump("stk",*stkptr); 4010927Srrh printf("all %ld rel %ld headmor %ld\n",all,rel,headmor); 4110927Srrh printf("nbytes %ld\n",nbytes); 4210927Srrh continue; 4310927Srrh case '_': 4410927Srrh p = readin(); 4510927Srrh savk = sunputc(p); 4610927Srrh chsign(p); 4710927Srrh sputc(p,savk); 4810927Srrh pushp(p); 4910927Srrh continue; 5010927Srrh case '-': 5110927Srrh subt(); 5210927Srrh continue; 5310927Srrh case '+': 5410927Srrh if(eqk() != 0)continue; 5510927Srrh binop('+'); 5610927Srrh continue; 5710927Srrh case '*': 5810927Srrh arg1 = pop(); 5910927Srrh EMPTY; 6010927Srrh arg2 = pop(); 6110927Srrh EMPTYR(arg1); 6210927Srrh sk1 = sunputc(arg1); 6310927Srrh sk2 = sunputc(arg2); 6410927Srrh binop('*'); 6510927Srrh p = pop(); 6610927Srrh sunputc(p); 6710927Srrh savk = sk1+sk2; 6810927Srrh if(savk>k && savk>sk1 && savk>sk2){ 6910927Srrh sk = sk1; 7010927Srrh if(sk<sk2)sk = sk2; 7110927Srrh if(sk<k)sk = k; 7210927Srrh p = removc(p,savk-sk); 7310927Srrh savk = sk; 7410927Srrh } 7510927Srrh sputc(p,savk); 7610927Srrh pushp(p); 7710927Srrh continue; 7810927Srrh case '/': 7910927Srrh casediv: 8010927Srrh if(dscale() != 0)continue; 8110927Srrh binop('/'); 8210927Srrh if(irem != 0)release(irem); 8310927Srrh release(rem); 8410927Srrh continue; 8510927Srrh case '%': 8610927Srrh if(dscale() != 0)continue; 8710927Srrh binop('/'); 8810927Srrh p = pop(); 8910927Srrh release(p); 9010927Srrh if(irem == 0){ 9110927Srrh sputc(rem,skr+k); 9210927Srrh pushp(rem); 9310927Srrh continue; 9410927Srrh } 9510927Srrh p = add0(rem,skd-(skr+k)); 9610927Srrh q = add(p,irem); 9710927Srrh release(p); 9810927Srrh release(irem); 9910927Srrh sputc(q,skd); 10010927Srrh pushp(q); 10110927Srrh continue; 10210927Srrh case 'v': 10310927Srrh p = pop(); 10410927Srrh EMPTY; 10510927Srrh savk = sunputc(p); 10610927Srrh if(length(p) == 0){ 10710927Srrh sputc(p,savk); 10810927Srrh pushp(p); 10910927Srrh continue; 11010927Srrh } 11110927Srrh if((c = sbackc(p))<0){ 11210927Srrh error("sqrt of neg number\n"); 11310927Srrh } 11410927Srrh if(k<savk)n = savk; 11510927Srrh else{ 11610927Srrh n = k*2-savk; 11710927Srrh savk = k; 11810927Srrh } 11910927Srrh arg1 = add0(p,n); 12010927Srrh arg2 = sqrt(arg1); 12110927Srrh sputc(arg2,savk); 12210927Srrh pushp(arg2); 12310927Srrh continue; 12410927Srrh case '^': 12510927Srrh neg = 0; 12610927Srrh arg1 = pop(); 12710927Srrh EMPTY; 12810927Srrh if(sunputc(arg1) != 0)error("exp not an integer\n"); 12910927Srrh arg2 = pop(); 13010927Srrh EMPTYR(arg1); 13110927Srrh if(sfbeg(arg1) == 0 && sbackc(arg1)<0){ 13210927Srrh neg++; 13310927Srrh chsign(arg1); 13410927Srrh } 13510927Srrh if(length(arg1)>=3){ 13610927Srrh error("exp too big\n"); 13710927Srrh } 13810927Srrh savk = sunputc(arg2); 13910927Srrh p = exp(arg2,arg1); 14010927Srrh release(arg2); 14110927Srrh rewind(arg1); 14210927Srrh c = sgetc(arg1); 14310927Srrh if(sfeof(arg1) == 0) 14410927Srrh c = sgetc(arg1)*100 + c; 14510927Srrh d = c*savk; 14610927Srrh release(arg1); 14710927Srrh if(neg == 0){ 14810927Srrh if(k>=savk)n = k; 14910927Srrh else n = savk; 15010927Srrh if(n<d){ 15110927Srrh q = removc(p,d-n); 15210927Srrh sputc(q,n); 15310927Srrh pushp(q); 15410927Srrh } 15510927Srrh else { 15610927Srrh sputc(p,d); 15710927Srrh pushp(p); 15810927Srrh } 15910927Srrh } 16010927Srrh else { 16110927Srrh sputc(p,d); 16210927Srrh pushp(p); 16310927Srrh } 16410927Srrh if(neg == 0)continue; 16510927Srrh p = pop(); 16610927Srrh q = salloc(2); 16710927Srrh sputc(q,1); 16810927Srrh sputc(q,0); 16910927Srrh pushp(q); 17010927Srrh pushp(p); 17110927Srrh goto casediv; 17210927Srrh case 'z': 17310927Srrh p = salloc(2); 17410927Srrh n = stkptr - stkbeg; 17510927Srrh if(n >= 100){ 17610927Srrh sputc(p,n/100); 17710927Srrh n %= 100; 17810927Srrh } 17910927Srrh sputc(p,n); 18010927Srrh sputc(p,0); 18110927Srrh pushp(p); 18210927Srrh continue; 18310927Srrh case 'Z': 18410927Srrh p = pop(); 18510927Srrh EMPTY; 18610927Srrh n = (length(p)-1)<<1; 18710927Srrh fsfile(p); 18810927Srrh sbackc(p); 18910927Srrh if(sfbeg(p) == 0){ 19010927Srrh if((c = sbackc(p))<0){ 19110927Srrh n -= 2; 19210927Srrh if(sfbeg(p) == 1)n += 1; 19310927Srrh else { 19410927Srrh if((c = sbackc(p)) == 0)n += 1; 19510927Srrh else if(c > 90)n -= 1; 19610927Srrh } 19710927Srrh } 19810927Srrh else if(c < 10) n -= 1; 19910927Srrh } 20010927Srrh release(p); 20110927Srrh q = salloc(1); 20210927Srrh if(n >= 100){ 20310927Srrh sputc(q,n%100); 20410927Srrh n /= 100; 20510927Srrh } 20610927Srrh sputc(q,n); 20710927Srrh sputc(q,0); 20810927Srrh pushp(q); 20910927Srrh continue; 21010927Srrh case 'i': 21110927Srrh p = pop(); 21210927Srrh EMPTY; 21310927Srrh p = scalint(p); 21410927Srrh release(inbas); 21510927Srrh inbas = p; 21610927Srrh continue; 21710927Srrh case 'I': 21810927Srrh p = copy(inbas,length(inbas)+1); 21910927Srrh sputc(p,0); 22010927Srrh pushp(p); 22110927Srrh continue; 22210927Srrh case 'o': 22310927Srrh p = pop(); 22410927Srrh EMPTY; 22510927Srrh p = scalint(p); 22610927Srrh sign = 0; 22710927Srrh n = length(p); 22810927Srrh q = copy(p,n); 22910927Srrh fsfile(q); 23010927Srrh l = c = sbackc(q); 23110927Srrh if(n != 1){ 23210927Srrh if(c<0){ 23310927Srrh sign = 1; 23410927Srrh chsign(q); 23510927Srrh n = length(q); 23610927Srrh fsfile(q); 23710927Srrh l = c = sbackc(q); 23810927Srrh } 23910927Srrh if(n != 1){ 24010927Srrh while(sfbeg(q) == 0)l = l*100+sbackc(q); 24110927Srrh } 24210927Srrh } 24310927Srrh logo = log2(l); 24410927Srrh obase = l; 24510927Srrh release(basptr); 24610927Srrh if(sign == 1)obase = -l; 24710927Srrh basptr = p; 24810927Srrh outdit = bigot; 24910927Srrh if(n == 1 && sign == 0){ 25010927Srrh if(c <= 16){ 25110927Srrh outdit = hexot; 25210927Srrh fw = 1; 25310927Srrh fw1 = 0; 25410927Srrh ll = 70; 25510927Srrh release(q); 25610927Srrh continue; 25710927Srrh } 25810927Srrh } 25910927Srrh n = 0; 26010927Srrh if(sign == 1)n++; 26110927Srrh p = salloc(1); 26210927Srrh sputc(p,-1); 26310927Srrh t = add(p,q); 26410927Srrh n += length(t)*2; 26510927Srrh fsfile(t); 26610927Srrh if((c = sbackc(t))>9)n++; 26710927Srrh release(t); 26810927Srrh release(q); 26910927Srrh release(p); 27010927Srrh fw = n; 27110927Srrh fw1 = n-1; 27210927Srrh ll = 70; 27310927Srrh if(fw>=ll)continue; 27410927Srrh ll = (70/fw)*fw; 27510927Srrh continue; 27610927Srrh case 'O': 27710927Srrh p = copy(basptr,length(basptr)+1); 27810927Srrh sputc(p,0); 27910927Srrh pushp(p); 28010927Srrh continue; 28110927Srrh case '[': 28210927Srrh n = 0; 28310927Srrh p = salloc(0); 28410927Srrh while(1){ 28510927Srrh if((c = readc()) == ']'){ 28610927Srrh if(n == 0)break; 28710927Srrh n--; 28810927Srrh } 28910927Srrh sputc(p,c); 29010927Srrh if(c == '[')n++; 29110927Srrh } 29210927Srrh pushp(p); 29310927Srrh continue; 29410927Srrh case 'k': 29510927Srrh p = pop(); 29610927Srrh EMPTY; 29710927Srrh p = scalint(p); 29810927Srrh if(length(p)>1){ 29910927Srrh error("scale too big\n"); 30010927Srrh } 30110927Srrh rewind(p); 30210927Srrh k = sfeof(p)?0:sgetc(p); 30310927Srrh release(scalptr); 30410927Srrh scalptr = p; 30510927Srrh continue; 30610927Srrh case 'K': 30710927Srrh p = copy(scalptr,length(scalptr)+1); 30810927Srrh sputc(p,0); 30910927Srrh pushp(p); 31010927Srrh continue; 31110927Srrh case 'X': 31210927Srrh p = pop(); 31310927Srrh EMPTY; 31410927Srrh fsfile(p); 31510927Srrh n = sbackc(p); 31610927Srrh release(p); 31710927Srrh p = salloc(2); 31810927Srrh sputc(p,n); 31910927Srrh sputc(p,0); 32010927Srrh pushp(p); 32110927Srrh continue; 32210927Srrh case 'Q': 32310927Srrh p = pop(); 32410927Srrh EMPTY; 32510927Srrh if(length(p)>2){ 32610927Srrh error("Q?\n"); 32710927Srrh } 32810927Srrh rewind(p); 32910927Srrh if((c = sgetc(p))<0){ 33010927Srrh error("neg Q\n"); 33110927Srrh } 33210927Srrh release(p); 33310927Srrh while(c-- > 0){ 33410927Srrh if(readptr == &readstk[0]){ 33510927Srrh error("readstk?\n"); 33610927Srrh } 33710927Srrh if(*readptr != 0)release(*readptr); 33810927Srrh readptr--; 33910927Srrh } 34010927Srrh continue; 34110927Srrh case 'q': 34210927Srrh if(readptr <= &readstk[1])exit(0); 34310927Srrh if(*readptr != 0)release(*readptr); 34410927Srrh readptr--; 34510927Srrh if(*readptr != 0)release(*readptr); 34610927Srrh readptr--; 34710927Srrh continue; 34810927Srrh case 'f': 34910927Srrh if(stkptr == &stack[0])printf("empty stack\n"); 35010927Srrh else { 35110927Srrh for(ptr = stkptr; ptr > &stack[0];){ 35210927Srrh print(*ptr--); 35310927Srrh } 35410927Srrh } 35510927Srrh continue; 35610927Srrh case 'p': 35710927Srrh if(stkptr == &stack[0])printf("empty stack\n"); 35810927Srrh else{ 35910927Srrh print(*stkptr); 36010927Srrh } 36110927Srrh continue; 36210927Srrh case 'P': 36310927Srrh p = pop(); 36410927Srrh EMPTY; 36510927Srrh sputc(p,0); 36610927Srrh printf("%s",p->beg); 36710927Srrh release(p); 36810927Srrh continue; 36910927Srrh case 'd': 37010927Srrh if(stkptr == &stack[0]){ 37110927Srrh printf("empty stack\n"); 37210927Srrh continue; 37310927Srrh } 37410927Srrh q = *stkptr; 37510927Srrh n = length(q); 37610927Srrh p = copy(*stkptr,n); 37710927Srrh pushp(p); 37810927Srrh continue; 37910927Srrh case 'c': 38010927Srrh while(stkerr == 0){ 38110927Srrh p = pop(); 38210927Srrh if(stkerr == 0)release(p); 38310927Srrh } 38410927Srrh continue; 38510927Srrh case 'S': 38610927Srrh if(stkptr == &stack[0]){ 38710927Srrh error("save: args\n"); 38810927Srrh } 38910927Srrh c = readc() & 0377; 39010927Srrh sptr = stable[c]; 39110927Srrh sp = stable[c] = sfree; 39210927Srrh sfree = sfree->next; 39310927Srrh if(sfree == 0)goto sempty; 39410927Srrh sp->next = sptr; 39510927Srrh p = pop(); 39610927Srrh EMPTY; 39710927Srrh if(c >= ARRAYST){ 39810927Srrh q = copy(p,PTRSZ); 39910927Srrh for(n = 0;n < PTRSZ-1;n++)sputc(q,0); 40010927Srrh release(p); 40110927Srrh p = q; 40210927Srrh } 40310927Srrh sp->val = p; 40410927Srrh continue; 40510927Srrh sempty: 40610927Srrh error("symbol table overflow\n"); 40710927Srrh case 's': 40810927Srrh if(stkptr == &stack[0]){ 40910927Srrh error("save:args\n"); 41010927Srrh } 41110927Srrh c = readc() & 0377; 41210927Srrh sptr = stable[c]; 41310927Srrh if(sptr != 0){ 41410927Srrh p = sptr->val; 41510927Srrh if(c >= ARRAYST){ 41610927Srrh rewind(p); 41710927Srrh while(sfeof(p) == 0)release(getwd(p)); 41810927Srrh } 41910927Srrh release(p); 42010927Srrh } 42110927Srrh else{ 42210927Srrh sptr = stable[c] = sfree; 42310927Srrh sfree = sfree->next; 42410927Srrh if(sfree == 0)goto sempty; 42510927Srrh sptr->next = 0; 42610927Srrh } 42710927Srrh p = pop(); 42810927Srrh sptr->val = p; 42910927Srrh continue; 43010927Srrh case 'l': 43110927Srrh load(); 43210927Srrh continue; 43310927Srrh case 'L': 43410927Srrh c = readc() & 0377; 43510927Srrh sptr = stable[c]; 43610927Srrh if(sptr == 0){ 43710927Srrh error("L?\n"); 43810927Srrh } 43910927Srrh stable[c] = sptr->next; 44010927Srrh sptr->next = sfree; 44110927Srrh sfree = sptr; 44210927Srrh p = sptr->val; 44310927Srrh if(c >= ARRAYST){ 44410927Srrh rewind(p); 44510927Srrh while(sfeof(p) == 0){ 44610927Srrh q = getwd(p); 44710927Srrh if(q != 0)release(q); 44810927Srrh } 44910927Srrh } 45010927Srrh pushp(p); 45110927Srrh continue; 45210927Srrh case ':': 45310927Srrh p = pop(); 45410927Srrh EMPTY; 45510927Srrh q = scalint(p); 45610927Srrh fsfile(q); 45710927Srrh c = 0; 45810927Srrh if((sfbeg(q) == 0) && ((c = sbackc(q))<0)){ 45910927Srrh error("neg index\n"); 46010927Srrh } 46110927Srrh if(length(q)>2){ 46210927Srrh error("index too big\n"); 46310927Srrh } 46410927Srrh if(sfbeg(q) == 0)c = c*100+sbackc(q); 46510927Srrh if(c >= MAXIND){ 46610927Srrh error("index too big\n"); 46710927Srrh } 46810927Srrh release(q); 46910927Srrh n = readc() & 0377; 47010927Srrh sptr = stable[n]; 47110927Srrh if(sptr == 0){ 47210927Srrh sptr = stable[n] = sfree; 47310927Srrh sfree = sfree->next; 47410927Srrh if(sfree == 0)goto sempty; 47510927Srrh sptr->next = 0; 47610927Srrh p = salloc((c+PTRSZ)*PTRSZ); 47710927Srrh zero(p); 47810927Srrh } 47910927Srrh else{ 48010927Srrh p = sptr->val; 48110927Srrh if(length(p)-PTRSZ < c*PTRSZ){ 48210927Srrh q = copy(p,(c+PTRSZ)*PTRSZ); 48310927Srrh release(p); 48410927Srrh p = q; 48510927Srrh } 48610927Srrh } 48710927Srrh seekc(p,c*PTRSZ); 48810927Srrh q = lookwd(p); 48910927Srrh if (q!=NULL) release(q); 49010927Srrh s = pop(); 49110927Srrh EMPTY; 49210927Srrh salterwd(p,s); 49310927Srrh sptr->val = p; 49410927Srrh continue; 49510927Srrh case ';': 49610927Srrh p = pop(); 49710927Srrh EMPTY; 49810927Srrh q = scalint(p); 49910927Srrh fsfile(q); 50010927Srrh c = 0; 50110927Srrh if((sfbeg(q) == 0) && ((c = sbackc(q))<0)){ 50210927Srrh error("neg index\n"); 50310927Srrh } 50410927Srrh if(length(q)>2){ 50510927Srrh error("index too big\n"); 50610927Srrh } 50710927Srrh if(sfbeg(q) == 0)c = c*100+sbackc(q); 50810927Srrh if(c >= MAXIND){ 50910927Srrh error("index too big\n"); 51010927Srrh } 51110927Srrh release(q); 51210927Srrh n = readc() & 0377; 51310927Srrh sptr = stable[n]; 51410927Srrh if(sptr != 0){ 51510927Srrh p = sptr->val; 51610927Srrh if(length(p)-PTRSZ >= c*PTRSZ){ 51710927Srrh seekc(p,c*PTRSZ); 51810927Srrh s = getwd(p); 51910927Srrh if(s != 0){ 52010927Srrh q = copy(s,length(s)); 52110927Srrh pushp(q); 52210927Srrh continue; 52310927Srrh } 52410927Srrh } 52510927Srrh } 52610927Srrh q = salloc(PTRSZ); 52710927Srrh putwd(q, (struct blk *)0); 52810927Srrh pushp(q); 52910927Srrh continue; 53010927Srrh case 'x': 53110927Srrh execute: 53210927Srrh p = pop(); 53310927Srrh EMPTY; 53410927Srrh if((readptr != &readstk[0]) && (*readptr != 0)){ 53510927Srrh if((*readptr)->rd == (*readptr)->wt) 53610927Srrh release(*readptr); 53710927Srrh else{ 53810927Srrh if(readptr++ == &readstk[RDSKSZ]){ 53910927Srrh error("nesting depth\n"); 54010927Srrh } 54110927Srrh } 54210927Srrh } 54310927Srrh else readptr++; 54410927Srrh *readptr = p; 54510927Srrh if(p != 0)rewind(p); 54610927Srrh else{ 54710927Srrh if((c = readc()) != '\n')unreadc(c); 54810927Srrh } 54910927Srrh continue; 55010927Srrh case '?': 55110927Srrh if(++readptr == &readstk[RDSKSZ]){ 55210927Srrh error("nesting depth\n"); 55310927Srrh } 55410927Srrh *readptr = 0; 55510927Srrh fsave = curfile; 55610927Srrh curfile = stdin; 55710927Srrh while((c = readc()) == '!')command(); 55810927Srrh p = salloc(0); 55910927Srrh sputc(p,c); 56010927Srrh while((c = readc()) != '\n'){ 56110927Srrh sputc(p,c); 56210927Srrh if(c == '\\')sputc(p,readc()); 56310927Srrh } 56410927Srrh curfile = fsave; 56510927Srrh *readptr = p; 56610927Srrh continue; 56710927Srrh case '!': 56810927Srrh if(command() == 1)goto execute; 56910927Srrh continue; 57010927Srrh case '<': 57110927Srrh case '>': 57210927Srrh case '=': 57310927Srrh if(cond(c) == 1)goto execute; 57410927Srrh continue; 57510927Srrh default: 57610927Srrh printf("%o is unimplemented\n",c); 57710927Srrh } 57810927Srrh } 57910927Srrh } 58010927Srrh struct blk * 58110927Srrh div(ddivd,ddivr) 58210927Srrh struct blk *ddivd,*ddivr; 58310927Srrh { 58410927Srrh int divsign,remsign,offset,divcarry; 58510927Srrh int carry, dig,magic,d,dd; 58610927Srrh long c,td,cc; 58710927Srrh struct blk *ps; 58810927Srrh register struct blk *p,*divd,*divr; 58910927Srrh 59010927Srrh rem = 0; 59110927Srrh p = salloc(0); 59210927Srrh if(length(ddivr) == 0){ 59310927Srrh pushp(ddivr); 59410927Srrh errorrt("divide by 0\n"); 59510927Srrh } 59610927Srrh divsign = remsign = 0; 59710927Srrh divr = ddivr; 59810927Srrh fsfile(divr); 59910927Srrh if(sbackc(divr) == -1){ 60010927Srrh divr = copy(ddivr,length(ddivr)); 60110927Srrh chsign(divr); 60210927Srrh divsign = ~divsign; 60310927Srrh } 60410927Srrh divd = copy(ddivd,length(ddivd)); 60510927Srrh fsfile(divd); 60610927Srrh if(sfbeg(divd) == 0 && sbackc(divd) == -1){ 60710927Srrh chsign(divd); 60810927Srrh divsign = ~divsign; 60910927Srrh remsign = ~remsign; 61010927Srrh } 61110927Srrh offset = length(divd) - length(divr); 61210927Srrh if(offset < 0)goto ddone; 61310927Srrh seekc(p,offset+1); 61410927Srrh sputc(divd,0); 61510927Srrh magic = 0; 61610927Srrh fsfile(divr); 61710927Srrh c = sbackc(divr); 61810927Srrh if(c<10)magic++; 61910927Srrh c = c*100 + (sfbeg(divr)?0:sbackc(divr)); 62010927Srrh if(magic>0){ 62110927Srrh c = (c*100 +(sfbeg(divr)?0:sbackc(divr)))*2; 62210927Srrh c /= 25; 62310927Srrh } 62410927Srrh while(offset >= 0){ 62510927Srrh fsfile(divd); 62610927Srrh td = sbackc(divd)*100; 62710927Srrh dd = sfbeg(divd)?0:sbackc(divd); 62810927Srrh td = (td+dd)*100; 62910927Srrh dd = sfbeg(divd)?0:sbackc(divd); 63010927Srrh td = td+dd; 63110927Srrh cc = c; 63210927Srrh if(offset == 0)td += 1; 63310927Srrh else cc += 1; 63410927Srrh if(magic != 0)td = td<<3; 63510927Srrh dig = td/cc; 63610927Srrh rewind(divr); 63710927Srrh rewind(divxyz); 63810927Srrh carry = 0; 63910927Srrh while(sfeof(divr) == 0){ 64010927Srrh d = sgetc(divr)*dig+carry; 64110927Srrh carry = d / 100; 64210927Srrh salterc(divxyz,d%100); 64310927Srrh } 64410927Srrh salterc(divxyz,carry); 64510927Srrh rewind(divxyz); 64610927Srrh seekc(divd,offset); 64710927Srrh carry = 0; 64810927Srrh while(sfeof(divd) == 0){ 64910927Srrh d = slookc(divd); 65010927Srrh d = d-(sfeof(divxyz)?0:sgetc(divxyz))-carry; 65110927Srrh carry = 0; 65210927Srrh if(d < 0){ 65310927Srrh d += 100; 65410927Srrh carry = 1; 65510927Srrh } 65610927Srrh salterc(divd,d); 65710927Srrh } 65810927Srrh divcarry = carry; 65910927Srrh sbackc(p); 66010927Srrh salterc(p,dig); 66110927Srrh sbackc(p); 66210927Srrh if(--offset >= 0)divd->wt--; 66310927Srrh } 66410927Srrh if(divcarry != 0){ 66510927Srrh salterc(p,dig-1); 66610927Srrh salterc(divd,-1); 66710927Srrh ps = add(divr,divd); 66810927Srrh release(divd); 66910927Srrh divd = ps; 67010927Srrh } 67110927Srrh 67210927Srrh rewind(p); 67310927Srrh divcarry = 0; 67410927Srrh while(sfeof(p) == 0){ 67510927Srrh d = slookc(p)+divcarry; 67610927Srrh divcarry = 0; 67710927Srrh if(d >= 100){ 67810927Srrh d -= 100; 67910927Srrh divcarry = 1; 68010927Srrh } 68110927Srrh salterc(p,d); 68210927Srrh } 68310927Srrh if(divcarry != 0)salterc(p,divcarry); 68410927Srrh fsfile(p); 68510927Srrh while(sfbeg(p) == 0){ 68610927Srrh if(sbackc(p) == 0)truncate(p); 68710927Srrh else break; 68810927Srrh } 68910927Srrh if(divsign < 0)chsign(p); 69010927Srrh fsfile(divd); 69110927Srrh while(sfbeg(divd) == 0){ 69210927Srrh if(sbackc(divd) == 0)truncate(divd); 69310927Srrh else break; 69410927Srrh } 69510927Srrh ddone: 69610927Srrh if(remsign<0)chsign(divd); 69710927Srrh if(divr != ddivr)release(divr); 69810927Srrh rem = divd; 69910927Srrh return(p); 70010927Srrh } 70110927Srrh dscale(){ 70210927Srrh register struct blk *dd,*dr; 70310927Srrh register struct blk *r; 70410927Srrh int c; 70510927Srrh 70610927Srrh dr = pop(); 70710927Srrh EMPTYS; 70810927Srrh dd = pop(); 70910927Srrh EMPTYSR(dr); 71010927Srrh fsfile(dd); 71110927Srrh skd = sunputc(dd); 71210927Srrh fsfile(dr); 71310927Srrh skr = sunputc(dr); 71410927Srrh if(sfbeg(dr) == 1 || (sfbeg(dr) == 0 && sbackc(dr) == 0)){ 71510927Srrh sputc(dr,skr); 71610927Srrh pushp(dr); 71710927Srrh errorrt("divide by 0\n"); 71810927Srrh } 71910927Srrh c = k-skd+skr; 72010927Srrh if(c < 0)r = removr(dd,-c); 72110927Srrh else { 72210927Srrh r = add0(dd,c); 72310927Srrh irem = 0; 72410927Srrh } 72510927Srrh arg1 = r; 72610927Srrh arg2 = dr; 72710927Srrh savk = k; 72810927Srrh return(0); 72910927Srrh } 73010927Srrh struct blk * 73110927Srrh removr(p,n) 73210927Srrh struct blk *p; 73310927Srrh { 73410927Srrh int nn; 73510927Srrh register struct blk *q,*s,*r; 73610927Srrh 73710927Srrh rewind(p); 73810927Srrh nn = (n+1)/2; 73910927Srrh q = salloc(nn); 74010927Srrh while(n>1){ 74110927Srrh sputc(q,sgetc(p)); 74210927Srrh n -= 2; 74310927Srrh } 74410927Srrh r = salloc(2); 74510927Srrh while(sfeof(p) == 0)sputc(r,sgetc(p)); 74610927Srrh release(p); 74710927Srrh if(n == 1){ 74810927Srrh s = div(r,tenptr); 74910927Srrh release(r); 75010927Srrh rewind(rem); 75110927Srrh if(sfeof(rem) == 0)sputc(q,sgetc(rem)); 75210927Srrh release(rem); 75310927Srrh irem = q; 75410927Srrh return(s); 75510927Srrh } 75610927Srrh irem = q; 75710927Srrh return(r); 75810927Srrh } 75910927Srrh struct blk * 76010927Srrh sqrt(p) 76110927Srrh struct blk *p; 76210927Srrh { 76310927Srrh struct blk *t; 76410927Srrh struct blk *r,*q,*s; 76510927Srrh int c,n,nn; 76610927Srrh 76710927Srrh n = length(p); 76810927Srrh fsfile(p); 76910927Srrh c = sbackc(p); 77010927Srrh if((n&1) != 1)c = c*100+(sfbeg(p)?0:sbackc(p)); 77110927Srrh n = (n+1)>>1; 77210927Srrh r = salloc(n); 77310927Srrh zero(r); 77410927Srrh seekc(r,n); 77510927Srrh nn=1; 77610927Srrh while((c -= nn)>=0)nn+=2; 77710927Srrh c=(nn+1)>>1; 77810927Srrh fsfile(r); 77910927Srrh sbackc(r); 78010927Srrh if(c>=100){ 78110927Srrh c -= 100; 78210927Srrh salterc(r,c); 78310927Srrh sputc(r,1); 78410927Srrh } 78510927Srrh else salterc(r,c); 78610927Srrh while(1){ 78710927Srrh q = div(p,r); 78810927Srrh s = add(q,r); 78910927Srrh release(q); 79010927Srrh release(rem); 79110927Srrh q = div(s,sqtemp); 79210927Srrh release(s); 79310927Srrh release(rem); 79410927Srrh s = copy(r,length(r)); 79510927Srrh chsign(s); 79610927Srrh t = add(s,q); 79710927Srrh release(s); 79810927Srrh fsfile(t); 79910927Srrh nn = sfbeg(t)?0:sbackc(t); 80010927Srrh if(nn>=0)break; 80110927Srrh release(r); 80210927Srrh release(t); 80310927Srrh r = q; 80410927Srrh } 80510927Srrh release(t); 80610927Srrh release(q); 80710927Srrh release(p); 80810927Srrh return(r); 80910927Srrh } 81010927Srrh struct blk * 81110927Srrh exp(base,ex) 81210927Srrh struct blk *base,*ex; 81310927Srrh { 81410927Srrh register struct blk *r,*e,*p; 81510927Srrh struct blk *e1,*t,*cp; 81610927Srrh int temp,c,n; 81710927Srrh r = salloc(1); 81810927Srrh sputc(r,1); 81910927Srrh p = copy(base,length(base)); 82010927Srrh e = copy(ex,length(ex)); 82110927Srrh fsfile(e); 82210927Srrh if(sfbeg(e) != 0)goto edone; 82310927Srrh temp=0; 82410927Srrh c = sbackc(e); 82510927Srrh if(c<0){ 82610927Srrh temp++; 82710927Srrh chsign(e); 82810927Srrh } 82910927Srrh while(length(e) != 0){ 83010927Srrh e1=div(e,sqtemp); 83110927Srrh release(e); 83210927Srrh e = e1; 83310927Srrh n = length(rem); 83410927Srrh release(rem); 83510927Srrh if(n != 0){ 83610927Srrh e1=mult(p,r); 83710927Srrh release(r); 83810927Srrh r = e1; 83910927Srrh } 84010927Srrh t = copy(p,length(p)); 84110927Srrh cp = mult(p,t); 84210927Srrh release(p); 84310927Srrh release(t); 84410927Srrh p = cp; 84510927Srrh } 84610927Srrh if(temp != 0){ 84710927Srrh if((c = length(base)) == 0){ 84810927Srrh goto edone; 84910927Srrh } 85010927Srrh if(c>1)create(r); 85110927Srrh else{ 85210927Srrh rewind(base); 85310927Srrh if((c = sgetc(base))<=1){ 85410927Srrh create(r); 85510927Srrh sputc(r,c); 85610927Srrh } 85710927Srrh else create(r); 85810927Srrh } 85910927Srrh } 86010927Srrh edone: 86110927Srrh release(p); 86210927Srrh release(e); 86310927Srrh return(r); 86410927Srrh } 86510927Srrh init(argc,argv) 86610927Srrh int argc; 86710927Srrh char *argv[]; 86810927Srrh { 86910927Srrh register struct sym *sp; 87010927Srrh 87110927Srrh if (signal(SIGINT, SIG_IGN) != SIG_IGN) 87210927Srrh signal(SIGINT,onintr); 87310927Srrh setbuf(stdout,(char *)NULL); 87410927Srrh svargc = --argc; 87510927Srrh svargv = argv; 87610927Srrh while(svargc>0 && svargv[1][0] == '-'){ 87710927Srrh switch(svargv[1][1]){ 87810927Srrh default: 87910927Srrh dbg=1; 88010927Srrh } 88110927Srrh svargc--; 88210927Srrh svargv++; 88310927Srrh } 88410927Srrh ifile=1; 88510927Srrh if(svargc<=0)curfile = stdin; 88610927Srrh else if((curfile = fopen(svargv[1],"r")) == NULL){ 88710927Srrh printf("can't open file %s\n",svargv[1]); 88810927Srrh exit(1); 88910927Srrh } 89010927Srrh scalptr = salloc(1); 89110927Srrh sputc(scalptr,0); 89210927Srrh basptr = salloc(1); 89310927Srrh sputc(basptr,10); 89410927Srrh obase=10; 89510927Srrh log10=log2(10L); 89610927Srrh ll=70; 89710927Srrh fw=1; 89810927Srrh fw1=0; 89910927Srrh tenptr = salloc(1); 90010927Srrh sputc(tenptr,10); 90110927Srrh obase=10; 90210927Srrh inbas = salloc(1); 90310927Srrh sputc(inbas,10); 90410927Srrh sqtemp = salloc(1); 90510927Srrh sputc(sqtemp,2); 90610927Srrh chptr = salloc(0); 90710927Srrh strptr = salloc(0); 90810927Srrh divxyz = salloc(0); 90910927Srrh stkbeg = stkptr = &stack[0]; 91010927Srrh stkend = &stack[STKSZ]; 91110927Srrh stkerr = 0; 91210927Srrh readptr = &readstk[0]; 91310927Srrh k=0; 91410927Srrh sp = sptr = &symlst[0]; 91511774Ssam while(sptr < &symlst[TBLSZ-1]){ 91610927Srrh sptr->next = ++sp; 91710927Srrh sptr++; 91810927Srrh } 91910927Srrh sptr->next=0; 92010927Srrh sfree = &symlst[0]; 92110927Srrh return; 92210927Srrh } 92310927Srrh onintr(){ 92410927Srrh 92510927Srrh signal(SIGINT,onintr); 92610927Srrh while(readptr != &readstk[0]){ 92710927Srrh if(*readptr != 0){release(*readptr);} 92810927Srrh readptr--; 92910927Srrh } 93010927Srrh curfile = stdin; 93110927Srrh commnds(); 93210927Srrh } 93310927Srrh pushp(p) 93410927Srrh struct blk *p; 93510927Srrh { 93610927Srrh if(stkptr == stkend){ 93710927Srrh printf("out of stack space\n"); 93810927Srrh return; 93910927Srrh } 94010927Srrh stkerr=0; 94110927Srrh *++stkptr = p; 94210927Srrh return; 94310927Srrh } 94410927Srrh struct blk * 94510927Srrh pop(){ 94610927Srrh if(stkptr == stack){ 94710927Srrh stkerr=1; 94810927Srrh return(0); 94910927Srrh } 95010927Srrh return(*stkptr--); 95110927Srrh } 95210927Srrh struct blk * 95310927Srrh readin(){ 95410927Srrh register struct blk *p,*q; 95510927Srrh int dp,dpct; 95610927Srrh register int c; 95710927Srrh 95810927Srrh dp = dpct=0; 95910927Srrh p = salloc(0); 96010927Srrh while(1){ 96110927Srrh c = readc(); 96210927Srrh switch(c){ 96310927Srrh case '.': 96410927Srrh if(dp != 0){ 96510927Srrh unreadc(c); 96610927Srrh break; 96710927Srrh } 96810927Srrh dp++; 96910927Srrh continue; 97010927Srrh case '\\': 97110927Srrh readc(); 97210927Srrh continue; 97310927Srrh default: 97410927Srrh if(c >= 'A' && c <= 'F')c = c - 'A' + 10; 97510927Srrh else if(c >= '0' && c <= '9')c -= '0'; 97610927Srrh else goto gotnum; 97710927Srrh if(dp != 0){ 97810927Srrh if(dpct >= 99)continue; 97910927Srrh dpct++; 98010927Srrh } 98110927Srrh create(chptr); 98210927Srrh if(c != 0)sputc(chptr,c); 98310927Srrh q = mult(p,inbas); 98410927Srrh release(p); 98510927Srrh p = add(chptr,q); 98610927Srrh release(q); 98710927Srrh } 98810927Srrh } 98910927Srrh gotnum: 99010927Srrh unreadc(c); 99110927Srrh if(dp == 0){ 99210927Srrh sputc(p,0); 99310927Srrh return(p); 99410927Srrh } 99510927Srrh else{ 99610927Srrh q = scale(p,dpct); 99710927Srrh return(q); 99810927Srrh } 99910927Srrh } 100010927Srrh struct blk * 100110927Srrh add0(p,ct) 100210927Srrh int ct; 100310927Srrh struct blk *p; 100410927Srrh { 100510927Srrh /* returns pointer to struct with ct 0's & p */ 100610927Srrh register struct blk *q,*t; 100710927Srrh 100810927Srrh q = salloc(length(p)+(ct+1)/2); 100910927Srrh while(ct>1){ 101010927Srrh sputc(q,0); 101110927Srrh ct -= 2; 101210927Srrh } 101310927Srrh rewind(p); 101410927Srrh while(sfeof(p) == 0){ 101510927Srrh sputc(q,sgetc(p)); 101610927Srrh } 101710927Srrh release(p); 101810927Srrh if(ct == 1){ 101910927Srrh t = mult(tenptr,q); 102010927Srrh release(q); 102110927Srrh return(t); 102210927Srrh } 102310927Srrh return(q); 102410927Srrh } 102510927Srrh struct blk * 102610927Srrh mult(p,q) 102710927Srrh struct blk *p,*q; 102810927Srrh { 102910927Srrh register struct blk *mp,*mq,*mr; 103010927Srrh int sign,offset,carry; 103110927Srrh int cq,cp,mt,mcr; 103210927Srrh 103310927Srrh offset = sign = 0; 103410927Srrh fsfile(p); 103510927Srrh mp = p; 103610927Srrh if(sfbeg(p) == 0){ 103710927Srrh if(sbackc(p)<0){ 103810927Srrh mp = copy(p,length(p)); 103910927Srrh chsign(mp); 104010927Srrh sign = ~sign; 104110927Srrh } 104210927Srrh } 104310927Srrh fsfile(q); 104410927Srrh mq = q; 104510927Srrh if(sfbeg(q) == 0){ 104610927Srrh if(sbackc(q)<0){ 104710927Srrh mq = copy(q,length(q)); 104810927Srrh chsign(mq); 104910927Srrh sign = ~sign; 105010927Srrh } 105110927Srrh } 105210927Srrh mr = salloc(length(mp)+length(mq)); 105310927Srrh zero(mr); 105410927Srrh rewind(mq); 105510927Srrh while(sfeof(mq) == 0){ 105610927Srrh cq = sgetc(mq); 105710927Srrh rewind(mp); 105810927Srrh rewind(mr); 105910927Srrh mr->rd += offset; 106010927Srrh carry=0; 106110927Srrh while(sfeof(mp) == 0){ 106210927Srrh cp = sgetc(mp); 106310927Srrh mcr = sfeof(mr)?0:slookc(mr); 106410927Srrh mt = cp*cq + carry + mcr; 106510927Srrh carry = mt/100; 106610927Srrh salterc(mr,mt%100); 106710927Srrh } 106810927Srrh offset++; 106910927Srrh if(carry != 0){ 107010927Srrh mcr = sfeof(mr)?0:slookc(mr); 107110927Srrh salterc(mr,mcr+carry); 107210927Srrh } 107310927Srrh } 107410927Srrh if(sign < 0){ 107510927Srrh chsign(mr); 107610927Srrh } 107710927Srrh if(mp != p)release(mp); 107810927Srrh if(mq != q)release(mq); 107910927Srrh return(mr); 108010927Srrh } 108110927Srrh chsign(p) 108210927Srrh struct blk *p; 108310927Srrh { 108410927Srrh register int carry; 108510927Srrh register char ct; 108610927Srrh 108710927Srrh carry=0; 108810927Srrh rewind(p); 108910927Srrh while(sfeof(p) == 0){ 109010927Srrh ct=100-slookc(p)-carry; 109110927Srrh carry=1; 109210927Srrh if(ct>=100){ 109310927Srrh ct -= 100; 109410927Srrh carry=0; 109510927Srrh } 109610927Srrh salterc(p,ct); 109710927Srrh } 109810927Srrh if(carry != 0){ 109910927Srrh sputc(p,-1); 110010927Srrh fsfile(p); 110110927Srrh sbackc(p); 110210927Srrh ct = sbackc(p); 110310927Srrh if(ct == 99){ 110410927Srrh truncate(p); 110510927Srrh sputc(p,-1); 110610927Srrh } 110710927Srrh } 110810927Srrh else{ 110910927Srrh fsfile(p); 111010927Srrh ct = sbackc(p); 111110927Srrh if(ct == 0)truncate(p); 111210927Srrh } 111310927Srrh return; 111410927Srrh } 111510927Srrh readc(){ 111610927Srrh loop: 111710927Srrh if((readptr != &readstk[0]) && (*readptr != 0)){ 111810927Srrh if(sfeof(*readptr) == 0)return(lastchar = sgetc(*readptr)); 111910927Srrh release(*readptr); 112010927Srrh readptr--; 112110927Srrh goto loop; 112210927Srrh } 112310927Srrh lastchar = getc(curfile); 112410927Srrh if(lastchar != EOF)return(lastchar); 112510927Srrh if(readptr != &readptr[0]){ 112610927Srrh readptr--; 112710927Srrh if(*readptr == 0)curfile = stdin; 112810927Srrh goto loop; 112910927Srrh } 113010927Srrh if(curfile != stdin){ 113110927Srrh fclose(curfile); 113210927Srrh curfile = stdin; 113310927Srrh goto loop; 113410927Srrh } 113510927Srrh exit(0); 113610927Srrh } 113710927Srrh unreadc(c) 113810927Srrh char c; 113910927Srrh { 114010927Srrh 114110927Srrh if((readptr != &readstk[0]) && (*readptr != 0)){ 114210927Srrh sungetc(*readptr,c); 114310927Srrh } 114410927Srrh else ungetc(c,curfile); 114510927Srrh return; 114610927Srrh } 114710927Srrh binop(c) 114810927Srrh char c; 114910927Srrh { 115010927Srrh register struct blk *r; 115110927Srrh 115210927Srrh switch(c){ 115310927Srrh case '+': 115410927Srrh r = add(arg1,arg2); 115510927Srrh break; 115610927Srrh case '*': 115710927Srrh r = mult(arg1,arg2); 115810927Srrh break; 115910927Srrh case '/': 116010927Srrh r = div(arg1,arg2); 116110927Srrh break; 116210927Srrh } 116310927Srrh release(arg1); 116410927Srrh release(arg2); 116510927Srrh sputc(r,savk); 116610927Srrh pushp(r); 116710927Srrh return; 116810927Srrh } 116910927Srrh print(hptr) 117010927Srrh struct blk *hptr; 117110927Srrh { 117210927Srrh int sc; 117310927Srrh register struct blk *p,*q,*dec; 117410927Srrh int dig,dout,ct; 117510927Srrh 117610927Srrh rewind(hptr); 117710927Srrh while(sfeof(hptr) == 0){ 117810927Srrh if(sgetc(hptr)>99){ 117910927Srrh rewind(hptr); 118010927Srrh while(sfeof(hptr) == 0){ 118110927Srrh printf("%c",sgetc(hptr)); 118210927Srrh } 118310927Srrh printf("\n"); 118410927Srrh return; 118510927Srrh } 118610927Srrh } 118710927Srrh fsfile(hptr); 118810927Srrh sc = sbackc(hptr); 118910927Srrh if(sfbeg(hptr) != 0){ 119010927Srrh printf("0\n"); 119110927Srrh return; 119210927Srrh } 119310927Srrh count = ll; 119410927Srrh p = copy(hptr,length(hptr)); 119510927Srrh sunputc(p); 119610927Srrh fsfile(p); 119710927Srrh if(sbackc(p)<0){ 119810927Srrh chsign(p); 119910927Srrh OUTC('-'); 120010927Srrh } 120110927Srrh if((obase == 0) || (obase == -1)){ 120210927Srrh oneot(p,sc,'d'); 120310927Srrh return; 120410927Srrh } 120510927Srrh if(obase == 1){ 120610927Srrh oneot(p,sc,'1'); 120710927Srrh return; 120810927Srrh } 120910927Srrh if(obase == 10){ 121010927Srrh tenot(p,sc); 121110927Srrh return; 121210927Srrh } 121310927Srrh create(strptr); 121410927Srrh dig = log10*sc; 121510927Srrh dout = ((dig/10) + dig) /logo; 121610927Srrh dec = getdec(p,sc); 121710927Srrh p = removc(p,sc); 121810927Srrh while(length(p) != 0){ 121910927Srrh q = div(p,basptr); 122010927Srrh release(p); 122110927Srrh p = q; 122210927Srrh (*outdit)(rem,0); 122310927Srrh } 122410927Srrh release(p); 122510927Srrh fsfile(strptr); 122610927Srrh while(sfbeg(strptr) == 0)OUTC(sbackc(strptr)); 122710927Srrh if(sc == 0){ 122810927Srrh release(dec); 122910927Srrh printf("\n"); 123010927Srrh return; 123110927Srrh } 123210927Srrh create(strptr); 123310927Srrh OUTC('.'); 123410927Srrh ct=0; 123510927Srrh do{ 123610927Srrh q = mult(basptr,dec); 123710927Srrh release(dec); 123810927Srrh dec = getdec(q,sc); 123910927Srrh p = removc(q,sc); 124010927Srrh (*outdit)(p,1); 124110927Srrh }while(++ct < dout); 124210927Srrh release(dec); 124310927Srrh rewind(strptr); 124410927Srrh while(sfeof(strptr) == 0)OUTC(sgetc(strptr)); 124510927Srrh printf("\n"); 124610927Srrh return; 124710927Srrh } 124810927Srrh 124910927Srrh struct blk * 125010927Srrh getdec(p,sc) 125110927Srrh struct blk *p; 125210927Srrh { 125310927Srrh int cc; 125410927Srrh register struct blk *q,*t,*s; 125510927Srrh 125610927Srrh rewind(p); 125710927Srrh if(length(p)*2 < sc){ 125810927Srrh q = copy(p,length(p)); 125910927Srrh return(q); 126010927Srrh } 126110927Srrh q = salloc(length(p)); 126210927Srrh while(sc >= 1){ 126310927Srrh sputc(q,sgetc(p)); 126410927Srrh sc -= 2; 126510927Srrh } 126610927Srrh if(sc != 0){ 126710927Srrh t = mult(q,tenptr); 126810927Srrh s = salloc(cc = length(q)); 126910927Srrh release(q); 127010927Srrh rewind(t); 127110927Srrh while(cc-- > 0)sputc(s,sgetc(t)); 127210927Srrh sputc(s,0); 127310927Srrh release(t); 127410927Srrh t = div(s,tenptr); 127510927Srrh release(s); 127610927Srrh release(rem); 127710927Srrh return(t); 127810927Srrh } 127910927Srrh return(q); 128010927Srrh } 128110927Srrh tenot(p,sc) 128210927Srrh struct blk *p; 128310927Srrh { 128410927Srrh register int c,f; 128510927Srrh 128610927Srrh fsfile(p); 128710927Srrh f=0; 128810927Srrh while((sfbeg(p) == 0) && ((p->rd-p->beg-1)*2 >= sc)){ 128910927Srrh c = sbackc(p); 129010927Srrh if((c<10) && (f == 1))printf("0%d",c); 129110927Srrh else printf("%d",c); 129210927Srrh f=1; 129310927Srrh TEST2; 129410927Srrh } 129510927Srrh if(sc == 0){ 129610927Srrh printf("\n"); 129710927Srrh release(p); 129810927Srrh return; 129910927Srrh } 130010927Srrh if((p->rd-p->beg)*2 > sc){ 130110927Srrh c = sbackc(p); 130210927Srrh printf("%d.",c/10); 130310927Srrh TEST2; 130410927Srrh OUTC(c%10 +'0'); 130510927Srrh sc--; 130610927Srrh } 130710927Srrh else { 130810927Srrh OUTC('.'); 130910927Srrh } 131010927Srrh if(sc > (p->rd-p->beg)*2){ 131110927Srrh while(sc>(p->rd-p->beg)*2){ 131210927Srrh OUTC('0'); 131310927Srrh sc--; 131410927Srrh } 131510927Srrh } 131610927Srrh while(sc > 1){ 131710927Srrh c = sbackc(p); 131810927Srrh if(c<10)printf("0%d",c); 131910927Srrh else printf("%d",c); 132010927Srrh sc -= 2; 132110927Srrh TEST2; 132210927Srrh } 132310927Srrh if(sc == 1){ 132410927Srrh OUTC(sbackc(p)/10 +'0'); 132510927Srrh } 132610927Srrh printf("\n"); 132710927Srrh release(p); 132810927Srrh return; 132910927Srrh } 133010927Srrh oneot(p,sc,ch) 133110927Srrh struct blk *p; 133210927Srrh char ch; 133310927Srrh { 133410927Srrh register struct blk *q; 133510927Srrh 133610927Srrh q = removc(p,sc); 133710927Srrh create(strptr); 133810927Srrh sputc(strptr,-1); 133910927Srrh while(length(q)>0){ 134010927Srrh p = add(strptr,q); 134110927Srrh release(q); 134210927Srrh q = p; 134310927Srrh OUTC(ch); 134410927Srrh } 134510927Srrh release(q); 134610927Srrh printf("\n"); 134710927Srrh return; 134810927Srrh } 134910927Srrh hexot(p,flg) 135010927Srrh struct blk *p; 135110927Srrh { 135210927Srrh register int c; 135310927Srrh rewind(p); 135410927Srrh if(sfeof(p) != 0){ 135510927Srrh sputc(strptr,'0'); 135610927Srrh release(p); 135710927Srrh return; 135810927Srrh } 135910927Srrh c = sgetc(p); 136010927Srrh release(p); 136110927Srrh if(c >= 16){ 136210927Srrh printf("hex digit > 16"); 136310927Srrh return; 136410927Srrh } 136510927Srrh sputc(strptr,c<10?c+'0':c-10+'A'); 136610927Srrh return; 136710927Srrh } 136810927Srrh bigot(p,flg) 136910927Srrh struct blk *p; 137010927Srrh { 137110927Srrh register struct blk *t,*q; 137210927Srrh register int l; 137310927Srrh int neg; 137410927Srrh 137510927Srrh if(flg == 1)t = salloc(0); 137610927Srrh else{ 137710927Srrh t = strptr; 137810927Srrh l = length(strptr)+fw-1; 137910927Srrh } 138010927Srrh neg=0; 138110927Srrh if(length(p) != 0){ 138210927Srrh fsfile(p); 138310927Srrh if(sbackc(p)<0){ 138410927Srrh neg=1; 138510927Srrh chsign(p); 138610927Srrh } 138710927Srrh while(length(p) != 0){ 138810927Srrh q = div(p,tenptr); 138910927Srrh release(p); 139010927Srrh p = q; 139110927Srrh rewind(rem); 139210927Srrh sputc(t,sfeof(rem)?'0':sgetc(rem)+'0'); 139310927Srrh release(rem); 139410927Srrh } 139510927Srrh } 139610927Srrh release(p); 139710927Srrh if(flg == 1){ 139810927Srrh l = fw1-length(t); 139910927Srrh if(neg != 0){ 140010927Srrh l--; 140110927Srrh sputc(strptr,'-'); 140210927Srrh } 140310927Srrh fsfile(t); 140410927Srrh while(l-- > 0)sputc(strptr,'0'); 140510927Srrh while(sfbeg(t) == 0)sputc(strptr,sbackc(t)); 140610927Srrh release(t); 140710927Srrh } 140810927Srrh else{ 140910927Srrh l -= length(strptr); 141010927Srrh while(l-- > 0)sputc(strptr,'0'); 141110927Srrh if(neg != 0){ 141210927Srrh sunputc(strptr); 141310927Srrh sputc(strptr,'-'); 141410927Srrh } 141510927Srrh } 141610927Srrh sputc(strptr,' '); 141710927Srrh return; 141810927Srrh } 141910927Srrh struct blk * 142010927Srrh add(a1,a2) 142110927Srrh struct blk *a1,*a2; 142210927Srrh { 142310927Srrh register struct blk *p; 142410927Srrh register int carry,n; 142510927Srrh int size; 142610927Srrh int c,n1,n2; 142710927Srrh 142810927Srrh size = length(a1)>length(a2)?length(a1):length(a2); 142910927Srrh p = salloc(size); 143010927Srrh rewind(a1); 143110927Srrh rewind(a2); 143210927Srrh carry=0; 143310927Srrh while(--size >= 0){ 143410927Srrh n1 = sfeof(a1)?0:sgetc(a1); 143510927Srrh n2 = sfeof(a2)?0:sgetc(a2); 143610927Srrh n = n1 + n2 + carry; 143710927Srrh if(n>=100){ 143810927Srrh carry=1; 143910927Srrh n -= 100; 144010927Srrh } 144110927Srrh else if(n<0){ 144210927Srrh carry = -1; 144310927Srrh n += 100; 144410927Srrh } 144510927Srrh else carry = 0; 144610927Srrh sputc(p,n); 144710927Srrh } 144810927Srrh if(carry != 0)sputc(p,carry); 144910927Srrh fsfile(p); 145010927Srrh if(sfbeg(p) == 0){ 145110927Srrh while(sfbeg(p) == 0 && (c = sbackc(p)) == 0); 145210927Srrh if(c != 0)salterc(p,c); 145310927Srrh truncate(p); 145410927Srrh } 145510927Srrh fsfile(p); 145610927Srrh if(sfbeg(p) == 0 && sbackc(p) == -1){ 145710927Srrh while((c = sbackc(p)) == 99){ 145810927Srrh if(c == EOF)break; 145910927Srrh } 146010927Srrh sgetc(p); 146110927Srrh salterc(p,-1); 146210927Srrh truncate(p); 146310927Srrh } 146410927Srrh return(p); 146510927Srrh } 146610927Srrh eqk(){ 146710927Srrh register struct blk *p,*q; 146810927Srrh register int skp; 146910927Srrh int skq; 147010927Srrh 147110927Srrh p = pop(); 147210927Srrh EMPTYS; 147310927Srrh q = pop(); 147410927Srrh EMPTYSR(p); 147510927Srrh skp = sunputc(p); 147610927Srrh skq = sunputc(q); 147710927Srrh if(skp == skq){ 147810927Srrh arg1=p; 147910927Srrh arg2=q; 148010927Srrh savk = skp; 148110927Srrh return(0); 148210927Srrh } 148310927Srrh else if(skp < skq){ 148410927Srrh savk = skq; 148510927Srrh p = add0(p,skq-skp); 148610927Srrh } 148710927Srrh else { 148810927Srrh savk = skp; 148910927Srrh q = add0(q,skp-skq); 149010927Srrh } 149110927Srrh arg1=p; 149210927Srrh arg2=q; 149310927Srrh return(0); 149410927Srrh } 149510927Srrh struct blk * 149610927Srrh removc(p,n) 149710927Srrh struct blk *p; 149810927Srrh { 149910927Srrh register struct blk *q,*r; 150010927Srrh 150110927Srrh rewind(p); 150210927Srrh while(n>1){ 150310927Srrh sgetc(p); 150410927Srrh n -= 2; 150510927Srrh } 150610927Srrh q = salloc(2); 150710927Srrh while(sfeof(p) == 0)sputc(q,sgetc(p)); 150810927Srrh if(n == 1){ 150910927Srrh r = div(q,tenptr); 151010927Srrh release(q); 151110927Srrh release(rem); 151210927Srrh q = r; 151310927Srrh } 151410927Srrh release(p); 151510927Srrh return(q); 151610927Srrh } 151710927Srrh struct blk * 151810927Srrh scalint(p) 151910927Srrh struct blk *p; 152010927Srrh { 152110927Srrh register int n; 152210927Srrh n = sunputc(p); 152310927Srrh p = removc(p,n); 152410927Srrh return(p); 152510927Srrh } 152610927Srrh struct blk * 152710927Srrh scale(p,n) 152810927Srrh struct blk *p; 152910927Srrh { 153010927Srrh register struct blk *q,*s,*t; 153110927Srrh 153210927Srrh t = add0(p,n); 153310927Srrh q = salloc(1); 153410927Srrh sputc(q,n); 153510927Srrh s = exp(inbas,q); 153610927Srrh release(q); 153710927Srrh q = div(t,s); 153810927Srrh release(t); 153910927Srrh release(s); 154010927Srrh release(rem); 154110927Srrh sputc(q,n); 154210927Srrh return(q); 154310927Srrh } 154410927Srrh subt(){ 154510927Srrh arg1=pop(); 154610927Srrh EMPTYS; 154710927Srrh savk = sunputc(arg1); 154810927Srrh chsign(arg1); 154910927Srrh sputc(arg1,savk); 155010927Srrh pushp(arg1); 155110927Srrh if(eqk() != 0)return(1); 155210927Srrh binop('+'); 155310927Srrh return(0); 155410927Srrh } 155510927Srrh command(){ 155610927Srrh int c; 155710927Srrh char line[100],*sl; 155810927Srrh register (*savint)(),pid,rpid; 155910927Srrh int retcode; 156010927Srrh 156110927Srrh switch(c = readc()){ 156210927Srrh case '<': 156310927Srrh return(cond(NL)); 156410927Srrh case '>': 156510927Srrh return(cond(NG)); 156610927Srrh case '=': 156710927Srrh return(cond(NE)); 156810927Srrh default: 156910927Srrh sl = line; 157010927Srrh *sl++ = c; 157110927Srrh while((c = readc()) != '\n')*sl++ = c; 157210927Srrh *sl = 0; 157310927Srrh if((pid = fork()) == 0){ 157410927Srrh execl("/bin/sh","sh","-c",line,0); 157510927Srrh exit(0100); 157610927Srrh } 157710927Srrh savint = signal(SIGINT, SIG_IGN); 157810927Srrh while((rpid = wait(&retcode)) != pid && rpid != -1); 157910927Srrh signal(SIGINT,savint); 158010927Srrh printf("!\n"); 158110927Srrh return(0); 158210927Srrh } 158310927Srrh } 158410927Srrh cond(c) 158510927Srrh char c; 158610927Srrh { 158710927Srrh register struct blk *p; 158810927Srrh register char cc; 158910927Srrh 159010927Srrh if(subt() != 0)return(1); 159110927Srrh p = pop(); 159210927Srrh sunputc(p); 159310927Srrh if(length(p) == 0){ 159410927Srrh release(p); 159510927Srrh if(c == '<' || c == '>' || c == NE){ 159610927Srrh readc(); 159710927Srrh return(0); 159810927Srrh } 159910927Srrh load(); 160010927Srrh return(1); 160110927Srrh } 160210927Srrh else { 160310927Srrh if(c == '='){ 160410927Srrh release(p); 160510927Srrh readc(); 160610927Srrh return(0); 160710927Srrh } 160810927Srrh } 160910927Srrh if(c == NE){ 161010927Srrh release(p); 161110927Srrh load(); 161210927Srrh return(1); 161310927Srrh } 161410927Srrh fsfile(p); 161510927Srrh cc = sbackc(p); 161610927Srrh release(p); 161710927Srrh if((cc<0 && (c == '<' || c == NG)) || 161810927Srrh (cc >0) && (c == '>' || c == NL)){ 161910927Srrh readc(); 162010927Srrh return(0); 162110927Srrh } 162210927Srrh load(); 162310927Srrh return(1); 162410927Srrh } 162510927Srrh load(){ 162610927Srrh register int c; 162710927Srrh register struct blk *p,*q; 162810927Srrh struct blk *t,*s; 162910927Srrh c = readc() & 0377; 163010927Srrh sptr = stable[c]; 163110927Srrh if(sptr != 0){ 163210927Srrh p = sptr->val; 163310927Srrh if(c >= ARRAYST){ 163410927Srrh q = salloc(length(p)); 163510927Srrh rewind(p); 163610927Srrh while(sfeof(p) == 0){ 163710927Srrh s = getwd(p); 163810927Srrh if(s == 0){putwd(q, (struct blk *)NULL);} 163910927Srrh else{ 164010927Srrh t = copy(s,length(s)); 164110927Srrh putwd(q,t); 164210927Srrh } 164310927Srrh } 164410927Srrh pushp(q); 164510927Srrh } 164610927Srrh else{ 164710927Srrh q = copy(p,length(p)); 164810927Srrh pushp(q); 164910927Srrh } 165010927Srrh } 165110927Srrh else{ 165210927Srrh q = salloc(1); 165310927Srrh sputc(q,0); 165410927Srrh pushp(q); 165510927Srrh } 165610927Srrh return; 165710927Srrh } 165810927Srrh log2(n) 165910927Srrh long n; 166010927Srrh { 166110927Srrh register int i; 166210927Srrh 166310927Srrh if(n == 0)return(0); 166410927Srrh i=31; 166510927Srrh if(n<0)return(i); 166610927Srrh while((n= n<<1) >0)i--; 166710927Srrh return(--i); 166810927Srrh } 166910927Srrh 167010927Srrh struct blk * 167110927Srrh salloc(size) 167210927Srrh int size; 167310927Srrh { 167410927Srrh register struct blk *hdr; 167510927Srrh register char *ptr; 167610927Srrh all++; 167710927Srrh nbytes += size; 167810927Srrh ptr = malloc((unsigned)size); 167910927Srrh if(ptr == 0){ 168010927Srrh garbage("salloc"); 168110927Srrh if((ptr = malloc((unsigned)size)) == 0) 168210927Srrh ospace("salloc"); 168310927Srrh } 168410927Srrh if((hdr = hfree) == 0)hdr = morehd(); 168510927Srrh hfree = (struct blk *)hdr->rd; 168610927Srrh hdr->rd = hdr->wt = hdr->beg = ptr; 168710927Srrh hdr->last = ptr+size; 168810927Srrh return(hdr); 168910927Srrh } 169010927Srrh struct blk * 169110927Srrh morehd(){ 169210927Srrh register struct blk *h,*kk; 169310927Srrh headmor++; 169410927Srrh nbytes += HEADSZ; 169510927Srrh hfree = h = (struct blk *)malloc(HEADSZ); 169610927Srrh if(hfree == 0){ 169710927Srrh garbage("morehd"); 169810927Srrh if((hfree = h = (struct blk *)malloc(HEADSZ)) == 0) 169910927Srrh ospace("headers"); 170010927Srrh } 170110927Srrh kk = h; 170210927Srrh while(h<hfree+(HEADSZ/BLK))(h++)->rd = (char *)++kk; 170310927Srrh (--h)->rd=0; 170410927Srrh return(hfree); 170510927Srrh } 170610927Srrh /* 170710927Srrh sunputc(hptr) 170810927Srrh struct blk *hptr; 170910927Srrh { 171010927Srrh hptr->wt--; 171110927Srrh hptr->rd = hptr->wt; 171210927Srrh return(*hptr->wt); 171310927Srrh } 171410927Srrh */ 171510927Srrh struct blk * 171610927Srrh copy(hptr,size) 171710927Srrh struct blk *hptr; 171810927Srrh int size; 171910927Srrh { 172010927Srrh register struct blk *hdr; 172110927Srrh register unsigned sz; 172210927Srrh register char *ptr; 172310927Srrh 172410927Srrh all++; 172510927Srrh nbytes += size; 172610927Srrh sz = length(hptr); 172710927Srrh ptr = nalloc(hptr->beg, (unsigned)size); 172810927Srrh if(ptr == 0){ 172910927Srrh garbage("copy"); 173010927Srrh if((ptr = nalloc(hptr->beg, (unsigned)size)) == NULL){ 173110927Srrh printf("copy size %d\n",size); 173210927Srrh ospace("copy"); 173310927Srrh } 173410927Srrh } 173510927Srrh if((hdr = hfree) == 0)hdr = morehd(); 173610927Srrh hfree = (struct blk *)hdr->rd; 173710927Srrh hdr->rd = hdr->beg = ptr; 173810927Srrh hdr->last = ptr+size; 173910927Srrh hdr->wt = ptr+sz; 174010927Srrh ptr = hdr->wt; 174110927Srrh while(ptr<hdr->last)*ptr++ = '\0'; 174210927Srrh return(hdr); 174310927Srrh } 174410927Srrh sdump(s1,hptr) 174510927Srrh char *s1; 174610927Srrh struct blk *hptr; 174710927Srrh { 174810927Srrh char *p; 174910927Srrh printf("%s %o rd %o wt %o beg %o last %o\n",s1,hptr,hptr->rd,hptr->wt,hptr->beg,hptr->last); 175010927Srrh p = hptr->beg; 175110927Srrh while(p < hptr->wt)printf("%d ",*p++); 175210927Srrh printf("\n"); 175310927Srrh } 175410927Srrh seekc(hptr,n) 175510927Srrh struct blk *hptr; 175610927Srrh { 175710927Srrh register char *nn,*p; 175810927Srrh 175910927Srrh nn = hptr->beg+n; 176010927Srrh if(nn > hptr->last){ 176110927Srrh nbytes += nn - hptr->last; 176210927Srrh p = realloc(hptr->beg, (unsigned)n); 176310927Srrh if(p == 0){ 176410927Srrh hptr->beg = realloc(hptr->beg, (unsigned)(hptr->last-hptr->beg)); 176510927Srrh garbage("seekc"); 176610927Srrh if((p = realloc(hptr->beg, (unsigned)n)) == 0) 176710927Srrh ospace("seekc"); 176810927Srrh } 176910927Srrh hptr->beg = p; 177010927Srrh hptr->wt = hptr->last = hptr->rd = p+n; 177110927Srrh return; 177210927Srrh } 177310927Srrh hptr->rd = nn; 177410927Srrh if(nn>hptr->wt)hptr->wt = nn; 177510927Srrh return; 177610927Srrh } 177710927Srrh salterwd(hptr,n) 177810927Srrh struct wblk *hptr; 177910927Srrh struct blk *n; 178010927Srrh { 178110927Srrh if(hptr->rdw == hptr->lastw)more(hptr); 178210927Srrh *hptr->rdw++ = n; 178310927Srrh if(hptr->rdw > hptr->wtw)hptr->wtw = hptr->rdw; 178410927Srrh return; 178510927Srrh } 178610927Srrh more(hptr) 178710927Srrh struct blk *hptr; 178810927Srrh { 178910927Srrh register unsigned size; 179010927Srrh register char *p; 179110927Srrh 179210927Srrh if((size=(hptr->last-hptr->beg)*2) == 0)size=1; 179310927Srrh nbytes += size/2; 179410927Srrh p = realloc(hptr->beg, (unsigned)size); 179510927Srrh if(p == 0){ 179610927Srrh hptr->beg = realloc(hptr->beg, (unsigned)(hptr->last-hptr->beg)); 179710927Srrh garbage("more"); 1798*27447Slepreau if((p = realloc(hptr->beg,(unsigned)size)) == 0) 179910927Srrh ospace("more"); 180010927Srrh } 180110927Srrh hptr->rd = hptr->rd-hptr->beg+p; 180210927Srrh hptr->wt = hptr->wt-hptr->beg+p; 180310927Srrh hptr->beg = p; 180410927Srrh hptr->last = p+size; 180510927Srrh return; 180610927Srrh } 180710927Srrh ospace(s) 180810927Srrh char *s; 180910927Srrh { 181010927Srrh printf("out of space: %s\n",s); 181110927Srrh printf("all %ld rel %ld headmor %ld\n",all,rel,headmor); 181210927Srrh printf("nbytes %ld\n",nbytes); 181310927Srrh sdump("stk",*stkptr); 181410927Srrh abort(); 181510927Srrh } 181610927Srrh garbage(s) 181710927Srrh char *s; 181810927Srrh { 181910927Srrh int i; 182010927Srrh struct blk *p, *q; 182110927Srrh struct sym *tmps; 182210927Srrh int ct; 182310927Srrh 182410927Srrh /* printf("got to garbage %s\n",s); */ 182510927Srrh for(i=0;i<TBLSZ;i++){ 182610927Srrh tmps = stable[i]; 182710927Srrh if(tmps != 0){ 182810927Srrh if(i < ARRAYST){ 182910927Srrh do { 183010927Srrh p = tmps->val; 183110927Srrh if(((int)p->beg & 01) != 0){ 183210927Srrh printf("string %o\n",i); 183310927Srrh sdump("odd beg",p); 183410927Srrh } 183510927Srrh redef(p); 183610927Srrh tmps = tmps->next; 183710927Srrh } while(tmps != 0); 183810927Srrh continue; 183910927Srrh } 184010927Srrh else { 184110927Srrh do { 184210927Srrh p = tmps->val; 184310927Srrh rewind(p); 184410927Srrh ct = 0; 184510927Srrh while((q = getwd(p)) != NULL){ 184610927Srrh ct++; 184710927Srrh if(q != 0){ 184810927Srrh if(((int)q->beg & 01) != 0){ 184910927Srrh printf("array %o elt %d odd\n",i-ARRAYST,ct); 185010927Srrh printf("tmps %o p %o\n",tmps,p); 185110927Srrh sdump("elt",q); 185210927Srrh } 185310927Srrh redef(q); 185410927Srrh } 185510927Srrh } 185610927Srrh tmps = tmps->next; 185710927Srrh } while(tmps != 0); 185810927Srrh } 185910927Srrh } 186010927Srrh } 186110927Srrh } 186210927Srrh redef(p) 186310927Srrh struct blk *p; 186410927Srrh { 186510927Srrh register offset; 186610927Srrh register char *newp; 186710927Srrh 186810927Srrh if ((int)p->beg&01) { 186910927Srrh printf("odd ptr %o hdr %o\n",p->beg,p); 187010927Srrh ospace("redef-bad"); 187110927Srrh } 187210927Srrh newp = realloc(p->beg, (unsigned)(p->last-p->beg)); 187310927Srrh if(newp == NULL)ospace("redef"); 187410927Srrh offset = newp - p->beg; 187510927Srrh p->beg = newp; 187610927Srrh p->rd += offset; 187710927Srrh p->wt += offset; 187810927Srrh p->last += offset; 187910927Srrh } 188010927Srrh 188110927Srrh release(p) 188210927Srrh register struct blk *p; 188310927Srrh { 188410927Srrh rel++; 188510927Srrh nbytes -= p->last - p->beg; 188610927Srrh p->rd = (char *)hfree; 188710927Srrh hfree = p; 188810927Srrh free(p->beg); 188910927Srrh } 189010927Srrh 189110927Srrh struct blk * 189210927Srrh getwd(p) 189310927Srrh struct blk *p; 189410927Srrh { 189510927Srrh register struct wblk *wp; 189610927Srrh 189710927Srrh wp = (struct wblk *)p; 189810927Srrh if (wp->rdw == wp->wtw) 189910927Srrh return(NULL); 190010927Srrh return(*wp->rdw++); 190110927Srrh } 190210927Srrh 190310927Srrh putwd(p, c) 190410927Srrh struct blk *p, *c; 190510927Srrh { 190610927Srrh register struct wblk *wp; 190710927Srrh 190810927Srrh wp = (struct wblk *)p; 190910927Srrh if (wp->wtw == wp->lastw) 191010927Srrh more(p); 191110927Srrh *wp->wtw++ = c; 191210927Srrh } 191310927Srrh 191410927Srrh struct blk * 191510927Srrh lookwd(p) 191610927Srrh struct blk *p; 191710927Srrh { 191810927Srrh register struct wblk *wp; 191910927Srrh 192010927Srrh wp = (struct wblk *)p; 192110927Srrh if (wp->rdw == wp->wtw) 192210927Srrh return(NULL); 192310927Srrh return(*wp->rdw); 192410927Srrh } 192510927Srrh char * 192610927Srrh nalloc(p,nbytes) 192710927Srrh register char *p; 192810927Srrh unsigned nbytes; 192910927Srrh { 193010927Srrh char *malloc(); 193110927Srrh register char *q, *r; 193210927Srrh q = r = malloc(nbytes); 193310927Srrh if(q==0) 193410927Srrh return(0); 193510927Srrh while(nbytes--) 193610927Srrh *q++ = *p++; 193710927Srrh return(r); 193810927Srrh } 1939