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