xref: /csrg-svn/usr.bin/dc/dc.c (revision 10927)
1*10927Srrh #ifndef lint
2*10927Srrh static char sccsid[] = "@(#)dc.c	4.1	(Berkeley)	02/11/83";
3*10927Srrh #endif not lint
4*10927Srrh 
5*10927Srrh #include <stdio.h>
6*10927Srrh #include <signal.h>
7*10927Srrh #include "dc.h"
8*10927Srrh main(argc,argv)
9*10927Srrh int argc;
10*10927Srrh char *argv[];
11*10927Srrh {
12*10927Srrh 	init(argc,argv);
13*10927Srrh 	commnds();
14*10927Srrh }
15*10927Srrh commnds(){
16*10927Srrh 	register int c;
17*10927Srrh 	register struct blk *p,*q;
18*10927Srrh 	long l;
19*10927Srrh 	int sign;
20*10927Srrh 	struct blk **ptr,*s,*t;
21*10927Srrh 	struct sym *sp;
22*10927Srrh 	int sk,sk1,sk2;
23*10927Srrh 	int n,d;
24*10927Srrh 
25*10927Srrh 	while(1){
26*10927Srrh 		if(((c = readc())>='0' && c <= '9')|| (c>='A' && c <='F') || c == '.'){
27*10927Srrh 			unreadc(c);
28*10927Srrh 			p = readin();
29*10927Srrh 			pushp(p);
30*10927Srrh 			continue;
31*10927Srrh 		}
32*10927Srrh 		switch(c){
33*10927Srrh 		case ' ':
34*10927Srrh 		case '\n':
35*10927Srrh 		case 0377:
36*10927Srrh 		case EOF:
37*10927Srrh 			continue;
38*10927Srrh 		case 'Y':
39*10927Srrh 			sdump("stk",*stkptr);
40*10927Srrh 			printf("all %ld rel %ld headmor %ld\n",all,rel,headmor);
41*10927Srrh 			printf("nbytes %ld\n",nbytes);
42*10927Srrh 			continue;
43*10927Srrh 		case '_':
44*10927Srrh 			p = readin();
45*10927Srrh 			savk = sunputc(p);
46*10927Srrh 			chsign(p);
47*10927Srrh 			sputc(p,savk);
48*10927Srrh 			pushp(p);
49*10927Srrh 			continue;
50*10927Srrh 		case '-':
51*10927Srrh 			subt();
52*10927Srrh 			continue;
53*10927Srrh 		case '+':
54*10927Srrh 			if(eqk() != 0)continue;
55*10927Srrh 			binop('+');
56*10927Srrh 			continue;
57*10927Srrh 		case '*':
58*10927Srrh 			arg1 = pop();
59*10927Srrh 			EMPTY;
60*10927Srrh 			arg2 = pop();
61*10927Srrh 			EMPTYR(arg1);
62*10927Srrh 			sk1 = sunputc(arg1);
63*10927Srrh 			sk2 = sunputc(arg2);
64*10927Srrh 			binop('*');
65*10927Srrh 			p = pop();
66*10927Srrh 			sunputc(p);
67*10927Srrh 			savk = sk1+sk2;
68*10927Srrh 			if(savk>k && savk>sk1 && savk>sk2){
69*10927Srrh 				sk = sk1;
70*10927Srrh 				if(sk<sk2)sk = sk2;
71*10927Srrh 				if(sk<k)sk = k;
72*10927Srrh 				p = removc(p,savk-sk);
73*10927Srrh 				savk = sk;
74*10927Srrh 			}
75*10927Srrh 			sputc(p,savk);
76*10927Srrh 			pushp(p);
77*10927Srrh 			continue;
78*10927Srrh 		case '/':
79*10927Srrh casediv:
80*10927Srrh 			if(dscale() != 0)continue;
81*10927Srrh 			binop('/');
82*10927Srrh 			if(irem != 0)release(irem);
83*10927Srrh 			release(rem);
84*10927Srrh 			continue;
85*10927Srrh 		case '%':
86*10927Srrh 			if(dscale() != 0)continue;
87*10927Srrh 			binop('/');
88*10927Srrh 			p = pop();
89*10927Srrh 			release(p);
90*10927Srrh 			if(irem == 0){
91*10927Srrh 				sputc(rem,skr+k);
92*10927Srrh 				pushp(rem);
93*10927Srrh 				continue;
94*10927Srrh 			}
95*10927Srrh 			p = add0(rem,skd-(skr+k));
96*10927Srrh 			q = add(p,irem);
97*10927Srrh 			release(p);
98*10927Srrh 			release(irem);
99*10927Srrh 			sputc(q,skd);
100*10927Srrh 			pushp(q);
101*10927Srrh 			continue;
102*10927Srrh 		case 'v':
103*10927Srrh 			p = pop();
104*10927Srrh 			EMPTY;
105*10927Srrh 			savk = sunputc(p);
106*10927Srrh 			if(length(p) == 0){
107*10927Srrh 				sputc(p,savk);
108*10927Srrh 				pushp(p);
109*10927Srrh 				continue;
110*10927Srrh 			}
111*10927Srrh 			if((c = sbackc(p))<0){
112*10927Srrh 				error("sqrt of neg number\n");
113*10927Srrh 			}
114*10927Srrh 			if(k<savk)n = savk;
115*10927Srrh 			else{
116*10927Srrh 				n = k*2-savk;
117*10927Srrh 				savk = k;
118*10927Srrh 			}
119*10927Srrh 			arg1 = add0(p,n);
120*10927Srrh 			arg2 = sqrt(arg1);
121*10927Srrh 			sputc(arg2,savk);
122*10927Srrh 			pushp(arg2);
123*10927Srrh 			continue;
124*10927Srrh 		case '^':
125*10927Srrh 			neg = 0;
126*10927Srrh 			arg1 = pop();
127*10927Srrh 			EMPTY;
128*10927Srrh 			if(sunputc(arg1) != 0)error("exp not an integer\n");
129*10927Srrh 			arg2 = pop();
130*10927Srrh 			EMPTYR(arg1);
131*10927Srrh 			if(sfbeg(arg1) == 0 && sbackc(arg1)<0){
132*10927Srrh 				neg++;
133*10927Srrh 				chsign(arg1);
134*10927Srrh 			}
135*10927Srrh 			if(length(arg1)>=3){
136*10927Srrh 				error("exp too big\n");
137*10927Srrh 			}
138*10927Srrh 			savk = sunputc(arg2);
139*10927Srrh 			p = exp(arg2,arg1);
140*10927Srrh 			release(arg2);
141*10927Srrh 			rewind(arg1);
142*10927Srrh 			c = sgetc(arg1);
143*10927Srrh 			if(sfeof(arg1) == 0)
144*10927Srrh 				c = sgetc(arg1)*100 + c;
145*10927Srrh 			d = c*savk;
146*10927Srrh 			release(arg1);
147*10927Srrh 			if(neg == 0){
148*10927Srrh 				if(k>=savk)n = k;
149*10927Srrh 				else n = savk;
150*10927Srrh 				if(n<d){
151*10927Srrh 					q = removc(p,d-n);
152*10927Srrh 					sputc(q,n);
153*10927Srrh 					pushp(q);
154*10927Srrh 				}
155*10927Srrh 				else {
156*10927Srrh 					sputc(p,d);
157*10927Srrh 					pushp(p);
158*10927Srrh 				}
159*10927Srrh 			}
160*10927Srrh 			else {
161*10927Srrh 				sputc(p,d);
162*10927Srrh 				pushp(p);
163*10927Srrh 			}
164*10927Srrh 			if(neg == 0)continue;
165*10927Srrh 			p = pop();
166*10927Srrh 			q = salloc(2);
167*10927Srrh 			sputc(q,1);
168*10927Srrh 			sputc(q,0);
169*10927Srrh 			pushp(q);
170*10927Srrh 			pushp(p);
171*10927Srrh 			goto casediv;
172*10927Srrh 		case 'z':
173*10927Srrh 			p = salloc(2);
174*10927Srrh 			n = stkptr - stkbeg;
175*10927Srrh 			if(n >= 100){
176*10927Srrh 				sputc(p,n/100);
177*10927Srrh 				n %= 100;
178*10927Srrh 			}
179*10927Srrh 			sputc(p,n);
180*10927Srrh 			sputc(p,0);
181*10927Srrh 			pushp(p);
182*10927Srrh 			continue;
183*10927Srrh 		case 'Z':
184*10927Srrh 			p = pop();
185*10927Srrh 			EMPTY;
186*10927Srrh 			n = (length(p)-1)<<1;
187*10927Srrh 			fsfile(p);
188*10927Srrh 			sbackc(p);
189*10927Srrh 			if(sfbeg(p) == 0){
190*10927Srrh 				if((c = sbackc(p))<0){
191*10927Srrh 					n -= 2;
192*10927Srrh 					if(sfbeg(p) == 1)n += 1;
193*10927Srrh 					else {
194*10927Srrh 						if((c = sbackc(p)) == 0)n += 1;
195*10927Srrh 						else if(c > 90)n -= 1;
196*10927Srrh 					}
197*10927Srrh 				}
198*10927Srrh 				else if(c < 10) n -= 1;
199*10927Srrh 			}
200*10927Srrh 			release(p);
201*10927Srrh 			q = salloc(1);
202*10927Srrh 			if(n >= 100){
203*10927Srrh 				sputc(q,n%100);
204*10927Srrh 				n /= 100;
205*10927Srrh 			}
206*10927Srrh 			sputc(q,n);
207*10927Srrh 			sputc(q,0);
208*10927Srrh 			pushp(q);
209*10927Srrh 			continue;
210*10927Srrh 		case 'i':
211*10927Srrh 			p = pop();
212*10927Srrh 			EMPTY;
213*10927Srrh 			p = scalint(p);
214*10927Srrh 			release(inbas);
215*10927Srrh 			inbas = p;
216*10927Srrh 			continue;
217*10927Srrh 		case 'I':
218*10927Srrh 			p = copy(inbas,length(inbas)+1);
219*10927Srrh 			sputc(p,0);
220*10927Srrh 			pushp(p);
221*10927Srrh 			continue;
222*10927Srrh 		case 'o':
223*10927Srrh 			p = pop();
224*10927Srrh 			EMPTY;
225*10927Srrh 			p = scalint(p);
226*10927Srrh 			sign = 0;
227*10927Srrh 			n = length(p);
228*10927Srrh 			q = copy(p,n);
229*10927Srrh 			fsfile(q);
230*10927Srrh 			l = c = sbackc(q);
231*10927Srrh 			if(n != 1){
232*10927Srrh 				if(c<0){
233*10927Srrh 					sign = 1;
234*10927Srrh 					chsign(q);
235*10927Srrh 					n = length(q);
236*10927Srrh 					fsfile(q);
237*10927Srrh 					l = c = sbackc(q);
238*10927Srrh 				}
239*10927Srrh 				if(n != 1){
240*10927Srrh 					while(sfbeg(q) == 0)l = l*100+sbackc(q);
241*10927Srrh 				}
242*10927Srrh 			}
243*10927Srrh 			logo = log2(l);
244*10927Srrh 			obase = l;
245*10927Srrh 			release(basptr);
246*10927Srrh 			if(sign == 1)obase = -l;
247*10927Srrh 			basptr = p;
248*10927Srrh 			outdit = bigot;
249*10927Srrh 			if(n == 1 && sign == 0){
250*10927Srrh 				if(c <= 16){
251*10927Srrh 					outdit = hexot;
252*10927Srrh 					fw = 1;
253*10927Srrh 					fw1 = 0;
254*10927Srrh 					ll = 70;
255*10927Srrh 					release(q);
256*10927Srrh 					continue;
257*10927Srrh 				}
258*10927Srrh 			}
259*10927Srrh 			n = 0;
260*10927Srrh 			if(sign == 1)n++;
261*10927Srrh 			p = salloc(1);
262*10927Srrh 			sputc(p,-1);
263*10927Srrh 			t = add(p,q);
264*10927Srrh 			n += length(t)*2;
265*10927Srrh 			fsfile(t);
266*10927Srrh 			if((c = sbackc(t))>9)n++;
267*10927Srrh 			release(t);
268*10927Srrh 			release(q);
269*10927Srrh 			release(p);
270*10927Srrh 			fw = n;
271*10927Srrh 			fw1 = n-1;
272*10927Srrh 			ll = 70;
273*10927Srrh 			if(fw>=ll)continue;
274*10927Srrh 			ll = (70/fw)*fw;
275*10927Srrh 			continue;
276*10927Srrh 		case 'O':
277*10927Srrh 			p = copy(basptr,length(basptr)+1);
278*10927Srrh 			sputc(p,0);
279*10927Srrh 			pushp(p);
280*10927Srrh 			continue;
281*10927Srrh 		case '[':
282*10927Srrh 			n = 0;
283*10927Srrh 			p = salloc(0);
284*10927Srrh 			while(1){
285*10927Srrh 				if((c = readc()) == ']'){
286*10927Srrh 					if(n == 0)break;
287*10927Srrh 					n--;
288*10927Srrh 				}
289*10927Srrh 				sputc(p,c);
290*10927Srrh 				if(c == '[')n++;
291*10927Srrh 			}
292*10927Srrh 			pushp(p);
293*10927Srrh 			continue;
294*10927Srrh 		case 'k':
295*10927Srrh 			p = pop();
296*10927Srrh 			EMPTY;
297*10927Srrh 			p = scalint(p);
298*10927Srrh 			if(length(p)>1){
299*10927Srrh 				error("scale too big\n");
300*10927Srrh 			}
301*10927Srrh 			rewind(p);
302*10927Srrh 			k = sfeof(p)?0:sgetc(p);
303*10927Srrh 			release(scalptr);
304*10927Srrh 			scalptr = p;
305*10927Srrh 			continue;
306*10927Srrh 		case 'K':
307*10927Srrh 			p = copy(scalptr,length(scalptr)+1);
308*10927Srrh 			sputc(p,0);
309*10927Srrh 			pushp(p);
310*10927Srrh 			continue;
311*10927Srrh 		case 'X':
312*10927Srrh 			p = pop();
313*10927Srrh 			EMPTY;
314*10927Srrh 			fsfile(p);
315*10927Srrh 			n = sbackc(p);
316*10927Srrh 			release(p);
317*10927Srrh 			p = salloc(2);
318*10927Srrh 			sputc(p,n);
319*10927Srrh 			sputc(p,0);
320*10927Srrh 			pushp(p);
321*10927Srrh 			continue;
322*10927Srrh 		case 'Q':
323*10927Srrh 			p = pop();
324*10927Srrh 			EMPTY;
325*10927Srrh 			if(length(p)>2){
326*10927Srrh 				error("Q?\n");
327*10927Srrh 			}
328*10927Srrh 			rewind(p);
329*10927Srrh 			if((c =  sgetc(p))<0){
330*10927Srrh 				error("neg Q\n");
331*10927Srrh 			}
332*10927Srrh 			release(p);
333*10927Srrh 			while(c-- > 0){
334*10927Srrh 				if(readptr == &readstk[0]){
335*10927Srrh 					error("readstk?\n");
336*10927Srrh 				}
337*10927Srrh 				if(*readptr != 0)release(*readptr);
338*10927Srrh 				readptr--;
339*10927Srrh 			}
340*10927Srrh 			continue;
341*10927Srrh 		case 'q':
342*10927Srrh 			if(readptr <= &readstk[1])exit(0);
343*10927Srrh 			if(*readptr != 0)release(*readptr);
344*10927Srrh 			readptr--;
345*10927Srrh 			if(*readptr != 0)release(*readptr);
346*10927Srrh 			readptr--;
347*10927Srrh 			continue;
348*10927Srrh 		case 'f':
349*10927Srrh 			if(stkptr == &stack[0])printf("empty stack\n");
350*10927Srrh 			else {
351*10927Srrh 				for(ptr = stkptr; ptr > &stack[0];){
352*10927Srrh 					print(*ptr--);
353*10927Srrh 				}
354*10927Srrh 			}
355*10927Srrh 			continue;
356*10927Srrh 		case 'p':
357*10927Srrh 			if(stkptr == &stack[0])printf("empty stack\n");
358*10927Srrh 			else{
359*10927Srrh 				print(*stkptr);
360*10927Srrh 			}
361*10927Srrh 			continue;
362*10927Srrh 		case 'P':
363*10927Srrh 			p = pop();
364*10927Srrh 			EMPTY;
365*10927Srrh 			sputc(p,0);
366*10927Srrh 			printf("%s",p->beg);
367*10927Srrh 			release(p);
368*10927Srrh 			continue;
369*10927Srrh 		case 'd':
370*10927Srrh 			if(stkptr == &stack[0]){
371*10927Srrh 				printf("empty stack\n");
372*10927Srrh 				continue;
373*10927Srrh 			}
374*10927Srrh 			q = *stkptr;
375*10927Srrh 			n = length(q);
376*10927Srrh 			p = copy(*stkptr,n);
377*10927Srrh 			pushp(p);
378*10927Srrh 			continue;
379*10927Srrh 		case 'c':
380*10927Srrh 			while(stkerr == 0){
381*10927Srrh 				p = pop();
382*10927Srrh 				if(stkerr == 0)release(p);
383*10927Srrh 			}
384*10927Srrh 			continue;
385*10927Srrh 		case 'S':
386*10927Srrh 			if(stkptr == &stack[0]){
387*10927Srrh 				error("save: args\n");
388*10927Srrh 			}
389*10927Srrh 			c = readc() & 0377;
390*10927Srrh 			sptr = stable[c];
391*10927Srrh 			sp = stable[c] = sfree;
392*10927Srrh 			sfree = sfree->next;
393*10927Srrh 			if(sfree == 0)goto sempty;
394*10927Srrh 			sp->next = sptr;
395*10927Srrh 			p = pop();
396*10927Srrh 			EMPTY;
397*10927Srrh 			if(c >= ARRAYST){
398*10927Srrh 				q = copy(p,PTRSZ);
399*10927Srrh 				for(n = 0;n < PTRSZ-1;n++)sputc(q,0);
400*10927Srrh 				release(p);
401*10927Srrh 				p = q;
402*10927Srrh 			}
403*10927Srrh 			sp->val = p;
404*10927Srrh 			continue;
405*10927Srrh sempty:
406*10927Srrh 			error("symbol table overflow\n");
407*10927Srrh 		case 's':
408*10927Srrh 			if(stkptr == &stack[0]){
409*10927Srrh 				error("save:args\n");
410*10927Srrh 			}
411*10927Srrh 			c = readc() & 0377;
412*10927Srrh 			sptr = stable[c];
413*10927Srrh 			if(sptr != 0){
414*10927Srrh 				p = sptr->val;
415*10927Srrh 				if(c >= ARRAYST){
416*10927Srrh 					rewind(p);
417*10927Srrh 					while(sfeof(p) == 0)release(getwd(p));
418*10927Srrh 				}
419*10927Srrh 				release(p);
420*10927Srrh 			}
421*10927Srrh 			else{
422*10927Srrh 				sptr = stable[c] = sfree;
423*10927Srrh 				sfree = sfree->next;
424*10927Srrh 				if(sfree == 0)goto sempty;
425*10927Srrh 				sptr->next = 0;
426*10927Srrh 			}
427*10927Srrh 			p = pop();
428*10927Srrh 			sptr->val = p;
429*10927Srrh 			continue;
430*10927Srrh 		case 'l':
431*10927Srrh 			load();
432*10927Srrh 			continue;
433*10927Srrh 		case 'L':
434*10927Srrh 			c = readc() & 0377;
435*10927Srrh 			sptr = stable[c];
436*10927Srrh 			if(sptr == 0){
437*10927Srrh 				error("L?\n");
438*10927Srrh 			}
439*10927Srrh 			stable[c] = sptr->next;
440*10927Srrh 			sptr->next = sfree;
441*10927Srrh 			sfree = sptr;
442*10927Srrh 			p = sptr->val;
443*10927Srrh 			if(c >= ARRAYST){
444*10927Srrh 				rewind(p);
445*10927Srrh 				while(sfeof(p) == 0){
446*10927Srrh 					q = getwd(p);
447*10927Srrh 					if(q != 0)release(q);
448*10927Srrh 				}
449*10927Srrh 			}
450*10927Srrh 			pushp(p);
451*10927Srrh 			continue;
452*10927Srrh 		case ':':
453*10927Srrh 			p = pop();
454*10927Srrh 			EMPTY;
455*10927Srrh 			q = scalint(p);
456*10927Srrh 			fsfile(q);
457*10927Srrh 			c = 0;
458*10927Srrh 			if((sfbeg(q) == 0) && ((c = sbackc(q))<0)){
459*10927Srrh 				error("neg index\n");
460*10927Srrh 			}
461*10927Srrh 			if(length(q)>2){
462*10927Srrh 				error("index too big\n");
463*10927Srrh 			}
464*10927Srrh 			if(sfbeg(q) == 0)c = c*100+sbackc(q);
465*10927Srrh 			if(c >= MAXIND){
466*10927Srrh 				error("index too big\n");
467*10927Srrh 			}
468*10927Srrh 			release(q);
469*10927Srrh 			n = readc() & 0377;
470*10927Srrh 			sptr = stable[n];
471*10927Srrh 			if(sptr == 0){
472*10927Srrh 				sptr = stable[n] = sfree;
473*10927Srrh 				sfree = sfree->next;
474*10927Srrh 				if(sfree == 0)goto sempty;
475*10927Srrh 				sptr->next = 0;
476*10927Srrh 				p = salloc((c+PTRSZ)*PTRSZ);
477*10927Srrh 				zero(p);
478*10927Srrh 			}
479*10927Srrh 			else{
480*10927Srrh 				p = sptr->val;
481*10927Srrh 				if(length(p)-PTRSZ < c*PTRSZ){
482*10927Srrh 					q = copy(p,(c+PTRSZ)*PTRSZ);
483*10927Srrh 					release(p);
484*10927Srrh 					p = q;
485*10927Srrh 				}
486*10927Srrh 			}
487*10927Srrh 			seekc(p,c*PTRSZ);
488*10927Srrh 			q = lookwd(p);
489*10927Srrh 			if (q!=NULL) release(q);
490*10927Srrh 			s = pop();
491*10927Srrh 			EMPTY;
492*10927Srrh 			salterwd(p,s);
493*10927Srrh 			sptr->val = p;
494*10927Srrh 			continue;
495*10927Srrh 		case ';':
496*10927Srrh 			p = pop();
497*10927Srrh 			EMPTY;
498*10927Srrh 			q = scalint(p);
499*10927Srrh 			fsfile(q);
500*10927Srrh 			c = 0;
501*10927Srrh 			if((sfbeg(q) == 0) && ((c = sbackc(q))<0)){
502*10927Srrh 				error("neg index\n");
503*10927Srrh 			}
504*10927Srrh 			if(length(q)>2){
505*10927Srrh 				error("index too big\n");
506*10927Srrh 			}
507*10927Srrh 			if(sfbeg(q) == 0)c = c*100+sbackc(q);
508*10927Srrh 			if(c >= MAXIND){
509*10927Srrh 				error("index too big\n");
510*10927Srrh 			}
511*10927Srrh 			release(q);
512*10927Srrh 			n = readc() & 0377;
513*10927Srrh 			sptr = stable[n];
514*10927Srrh 			if(sptr != 0){
515*10927Srrh 				p = sptr->val;
516*10927Srrh 				if(length(p)-PTRSZ >= c*PTRSZ){
517*10927Srrh 					seekc(p,c*PTRSZ);
518*10927Srrh 					s = getwd(p);
519*10927Srrh 					if(s != 0){
520*10927Srrh 						q = copy(s,length(s));
521*10927Srrh 						pushp(q);
522*10927Srrh 						continue;
523*10927Srrh 					}
524*10927Srrh 				}
525*10927Srrh 			}
526*10927Srrh 			q = salloc(PTRSZ);
527*10927Srrh 			putwd(q, (struct blk *)0);
528*10927Srrh 			pushp(q);
529*10927Srrh 			continue;
530*10927Srrh 		case 'x':
531*10927Srrh execute:
532*10927Srrh 			p = pop();
533*10927Srrh 			EMPTY;
534*10927Srrh 			if((readptr != &readstk[0]) && (*readptr != 0)){
535*10927Srrh 				if((*readptr)->rd == (*readptr)->wt)
536*10927Srrh 					release(*readptr);
537*10927Srrh 				else{
538*10927Srrh 					if(readptr++ == &readstk[RDSKSZ]){
539*10927Srrh 						error("nesting depth\n");
540*10927Srrh 					}
541*10927Srrh 				}
542*10927Srrh 			}
543*10927Srrh 			else readptr++;
544*10927Srrh 			*readptr = p;
545*10927Srrh 			if(p != 0)rewind(p);
546*10927Srrh 			else{
547*10927Srrh 				if((c = readc()) != '\n')unreadc(c);
548*10927Srrh 			}
549*10927Srrh 			continue;
550*10927Srrh 		case '?':
551*10927Srrh 			if(++readptr == &readstk[RDSKSZ]){
552*10927Srrh 				error("nesting depth\n");
553*10927Srrh 			}
554*10927Srrh 			*readptr = 0;
555*10927Srrh 			fsave = curfile;
556*10927Srrh 			curfile = stdin;
557*10927Srrh 			while((c = readc()) == '!')command();
558*10927Srrh 			p = salloc(0);
559*10927Srrh 			sputc(p,c);
560*10927Srrh 			while((c = readc()) != '\n'){
561*10927Srrh 				sputc(p,c);
562*10927Srrh 				if(c == '\\')sputc(p,readc());
563*10927Srrh 			}
564*10927Srrh 			curfile = fsave;
565*10927Srrh 			*readptr = p;
566*10927Srrh 			continue;
567*10927Srrh 		case '!':
568*10927Srrh 			if(command() == 1)goto execute;
569*10927Srrh 			continue;
570*10927Srrh 		case '<':
571*10927Srrh 		case '>':
572*10927Srrh 		case '=':
573*10927Srrh 			if(cond(c) == 1)goto execute;
574*10927Srrh 			continue;
575*10927Srrh 		default:
576*10927Srrh 			printf("%o is unimplemented\n",c);
577*10927Srrh 		}
578*10927Srrh 	}
579*10927Srrh }
580*10927Srrh struct blk *
581*10927Srrh div(ddivd,ddivr)
582*10927Srrh struct blk *ddivd,*ddivr;
583*10927Srrh {
584*10927Srrh 	int divsign,remsign,offset,divcarry;
585*10927Srrh 	int carry, dig,magic,d,dd;
586*10927Srrh 	long c,td,cc;
587*10927Srrh 	struct blk *ps;
588*10927Srrh 	register struct blk *p,*divd,*divr;
589*10927Srrh 
590*10927Srrh 	rem = 0;
591*10927Srrh 	p = salloc(0);
592*10927Srrh 	if(length(ddivr) == 0){
593*10927Srrh 		pushp(ddivr);
594*10927Srrh 		errorrt("divide by 0\n");
595*10927Srrh 	}
596*10927Srrh 	divsign = remsign = 0;
597*10927Srrh 	divr = ddivr;
598*10927Srrh 	fsfile(divr);
599*10927Srrh 	if(sbackc(divr) == -1){
600*10927Srrh 		divr = copy(ddivr,length(ddivr));
601*10927Srrh 		chsign(divr);
602*10927Srrh 		divsign = ~divsign;
603*10927Srrh 	}
604*10927Srrh 	divd = copy(ddivd,length(ddivd));
605*10927Srrh 	fsfile(divd);
606*10927Srrh 	if(sfbeg(divd) == 0 && sbackc(divd) == -1){
607*10927Srrh 		chsign(divd);
608*10927Srrh 		divsign = ~divsign;
609*10927Srrh 		remsign = ~remsign;
610*10927Srrh 	}
611*10927Srrh 	offset = length(divd) - length(divr);
612*10927Srrh 	if(offset < 0)goto ddone;
613*10927Srrh 	seekc(p,offset+1);
614*10927Srrh 	sputc(divd,0);
615*10927Srrh 	magic = 0;
616*10927Srrh 	fsfile(divr);
617*10927Srrh 	c = sbackc(divr);
618*10927Srrh 	if(c<10)magic++;
619*10927Srrh 	c = c*100 + (sfbeg(divr)?0:sbackc(divr));
620*10927Srrh 	if(magic>0){
621*10927Srrh 		c = (c*100 +(sfbeg(divr)?0:sbackc(divr)))*2;
622*10927Srrh 		c /= 25;
623*10927Srrh 	}
624*10927Srrh 	while(offset >= 0){
625*10927Srrh 		fsfile(divd);
626*10927Srrh 		td = sbackc(divd)*100;
627*10927Srrh 		dd = sfbeg(divd)?0:sbackc(divd);
628*10927Srrh 		td = (td+dd)*100;
629*10927Srrh 		dd = sfbeg(divd)?0:sbackc(divd);
630*10927Srrh 		td = td+dd;
631*10927Srrh 		cc = c;
632*10927Srrh 		if(offset == 0)td += 1;
633*10927Srrh 		else cc += 1;
634*10927Srrh 		if(magic != 0)td = td<<3;
635*10927Srrh 		dig = td/cc;
636*10927Srrh 		rewind(divr);
637*10927Srrh 		rewind(divxyz);
638*10927Srrh 		carry = 0;
639*10927Srrh 		while(sfeof(divr) == 0){
640*10927Srrh 			d = sgetc(divr)*dig+carry;
641*10927Srrh 			carry = d / 100;
642*10927Srrh 			salterc(divxyz,d%100);
643*10927Srrh 		}
644*10927Srrh 		salterc(divxyz,carry);
645*10927Srrh 		rewind(divxyz);
646*10927Srrh 		seekc(divd,offset);
647*10927Srrh 		carry = 0;
648*10927Srrh 		while(sfeof(divd) == 0){
649*10927Srrh 			d = slookc(divd);
650*10927Srrh 			d = d-(sfeof(divxyz)?0:sgetc(divxyz))-carry;
651*10927Srrh 			carry = 0;
652*10927Srrh 			if(d < 0){
653*10927Srrh 				d += 100;
654*10927Srrh 				carry = 1;
655*10927Srrh 			}
656*10927Srrh 			salterc(divd,d);
657*10927Srrh 		}
658*10927Srrh 		divcarry = carry;
659*10927Srrh 		sbackc(p);
660*10927Srrh 		salterc(p,dig);
661*10927Srrh 		sbackc(p);
662*10927Srrh 		if(--offset >= 0)divd->wt--;
663*10927Srrh 	}
664*10927Srrh 	if(divcarry != 0){
665*10927Srrh 		salterc(p,dig-1);
666*10927Srrh 		salterc(divd,-1);
667*10927Srrh 		ps = add(divr,divd);
668*10927Srrh 		release(divd);
669*10927Srrh 		divd = ps;
670*10927Srrh 	}
671*10927Srrh 
672*10927Srrh 	rewind(p);
673*10927Srrh 	divcarry = 0;
674*10927Srrh 	while(sfeof(p) == 0){
675*10927Srrh 		d = slookc(p)+divcarry;
676*10927Srrh 		divcarry = 0;
677*10927Srrh 		if(d >= 100){
678*10927Srrh 			d -= 100;
679*10927Srrh 			divcarry = 1;
680*10927Srrh 		}
681*10927Srrh 		salterc(p,d);
682*10927Srrh 	}
683*10927Srrh 	if(divcarry != 0)salterc(p,divcarry);
684*10927Srrh 	fsfile(p);
685*10927Srrh 	while(sfbeg(p) == 0){
686*10927Srrh 		if(sbackc(p) == 0)truncate(p);
687*10927Srrh 		else break;
688*10927Srrh 	}
689*10927Srrh 	if(divsign < 0)chsign(p);
690*10927Srrh 	fsfile(divd);
691*10927Srrh 	while(sfbeg(divd) == 0){
692*10927Srrh 		if(sbackc(divd) == 0)truncate(divd);
693*10927Srrh 		else break;
694*10927Srrh 	}
695*10927Srrh ddone:
696*10927Srrh 	if(remsign<0)chsign(divd);
697*10927Srrh 	if(divr != ddivr)release(divr);
698*10927Srrh 	rem = divd;
699*10927Srrh 	return(p);
700*10927Srrh }
701*10927Srrh dscale(){
702*10927Srrh 	register struct blk *dd,*dr;
703*10927Srrh 	register struct blk *r;
704*10927Srrh 	int c;
705*10927Srrh 
706*10927Srrh 	dr = pop();
707*10927Srrh 	EMPTYS;
708*10927Srrh 	dd = pop();
709*10927Srrh 	EMPTYSR(dr);
710*10927Srrh 	fsfile(dd);
711*10927Srrh 	skd = sunputc(dd);
712*10927Srrh 	fsfile(dr);
713*10927Srrh 	skr = sunputc(dr);
714*10927Srrh 	if(sfbeg(dr) == 1 || (sfbeg(dr) == 0 && sbackc(dr) == 0)){
715*10927Srrh 		sputc(dr,skr);
716*10927Srrh 		pushp(dr);
717*10927Srrh 		errorrt("divide by 0\n");
718*10927Srrh 	}
719*10927Srrh 	c = k-skd+skr;
720*10927Srrh 	if(c < 0)r = removr(dd,-c);
721*10927Srrh 	else {
722*10927Srrh 		r = add0(dd,c);
723*10927Srrh 		irem = 0;
724*10927Srrh 	}
725*10927Srrh 	arg1 = r;
726*10927Srrh 	arg2 = dr;
727*10927Srrh 	savk = k;
728*10927Srrh 	return(0);
729*10927Srrh }
730*10927Srrh struct blk *
731*10927Srrh removr(p,n)
732*10927Srrh struct blk *p;
733*10927Srrh {
734*10927Srrh 	int nn;
735*10927Srrh 	register struct blk *q,*s,*r;
736*10927Srrh 
737*10927Srrh 	rewind(p);
738*10927Srrh 	nn = (n+1)/2;
739*10927Srrh 	q = salloc(nn);
740*10927Srrh 	while(n>1){
741*10927Srrh 		sputc(q,sgetc(p));
742*10927Srrh 		n -= 2;
743*10927Srrh 	}
744*10927Srrh 	r = salloc(2);
745*10927Srrh 	while(sfeof(p) == 0)sputc(r,sgetc(p));
746*10927Srrh 	release(p);
747*10927Srrh 	if(n == 1){
748*10927Srrh 		s = div(r,tenptr);
749*10927Srrh 		release(r);
750*10927Srrh 		rewind(rem);
751*10927Srrh 		if(sfeof(rem) == 0)sputc(q,sgetc(rem));
752*10927Srrh 		release(rem);
753*10927Srrh 		irem = q;
754*10927Srrh 		return(s);
755*10927Srrh 	}
756*10927Srrh 	irem = q;
757*10927Srrh 	return(r);
758*10927Srrh }
759*10927Srrh struct blk *
760*10927Srrh sqrt(p)
761*10927Srrh struct blk *p;
762*10927Srrh {
763*10927Srrh 	struct blk *t;
764*10927Srrh 	struct blk *r,*q,*s;
765*10927Srrh 	int c,n,nn;
766*10927Srrh 
767*10927Srrh 	n = length(p);
768*10927Srrh 	fsfile(p);
769*10927Srrh 	c = sbackc(p);
770*10927Srrh 	if((n&1) != 1)c = c*100+(sfbeg(p)?0:sbackc(p));
771*10927Srrh 	n = (n+1)>>1;
772*10927Srrh 	r = salloc(n);
773*10927Srrh 	zero(r);
774*10927Srrh 	seekc(r,n);
775*10927Srrh 	nn=1;
776*10927Srrh 	while((c -= nn)>=0)nn+=2;
777*10927Srrh 	c=(nn+1)>>1;
778*10927Srrh 	fsfile(r);
779*10927Srrh 	sbackc(r);
780*10927Srrh 	if(c>=100){
781*10927Srrh 		c -= 100;
782*10927Srrh 		salterc(r,c);
783*10927Srrh 		sputc(r,1);
784*10927Srrh 	}
785*10927Srrh 	else salterc(r,c);
786*10927Srrh 	while(1){
787*10927Srrh 		q = div(p,r);
788*10927Srrh 		s = add(q,r);
789*10927Srrh 		release(q);
790*10927Srrh 		release(rem);
791*10927Srrh 		q = div(s,sqtemp);
792*10927Srrh 		release(s);
793*10927Srrh 		release(rem);
794*10927Srrh 		s = copy(r,length(r));
795*10927Srrh 		chsign(s);
796*10927Srrh 		t = add(s,q);
797*10927Srrh 		release(s);
798*10927Srrh 		fsfile(t);
799*10927Srrh 		nn = sfbeg(t)?0:sbackc(t);
800*10927Srrh 		if(nn>=0)break;
801*10927Srrh 		release(r);
802*10927Srrh 		release(t);
803*10927Srrh 		r = q;
804*10927Srrh 	}
805*10927Srrh 	release(t);
806*10927Srrh 	release(q);
807*10927Srrh 	release(p);
808*10927Srrh 	return(r);
809*10927Srrh }
810*10927Srrh struct blk *
811*10927Srrh exp(base,ex)
812*10927Srrh struct blk *base,*ex;
813*10927Srrh {
814*10927Srrh 	register struct blk *r,*e,*p;
815*10927Srrh 	struct blk *e1,*t,*cp;
816*10927Srrh 	int temp,c,n;
817*10927Srrh 	r = salloc(1);
818*10927Srrh 	sputc(r,1);
819*10927Srrh 	p = copy(base,length(base));
820*10927Srrh 	e = copy(ex,length(ex));
821*10927Srrh 	fsfile(e);
822*10927Srrh 	if(sfbeg(e) != 0)goto edone;
823*10927Srrh 	temp=0;
824*10927Srrh 	c = sbackc(e);
825*10927Srrh 	if(c<0){
826*10927Srrh 		temp++;
827*10927Srrh 		chsign(e);
828*10927Srrh 	}
829*10927Srrh 	while(length(e) != 0){
830*10927Srrh 		e1=div(e,sqtemp);
831*10927Srrh 		release(e);
832*10927Srrh 		e = e1;
833*10927Srrh 		n = length(rem);
834*10927Srrh 		release(rem);
835*10927Srrh 		if(n != 0){
836*10927Srrh 			e1=mult(p,r);
837*10927Srrh 			release(r);
838*10927Srrh 			r = e1;
839*10927Srrh 		}
840*10927Srrh 		t = copy(p,length(p));
841*10927Srrh 		cp = mult(p,t);
842*10927Srrh 		release(p);
843*10927Srrh 		release(t);
844*10927Srrh 		p = cp;
845*10927Srrh 	}
846*10927Srrh 	if(temp != 0){
847*10927Srrh 		if((c = length(base)) == 0){
848*10927Srrh 			goto edone;
849*10927Srrh 		}
850*10927Srrh 		if(c>1)create(r);
851*10927Srrh 		else{
852*10927Srrh 			rewind(base);
853*10927Srrh 			if((c = sgetc(base))<=1){
854*10927Srrh 				create(r);
855*10927Srrh 				sputc(r,c);
856*10927Srrh 			}
857*10927Srrh 			else create(r);
858*10927Srrh 		}
859*10927Srrh 	}
860*10927Srrh edone:
861*10927Srrh 	release(p);
862*10927Srrh 	release(e);
863*10927Srrh 	return(r);
864*10927Srrh }
865*10927Srrh init(argc,argv)
866*10927Srrh int argc;
867*10927Srrh char *argv[];
868*10927Srrh {
869*10927Srrh 	register struct sym *sp;
870*10927Srrh 
871*10927Srrh 	if (signal(SIGINT, SIG_IGN) != SIG_IGN)
872*10927Srrh 		signal(SIGINT,onintr);
873*10927Srrh 	setbuf(stdout,(char *)NULL);
874*10927Srrh 	svargc = --argc;
875*10927Srrh 	svargv = argv;
876*10927Srrh 	while(svargc>0 && svargv[1][0] == '-'){
877*10927Srrh 		switch(svargv[1][1]){
878*10927Srrh 		default:
879*10927Srrh 			dbg=1;
880*10927Srrh 		}
881*10927Srrh 		svargc--;
882*10927Srrh 		svargv++;
883*10927Srrh 	}
884*10927Srrh 	ifile=1;
885*10927Srrh 	if(svargc<=0)curfile = stdin;
886*10927Srrh 	else if((curfile = fopen(svargv[1],"r")) == NULL){
887*10927Srrh 		printf("can't open file %s\n",svargv[1]);
888*10927Srrh 		exit(1);
889*10927Srrh 		}
890*10927Srrh 	dummy = malloc(1);
891*10927Srrh 	scalptr = salloc(1);
892*10927Srrh 	sputc(scalptr,0);
893*10927Srrh 	basptr = salloc(1);
894*10927Srrh 	sputc(basptr,10);
895*10927Srrh 	obase=10;
896*10927Srrh 	log10=log2(10L);
897*10927Srrh 	ll=70;
898*10927Srrh 	fw=1;
899*10927Srrh 	fw1=0;
900*10927Srrh 	tenptr = salloc(1);
901*10927Srrh 	sputc(tenptr,10);
902*10927Srrh 	obase=10;
903*10927Srrh 	inbas = salloc(1);
904*10927Srrh 	sputc(inbas,10);
905*10927Srrh 	sqtemp = salloc(1);
906*10927Srrh 	sputc(sqtemp,2);
907*10927Srrh 	chptr = salloc(0);
908*10927Srrh 	strptr = salloc(0);
909*10927Srrh 	divxyz = salloc(0);
910*10927Srrh 	stkbeg = stkptr = &stack[0];
911*10927Srrh 	stkend = &stack[STKSZ];
912*10927Srrh 	stkerr = 0;
913*10927Srrh 	readptr = &readstk[0];
914*10927Srrh 	k=0;
915*10927Srrh 	sp = sptr = &symlst[0];
916*10927Srrh 	while(sptr < &symlst[TBLSZ]){
917*10927Srrh 		sptr->next = ++sp;
918*10927Srrh 		sptr++;
919*10927Srrh 	}
920*10927Srrh 	sptr->next=0;
921*10927Srrh 	sfree = &symlst[0];
922*10927Srrh 	return;
923*10927Srrh }
924*10927Srrh onintr(){
925*10927Srrh 
926*10927Srrh 	signal(SIGINT,onintr);
927*10927Srrh 	while(readptr != &readstk[0]){
928*10927Srrh 		if(*readptr != 0){release(*readptr);}
929*10927Srrh 		readptr--;
930*10927Srrh 	}
931*10927Srrh 	curfile = stdin;
932*10927Srrh 	commnds();
933*10927Srrh }
934*10927Srrh pushp(p)
935*10927Srrh struct blk *p;
936*10927Srrh {
937*10927Srrh 	if(stkptr == stkend){
938*10927Srrh 		printf("out of stack space\n");
939*10927Srrh 		return;
940*10927Srrh 	}
941*10927Srrh 	stkerr=0;
942*10927Srrh 	*++stkptr = p;
943*10927Srrh 	return;
944*10927Srrh }
945*10927Srrh struct blk *
946*10927Srrh pop(){
947*10927Srrh 	if(stkptr == stack){
948*10927Srrh 		stkerr=1;
949*10927Srrh 		return(0);
950*10927Srrh 	}
951*10927Srrh 	return(*stkptr--);
952*10927Srrh }
953*10927Srrh struct blk *
954*10927Srrh readin(){
955*10927Srrh 	register struct blk *p,*q;
956*10927Srrh 	int dp,dpct;
957*10927Srrh 	register int c;
958*10927Srrh 
959*10927Srrh 	dp = dpct=0;
960*10927Srrh 	p = salloc(0);
961*10927Srrh 	while(1){
962*10927Srrh 		c = readc();
963*10927Srrh 		switch(c){
964*10927Srrh 		case '.':
965*10927Srrh 			if(dp != 0){
966*10927Srrh 				unreadc(c);
967*10927Srrh 				break;
968*10927Srrh 			}
969*10927Srrh 			dp++;
970*10927Srrh 			continue;
971*10927Srrh 		case '\\':
972*10927Srrh 			readc();
973*10927Srrh 			continue;
974*10927Srrh 		default:
975*10927Srrh 			if(c >= 'A' && c <= 'F')c = c - 'A' + 10;
976*10927Srrh 			else if(c >= '0' && c <= '9')c -= '0';
977*10927Srrh 			else goto gotnum;
978*10927Srrh 			if(dp != 0){
979*10927Srrh 				if(dpct >= 99)continue;
980*10927Srrh 				dpct++;
981*10927Srrh 			}
982*10927Srrh 			create(chptr);
983*10927Srrh 			if(c != 0)sputc(chptr,c);
984*10927Srrh 			q = mult(p,inbas);
985*10927Srrh 			release(p);
986*10927Srrh 			p = add(chptr,q);
987*10927Srrh 			release(q);
988*10927Srrh 		}
989*10927Srrh 	}
990*10927Srrh gotnum:
991*10927Srrh 	unreadc(c);
992*10927Srrh 	if(dp == 0){
993*10927Srrh 		sputc(p,0);
994*10927Srrh 		return(p);
995*10927Srrh 	}
996*10927Srrh 	else{
997*10927Srrh 		q = scale(p,dpct);
998*10927Srrh 		return(q);
999*10927Srrh 	}
1000*10927Srrh }
1001*10927Srrh struct blk *
1002*10927Srrh add0(p,ct)
1003*10927Srrh int ct;
1004*10927Srrh struct blk *p;
1005*10927Srrh {
1006*10927Srrh 		/* returns pointer to struct with ct 0's & p */
1007*10927Srrh 	register struct blk *q,*t;
1008*10927Srrh 
1009*10927Srrh 	q = salloc(length(p)+(ct+1)/2);
1010*10927Srrh 	while(ct>1){
1011*10927Srrh 		sputc(q,0);
1012*10927Srrh 		ct -= 2;
1013*10927Srrh 	}
1014*10927Srrh 	rewind(p);
1015*10927Srrh 	while(sfeof(p) == 0){
1016*10927Srrh 		sputc(q,sgetc(p));
1017*10927Srrh 	}
1018*10927Srrh 	release(p);
1019*10927Srrh 	if(ct == 1){
1020*10927Srrh 		t = mult(tenptr,q);
1021*10927Srrh 		release(q);
1022*10927Srrh 		return(t);
1023*10927Srrh 	}
1024*10927Srrh 	return(q);
1025*10927Srrh }
1026*10927Srrh struct blk *
1027*10927Srrh mult(p,q)
1028*10927Srrh struct blk *p,*q;
1029*10927Srrh {
1030*10927Srrh 	register struct blk *mp,*mq,*mr;
1031*10927Srrh 	int sign,offset,carry;
1032*10927Srrh 	int cq,cp,mt,mcr;
1033*10927Srrh 
1034*10927Srrh 	offset = sign = 0;
1035*10927Srrh 	fsfile(p);
1036*10927Srrh 	mp = p;
1037*10927Srrh 	if(sfbeg(p) == 0){
1038*10927Srrh 		if(sbackc(p)<0){
1039*10927Srrh 			mp = copy(p,length(p));
1040*10927Srrh 			chsign(mp);
1041*10927Srrh 			sign = ~sign;
1042*10927Srrh 		}
1043*10927Srrh 	}
1044*10927Srrh 	fsfile(q);
1045*10927Srrh 	mq = q;
1046*10927Srrh 	if(sfbeg(q) == 0){
1047*10927Srrh 		if(sbackc(q)<0){
1048*10927Srrh 			mq = copy(q,length(q));
1049*10927Srrh 			chsign(mq);
1050*10927Srrh 			sign = ~sign;
1051*10927Srrh 		}
1052*10927Srrh 	}
1053*10927Srrh 	mr = salloc(length(mp)+length(mq));
1054*10927Srrh 	zero(mr);
1055*10927Srrh 	rewind(mq);
1056*10927Srrh 	while(sfeof(mq) == 0){
1057*10927Srrh 		cq = sgetc(mq);
1058*10927Srrh 		rewind(mp);
1059*10927Srrh 		rewind(mr);
1060*10927Srrh 		mr->rd += offset;
1061*10927Srrh 		carry=0;
1062*10927Srrh 		while(sfeof(mp) == 0){
1063*10927Srrh 			cp = sgetc(mp);
1064*10927Srrh 			mcr = sfeof(mr)?0:slookc(mr);
1065*10927Srrh 			mt = cp*cq + carry + mcr;
1066*10927Srrh 			carry = mt/100;
1067*10927Srrh 			salterc(mr,mt%100);
1068*10927Srrh 		}
1069*10927Srrh 		offset++;
1070*10927Srrh 		if(carry != 0){
1071*10927Srrh 			mcr = sfeof(mr)?0:slookc(mr);
1072*10927Srrh 			salterc(mr,mcr+carry);
1073*10927Srrh 		}
1074*10927Srrh 	}
1075*10927Srrh 	if(sign < 0){
1076*10927Srrh 		chsign(mr);
1077*10927Srrh 	}
1078*10927Srrh 	if(mp != p)release(mp);
1079*10927Srrh 	if(mq != q)release(mq);
1080*10927Srrh 	return(mr);
1081*10927Srrh }
1082*10927Srrh chsign(p)
1083*10927Srrh struct blk *p;
1084*10927Srrh {
1085*10927Srrh 	register int carry;
1086*10927Srrh 	register char ct;
1087*10927Srrh 
1088*10927Srrh 	carry=0;
1089*10927Srrh 	rewind(p);
1090*10927Srrh 	while(sfeof(p) == 0){
1091*10927Srrh 		ct=100-slookc(p)-carry;
1092*10927Srrh 		carry=1;
1093*10927Srrh 		if(ct>=100){
1094*10927Srrh 			ct -= 100;
1095*10927Srrh 			carry=0;
1096*10927Srrh 		}
1097*10927Srrh 		salterc(p,ct);
1098*10927Srrh 	}
1099*10927Srrh 	if(carry != 0){
1100*10927Srrh 		sputc(p,-1);
1101*10927Srrh 		fsfile(p);
1102*10927Srrh 		sbackc(p);
1103*10927Srrh 		ct = sbackc(p);
1104*10927Srrh 		if(ct == 99){
1105*10927Srrh 			truncate(p);
1106*10927Srrh 			sputc(p,-1);
1107*10927Srrh 		}
1108*10927Srrh 	}
1109*10927Srrh 	else{
1110*10927Srrh 		fsfile(p);
1111*10927Srrh 		ct = sbackc(p);
1112*10927Srrh 		if(ct == 0)truncate(p);
1113*10927Srrh 	}
1114*10927Srrh 	return;
1115*10927Srrh }
1116*10927Srrh readc(){
1117*10927Srrh loop:
1118*10927Srrh 	if((readptr != &readstk[0]) && (*readptr != 0)){
1119*10927Srrh 		if(sfeof(*readptr) == 0)return(lastchar = sgetc(*readptr));
1120*10927Srrh 		release(*readptr);
1121*10927Srrh 		readptr--;
1122*10927Srrh 		goto loop;
1123*10927Srrh 	}
1124*10927Srrh 	lastchar = getc(curfile);
1125*10927Srrh 	if(lastchar != EOF)return(lastchar);
1126*10927Srrh 	if(readptr != &readptr[0]){
1127*10927Srrh 		readptr--;
1128*10927Srrh 		if(*readptr == 0)curfile = stdin;
1129*10927Srrh 		goto loop;
1130*10927Srrh 	}
1131*10927Srrh 	if(curfile != stdin){
1132*10927Srrh 		fclose(curfile);
1133*10927Srrh 		curfile = stdin;
1134*10927Srrh 		goto loop;
1135*10927Srrh 	}
1136*10927Srrh 	exit(0);
1137*10927Srrh }
1138*10927Srrh unreadc(c)
1139*10927Srrh char c;
1140*10927Srrh {
1141*10927Srrh 
1142*10927Srrh 	if((readptr != &readstk[0]) && (*readptr != 0)){
1143*10927Srrh 		sungetc(*readptr,c);
1144*10927Srrh 	}
1145*10927Srrh 	else ungetc(c,curfile);
1146*10927Srrh 	return;
1147*10927Srrh }
1148*10927Srrh binop(c)
1149*10927Srrh char c;
1150*10927Srrh {
1151*10927Srrh 	register struct blk *r;
1152*10927Srrh 
1153*10927Srrh 	switch(c){
1154*10927Srrh 	case '+':
1155*10927Srrh 		r = add(arg1,arg2);
1156*10927Srrh 		break;
1157*10927Srrh 	case '*':
1158*10927Srrh 		r = mult(arg1,arg2);
1159*10927Srrh 		break;
1160*10927Srrh 	case '/':
1161*10927Srrh 		r = div(arg1,arg2);
1162*10927Srrh 		break;
1163*10927Srrh 	}
1164*10927Srrh 	release(arg1);
1165*10927Srrh 	release(arg2);
1166*10927Srrh 	sputc(r,savk);
1167*10927Srrh 	pushp(r);
1168*10927Srrh 	return;
1169*10927Srrh }
1170*10927Srrh print(hptr)
1171*10927Srrh struct blk *hptr;
1172*10927Srrh {
1173*10927Srrh 	int sc;
1174*10927Srrh 	register struct blk *p,*q,*dec;
1175*10927Srrh 	int dig,dout,ct;
1176*10927Srrh 
1177*10927Srrh 	rewind(hptr);
1178*10927Srrh 	while(sfeof(hptr) == 0){
1179*10927Srrh 		if(sgetc(hptr)>99){
1180*10927Srrh 			rewind(hptr);
1181*10927Srrh 			while(sfeof(hptr) == 0){
1182*10927Srrh 				printf("%c",sgetc(hptr));
1183*10927Srrh 			}
1184*10927Srrh 			printf("\n");
1185*10927Srrh 			return;
1186*10927Srrh 		}
1187*10927Srrh 	}
1188*10927Srrh 	fsfile(hptr);
1189*10927Srrh 	sc = sbackc(hptr);
1190*10927Srrh 	if(sfbeg(hptr) != 0){
1191*10927Srrh 		printf("0\n");
1192*10927Srrh 		return;
1193*10927Srrh 	}
1194*10927Srrh 	count = ll;
1195*10927Srrh 	p = copy(hptr,length(hptr));
1196*10927Srrh 	sunputc(p);
1197*10927Srrh 	fsfile(p);
1198*10927Srrh 	if(sbackc(p)<0){
1199*10927Srrh 		chsign(p);
1200*10927Srrh 		OUTC('-');
1201*10927Srrh 	}
1202*10927Srrh 	if((obase == 0) || (obase == -1)){
1203*10927Srrh 		oneot(p,sc,'d');
1204*10927Srrh 		return;
1205*10927Srrh 	}
1206*10927Srrh 	if(obase == 1){
1207*10927Srrh 		oneot(p,sc,'1');
1208*10927Srrh 		return;
1209*10927Srrh 	}
1210*10927Srrh 	if(obase == 10){
1211*10927Srrh 		tenot(p,sc);
1212*10927Srrh 		return;
1213*10927Srrh 	}
1214*10927Srrh 	create(strptr);
1215*10927Srrh 	dig = log10*sc;
1216*10927Srrh 	dout = ((dig/10) + dig) /logo;
1217*10927Srrh 	dec = getdec(p,sc);
1218*10927Srrh 	p = removc(p,sc);
1219*10927Srrh 	while(length(p) != 0){
1220*10927Srrh 		q = div(p,basptr);
1221*10927Srrh 		release(p);
1222*10927Srrh 		p = q;
1223*10927Srrh 		(*outdit)(rem,0);
1224*10927Srrh 	}
1225*10927Srrh 	release(p);
1226*10927Srrh 	fsfile(strptr);
1227*10927Srrh 	while(sfbeg(strptr) == 0)OUTC(sbackc(strptr));
1228*10927Srrh 	if(sc == 0){
1229*10927Srrh 		release(dec);
1230*10927Srrh 		printf("\n");
1231*10927Srrh 		return;
1232*10927Srrh 	}
1233*10927Srrh 	create(strptr);
1234*10927Srrh 	OUTC('.');
1235*10927Srrh 	ct=0;
1236*10927Srrh 	do{
1237*10927Srrh 		q = mult(basptr,dec);
1238*10927Srrh 		release(dec);
1239*10927Srrh 		dec = getdec(q,sc);
1240*10927Srrh 		p = removc(q,sc);
1241*10927Srrh 		(*outdit)(p,1);
1242*10927Srrh 	}while(++ct < dout);
1243*10927Srrh 	release(dec);
1244*10927Srrh 	rewind(strptr);
1245*10927Srrh 	while(sfeof(strptr) == 0)OUTC(sgetc(strptr));
1246*10927Srrh 	printf("\n");
1247*10927Srrh 	return;
1248*10927Srrh }
1249*10927Srrh 
1250*10927Srrh struct blk *
1251*10927Srrh getdec(p,sc)
1252*10927Srrh struct blk *p;
1253*10927Srrh {
1254*10927Srrh 	int cc;
1255*10927Srrh 	register struct blk *q,*t,*s;
1256*10927Srrh 
1257*10927Srrh 	rewind(p);
1258*10927Srrh 	if(length(p)*2 < sc){
1259*10927Srrh 		q = copy(p,length(p));
1260*10927Srrh 		return(q);
1261*10927Srrh 	}
1262*10927Srrh 	q = salloc(length(p));
1263*10927Srrh 	while(sc >= 1){
1264*10927Srrh 		sputc(q,sgetc(p));
1265*10927Srrh 		sc -= 2;
1266*10927Srrh 	}
1267*10927Srrh 	if(sc != 0){
1268*10927Srrh 		t = mult(q,tenptr);
1269*10927Srrh 		s = salloc(cc = length(q));
1270*10927Srrh 		release(q);
1271*10927Srrh 		rewind(t);
1272*10927Srrh 		while(cc-- > 0)sputc(s,sgetc(t));
1273*10927Srrh 		sputc(s,0);
1274*10927Srrh 		release(t);
1275*10927Srrh 		t = div(s,tenptr);
1276*10927Srrh 		release(s);
1277*10927Srrh 		release(rem);
1278*10927Srrh 		return(t);
1279*10927Srrh 	}
1280*10927Srrh 	return(q);
1281*10927Srrh }
1282*10927Srrh tenot(p,sc)
1283*10927Srrh struct blk *p;
1284*10927Srrh {
1285*10927Srrh 	register int c,f;
1286*10927Srrh 
1287*10927Srrh 	fsfile(p);
1288*10927Srrh 	f=0;
1289*10927Srrh 	while((sfbeg(p) == 0) && ((p->rd-p->beg-1)*2 >= sc)){
1290*10927Srrh 		c = sbackc(p);
1291*10927Srrh 		if((c<10) && (f == 1))printf("0%d",c);
1292*10927Srrh 		else printf("%d",c);
1293*10927Srrh 		f=1;
1294*10927Srrh 		TEST2;
1295*10927Srrh 	}
1296*10927Srrh 	if(sc == 0){
1297*10927Srrh 		printf("\n");
1298*10927Srrh 		release(p);
1299*10927Srrh 		return;
1300*10927Srrh 	}
1301*10927Srrh 	if((p->rd-p->beg)*2 > sc){
1302*10927Srrh 		c = sbackc(p);
1303*10927Srrh 		printf("%d.",c/10);
1304*10927Srrh 		TEST2;
1305*10927Srrh 		OUTC(c%10 +'0');
1306*10927Srrh 		sc--;
1307*10927Srrh 	}
1308*10927Srrh 	else {
1309*10927Srrh 		OUTC('.');
1310*10927Srrh 	}
1311*10927Srrh 	if(sc > (p->rd-p->beg)*2){
1312*10927Srrh 		while(sc>(p->rd-p->beg)*2){
1313*10927Srrh 			OUTC('0');
1314*10927Srrh 			sc--;
1315*10927Srrh 		}
1316*10927Srrh 	}
1317*10927Srrh 	while(sc > 1){
1318*10927Srrh 		c = sbackc(p);
1319*10927Srrh 		if(c<10)printf("0%d",c);
1320*10927Srrh 		else printf("%d",c);
1321*10927Srrh 		sc -= 2;
1322*10927Srrh 		TEST2;
1323*10927Srrh 	}
1324*10927Srrh 	if(sc == 1){
1325*10927Srrh 		OUTC(sbackc(p)/10 +'0');
1326*10927Srrh 	}
1327*10927Srrh 	printf("\n");
1328*10927Srrh 	release(p);
1329*10927Srrh 	return;
1330*10927Srrh }
1331*10927Srrh oneot(p,sc,ch)
1332*10927Srrh struct blk *p;
1333*10927Srrh char ch;
1334*10927Srrh {
1335*10927Srrh 	register struct blk *q;
1336*10927Srrh 
1337*10927Srrh 	q = removc(p,sc);
1338*10927Srrh 	create(strptr);
1339*10927Srrh 	sputc(strptr,-1);
1340*10927Srrh 	while(length(q)>0){
1341*10927Srrh 		p = add(strptr,q);
1342*10927Srrh 		release(q);
1343*10927Srrh 		q = p;
1344*10927Srrh 		OUTC(ch);
1345*10927Srrh 	}
1346*10927Srrh 	release(q);
1347*10927Srrh 	printf("\n");
1348*10927Srrh 	return;
1349*10927Srrh }
1350*10927Srrh hexot(p,flg)
1351*10927Srrh struct blk *p;
1352*10927Srrh {
1353*10927Srrh 	register int c;
1354*10927Srrh 	rewind(p);
1355*10927Srrh 	if(sfeof(p) != 0){
1356*10927Srrh 		sputc(strptr,'0');
1357*10927Srrh 		release(p);
1358*10927Srrh 		return;
1359*10927Srrh 	}
1360*10927Srrh 	c = sgetc(p);
1361*10927Srrh 	release(p);
1362*10927Srrh 	if(c >= 16){
1363*10927Srrh 		printf("hex digit > 16");
1364*10927Srrh 		return;
1365*10927Srrh 	}
1366*10927Srrh 	sputc(strptr,c<10?c+'0':c-10+'A');
1367*10927Srrh 	return;
1368*10927Srrh }
1369*10927Srrh bigot(p,flg)
1370*10927Srrh struct blk *p;
1371*10927Srrh {
1372*10927Srrh 	register struct blk *t,*q;
1373*10927Srrh 	register int l;
1374*10927Srrh 	int neg;
1375*10927Srrh 
1376*10927Srrh 	if(flg == 1)t = salloc(0);
1377*10927Srrh 	else{
1378*10927Srrh 		t = strptr;
1379*10927Srrh 		l = length(strptr)+fw-1;
1380*10927Srrh 	}
1381*10927Srrh 	neg=0;
1382*10927Srrh 	if(length(p) != 0){
1383*10927Srrh 		fsfile(p);
1384*10927Srrh 		if(sbackc(p)<0){
1385*10927Srrh 			neg=1;
1386*10927Srrh 			chsign(p);
1387*10927Srrh 		}
1388*10927Srrh 		while(length(p) != 0){
1389*10927Srrh 			q = div(p,tenptr);
1390*10927Srrh 			release(p);
1391*10927Srrh 			p = q;
1392*10927Srrh 			rewind(rem);
1393*10927Srrh 			sputc(t,sfeof(rem)?'0':sgetc(rem)+'0');
1394*10927Srrh 			release(rem);
1395*10927Srrh 		}
1396*10927Srrh 	}
1397*10927Srrh 	release(p);
1398*10927Srrh 	if(flg == 1){
1399*10927Srrh 		l = fw1-length(t);
1400*10927Srrh 		if(neg != 0){
1401*10927Srrh 			l--;
1402*10927Srrh 			sputc(strptr,'-');
1403*10927Srrh 		}
1404*10927Srrh 		fsfile(t);
1405*10927Srrh 		while(l-- > 0)sputc(strptr,'0');
1406*10927Srrh 		while(sfbeg(t) == 0)sputc(strptr,sbackc(t));
1407*10927Srrh 		release(t);
1408*10927Srrh 	}
1409*10927Srrh 	else{
1410*10927Srrh 		l -= length(strptr);
1411*10927Srrh 		while(l-- > 0)sputc(strptr,'0');
1412*10927Srrh 		if(neg != 0){
1413*10927Srrh 			sunputc(strptr);
1414*10927Srrh 			sputc(strptr,'-');
1415*10927Srrh 		}
1416*10927Srrh 	}
1417*10927Srrh 	sputc(strptr,' ');
1418*10927Srrh 	return;
1419*10927Srrh }
1420*10927Srrh struct blk *
1421*10927Srrh add(a1,a2)
1422*10927Srrh struct blk *a1,*a2;
1423*10927Srrh {
1424*10927Srrh 	register struct blk *p;
1425*10927Srrh 	register int carry,n;
1426*10927Srrh 	int size;
1427*10927Srrh 	int c,n1,n2;
1428*10927Srrh 
1429*10927Srrh 	size = length(a1)>length(a2)?length(a1):length(a2);
1430*10927Srrh 	p = salloc(size);
1431*10927Srrh 	rewind(a1);
1432*10927Srrh 	rewind(a2);
1433*10927Srrh 	carry=0;
1434*10927Srrh 	while(--size >= 0){
1435*10927Srrh 		n1 = sfeof(a1)?0:sgetc(a1);
1436*10927Srrh 		n2 = sfeof(a2)?0:sgetc(a2);
1437*10927Srrh 		n = n1 + n2 + carry;
1438*10927Srrh 		if(n>=100){
1439*10927Srrh 			carry=1;
1440*10927Srrh 			n -= 100;
1441*10927Srrh 		}
1442*10927Srrh 		else if(n<0){
1443*10927Srrh 			carry = -1;
1444*10927Srrh 			n += 100;
1445*10927Srrh 		}
1446*10927Srrh 		else carry = 0;
1447*10927Srrh 		sputc(p,n);
1448*10927Srrh 	}
1449*10927Srrh 	if(carry != 0)sputc(p,carry);
1450*10927Srrh 	fsfile(p);
1451*10927Srrh 	if(sfbeg(p) == 0){
1452*10927Srrh 		while(sfbeg(p) == 0 && (c = sbackc(p)) == 0);
1453*10927Srrh 		if(c != 0)salterc(p,c);
1454*10927Srrh 		truncate(p);
1455*10927Srrh 	}
1456*10927Srrh 	fsfile(p);
1457*10927Srrh 	if(sfbeg(p) == 0 && sbackc(p) == -1){
1458*10927Srrh 		while((c = sbackc(p)) == 99){
1459*10927Srrh 			if(c == EOF)break;
1460*10927Srrh 		}
1461*10927Srrh 		sgetc(p);
1462*10927Srrh 		salterc(p,-1);
1463*10927Srrh 		truncate(p);
1464*10927Srrh 	}
1465*10927Srrh 	return(p);
1466*10927Srrh }
1467*10927Srrh eqk(){
1468*10927Srrh 	register struct blk *p,*q;
1469*10927Srrh 	register int skp;
1470*10927Srrh 	int skq;
1471*10927Srrh 
1472*10927Srrh 	p = pop();
1473*10927Srrh 	EMPTYS;
1474*10927Srrh 	q = pop();
1475*10927Srrh 	EMPTYSR(p);
1476*10927Srrh 	skp = sunputc(p);
1477*10927Srrh 	skq = sunputc(q);
1478*10927Srrh 	if(skp == skq){
1479*10927Srrh 		arg1=p;
1480*10927Srrh 		arg2=q;
1481*10927Srrh 		savk = skp;
1482*10927Srrh 		return(0);
1483*10927Srrh 	}
1484*10927Srrh 	else if(skp < skq){
1485*10927Srrh 		savk = skq;
1486*10927Srrh 		p = add0(p,skq-skp);
1487*10927Srrh 	}
1488*10927Srrh 	else {
1489*10927Srrh 		savk = skp;
1490*10927Srrh 		q = add0(q,skp-skq);
1491*10927Srrh 	}
1492*10927Srrh 	arg1=p;
1493*10927Srrh 	arg2=q;
1494*10927Srrh 	return(0);
1495*10927Srrh }
1496*10927Srrh struct blk *
1497*10927Srrh removc(p,n)
1498*10927Srrh struct blk *p;
1499*10927Srrh {
1500*10927Srrh 	register struct blk *q,*r;
1501*10927Srrh 
1502*10927Srrh 	rewind(p);
1503*10927Srrh 	while(n>1){
1504*10927Srrh 		sgetc(p);
1505*10927Srrh 		n -= 2;
1506*10927Srrh 	}
1507*10927Srrh 	q = salloc(2);
1508*10927Srrh 	while(sfeof(p) == 0)sputc(q,sgetc(p));
1509*10927Srrh 	if(n == 1){
1510*10927Srrh 		r = div(q,tenptr);
1511*10927Srrh 		release(q);
1512*10927Srrh 		release(rem);
1513*10927Srrh 		q = r;
1514*10927Srrh 	}
1515*10927Srrh 	release(p);
1516*10927Srrh 	return(q);
1517*10927Srrh }
1518*10927Srrh struct blk *
1519*10927Srrh scalint(p)
1520*10927Srrh struct blk *p;
1521*10927Srrh {
1522*10927Srrh 	register int n;
1523*10927Srrh 	n = sunputc(p);
1524*10927Srrh 	p = removc(p,n);
1525*10927Srrh 	return(p);
1526*10927Srrh }
1527*10927Srrh struct blk *
1528*10927Srrh scale(p,n)
1529*10927Srrh struct blk *p;
1530*10927Srrh {
1531*10927Srrh 	register struct blk *q,*s,*t;
1532*10927Srrh 
1533*10927Srrh 	t = add0(p,n);
1534*10927Srrh 	q = salloc(1);
1535*10927Srrh 	sputc(q,n);
1536*10927Srrh 	s = exp(inbas,q);
1537*10927Srrh 	release(q);
1538*10927Srrh 	q = div(t,s);
1539*10927Srrh 	release(t);
1540*10927Srrh 	release(s);
1541*10927Srrh 	release(rem);
1542*10927Srrh 	sputc(q,n);
1543*10927Srrh 	return(q);
1544*10927Srrh }
1545*10927Srrh subt(){
1546*10927Srrh 	arg1=pop();
1547*10927Srrh 	EMPTYS;
1548*10927Srrh 	savk = sunputc(arg1);
1549*10927Srrh 	chsign(arg1);
1550*10927Srrh 	sputc(arg1,savk);
1551*10927Srrh 	pushp(arg1);
1552*10927Srrh 	if(eqk() != 0)return(1);
1553*10927Srrh 	binop('+');
1554*10927Srrh 	return(0);
1555*10927Srrh }
1556*10927Srrh command(){
1557*10927Srrh 	int c;
1558*10927Srrh 	char line[100],*sl;
1559*10927Srrh 	register (*savint)(),pid,rpid;
1560*10927Srrh 	int retcode;
1561*10927Srrh 
1562*10927Srrh 	switch(c = readc()){
1563*10927Srrh 	case '<':
1564*10927Srrh 		return(cond(NL));
1565*10927Srrh 	case '>':
1566*10927Srrh 		return(cond(NG));
1567*10927Srrh 	case '=':
1568*10927Srrh 		return(cond(NE));
1569*10927Srrh 	default:
1570*10927Srrh 		sl = line;
1571*10927Srrh 		*sl++ = c;
1572*10927Srrh 		while((c = readc()) != '\n')*sl++ = c;
1573*10927Srrh 		*sl = 0;
1574*10927Srrh 		if((pid = fork()) == 0){
1575*10927Srrh 			execl("/bin/sh","sh","-c",line,0);
1576*10927Srrh 			exit(0100);
1577*10927Srrh 		}
1578*10927Srrh 		savint = signal(SIGINT, SIG_IGN);
1579*10927Srrh 		while((rpid = wait(&retcode)) != pid && rpid != -1);
1580*10927Srrh 		signal(SIGINT,savint);
1581*10927Srrh 		printf("!\n");
1582*10927Srrh 		return(0);
1583*10927Srrh 	}
1584*10927Srrh }
1585*10927Srrh cond(c)
1586*10927Srrh char c;
1587*10927Srrh {
1588*10927Srrh 	register struct blk *p;
1589*10927Srrh 	register char cc;
1590*10927Srrh 
1591*10927Srrh 	if(subt() != 0)return(1);
1592*10927Srrh 	p = pop();
1593*10927Srrh 	sunputc(p);
1594*10927Srrh 	if(length(p) == 0){
1595*10927Srrh 		release(p);
1596*10927Srrh 		if(c == '<' || c == '>' || c == NE){
1597*10927Srrh 			readc();
1598*10927Srrh 			return(0);
1599*10927Srrh 		}
1600*10927Srrh 		load();
1601*10927Srrh 		return(1);
1602*10927Srrh 	}
1603*10927Srrh 	else {
1604*10927Srrh 		if(c == '='){
1605*10927Srrh 			release(p);
1606*10927Srrh 			readc();
1607*10927Srrh 			return(0);
1608*10927Srrh 		}
1609*10927Srrh 	}
1610*10927Srrh 	if(c == NE){
1611*10927Srrh 		release(p);
1612*10927Srrh 		load();
1613*10927Srrh 		return(1);
1614*10927Srrh 	}
1615*10927Srrh 	fsfile(p);
1616*10927Srrh 	cc = sbackc(p);
1617*10927Srrh 	release(p);
1618*10927Srrh 	if((cc<0 && (c == '<' || c == NG)) ||
1619*10927Srrh 		(cc >0) && (c == '>' || c == NL)){
1620*10927Srrh 		readc();
1621*10927Srrh 		return(0);
1622*10927Srrh 	}
1623*10927Srrh 	load();
1624*10927Srrh 	return(1);
1625*10927Srrh }
1626*10927Srrh load(){
1627*10927Srrh 	register int c;
1628*10927Srrh 	register struct blk *p,*q;
1629*10927Srrh 	struct blk *t,*s;
1630*10927Srrh 	c = readc() & 0377;
1631*10927Srrh 	sptr = stable[c];
1632*10927Srrh 	if(sptr != 0){
1633*10927Srrh 		p = sptr->val;
1634*10927Srrh 		if(c >= ARRAYST){
1635*10927Srrh 			q = salloc(length(p));
1636*10927Srrh 			rewind(p);
1637*10927Srrh 			while(sfeof(p) == 0){
1638*10927Srrh 				s = getwd(p);
1639*10927Srrh 				if(s == 0){putwd(q, (struct blk *)NULL);}
1640*10927Srrh 				else{
1641*10927Srrh 					t = copy(s,length(s));
1642*10927Srrh 					putwd(q,t);
1643*10927Srrh 				}
1644*10927Srrh 			}
1645*10927Srrh 			pushp(q);
1646*10927Srrh 		}
1647*10927Srrh 		else{
1648*10927Srrh 			q = copy(p,length(p));
1649*10927Srrh 			pushp(q);
1650*10927Srrh 		}
1651*10927Srrh 	}
1652*10927Srrh 	else{
1653*10927Srrh 		q = salloc(1);
1654*10927Srrh 		sputc(q,0);
1655*10927Srrh 		pushp(q);
1656*10927Srrh 	}
1657*10927Srrh 	return;
1658*10927Srrh }
1659*10927Srrh log2(n)
1660*10927Srrh long n;
1661*10927Srrh {
1662*10927Srrh 	register int i;
1663*10927Srrh 
1664*10927Srrh 	if(n == 0)return(0);
1665*10927Srrh 	i=31;
1666*10927Srrh 	if(n<0)return(i);
1667*10927Srrh 	while((n= n<<1) >0)i--;
1668*10927Srrh 	return(--i);
1669*10927Srrh }
1670*10927Srrh 
1671*10927Srrh struct blk *
1672*10927Srrh salloc(size)
1673*10927Srrh int size;
1674*10927Srrh {
1675*10927Srrh 	register struct blk *hdr;
1676*10927Srrh 	register char *ptr;
1677*10927Srrh 	all++;
1678*10927Srrh 	nbytes += size;
1679*10927Srrh 	ptr = malloc((unsigned)size);
1680*10927Srrh 	if(ptr == 0){
1681*10927Srrh 		garbage("salloc");
1682*10927Srrh 		if((ptr = malloc((unsigned)size)) == 0)
1683*10927Srrh 			ospace("salloc");
1684*10927Srrh 	}
1685*10927Srrh 	if((hdr = hfree) == 0)hdr = morehd();
1686*10927Srrh 	hfree = (struct blk *)hdr->rd;
1687*10927Srrh 	hdr->rd = hdr->wt = hdr->beg = ptr;
1688*10927Srrh 	hdr->last = ptr+size;
1689*10927Srrh 	return(hdr);
1690*10927Srrh }
1691*10927Srrh struct blk *
1692*10927Srrh morehd(){
1693*10927Srrh 	register struct blk *h,*kk;
1694*10927Srrh 	headmor++;
1695*10927Srrh 	nbytes += HEADSZ;
1696*10927Srrh 	hfree = h = (struct blk *)malloc(HEADSZ);
1697*10927Srrh 	if(hfree == 0){
1698*10927Srrh 		garbage("morehd");
1699*10927Srrh 		if((hfree = h = (struct blk *)malloc(HEADSZ)) == 0)
1700*10927Srrh 			ospace("headers");
1701*10927Srrh 	}
1702*10927Srrh 	kk = h;
1703*10927Srrh 	while(h<hfree+(HEADSZ/BLK))(h++)->rd = (char *)++kk;
1704*10927Srrh 	(--h)->rd=0;
1705*10927Srrh 	return(hfree);
1706*10927Srrh }
1707*10927Srrh /*
1708*10927Srrh sunputc(hptr)
1709*10927Srrh struct blk *hptr;
1710*10927Srrh {
1711*10927Srrh 	hptr->wt--;
1712*10927Srrh 	hptr->rd = hptr->wt;
1713*10927Srrh 	return(*hptr->wt);
1714*10927Srrh }
1715*10927Srrh */
1716*10927Srrh struct blk *
1717*10927Srrh copy(hptr,size)
1718*10927Srrh struct blk *hptr;
1719*10927Srrh int size;
1720*10927Srrh {
1721*10927Srrh 	register struct blk *hdr;
1722*10927Srrh 	register unsigned sz;
1723*10927Srrh 	register char *ptr;
1724*10927Srrh 
1725*10927Srrh 	all++;
1726*10927Srrh 	nbytes += size;
1727*10927Srrh 	sz = length(hptr);
1728*10927Srrh 	ptr = nalloc(hptr->beg, (unsigned)size);
1729*10927Srrh 	if(ptr == 0){
1730*10927Srrh 		garbage("copy");
1731*10927Srrh 		if((ptr = nalloc(hptr->beg, (unsigned)size)) == NULL){
1732*10927Srrh 			printf("copy size %d\n",size);
1733*10927Srrh 			ospace("copy");
1734*10927Srrh 		}
1735*10927Srrh 	}
1736*10927Srrh 	if((hdr = hfree) == 0)hdr = morehd();
1737*10927Srrh 	hfree = (struct blk *)hdr->rd;
1738*10927Srrh 	hdr->rd = hdr->beg = ptr;
1739*10927Srrh 	hdr->last = ptr+size;
1740*10927Srrh 	hdr->wt = ptr+sz;
1741*10927Srrh 	ptr = hdr->wt;
1742*10927Srrh 	while(ptr<hdr->last)*ptr++ = '\0';
1743*10927Srrh 	return(hdr);
1744*10927Srrh }
1745*10927Srrh sdump(s1,hptr)
1746*10927Srrh char *s1;
1747*10927Srrh struct blk *hptr;
1748*10927Srrh {
1749*10927Srrh 	char *p;
1750*10927Srrh 	printf("%s %o rd %o wt %o beg %o last %o\n",s1,hptr,hptr->rd,hptr->wt,hptr->beg,hptr->last);
1751*10927Srrh 	p = hptr->beg;
1752*10927Srrh 	while(p < hptr->wt)printf("%d ",*p++);
1753*10927Srrh 	printf("\n");
1754*10927Srrh }
1755*10927Srrh seekc(hptr,n)
1756*10927Srrh struct blk *hptr;
1757*10927Srrh {
1758*10927Srrh 	register char *nn,*p;
1759*10927Srrh 
1760*10927Srrh 	nn = hptr->beg+n;
1761*10927Srrh 	if(nn > hptr->last){
1762*10927Srrh 		nbytes += nn - hptr->last;
1763*10927Srrh 		free(hptr->beg);
1764*10927Srrh 		p = realloc(hptr->beg, (unsigned)n);
1765*10927Srrh 		if(p == 0){
1766*10927Srrh 			hptr->beg = realloc(hptr->beg, (unsigned)(hptr->last-hptr->beg));
1767*10927Srrh 			garbage("seekc");
1768*10927Srrh 			if((p = realloc(hptr->beg, (unsigned)n)) == 0)
1769*10927Srrh 				ospace("seekc");
1770*10927Srrh 		}
1771*10927Srrh 		hptr->beg = p;
1772*10927Srrh 		hptr->wt = hptr->last = hptr->rd = p+n;
1773*10927Srrh 		return;
1774*10927Srrh 	}
1775*10927Srrh 	hptr->rd = nn;
1776*10927Srrh 	if(nn>hptr->wt)hptr->wt = nn;
1777*10927Srrh 	return;
1778*10927Srrh }
1779*10927Srrh salterwd(hptr,n)
1780*10927Srrh struct wblk *hptr;
1781*10927Srrh struct blk *n;
1782*10927Srrh {
1783*10927Srrh 	if(hptr->rdw == hptr->lastw)more(hptr);
1784*10927Srrh 	*hptr->rdw++ = n;
1785*10927Srrh 	if(hptr->rdw > hptr->wtw)hptr->wtw = hptr->rdw;
1786*10927Srrh 	return;
1787*10927Srrh }
1788*10927Srrh more(hptr)
1789*10927Srrh struct blk *hptr;
1790*10927Srrh {
1791*10927Srrh 	register unsigned size;
1792*10927Srrh 	register char *p;
1793*10927Srrh 
1794*10927Srrh 	if((size=(hptr->last-hptr->beg)*2) == 0)size=1;
1795*10927Srrh 	nbytes += size/2;
1796*10927Srrh 	free(hptr->beg);
1797*10927Srrh 	p = realloc(hptr->beg, (unsigned)size);
1798*10927Srrh 	if(p == 0){
1799*10927Srrh 		hptr->beg = realloc(hptr->beg, (unsigned)(hptr->last-hptr->beg));
1800*10927Srrh 		garbage("more");
1801*10927Srrh 		if((p = realloc(hptr->beg,size)) == 0)
1802*10927Srrh 			ospace("more");
1803*10927Srrh 	}
1804*10927Srrh 	hptr->rd = hptr->rd-hptr->beg+p;
1805*10927Srrh 	hptr->wt = hptr->wt-hptr->beg+p;
1806*10927Srrh 	hptr->beg = p;
1807*10927Srrh 	hptr->last = p+size;
1808*10927Srrh 	return;
1809*10927Srrh }
1810*10927Srrh ospace(s)
1811*10927Srrh char *s;
1812*10927Srrh {
1813*10927Srrh 	printf("out of space: %s\n",s);
1814*10927Srrh 	printf("all %ld rel %ld headmor %ld\n",all,rel,headmor);
1815*10927Srrh 	printf("nbytes %ld\n",nbytes);
1816*10927Srrh 	sdump("stk",*stkptr);
1817*10927Srrh 	abort();
1818*10927Srrh }
1819*10927Srrh garbage(s)
1820*10927Srrh char *s;
1821*10927Srrh {
1822*10927Srrh 	int i;
1823*10927Srrh 	struct blk *p, *q;
1824*10927Srrh 	struct sym *tmps;
1825*10927Srrh 	int ct;
1826*10927Srrh 
1827*10927Srrh /*	printf("got to garbage %s\n",s);	*/
1828*10927Srrh 	for(i=0;i<TBLSZ;i++){
1829*10927Srrh 		tmps = stable[i];
1830*10927Srrh 		if(tmps != 0){
1831*10927Srrh 			if(i < ARRAYST){
1832*10927Srrh 				do {
1833*10927Srrh 					p = tmps->val;
1834*10927Srrh 					if(((int)p->beg & 01)  != 0){
1835*10927Srrh 						printf("string %o\n",i);
1836*10927Srrh 						sdump("odd beg",p);
1837*10927Srrh 					}
1838*10927Srrh 					redef(p);
1839*10927Srrh 					tmps = tmps->next;
1840*10927Srrh 				} while(tmps != 0);
1841*10927Srrh 				continue;
1842*10927Srrh 			}
1843*10927Srrh 			else {
1844*10927Srrh 				do {
1845*10927Srrh 					p = tmps->val;
1846*10927Srrh 					rewind(p);
1847*10927Srrh 					ct = 0;
1848*10927Srrh 					while((q = getwd(p)) != NULL){
1849*10927Srrh 						ct++;
1850*10927Srrh 						if(q != 0){
1851*10927Srrh 							if(((int)q->beg & 01) != 0){
1852*10927Srrh 								printf("array %o elt %d odd\n",i-ARRAYST,ct);
1853*10927Srrh printf("tmps %o p %o\n",tmps,p);
1854*10927Srrh 								sdump("elt",q);
1855*10927Srrh 							}
1856*10927Srrh 							redef(q);
1857*10927Srrh 						}
1858*10927Srrh 					}
1859*10927Srrh 					tmps = tmps->next;
1860*10927Srrh 				} while(tmps != 0);
1861*10927Srrh 			}
1862*10927Srrh 		}
1863*10927Srrh 	}
1864*10927Srrh }
1865*10927Srrh redef(p)
1866*10927Srrh struct blk *p;
1867*10927Srrh {
1868*10927Srrh 	register offset;
1869*10927Srrh 	register char *newp;
1870*10927Srrh 
1871*10927Srrh 	if ((int)p->beg&01) {
1872*10927Srrh 		printf("odd ptr %o hdr %o\n",p->beg,p);
1873*10927Srrh 		ospace("redef-bad");
1874*10927Srrh 	}
1875*10927Srrh 	free(p->beg);
1876*10927Srrh 	free(dummy);
1877*10927Srrh 	dummy = malloc(1);
1878*10927Srrh 	if(dummy == NULL)ospace("dummy");
1879*10927Srrh 	newp = realloc(p->beg, (unsigned)(p->last-p->beg));
1880*10927Srrh 	if(newp == NULL)ospace("redef");
1881*10927Srrh 	offset = newp - p->beg;
1882*10927Srrh 	p->beg = newp;
1883*10927Srrh 	p->rd += offset;
1884*10927Srrh 	p->wt += offset;
1885*10927Srrh 	p->last += offset;
1886*10927Srrh }
1887*10927Srrh 
1888*10927Srrh release(p)
1889*10927Srrh register struct blk *p;
1890*10927Srrh {
1891*10927Srrh 	rel++;
1892*10927Srrh 	nbytes -= p->last - p->beg;
1893*10927Srrh 	p->rd = (char *)hfree;
1894*10927Srrh 	hfree = p;
1895*10927Srrh 	free(p->beg);
1896*10927Srrh }
1897*10927Srrh 
1898*10927Srrh struct blk *
1899*10927Srrh getwd(p)
1900*10927Srrh struct blk *p;
1901*10927Srrh {
1902*10927Srrh 	register struct wblk *wp;
1903*10927Srrh 
1904*10927Srrh 	wp = (struct wblk *)p;
1905*10927Srrh 	if (wp->rdw == wp->wtw)
1906*10927Srrh 		return(NULL);
1907*10927Srrh 	return(*wp->rdw++);
1908*10927Srrh }
1909*10927Srrh 
1910*10927Srrh putwd(p, c)
1911*10927Srrh struct blk *p, *c;
1912*10927Srrh {
1913*10927Srrh 	register struct wblk *wp;
1914*10927Srrh 
1915*10927Srrh 	wp = (struct wblk *)p;
1916*10927Srrh 	if (wp->wtw == wp->lastw)
1917*10927Srrh 		more(p);
1918*10927Srrh 	*wp->wtw++ = c;
1919*10927Srrh }
1920*10927Srrh 
1921*10927Srrh struct blk *
1922*10927Srrh lookwd(p)
1923*10927Srrh struct blk *p;
1924*10927Srrh {
1925*10927Srrh 	register struct wblk *wp;
1926*10927Srrh 
1927*10927Srrh 	wp = (struct wblk *)p;
1928*10927Srrh 	if (wp->rdw == wp->wtw)
1929*10927Srrh 		return(NULL);
1930*10927Srrh 	return(*wp->rdw);
1931*10927Srrh }
1932*10927Srrh char *
1933*10927Srrh nalloc(p,nbytes)
1934*10927Srrh register char *p;
1935*10927Srrh unsigned nbytes;
1936*10927Srrh {
1937*10927Srrh 	char *malloc();
1938*10927Srrh 	register char *q, *r;
1939*10927Srrh 	q = r = malloc(nbytes);
1940*10927Srrh 	if(q==0)
1941*10927Srrh 		return(0);
1942*10927Srrh 	while(nbytes--)
1943*10927Srrh 		*q++ = *p++;
1944*10927Srrh 	return(r);
1945*10927Srrh }
1946