xref: /titanic_54/usr/src/cmd/dc/dc.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
24*7c478bd9Sstevel@tonic-gate 
25*7c478bd9Sstevel@tonic-gate 
26*7c478bd9Sstevel@tonic-gate /*
27*7c478bd9Sstevel@tonic-gate  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
28*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
29*7c478bd9Sstevel@tonic-gate  */
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate #include <stdio.h>
34*7c478bd9Sstevel@tonic-gate #include <signal.h>
35*7c478bd9Sstevel@tonic-gate #include <errno.h>
36*7c478bd9Sstevel@tonic-gate #include <sys/stat.h>
37*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
38*7c478bd9Sstevel@tonic-gate #include <limits.h>
39*7c478bd9Sstevel@tonic-gate #include "dc.h"
40*7c478bd9Sstevel@tonic-gate #include <locale.h>
41*7c478bd9Sstevel@tonic-gate 
42*7c478bd9Sstevel@tonic-gate #define	LASTFUN 026
43*7c478bd9Sstevel@tonic-gate long longest = 0, maxsize = 0, active = 0;
44*7c478bd9Sstevel@tonic-gate int lall = 0, lrel = 0, lcopy = 0, lmore = 0, lbytes = 0;
45*7c478bd9Sstevel@tonic-gate 
46*7c478bd9Sstevel@tonic-gate /*
47*7c478bd9Sstevel@tonic-gate  * Routine to handle sign extension of characters on systems that do not
48*7c478bd9Sstevel@tonic-gate  * do automatic sign extension.  This should be portable to all 2's and 1's
49*7c478bd9Sstevel@tonic-gate  * complement systems that do or do not provide automatic sign
50*7c478bd9Sstevel@tonic-gate  * extension. If the system provides automatic sign extension or the
51*7c478bd9Sstevel@tonic-gate  * value of 'c' is positive, ctoint() will always return quickly,
52*7c478bd9Sstevel@tonic-gate  * otherwise ctoint() will search for the negative value by attempting
53*7c478bd9Sstevel@tonic-gate  * to wrap 'c' to 0.  The number of increments needed to get to 0 is the
54*7c478bd9Sstevel@tonic-gate  * negative value.
55*7c478bd9Sstevel@tonic-gate  *
56*7c478bd9Sstevel@tonic-gate  * Note: This assummes that the representation of values stored in chars
57*7c478bd9Sstevel@tonic-gate  * is sequential and allowed to wrap, and that values < 128 are
58*7c478bd9Sstevel@tonic-gate  * positive.  While this is true on 1's and 2's complement machines, it
59*7c478bd9Sstevel@tonic-gate  * may vary on less common architectures.
60*7c478bd9Sstevel@tonic-gate  */
61*7c478bd9Sstevel@tonic-gate 
62*7c478bd9Sstevel@tonic-gate int
63*7c478bd9Sstevel@tonic-gate ctoint(c)
64*7c478bd9Sstevel@tonic-gate #if __STDC__
65*7c478bd9Sstevel@tonic-gate char	c;
66*7c478bd9Sstevel@tonic-gate #else
67*7c478bd9Sstevel@tonic-gate unsigned char	c;
68*7c478bd9Sstevel@tonic-gate #endif
69*7c478bd9Sstevel@tonic-gate {
70*7c478bd9Sstevel@tonic-gate 	int	i;
71*7c478bd9Sstevel@tonic-gate 
72*7c478bd9Sstevel@tonic-gate 	if (c <= SCHAR_MAX)
73*7c478bd9Sstevel@tonic-gate 		return ((int)c);	/* Normal promotion will work */
74*7c478bd9Sstevel@tonic-gate 	for (i = 0; c++; i--);		/* Scan for negative value */
75*7c478bd9Sstevel@tonic-gate 	return (i);
76*7c478bd9Sstevel@tonic-gate }
77*7c478bd9Sstevel@tonic-gate 
78*7c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)		/* Should be defined by cc -D */
79*7c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"  /* Use this only if it weren't. */
80*7c478bd9Sstevel@tonic-gate #endif
81*7c478bd9Sstevel@tonic-gate 
82*7c478bd9Sstevel@tonic-gate void
83*7c478bd9Sstevel@tonic-gate main(int argc, char **argv)
84*7c478bd9Sstevel@tonic-gate {
85*7c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
86*7c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
87*7c478bd9Sstevel@tonic-gate 
88*7c478bd9Sstevel@tonic-gate 	init(argc, argv);
89*7c478bd9Sstevel@tonic-gate 	commnds();
90*7c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
91*7c478bd9Sstevel@tonic-gate }
92*7c478bd9Sstevel@tonic-gate 
93*7c478bd9Sstevel@tonic-gate void
94*7c478bd9Sstevel@tonic-gate commnds()
95*7c478bd9Sstevel@tonic-gate {
96*7c478bd9Sstevel@tonic-gate 	int c;
97*7c478bd9Sstevel@tonic-gate 	struct blk *p, *q;
98*7c478bd9Sstevel@tonic-gate 	long l;
99*7c478bd9Sstevel@tonic-gate 	int sign;
100*7c478bd9Sstevel@tonic-gate 	struct blk **ptr, *s, *t;
101*7c478bd9Sstevel@tonic-gate 	struct sym *sp;
102*7c478bd9Sstevel@tonic-gate 	int sk, sk1, sk2;
103*7c478bd9Sstevel@tonic-gate 	int n, d;
104*7c478bd9Sstevel@tonic-gate 	int scalev;	/* scaling value for converting blks to integers */
105*7c478bd9Sstevel@tonic-gate 
106*7c478bd9Sstevel@tonic-gate 	for (; ; ) {
107*7c478bd9Sstevel@tonic-gate 		if (((c = readc()) >= '0' && c <= '9') ||
108*7c478bd9Sstevel@tonic-gate 		    (c >= 'A' && c <= 'F') || c == '.') {
109*7c478bd9Sstevel@tonic-gate 			unreadc(c);
110*7c478bd9Sstevel@tonic-gate 			p = readin();
111*7c478bd9Sstevel@tonic-gate 			pushp(p);
112*7c478bd9Sstevel@tonic-gate 			continue;
113*7c478bd9Sstevel@tonic-gate 		}
114*7c478bd9Sstevel@tonic-gate 		switch (c) {
115*7c478bd9Sstevel@tonic-gate 		case ' ':
116*7c478bd9Sstevel@tonic-gate 		case '\n':
117*7c478bd9Sstevel@tonic-gate 		case 0377:
118*7c478bd9Sstevel@tonic-gate 		case EOF:
119*7c478bd9Sstevel@tonic-gate 			continue;
120*7c478bd9Sstevel@tonic-gate 		case 'Y':
121*7c478bd9Sstevel@tonic-gate 			sdump("stk", *stkptr);
122*7c478bd9Sstevel@tonic-gate 			printf(gettext
123*7c478bd9Sstevel@tonic-gate 			    ("all %ld rel %ld headmor %ld\n"), all, rel,
124*7c478bd9Sstevel@tonic-gate 			    headmor);
125*7c478bd9Sstevel@tonic-gate 			printf(gettext("nbytes %ld\n"), nbytes);
126*7c478bd9Sstevel@tonic-gate 			printf(gettext
127*7c478bd9Sstevel@tonic-gate 			    ("longest %ld active %ld maxsize %ld\n"), longest,
128*7c478bd9Sstevel@tonic-gate 			    active, maxsize);
129*7c478bd9Sstevel@tonic-gate 			printf(gettext
130*7c478bd9Sstevel@tonic-gate 			    ("new all %d rel %d copy %d more %d lbytes %d\n"),
131*7c478bd9Sstevel@tonic-gate 			    lall, lrel, lcopy, lmore, lbytes);
132*7c478bd9Sstevel@tonic-gate 			lall = lrel = lcopy = lmore = lbytes = 0;
133*7c478bd9Sstevel@tonic-gate 			continue;
134*7c478bd9Sstevel@tonic-gate 		case '_':
135*7c478bd9Sstevel@tonic-gate 			p = readin();
136*7c478bd9Sstevel@tonic-gate 			savk = sunputc(p);
137*7c478bd9Sstevel@tonic-gate 			chsign(p);
138*7c478bd9Sstevel@tonic-gate 			sputc(p, savk);
139*7c478bd9Sstevel@tonic-gate 			pushp(p);
140*7c478bd9Sstevel@tonic-gate 			continue;
141*7c478bd9Sstevel@tonic-gate 		case '-':
142*7c478bd9Sstevel@tonic-gate 			subt();
143*7c478bd9Sstevel@tonic-gate 			continue;
144*7c478bd9Sstevel@tonic-gate 		case '+':
145*7c478bd9Sstevel@tonic-gate 			if (eqk() != 0)
146*7c478bd9Sstevel@tonic-gate 				continue;
147*7c478bd9Sstevel@tonic-gate 			binop('+');
148*7c478bd9Sstevel@tonic-gate 			continue;
149*7c478bd9Sstevel@tonic-gate 		case '*':
150*7c478bd9Sstevel@tonic-gate 			arg1 = pop();
151*7c478bd9Sstevel@tonic-gate 			EMPTY;
152*7c478bd9Sstevel@tonic-gate 			arg2 = pop();
153*7c478bd9Sstevel@tonic-gate 			EMPTYR(arg1);
154*7c478bd9Sstevel@tonic-gate 			sk1 = sunputc(arg1);
155*7c478bd9Sstevel@tonic-gate 			sk2 = sunputc(arg2);
156*7c478bd9Sstevel@tonic-gate 			binop('*');
157*7c478bd9Sstevel@tonic-gate 			p = pop();
158*7c478bd9Sstevel@tonic-gate 			sunputc(p);
159*7c478bd9Sstevel@tonic-gate 			savk = n = sk1 + sk2;
160*7c478bd9Sstevel@tonic-gate 			if (n > k && n > sk1 && n > sk2) {
161*7c478bd9Sstevel@tonic-gate 				sk = sk1;
162*7c478bd9Sstevel@tonic-gate 				if (sk < sk2)
163*7c478bd9Sstevel@tonic-gate 					sk = sk2;
164*7c478bd9Sstevel@tonic-gate 				if (sk < k)
165*7c478bd9Sstevel@tonic-gate 					sk = k;
166*7c478bd9Sstevel@tonic-gate 				p = removc(p, n - sk);
167*7c478bd9Sstevel@tonic-gate 				savk = sk;
168*7c478bd9Sstevel@tonic-gate 			}
169*7c478bd9Sstevel@tonic-gate 			sputc(p, savk);
170*7c478bd9Sstevel@tonic-gate 			pushp(p);
171*7c478bd9Sstevel@tonic-gate 			continue;
172*7c478bd9Sstevel@tonic-gate 		case '/':
173*7c478bd9Sstevel@tonic-gate casediv:
174*7c478bd9Sstevel@tonic-gate 			if (dscale() != 0)
175*7c478bd9Sstevel@tonic-gate 				continue;
176*7c478bd9Sstevel@tonic-gate 			binop('/');
177*7c478bd9Sstevel@tonic-gate 			if (irem != 0)
178*7c478bd9Sstevel@tonic-gate 				release(irem);
179*7c478bd9Sstevel@tonic-gate 			release(rem);
180*7c478bd9Sstevel@tonic-gate 			continue;
181*7c478bd9Sstevel@tonic-gate 		case '%':
182*7c478bd9Sstevel@tonic-gate 			if (dscale() != 0)
183*7c478bd9Sstevel@tonic-gate 				continue;
184*7c478bd9Sstevel@tonic-gate 			binop('/');
185*7c478bd9Sstevel@tonic-gate 			p = pop();
186*7c478bd9Sstevel@tonic-gate 			release(p);
187*7c478bd9Sstevel@tonic-gate 			if (irem == 0) {
188*7c478bd9Sstevel@tonic-gate 				sputc(rem, skr + k);
189*7c478bd9Sstevel@tonic-gate 				pushp(rem);
190*7c478bd9Sstevel@tonic-gate 				continue;
191*7c478bd9Sstevel@tonic-gate 			}
192*7c478bd9Sstevel@tonic-gate 			p = add0(rem, skd - (skr + k));
193*7c478bd9Sstevel@tonic-gate 			q = add(p, irem);
194*7c478bd9Sstevel@tonic-gate 			release(p);
195*7c478bd9Sstevel@tonic-gate 			release(irem);
196*7c478bd9Sstevel@tonic-gate 			sputc(q, skd);
197*7c478bd9Sstevel@tonic-gate 			pushp(q);
198*7c478bd9Sstevel@tonic-gate 			continue;
199*7c478bd9Sstevel@tonic-gate 		case 'v':
200*7c478bd9Sstevel@tonic-gate 			p = pop();
201*7c478bd9Sstevel@tonic-gate 			EMPTY;
202*7c478bd9Sstevel@tonic-gate 			savk = sunputc(p);
203*7c478bd9Sstevel@tonic-gate 			if (length(p) == 0) {
204*7c478bd9Sstevel@tonic-gate 				sputc(p, savk);
205*7c478bd9Sstevel@tonic-gate 				pushp(p);
206*7c478bd9Sstevel@tonic-gate 				continue;
207*7c478bd9Sstevel@tonic-gate 			}
208*7c478bd9Sstevel@tonic-gate 			if ((c = sbackc(p)) < 0) {
209*7c478bd9Sstevel@tonic-gate 				error(gettext("sqrt of neg number\n"));
210*7c478bd9Sstevel@tonic-gate 			}
211*7c478bd9Sstevel@tonic-gate 			if (k < savk)
212*7c478bd9Sstevel@tonic-gate 				n = savk;
213*7c478bd9Sstevel@tonic-gate 			else {
214*7c478bd9Sstevel@tonic-gate 				n = k * 2 - savk;
215*7c478bd9Sstevel@tonic-gate 				savk = k;
216*7c478bd9Sstevel@tonic-gate 			}
217*7c478bd9Sstevel@tonic-gate 			arg1 = add0(p, n);
218*7c478bd9Sstevel@tonic-gate 			arg2 = sqrt(arg1);
219*7c478bd9Sstevel@tonic-gate 			sputc(arg2, savk);
220*7c478bd9Sstevel@tonic-gate 			pushp(arg2);
221*7c478bd9Sstevel@tonic-gate 			continue;
222*7c478bd9Sstevel@tonic-gate 		case '^':
223*7c478bd9Sstevel@tonic-gate 			neg = 0;
224*7c478bd9Sstevel@tonic-gate 			arg1 = pop();
225*7c478bd9Sstevel@tonic-gate 			EMPTY;
226*7c478bd9Sstevel@tonic-gate 			if (sunputc(arg1) != 0)
227*7c478bd9Sstevel@tonic-gate 				error(gettext("exp not an integer\n"));
228*7c478bd9Sstevel@tonic-gate 			arg2 = pop();
229*7c478bd9Sstevel@tonic-gate 			EMPTYR(arg1);
230*7c478bd9Sstevel@tonic-gate 			if (sfbeg(arg1) == 0 && sbackc(arg1) < 0) {
231*7c478bd9Sstevel@tonic-gate 				neg++;
232*7c478bd9Sstevel@tonic-gate 				chsign(arg1);
233*7c478bd9Sstevel@tonic-gate 			}
234*7c478bd9Sstevel@tonic-gate 			if (length(arg1) >= 3)
235*7c478bd9Sstevel@tonic-gate 				error(gettext("exp too big\n"));
236*7c478bd9Sstevel@tonic-gate 			savk = sunputc(arg2);
237*7c478bd9Sstevel@tonic-gate 			p = exp(arg2, arg1);
238*7c478bd9Sstevel@tonic-gate 			release(arg2);
239*7c478bd9Sstevel@tonic-gate 			rewind(arg1);
240*7c478bd9Sstevel@tonic-gate 			c = sgetc(arg1);
241*7c478bd9Sstevel@tonic-gate 			if (c == EOF)
242*7c478bd9Sstevel@tonic-gate 				c = 0;
243*7c478bd9Sstevel@tonic-gate 			else if (sfeof(arg1) == 0)
244*7c478bd9Sstevel@tonic-gate 				c = sgetc(arg1) * 100 + c;
245*7c478bd9Sstevel@tonic-gate 			d = c * savk;
246*7c478bd9Sstevel@tonic-gate 			release(arg1);
247*7c478bd9Sstevel@tonic-gate 			if (k >= savk)
248*7c478bd9Sstevel@tonic-gate 				n = k;
249*7c478bd9Sstevel@tonic-gate 			else
250*7c478bd9Sstevel@tonic-gate 				n = savk;
251*7c478bd9Sstevel@tonic-gate 			if (n < d) {
252*7c478bd9Sstevel@tonic-gate 				q = removc(p, d - n);
253*7c478bd9Sstevel@tonic-gate 				sputc(q, n);
254*7c478bd9Sstevel@tonic-gate 				pushp(q);
255*7c478bd9Sstevel@tonic-gate 			} else {
256*7c478bd9Sstevel@tonic-gate 				sputc(p, d);
257*7c478bd9Sstevel@tonic-gate 				pushp(p);
258*7c478bd9Sstevel@tonic-gate 			}
259*7c478bd9Sstevel@tonic-gate 			if (neg == 0)
260*7c478bd9Sstevel@tonic-gate 				continue;
261*7c478bd9Sstevel@tonic-gate 			p = pop();
262*7c478bd9Sstevel@tonic-gate 			q = salloc(2);
263*7c478bd9Sstevel@tonic-gate 			sputc(q, 1);
264*7c478bd9Sstevel@tonic-gate 			sputc(q, 0);
265*7c478bd9Sstevel@tonic-gate 			pushp(q);
266*7c478bd9Sstevel@tonic-gate 			pushp(p);
267*7c478bd9Sstevel@tonic-gate 			goto casediv;
268*7c478bd9Sstevel@tonic-gate 		case 'z':
269*7c478bd9Sstevel@tonic-gate 			p = salloc(2);
270*7c478bd9Sstevel@tonic-gate 			n = stkptr - stkbeg;
271*7c478bd9Sstevel@tonic-gate 			if (n >= 100) {
272*7c478bd9Sstevel@tonic-gate 				sputc(p, n / 100);
273*7c478bd9Sstevel@tonic-gate 				n %= 100;
274*7c478bd9Sstevel@tonic-gate 			}
275*7c478bd9Sstevel@tonic-gate 			sputc(p, n);
276*7c478bd9Sstevel@tonic-gate 			sputc(p, 0);
277*7c478bd9Sstevel@tonic-gate 			pushp(p);
278*7c478bd9Sstevel@tonic-gate 			continue;
279*7c478bd9Sstevel@tonic-gate 		case 'Z':
280*7c478bd9Sstevel@tonic-gate 			p = pop();
281*7c478bd9Sstevel@tonic-gate 			EMPTY;
282*7c478bd9Sstevel@tonic-gate 			n = (length(p) - 1) << 1;
283*7c478bd9Sstevel@tonic-gate 			fsfile(p);
284*7c478bd9Sstevel@tonic-gate 			sbackc(p);
285*7c478bd9Sstevel@tonic-gate 			if (sfbeg(p) == 0) {
286*7c478bd9Sstevel@tonic-gate 				if ((c = sbackc(p)) < 0) {
287*7c478bd9Sstevel@tonic-gate 					n -= 2;
288*7c478bd9Sstevel@tonic-gate 					if (sfbeg(p) == 1)
289*7c478bd9Sstevel@tonic-gate 						n += 1;
290*7c478bd9Sstevel@tonic-gate 					else {
291*7c478bd9Sstevel@tonic-gate 						if ((c = sbackc(p)) == 0)
292*7c478bd9Sstevel@tonic-gate 							n += 1;
293*7c478bd9Sstevel@tonic-gate 						else if (c > 90)
294*7c478bd9Sstevel@tonic-gate 							n -= 1;
295*7c478bd9Sstevel@tonic-gate 					}
296*7c478bd9Sstevel@tonic-gate 				} else
297*7c478bd9Sstevel@tonic-gate 					if (c < 10)
298*7c478bd9Sstevel@tonic-gate 						n -= 1;
299*7c478bd9Sstevel@tonic-gate 			}
300*7c478bd9Sstevel@tonic-gate 			release(p);
301*7c478bd9Sstevel@tonic-gate 			q = salloc(1);
302*7c478bd9Sstevel@tonic-gate 			if (n >= 100) {
303*7c478bd9Sstevel@tonic-gate 				sputc(q, n%100);
304*7c478bd9Sstevel@tonic-gate 				n /= 100;
305*7c478bd9Sstevel@tonic-gate 			}
306*7c478bd9Sstevel@tonic-gate 			sputc(q, n);
307*7c478bd9Sstevel@tonic-gate 			sputc(q, 0);
308*7c478bd9Sstevel@tonic-gate 			pushp(q);
309*7c478bd9Sstevel@tonic-gate 			continue;
310*7c478bd9Sstevel@tonic-gate 		case 'i':
311*7c478bd9Sstevel@tonic-gate 			p = pop();
312*7c478bd9Sstevel@tonic-gate 			EMPTY;
313*7c478bd9Sstevel@tonic-gate 			p = scalint(p);
314*7c478bd9Sstevel@tonic-gate 
315*7c478bd9Sstevel@tonic-gate 			/*
316*7c478bd9Sstevel@tonic-gate 			 * POSIX.2
317*7c478bd9Sstevel@tonic-gate 			 * input base must be between 2 and 16
318*7c478bd9Sstevel@tonic-gate 			 */
319*7c478bd9Sstevel@tonic-gate 			n = length(p);
320*7c478bd9Sstevel@tonic-gate 			q = copy(p, n);
321*7c478bd9Sstevel@tonic-gate 			fsfile(q);
322*7c478bd9Sstevel@tonic-gate 			c = sbackc(q);
323*7c478bd9Sstevel@tonic-gate 			if (sfbeg(q) == 0)
324*7c478bd9Sstevel@tonic-gate 				error(gettext("input base is too large\n"));
325*7c478bd9Sstevel@tonic-gate 			if (c < 2)
326*7c478bd9Sstevel@tonic-gate 				error(gettext("input base is too small\n"));
327*7c478bd9Sstevel@tonic-gate 			if (c > 16)
328*7c478bd9Sstevel@tonic-gate 				error(gettext("input base is too large\n"));
329*7c478bd9Sstevel@tonic-gate 			release(q);
330*7c478bd9Sstevel@tonic-gate 
331*7c478bd9Sstevel@tonic-gate 			release(inbas);
332*7c478bd9Sstevel@tonic-gate 			inbas = p;
333*7c478bd9Sstevel@tonic-gate 			continue;
334*7c478bd9Sstevel@tonic-gate 		case 'I':
335*7c478bd9Sstevel@tonic-gate 			p = copy(inbas, length(inbas) + 1);
336*7c478bd9Sstevel@tonic-gate 			sputc(p, 0);
337*7c478bd9Sstevel@tonic-gate 			pushp(p);
338*7c478bd9Sstevel@tonic-gate 			continue;
339*7c478bd9Sstevel@tonic-gate 		case 'o':
340*7c478bd9Sstevel@tonic-gate 			p = pop();
341*7c478bd9Sstevel@tonic-gate 			EMPTY;
342*7c478bd9Sstevel@tonic-gate 			p = scalint(p);
343*7c478bd9Sstevel@tonic-gate 			sign = 0;
344*7c478bd9Sstevel@tonic-gate 			n = length(p);
345*7c478bd9Sstevel@tonic-gate 			q = copy(p, n);
346*7c478bd9Sstevel@tonic-gate 			fsfile(q);
347*7c478bd9Sstevel@tonic-gate 			l = c = sbackc(q);
348*7c478bd9Sstevel@tonic-gate 			if (n != 1) {
349*7c478bd9Sstevel@tonic-gate 				if (c < 0) {
350*7c478bd9Sstevel@tonic-gate 					sign = 1;
351*7c478bd9Sstevel@tonic-gate 					chsign(q);
352*7c478bd9Sstevel@tonic-gate 					n = length(q);
353*7c478bd9Sstevel@tonic-gate 					fsfile(q);
354*7c478bd9Sstevel@tonic-gate 					l = c = sbackc(q);
355*7c478bd9Sstevel@tonic-gate 				}
356*7c478bd9Sstevel@tonic-gate 				if (n != 1) {
357*7c478bd9Sstevel@tonic-gate 					while (sfbeg(q) == 0)
358*7c478bd9Sstevel@tonic-gate 						l = l * 100 + sbackc(q);
359*7c478bd9Sstevel@tonic-gate 				}
360*7c478bd9Sstevel@tonic-gate 			}
361*7c478bd9Sstevel@tonic-gate 
362*7c478bd9Sstevel@tonic-gate 			/*
363*7c478bd9Sstevel@tonic-gate 			 * POSIX.2
364*7c478bd9Sstevel@tonic-gate 			 * Check that output base is less than or equal
365*7c478bd9Sstevel@tonic-gate 			 * BC_BASE_MAX.
366*7c478bd9Sstevel@tonic-gate 			 */
367*7c478bd9Sstevel@tonic-gate 			if (l > BC_BASE_MAX)
368*7c478bd9Sstevel@tonic-gate 				error(gettext("output base is too large\n"));
369*7c478bd9Sstevel@tonic-gate 
370*7c478bd9Sstevel@tonic-gate 			logo = log2(l);
371*7c478bd9Sstevel@tonic-gate 			obase = l;
372*7c478bd9Sstevel@tonic-gate 			release(basptr);
373*7c478bd9Sstevel@tonic-gate 			if (sign == 1)
374*7c478bd9Sstevel@tonic-gate 				obase = -l;
375*7c478bd9Sstevel@tonic-gate 			basptr = p;
376*7c478bd9Sstevel@tonic-gate 			outdit = bigot;
377*7c478bd9Sstevel@tonic-gate 			if (n == 1 && sign == 0) {
378*7c478bd9Sstevel@tonic-gate 				if (c <= 16) {
379*7c478bd9Sstevel@tonic-gate 					outdit = hexot;
380*7c478bd9Sstevel@tonic-gate 					fw = 1;
381*7c478bd9Sstevel@tonic-gate 					fw1 = 0;
382*7c478bd9Sstevel@tonic-gate 
383*7c478bd9Sstevel@tonic-gate 					/*
384*7c478bd9Sstevel@tonic-gate 					 * POSIX.2
385*7c478bd9Sstevel@tonic-gate 					 * Line length is 70 characters,
386*7c478bd9Sstevel@tonic-gate 					 * including newline.
387*7c478bd9Sstevel@tonic-gate 					 */
388*7c478bd9Sstevel@tonic-gate 					ll = 70;
389*7c478bd9Sstevel@tonic-gate 					release(q);
390*7c478bd9Sstevel@tonic-gate 					continue;
391*7c478bd9Sstevel@tonic-gate 				}
392*7c478bd9Sstevel@tonic-gate 			}
393*7c478bd9Sstevel@tonic-gate 			n = 0;
394*7c478bd9Sstevel@tonic-gate 			if (sign == 1)
395*7c478bd9Sstevel@tonic-gate 				n++;
396*7c478bd9Sstevel@tonic-gate 			p = salloc(1);
397*7c478bd9Sstevel@tonic-gate 			sputc(p, -1);
398*7c478bd9Sstevel@tonic-gate 			t = add(p, q);
399*7c478bd9Sstevel@tonic-gate 			n += length(t) * 2;
400*7c478bd9Sstevel@tonic-gate 			fsfile(t);
401*7c478bd9Sstevel@tonic-gate 			if ((c = sbackc(t)) > 9)
402*7c478bd9Sstevel@tonic-gate 				n++;
403*7c478bd9Sstevel@tonic-gate 			release(t);
404*7c478bd9Sstevel@tonic-gate 			release(q);
405*7c478bd9Sstevel@tonic-gate 			release(p);
406*7c478bd9Sstevel@tonic-gate 			fw = n;
407*7c478bd9Sstevel@tonic-gate 			fw1 = n-1;
408*7c478bd9Sstevel@tonic-gate 
409*7c478bd9Sstevel@tonic-gate 			/*
410*7c478bd9Sstevel@tonic-gate 			 * POSIX.2
411*7c478bd9Sstevel@tonic-gate 			 * Line length is 70 characters including newline
412*7c478bd9Sstevel@tonic-gate 			 */
413*7c478bd9Sstevel@tonic-gate 			ll = 70;
414*7c478bd9Sstevel@tonic-gate 			if (fw >= ll)
415*7c478bd9Sstevel@tonic-gate 				continue;
416*7c478bd9Sstevel@tonic-gate 			ll = (70 / fw) * fw;
417*7c478bd9Sstevel@tonic-gate 			continue;
418*7c478bd9Sstevel@tonic-gate 		case 'O':
419*7c478bd9Sstevel@tonic-gate 			p = copy(basptr, length(basptr) + 1);
420*7c478bd9Sstevel@tonic-gate 			sputc(p, 0);
421*7c478bd9Sstevel@tonic-gate 			pushp(p);
422*7c478bd9Sstevel@tonic-gate 			continue;
423*7c478bd9Sstevel@tonic-gate 		case '[':
424*7c478bd9Sstevel@tonic-gate 			n = 0;
425*7c478bd9Sstevel@tonic-gate 			p = salloc(0);
426*7c478bd9Sstevel@tonic-gate 			for (; ; ) {
427*7c478bd9Sstevel@tonic-gate 				if ((c = readc()) == ']') {
428*7c478bd9Sstevel@tonic-gate 					if (n == 0)
429*7c478bd9Sstevel@tonic-gate 						break;
430*7c478bd9Sstevel@tonic-gate 					n--;
431*7c478bd9Sstevel@tonic-gate 				}
432*7c478bd9Sstevel@tonic-gate 				sputc(p, c);
433*7c478bd9Sstevel@tonic-gate 				if (c == '[')
434*7c478bd9Sstevel@tonic-gate 					n++;
435*7c478bd9Sstevel@tonic-gate 			}
436*7c478bd9Sstevel@tonic-gate 			pushp(p);
437*7c478bd9Sstevel@tonic-gate 			continue;
438*7c478bd9Sstevel@tonic-gate 		case 'k':
439*7c478bd9Sstevel@tonic-gate 			p = pop();
440*7c478bd9Sstevel@tonic-gate 			EMPTY;
441*7c478bd9Sstevel@tonic-gate 			p = scalint(p);
442*7c478bd9Sstevel@tonic-gate 
443*7c478bd9Sstevel@tonic-gate 			/*
444*7c478bd9Sstevel@tonic-gate 			 * POSIX.2
445*7c478bd9Sstevel@tonic-gate 			 * Make sure scaling factor is between 0 and
446*7c478bd9Sstevel@tonic-gate 			 * BC_SCALE_MAX.  Copy p to q and figure the
447*7c478bd9Sstevel@tonic-gate 			 * scaling factor.
448*7c478bd9Sstevel@tonic-gate 			 */
449*7c478bd9Sstevel@tonic-gate 			n = length(p);
450*7c478bd9Sstevel@tonic-gate 			q = copy(p, n);
451*7c478bd9Sstevel@tonic-gate 			fsfile(q);
452*7c478bd9Sstevel@tonic-gate 			c = 0;
453*7c478bd9Sstevel@tonic-gate 			if ((sfbeg(q) == 0) && ((c = sbackc(q)) < 0))
454*7c478bd9Sstevel@tonic-gate 				error(gettext("invalid scale factor\n"));
455*7c478bd9Sstevel@tonic-gate 
456*7c478bd9Sstevel@tonic-gate 			scalev = 1;
457*7c478bd9Sstevel@tonic-gate 			while (c < BC_SCALE_MAX && sfbeg(q) == 0)
458*7c478bd9Sstevel@tonic-gate 				c = (c * (scalev *= 100)) + sbackc(q);
459*7c478bd9Sstevel@tonic-gate 
460*7c478bd9Sstevel@tonic-gate 			if (c > BC_SCALE_MAX)
461*7c478bd9Sstevel@tonic-gate 				error(gettext("scale factor is too large\n"));
462*7c478bd9Sstevel@tonic-gate 			release(q);
463*7c478bd9Sstevel@tonic-gate 
464*7c478bd9Sstevel@tonic-gate 			rewind(p);
465*7c478bd9Sstevel@tonic-gate 			k = sfeof(p) ? 0 : sgetc(p);
466*7c478bd9Sstevel@tonic-gate 			release(scalptr);
467*7c478bd9Sstevel@tonic-gate 			scalptr = p;
468*7c478bd9Sstevel@tonic-gate 			continue;
469*7c478bd9Sstevel@tonic-gate 
470*7c478bd9Sstevel@tonic-gate 		case 'K':
471*7c478bd9Sstevel@tonic-gate 			p = copy(scalptr, length(scalptr) + 1);
472*7c478bd9Sstevel@tonic-gate 			sputc(p, 0);
473*7c478bd9Sstevel@tonic-gate 			pushp(p);
474*7c478bd9Sstevel@tonic-gate 			continue;
475*7c478bd9Sstevel@tonic-gate 		case 'X':
476*7c478bd9Sstevel@tonic-gate 			p = pop();
477*7c478bd9Sstevel@tonic-gate 			EMPTY;
478*7c478bd9Sstevel@tonic-gate 			fsfile(p);
479*7c478bd9Sstevel@tonic-gate 			n = sbackc(p);
480*7c478bd9Sstevel@tonic-gate 			release(p);
481*7c478bd9Sstevel@tonic-gate 			p = salloc(2);
482*7c478bd9Sstevel@tonic-gate 			sputc(p, n);
483*7c478bd9Sstevel@tonic-gate 			sputc(p, 0);
484*7c478bd9Sstevel@tonic-gate 			pushp(p);
485*7c478bd9Sstevel@tonic-gate 			continue;
486*7c478bd9Sstevel@tonic-gate 		case 'Q':
487*7c478bd9Sstevel@tonic-gate 			p = pop();
488*7c478bd9Sstevel@tonic-gate 			EMPTY;
489*7c478bd9Sstevel@tonic-gate 			if (length(p) > 2) {
490*7c478bd9Sstevel@tonic-gate 				error("Q?\n");
491*7c478bd9Sstevel@tonic-gate 			}
492*7c478bd9Sstevel@tonic-gate 			rewind(p);
493*7c478bd9Sstevel@tonic-gate 			if ((c =  sgetc(p)) < 0) {
494*7c478bd9Sstevel@tonic-gate 				error(gettext("neg Q\n"));
495*7c478bd9Sstevel@tonic-gate 			}
496*7c478bd9Sstevel@tonic-gate 			release(p);
497*7c478bd9Sstevel@tonic-gate 			while (c-- > 0) {
498*7c478bd9Sstevel@tonic-gate 				if (readptr == &readstk[0]) {
499*7c478bd9Sstevel@tonic-gate 					error("readstk?\n");
500*7c478bd9Sstevel@tonic-gate 				}
501*7c478bd9Sstevel@tonic-gate 				if (*readptr != 0)
502*7c478bd9Sstevel@tonic-gate 					release(*readptr);
503*7c478bd9Sstevel@tonic-gate 				readptr--;
504*7c478bd9Sstevel@tonic-gate 			}
505*7c478bd9Sstevel@tonic-gate 			continue;
506*7c478bd9Sstevel@tonic-gate 		case 'q':
507*7c478bd9Sstevel@tonic-gate 			if (readptr <= &readstk[1])
508*7c478bd9Sstevel@tonic-gate 				exit(0);
509*7c478bd9Sstevel@tonic-gate 			if (*readptr != 0)
510*7c478bd9Sstevel@tonic-gate 				release(*readptr);
511*7c478bd9Sstevel@tonic-gate 			readptr--;
512*7c478bd9Sstevel@tonic-gate 			if (*readptr != 0)
513*7c478bd9Sstevel@tonic-gate 				release(*readptr);
514*7c478bd9Sstevel@tonic-gate 			readptr--;
515*7c478bd9Sstevel@tonic-gate 			continue;
516*7c478bd9Sstevel@tonic-gate 		case 'f':
517*7c478bd9Sstevel@tonic-gate 			if (stkptr == &stack[0])
518*7c478bd9Sstevel@tonic-gate 				printf(gettext("empty stack\n"));
519*7c478bd9Sstevel@tonic-gate 			else {
520*7c478bd9Sstevel@tonic-gate 				for (ptr = stkptr; ptr > &stack[0]; ) {
521*7c478bd9Sstevel@tonic-gate 					print(*ptr--);
522*7c478bd9Sstevel@tonic-gate 				}
523*7c478bd9Sstevel@tonic-gate 			}
524*7c478bd9Sstevel@tonic-gate 			continue;
525*7c478bd9Sstevel@tonic-gate 		case 'p':
526*7c478bd9Sstevel@tonic-gate 			if (stkptr == &stack[0])
527*7c478bd9Sstevel@tonic-gate 				printf(gettext("empty stack\n"));
528*7c478bd9Sstevel@tonic-gate 			else {
529*7c478bd9Sstevel@tonic-gate 				print(*stkptr);
530*7c478bd9Sstevel@tonic-gate 			}
531*7c478bd9Sstevel@tonic-gate 			continue;
532*7c478bd9Sstevel@tonic-gate 		case 'P':
533*7c478bd9Sstevel@tonic-gate 			p = pop();
534*7c478bd9Sstevel@tonic-gate 			EMPTY;
535*7c478bd9Sstevel@tonic-gate 			sputc(p, 0);
536*7c478bd9Sstevel@tonic-gate 			printf("%s", p->beg);
537*7c478bd9Sstevel@tonic-gate 			release(p);
538*7c478bd9Sstevel@tonic-gate 			continue;
539*7c478bd9Sstevel@tonic-gate 		case 'd':
540*7c478bd9Sstevel@tonic-gate 			if (stkptr == &stack[0]) {
541*7c478bd9Sstevel@tonic-gate 				printf(gettext("empty stack\n"));
542*7c478bd9Sstevel@tonic-gate 				continue;
543*7c478bd9Sstevel@tonic-gate 			}
544*7c478bd9Sstevel@tonic-gate 			q = *stkptr;
545*7c478bd9Sstevel@tonic-gate 			n = length(q);
546*7c478bd9Sstevel@tonic-gate 			p = copy(*stkptr, n);
547*7c478bd9Sstevel@tonic-gate 			pushp(p);
548*7c478bd9Sstevel@tonic-gate 			continue;
549*7c478bd9Sstevel@tonic-gate 		case 'c':
550*7c478bd9Sstevel@tonic-gate 			while (stkerr == 0) {
551*7c478bd9Sstevel@tonic-gate 				p = pop();
552*7c478bd9Sstevel@tonic-gate 				if (stkerr == 0)
553*7c478bd9Sstevel@tonic-gate 					release(p);
554*7c478bd9Sstevel@tonic-gate 			}
555*7c478bd9Sstevel@tonic-gate 			continue;
556*7c478bd9Sstevel@tonic-gate 		case 'S':
557*7c478bd9Sstevel@tonic-gate 			if (stkptr == &stack[0]) {
558*7c478bd9Sstevel@tonic-gate 				error(gettext("save: args\n"));
559*7c478bd9Sstevel@tonic-gate 			}
560*7c478bd9Sstevel@tonic-gate 			c = readc() & 0377;
561*7c478bd9Sstevel@tonic-gate 			sptr = stable[c];
562*7c478bd9Sstevel@tonic-gate 			sp = stable[c] = sfree;
563*7c478bd9Sstevel@tonic-gate 			sfree = sfree->next;
564*7c478bd9Sstevel@tonic-gate 			if (sfree == 0)
565*7c478bd9Sstevel@tonic-gate 				goto sempty;
566*7c478bd9Sstevel@tonic-gate 			sp->next = sptr;
567*7c478bd9Sstevel@tonic-gate 			p = pop();
568*7c478bd9Sstevel@tonic-gate 			EMPTY;
569*7c478bd9Sstevel@tonic-gate 			if (c >= ARRAYST) {
570*7c478bd9Sstevel@tonic-gate 				q = copy(p, length(p) + PTRSZ);
571*7c478bd9Sstevel@tonic-gate 				for (n = 0; n < PTRSZ; n++) {
572*7c478bd9Sstevel@tonic-gate 					sputc(q, 0);
573*7c478bd9Sstevel@tonic-gate 				}
574*7c478bd9Sstevel@tonic-gate 				release(p);
575*7c478bd9Sstevel@tonic-gate 				p = q;
576*7c478bd9Sstevel@tonic-gate 			}
577*7c478bd9Sstevel@tonic-gate 			sp->val = p;
578*7c478bd9Sstevel@tonic-gate 			continue;
579*7c478bd9Sstevel@tonic-gate sempty:
580*7c478bd9Sstevel@tonic-gate 			error(gettext("symbol table overflow\n"));
581*7c478bd9Sstevel@tonic-gate 		case 's':
582*7c478bd9Sstevel@tonic-gate 			if (stkptr == &stack[0]) {
583*7c478bd9Sstevel@tonic-gate 				error(gettext("save:args\n"));
584*7c478bd9Sstevel@tonic-gate 			}
585*7c478bd9Sstevel@tonic-gate 			c = readc() & 0377;
586*7c478bd9Sstevel@tonic-gate 			sptr = stable[c];
587*7c478bd9Sstevel@tonic-gate 			if (sptr != 0) {
588*7c478bd9Sstevel@tonic-gate 				p = sptr->val;
589*7c478bd9Sstevel@tonic-gate 				if (c >= ARRAYST) {
590*7c478bd9Sstevel@tonic-gate 					rewind(p);
591*7c478bd9Sstevel@tonic-gate 					while (sfeof(p) == 0)
592*7c478bd9Sstevel@tonic-gate 						release(getwd(p));
593*7c478bd9Sstevel@tonic-gate 				}
594*7c478bd9Sstevel@tonic-gate 				release(p);
595*7c478bd9Sstevel@tonic-gate 			} else {
596*7c478bd9Sstevel@tonic-gate 				sptr = stable[c] = sfree;
597*7c478bd9Sstevel@tonic-gate 				sfree = sfree->next;
598*7c478bd9Sstevel@tonic-gate 				if (sfree == 0)
599*7c478bd9Sstevel@tonic-gate 					goto sempty;
600*7c478bd9Sstevel@tonic-gate 				sptr->next = 0;
601*7c478bd9Sstevel@tonic-gate 			}
602*7c478bd9Sstevel@tonic-gate 			p = pop();
603*7c478bd9Sstevel@tonic-gate 			sptr->val = p;
604*7c478bd9Sstevel@tonic-gate 			continue;
605*7c478bd9Sstevel@tonic-gate 		case 'l':
606*7c478bd9Sstevel@tonic-gate 			load();
607*7c478bd9Sstevel@tonic-gate 			continue;
608*7c478bd9Sstevel@tonic-gate 		case 'L':
609*7c478bd9Sstevel@tonic-gate 			c = readc() & 0377;
610*7c478bd9Sstevel@tonic-gate 			sptr = stable[c];
611*7c478bd9Sstevel@tonic-gate 			if (sptr == 0) {
612*7c478bd9Sstevel@tonic-gate 				error("L?\n");
613*7c478bd9Sstevel@tonic-gate 			}
614*7c478bd9Sstevel@tonic-gate 			stable[c] = sptr->next;
615*7c478bd9Sstevel@tonic-gate 			sptr->next = sfree;
616*7c478bd9Sstevel@tonic-gate 			sfree = sptr;
617*7c478bd9Sstevel@tonic-gate 			p = sptr->val;
618*7c478bd9Sstevel@tonic-gate 			if (c >= ARRAYST) {
619*7c478bd9Sstevel@tonic-gate 				rewind(p);
620*7c478bd9Sstevel@tonic-gate 				while (sfeof(p) == 0) {
621*7c478bd9Sstevel@tonic-gate 					q = getwd(p);
622*7c478bd9Sstevel@tonic-gate 					if (q != 0)
623*7c478bd9Sstevel@tonic-gate 						release(q);
624*7c478bd9Sstevel@tonic-gate 				}
625*7c478bd9Sstevel@tonic-gate 			}
626*7c478bd9Sstevel@tonic-gate 			pushp(p);
627*7c478bd9Sstevel@tonic-gate 			continue;
628*7c478bd9Sstevel@tonic-gate 		case ':':
629*7c478bd9Sstevel@tonic-gate 			p = pop();
630*7c478bd9Sstevel@tonic-gate 			EMPTY;
631*7c478bd9Sstevel@tonic-gate 			q = scalint(p);
632*7c478bd9Sstevel@tonic-gate 			fsfile(q);
633*7c478bd9Sstevel@tonic-gate 
634*7c478bd9Sstevel@tonic-gate 			/*
635*7c478bd9Sstevel@tonic-gate 			 * POSIX.2
636*7c478bd9Sstevel@tonic-gate 			 * Make sure index is between 0 and BC_DIM_MAX-1
637*7c478bd9Sstevel@tonic-gate 			 */
638*7c478bd9Sstevel@tonic-gate 			c = 0;
639*7c478bd9Sstevel@tonic-gate 			if ((sfbeg(q) == 0) && ((c = sbackc(q)) < 0))
640*7c478bd9Sstevel@tonic-gate 				error(gettext("invalid index\n"));
641*7c478bd9Sstevel@tonic-gate 			scalev = 1;
642*7c478bd9Sstevel@tonic-gate 			while (c < BC_DIM_MAX && sfbeg(q) == 0)
643*7c478bd9Sstevel@tonic-gate 				c = (c * (scalev *= 100)) + sbackc(q);
644*7c478bd9Sstevel@tonic-gate 
645*7c478bd9Sstevel@tonic-gate 			if (c >= BC_DIM_MAX)
646*7c478bd9Sstevel@tonic-gate 				error(gettext("index is too large\n"));
647*7c478bd9Sstevel@tonic-gate 
648*7c478bd9Sstevel@tonic-gate 			release(q);
649*7c478bd9Sstevel@tonic-gate 			n = readc() & 0377;
650*7c478bd9Sstevel@tonic-gate 			sptr = stable[n];
651*7c478bd9Sstevel@tonic-gate 			if (sptr == 0) {
652*7c478bd9Sstevel@tonic-gate 				sptr = stable[n] = sfree;
653*7c478bd9Sstevel@tonic-gate 				sfree = sfree->next;
654*7c478bd9Sstevel@tonic-gate 				if (sfree == 0)
655*7c478bd9Sstevel@tonic-gate 					goto sempty;
656*7c478bd9Sstevel@tonic-gate 				sptr->next = 0;
657*7c478bd9Sstevel@tonic-gate 				p = salloc((c + PTRSZ) * PTRSZ);
658*7c478bd9Sstevel@tonic-gate 				zero(p);
659*7c478bd9Sstevel@tonic-gate 			} else {
660*7c478bd9Sstevel@tonic-gate 				p = sptr->val;
661*7c478bd9Sstevel@tonic-gate 				if (length(p) - PTRSZ < c * PTRSZ) {
662*7c478bd9Sstevel@tonic-gate 					q = copy(p, (c + PTRSZ) * PTRSZ);
663*7c478bd9Sstevel@tonic-gate 					release(p);
664*7c478bd9Sstevel@tonic-gate 					p = q;
665*7c478bd9Sstevel@tonic-gate 				}
666*7c478bd9Sstevel@tonic-gate 			}
667*7c478bd9Sstevel@tonic-gate 			seekc(p, c * PTRSZ);
668*7c478bd9Sstevel@tonic-gate 			q = lookwd(p);
669*7c478bd9Sstevel@tonic-gate 			if (q != NULL)
670*7c478bd9Sstevel@tonic-gate 				release(q);
671*7c478bd9Sstevel@tonic-gate 			s = pop();
672*7c478bd9Sstevel@tonic-gate 			EMPTY;
673*7c478bd9Sstevel@tonic-gate 			salterwd((struct wblk *)p, s);
674*7c478bd9Sstevel@tonic-gate 			sptr->val = p;
675*7c478bd9Sstevel@tonic-gate 			continue;
676*7c478bd9Sstevel@tonic-gate 
677*7c478bd9Sstevel@tonic-gate 		case ';':
678*7c478bd9Sstevel@tonic-gate 			p = pop();
679*7c478bd9Sstevel@tonic-gate 			EMPTY;
680*7c478bd9Sstevel@tonic-gate 			q = scalint(p);
681*7c478bd9Sstevel@tonic-gate 			fsfile(q);
682*7c478bd9Sstevel@tonic-gate 
683*7c478bd9Sstevel@tonic-gate 			/*
684*7c478bd9Sstevel@tonic-gate 			 * POSIX.2
685*7c478bd9Sstevel@tonic-gate 			 * Make sure index is between 0 and BC_DIM_MAX-1
686*7c478bd9Sstevel@tonic-gate 			 */
687*7c478bd9Sstevel@tonic-gate 			c = 0;
688*7c478bd9Sstevel@tonic-gate 			if ((sfbeg(q) == 0) && ((c = sbackc(q)) < 0))
689*7c478bd9Sstevel@tonic-gate 				error(gettext("invalid index\n"));
690*7c478bd9Sstevel@tonic-gate 			scalev = 1;
691*7c478bd9Sstevel@tonic-gate 			while (c < BC_DIM_MAX && sfbeg(q) == 0)
692*7c478bd9Sstevel@tonic-gate 				c = (c * (scalev *= 100)) + sbackc(q);
693*7c478bd9Sstevel@tonic-gate 
694*7c478bd9Sstevel@tonic-gate 			if (c >= BC_DIM_MAX)
695*7c478bd9Sstevel@tonic-gate 				error(gettext("index is too large\n"));
696*7c478bd9Sstevel@tonic-gate 
697*7c478bd9Sstevel@tonic-gate 			release(q);
698*7c478bd9Sstevel@tonic-gate 			n = readc() & 0377;
699*7c478bd9Sstevel@tonic-gate 			sptr = stable[n];
700*7c478bd9Sstevel@tonic-gate 			if (sptr != 0) {
701*7c478bd9Sstevel@tonic-gate 				p = sptr->val;
702*7c478bd9Sstevel@tonic-gate 				if (length(p) - PTRSZ >= c * PTRSZ) {
703*7c478bd9Sstevel@tonic-gate 					seekc(p, c * PTRSZ);
704*7c478bd9Sstevel@tonic-gate 					s = getwd(p);
705*7c478bd9Sstevel@tonic-gate 					if (s != 0) {
706*7c478bd9Sstevel@tonic-gate 						q = copy(s, length(s));
707*7c478bd9Sstevel@tonic-gate 						pushp(q);
708*7c478bd9Sstevel@tonic-gate 						continue;
709*7c478bd9Sstevel@tonic-gate 					}
710*7c478bd9Sstevel@tonic-gate 				}
711*7c478bd9Sstevel@tonic-gate 			}
712*7c478bd9Sstevel@tonic-gate 			q = salloc(1);	/* uninitializd array elt prints as 0 */
713*7c478bd9Sstevel@tonic-gate 			sputc(q, 0);
714*7c478bd9Sstevel@tonic-gate 			pushp(q);
715*7c478bd9Sstevel@tonic-gate 			continue;
716*7c478bd9Sstevel@tonic-gate 		case 'x':
717*7c478bd9Sstevel@tonic-gate execute:
718*7c478bd9Sstevel@tonic-gate 			p = pop();
719*7c478bd9Sstevel@tonic-gate 			EMPTY;
720*7c478bd9Sstevel@tonic-gate 			if ((readptr != &readstk[0]) && (*readptr != 0)) {
721*7c478bd9Sstevel@tonic-gate 				if ((*readptr)->rd == (*readptr)->wt)
722*7c478bd9Sstevel@tonic-gate 					release(*readptr);
723*7c478bd9Sstevel@tonic-gate 				else {
724*7c478bd9Sstevel@tonic-gate 					if (readptr++ == &readstk[RDSKSZ]) {
725*7c478bd9Sstevel@tonic-gate 						error(gettext
726*7c478bd9Sstevel@tonic-gate 						    ("nesting depth\n"));
727*7c478bd9Sstevel@tonic-gate 					}
728*7c478bd9Sstevel@tonic-gate 				}
729*7c478bd9Sstevel@tonic-gate 			} else
730*7c478bd9Sstevel@tonic-gate 				readptr++;
731*7c478bd9Sstevel@tonic-gate 			*readptr = p;
732*7c478bd9Sstevel@tonic-gate 			if (p != 0)
733*7c478bd9Sstevel@tonic-gate 				rewind(p);
734*7c478bd9Sstevel@tonic-gate 			else {
735*7c478bd9Sstevel@tonic-gate 				if ((c = readc()) != '\n')
736*7c478bd9Sstevel@tonic-gate 					unreadc(c);
737*7c478bd9Sstevel@tonic-gate 			}
738*7c478bd9Sstevel@tonic-gate 			continue;
739*7c478bd9Sstevel@tonic-gate 		case '?':
740*7c478bd9Sstevel@tonic-gate 			if (++readptr == &readstk[RDSKSZ]) {
741*7c478bd9Sstevel@tonic-gate 				error(gettext("nesting depth\n"));
742*7c478bd9Sstevel@tonic-gate 			}
743*7c478bd9Sstevel@tonic-gate 			*readptr = 0;
744*7c478bd9Sstevel@tonic-gate 			fsave = curfile;
745*7c478bd9Sstevel@tonic-gate 			curfile = stdin;
746*7c478bd9Sstevel@tonic-gate 			while ((c = readc()) == '!')
747*7c478bd9Sstevel@tonic-gate 				command();
748*7c478bd9Sstevel@tonic-gate 			p = salloc(0);
749*7c478bd9Sstevel@tonic-gate 			sputc(p, c);
750*7c478bd9Sstevel@tonic-gate 			while ((c = readc()) != '\n') {
751*7c478bd9Sstevel@tonic-gate 				sputc(p, c);
752*7c478bd9Sstevel@tonic-gate 				if (c == '\\')
753*7c478bd9Sstevel@tonic-gate 					sputc(p, readc());
754*7c478bd9Sstevel@tonic-gate 			}
755*7c478bd9Sstevel@tonic-gate 			curfile = fsave;
756*7c478bd9Sstevel@tonic-gate 			*readptr = p;
757*7c478bd9Sstevel@tonic-gate 			continue;
758*7c478bd9Sstevel@tonic-gate 		case '!':
759*7c478bd9Sstevel@tonic-gate 			if (command() == 1)
760*7c478bd9Sstevel@tonic-gate 				goto execute;
761*7c478bd9Sstevel@tonic-gate 			continue;
762*7c478bd9Sstevel@tonic-gate 		case '<':
763*7c478bd9Sstevel@tonic-gate 		case '>':
764*7c478bd9Sstevel@tonic-gate 		case '=':
765*7c478bd9Sstevel@tonic-gate 			if (cond(c) == 1)
766*7c478bd9Sstevel@tonic-gate 				goto execute;
767*7c478bd9Sstevel@tonic-gate 			continue;
768*7c478bd9Sstevel@tonic-gate 		default:
769*7c478bd9Sstevel@tonic-gate 			printf(gettext("%o is unimplemented\n"), c);
770*7c478bd9Sstevel@tonic-gate 		}
771*7c478bd9Sstevel@tonic-gate 	}
772*7c478bd9Sstevel@tonic-gate }
773*7c478bd9Sstevel@tonic-gate 
774*7c478bd9Sstevel@tonic-gate struct blk *
775*7c478bd9Sstevel@tonic-gate div(struct blk *ddivd, struct blk *ddivr)
776*7c478bd9Sstevel@tonic-gate {
777*7c478bd9Sstevel@tonic-gate 	int divsign, remsign, offset, divcarry;
778*7c478bd9Sstevel@tonic-gate 	int carry, dig, magic, d, dd, under;
779*7c478bd9Sstevel@tonic-gate 	long c, td, cc;
780*7c478bd9Sstevel@tonic-gate 	struct blk *ps, *px;
781*7c478bd9Sstevel@tonic-gate 	struct blk *p, *divd, *divr;
782*7c478bd9Sstevel@tonic-gate 
783*7c478bd9Sstevel@tonic-gate 	rem = 0;
784*7c478bd9Sstevel@tonic-gate 	p = salloc(0);
785*7c478bd9Sstevel@tonic-gate 	if (length(ddivr) == 0) {
786*7c478bd9Sstevel@tonic-gate 		pushp(ddivr);
787*7c478bd9Sstevel@tonic-gate 		printf(gettext("divide by 0\n"));
788*7c478bd9Sstevel@tonic-gate 		return (p);
789*7c478bd9Sstevel@tonic-gate 	}
790*7c478bd9Sstevel@tonic-gate 	divsign = remsign = 0;
791*7c478bd9Sstevel@tonic-gate 	divr = ddivr;
792*7c478bd9Sstevel@tonic-gate 	fsfile(divr);
793*7c478bd9Sstevel@tonic-gate 	if (sbackc(divr) == -1) {
794*7c478bd9Sstevel@tonic-gate 		divr = copy(ddivr, length(ddivr));
795*7c478bd9Sstevel@tonic-gate 		chsign(divr);
796*7c478bd9Sstevel@tonic-gate 		divsign = ~divsign;
797*7c478bd9Sstevel@tonic-gate 	}
798*7c478bd9Sstevel@tonic-gate 	divd = copy(ddivd, length(ddivd));
799*7c478bd9Sstevel@tonic-gate 	fsfile(divd);
800*7c478bd9Sstevel@tonic-gate 	if (sfbeg(divd) == 0 && sbackc(divd) == -1) {
801*7c478bd9Sstevel@tonic-gate 		chsign(divd);
802*7c478bd9Sstevel@tonic-gate 		divsign = ~divsign;
803*7c478bd9Sstevel@tonic-gate 		remsign = ~remsign;
804*7c478bd9Sstevel@tonic-gate 	}
805*7c478bd9Sstevel@tonic-gate 	offset = length(divd) - length(divr);
806*7c478bd9Sstevel@tonic-gate 	if (offset < 0)
807*7c478bd9Sstevel@tonic-gate 		goto ddone;
808*7c478bd9Sstevel@tonic-gate 	seekc(p, offset + 1);
809*7c478bd9Sstevel@tonic-gate 	sputc(divd, 0);
810*7c478bd9Sstevel@tonic-gate 	magic = 0;
811*7c478bd9Sstevel@tonic-gate 	fsfile(divr);
812*7c478bd9Sstevel@tonic-gate 	c = sbackc(divr);
813*7c478bd9Sstevel@tonic-gate 	if (c < 10)
814*7c478bd9Sstevel@tonic-gate 		magic++;
815*7c478bd9Sstevel@tonic-gate 	c = c * 100 + (sfbeg(divr)?0:sbackc(divr));
816*7c478bd9Sstevel@tonic-gate 	if (magic > 0) {
817*7c478bd9Sstevel@tonic-gate 		c = (c * 100 +(sfbeg(divr)?0:sbackc(divr)))*2;
818*7c478bd9Sstevel@tonic-gate 		c /= 25;
819*7c478bd9Sstevel@tonic-gate 	}
820*7c478bd9Sstevel@tonic-gate 	while (offset >= 0) {
821*7c478bd9Sstevel@tonic-gate 		fsfile(divd);
822*7c478bd9Sstevel@tonic-gate 		td = sbackc(divd) * 100;
823*7c478bd9Sstevel@tonic-gate 		dd = sfbeg(divd)?0:sbackc(divd);
824*7c478bd9Sstevel@tonic-gate 		td = (td + dd) * 100;
825*7c478bd9Sstevel@tonic-gate 		dd = sfbeg(divd)?0:sbackc(divd);
826*7c478bd9Sstevel@tonic-gate 		td = td + dd;
827*7c478bd9Sstevel@tonic-gate 		cc = c;
828*7c478bd9Sstevel@tonic-gate 		if (offset == 0)
829*7c478bd9Sstevel@tonic-gate 			td++;
830*7c478bd9Sstevel@tonic-gate 		else
831*7c478bd9Sstevel@tonic-gate 			cc++;
832*7c478bd9Sstevel@tonic-gate 		if (magic != 0)
833*7c478bd9Sstevel@tonic-gate 			td = td<<3;
834*7c478bd9Sstevel@tonic-gate 		dig = td/cc;
835*7c478bd9Sstevel@tonic-gate 		under = 0;
836*7c478bd9Sstevel@tonic-gate 		if (td%cc < 8 && dig > 0 && magic) {
837*7c478bd9Sstevel@tonic-gate 			dig--;
838*7c478bd9Sstevel@tonic-gate 			under = 1;
839*7c478bd9Sstevel@tonic-gate 		}
840*7c478bd9Sstevel@tonic-gate 		rewind(divr);
841*7c478bd9Sstevel@tonic-gate 		rewind(divxyz);
842*7c478bd9Sstevel@tonic-gate 		carry = 0;
843*7c478bd9Sstevel@tonic-gate 		while (sfeof(divr) == 0) {
844*7c478bd9Sstevel@tonic-gate 			d = sgetc(divr) * dig + carry;
845*7c478bd9Sstevel@tonic-gate 			carry = d / 100;
846*7c478bd9Sstevel@tonic-gate 			salterc(divxyz, d % 100);
847*7c478bd9Sstevel@tonic-gate 		}
848*7c478bd9Sstevel@tonic-gate 		salterc(divxyz, carry);
849*7c478bd9Sstevel@tonic-gate 		rewind(divxyz);
850*7c478bd9Sstevel@tonic-gate 		seekc(divd, offset);
851*7c478bd9Sstevel@tonic-gate 		carry = 0;
852*7c478bd9Sstevel@tonic-gate 		while (sfeof(divd) == 0) {
853*7c478bd9Sstevel@tonic-gate 			d = slookc(divd);
854*7c478bd9Sstevel@tonic-gate 			d = d - (sfeof(divxyz) ? 0 : sgetc(divxyz)) - carry;
855*7c478bd9Sstevel@tonic-gate 			carry = 0;
856*7c478bd9Sstevel@tonic-gate 			if (d < 0) {
857*7c478bd9Sstevel@tonic-gate 				d += 100;
858*7c478bd9Sstevel@tonic-gate 				carry = 1;
859*7c478bd9Sstevel@tonic-gate 			}
860*7c478bd9Sstevel@tonic-gate 			salterc(divd, d);
861*7c478bd9Sstevel@tonic-gate 		}
862*7c478bd9Sstevel@tonic-gate 		divcarry = carry;
863*7c478bd9Sstevel@tonic-gate 		sbackc(p);
864*7c478bd9Sstevel@tonic-gate 		salterc(p, dig);
865*7c478bd9Sstevel@tonic-gate 		sbackc(p);
866*7c478bd9Sstevel@tonic-gate 		fsfile(divd);
867*7c478bd9Sstevel@tonic-gate 		d = sbackc(divd);
868*7c478bd9Sstevel@tonic-gate 		if ((d != 0) && /* !divcarry */ (offset != 0)) {
869*7c478bd9Sstevel@tonic-gate 			d = sbackc(divd) + 100;
870*7c478bd9Sstevel@tonic-gate 			salterc(divd, d);
871*7c478bd9Sstevel@tonic-gate 		}
872*7c478bd9Sstevel@tonic-gate 		if (--offset >= 0) {
873*7c478bd9Sstevel@tonic-gate 			divd->wt--;
874*7c478bd9Sstevel@tonic-gate 		}
875*7c478bd9Sstevel@tonic-gate 	}
876*7c478bd9Sstevel@tonic-gate 	if (under) {	/* undershot last - adjust */
877*7c478bd9Sstevel@tonic-gate 		px = copy(divr, length(divr));	/* 11/88 don't corrupt ddivr */
878*7c478bd9Sstevel@tonic-gate 		chsign(px);
879*7c478bd9Sstevel@tonic-gate 		ps = add(px, divd);
880*7c478bd9Sstevel@tonic-gate 		fsfile(ps);
881*7c478bd9Sstevel@tonic-gate 		if (length(ps) > 0 && sbackc(ps) < 0) {
882*7c478bd9Sstevel@tonic-gate 			release(ps);	/* only adjust in really undershot */
883*7c478bd9Sstevel@tonic-gate 		} else {
884*7c478bd9Sstevel@tonic-gate 			release(divd);
885*7c478bd9Sstevel@tonic-gate 			salterc(p, dig + 1);
886*7c478bd9Sstevel@tonic-gate 			divd = ps;
887*7c478bd9Sstevel@tonic-gate 		}
888*7c478bd9Sstevel@tonic-gate 	}
889*7c478bd9Sstevel@tonic-gate 	if (divcarry != 0) {
890*7c478bd9Sstevel@tonic-gate 		salterc(p, dig - 1);
891*7c478bd9Sstevel@tonic-gate 		salterc(divd, -1);
892*7c478bd9Sstevel@tonic-gate 		ps = add(divr, divd);
893*7c478bd9Sstevel@tonic-gate 		release(divd);
894*7c478bd9Sstevel@tonic-gate 		divd = ps;
895*7c478bd9Sstevel@tonic-gate 	}
896*7c478bd9Sstevel@tonic-gate 
897*7c478bd9Sstevel@tonic-gate 	rewind(p);
898*7c478bd9Sstevel@tonic-gate 	divcarry = 0;
899*7c478bd9Sstevel@tonic-gate 	while (sfeof(p) == 0) {
900*7c478bd9Sstevel@tonic-gate 		d = slookc(p) + divcarry;
901*7c478bd9Sstevel@tonic-gate 		divcarry = 0;
902*7c478bd9Sstevel@tonic-gate 		if (d >= 100) {
903*7c478bd9Sstevel@tonic-gate 			d -= 100;
904*7c478bd9Sstevel@tonic-gate 			divcarry = 1;
905*7c478bd9Sstevel@tonic-gate 		}
906*7c478bd9Sstevel@tonic-gate 		salterc(p, d);
907*7c478bd9Sstevel@tonic-gate 	}
908*7c478bd9Sstevel@tonic-gate 	if (divcarry != 0)
909*7c478bd9Sstevel@tonic-gate 		salterc(p, divcarry);
910*7c478bd9Sstevel@tonic-gate 	fsfile(p);
911*7c478bd9Sstevel@tonic-gate 	while (sfbeg(p) == 0) {
912*7c478bd9Sstevel@tonic-gate 		if (sbackc(p) == 0)
913*7c478bd9Sstevel@tonic-gate 			truncate(p);
914*7c478bd9Sstevel@tonic-gate 		else break;
915*7c478bd9Sstevel@tonic-gate 	}
916*7c478bd9Sstevel@tonic-gate 	if (divsign < 0)
917*7c478bd9Sstevel@tonic-gate 		chsign(p);
918*7c478bd9Sstevel@tonic-gate 	fsfile(divd);
919*7c478bd9Sstevel@tonic-gate 	while (sfbeg(divd) == 0) {
920*7c478bd9Sstevel@tonic-gate 		if (sbackc(divd) == 0)
921*7c478bd9Sstevel@tonic-gate 			truncate(divd);
922*7c478bd9Sstevel@tonic-gate 		else break;
923*7c478bd9Sstevel@tonic-gate 	}
924*7c478bd9Sstevel@tonic-gate ddone:
925*7c478bd9Sstevel@tonic-gate 	if (remsign < 0)
926*7c478bd9Sstevel@tonic-gate 		chsign(divd);
927*7c478bd9Sstevel@tonic-gate 	if (divr != ddivr)
928*7c478bd9Sstevel@tonic-gate 		release(divr);
929*7c478bd9Sstevel@tonic-gate 	rem = divd;
930*7c478bd9Sstevel@tonic-gate 	return (p);
931*7c478bd9Sstevel@tonic-gate }
932*7c478bd9Sstevel@tonic-gate 
933*7c478bd9Sstevel@tonic-gate int
934*7c478bd9Sstevel@tonic-gate dscale() {
935*7c478bd9Sstevel@tonic-gate 	struct blk *dd, *dr, *r;
936*7c478bd9Sstevel@tonic-gate 	int c;
937*7c478bd9Sstevel@tonic-gate 
938*7c478bd9Sstevel@tonic-gate 	dr = pop();
939*7c478bd9Sstevel@tonic-gate 	EMPTYS;
940*7c478bd9Sstevel@tonic-gate 	dd = pop();
941*7c478bd9Sstevel@tonic-gate 	EMPTYSR(dr);
942*7c478bd9Sstevel@tonic-gate 	fsfile(dd);
943*7c478bd9Sstevel@tonic-gate 	skd = sunputc(dd);
944*7c478bd9Sstevel@tonic-gate 	fsfile(dr);
945*7c478bd9Sstevel@tonic-gate 	skr = sunputc(dr);
946*7c478bd9Sstevel@tonic-gate 	if (sfbeg(dr) == 1 || (sfbeg(dr) == 0 && sbackc(dr) == 0)) {
947*7c478bd9Sstevel@tonic-gate 		sputc(dr, skr);
948*7c478bd9Sstevel@tonic-gate 		pushp(dr);
949*7c478bd9Sstevel@tonic-gate 		printf(gettext("divide by 0\n"));
950*7c478bd9Sstevel@tonic-gate 		return (1);
951*7c478bd9Sstevel@tonic-gate 	}
952*7c478bd9Sstevel@tonic-gate 	if (sfbeg(dd) == 1 || (sfbeg(dd) == 0 && sbackc(dd) == 0)) {
953*7c478bd9Sstevel@tonic-gate #ifdef XPG6
954*7c478bd9Sstevel@tonic-gate 		sputc(dd, k);
955*7c478bd9Sstevel@tonic-gate #else
956*7c478bd9Sstevel@tonic-gate 		sputc(dd, skd);
957*7c478bd9Sstevel@tonic-gate #endif
958*7c478bd9Sstevel@tonic-gate 		pushp(dd);
959*7c478bd9Sstevel@tonic-gate 		return (1);
960*7c478bd9Sstevel@tonic-gate 	}
961*7c478bd9Sstevel@tonic-gate 	c = k-skd+skr;
962*7c478bd9Sstevel@tonic-gate 	if (c < 0)
963*7c478bd9Sstevel@tonic-gate 		r = removr(dd, -c);
964*7c478bd9Sstevel@tonic-gate 	else {
965*7c478bd9Sstevel@tonic-gate 		r = add0(dd, c);
966*7c478bd9Sstevel@tonic-gate 		irem = 0;
967*7c478bd9Sstevel@tonic-gate 	}
968*7c478bd9Sstevel@tonic-gate 	arg1 = r;
969*7c478bd9Sstevel@tonic-gate 	arg2 = dr;
970*7c478bd9Sstevel@tonic-gate 	savk = k;
971*7c478bd9Sstevel@tonic-gate 	return (0);
972*7c478bd9Sstevel@tonic-gate }
973*7c478bd9Sstevel@tonic-gate 
974*7c478bd9Sstevel@tonic-gate struct blk *
975*7c478bd9Sstevel@tonic-gate removr(struct blk *p, int n)
976*7c478bd9Sstevel@tonic-gate {
977*7c478bd9Sstevel@tonic-gate 	int nn, neg;
978*7c478bd9Sstevel@tonic-gate 	struct blk *q, *s, *r;
979*7c478bd9Sstevel@tonic-gate 	fsfile(p);
980*7c478bd9Sstevel@tonic-gate 	neg = sbackc(p);
981*7c478bd9Sstevel@tonic-gate 	if (neg < 0)
982*7c478bd9Sstevel@tonic-gate 		chsign(p);
983*7c478bd9Sstevel@tonic-gate 	rewind(p);
984*7c478bd9Sstevel@tonic-gate 	nn = (n + 1) / 2;
985*7c478bd9Sstevel@tonic-gate 	q = salloc(nn);
986*7c478bd9Sstevel@tonic-gate 	while (n > 1) {
987*7c478bd9Sstevel@tonic-gate 		sputc(q, sgetc(p));
988*7c478bd9Sstevel@tonic-gate 		n -= 2;
989*7c478bd9Sstevel@tonic-gate 	}
990*7c478bd9Sstevel@tonic-gate 	r = salloc(2);
991*7c478bd9Sstevel@tonic-gate 	while (sfeof(p) == 0)
992*7c478bd9Sstevel@tonic-gate 		sputc(r, sgetc(p));
993*7c478bd9Sstevel@tonic-gate 	release(p);
994*7c478bd9Sstevel@tonic-gate 	if (n == 1) {
995*7c478bd9Sstevel@tonic-gate 		s = div(r, tenptr);
996*7c478bd9Sstevel@tonic-gate 		release(r);
997*7c478bd9Sstevel@tonic-gate 		rewind(rem);
998*7c478bd9Sstevel@tonic-gate 		if (sfeof(rem) == 0)
999*7c478bd9Sstevel@tonic-gate 			sputc(q, sgetc(rem));
1000*7c478bd9Sstevel@tonic-gate 		release(rem);
1001*7c478bd9Sstevel@tonic-gate 		if (neg < 0) {
1002*7c478bd9Sstevel@tonic-gate 			chsign(s);
1003*7c478bd9Sstevel@tonic-gate 			chsign(q);
1004*7c478bd9Sstevel@tonic-gate 			irem = q;
1005*7c478bd9Sstevel@tonic-gate 			return (s);
1006*7c478bd9Sstevel@tonic-gate 		}
1007*7c478bd9Sstevel@tonic-gate 		irem = q;
1008*7c478bd9Sstevel@tonic-gate 		return (s);
1009*7c478bd9Sstevel@tonic-gate 	}
1010*7c478bd9Sstevel@tonic-gate 	if (neg < 0) {
1011*7c478bd9Sstevel@tonic-gate 		chsign(r);
1012*7c478bd9Sstevel@tonic-gate 		chsign(q);
1013*7c478bd9Sstevel@tonic-gate 		irem = q;
1014*7c478bd9Sstevel@tonic-gate 		return (r);
1015*7c478bd9Sstevel@tonic-gate 	}
1016*7c478bd9Sstevel@tonic-gate 	irem = q;
1017*7c478bd9Sstevel@tonic-gate 	return (r);
1018*7c478bd9Sstevel@tonic-gate }
1019*7c478bd9Sstevel@tonic-gate 
1020*7c478bd9Sstevel@tonic-gate struct blk *
1021*7c478bd9Sstevel@tonic-gate sqrt(struct blk *p)
1022*7c478bd9Sstevel@tonic-gate {
1023*7c478bd9Sstevel@tonic-gate 	struct blk *r, *q, *s, *t;
1024*7c478bd9Sstevel@tonic-gate 	int c, n, nn;
1025*7c478bd9Sstevel@tonic-gate 
1026*7c478bd9Sstevel@tonic-gate 	n = length(p);
1027*7c478bd9Sstevel@tonic-gate 	fsfile(p);
1028*7c478bd9Sstevel@tonic-gate 	c = sbackc(p);
1029*7c478bd9Sstevel@tonic-gate 	if ((n & 1) != 1)
1030*7c478bd9Sstevel@tonic-gate 		c = c * 100 + (sfbeg(p) ? 0 : sbackc(p));
1031*7c478bd9Sstevel@tonic-gate 	n = (n + 1) >> 1;
1032*7c478bd9Sstevel@tonic-gate 	r = salloc(n);
1033*7c478bd9Sstevel@tonic-gate 	zero(r);
1034*7c478bd9Sstevel@tonic-gate 	seekc(r, n);
1035*7c478bd9Sstevel@tonic-gate 	nn = 1;
1036*7c478bd9Sstevel@tonic-gate 	while ((c -= nn) >= 0)
1037*7c478bd9Sstevel@tonic-gate 		nn += 2;
1038*7c478bd9Sstevel@tonic-gate 	c = (nn + 1) >> 1;
1039*7c478bd9Sstevel@tonic-gate 	fsfile(r);
1040*7c478bd9Sstevel@tonic-gate 	sbackc(r);
1041*7c478bd9Sstevel@tonic-gate 	if (c >= 100) {
1042*7c478bd9Sstevel@tonic-gate 		c -= 100;
1043*7c478bd9Sstevel@tonic-gate 		salterc(r, c);
1044*7c478bd9Sstevel@tonic-gate 		sputc(r, 1);
1045*7c478bd9Sstevel@tonic-gate 	} else
1046*7c478bd9Sstevel@tonic-gate 		salterc(r, c);
1047*7c478bd9Sstevel@tonic-gate 	for (; ; ) {
1048*7c478bd9Sstevel@tonic-gate 		q = div(p, r);
1049*7c478bd9Sstevel@tonic-gate 		s = add(q, r);
1050*7c478bd9Sstevel@tonic-gate 		release(q);
1051*7c478bd9Sstevel@tonic-gate 		release(rem);
1052*7c478bd9Sstevel@tonic-gate 		q = div(s, sqtemp);
1053*7c478bd9Sstevel@tonic-gate 		release(s);
1054*7c478bd9Sstevel@tonic-gate 		release(rem);
1055*7c478bd9Sstevel@tonic-gate 		s = copy(r, length(r));
1056*7c478bd9Sstevel@tonic-gate 		chsign(s);
1057*7c478bd9Sstevel@tonic-gate 		t = add(s, q);
1058*7c478bd9Sstevel@tonic-gate 		release(s);
1059*7c478bd9Sstevel@tonic-gate 		fsfile(t);
1060*7c478bd9Sstevel@tonic-gate 		nn = sfbeg(t) ? 0 : sbackc(t);
1061*7c478bd9Sstevel@tonic-gate 		if (nn >= 0)
1062*7c478bd9Sstevel@tonic-gate 			break;
1063*7c478bd9Sstevel@tonic-gate 		release(r);
1064*7c478bd9Sstevel@tonic-gate 		release(t);
1065*7c478bd9Sstevel@tonic-gate 		r = q;
1066*7c478bd9Sstevel@tonic-gate 	}
1067*7c478bd9Sstevel@tonic-gate 	release(t);
1068*7c478bd9Sstevel@tonic-gate 	release(q);
1069*7c478bd9Sstevel@tonic-gate 	release(p);
1070*7c478bd9Sstevel@tonic-gate 	return (r);
1071*7c478bd9Sstevel@tonic-gate }
1072*7c478bd9Sstevel@tonic-gate 
1073*7c478bd9Sstevel@tonic-gate struct blk *
1074*7c478bd9Sstevel@tonic-gate exp(struct blk *base, struct blk *ex)
1075*7c478bd9Sstevel@tonic-gate {
1076*7c478bd9Sstevel@tonic-gate 	struct blk *r, *e, *p, *e1, *t, *cp;
1077*7c478bd9Sstevel@tonic-gate 	int temp, c, n;
1078*7c478bd9Sstevel@tonic-gate 	r = salloc(1);
1079*7c478bd9Sstevel@tonic-gate 	sputc(r, 1);
1080*7c478bd9Sstevel@tonic-gate 	p = copy(base, length(base));
1081*7c478bd9Sstevel@tonic-gate 	e = copy(ex, length(ex));
1082*7c478bd9Sstevel@tonic-gate 	fsfile(e);
1083*7c478bd9Sstevel@tonic-gate 	if (sfbeg(e) != 0)
1084*7c478bd9Sstevel@tonic-gate 		goto edone;
1085*7c478bd9Sstevel@tonic-gate 	temp = 0;
1086*7c478bd9Sstevel@tonic-gate 	c = sbackc(e);
1087*7c478bd9Sstevel@tonic-gate 	if (c < 0) {
1088*7c478bd9Sstevel@tonic-gate 		temp++;
1089*7c478bd9Sstevel@tonic-gate 		chsign(e);
1090*7c478bd9Sstevel@tonic-gate 	}
1091*7c478bd9Sstevel@tonic-gate 	while (length(e) != 0) {
1092*7c478bd9Sstevel@tonic-gate 		e1 = div(e, sqtemp);
1093*7c478bd9Sstevel@tonic-gate 		release(e);
1094*7c478bd9Sstevel@tonic-gate 		e = e1;
1095*7c478bd9Sstevel@tonic-gate 		n = length(rem);
1096*7c478bd9Sstevel@tonic-gate 		release(rem);
1097*7c478bd9Sstevel@tonic-gate 		if (n != 0) {
1098*7c478bd9Sstevel@tonic-gate 			e1 = mult(p, r);
1099*7c478bd9Sstevel@tonic-gate 			release(r);
1100*7c478bd9Sstevel@tonic-gate 			r = e1;
1101*7c478bd9Sstevel@tonic-gate 		}
1102*7c478bd9Sstevel@tonic-gate 		t = copy(p, length(p));
1103*7c478bd9Sstevel@tonic-gate 		cp = mult(p, t);
1104*7c478bd9Sstevel@tonic-gate 		release(p);
1105*7c478bd9Sstevel@tonic-gate 		release(t);
1106*7c478bd9Sstevel@tonic-gate 		p = cp;
1107*7c478bd9Sstevel@tonic-gate 	}
1108*7c478bd9Sstevel@tonic-gate 	if (temp != 0) {
1109*7c478bd9Sstevel@tonic-gate 		if ((c = length(base)) == 0) {
1110*7c478bd9Sstevel@tonic-gate 			goto edone;
1111*7c478bd9Sstevel@tonic-gate 		}
1112*7c478bd9Sstevel@tonic-gate 		if (c > 1)
1113*7c478bd9Sstevel@tonic-gate 			create(r);
1114*7c478bd9Sstevel@tonic-gate 		else {
1115*7c478bd9Sstevel@tonic-gate 			rewind(base);
1116*7c478bd9Sstevel@tonic-gate 			if ((c = sgetc(base)) <= 1) {
1117*7c478bd9Sstevel@tonic-gate 				create(r);
1118*7c478bd9Sstevel@tonic-gate 				sputc(r, c);
1119*7c478bd9Sstevel@tonic-gate 			} else
1120*7c478bd9Sstevel@tonic-gate 				create(r);
1121*7c478bd9Sstevel@tonic-gate 		}
1122*7c478bd9Sstevel@tonic-gate 	}
1123*7c478bd9Sstevel@tonic-gate edone:
1124*7c478bd9Sstevel@tonic-gate 	release(p);
1125*7c478bd9Sstevel@tonic-gate 	release(e);
1126*7c478bd9Sstevel@tonic-gate 	return (r);
1127*7c478bd9Sstevel@tonic-gate }
1128*7c478bd9Sstevel@tonic-gate 
1129*7c478bd9Sstevel@tonic-gate void
1130*7c478bd9Sstevel@tonic-gate init(int argc, char **argv)
1131*7c478bd9Sstevel@tonic-gate {
1132*7c478bd9Sstevel@tonic-gate 	struct sym *sp;
1133*7c478bd9Sstevel@tonic-gate 	char *dcmalloc();
1134*7c478bd9Sstevel@tonic-gate 	struct stat tsb;
1135*7c478bd9Sstevel@tonic-gate 
1136*7c478bd9Sstevel@tonic-gate 	if (signal(SIGINT, SIG_IGN) != SIG_IGN)
1137*7c478bd9Sstevel@tonic-gate 		signal(SIGINT, onintr);
1138*7c478bd9Sstevel@tonic-gate 	setbuf(stdout, (char *)NULL);
1139*7c478bd9Sstevel@tonic-gate 	svargc = --argc;
1140*7c478bd9Sstevel@tonic-gate 	svargv = argv;
1141*7c478bd9Sstevel@tonic-gate 	while (svargc > 0 && svargv[1][0] == '-') {
1142*7c478bd9Sstevel@tonic-gate 		switch (svargv[1][1]) {
1143*7c478bd9Sstevel@tonic-gate 		default:
1144*7c478bd9Sstevel@tonic-gate 			dbg = 1;
1145*7c478bd9Sstevel@tonic-gate 		}
1146*7c478bd9Sstevel@tonic-gate 		svargc--;
1147*7c478bd9Sstevel@tonic-gate 		svargv++;
1148*7c478bd9Sstevel@tonic-gate 	}
1149*7c478bd9Sstevel@tonic-gate 
1150*7c478bd9Sstevel@tonic-gate 	ifile = 1;
1151*7c478bd9Sstevel@tonic-gate 
1152*7c478bd9Sstevel@tonic-gate 	if (svargc <= 0)
1153*7c478bd9Sstevel@tonic-gate 		curfile = stdin;
1154*7c478bd9Sstevel@tonic-gate 	else {
1155*7c478bd9Sstevel@tonic-gate 		if (stat(svargv[1], &tsb) < 0) {
1156*7c478bd9Sstevel@tonic-gate 			printf(gettext("Cannot stat %s: "), svargv[1]);
1157*7c478bd9Sstevel@tonic-gate 			perror("");
1158*7c478bd9Sstevel@tonic-gate 			exit(1);
1159*7c478bd9Sstevel@tonic-gate 		}
1160*7c478bd9Sstevel@tonic-gate 
1161*7c478bd9Sstevel@tonic-gate 		if (S_ISREG(tsb.st_mode)) {
1162*7c478bd9Sstevel@tonic-gate 			if ((curfile = fopen(svargv[1], "r")) == NULL) {
1163*7c478bd9Sstevel@tonic-gate 				printf(gettext("can't open file %s\n"), \
1164*7c478bd9Sstevel@tonic-gate 				    svargv[1]);
1165*7c478bd9Sstevel@tonic-gate 				exit(1);
1166*7c478bd9Sstevel@tonic-gate 			}
1167*7c478bd9Sstevel@tonic-gate 		} else {
1168*7c478bd9Sstevel@tonic-gate 			printf(gettext("invalid file type: %s\n"), \
1169*7c478bd9Sstevel@tonic-gate 			    svargv[1]);
1170*7c478bd9Sstevel@tonic-gate 			exit(1);
1171*7c478bd9Sstevel@tonic-gate 		}
1172*7c478bd9Sstevel@tonic-gate 	}
1173*7c478bd9Sstevel@tonic-gate 
1174*7c478bd9Sstevel@tonic-gate 	dummy = dcmalloc(0);
1175*7c478bd9Sstevel@tonic-gate 	scalptr = salloc(1);
1176*7c478bd9Sstevel@tonic-gate 	sputc(scalptr, 0);
1177*7c478bd9Sstevel@tonic-gate 	basptr = salloc(1);
1178*7c478bd9Sstevel@tonic-gate 	sputc(basptr, 10);
1179*7c478bd9Sstevel@tonic-gate 	obase = 10;
1180*7c478bd9Sstevel@tonic-gate 	log10 = log2(10L);
1181*7c478bd9Sstevel@tonic-gate 
1182*7c478bd9Sstevel@tonic-gate 	/*
1183*7c478bd9Sstevel@tonic-gate 	 * POSIX.2
1184*7c478bd9Sstevel@tonic-gate 	 * default line length is 70 characters including newline
1185*7c478bd9Sstevel@tonic-gate 	 */
1186*7c478bd9Sstevel@tonic-gate 	ll = 70;
1187*7c478bd9Sstevel@tonic-gate 	fw = 1;
1188*7c478bd9Sstevel@tonic-gate 	fw1 = 0;
1189*7c478bd9Sstevel@tonic-gate 	tenptr = salloc(1);
1190*7c478bd9Sstevel@tonic-gate 	sputc(tenptr, 10);
1191*7c478bd9Sstevel@tonic-gate 	obase = 10;
1192*7c478bd9Sstevel@tonic-gate 	inbas = salloc(1);
1193*7c478bd9Sstevel@tonic-gate 	sputc(inbas, 10);
1194*7c478bd9Sstevel@tonic-gate 	sqtemp = salloc(1);
1195*7c478bd9Sstevel@tonic-gate 	sputc(sqtemp, 2);
1196*7c478bd9Sstevel@tonic-gate 	chptr = salloc(0);
1197*7c478bd9Sstevel@tonic-gate 	strptr = salloc(0);
1198*7c478bd9Sstevel@tonic-gate 	divxyz = salloc(0);
1199*7c478bd9Sstevel@tonic-gate 	stkbeg = stkptr = &stack[0];
1200*7c478bd9Sstevel@tonic-gate 	stkend = &stack[STKSZ];
1201*7c478bd9Sstevel@tonic-gate 	stkerr = 0;
1202*7c478bd9Sstevel@tonic-gate 	readptr = &readstk[0];
1203*7c478bd9Sstevel@tonic-gate 	k = 0;
1204*7c478bd9Sstevel@tonic-gate 	sp = sptr = &symlst[0];
1205*7c478bd9Sstevel@tonic-gate 	while (sptr < &symlst[TBLSZ]) {
1206*7c478bd9Sstevel@tonic-gate 		sptr->next = ++sp;
1207*7c478bd9Sstevel@tonic-gate 		sptr++;
1208*7c478bd9Sstevel@tonic-gate 	}
1209*7c478bd9Sstevel@tonic-gate 	sptr->next = 0;
1210*7c478bd9Sstevel@tonic-gate 	sfree = &symlst[0];
1211*7c478bd9Sstevel@tonic-gate }
1212*7c478bd9Sstevel@tonic-gate 
1213*7c478bd9Sstevel@tonic-gate void
1214*7c478bd9Sstevel@tonic-gate onintr(int sig)
1215*7c478bd9Sstevel@tonic-gate {
1216*7c478bd9Sstevel@tonic-gate 
1217*7c478bd9Sstevel@tonic-gate 	signal(sig, onintr);
1218*7c478bd9Sstevel@tonic-gate 	while (readptr != &readstk[0]) {
1219*7c478bd9Sstevel@tonic-gate 		if (*readptr != 0)
1220*7c478bd9Sstevel@tonic-gate 			release(*readptr);
1221*7c478bd9Sstevel@tonic-gate 		readptr--;
1222*7c478bd9Sstevel@tonic-gate 	}
1223*7c478bd9Sstevel@tonic-gate 	curfile = stdin;
1224*7c478bd9Sstevel@tonic-gate 	commnds();
1225*7c478bd9Sstevel@tonic-gate }
1226*7c478bd9Sstevel@tonic-gate 
1227*7c478bd9Sstevel@tonic-gate void
1228*7c478bd9Sstevel@tonic-gate pushp(struct blk *p)
1229*7c478bd9Sstevel@tonic-gate {
1230*7c478bd9Sstevel@tonic-gate 	if (stkptr == stkend)
1231*7c478bd9Sstevel@tonic-gate 		printf(gettext("out of stack space\n"));
1232*7c478bd9Sstevel@tonic-gate 	else {
1233*7c478bd9Sstevel@tonic-gate 		stkerr = 0;
1234*7c478bd9Sstevel@tonic-gate 		*++stkptr = p;
1235*7c478bd9Sstevel@tonic-gate 	}
1236*7c478bd9Sstevel@tonic-gate }
1237*7c478bd9Sstevel@tonic-gate 
1238*7c478bd9Sstevel@tonic-gate struct blk *
1239*7c478bd9Sstevel@tonic-gate pop() {
1240*7c478bd9Sstevel@tonic-gate 	if (stkptr == stack) {
1241*7c478bd9Sstevel@tonic-gate 		stkerr = 1;
1242*7c478bd9Sstevel@tonic-gate 		return (0);
1243*7c478bd9Sstevel@tonic-gate 	}
1244*7c478bd9Sstevel@tonic-gate 	return (*stkptr--);
1245*7c478bd9Sstevel@tonic-gate }
1246*7c478bd9Sstevel@tonic-gate 
1247*7c478bd9Sstevel@tonic-gate struct blk *
1248*7c478bd9Sstevel@tonic-gate readin() {
1249*7c478bd9Sstevel@tonic-gate 	struct blk *p, *q;
1250*7c478bd9Sstevel@tonic-gate 	int dp, dpct;
1251*7c478bd9Sstevel@tonic-gate 	int c;
1252*7c478bd9Sstevel@tonic-gate 
1253*7c478bd9Sstevel@tonic-gate 	dp = dpct = 0;
1254*7c478bd9Sstevel@tonic-gate 	p = salloc(0);
1255*7c478bd9Sstevel@tonic-gate 	for (; ; ) {
1256*7c478bd9Sstevel@tonic-gate 		c = readc();
1257*7c478bd9Sstevel@tonic-gate 		switch (c) {
1258*7c478bd9Sstevel@tonic-gate 		case '.':
1259*7c478bd9Sstevel@tonic-gate 			if (dp != 0)
1260*7c478bd9Sstevel@tonic-gate 				goto gotnum;
1261*7c478bd9Sstevel@tonic-gate 			dp++;
1262*7c478bd9Sstevel@tonic-gate 			continue;
1263*7c478bd9Sstevel@tonic-gate 		case '\\':
1264*7c478bd9Sstevel@tonic-gate 			readc();
1265*7c478bd9Sstevel@tonic-gate 			continue;
1266*7c478bd9Sstevel@tonic-gate 		default:
1267*7c478bd9Sstevel@tonic-gate 			if (c >= 'A' && c <= 'F')
1268*7c478bd9Sstevel@tonic-gate 				c = c - 'A' + 10;
1269*7c478bd9Sstevel@tonic-gate 			else
1270*7c478bd9Sstevel@tonic-gate 				if (c >= '0' && c <= '9')
1271*7c478bd9Sstevel@tonic-gate 					c -= '0';
1272*7c478bd9Sstevel@tonic-gate 				else
1273*7c478bd9Sstevel@tonic-gate 					goto gotnum;
1274*7c478bd9Sstevel@tonic-gate 			if (dp != 0) {
1275*7c478bd9Sstevel@tonic-gate 				if (dpct >= 99)
1276*7c478bd9Sstevel@tonic-gate 					continue;
1277*7c478bd9Sstevel@tonic-gate 				dpct++;
1278*7c478bd9Sstevel@tonic-gate 			}
1279*7c478bd9Sstevel@tonic-gate 			create(chptr);
1280*7c478bd9Sstevel@tonic-gate 			if (c != 0)
1281*7c478bd9Sstevel@tonic-gate 				sputc(chptr, c);
1282*7c478bd9Sstevel@tonic-gate 			q = mult(p, inbas);
1283*7c478bd9Sstevel@tonic-gate 			release(p);
1284*7c478bd9Sstevel@tonic-gate 			p = add(chptr, q);
1285*7c478bd9Sstevel@tonic-gate 			release(q);
1286*7c478bd9Sstevel@tonic-gate 		}
1287*7c478bd9Sstevel@tonic-gate 	}
1288*7c478bd9Sstevel@tonic-gate gotnum:
1289*7c478bd9Sstevel@tonic-gate 	unreadc(c);
1290*7c478bd9Sstevel@tonic-gate 	if (dp == 0) {
1291*7c478bd9Sstevel@tonic-gate 		sputc(p, 0);
1292*7c478bd9Sstevel@tonic-gate 		return (p);
1293*7c478bd9Sstevel@tonic-gate 	} else {
1294*7c478bd9Sstevel@tonic-gate 		/* if not base 10, then scale fractional input to precision */
1295*7c478bd9Sstevel@tonic-gate 		if (((int)*(inbas->beg)) != 10) {
1296*7c478bd9Sstevel@tonic-gate 			while (dpct < k) {
1297*7c478bd9Sstevel@tonic-gate 				create(chptr);
1298*7c478bd9Sstevel@tonic-gate 				q = mult(p, inbas);
1299*7c478bd9Sstevel@tonic-gate 				release(p);
1300*7c478bd9Sstevel@tonic-gate 				p = add(chptr, q);
1301*7c478bd9Sstevel@tonic-gate 				release(q);
1302*7c478bd9Sstevel@tonic-gate 				dpct++;
1303*7c478bd9Sstevel@tonic-gate 			}
1304*7c478bd9Sstevel@tonic-gate 		}
1305*7c478bd9Sstevel@tonic-gate 		q = scale(p, dpct);
1306*7c478bd9Sstevel@tonic-gate 		return (q);
1307*7c478bd9Sstevel@tonic-gate 	}
1308*7c478bd9Sstevel@tonic-gate }
1309*7c478bd9Sstevel@tonic-gate 
1310*7c478bd9Sstevel@tonic-gate /*
1311*7c478bd9Sstevel@tonic-gate  * returns pointer to struct with ct 0's & p
1312*7c478bd9Sstevel@tonic-gate  */
1313*7c478bd9Sstevel@tonic-gate struct blk *
1314*7c478bd9Sstevel@tonic-gate add0(struct blk *p, int ct)
1315*7c478bd9Sstevel@tonic-gate {
1316*7c478bd9Sstevel@tonic-gate 	struct blk *q, *t;
1317*7c478bd9Sstevel@tonic-gate 
1318*7c478bd9Sstevel@tonic-gate 	q = salloc(length(p) + (ct + 1) / 2);
1319*7c478bd9Sstevel@tonic-gate 	while (ct > 1) {
1320*7c478bd9Sstevel@tonic-gate 		sputc(q, 0);
1321*7c478bd9Sstevel@tonic-gate 		ct -= 2;
1322*7c478bd9Sstevel@tonic-gate 	}
1323*7c478bd9Sstevel@tonic-gate 	rewind(p);
1324*7c478bd9Sstevel@tonic-gate 	while (sfeof(p) == 0) {
1325*7c478bd9Sstevel@tonic-gate 		sputc(q, sgetc(p));
1326*7c478bd9Sstevel@tonic-gate 	}
1327*7c478bd9Sstevel@tonic-gate 	release(p);
1328*7c478bd9Sstevel@tonic-gate 	if (ct == 1) {
1329*7c478bd9Sstevel@tonic-gate 		t = mult(tenptr, q);
1330*7c478bd9Sstevel@tonic-gate 		release(q);
1331*7c478bd9Sstevel@tonic-gate 		return (t);
1332*7c478bd9Sstevel@tonic-gate 	}
1333*7c478bd9Sstevel@tonic-gate 	return (q);
1334*7c478bd9Sstevel@tonic-gate }
1335*7c478bd9Sstevel@tonic-gate 
1336*7c478bd9Sstevel@tonic-gate struct blk *
1337*7c478bd9Sstevel@tonic-gate mult(struct blk *p, struct blk *q)
1338*7c478bd9Sstevel@tonic-gate {
1339*7c478bd9Sstevel@tonic-gate 	struct blk *mp, *mq, *mr;
1340*7c478bd9Sstevel@tonic-gate 	int sign, offset, carry;
1341*7c478bd9Sstevel@tonic-gate 	int cq, cp, mt, mcr;
1342*7c478bd9Sstevel@tonic-gate 
1343*7c478bd9Sstevel@tonic-gate 	offset = sign = 0;
1344*7c478bd9Sstevel@tonic-gate 	fsfile(p);
1345*7c478bd9Sstevel@tonic-gate 	mp = p;
1346*7c478bd9Sstevel@tonic-gate 	if (sfbeg(p) == 0) {
1347*7c478bd9Sstevel@tonic-gate 		if (sbackc(p) < 0) {
1348*7c478bd9Sstevel@tonic-gate 			mp = copy(p, length(p));
1349*7c478bd9Sstevel@tonic-gate 			chsign(mp);
1350*7c478bd9Sstevel@tonic-gate 			sign = ~sign;
1351*7c478bd9Sstevel@tonic-gate 		}
1352*7c478bd9Sstevel@tonic-gate 	}
1353*7c478bd9Sstevel@tonic-gate 	fsfile(q);
1354*7c478bd9Sstevel@tonic-gate 	mq = q;
1355*7c478bd9Sstevel@tonic-gate 	if (sfbeg(q) == 0) {
1356*7c478bd9Sstevel@tonic-gate 		if (sbackc(q) < 0) {
1357*7c478bd9Sstevel@tonic-gate 			mq = copy(q, length(q));
1358*7c478bd9Sstevel@tonic-gate 			chsign(mq);
1359*7c478bd9Sstevel@tonic-gate 			sign = ~sign;
1360*7c478bd9Sstevel@tonic-gate 		}
1361*7c478bd9Sstevel@tonic-gate 	}
1362*7c478bd9Sstevel@tonic-gate 	mr = salloc(length(mp) + length(mq));
1363*7c478bd9Sstevel@tonic-gate 	zero(mr);
1364*7c478bd9Sstevel@tonic-gate 	rewind(mq);
1365*7c478bd9Sstevel@tonic-gate 	while (sfeof(mq) == 0) {
1366*7c478bd9Sstevel@tonic-gate 		cq = sgetc(mq);
1367*7c478bd9Sstevel@tonic-gate 		rewind(mp);
1368*7c478bd9Sstevel@tonic-gate 		rewind(mr);
1369*7c478bd9Sstevel@tonic-gate 		mr->rd += offset;
1370*7c478bd9Sstevel@tonic-gate 		carry = 0;
1371*7c478bd9Sstevel@tonic-gate 		while (sfeof(mp) == 0) {
1372*7c478bd9Sstevel@tonic-gate 			cp = sgetc(mp);
1373*7c478bd9Sstevel@tonic-gate 			mcr = sfeof(mr) ? 0 : slookc(mr);
1374*7c478bd9Sstevel@tonic-gate 			mt = cp*cq + carry + mcr;
1375*7c478bd9Sstevel@tonic-gate 			carry = mt / 100;
1376*7c478bd9Sstevel@tonic-gate 			salterc(mr, mt % 100);
1377*7c478bd9Sstevel@tonic-gate 		}
1378*7c478bd9Sstevel@tonic-gate 		offset++;
1379*7c478bd9Sstevel@tonic-gate 		if (carry != 0) {
1380*7c478bd9Sstevel@tonic-gate 			mcr = sfeof(mr) ? 0 : slookc(mr);
1381*7c478bd9Sstevel@tonic-gate 			salterc(mr, mcr + carry);
1382*7c478bd9Sstevel@tonic-gate 		}
1383*7c478bd9Sstevel@tonic-gate 	}
1384*7c478bd9Sstevel@tonic-gate 	if (sign < 0) {
1385*7c478bd9Sstevel@tonic-gate 		chsign(mr);
1386*7c478bd9Sstevel@tonic-gate 	}
1387*7c478bd9Sstevel@tonic-gate 	if (mp != p)
1388*7c478bd9Sstevel@tonic-gate 		release(mp);
1389*7c478bd9Sstevel@tonic-gate 	if (mq != q)
1390*7c478bd9Sstevel@tonic-gate 		release(mq);
1391*7c478bd9Sstevel@tonic-gate 	return (mr);
1392*7c478bd9Sstevel@tonic-gate }
1393*7c478bd9Sstevel@tonic-gate 
1394*7c478bd9Sstevel@tonic-gate void
1395*7c478bd9Sstevel@tonic-gate chsign(struct blk *p)
1396*7c478bd9Sstevel@tonic-gate {
1397*7c478bd9Sstevel@tonic-gate 	int carry;
1398*7c478bd9Sstevel@tonic-gate 	char ct;
1399*7c478bd9Sstevel@tonic-gate 
1400*7c478bd9Sstevel@tonic-gate 	carry = 0;
1401*7c478bd9Sstevel@tonic-gate 	rewind(p);
1402*7c478bd9Sstevel@tonic-gate 	while (sfeof(p) == 0) {
1403*7c478bd9Sstevel@tonic-gate 		ct = 100 - slookc(p) - carry;
1404*7c478bd9Sstevel@tonic-gate 		carry = 1;
1405*7c478bd9Sstevel@tonic-gate 		if (ct >= 100) {
1406*7c478bd9Sstevel@tonic-gate 			ct -= 100;
1407*7c478bd9Sstevel@tonic-gate 			carry = 0;
1408*7c478bd9Sstevel@tonic-gate 		}
1409*7c478bd9Sstevel@tonic-gate 		salterc(p, ct);
1410*7c478bd9Sstevel@tonic-gate 	}
1411*7c478bd9Sstevel@tonic-gate 	if (carry != 0) {
1412*7c478bd9Sstevel@tonic-gate 		sputc(p, -1);
1413*7c478bd9Sstevel@tonic-gate 		fsfile(p);
1414*7c478bd9Sstevel@tonic-gate 		sbackc(p);
1415*7c478bd9Sstevel@tonic-gate 		ct = sbackc(p);
1416*7c478bd9Sstevel@tonic-gate 		if (ct == 99) {
1417*7c478bd9Sstevel@tonic-gate 			truncate(p);
1418*7c478bd9Sstevel@tonic-gate 			sputc(p, -1);
1419*7c478bd9Sstevel@tonic-gate 		}
1420*7c478bd9Sstevel@tonic-gate 	} else {
1421*7c478bd9Sstevel@tonic-gate 		fsfile(p);
1422*7c478bd9Sstevel@tonic-gate 		ct = sbackc(p);
1423*7c478bd9Sstevel@tonic-gate 		if (ct == 0)
1424*7c478bd9Sstevel@tonic-gate 			truncate(p);
1425*7c478bd9Sstevel@tonic-gate 	}
1426*7c478bd9Sstevel@tonic-gate }
1427*7c478bd9Sstevel@tonic-gate 
1428*7c478bd9Sstevel@tonic-gate char
1429*7c478bd9Sstevel@tonic-gate readc() {
1430*7c478bd9Sstevel@tonic-gate loop:
1431*7c478bd9Sstevel@tonic-gate 	if ((readptr != &readstk[0]) && (*readptr != 0)) {
1432*7c478bd9Sstevel@tonic-gate 		if (sfeof(*readptr) == 0)
1433*7c478bd9Sstevel@tonic-gate 			return (lastchar = sgetc(*readptr));
1434*7c478bd9Sstevel@tonic-gate 		release(*readptr);
1435*7c478bd9Sstevel@tonic-gate 		readptr--;
1436*7c478bd9Sstevel@tonic-gate 		goto loop;
1437*7c478bd9Sstevel@tonic-gate 	}
1438*7c478bd9Sstevel@tonic-gate 	lastchar = getc(curfile);
1439*7c478bd9Sstevel@tonic-gate 	if (lastchar != EOF)
1440*7c478bd9Sstevel@tonic-gate 		return (lastchar);
1441*7c478bd9Sstevel@tonic-gate 	if (readptr != &readptr[0]) {
1442*7c478bd9Sstevel@tonic-gate 		readptr--;
1443*7c478bd9Sstevel@tonic-gate 		if (*readptr == 0)
1444*7c478bd9Sstevel@tonic-gate 			curfile = stdin;
1445*7c478bd9Sstevel@tonic-gate 		goto loop;
1446*7c478bd9Sstevel@tonic-gate 	}
1447*7c478bd9Sstevel@tonic-gate 	if (curfile != stdin) {
1448*7c478bd9Sstevel@tonic-gate 		fclose(curfile);
1449*7c478bd9Sstevel@tonic-gate 		curfile = stdin;
1450*7c478bd9Sstevel@tonic-gate 		goto loop;
1451*7c478bd9Sstevel@tonic-gate 	}
1452*7c478bd9Sstevel@tonic-gate 	exit(0);
1453*7c478bd9Sstevel@tonic-gate }
1454*7c478bd9Sstevel@tonic-gate 
1455*7c478bd9Sstevel@tonic-gate void
1456*7c478bd9Sstevel@tonic-gate unreadc(char c)
1457*7c478bd9Sstevel@tonic-gate {
1458*7c478bd9Sstevel@tonic-gate 
1459*7c478bd9Sstevel@tonic-gate 	if ((readptr != &readstk[0]) && (*readptr != 0)) {
1460*7c478bd9Sstevel@tonic-gate 		sungetc(*readptr, c);
1461*7c478bd9Sstevel@tonic-gate 	} else
1462*7c478bd9Sstevel@tonic-gate 		ungetc(c, curfile);
1463*7c478bd9Sstevel@tonic-gate }
1464*7c478bd9Sstevel@tonic-gate 
1465*7c478bd9Sstevel@tonic-gate void
1466*7c478bd9Sstevel@tonic-gate binop(char c)
1467*7c478bd9Sstevel@tonic-gate {
1468*7c478bd9Sstevel@tonic-gate 	struct blk *r;
1469*7c478bd9Sstevel@tonic-gate 
1470*7c478bd9Sstevel@tonic-gate 	switch (c) {
1471*7c478bd9Sstevel@tonic-gate 	case '+':
1472*7c478bd9Sstevel@tonic-gate 		r = add(arg1, arg2);
1473*7c478bd9Sstevel@tonic-gate 		break;
1474*7c478bd9Sstevel@tonic-gate 	case '*':
1475*7c478bd9Sstevel@tonic-gate 		r = mult(arg1, arg2);
1476*7c478bd9Sstevel@tonic-gate 		break;
1477*7c478bd9Sstevel@tonic-gate 	case '/':
1478*7c478bd9Sstevel@tonic-gate 		r = div(arg1, arg2);
1479*7c478bd9Sstevel@tonic-gate 		break;
1480*7c478bd9Sstevel@tonic-gate 	}
1481*7c478bd9Sstevel@tonic-gate 	release(arg1);
1482*7c478bd9Sstevel@tonic-gate 	release(arg2);
1483*7c478bd9Sstevel@tonic-gate 	sputc(r, savk);
1484*7c478bd9Sstevel@tonic-gate 	pushp(r);
1485*7c478bd9Sstevel@tonic-gate }
1486*7c478bd9Sstevel@tonic-gate 
1487*7c478bd9Sstevel@tonic-gate void
1488*7c478bd9Sstevel@tonic-gate print(struct blk *hptr)
1489*7c478bd9Sstevel@tonic-gate {
1490*7c478bd9Sstevel@tonic-gate 	struct blk *p, *q, *dec;
1491*7c478bd9Sstevel@tonic-gate 	int sc;				/* scale */
1492*7c478bd9Sstevel@tonic-gate 	int dig, dout, ct;
1493*7c478bd9Sstevel@tonic-gate 
1494*7c478bd9Sstevel@tonic-gate 	rewind(hptr);
1495*7c478bd9Sstevel@tonic-gate 	while (sfeof(hptr) == 0) {
1496*7c478bd9Sstevel@tonic-gate 		if (sgetc(hptr) > 99) {
1497*7c478bd9Sstevel@tonic-gate 			rewind(hptr);
1498*7c478bd9Sstevel@tonic-gate 			while (sfeof(hptr) == 0) {
1499*7c478bd9Sstevel@tonic-gate 				printf("%c", sgetc(hptr));
1500*7c478bd9Sstevel@tonic-gate 			}
1501*7c478bd9Sstevel@tonic-gate 			printf("\n");
1502*7c478bd9Sstevel@tonic-gate 			return;
1503*7c478bd9Sstevel@tonic-gate 		}
1504*7c478bd9Sstevel@tonic-gate 	}
1505*7c478bd9Sstevel@tonic-gate 	fsfile(hptr);
1506*7c478bd9Sstevel@tonic-gate 	sc = sbackc(hptr);		/* read scale off end of blk */
1507*7c478bd9Sstevel@tonic-gate 	if (sfbeg(hptr) != 0) {
1508*7c478bd9Sstevel@tonic-gate 		printf("0\n");
1509*7c478bd9Sstevel@tonic-gate 		return;
1510*7c478bd9Sstevel@tonic-gate 	}
1511*7c478bd9Sstevel@tonic-gate 	count = ll;
1512*7c478bd9Sstevel@tonic-gate 	p = copy(hptr, length(hptr));
1513*7c478bd9Sstevel@tonic-gate 	sunputc(p);
1514*7c478bd9Sstevel@tonic-gate 	fsfile(p);
1515*7c478bd9Sstevel@tonic-gate 	if (sbackc(p) < 0) {
1516*7c478bd9Sstevel@tonic-gate 		chsign(p);
1517*7c478bd9Sstevel@tonic-gate 		OUTC('-');
1518*7c478bd9Sstevel@tonic-gate 	}
1519*7c478bd9Sstevel@tonic-gate 	if ((obase == 0) || (obase == -1)) {
1520*7c478bd9Sstevel@tonic-gate 		oneot(p, sc, 'd');
1521*7c478bd9Sstevel@tonic-gate 		return;
1522*7c478bd9Sstevel@tonic-gate 	}
1523*7c478bd9Sstevel@tonic-gate 	if (obase == 1) {
1524*7c478bd9Sstevel@tonic-gate 		oneot(p, sc, '1');
1525*7c478bd9Sstevel@tonic-gate 		return;
1526*7c478bd9Sstevel@tonic-gate 	}
1527*7c478bd9Sstevel@tonic-gate 	if (obase == 10) {
1528*7c478bd9Sstevel@tonic-gate 		tenot(p, sc);
1529*7c478bd9Sstevel@tonic-gate 		return;
1530*7c478bd9Sstevel@tonic-gate 	}
1531*7c478bd9Sstevel@tonic-gate 	create(strptr);
1532*7c478bd9Sstevel@tonic-gate 	dig = log10 * sc;
1533*7c478bd9Sstevel@tonic-gate 	dout = ((dig / 10) + dig) / logo;
1534*7c478bd9Sstevel@tonic-gate 	dec = getdec(p, sc);
1535*7c478bd9Sstevel@tonic-gate 	p = removc(p, sc);
1536*7c478bd9Sstevel@tonic-gate 	while (length(p) != 0) {
1537*7c478bd9Sstevel@tonic-gate 		q = div(p, basptr);
1538*7c478bd9Sstevel@tonic-gate 		release(p);
1539*7c478bd9Sstevel@tonic-gate 		p = q;
1540*7c478bd9Sstevel@tonic-gate 		(*outdit)(rem, 0);
1541*7c478bd9Sstevel@tonic-gate 		if (obase > 16)
1542*7c478bd9Sstevel@tonic-gate 			sputc(strptr, ' ');
1543*7c478bd9Sstevel@tonic-gate 	}
1544*7c478bd9Sstevel@tonic-gate 	release(p);
1545*7c478bd9Sstevel@tonic-gate 	fsfile(strptr);
1546*7c478bd9Sstevel@tonic-gate 	while (sfbeg(strptr) == 0)
1547*7c478bd9Sstevel@tonic-gate 		OUTC(sbackc(strptr));
1548*7c478bd9Sstevel@tonic-gate 	if (sc == 0) {
1549*7c478bd9Sstevel@tonic-gate 		release(dec);
1550*7c478bd9Sstevel@tonic-gate 		printf("\n");
1551*7c478bd9Sstevel@tonic-gate 		return;
1552*7c478bd9Sstevel@tonic-gate 	}
1553*7c478bd9Sstevel@tonic-gate 	create(strptr);
1554*7c478bd9Sstevel@tonic-gate 	OUTC('.');
1555*7c478bd9Sstevel@tonic-gate 	ct = 0;
1556*7c478bd9Sstevel@tonic-gate 	do {
1557*7c478bd9Sstevel@tonic-gate 		if (ct != 0 && obase > 16)
1558*7c478bd9Sstevel@tonic-gate 			sputc(strptr, ' ');
1559*7c478bd9Sstevel@tonic-gate 		q = mult(basptr, dec);
1560*7c478bd9Sstevel@tonic-gate 		release(dec);
1561*7c478bd9Sstevel@tonic-gate 		dec = getdec(q, sc);
1562*7c478bd9Sstevel@tonic-gate 		p = removc(q, sc);
1563*7c478bd9Sstevel@tonic-gate 		(*outdit)(p, 1);
1564*7c478bd9Sstevel@tonic-gate 	} while (++ct < dout);
1565*7c478bd9Sstevel@tonic-gate 	release(dec);
1566*7c478bd9Sstevel@tonic-gate 	rewind(strptr);
1567*7c478bd9Sstevel@tonic-gate 	while (sfeof(strptr) == 0)
1568*7c478bd9Sstevel@tonic-gate 		OUTC(sgetc(strptr));
1569*7c478bd9Sstevel@tonic-gate 	printf("\n");
1570*7c478bd9Sstevel@tonic-gate }
1571*7c478bd9Sstevel@tonic-gate 
1572*7c478bd9Sstevel@tonic-gate struct blk *
1573*7c478bd9Sstevel@tonic-gate getdec(struct blk *p, int sc)
1574*7c478bd9Sstevel@tonic-gate {
1575*7c478bd9Sstevel@tonic-gate 	int cc;
1576*7c478bd9Sstevel@tonic-gate 	struct blk *q, *t, *s;
1577*7c478bd9Sstevel@tonic-gate 
1578*7c478bd9Sstevel@tonic-gate 	rewind(p);
1579*7c478bd9Sstevel@tonic-gate 	if (length(p) * 2 < sc) {
1580*7c478bd9Sstevel@tonic-gate 		q = copy(p, length(p));
1581*7c478bd9Sstevel@tonic-gate 		return (q);
1582*7c478bd9Sstevel@tonic-gate 	}
1583*7c478bd9Sstevel@tonic-gate 	q = salloc(length(p));
1584*7c478bd9Sstevel@tonic-gate 	while (sc >= 1) {
1585*7c478bd9Sstevel@tonic-gate 		sputc(q, sgetc(p));
1586*7c478bd9Sstevel@tonic-gate 		sc -= 2;
1587*7c478bd9Sstevel@tonic-gate 	}
1588*7c478bd9Sstevel@tonic-gate 	if (sc != 0) {
1589*7c478bd9Sstevel@tonic-gate 		t = mult(q, tenptr);
1590*7c478bd9Sstevel@tonic-gate 		s = salloc(cc = length(q));
1591*7c478bd9Sstevel@tonic-gate 		release(q);
1592*7c478bd9Sstevel@tonic-gate 		rewind(t);
1593*7c478bd9Sstevel@tonic-gate 		while (cc-- > 0)
1594*7c478bd9Sstevel@tonic-gate 			sputc(s, sgetc(t));
1595*7c478bd9Sstevel@tonic-gate 		sputc(s, 0);
1596*7c478bd9Sstevel@tonic-gate 		release(t);
1597*7c478bd9Sstevel@tonic-gate 		t = div(s, tenptr);
1598*7c478bd9Sstevel@tonic-gate 		release(s);
1599*7c478bd9Sstevel@tonic-gate 		release(rem);
1600*7c478bd9Sstevel@tonic-gate 		return (t);
1601*7c478bd9Sstevel@tonic-gate 	}
1602*7c478bd9Sstevel@tonic-gate 	return (q);
1603*7c478bd9Sstevel@tonic-gate }
1604*7c478bd9Sstevel@tonic-gate 
1605*7c478bd9Sstevel@tonic-gate void
1606*7c478bd9Sstevel@tonic-gate tenot(struct blk *p, int sc)
1607*7c478bd9Sstevel@tonic-gate {
1608*7c478bd9Sstevel@tonic-gate 	int c, f;
1609*7c478bd9Sstevel@tonic-gate 
1610*7c478bd9Sstevel@tonic-gate 	fsfile(p);
1611*7c478bd9Sstevel@tonic-gate 
1612*7c478bd9Sstevel@tonic-gate 	f = 0;
1613*7c478bd9Sstevel@tonic-gate 
1614*7c478bd9Sstevel@tonic-gate 	/*
1615*7c478bd9Sstevel@tonic-gate 	 * at this point, the number is stored as base 100 (two decimal
1616*7c478bd9Sstevel@tonic-gate 	 * digits per char) stuck in a buf (character array) backwards.
1617*7c478bd9Sstevel@tonic-gate 	 * sc indicates the scaling factor.
1618*7c478bd9Sstevel@tonic-gate 	 */
1619*7c478bd9Sstevel@tonic-gate 
1620*7c478bd9Sstevel@tonic-gate 	while ((sfbeg(p) == 0) && ((p->rd-p->beg-1)*2 >= sc)) {
1621*7c478bd9Sstevel@tonic-gate 		/*
1622*7c478bd9Sstevel@tonic-gate 		 * get numbers from the buf until we are the beginning of
1623*7c478bd9Sstevel@tonic-gate 		 * the buf (i.e., there are no more numbers) or the numbers
1624*7c478bd9Sstevel@tonic-gate 		 * remaining fall within the scaled (to the right of the
1625*7c478bd9Sstevel@tonic-gate 		 * decimal point) portion.
1626*7c478bd9Sstevel@tonic-gate 		 */
1627*7c478bd9Sstevel@tonic-gate 		c = sbackc(p);
1628*7c478bd9Sstevel@tonic-gate 
1629*7c478bd9Sstevel@tonic-gate 		/*
1630*7c478bd9Sstevel@tonic-gate 		 * POSIX.2
1631*7c478bd9Sstevel@tonic-gate 		 * as we output digits, we have to watch the line length (ll)
1632*7c478bd9Sstevel@tonic-gate 		 * which should include a '\' and a newline.
1633*7c478bd9Sstevel@tonic-gate 		 */
1634*7c478bd9Sstevel@tonic-gate 		if (c < 10) {
1635*7c478bd9Sstevel@tonic-gate 			/*
1636*7c478bd9Sstevel@tonic-gate 			 * if the number is less than 10, we need to output
1637*7c478bd9Sstevel@tonic-gate 			 * a space-holding '0' (unless this is the first time
1638*7c478bd9Sstevel@tonic-gate 			 * through).
1639*7c478bd9Sstevel@tonic-gate 			 */
1640*7c478bd9Sstevel@tonic-gate 			if (f == 1) {
1641*7c478bd9Sstevel@tonic-gate 				CHECKEND;
1642*7c478bd9Sstevel@tonic-gate 				printf("0");
1643*7c478bd9Sstevel@tonic-gate 				count--;
1644*7c478bd9Sstevel@tonic-gate 			}
1645*7c478bd9Sstevel@tonic-gate 
1646*7c478bd9Sstevel@tonic-gate 			CHECKEND;
1647*7c478bd9Sstevel@tonic-gate 			printf("%d", c);
1648*7c478bd9Sstevel@tonic-gate 			count--;
1649*7c478bd9Sstevel@tonic-gate 		} else  {
1650*7c478bd9Sstevel@tonic-gate 			CHECKEND;
1651*7c478bd9Sstevel@tonic-gate 			printf("%d", c / 10);
1652*7c478bd9Sstevel@tonic-gate 			count--;
1653*7c478bd9Sstevel@tonic-gate 
1654*7c478bd9Sstevel@tonic-gate 			CHECKEND;
1655*7c478bd9Sstevel@tonic-gate 			printf("%d", c % 10);
1656*7c478bd9Sstevel@tonic-gate 			count--;
1657*7c478bd9Sstevel@tonic-gate 		}
1658*7c478bd9Sstevel@tonic-gate 		f = 1;
1659*7c478bd9Sstevel@tonic-gate 	}
1660*7c478bd9Sstevel@tonic-gate 
1661*7c478bd9Sstevel@tonic-gate 	if (sc == 0) {
1662*7c478bd9Sstevel@tonic-gate 		/*
1663*7c478bd9Sstevel@tonic-gate 		 * no scaling factor, so we must have exited loop because we
1664*7c478bd9Sstevel@tonic-gate 		 * ran out of numbers.
1665*7c478bd9Sstevel@tonic-gate 		 */
1666*7c478bd9Sstevel@tonic-gate 		printf("\n");
1667*7c478bd9Sstevel@tonic-gate 		release(p);
1668*7c478bd9Sstevel@tonic-gate 		return;
1669*7c478bd9Sstevel@tonic-gate 	}
1670*7c478bd9Sstevel@tonic-gate 
1671*7c478bd9Sstevel@tonic-gate 	if ((p->rd - p->beg) * 2 > sc) {
1672*7c478bd9Sstevel@tonic-gate 		c = sbackc(p);
1673*7c478bd9Sstevel@tonic-gate 
1674*7c478bd9Sstevel@tonic-gate 		CHECKEND;
1675*7c478bd9Sstevel@tonic-gate 		printf("%d", c / 10);
1676*7c478bd9Sstevel@tonic-gate 		count--;
1677*7c478bd9Sstevel@tonic-gate 
1678*7c478bd9Sstevel@tonic-gate 		CHECKEND;
1679*7c478bd9Sstevel@tonic-gate 		printf(".");
1680*7c478bd9Sstevel@tonic-gate 		count--;
1681*7c478bd9Sstevel@tonic-gate 
1682*7c478bd9Sstevel@tonic-gate 		CHECKEND;
1683*7c478bd9Sstevel@tonic-gate 		printf("%d", c % 10);
1684*7c478bd9Sstevel@tonic-gate 		count--;
1685*7c478bd9Sstevel@tonic-gate 
1686*7c478bd9Sstevel@tonic-gate 		sc--;
1687*7c478bd9Sstevel@tonic-gate 	} else {
1688*7c478bd9Sstevel@tonic-gate 		CHECKEND;
1689*7c478bd9Sstevel@tonic-gate 		printf(".");
1690*7c478bd9Sstevel@tonic-gate 		count--;
1691*7c478bd9Sstevel@tonic-gate 	}
1692*7c478bd9Sstevel@tonic-gate 
1693*7c478bd9Sstevel@tonic-gate 	if (sc > (p->rd - p->beg) * 2) {
1694*7c478bd9Sstevel@tonic-gate 		while (sc > (p->rd - p->beg) * 2) {
1695*7c478bd9Sstevel@tonic-gate 			CHECKEND;
1696*7c478bd9Sstevel@tonic-gate 			printf("0");
1697*7c478bd9Sstevel@tonic-gate 			count--;
1698*7c478bd9Sstevel@tonic-gate 
1699*7c478bd9Sstevel@tonic-gate 			sc--;
1700*7c478bd9Sstevel@tonic-gate 		}
1701*7c478bd9Sstevel@tonic-gate 	}
1702*7c478bd9Sstevel@tonic-gate 
1703*7c478bd9Sstevel@tonic-gate 	/* now go through the scaled portion of the number */
1704*7c478bd9Sstevel@tonic-gate 	while (sc > 1) {
1705*7c478bd9Sstevel@tonic-gate 		c = sbackc(p);
1706*7c478bd9Sstevel@tonic-gate 		if (c < 10) {
1707*7c478bd9Sstevel@tonic-gate 			CHECKEND;
1708*7c478bd9Sstevel@tonic-gate 			printf("0");
1709*7c478bd9Sstevel@tonic-gate 			count--;
1710*7c478bd9Sstevel@tonic-gate 
1711*7c478bd9Sstevel@tonic-gate 			CHECKEND;
1712*7c478bd9Sstevel@tonic-gate 			printf("%d", c);
1713*7c478bd9Sstevel@tonic-gate 			count--;
1714*7c478bd9Sstevel@tonic-gate 		} else {
1715*7c478bd9Sstevel@tonic-gate 			CHECKEND;
1716*7c478bd9Sstevel@tonic-gate 			printf("%d", c / 10);
1717*7c478bd9Sstevel@tonic-gate 			count--;
1718*7c478bd9Sstevel@tonic-gate 
1719*7c478bd9Sstevel@tonic-gate 			CHECKEND;
1720*7c478bd9Sstevel@tonic-gate 			printf("%d", c % 10);
1721*7c478bd9Sstevel@tonic-gate 			count--;
1722*7c478bd9Sstevel@tonic-gate 		}
1723*7c478bd9Sstevel@tonic-gate 		sc -= 2;
1724*7c478bd9Sstevel@tonic-gate 	}
1725*7c478bd9Sstevel@tonic-gate 
1726*7c478bd9Sstevel@tonic-gate 	if (sc == 1) {		/* just in case the scaling factor was odd */
1727*7c478bd9Sstevel@tonic-gate 		CHECKEND;
1728*7c478bd9Sstevel@tonic-gate 		printf("%d", sbackc(p) / 10);
1729*7c478bd9Sstevel@tonic-gate 	}
1730*7c478bd9Sstevel@tonic-gate 
1731*7c478bd9Sstevel@tonic-gate 	printf("\n");
1732*7c478bd9Sstevel@tonic-gate 	release(p);
1733*7c478bd9Sstevel@tonic-gate }
1734*7c478bd9Sstevel@tonic-gate 
1735*7c478bd9Sstevel@tonic-gate void
1736*7c478bd9Sstevel@tonic-gate oneot(struct blk *p, int sc, char ch)
1737*7c478bd9Sstevel@tonic-gate {
1738*7c478bd9Sstevel@tonic-gate 	struct blk *q;
1739*7c478bd9Sstevel@tonic-gate 
1740*7c478bd9Sstevel@tonic-gate 	q = removc(p, sc);
1741*7c478bd9Sstevel@tonic-gate 	create(strptr);
1742*7c478bd9Sstevel@tonic-gate 	sputc(strptr, -1);
1743*7c478bd9Sstevel@tonic-gate 	while (length(q) > 0) {
1744*7c478bd9Sstevel@tonic-gate 		p = add(strptr, q);
1745*7c478bd9Sstevel@tonic-gate 		release(q);
1746*7c478bd9Sstevel@tonic-gate 		q = p;
1747*7c478bd9Sstevel@tonic-gate 		OUTC(ch);
1748*7c478bd9Sstevel@tonic-gate 	}
1749*7c478bd9Sstevel@tonic-gate 	release(q);
1750*7c478bd9Sstevel@tonic-gate 	printf("\n");
1751*7c478bd9Sstevel@tonic-gate }
1752*7c478bd9Sstevel@tonic-gate 
1753*7c478bd9Sstevel@tonic-gate void
1754*7c478bd9Sstevel@tonic-gate hexot(struct blk *p, int flg)
1755*7c478bd9Sstevel@tonic-gate {
1756*7c478bd9Sstevel@tonic-gate 	int c;
1757*7c478bd9Sstevel@tonic-gate 
1758*7c478bd9Sstevel@tonic-gate 	rewind(p);
1759*7c478bd9Sstevel@tonic-gate 	if (sfeof(p) != 0) {
1760*7c478bd9Sstevel@tonic-gate 		sputc(strptr, '0');
1761*7c478bd9Sstevel@tonic-gate 		release(p);
1762*7c478bd9Sstevel@tonic-gate 		return;
1763*7c478bd9Sstevel@tonic-gate 	}
1764*7c478bd9Sstevel@tonic-gate 	c = sgetc(p);
1765*7c478bd9Sstevel@tonic-gate 	release(p);
1766*7c478bd9Sstevel@tonic-gate 	if (c >= 16) {
1767*7c478bd9Sstevel@tonic-gate 		printf(gettext("hex digit > 16"));
1768*7c478bd9Sstevel@tonic-gate 		return;
1769*7c478bd9Sstevel@tonic-gate 	}
1770*7c478bd9Sstevel@tonic-gate 	sputc(strptr, c < 10 ? c + '0' : c - 10 + 'A');
1771*7c478bd9Sstevel@tonic-gate }
1772*7c478bd9Sstevel@tonic-gate 
1773*7c478bd9Sstevel@tonic-gate void
1774*7c478bd9Sstevel@tonic-gate bigot(struct blk *p, int flg)
1775*7c478bd9Sstevel@tonic-gate {
1776*7c478bd9Sstevel@tonic-gate 	struct blk *t, *q;
1777*7c478bd9Sstevel@tonic-gate 	int l;
1778*7c478bd9Sstevel@tonic-gate 	int neg;
1779*7c478bd9Sstevel@tonic-gate 
1780*7c478bd9Sstevel@tonic-gate 	if (flg == 1)
1781*7c478bd9Sstevel@tonic-gate 		t = salloc(0);
1782*7c478bd9Sstevel@tonic-gate 	else {
1783*7c478bd9Sstevel@tonic-gate 		t = strptr;
1784*7c478bd9Sstevel@tonic-gate 		l = length(strptr) + fw - 1;
1785*7c478bd9Sstevel@tonic-gate 	}
1786*7c478bd9Sstevel@tonic-gate 	neg = 0;
1787*7c478bd9Sstevel@tonic-gate 	if (length(p) != 0) {
1788*7c478bd9Sstevel@tonic-gate 		fsfile(p);
1789*7c478bd9Sstevel@tonic-gate 		if (sbackc(p) < 0) {
1790*7c478bd9Sstevel@tonic-gate 			neg = 1;
1791*7c478bd9Sstevel@tonic-gate 			chsign(p);
1792*7c478bd9Sstevel@tonic-gate 		}
1793*7c478bd9Sstevel@tonic-gate 		while (length(p) != 0) {
1794*7c478bd9Sstevel@tonic-gate 			q = div(p, tenptr);
1795*7c478bd9Sstevel@tonic-gate 			release(p);
1796*7c478bd9Sstevel@tonic-gate 			p = q;
1797*7c478bd9Sstevel@tonic-gate 			rewind(rem);
1798*7c478bd9Sstevel@tonic-gate 			sputc(t, sfeof(rem) ? '0' : sgetc(rem) + '0');
1799*7c478bd9Sstevel@tonic-gate 			release(rem);
1800*7c478bd9Sstevel@tonic-gate 		}
1801*7c478bd9Sstevel@tonic-gate 	}
1802*7c478bd9Sstevel@tonic-gate 	release(p);
1803*7c478bd9Sstevel@tonic-gate 	if (flg == 1) {
1804*7c478bd9Sstevel@tonic-gate 		l = fw1 - length(t);
1805*7c478bd9Sstevel@tonic-gate 		if (neg != 0) {
1806*7c478bd9Sstevel@tonic-gate 			l--;
1807*7c478bd9Sstevel@tonic-gate 			sputc(strptr, '-');
1808*7c478bd9Sstevel@tonic-gate 		}
1809*7c478bd9Sstevel@tonic-gate 		fsfile(t);
1810*7c478bd9Sstevel@tonic-gate 		while (l-- > 0)
1811*7c478bd9Sstevel@tonic-gate 			sputc(strptr, '0');
1812*7c478bd9Sstevel@tonic-gate 		while (sfbeg(t) == 0)
1813*7c478bd9Sstevel@tonic-gate 			sputc(strptr, sbackc(t));
1814*7c478bd9Sstevel@tonic-gate 		release(t);
1815*7c478bd9Sstevel@tonic-gate 	} else {
1816*7c478bd9Sstevel@tonic-gate 		l -= length(strptr);
1817*7c478bd9Sstevel@tonic-gate 		while (l-- > 0)
1818*7c478bd9Sstevel@tonic-gate 			sputc(strptr, '0');
1819*7c478bd9Sstevel@tonic-gate 		if (neg != 0) {
1820*7c478bd9Sstevel@tonic-gate 			sunputc(strptr);
1821*7c478bd9Sstevel@tonic-gate 			sputc(strptr, '-');
1822*7c478bd9Sstevel@tonic-gate 		}
1823*7c478bd9Sstevel@tonic-gate 	}
1824*7c478bd9Sstevel@tonic-gate }
1825*7c478bd9Sstevel@tonic-gate 
1826*7c478bd9Sstevel@tonic-gate struct blk *
1827*7c478bd9Sstevel@tonic-gate add(struct blk *a1, struct blk *a2)
1828*7c478bd9Sstevel@tonic-gate {
1829*7c478bd9Sstevel@tonic-gate 	struct blk *p;
1830*7c478bd9Sstevel@tonic-gate 	int carry, n;
1831*7c478bd9Sstevel@tonic-gate 	int size;
1832*7c478bd9Sstevel@tonic-gate 	int c, n1, n2;
1833*7c478bd9Sstevel@tonic-gate 
1834*7c478bd9Sstevel@tonic-gate 	size = length(a1) > length(a2) ? length(a1) : length(a2);
1835*7c478bd9Sstevel@tonic-gate 	p = salloc(size);
1836*7c478bd9Sstevel@tonic-gate 	rewind(a1);
1837*7c478bd9Sstevel@tonic-gate 	rewind(a2);
1838*7c478bd9Sstevel@tonic-gate 	carry = 0;
1839*7c478bd9Sstevel@tonic-gate 	while (--size >= 0) {
1840*7c478bd9Sstevel@tonic-gate 		n1 = sfeof(a1) ? 0 : sgetc(a1);
1841*7c478bd9Sstevel@tonic-gate 		n2 = sfeof(a2) ? 0 : sgetc(a2);
1842*7c478bd9Sstevel@tonic-gate 		n = n1 + n2 + carry;
1843*7c478bd9Sstevel@tonic-gate 		if (n >= 100) {
1844*7c478bd9Sstevel@tonic-gate 			carry = 1;
1845*7c478bd9Sstevel@tonic-gate 			n -= 100;
1846*7c478bd9Sstevel@tonic-gate 		} else
1847*7c478bd9Sstevel@tonic-gate 			if (n < 0) {
1848*7c478bd9Sstevel@tonic-gate 				carry = -1;
1849*7c478bd9Sstevel@tonic-gate 				n += 100;
1850*7c478bd9Sstevel@tonic-gate 			} else
1851*7c478bd9Sstevel@tonic-gate 				carry = 0;
1852*7c478bd9Sstevel@tonic-gate 		sputc(p, n);
1853*7c478bd9Sstevel@tonic-gate 	}
1854*7c478bd9Sstevel@tonic-gate 	if (carry != 0)
1855*7c478bd9Sstevel@tonic-gate 		sputc(p, carry);
1856*7c478bd9Sstevel@tonic-gate 	fsfile(p);
1857*7c478bd9Sstevel@tonic-gate 	if (sfbeg(p) == 0) {
1858*7c478bd9Sstevel@tonic-gate 		while (sfbeg(p) == 0 && (c = sbackc(p)) == 0);
1859*7c478bd9Sstevel@tonic-gate 		if (c != 0)
1860*7c478bd9Sstevel@tonic-gate 			salterc(p, c);
1861*7c478bd9Sstevel@tonic-gate 		truncate(p);
1862*7c478bd9Sstevel@tonic-gate 	}
1863*7c478bd9Sstevel@tonic-gate 	fsfile(p);
1864*7c478bd9Sstevel@tonic-gate 	if (sfbeg(p) == 0 && sbackc(p) == -1) {
1865*7c478bd9Sstevel@tonic-gate 		while ((c = sbackc(p)) == 99) {
1866*7c478bd9Sstevel@tonic-gate 			if (c == EOF)
1867*7c478bd9Sstevel@tonic-gate 				break;
1868*7c478bd9Sstevel@tonic-gate 		}
1869*7c478bd9Sstevel@tonic-gate 		sgetc(p);
1870*7c478bd9Sstevel@tonic-gate 		salterc(p, -1);
1871*7c478bd9Sstevel@tonic-gate 		truncate(p);
1872*7c478bd9Sstevel@tonic-gate 	}
1873*7c478bd9Sstevel@tonic-gate 	return (p);
1874*7c478bd9Sstevel@tonic-gate }
1875*7c478bd9Sstevel@tonic-gate 
1876*7c478bd9Sstevel@tonic-gate int
1877*7c478bd9Sstevel@tonic-gate eqk() {
1878*7c478bd9Sstevel@tonic-gate 	struct blk *p, *q;
1879*7c478bd9Sstevel@tonic-gate 	int skp, skq;
1880*7c478bd9Sstevel@tonic-gate 
1881*7c478bd9Sstevel@tonic-gate 	p = pop();
1882*7c478bd9Sstevel@tonic-gate 	EMPTYS;
1883*7c478bd9Sstevel@tonic-gate 	q = pop();
1884*7c478bd9Sstevel@tonic-gate 	EMPTYSR(p);
1885*7c478bd9Sstevel@tonic-gate 	skp = sunputc(p);
1886*7c478bd9Sstevel@tonic-gate 	skq = sunputc(q);
1887*7c478bd9Sstevel@tonic-gate 	if (skp == skq) {
1888*7c478bd9Sstevel@tonic-gate 		arg1 = p;
1889*7c478bd9Sstevel@tonic-gate 		arg2 = q;
1890*7c478bd9Sstevel@tonic-gate 		savk = skp;
1891*7c478bd9Sstevel@tonic-gate 		return (0);
1892*7c478bd9Sstevel@tonic-gate 	} else
1893*7c478bd9Sstevel@tonic-gate 		if (skp < skq) {
1894*7c478bd9Sstevel@tonic-gate 			savk = skq;
1895*7c478bd9Sstevel@tonic-gate 			p = add0(p, skq - skp);
1896*7c478bd9Sstevel@tonic-gate 		} else {
1897*7c478bd9Sstevel@tonic-gate 			savk = skp;
1898*7c478bd9Sstevel@tonic-gate 			q = add0(q, skp - skq);
1899*7c478bd9Sstevel@tonic-gate 		}
1900*7c478bd9Sstevel@tonic-gate 	arg1 = p;
1901*7c478bd9Sstevel@tonic-gate 	arg2 = q;
1902*7c478bd9Sstevel@tonic-gate 	return (0);
1903*7c478bd9Sstevel@tonic-gate }
1904*7c478bd9Sstevel@tonic-gate 
1905*7c478bd9Sstevel@tonic-gate struct blk *
1906*7c478bd9Sstevel@tonic-gate removc(struct blk *p, int n)
1907*7c478bd9Sstevel@tonic-gate {
1908*7c478bd9Sstevel@tonic-gate 	struct blk *q, *r;
1909*7c478bd9Sstevel@tonic-gate 
1910*7c478bd9Sstevel@tonic-gate 	rewind(p);
1911*7c478bd9Sstevel@tonic-gate 	while (n > 1) {
1912*7c478bd9Sstevel@tonic-gate 		sgetc(p);
1913*7c478bd9Sstevel@tonic-gate 		n -= 2;
1914*7c478bd9Sstevel@tonic-gate 	}
1915*7c478bd9Sstevel@tonic-gate 	q = salloc(2);
1916*7c478bd9Sstevel@tonic-gate 	while (sfeof(p) == 0)
1917*7c478bd9Sstevel@tonic-gate 		sputc(q, sgetc(p));
1918*7c478bd9Sstevel@tonic-gate 	if (n == 1) {
1919*7c478bd9Sstevel@tonic-gate 		r = div(q, tenptr);
1920*7c478bd9Sstevel@tonic-gate 		release(q);
1921*7c478bd9Sstevel@tonic-gate 		release(rem);
1922*7c478bd9Sstevel@tonic-gate 		q = r;
1923*7c478bd9Sstevel@tonic-gate 	}
1924*7c478bd9Sstevel@tonic-gate 	release(p);
1925*7c478bd9Sstevel@tonic-gate 	return (q);
1926*7c478bd9Sstevel@tonic-gate }
1927*7c478bd9Sstevel@tonic-gate 
1928*7c478bd9Sstevel@tonic-gate struct blk *
1929*7c478bd9Sstevel@tonic-gate scalint(struct blk *p)
1930*7c478bd9Sstevel@tonic-gate {
1931*7c478bd9Sstevel@tonic-gate 	int n;
1932*7c478bd9Sstevel@tonic-gate 
1933*7c478bd9Sstevel@tonic-gate 	n = sunputc(p);
1934*7c478bd9Sstevel@tonic-gate 	p = removc(p, n);
1935*7c478bd9Sstevel@tonic-gate 	return (p);
1936*7c478bd9Sstevel@tonic-gate }
1937*7c478bd9Sstevel@tonic-gate 
1938*7c478bd9Sstevel@tonic-gate struct blk *
1939*7c478bd9Sstevel@tonic-gate scale(struct blk *p, int n)
1940*7c478bd9Sstevel@tonic-gate {
1941*7c478bd9Sstevel@tonic-gate 	struct blk *q, *s, *t;
1942*7c478bd9Sstevel@tonic-gate 
1943*7c478bd9Sstevel@tonic-gate 	t = add0(p, n);
1944*7c478bd9Sstevel@tonic-gate 	q = salloc(1);
1945*7c478bd9Sstevel@tonic-gate 	sputc(q, n);
1946*7c478bd9Sstevel@tonic-gate 	s = exp(inbas, q);
1947*7c478bd9Sstevel@tonic-gate 	release(q);
1948*7c478bd9Sstevel@tonic-gate 	q = div(t, s);
1949*7c478bd9Sstevel@tonic-gate 	release(t);
1950*7c478bd9Sstevel@tonic-gate 	release(s);
1951*7c478bd9Sstevel@tonic-gate 	release(rem);
1952*7c478bd9Sstevel@tonic-gate 	sputc(q, n);
1953*7c478bd9Sstevel@tonic-gate 	return (q);
1954*7c478bd9Sstevel@tonic-gate }
1955*7c478bd9Sstevel@tonic-gate 
1956*7c478bd9Sstevel@tonic-gate int
1957*7c478bd9Sstevel@tonic-gate subt() {
1958*7c478bd9Sstevel@tonic-gate 	arg1 = pop();
1959*7c478bd9Sstevel@tonic-gate 	EMPTYS;
1960*7c478bd9Sstevel@tonic-gate 	savk = sunputc(arg1);
1961*7c478bd9Sstevel@tonic-gate 	chsign(arg1);
1962*7c478bd9Sstevel@tonic-gate 	sputc(arg1, savk);
1963*7c478bd9Sstevel@tonic-gate 	pushp(arg1);
1964*7c478bd9Sstevel@tonic-gate 	if (eqk() != 0)
1965*7c478bd9Sstevel@tonic-gate 		return (1);
1966*7c478bd9Sstevel@tonic-gate 	binop('+');
1967*7c478bd9Sstevel@tonic-gate 	return (0);
1968*7c478bd9Sstevel@tonic-gate }
1969*7c478bd9Sstevel@tonic-gate 
1970*7c478bd9Sstevel@tonic-gate int
1971*7c478bd9Sstevel@tonic-gate command() {
1972*7c478bd9Sstevel@tonic-gate 	int c;
1973*7c478bd9Sstevel@tonic-gate 	char line[100], *sl;
1974*7c478bd9Sstevel@tonic-gate 	void (*savint)();
1975*7c478bd9Sstevel@tonic-gate 	pid_t pid, rpid;
1976*7c478bd9Sstevel@tonic-gate 	int retcode;
1977*7c478bd9Sstevel@tonic-gate 
1978*7c478bd9Sstevel@tonic-gate 	switch (c = readc()) {
1979*7c478bd9Sstevel@tonic-gate 	case '<':
1980*7c478bd9Sstevel@tonic-gate 		return (cond(NL));
1981*7c478bd9Sstevel@tonic-gate 	case '>':
1982*7c478bd9Sstevel@tonic-gate 		return (cond(NG));
1983*7c478bd9Sstevel@tonic-gate 	case '=':
1984*7c478bd9Sstevel@tonic-gate 		return (cond(NE));
1985*7c478bd9Sstevel@tonic-gate 	default:
1986*7c478bd9Sstevel@tonic-gate 		sl = line;
1987*7c478bd9Sstevel@tonic-gate 		*sl++ = c;
1988*7c478bd9Sstevel@tonic-gate 		while ((c = readc()) != '\n')
1989*7c478bd9Sstevel@tonic-gate 			*sl++ = c;
1990*7c478bd9Sstevel@tonic-gate 		*sl = 0;
1991*7c478bd9Sstevel@tonic-gate 		if ((pid = fork()) == (pid_t)0) {
1992*7c478bd9Sstevel@tonic-gate 			execl("/usr/bin/sh", "sh", "-c", line, 0);
1993*7c478bd9Sstevel@tonic-gate 			exit(0100);
1994*7c478bd9Sstevel@tonic-gate 		}
1995*7c478bd9Sstevel@tonic-gate 		savint = signal(SIGINT, SIG_IGN);
1996*7c478bd9Sstevel@tonic-gate 		while ((rpid = wait(&retcode)) != pid && rpid != (pid_t)-1);
1997*7c478bd9Sstevel@tonic-gate 		signal(SIGINT, savint);
1998*7c478bd9Sstevel@tonic-gate 		printf(gettext("!\n"));
1999*7c478bd9Sstevel@tonic-gate 		return (0);
2000*7c478bd9Sstevel@tonic-gate 	}
2001*7c478bd9Sstevel@tonic-gate }
2002*7c478bd9Sstevel@tonic-gate 
2003*7c478bd9Sstevel@tonic-gate int
2004*7c478bd9Sstevel@tonic-gate cond(char c)
2005*7c478bd9Sstevel@tonic-gate {
2006*7c478bd9Sstevel@tonic-gate 	struct blk *p;
2007*7c478bd9Sstevel@tonic-gate 	int cc;
2008*7c478bd9Sstevel@tonic-gate 
2009*7c478bd9Sstevel@tonic-gate 	if (subt() != 0)
2010*7c478bd9Sstevel@tonic-gate 		return (1);
2011*7c478bd9Sstevel@tonic-gate 	p = pop();
2012*7c478bd9Sstevel@tonic-gate 	sunputc(p);
2013*7c478bd9Sstevel@tonic-gate 	if (length(p) == 0) {
2014*7c478bd9Sstevel@tonic-gate 		release(p);
2015*7c478bd9Sstevel@tonic-gate 		if (c == '<' || c == '>' || c == NE) {
2016*7c478bd9Sstevel@tonic-gate 			readc();
2017*7c478bd9Sstevel@tonic-gate 			return (0);
2018*7c478bd9Sstevel@tonic-gate 		}
2019*7c478bd9Sstevel@tonic-gate 		load();
2020*7c478bd9Sstevel@tonic-gate 		return (1);
2021*7c478bd9Sstevel@tonic-gate 	} else {
2022*7c478bd9Sstevel@tonic-gate 		if (c == '=') {
2023*7c478bd9Sstevel@tonic-gate 			release(p);
2024*7c478bd9Sstevel@tonic-gate 			readc();
2025*7c478bd9Sstevel@tonic-gate 			return (0);
2026*7c478bd9Sstevel@tonic-gate 		}
2027*7c478bd9Sstevel@tonic-gate 	}
2028*7c478bd9Sstevel@tonic-gate 	if (c == NE) {
2029*7c478bd9Sstevel@tonic-gate 		release(p);
2030*7c478bd9Sstevel@tonic-gate 		load();
2031*7c478bd9Sstevel@tonic-gate 		return (1);
2032*7c478bd9Sstevel@tonic-gate 	}
2033*7c478bd9Sstevel@tonic-gate 	fsfile(p);
2034*7c478bd9Sstevel@tonic-gate 	cc = sbackc(p);
2035*7c478bd9Sstevel@tonic-gate 	release(p);
2036*7c478bd9Sstevel@tonic-gate 	if ((cc < 0 && (c == '<' || c == NG)) ||
2037*7c478bd9Sstevel@tonic-gate 	    (cc > 0) && (c == '>' || c == NL)) {
2038*7c478bd9Sstevel@tonic-gate 		readc();
2039*7c478bd9Sstevel@tonic-gate 		return (0);
2040*7c478bd9Sstevel@tonic-gate 	}
2041*7c478bd9Sstevel@tonic-gate 	load();
2042*7c478bd9Sstevel@tonic-gate 	return (1);
2043*7c478bd9Sstevel@tonic-gate }
2044*7c478bd9Sstevel@tonic-gate 
2045*7c478bd9Sstevel@tonic-gate void
2046*7c478bd9Sstevel@tonic-gate load() {
2047*7c478bd9Sstevel@tonic-gate 	int c;
2048*7c478bd9Sstevel@tonic-gate 	struct blk *p, *q, *t, *s;
2049*7c478bd9Sstevel@tonic-gate 
2050*7c478bd9Sstevel@tonic-gate 	c = readc() & 0377;
2051*7c478bd9Sstevel@tonic-gate 	sptr = stable[c];
2052*7c478bd9Sstevel@tonic-gate 	if (sptr != 0) {
2053*7c478bd9Sstevel@tonic-gate 		p = sptr->val;
2054*7c478bd9Sstevel@tonic-gate 		if (c >= ARRAYST) {
2055*7c478bd9Sstevel@tonic-gate 			q = salloc(length(p));
2056*7c478bd9Sstevel@tonic-gate 			rewind(p);
2057*7c478bd9Sstevel@tonic-gate 			while (sfeof(p) == 0) {
2058*7c478bd9Sstevel@tonic-gate 				s = getwd(p);
2059*7c478bd9Sstevel@tonic-gate 				if (s == 0)
2060*7c478bd9Sstevel@tonic-gate 					putwd(q, (struct blk *)NULL);
2061*7c478bd9Sstevel@tonic-gate 				else {
2062*7c478bd9Sstevel@tonic-gate 					t = copy(s, length(s));
2063*7c478bd9Sstevel@tonic-gate 					putwd(q, t);
2064*7c478bd9Sstevel@tonic-gate 				}
2065*7c478bd9Sstevel@tonic-gate 			}
2066*7c478bd9Sstevel@tonic-gate 			pushp(q);
2067*7c478bd9Sstevel@tonic-gate 		} else {
2068*7c478bd9Sstevel@tonic-gate 			q = copy(p, length(p));
2069*7c478bd9Sstevel@tonic-gate 			pushp(q);
2070*7c478bd9Sstevel@tonic-gate 		}
2071*7c478bd9Sstevel@tonic-gate 	} else {
2072*7c478bd9Sstevel@tonic-gate 		q = salloc(1);
2073*7c478bd9Sstevel@tonic-gate 		if (c <= LASTFUN) {
2074*7c478bd9Sstevel@tonic-gate 			printf(gettext
2075*7c478bd9Sstevel@tonic-gate 			    ("function %c undefined\n"), c + 'a' - 1);
2076*7c478bd9Sstevel@tonic-gate 			sputc(q, 'c');
2077*7c478bd9Sstevel@tonic-gate 			sputc(q, '0');
2078*7c478bd9Sstevel@tonic-gate 			sputc(q, ' ');
2079*7c478bd9Sstevel@tonic-gate 			sputc(q, '1');
2080*7c478bd9Sstevel@tonic-gate 			sputc(q, 'Q');
2081*7c478bd9Sstevel@tonic-gate 		} else
2082*7c478bd9Sstevel@tonic-gate 			sputc(q, 0);
2083*7c478bd9Sstevel@tonic-gate 		pushp(q);
2084*7c478bd9Sstevel@tonic-gate 	}
2085*7c478bd9Sstevel@tonic-gate }
2086*7c478bd9Sstevel@tonic-gate 
2087*7c478bd9Sstevel@tonic-gate int
2088*7c478bd9Sstevel@tonic-gate log2(long n)
2089*7c478bd9Sstevel@tonic-gate {
2090*7c478bd9Sstevel@tonic-gate 	int i;
2091*7c478bd9Sstevel@tonic-gate 
2092*7c478bd9Sstevel@tonic-gate 	if (n == 0)
2093*7c478bd9Sstevel@tonic-gate 		return (0);
2094*7c478bd9Sstevel@tonic-gate 	i = 31;
2095*7c478bd9Sstevel@tonic-gate 	if (n < 0)
2096*7c478bd9Sstevel@tonic-gate 		return (i);
2097*7c478bd9Sstevel@tonic-gate 	while ((n = n << 1) > 0)
2098*7c478bd9Sstevel@tonic-gate 		i--;
2099*7c478bd9Sstevel@tonic-gate 	return (--i);
2100*7c478bd9Sstevel@tonic-gate }
2101*7c478bd9Sstevel@tonic-gate 
2102*7c478bd9Sstevel@tonic-gate struct blk *
2103*7c478bd9Sstevel@tonic-gate salloc(int size)
2104*7c478bd9Sstevel@tonic-gate {
2105*7c478bd9Sstevel@tonic-gate 	struct blk *hdr;
2106*7c478bd9Sstevel@tonic-gate 	char *ptr;
2107*7c478bd9Sstevel@tonic-gate 	char *dcmalloc();
2108*7c478bd9Sstevel@tonic-gate 	all++;
2109*7c478bd9Sstevel@tonic-gate 	lall++;
2110*7c478bd9Sstevel@tonic-gate 	if (all - rel > active)
2111*7c478bd9Sstevel@tonic-gate 		active = all - rel;
2112*7c478bd9Sstevel@tonic-gate 	nbytes += size;
2113*7c478bd9Sstevel@tonic-gate 	lbytes += size;
2114*7c478bd9Sstevel@tonic-gate 	if (nbytes > maxsize)
2115*7c478bd9Sstevel@tonic-gate 		maxsize = nbytes;
2116*7c478bd9Sstevel@tonic-gate 	if (size > longest)
2117*7c478bd9Sstevel@tonic-gate 		longest = size;
2118*7c478bd9Sstevel@tonic-gate 	ptr = dcmalloc((unsigned)size);
2119*7c478bd9Sstevel@tonic-gate 	if (ptr == 0) {
2120*7c478bd9Sstevel@tonic-gate 		garbage("salloc");
2121*7c478bd9Sstevel@tonic-gate 		if ((ptr = dcmalloc((unsigned)size)) == 0)
2122*7c478bd9Sstevel@tonic-gate 			ospace("salloc");
2123*7c478bd9Sstevel@tonic-gate 	}
2124*7c478bd9Sstevel@tonic-gate 	if ((hdr = hfree) == 0)
2125*7c478bd9Sstevel@tonic-gate 		hdr = morehd();
2126*7c478bd9Sstevel@tonic-gate 	hfree = (struct blk *)hdr->rd;
2127*7c478bd9Sstevel@tonic-gate 	hdr->rd = hdr->wt = hdr->beg = ptr;
2128*7c478bd9Sstevel@tonic-gate 	hdr->last = ptr + size;
2129*7c478bd9Sstevel@tonic-gate 	return (hdr);
2130*7c478bd9Sstevel@tonic-gate }
2131*7c478bd9Sstevel@tonic-gate 
2132*7c478bd9Sstevel@tonic-gate struct blk *
2133*7c478bd9Sstevel@tonic-gate morehd() {
2134*7c478bd9Sstevel@tonic-gate 	struct blk *h, *kk;
2135*7c478bd9Sstevel@tonic-gate 	char *dcmalloc();
2136*7c478bd9Sstevel@tonic-gate 
2137*7c478bd9Sstevel@tonic-gate 	headmor++;
2138*7c478bd9Sstevel@tonic-gate 	nbytes += HEADSZ;
2139*7c478bd9Sstevel@tonic-gate 	hfree = h = (struct blk *)dcmalloc(HEADSZ);
2140*7c478bd9Sstevel@tonic-gate 	if (hfree == 0) {
2141*7c478bd9Sstevel@tonic-gate 		garbage("morehd");
2142*7c478bd9Sstevel@tonic-gate 		if ((hfree = h = (struct blk *)dcmalloc(HEADSZ)) == 0)
2143*7c478bd9Sstevel@tonic-gate 			ospace("headers");
2144*7c478bd9Sstevel@tonic-gate 	}
2145*7c478bd9Sstevel@tonic-gate 	kk = h;
2146*7c478bd9Sstevel@tonic-gate 	while (h < hfree + (HEADSZ/BLK))
2147*7c478bd9Sstevel@tonic-gate 		(h++)->rd = (char *)++kk;
2148*7c478bd9Sstevel@tonic-gate 	(--h)->rd = 0;
2149*7c478bd9Sstevel@tonic-gate 	return (hfree);
2150*7c478bd9Sstevel@tonic-gate }
2151*7c478bd9Sstevel@tonic-gate 
2152*7c478bd9Sstevel@tonic-gate struct blk *
2153*7c478bd9Sstevel@tonic-gate copy(struct blk *hptr, int size)
2154*7c478bd9Sstevel@tonic-gate {
2155*7c478bd9Sstevel@tonic-gate 	struct blk *hdr;
2156*7c478bd9Sstevel@tonic-gate 	unsigned sz;
2157*7c478bd9Sstevel@tonic-gate 	char *ptr;
2158*7c478bd9Sstevel@tonic-gate 
2159*7c478bd9Sstevel@tonic-gate 	all++;
2160*7c478bd9Sstevel@tonic-gate 	lall++;
2161*7c478bd9Sstevel@tonic-gate 	lcopy++;
2162*7c478bd9Sstevel@tonic-gate 	nbytes += size;
2163*7c478bd9Sstevel@tonic-gate 	lbytes += size;
2164*7c478bd9Sstevel@tonic-gate 	if (size > longest)
2165*7c478bd9Sstevel@tonic-gate 		longest = size;
2166*7c478bd9Sstevel@tonic-gate 	if (size > maxsize)
2167*7c478bd9Sstevel@tonic-gate 		maxsize = size;
2168*7c478bd9Sstevel@tonic-gate 	sz = length(hptr);
2169*7c478bd9Sstevel@tonic-gate 	ptr = nalloc(hptr->beg, (unsigned)size);
2170*7c478bd9Sstevel@tonic-gate 	if (ptr == 0) {
2171*7c478bd9Sstevel@tonic-gate 		garbage("copy");
2172*7c478bd9Sstevel@tonic-gate 		if ((ptr = nalloc(hptr->beg, (unsigned)size)) == NULL) {
2173*7c478bd9Sstevel@tonic-gate 			printf(gettext("copy size %d\n"), size);
2174*7c478bd9Sstevel@tonic-gate 			ospace("copy");
2175*7c478bd9Sstevel@tonic-gate 		}
2176*7c478bd9Sstevel@tonic-gate 	}
2177*7c478bd9Sstevel@tonic-gate 	if ((hdr = hfree) == 0)
2178*7c478bd9Sstevel@tonic-gate 		hdr = morehd();
2179*7c478bd9Sstevel@tonic-gate 	hfree = (struct blk *)hdr->rd;
2180*7c478bd9Sstevel@tonic-gate 	hdr->rd = hdr->beg = ptr;
2181*7c478bd9Sstevel@tonic-gate 	hdr->last = ptr + size;
2182*7c478bd9Sstevel@tonic-gate 	hdr->wt = ptr + sz;
2183*7c478bd9Sstevel@tonic-gate 	ptr = hdr->wt;
2184*7c478bd9Sstevel@tonic-gate 	while (ptr < hdr->last)
2185*7c478bd9Sstevel@tonic-gate 		*ptr++ = '\0';
2186*7c478bd9Sstevel@tonic-gate 	return (hdr);
2187*7c478bd9Sstevel@tonic-gate }
2188*7c478bd9Sstevel@tonic-gate 
2189*7c478bd9Sstevel@tonic-gate void
2190*7c478bd9Sstevel@tonic-gate sdump(char *s1, struct blk *hptr)
2191*7c478bd9Sstevel@tonic-gate {
2192*7c478bd9Sstevel@tonic-gate 	char *p;
2193*7c478bd9Sstevel@tonic-gate 
2194*7c478bd9Sstevel@tonic-gate 	if (hptr) {
2195*7c478bd9Sstevel@tonic-gate 		printf("%s %o rd %o wt %o beg %o last %o\n", s1, hptr,
2196*7c478bd9Sstevel@tonic-gate 		    hptr->rd, hptr->wt, hptr->beg, hptr->last);
2197*7c478bd9Sstevel@tonic-gate 		p = hptr->beg;
2198*7c478bd9Sstevel@tonic-gate 		while (p < hptr->wt)
2199*7c478bd9Sstevel@tonic-gate 			printf("%d ", *p++);
2200*7c478bd9Sstevel@tonic-gate 		printf("\n");
2201*7c478bd9Sstevel@tonic-gate 	} else
2202*7c478bd9Sstevel@tonic-gate 		printf("%s %o\n", s1, hptr);
2203*7c478bd9Sstevel@tonic-gate }
2204*7c478bd9Sstevel@tonic-gate 
2205*7c478bd9Sstevel@tonic-gate void
2206*7c478bd9Sstevel@tonic-gate seekc(struct blk *hptr, int n)
2207*7c478bd9Sstevel@tonic-gate {
2208*7c478bd9Sstevel@tonic-gate 	char *nn, *p;
2209*7c478bd9Sstevel@tonic-gate 
2210*7c478bd9Sstevel@tonic-gate 	nn = hptr->beg + n;
2211*7c478bd9Sstevel@tonic-gate 	if (nn > hptr->last) {
2212*7c478bd9Sstevel@tonic-gate 		nbytes += nn - hptr->last;
2213*7c478bd9Sstevel@tonic-gate 		if (nbytes > maxsize)
2214*7c478bd9Sstevel@tonic-gate 			maxsize = nbytes;
2215*7c478bd9Sstevel@tonic-gate 		lbytes += nn - hptr->last;
2216*7c478bd9Sstevel@tonic-gate 		if (n > longest)
2217*7c478bd9Sstevel@tonic-gate 			longest = n;
2218*7c478bd9Sstevel@tonic-gate 		p = realloc(hptr->beg, (unsigned)n);
2219*7c478bd9Sstevel@tonic-gate 		if (p == 0) {
2220*7c478bd9Sstevel@tonic-gate 			hptr->beg = realloc(hptr->beg,
2221*7c478bd9Sstevel@tonic-gate 			    (unsigned)(hptr->last - hptr->beg));
2222*7c478bd9Sstevel@tonic-gate 			garbage("seekc");
2223*7c478bd9Sstevel@tonic-gate 			if ((p = realloc(hptr->beg, (unsigned)n)) == 0)
2224*7c478bd9Sstevel@tonic-gate 				ospace("seekc");
2225*7c478bd9Sstevel@tonic-gate 		}
2226*7c478bd9Sstevel@tonic-gate 		hptr->beg = p;
2227*7c478bd9Sstevel@tonic-gate 		hptr->wt = hptr->last = hptr->rd = p + n;
2228*7c478bd9Sstevel@tonic-gate 		return;
2229*7c478bd9Sstevel@tonic-gate 	}
2230*7c478bd9Sstevel@tonic-gate 	hptr->rd = nn;
2231*7c478bd9Sstevel@tonic-gate 	if (nn > hptr->wt)
2232*7c478bd9Sstevel@tonic-gate 		hptr->wt = nn;
2233*7c478bd9Sstevel@tonic-gate }
2234*7c478bd9Sstevel@tonic-gate 
2235*7c478bd9Sstevel@tonic-gate void
2236*7c478bd9Sstevel@tonic-gate salterwd(struct wblk *hptr, struct blk *n)
2237*7c478bd9Sstevel@tonic-gate {
2238*7c478bd9Sstevel@tonic-gate 	if (hptr->rdw == hptr->lastw)
2239*7c478bd9Sstevel@tonic-gate 		more((struct blk *)hptr);
2240*7c478bd9Sstevel@tonic-gate 	*hptr->rdw++ = n;
2241*7c478bd9Sstevel@tonic-gate 	if (hptr->rdw > hptr->wtw)
2242*7c478bd9Sstevel@tonic-gate 		hptr->wtw = hptr->rdw;
2243*7c478bd9Sstevel@tonic-gate }
2244*7c478bd9Sstevel@tonic-gate 
2245*7c478bd9Sstevel@tonic-gate void
2246*7c478bd9Sstevel@tonic-gate more(struct blk *hptr)
2247*7c478bd9Sstevel@tonic-gate {
2248*7c478bd9Sstevel@tonic-gate 	unsigned size;
2249*7c478bd9Sstevel@tonic-gate 	char *p;
2250*7c478bd9Sstevel@tonic-gate 
2251*7c478bd9Sstevel@tonic-gate 	if ((size = (hptr->last - hptr->beg) * 2) == 0)
2252*7c478bd9Sstevel@tonic-gate 		size = 1;
2253*7c478bd9Sstevel@tonic-gate 	nbytes += size / 2;
2254*7c478bd9Sstevel@tonic-gate 	if (nbytes > maxsize)
2255*7c478bd9Sstevel@tonic-gate 		maxsize = nbytes;
2256*7c478bd9Sstevel@tonic-gate 	if (size > longest)
2257*7c478bd9Sstevel@tonic-gate 		longest = size;
2258*7c478bd9Sstevel@tonic-gate 	lbytes += size / 2;
2259*7c478bd9Sstevel@tonic-gate 	lmore++;
2260*7c478bd9Sstevel@tonic-gate 	p = realloc(hptr->beg, (unsigned)size);
2261*7c478bd9Sstevel@tonic-gate 	if (p == 0) {
2262*7c478bd9Sstevel@tonic-gate 		hptr->beg = realloc(hptr->beg,
2263*7c478bd9Sstevel@tonic-gate 		    (unsigned)(hptr->last - hptr->beg));
2264*7c478bd9Sstevel@tonic-gate 		garbage("more");
2265*7c478bd9Sstevel@tonic-gate 		if ((p = realloc(hptr->beg, size)) == 0)
2266*7c478bd9Sstevel@tonic-gate 			ospace("more");
2267*7c478bd9Sstevel@tonic-gate 	}
2268*7c478bd9Sstevel@tonic-gate 	hptr->rd = hptr->rd - hptr->beg + p;
2269*7c478bd9Sstevel@tonic-gate 	hptr->wt = hptr->wt - hptr->beg + p;
2270*7c478bd9Sstevel@tonic-gate 	hptr->beg = p;
2271*7c478bd9Sstevel@tonic-gate 	hptr->last = p + size;
2272*7c478bd9Sstevel@tonic-gate }
2273*7c478bd9Sstevel@tonic-gate 
2274*7c478bd9Sstevel@tonic-gate void
2275*7c478bd9Sstevel@tonic-gate ospace(char *s)
2276*7c478bd9Sstevel@tonic-gate {
2277*7c478bd9Sstevel@tonic-gate 	printf(gettext("out of space: %s\n"), s);
2278*7c478bd9Sstevel@tonic-gate 	printf(gettext("all %ld rel %ld headmor %ld\n"), all, rel, headmor);
2279*7c478bd9Sstevel@tonic-gate 	printf(gettext("nbytes %ld\n"), nbytes);
2280*7c478bd9Sstevel@tonic-gate 	sdump("stk", *stkptr);
2281*7c478bd9Sstevel@tonic-gate 	abort();
2282*7c478bd9Sstevel@tonic-gate }
2283*7c478bd9Sstevel@tonic-gate 
2284*7c478bd9Sstevel@tonic-gate #define	G1	gettext("array %o elt %d odd\n")
2285*7c478bd9Sstevel@tonic-gate #define	G2	gettext("tmps %o p %o\n")
2286*7c478bd9Sstevel@tonic-gate void
2287*7c478bd9Sstevel@tonic-gate garbage(char *s)
2288*7c478bd9Sstevel@tonic-gate {
2289*7c478bd9Sstevel@tonic-gate 	int i;
2290*7c478bd9Sstevel@tonic-gate 	struct blk *p, *q;
2291*7c478bd9Sstevel@tonic-gate 	struct sym *tmps;
2292*7c478bd9Sstevel@tonic-gate 	int ct;
2293*7c478bd9Sstevel@tonic-gate 
2294*7c478bd9Sstevel@tonic-gate 	printf(gettext("got to garbage %s\n"), s);
2295*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < TBLSZ; i++) {
2296*7c478bd9Sstevel@tonic-gate 		tmps = stable[i];
2297*7c478bd9Sstevel@tonic-gate 		if (tmps != 0) {
2298*7c478bd9Sstevel@tonic-gate 			if (i < ARRAYST) {
2299*7c478bd9Sstevel@tonic-gate 				do {
2300*7c478bd9Sstevel@tonic-gate 					p = tmps->val;
2301*7c478bd9Sstevel@tonic-gate 					if (((int)p->beg & 01) != 0) {
2302*7c478bd9Sstevel@tonic-gate 						printf(gettext(
2303*7c478bd9Sstevel@tonic-gate 						    "string %o\n"), i);
2304*7c478bd9Sstevel@tonic-gate 						sdump("odd beg", p);
2305*7c478bd9Sstevel@tonic-gate 					}
2306*7c478bd9Sstevel@tonic-gate 					redef(p);
2307*7c478bd9Sstevel@tonic-gate 					tmps = tmps->next;
2308*7c478bd9Sstevel@tonic-gate 				} while (tmps != 0);
2309*7c478bd9Sstevel@tonic-gate 				continue;
2310*7c478bd9Sstevel@tonic-gate 			} else {
2311*7c478bd9Sstevel@tonic-gate 				do {
2312*7c478bd9Sstevel@tonic-gate 					p = tmps->val;
2313*7c478bd9Sstevel@tonic-gate 					rewind(p);
2314*7c478bd9Sstevel@tonic-gate 					ct = 0;
2315*7c478bd9Sstevel@tonic-gate 					while ((q = getwd(p)) != NULL) {
2316*7c478bd9Sstevel@tonic-gate 						ct++;
2317*7c478bd9Sstevel@tonic-gate 						if (q != 0) {
2318*7c478bd9Sstevel@tonic-gate 							if (((int)q->beg & 01)
2319*7c478bd9Sstevel@tonic-gate 							    != 0) {
2320*7c478bd9Sstevel@tonic-gate 								printf(G1,
2321*7c478bd9Sstevel@tonic-gate 								    i - ARRAYST,
2322*7c478bd9Sstevel@tonic-gate 								    ct);
2323*7c478bd9Sstevel@tonic-gate 								printf(G2,
2324*7c478bd9Sstevel@tonic-gate 								    tmps, p);
2325*7c478bd9Sstevel@tonic-gate 								sdump("elt", q);
2326*7c478bd9Sstevel@tonic-gate 							}
2327*7c478bd9Sstevel@tonic-gate 							redef(q);
2328*7c478bd9Sstevel@tonic-gate 						}
2329*7c478bd9Sstevel@tonic-gate 					}
2330*7c478bd9Sstevel@tonic-gate 					tmps = tmps->next;
2331*7c478bd9Sstevel@tonic-gate 				} while (tmps != 0);
2332*7c478bd9Sstevel@tonic-gate 			}
2333*7c478bd9Sstevel@tonic-gate 		}
2334*7c478bd9Sstevel@tonic-gate 	}
2335*7c478bd9Sstevel@tonic-gate }
2336*7c478bd9Sstevel@tonic-gate 
2337*7c478bd9Sstevel@tonic-gate void
2338*7c478bd9Sstevel@tonic-gate redef(struct blk *p)
2339*7c478bd9Sstevel@tonic-gate {
2340*7c478bd9Sstevel@tonic-gate 	int offset;
2341*7c478bd9Sstevel@tonic-gate 	char *newp;
2342*7c478bd9Sstevel@tonic-gate 	char *dcmalloc();
2343*7c478bd9Sstevel@tonic-gate 
2344*7c478bd9Sstevel@tonic-gate 	if ((int)p->beg & 01) {
2345*7c478bd9Sstevel@tonic-gate 		printf(gettext("odd ptr %o hdr %o\n"), p->beg, p);
2346*7c478bd9Sstevel@tonic-gate 		ospace("redef-bad");
2347*7c478bd9Sstevel@tonic-gate 	}
2348*7c478bd9Sstevel@tonic-gate 	free(dummy);
2349*7c478bd9Sstevel@tonic-gate 	dummy = dcmalloc(0);
2350*7c478bd9Sstevel@tonic-gate 	if (dummy == NULL)
2351*7c478bd9Sstevel@tonic-gate 		ospace("dummy");
2352*7c478bd9Sstevel@tonic-gate 	newp = realloc(p->beg, (unsigned)(p->last - p->beg));
2353*7c478bd9Sstevel@tonic-gate 	if (newp == NULL)
2354*7c478bd9Sstevel@tonic-gate 		ospace("redef");
2355*7c478bd9Sstevel@tonic-gate 	offset = newp - p->beg;
2356*7c478bd9Sstevel@tonic-gate 	p->beg = newp;
2357*7c478bd9Sstevel@tonic-gate 	p->rd += offset;
2358*7c478bd9Sstevel@tonic-gate 	p->wt += offset;
2359*7c478bd9Sstevel@tonic-gate 	p->last += offset;
2360*7c478bd9Sstevel@tonic-gate }
2361*7c478bd9Sstevel@tonic-gate 
2362*7c478bd9Sstevel@tonic-gate void
2363*7c478bd9Sstevel@tonic-gate release(struct blk *p)
2364*7c478bd9Sstevel@tonic-gate {
2365*7c478bd9Sstevel@tonic-gate 	rel++;
2366*7c478bd9Sstevel@tonic-gate 	lrel++;
2367*7c478bd9Sstevel@tonic-gate 	nbytes -= p->last - p->beg;
2368*7c478bd9Sstevel@tonic-gate 	p->rd = (char *)hfree;
2369*7c478bd9Sstevel@tonic-gate 	hfree = p;
2370*7c478bd9Sstevel@tonic-gate 	free(p->beg);
2371*7c478bd9Sstevel@tonic-gate 	p->beg = NULL;
2372*7c478bd9Sstevel@tonic-gate }
2373*7c478bd9Sstevel@tonic-gate 
2374*7c478bd9Sstevel@tonic-gate struct blk *
2375*7c478bd9Sstevel@tonic-gate getwd(struct blk *p)
2376*7c478bd9Sstevel@tonic-gate {
2377*7c478bd9Sstevel@tonic-gate 	struct wblk *wp;
2378*7c478bd9Sstevel@tonic-gate 
2379*7c478bd9Sstevel@tonic-gate 	wp = (struct wblk *)p;
2380*7c478bd9Sstevel@tonic-gate 	if (wp->rdw == wp->wtw)
2381*7c478bd9Sstevel@tonic-gate 		return (NULL);
2382*7c478bd9Sstevel@tonic-gate 	return (*wp->rdw++);
2383*7c478bd9Sstevel@tonic-gate }
2384*7c478bd9Sstevel@tonic-gate 
2385*7c478bd9Sstevel@tonic-gate void
2386*7c478bd9Sstevel@tonic-gate putwd(struct blk *p, struct blk *c)
2387*7c478bd9Sstevel@tonic-gate {
2388*7c478bd9Sstevel@tonic-gate 	struct wblk *wp;
2389*7c478bd9Sstevel@tonic-gate 
2390*7c478bd9Sstevel@tonic-gate 	wp = (struct wblk *)p;
2391*7c478bd9Sstevel@tonic-gate 	if (wp->wtw == wp->lastw)
2392*7c478bd9Sstevel@tonic-gate 		more(p);
2393*7c478bd9Sstevel@tonic-gate 	*wp->wtw++ = c;
2394*7c478bd9Sstevel@tonic-gate }
2395*7c478bd9Sstevel@tonic-gate 
2396*7c478bd9Sstevel@tonic-gate struct blk *
2397*7c478bd9Sstevel@tonic-gate lookwd(struct blk *p)
2398*7c478bd9Sstevel@tonic-gate {
2399*7c478bd9Sstevel@tonic-gate 	struct wblk *wp;
2400*7c478bd9Sstevel@tonic-gate 
2401*7c478bd9Sstevel@tonic-gate 	wp = (struct wblk *)p;
2402*7c478bd9Sstevel@tonic-gate 	if (wp->rdw == wp->wtw)
2403*7c478bd9Sstevel@tonic-gate 		return (NULL);
2404*7c478bd9Sstevel@tonic-gate 	return (*wp->rdw);
2405*7c478bd9Sstevel@tonic-gate }
2406*7c478bd9Sstevel@tonic-gate 
2407*7c478bd9Sstevel@tonic-gate char *
2408*7c478bd9Sstevel@tonic-gate nalloc(char *p, unsigned int nbytes)
2409*7c478bd9Sstevel@tonic-gate {
2410*7c478bd9Sstevel@tonic-gate 	char *dcmalloc();
2411*7c478bd9Sstevel@tonic-gate 	char *q, *r;
2412*7c478bd9Sstevel@tonic-gate 	q = r = dcmalloc(nbytes);
2413*7c478bd9Sstevel@tonic-gate 	if (q == 0)
2414*7c478bd9Sstevel@tonic-gate 		return (0);
2415*7c478bd9Sstevel@tonic-gate 	while (nbytes--)
2416*7c478bd9Sstevel@tonic-gate 		*q++ = *p++;
2417*7c478bd9Sstevel@tonic-gate 	return (r);
2418*7c478bd9Sstevel@tonic-gate }
2419*7c478bd9Sstevel@tonic-gate 
2420*7c478bd9Sstevel@tonic-gate char *
2421*7c478bd9Sstevel@tonic-gate dcmalloc(int size)
2422*7c478bd9Sstevel@tonic-gate {
2423*7c478bd9Sstevel@tonic-gate 	return (malloc(size ? size : 1));
2424*7c478bd9Sstevel@tonic-gate }
2425