xref: /csrg-svn/usr.bin/dc/dc.c (revision 11774)
110927Srrh #ifndef lint
2*11774Ssam static char sccsid[] = "@(#)dc.c	4.2	(Berkeley)	03/30/83";
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 	dummy = malloc(1);
89110927Srrh 	scalptr = salloc(1);
89210927Srrh 	sputc(scalptr,0);
89310927Srrh 	basptr = salloc(1);
89410927Srrh 	sputc(basptr,10);
89510927Srrh 	obase=10;
89610927Srrh 	log10=log2(10L);
89710927Srrh 	ll=70;
89810927Srrh 	fw=1;
89910927Srrh 	fw1=0;
90010927Srrh 	tenptr = salloc(1);
90110927Srrh 	sputc(tenptr,10);
90210927Srrh 	obase=10;
90310927Srrh 	inbas = salloc(1);
90410927Srrh 	sputc(inbas,10);
90510927Srrh 	sqtemp = salloc(1);
90610927Srrh 	sputc(sqtemp,2);
90710927Srrh 	chptr = salloc(0);
90810927Srrh 	strptr = salloc(0);
90910927Srrh 	divxyz = salloc(0);
91010927Srrh 	stkbeg = stkptr = &stack[0];
91110927Srrh 	stkend = &stack[STKSZ];
91210927Srrh 	stkerr = 0;
91310927Srrh 	readptr = &readstk[0];
91410927Srrh 	k=0;
91510927Srrh 	sp = sptr = &symlst[0];
916*11774Ssam 	while(sptr < &symlst[TBLSZ-1]){
91710927Srrh 		sptr->next = ++sp;
91810927Srrh 		sptr++;
91910927Srrh 	}
92010927Srrh 	sptr->next=0;
92110927Srrh 	sfree = &symlst[0];
92210927Srrh 	return;
92310927Srrh }
92410927Srrh onintr(){
92510927Srrh 
92610927Srrh 	signal(SIGINT,onintr);
92710927Srrh 	while(readptr != &readstk[0]){
92810927Srrh 		if(*readptr != 0){release(*readptr);}
92910927Srrh 		readptr--;
93010927Srrh 	}
93110927Srrh 	curfile = stdin;
93210927Srrh 	commnds();
93310927Srrh }
93410927Srrh pushp(p)
93510927Srrh struct blk *p;
93610927Srrh {
93710927Srrh 	if(stkptr == stkend){
93810927Srrh 		printf("out of stack space\n");
93910927Srrh 		return;
94010927Srrh 	}
94110927Srrh 	stkerr=0;
94210927Srrh 	*++stkptr = p;
94310927Srrh 	return;
94410927Srrh }
94510927Srrh struct blk *
94610927Srrh pop(){
94710927Srrh 	if(stkptr == stack){
94810927Srrh 		stkerr=1;
94910927Srrh 		return(0);
95010927Srrh 	}
95110927Srrh 	return(*stkptr--);
95210927Srrh }
95310927Srrh struct blk *
95410927Srrh readin(){
95510927Srrh 	register struct blk *p,*q;
95610927Srrh 	int dp,dpct;
95710927Srrh 	register int c;
95810927Srrh 
95910927Srrh 	dp = dpct=0;
96010927Srrh 	p = salloc(0);
96110927Srrh 	while(1){
96210927Srrh 		c = readc();
96310927Srrh 		switch(c){
96410927Srrh 		case '.':
96510927Srrh 			if(dp != 0){
96610927Srrh 				unreadc(c);
96710927Srrh 				break;
96810927Srrh 			}
96910927Srrh 			dp++;
97010927Srrh 			continue;
97110927Srrh 		case '\\':
97210927Srrh 			readc();
97310927Srrh 			continue;
97410927Srrh 		default:
97510927Srrh 			if(c >= 'A' && c <= 'F')c = c - 'A' + 10;
97610927Srrh 			else if(c >= '0' && c <= '9')c -= '0';
97710927Srrh 			else goto gotnum;
97810927Srrh 			if(dp != 0){
97910927Srrh 				if(dpct >= 99)continue;
98010927Srrh 				dpct++;
98110927Srrh 			}
98210927Srrh 			create(chptr);
98310927Srrh 			if(c != 0)sputc(chptr,c);
98410927Srrh 			q = mult(p,inbas);
98510927Srrh 			release(p);
98610927Srrh 			p = add(chptr,q);
98710927Srrh 			release(q);
98810927Srrh 		}
98910927Srrh 	}
99010927Srrh gotnum:
99110927Srrh 	unreadc(c);
99210927Srrh 	if(dp == 0){
99310927Srrh 		sputc(p,0);
99410927Srrh 		return(p);
99510927Srrh 	}
99610927Srrh 	else{
99710927Srrh 		q = scale(p,dpct);
99810927Srrh 		return(q);
99910927Srrh 	}
100010927Srrh }
100110927Srrh struct blk *
100210927Srrh add0(p,ct)
100310927Srrh int ct;
100410927Srrh struct blk *p;
100510927Srrh {
100610927Srrh 		/* returns pointer to struct with ct 0's & p */
100710927Srrh 	register struct blk *q,*t;
100810927Srrh 
100910927Srrh 	q = salloc(length(p)+(ct+1)/2);
101010927Srrh 	while(ct>1){
101110927Srrh 		sputc(q,0);
101210927Srrh 		ct -= 2;
101310927Srrh 	}
101410927Srrh 	rewind(p);
101510927Srrh 	while(sfeof(p) == 0){
101610927Srrh 		sputc(q,sgetc(p));
101710927Srrh 	}
101810927Srrh 	release(p);
101910927Srrh 	if(ct == 1){
102010927Srrh 		t = mult(tenptr,q);
102110927Srrh 		release(q);
102210927Srrh 		return(t);
102310927Srrh 	}
102410927Srrh 	return(q);
102510927Srrh }
102610927Srrh struct blk *
102710927Srrh mult(p,q)
102810927Srrh struct blk *p,*q;
102910927Srrh {
103010927Srrh 	register struct blk *mp,*mq,*mr;
103110927Srrh 	int sign,offset,carry;
103210927Srrh 	int cq,cp,mt,mcr;
103310927Srrh 
103410927Srrh 	offset = sign = 0;
103510927Srrh 	fsfile(p);
103610927Srrh 	mp = p;
103710927Srrh 	if(sfbeg(p) == 0){
103810927Srrh 		if(sbackc(p)<0){
103910927Srrh 			mp = copy(p,length(p));
104010927Srrh 			chsign(mp);
104110927Srrh 			sign = ~sign;
104210927Srrh 		}
104310927Srrh 	}
104410927Srrh 	fsfile(q);
104510927Srrh 	mq = q;
104610927Srrh 	if(sfbeg(q) == 0){
104710927Srrh 		if(sbackc(q)<0){
104810927Srrh 			mq = copy(q,length(q));
104910927Srrh 			chsign(mq);
105010927Srrh 			sign = ~sign;
105110927Srrh 		}
105210927Srrh 	}
105310927Srrh 	mr = salloc(length(mp)+length(mq));
105410927Srrh 	zero(mr);
105510927Srrh 	rewind(mq);
105610927Srrh 	while(sfeof(mq) == 0){
105710927Srrh 		cq = sgetc(mq);
105810927Srrh 		rewind(mp);
105910927Srrh 		rewind(mr);
106010927Srrh 		mr->rd += offset;
106110927Srrh 		carry=0;
106210927Srrh 		while(sfeof(mp) == 0){
106310927Srrh 			cp = sgetc(mp);
106410927Srrh 			mcr = sfeof(mr)?0:slookc(mr);
106510927Srrh 			mt = cp*cq + carry + mcr;
106610927Srrh 			carry = mt/100;
106710927Srrh 			salterc(mr,mt%100);
106810927Srrh 		}
106910927Srrh 		offset++;
107010927Srrh 		if(carry != 0){
107110927Srrh 			mcr = sfeof(mr)?0:slookc(mr);
107210927Srrh 			salterc(mr,mcr+carry);
107310927Srrh 		}
107410927Srrh 	}
107510927Srrh 	if(sign < 0){
107610927Srrh 		chsign(mr);
107710927Srrh 	}
107810927Srrh 	if(mp != p)release(mp);
107910927Srrh 	if(mq != q)release(mq);
108010927Srrh 	return(mr);
108110927Srrh }
108210927Srrh chsign(p)
108310927Srrh struct blk *p;
108410927Srrh {
108510927Srrh 	register int carry;
108610927Srrh 	register char ct;
108710927Srrh 
108810927Srrh 	carry=0;
108910927Srrh 	rewind(p);
109010927Srrh 	while(sfeof(p) == 0){
109110927Srrh 		ct=100-slookc(p)-carry;
109210927Srrh 		carry=1;
109310927Srrh 		if(ct>=100){
109410927Srrh 			ct -= 100;
109510927Srrh 			carry=0;
109610927Srrh 		}
109710927Srrh 		salterc(p,ct);
109810927Srrh 	}
109910927Srrh 	if(carry != 0){
110010927Srrh 		sputc(p,-1);
110110927Srrh 		fsfile(p);
110210927Srrh 		sbackc(p);
110310927Srrh 		ct = sbackc(p);
110410927Srrh 		if(ct == 99){
110510927Srrh 			truncate(p);
110610927Srrh 			sputc(p,-1);
110710927Srrh 		}
110810927Srrh 	}
110910927Srrh 	else{
111010927Srrh 		fsfile(p);
111110927Srrh 		ct = sbackc(p);
111210927Srrh 		if(ct == 0)truncate(p);
111310927Srrh 	}
111410927Srrh 	return;
111510927Srrh }
111610927Srrh readc(){
111710927Srrh loop:
111810927Srrh 	if((readptr != &readstk[0]) && (*readptr != 0)){
111910927Srrh 		if(sfeof(*readptr) == 0)return(lastchar = sgetc(*readptr));
112010927Srrh 		release(*readptr);
112110927Srrh 		readptr--;
112210927Srrh 		goto loop;
112310927Srrh 	}
112410927Srrh 	lastchar = getc(curfile);
112510927Srrh 	if(lastchar != EOF)return(lastchar);
112610927Srrh 	if(readptr != &readptr[0]){
112710927Srrh 		readptr--;
112810927Srrh 		if(*readptr == 0)curfile = stdin;
112910927Srrh 		goto loop;
113010927Srrh 	}
113110927Srrh 	if(curfile != stdin){
113210927Srrh 		fclose(curfile);
113310927Srrh 		curfile = stdin;
113410927Srrh 		goto loop;
113510927Srrh 	}
113610927Srrh 	exit(0);
113710927Srrh }
113810927Srrh unreadc(c)
113910927Srrh char c;
114010927Srrh {
114110927Srrh 
114210927Srrh 	if((readptr != &readstk[0]) && (*readptr != 0)){
114310927Srrh 		sungetc(*readptr,c);
114410927Srrh 	}
114510927Srrh 	else ungetc(c,curfile);
114610927Srrh 	return;
114710927Srrh }
114810927Srrh binop(c)
114910927Srrh char c;
115010927Srrh {
115110927Srrh 	register struct blk *r;
115210927Srrh 
115310927Srrh 	switch(c){
115410927Srrh 	case '+':
115510927Srrh 		r = add(arg1,arg2);
115610927Srrh 		break;
115710927Srrh 	case '*':
115810927Srrh 		r = mult(arg1,arg2);
115910927Srrh 		break;
116010927Srrh 	case '/':
116110927Srrh 		r = div(arg1,arg2);
116210927Srrh 		break;
116310927Srrh 	}
116410927Srrh 	release(arg1);
116510927Srrh 	release(arg2);
116610927Srrh 	sputc(r,savk);
116710927Srrh 	pushp(r);
116810927Srrh 	return;
116910927Srrh }
117010927Srrh print(hptr)
117110927Srrh struct blk *hptr;
117210927Srrh {
117310927Srrh 	int sc;
117410927Srrh 	register struct blk *p,*q,*dec;
117510927Srrh 	int dig,dout,ct;
117610927Srrh 
117710927Srrh 	rewind(hptr);
117810927Srrh 	while(sfeof(hptr) == 0){
117910927Srrh 		if(sgetc(hptr)>99){
118010927Srrh 			rewind(hptr);
118110927Srrh 			while(sfeof(hptr) == 0){
118210927Srrh 				printf("%c",sgetc(hptr));
118310927Srrh 			}
118410927Srrh 			printf("\n");
118510927Srrh 			return;
118610927Srrh 		}
118710927Srrh 	}
118810927Srrh 	fsfile(hptr);
118910927Srrh 	sc = sbackc(hptr);
119010927Srrh 	if(sfbeg(hptr) != 0){
119110927Srrh 		printf("0\n");
119210927Srrh 		return;
119310927Srrh 	}
119410927Srrh 	count = ll;
119510927Srrh 	p = copy(hptr,length(hptr));
119610927Srrh 	sunputc(p);
119710927Srrh 	fsfile(p);
119810927Srrh 	if(sbackc(p)<0){
119910927Srrh 		chsign(p);
120010927Srrh 		OUTC('-');
120110927Srrh 	}
120210927Srrh 	if((obase == 0) || (obase == -1)){
120310927Srrh 		oneot(p,sc,'d');
120410927Srrh 		return;
120510927Srrh 	}
120610927Srrh 	if(obase == 1){
120710927Srrh 		oneot(p,sc,'1');
120810927Srrh 		return;
120910927Srrh 	}
121010927Srrh 	if(obase == 10){
121110927Srrh 		tenot(p,sc);
121210927Srrh 		return;
121310927Srrh 	}
121410927Srrh 	create(strptr);
121510927Srrh 	dig = log10*sc;
121610927Srrh 	dout = ((dig/10) + dig) /logo;
121710927Srrh 	dec = getdec(p,sc);
121810927Srrh 	p = removc(p,sc);
121910927Srrh 	while(length(p) != 0){
122010927Srrh 		q = div(p,basptr);
122110927Srrh 		release(p);
122210927Srrh 		p = q;
122310927Srrh 		(*outdit)(rem,0);
122410927Srrh 	}
122510927Srrh 	release(p);
122610927Srrh 	fsfile(strptr);
122710927Srrh 	while(sfbeg(strptr) == 0)OUTC(sbackc(strptr));
122810927Srrh 	if(sc == 0){
122910927Srrh 		release(dec);
123010927Srrh 		printf("\n");
123110927Srrh 		return;
123210927Srrh 	}
123310927Srrh 	create(strptr);
123410927Srrh 	OUTC('.');
123510927Srrh 	ct=0;
123610927Srrh 	do{
123710927Srrh 		q = mult(basptr,dec);
123810927Srrh 		release(dec);
123910927Srrh 		dec = getdec(q,sc);
124010927Srrh 		p = removc(q,sc);
124110927Srrh 		(*outdit)(p,1);
124210927Srrh 	}while(++ct < dout);
124310927Srrh 	release(dec);
124410927Srrh 	rewind(strptr);
124510927Srrh 	while(sfeof(strptr) == 0)OUTC(sgetc(strptr));
124610927Srrh 	printf("\n");
124710927Srrh 	return;
124810927Srrh }
124910927Srrh 
125010927Srrh struct blk *
125110927Srrh getdec(p,sc)
125210927Srrh struct blk *p;
125310927Srrh {
125410927Srrh 	int cc;
125510927Srrh 	register struct blk *q,*t,*s;
125610927Srrh 
125710927Srrh 	rewind(p);
125810927Srrh 	if(length(p)*2 < sc){
125910927Srrh 		q = copy(p,length(p));
126010927Srrh 		return(q);
126110927Srrh 	}
126210927Srrh 	q = salloc(length(p));
126310927Srrh 	while(sc >= 1){
126410927Srrh 		sputc(q,sgetc(p));
126510927Srrh 		sc -= 2;
126610927Srrh 	}
126710927Srrh 	if(sc != 0){
126810927Srrh 		t = mult(q,tenptr);
126910927Srrh 		s = salloc(cc = length(q));
127010927Srrh 		release(q);
127110927Srrh 		rewind(t);
127210927Srrh 		while(cc-- > 0)sputc(s,sgetc(t));
127310927Srrh 		sputc(s,0);
127410927Srrh 		release(t);
127510927Srrh 		t = div(s,tenptr);
127610927Srrh 		release(s);
127710927Srrh 		release(rem);
127810927Srrh 		return(t);
127910927Srrh 	}
128010927Srrh 	return(q);
128110927Srrh }
128210927Srrh tenot(p,sc)
128310927Srrh struct blk *p;
128410927Srrh {
128510927Srrh 	register int c,f;
128610927Srrh 
128710927Srrh 	fsfile(p);
128810927Srrh 	f=0;
128910927Srrh 	while((sfbeg(p) == 0) && ((p->rd-p->beg-1)*2 >= sc)){
129010927Srrh 		c = sbackc(p);
129110927Srrh 		if((c<10) && (f == 1))printf("0%d",c);
129210927Srrh 		else printf("%d",c);
129310927Srrh 		f=1;
129410927Srrh 		TEST2;
129510927Srrh 	}
129610927Srrh 	if(sc == 0){
129710927Srrh 		printf("\n");
129810927Srrh 		release(p);
129910927Srrh 		return;
130010927Srrh 	}
130110927Srrh 	if((p->rd-p->beg)*2 > sc){
130210927Srrh 		c = sbackc(p);
130310927Srrh 		printf("%d.",c/10);
130410927Srrh 		TEST2;
130510927Srrh 		OUTC(c%10 +'0');
130610927Srrh 		sc--;
130710927Srrh 	}
130810927Srrh 	else {
130910927Srrh 		OUTC('.');
131010927Srrh 	}
131110927Srrh 	if(sc > (p->rd-p->beg)*2){
131210927Srrh 		while(sc>(p->rd-p->beg)*2){
131310927Srrh 			OUTC('0');
131410927Srrh 			sc--;
131510927Srrh 		}
131610927Srrh 	}
131710927Srrh 	while(sc > 1){
131810927Srrh 		c = sbackc(p);
131910927Srrh 		if(c<10)printf("0%d",c);
132010927Srrh 		else printf("%d",c);
132110927Srrh 		sc -= 2;
132210927Srrh 		TEST2;
132310927Srrh 	}
132410927Srrh 	if(sc == 1){
132510927Srrh 		OUTC(sbackc(p)/10 +'0');
132610927Srrh 	}
132710927Srrh 	printf("\n");
132810927Srrh 	release(p);
132910927Srrh 	return;
133010927Srrh }
133110927Srrh oneot(p,sc,ch)
133210927Srrh struct blk *p;
133310927Srrh char ch;
133410927Srrh {
133510927Srrh 	register struct blk *q;
133610927Srrh 
133710927Srrh 	q = removc(p,sc);
133810927Srrh 	create(strptr);
133910927Srrh 	sputc(strptr,-1);
134010927Srrh 	while(length(q)>0){
134110927Srrh 		p = add(strptr,q);
134210927Srrh 		release(q);
134310927Srrh 		q = p;
134410927Srrh 		OUTC(ch);
134510927Srrh 	}
134610927Srrh 	release(q);
134710927Srrh 	printf("\n");
134810927Srrh 	return;
134910927Srrh }
135010927Srrh hexot(p,flg)
135110927Srrh struct blk *p;
135210927Srrh {
135310927Srrh 	register int c;
135410927Srrh 	rewind(p);
135510927Srrh 	if(sfeof(p) != 0){
135610927Srrh 		sputc(strptr,'0');
135710927Srrh 		release(p);
135810927Srrh 		return;
135910927Srrh 	}
136010927Srrh 	c = sgetc(p);
136110927Srrh 	release(p);
136210927Srrh 	if(c >= 16){
136310927Srrh 		printf("hex digit > 16");
136410927Srrh 		return;
136510927Srrh 	}
136610927Srrh 	sputc(strptr,c<10?c+'0':c-10+'A');
136710927Srrh 	return;
136810927Srrh }
136910927Srrh bigot(p,flg)
137010927Srrh struct blk *p;
137110927Srrh {
137210927Srrh 	register struct blk *t,*q;
137310927Srrh 	register int l;
137410927Srrh 	int neg;
137510927Srrh 
137610927Srrh 	if(flg == 1)t = salloc(0);
137710927Srrh 	else{
137810927Srrh 		t = strptr;
137910927Srrh 		l = length(strptr)+fw-1;
138010927Srrh 	}
138110927Srrh 	neg=0;
138210927Srrh 	if(length(p) != 0){
138310927Srrh 		fsfile(p);
138410927Srrh 		if(sbackc(p)<0){
138510927Srrh 			neg=1;
138610927Srrh 			chsign(p);
138710927Srrh 		}
138810927Srrh 		while(length(p) != 0){
138910927Srrh 			q = div(p,tenptr);
139010927Srrh 			release(p);
139110927Srrh 			p = q;
139210927Srrh 			rewind(rem);
139310927Srrh 			sputc(t,sfeof(rem)?'0':sgetc(rem)+'0');
139410927Srrh 			release(rem);
139510927Srrh 		}
139610927Srrh 	}
139710927Srrh 	release(p);
139810927Srrh 	if(flg == 1){
139910927Srrh 		l = fw1-length(t);
140010927Srrh 		if(neg != 0){
140110927Srrh 			l--;
140210927Srrh 			sputc(strptr,'-');
140310927Srrh 		}
140410927Srrh 		fsfile(t);
140510927Srrh 		while(l-- > 0)sputc(strptr,'0');
140610927Srrh 		while(sfbeg(t) == 0)sputc(strptr,sbackc(t));
140710927Srrh 		release(t);
140810927Srrh 	}
140910927Srrh 	else{
141010927Srrh 		l -= length(strptr);
141110927Srrh 		while(l-- > 0)sputc(strptr,'0');
141210927Srrh 		if(neg != 0){
141310927Srrh 			sunputc(strptr);
141410927Srrh 			sputc(strptr,'-');
141510927Srrh 		}
141610927Srrh 	}
141710927Srrh 	sputc(strptr,' ');
141810927Srrh 	return;
141910927Srrh }
142010927Srrh struct blk *
142110927Srrh add(a1,a2)
142210927Srrh struct blk *a1,*a2;
142310927Srrh {
142410927Srrh 	register struct blk *p;
142510927Srrh 	register int carry,n;
142610927Srrh 	int size;
142710927Srrh 	int c,n1,n2;
142810927Srrh 
142910927Srrh 	size = length(a1)>length(a2)?length(a1):length(a2);
143010927Srrh 	p = salloc(size);
143110927Srrh 	rewind(a1);
143210927Srrh 	rewind(a2);
143310927Srrh 	carry=0;
143410927Srrh 	while(--size >= 0){
143510927Srrh 		n1 = sfeof(a1)?0:sgetc(a1);
143610927Srrh 		n2 = sfeof(a2)?0:sgetc(a2);
143710927Srrh 		n = n1 + n2 + carry;
143810927Srrh 		if(n>=100){
143910927Srrh 			carry=1;
144010927Srrh 			n -= 100;
144110927Srrh 		}
144210927Srrh 		else if(n<0){
144310927Srrh 			carry = -1;
144410927Srrh 			n += 100;
144510927Srrh 		}
144610927Srrh 		else carry = 0;
144710927Srrh 		sputc(p,n);
144810927Srrh 	}
144910927Srrh 	if(carry != 0)sputc(p,carry);
145010927Srrh 	fsfile(p);
145110927Srrh 	if(sfbeg(p) == 0){
145210927Srrh 		while(sfbeg(p) == 0 && (c = sbackc(p)) == 0);
145310927Srrh 		if(c != 0)salterc(p,c);
145410927Srrh 		truncate(p);
145510927Srrh 	}
145610927Srrh 	fsfile(p);
145710927Srrh 	if(sfbeg(p) == 0 && sbackc(p) == -1){
145810927Srrh 		while((c = sbackc(p)) == 99){
145910927Srrh 			if(c == EOF)break;
146010927Srrh 		}
146110927Srrh 		sgetc(p);
146210927Srrh 		salterc(p,-1);
146310927Srrh 		truncate(p);
146410927Srrh 	}
146510927Srrh 	return(p);
146610927Srrh }
146710927Srrh eqk(){
146810927Srrh 	register struct blk *p,*q;
146910927Srrh 	register int skp;
147010927Srrh 	int skq;
147110927Srrh 
147210927Srrh 	p = pop();
147310927Srrh 	EMPTYS;
147410927Srrh 	q = pop();
147510927Srrh 	EMPTYSR(p);
147610927Srrh 	skp = sunputc(p);
147710927Srrh 	skq = sunputc(q);
147810927Srrh 	if(skp == skq){
147910927Srrh 		arg1=p;
148010927Srrh 		arg2=q;
148110927Srrh 		savk = skp;
148210927Srrh 		return(0);
148310927Srrh 	}
148410927Srrh 	else if(skp < skq){
148510927Srrh 		savk = skq;
148610927Srrh 		p = add0(p,skq-skp);
148710927Srrh 	}
148810927Srrh 	else {
148910927Srrh 		savk = skp;
149010927Srrh 		q = add0(q,skp-skq);
149110927Srrh 	}
149210927Srrh 	arg1=p;
149310927Srrh 	arg2=q;
149410927Srrh 	return(0);
149510927Srrh }
149610927Srrh struct blk *
149710927Srrh removc(p,n)
149810927Srrh struct blk *p;
149910927Srrh {
150010927Srrh 	register struct blk *q,*r;
150110927Srrh 
150210927Srrh 	rewind(p);
150310927Srrh 	while(n>1){
150410927Srrh 		sgetc(p);
150510927Srrh 		n -= 2;
150610927Srrh 	}
150710927Srrh 	q = salloc(2);
150810927Srrh 	while(sfeof(p) == 0)sputc(q,sgetc(p));
150910927Srrh 	if(n == 1){
151010927Srrh 		r = div(q,tenptr);
151110927Srrh 		release(q);
151210927Srrh 		release(rem);
151310927Srrh 		q = r;
151410927Srrh 	}
151510927Srrh 	release(p);
151610927Srrh 	return(q);
151710927Srrh }
151810927Srrh struct blk *
151910927Srrh scalint(p)
152010927Srrh struct blk *p;
152110927Srrh {
152210927Srrh 	register int n;
152310927Srrh 	n = sunputc(p);
152410927Srrh 	p = removc(p,n);
152510927Srrh 	return(p);
152610927Srrh }
152710927Srrh struct blk *
152810927Srrh scale(p,n)
152910927Srrh struct blk *p;
153010927Srrh {
153110927Srrh 	register struct blk *q,*s,*t;
153210927Srrh 
153310927Srrh 	t = add0(p,n);
153410927Srrh 	q = salloc(1);
153510927Srrh 	sputc(q,n);
153610927Srrh 	s = exp(inbas,q);
153710927Srrh 	release(q);
153810927Srrh 	q = div(t,s);
153910927Srrh 	release(t);
154010927Srrh 	release(s);
154110927Srrh 	release(rem);
154210927Srrh 	sputc(q,n);
154310927Srrh 	return(q);
154410927Srrh }
154510927Srrh subt(){
154610927Srrh 	arg1=pop();
154710927Srrh 	EMPTYS;
154810927Srrh 	savk = sunputc(arg1);
154910927Srrh 	chsign(arg1);
155010927Srrh 	sputc(arg1,savk);
155110927Srrh 	pushp(arg1);
155210927Srrh 	if(eqk() != 0)return(1);
155310927Srrh 	binop('+');
155410927Srrh 	return(0);
155510927Srrh }
155610927Srrh command(){
155710927Srrh 	int c;
155810927Srrh 	char line[100],*sl;
155910927Srrh 	register (*savint)(),pid,rpid;
156010927Srrh 	int retcode;
156110927Srrh 
156210927Srrh 	switch(c = readc()){
156310927Srrh 	case '<':
156410927Srrh 		return(cond(NL));
156510927Srrh 	case '>':
156610927Srrh 		return(cond(NG));
156710927Srrh 	case '=':
156810927Srrh 		return(cond(NE));
156910927Srrh 	default:
157010927Srrh 		sl = line;
157110927Srrh 		*sl++ = c;
157210927Srrh 		while((c = readc()) != '\n')*sl++ = c;
157310927Srrh 		*sl = 0;
157410927Srrh 		if((pid = fork()) == 0){
157510927Srrh 			execl("/bin/sh","sh","-c",line,0);
157610927Srrh 			exit(0100);
157710927Srrh 		}
157810927Srrh 		savint = signal(SIGINT, SIG_IGN);
157910927Srrh 		while((rpid = wait(&retcode)) != pid && rpid != -1);
158010927Srrh 		signal(SIGINT,savint);
158110927Srrh 		printf("!\n");
158210927Srrh 		return(0);
158310927Srrh 	}
158410927Srrh }
158510927Srrh cond(c)
158610927Srrh char c;
158710927Srrh {
158810927Srrh 	register struct blk *p;
158910927Srrh 	register char cc;
159010927Srrh 
159110927Srrh 	if(subt() != 0)return(1);
159210927Srrh 	p = pop();
159310927Srrh 	sunputc(p);
159410927Srrh 	if(length(p) == 0){
159510927Srrh 		release(p);
159610927Srrh 		if(c == '<' || c == '>' || c == NE){
159710927Srrh 			readc();
159810927Srrh 			return(0);
159910927Srrh 		}
160010927Srrh 		load();
160110927Srrh 		return(1);
160210927Srrh 	}
160310927Srrh 	else {
160410927Srrh 		if(c == '='){
160510927Srrh 			release(p);
160610927Srrh 			readc();
160710927Srrh 			return(0);
160810927Srrh 		}
160910927Srrh 	}
161010927Srrh 	if(c == NE){
161110927Srrh 		release(p);
161210927Srrh 		load();
161310927Srrh 		return(1);
161410927Srrh 	}
161510927Srrh 	fsfile(p);
161610927Srrh 	cc = sbackc(p);
161710927Srrh 	release(p);
161810927Srrh 	if((cc<0 && (c == '<' || c == NG)) ||
161910927Srrh 		(cc >0) && (c == '>' || c == NL)){
162010927Srrh 		readc();
162110927Srrh 		return(0);
162210927Srrh 	}
162310927Srrh 	load();
162410927Srrh 	return(1);
162510927Srrh }
162610927Srrh load(){
162710927Srrh 	register int c;
162810927Srrh 	register struct blk *p,*q;
162910927Srrh 	struct blk *t,*s;
163010927Srrh 	c = readc() & 0377;
163110927Srrh 	sptr = stable[c];
163210927Srrh 	if(sptr != 0){
163310927Srrh 		p = sptr->val;
163410927Srrh 		if(c >= ARRAYST){
163510927Srrh 			q = salloc(length(p));
163610927Srrh 			rewind(p);
163710927Srrh 			while(sfeof(p) == 0){
163810927Srrh 				s = getwd(p);
163910927Srrh 				if(s == 0){putwd(q, (struct blk *)NULL);}
164010927Srrh 				else{
164110927Srrh 					t = copy(s,length(s));
164210927Srrh 					putwd(q,t);
164310927Srrh 				}
164410927Srrh 			}
164510927Srrh 			pushp(q);
164610927Srrh 		}
164710927Srrh 		else{
164810927Srrh 			q = copy(p,length(p));
164910927Srrh 			pushp(q);
165010927Srrh 		}
165110927Srrh 	}
165210927Srrh 	else{
165310927Srrh 		q = salloc(1);
165410927Srrh 		sputc(q,0);
165510927Srrh 		pushp(q);
165610927Srrh 	}
165710927Srrh 	return;
165810927Srrh }
165910927Srrh log2(n)
166010927Srrh long n;
166110927Srrh {
166210927Srrh 	register int i;
166310927Srrh 
166410927Srrh 	if(n == 0)return(0);
166510927Srrh 	i=31;
166610927Srrh 	if(n<0)return(i);
166710927Srrh 	while((n= n<<1) >0)i--;
166810927Srrh 	return(--i);
166910927Srrh }
167010927Srrh 
167110927Srrh struct blk *
167210927Srrh salloc(size)
167310927Srrh int size;
167410927Srrh {
167510927Srrh 	register struct blk *hdr;
167610927Srrh 	register char *ptr;
167710927Srrh 	all++;
167810927Srrh 	nbytes += size;
167910927Srrh 	ptr = malloc((unsigned)size);
168010927Srrh 	if(ptr == 0){
168110927Srrh 		garbage("salloc");
168210927Srrh 		if((ptr = malloc((unsigned)size)) == 0)
168310927Srrh 			ospace("salloc");
168410927Srrh 	}
168510927Srrh 	if((hdr = hfree) == 0)hdr = morehd();
168610927Srrh 	hfree = (struct blk *)hdr->rd;
168710927Srrh 	hdr->rd = hdr->wt = hdr->beg = ptr;
168810927Srrh 	hdr->last = ptr+size;
168910927Srrh 	return(hdr);
169010927Srrh }
169110927Srrh struct blk *
169210927Srrh morehd(){
169310927Srrh 	register struct blk *h,*kk;
169410927Srrh 	headmor++;
169510927Srrh 	nbytes += HEADSZ;
169610927Srrh 	hfree = h = (struct blk *)malloc(HEADSZ);
169710927Srrh 	if(hfree == 0){
169810927Srrh 		garbage("morehd");
169910927Srrh 		if((hfree = h = (struct blk *)malloc(HEADSZ)) == 0)
170010927Srrh 			ospace("headers");
170110927Srrh 	}
170210927Srrh 	kk = h;
170310927Srrh 	while(h<hfree+(HEADSZ/BLK))(h++)->rd = (char *)++kk;
170410927Srrh 	(--h)->rd=0;
170510927Srrh 	return(hfree);
170610927Srrh }
170710927Srrh /*
170810927Srrh sunputc(hptr)
170910927Srrh struct blk *hptr;
171010927Srrh {
171110927Srrh 	hptr->wt--;
171210927Srrh 	hptr->rd = hptr->wt;
171310927Srrh 	return(*hptr->wt);
171410927Srrh }
171510927Srrh */
171610927Srrh struct blk *
171710927Srrh copy(hptr,size)
171810927Srrh struct blk *hptr;
171910927Srrh int size;
172010927Srrh {
172110927Srrh 	register struct blk *hdr;
172210927Srrh 	register unsigned sz;
172310927Srrh 	register char *ptr;
172410927Srrh 
172510927Srrh 	all++;
172610927Srrh 	nbytes += size;
172710927Srrh 	sz = length(hptr);
172810927Srrh 	ptr = nalloc(hptr->beg, (unsigned)size);
172910927Srrh 	if(ptr == 0){
173010927Srrh 		garbage("copy");
173110927Srrh 		if((ptr = nalloc(hptr->beg, (unsigned)size)) == NULL){
173210927Srrh 			printf("copy size %d\n",size);
173310927Srrh 			ospace("copy");
173410927Srrh 		}
173510927Srrh 	}
173610927Srrh 	if((hdr = hfree) == 0)hdr = morehd();
173710927Srrh 	hfree = (struct blk *)hdr->rd;
173810927Srrh 	hdr->rd = hdr->beg = ptr;
173910927Srrh 	hdr->last = ptr+size;
174010927Srrh 	hdr->wt = ptr+sz;
174110927Srrh 	ptr = hdr->wt;
174210927Srrh 	while(ptr<hdr->last)*ptr++ = '\0';
174310927Srrh 	return(hdr);
174410927Srrh }
174510927Srrh sdump(s1,hptr)
174610927Srrh char *s1;
174710927Srrh struct blk *hptr;
174810927Srrh {
174910927Srrh 	char *p;
175010927Srrh 	printf("%s %o rd %o wt %o beg %o last %o\n",s1,hptr,hptr->rd,hptr->wt,hptr->beg,hptr->last);
175110927Srrh 	p = hptr->beg;
175210927Srrh 	while(p < hptr->wt)printf("%d ",*p++);
175310927Srrh 	printf("\n");
175410927Srrh }
175510927Srrh seekc(hptr,n)
175610927Srrh struct blk *hptr;
175710927Srrh {
175810927Srrh 	register char *nn,*p;
175910927Srrh 
176010927Srrh 	nn = hptr->beg+n;
176110927Srrh 	if(nn > hptr->last){
176210927Srrh 		nbytes += nn - hptr->last;
176310927Srrh 		free(hptr->beg);
176410927Srrh 		p = realloc(hptr->beg, (unsigned)n);
176510927Srrh 		if(p == 0){
176610927Srrh 			hptr->beg = realloc(hptr->beg, (unsigned)(hptr->last-hptr->beg));
176710927Srrh 			garbage("seekc");
176810927Srrh 			if((p = realloc(hptr->beg, (unsigned)n)) == 0)
176910927Srrh 				ospace("seekc");
177010927Srrh 		}
177110927Srrh 		hptr->beg = p;
177210927Srrh 		hptr->wt = hptr->last = hptr->rd = p+n;
177310927Srrh 		return;
177410927Srrh 	}
177510927Srrh 	hptr->rd = nn;
177610927Srrh 	if(nn>hptr->wt)hptr->wt = nn;
177710927Srrh 	return;
177810927Srrh }
177910927Srrh salterwd(hptr,n)
178010927Srrh struct wblk *hptr;
178110927Srrh struct blk *n;
178210927Srrh {
178310927Srrh 	if(hptr->rdw == hptr->lastw)more(hptr);
178410927Srrh 	*hptr->rdw++ = n;
178510927Srrh 	if(hptr->rdw > hptr->wtw)hptr->wtw = hptr->rdw;
178610927Srrh 	return;
178710927Srrh }
178810927Srrh more(hptr)
178910927Srrh struct blk *hptr;
179010927Srrh {
179110927Srrh 	register unsigned size;
179210927Srrh 	register char *p;
179310927Srrh 
179410927Srrh 	if((size=(hptr->last-hptr->beg)*2) == 0)size=1;
179510927Srrh 	nbytes += size/2;
179610927Srrh 	free(hptr->beg);
179710927Srrh 	p = realloc(hptr->beg, (unsigned)size);
179810927Srrh 	if(p == 0){
179910927Srrh 		hptr->beg = realloc(hptr->beg, (unsigned)(hptr->last-hptr->beg));
180010927Srrh 		garbage("more");
180110927Srrh 		if((p = realloc(hptr->beg,size)) == 0)
180210927Srrh 			ospace("more");
180310927Srrh 	}
180410927Srrh 	hptr->rd = hptr->rd-hptr->beg+p;
180510927Srrh 	hptr->wt = hptr->wt-hptr->beg+p;
180610927Srrh 	hptr->beg = p;
180710927Srrh 	hptr->last = p+size;
180810927Srrh 	return;
180910927Srrh }
181010927Srrh ospace(s)
181110927Srrh char *s;
181210927Srrh {
181310927Srrh 	printf("out of space: %s\n",s);
181410927Srrh 	printf("all %ld rel %ld headmor %ld\n",all,rel,headmor);
181510927Srrh 	printf("nbytes %ld\n",nbytes);
181610927Srrh 	sdump("stk",*stkptr);
181710927Srrh 	abort();
181810927Srrh }
181910927Srrh garbage(s)
182010927Srrh char *s;
182110927Srrh {
182210927Srrh 	int i;
182310927Srrh 	struct blk *p, *q;
182410927Srrh 	struct sym *tmps;
182510927Srrh 	int ct;
182610927Srrh 
182710927Srrh /*	printf("got to garbage %s\n",s);	*/
182810927Srrh 	for(i=0;i<TBLSZ;i++){
182910927Srrh 		tmps = stable[i];
183010927Srrh 		if(tmps != 0){
183110927Srrh 			if(i < ARRAYST){
183210927Srrh 				do {
183310927Srrh 					p = tmps->val;
183410927Srrh 					if(((int)p->beg & 01)  != 0){
183510927Srrh 						printf("string %o\n",i);
183610927Srrh 						sdump("odd beg",p);
183710927Srrh 					}
183810927Srrh 					redef(p);
183910927Srrh 					tmps = tmps->next;
184010927Srrh 				} while(tmps != 0);
184110927Srrh 				continue;
184210927Srrh 			}
184310927Srrh 			else {
184410927Srrh 				do {
184510927Srrh 					p = tmps->val;
184610927Srrh 					rewind(p);
184710927Srrh 					ct = 0;
184810927Srrh 					while((q = getwd(p)) != NULL){
184910927Srrh 						ct++;
185010927Srrh 						if(q != 0){
185110927Srrh 							if(((int)q->beg & 01) != 0){
185210927Srrh 								printf("array %o elt %d odd\n",i-ARRAYST,ct);
185310927Srrh printf("tmps %o p %o\n",tmps,p);
185410927Srrh 								sdump("elt",q);
185510927Srrh 							}
185610927Srrh 							redef(q);
185710927Srrh 						}
185810927Srrh 					}
185910927Srrh 					tmps = tmps->next;
186010927Srrh 				} while(tmps != 0);
186110927Srrh 			}
186210927Srrh 		}
186310927Srrh 	}
186410927Srrh }
186510927Srrh redef(p)
186610927Srrh struct blk *p;
186710927Srrh {
186810927Srrh 	register offset;
186910927Srrh 	register char *newp;
187010927Srrh 
187110927Srrh 	if ((int)p->beg&01) {
187210927Srrh 		printf("odd ptr %o hdr %o\n",p->beg,p);
187310927Srrh 		ospace("redef-bad");
187410927Srrh 	}
187510927Srrh 	free(p->beg);
187610927Srrh 	free(dummy);
187710927Srrh 	dummy = malloc(1);
187810927Srrh 	if(dummy == NULL)ospace("dummy");
187910927Srrh 	newp = realloc(p->beg, (unsigned)(p->last-p->beg));
188010927Srrh 	if(newp == NULL)ospace("redef");
188110927Srrh 	offset = newp - p->beg;
188210927Srrh 	p->beg = newp;
188310927Srrh 	p->rd += offset;
188410927Srrh 	p->wt += offset;
188510927Srrh 	p->last += offset;
188610927Srrh }
188710927Srrh 
188810927Srrh release(p)
188910927Srrh register struct blk *p;
189010927Srrh {
189110927Srrh 	rel++;
189210927Srrh 	nbytes -= p->last - p->beg;
189310927Srrh 	p->rd = (char *)hfree;
189410927Srrh 	hfree = p;
189510927Srrh 	free(p->beg);
189610927Srrh }
189710927Srrh 
189810927Srrh struct blk *
189910927Srrh getwd(p)
190010927Srrh struct blk *p;
190110927Srrh {
190210927Srrh 	register struct wblk *wp;
190310927Srrh 
190410927Srrh 	wp = (struct wblk *)p;
190510927Srrh 	if (wp->rdw == wp->wtw)
190610927Srrh 		return(NULL);
190710927Srrh 	return(*wp->rdw++);
190810927Srrh }
190910927Srrh 
191010927Srrh putwd(p, c)
191110927Srrh struct blk *p, *c;
191210927Srrh {
191310927Srrh 	register struct wblk *wp;
191410927Srrh 
191510927Srrh 	wp = (struct wblk *)p;
191610927Srrh 	if (wp->wtw == wp->lastw)
191710927Srrh 		more(p);
191810927Srrh 	*wp->wtw++ = c;
191910927Srrh }
192010927Srrh 
192110927Srrh struct blk *
192210927Srrh lookwd(p)
192310927Srrh struct blk *p;
192410927Srrh {
192510927Srrh 	register struct wblk *wp;
192610927Srrh 
192710927Srrh 	wp = (struct wblk *)p;
192810927Srrh 	if (wp->rdw == wp->wtw)
192910927Srrh 		return(NULL);
193010927Srrh 	return(*wp->rdw);
193110927Srrh }
193210927Srrh char *
193310927Srrh nalloc(p,nbytes)
193410927Srrh register char *p;
193510927Srrh unsigned nbytes;
193610927Srrh {
193710927Srrh 	char *malloc();
193810927Srrh 	register char *q, *r;
193910927Srrh 	q = r = malloc(nbytes);
194010927Srrh 	if(q==0)
194110927Srrh 		return(0);
194210927Srrh 	while(nbytes--)
194310927Srrh 		*q++ = *p++;
194410927Srrh 	return(r);
194510927Srrh }
1946