17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate * with the License.
87c478bd9Sstevel@tonic-gate *
97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate * and limitations under the License.
137c478bd9Sstevel@tonic-gate *
147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate *
207c478bd9Sstevel@tonic-gate * CDDL HEADER END
217c478bd9Sstevel@tonic-gate */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
247c478bd9Sstevel@tonic-gate * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate */
267c478bd9Sstevel@tonic-gate
27*b390fe2cSmuffin /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28*b390fe2cSmuffin /* All Rights Reserved */
29*b390fe2cSmuffin
307c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
317c478bd9Sstevel@tonic-gate
327c478bd9Sstevel@tonic-gate #include <stdio.h>
337c478bd9Sstevel@tonic-gate #include <signal.h>
347c478bd9Sstevel@tonic-gate #include <errno.h>
357c478bd9Sstevel@tonic-gate #include <sys/stat.h>
367c478bd9Sstevel@tonic-gate #include <sys/types.h>
377c478bd9Sstevel@tonic-gate #include <limits.h>
387c478bd9Sstevel@tonic-gate #include "dc.h"
397c478bd9Sstevel@tonic-gate #include <locale.h>
40*b390fe2cSmuffin #include <stdlib.h>
417c478bd9Sstevel@tonic-gate
427c478bd9Sstevel@tonic-gate #define LASTFUN 026
437c478bd9Sstevel@tonic-gate long longest = 0, maxsize = 0, active = 0;
447c478bd9Sstevel@tonic-gate int lall = 0, lrel = 0, lcopy = 0, lmore = 0, lbytes = 0;
457c478bd9Sstevel@tonic-gate
467c478bd9Sstevel@tonic-gate /*
477c478bd9Sstevel@tonic-gate * Routine to handle sign extension of characters on systems that do not
487c478bd9Sstevel@tonic-gate * do automatic sign extension. This should be portable to all 2's and 1's
497c478bd9Sstevel@tonic-gate * complement systems that do or do not provide automatic sign
507c478bd9Sstevel@tonic-gate * extension. If the system provides automatic sign extension or the
517c478bd9Sstevel@tonic-gate * value of 'c' is positive, ctoint() will always return quickly,
527c478bd9Sstevel@tonic-gate * otherwise ctoint() will search for the negative value by attempting
537c478bd9Sstevel@tonic-gate * to wrap 'c' to 0. The number of increments needed to get to 0 is the
547c478bd9Sstevel@tonic-gate * negative value.
557c478bd9Sstevel@tonic-gate *
567c478bd9Sstevel@tonic-gate * Note: This assummes that the representation of values stored in chars
577c478bd9Sstevel@tonic-gate * is sequential and allowed to wrap, and that values < 128 are
587c478bd9Sstevel@tonic-gate * positive. While this is true on 1's and 2's complement machines, it
597c478bd9Sstevel@tonic-gate * may vary on less common architectures.
607c478bd9Sstevel@tonic-gate */
617c478bd9Sstevel@tonic-gate
627c478bd9Sstevel@tonic-gate #if __STDC__
63*b390fe2cSmuffin int
ctoint(char c)64*b390fe2cSmuffin ctoint(char c)
657c478bd9Sstevel@tonic-gate #else
66*b390fe2cSmuffin int
67*b390fe2cSmuffin ctoint(unsigned char c)
687c478bd9Sstevel@tonic-gate #endif
697c478bd9Sstevel@tonic-gate {
707c478bd9Sstevel@tonic-gate int i;
717c478bd9Sstevel@tonic-gate
72*b390fe2cSmuffin if ((unsigned char)c <= SCHAR_MAX)
737c478bd9Sstevel@tonic-gate return ((int)c); /* Normal promotion will work */
747c478bd9Sstevel@tonic-gate for (i = 0; c++; i--); /* Scan for negative value */
757c478bd9Sstevel@tonic-gate return (i);
767c478bd9Sstevel@tonic-gate }
777c478bd9Sstevel@tonic-gate
787c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */
797c478bd9Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't. */
807c478bd9Sstevel@tonic-gate #endif
817c478bd9Sstevel@tonic-gate
82*b390fe2cSmuffin void commnds(void) __NORETURN;
83*b390fe2cSmuffin
84*b390fe2cSmuffin int
main(int argc,char ** argv)857c478bd9Sstevel@tonic-gate main(int argc, char **argv)
867c478bd9Sstevel@tonic-gate {
877c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, "");
887c478bd9Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN);
897c478bd9Sstevel@tonic-gate
907c478bd9Sstevel@tonic-gate init(argc, argv);
917c478bd9Sstevel@tonic-gate commnds();
927c478bd9Sstevel@tonic-gate /* NOTREACHED */
937c478bd9Sstevel@tonic-gate }
947c478bd9Sstevel@tonic-gate
957c478bd9Sstevel@tonic-gate void
commnds(void)96*b390fe2cSmuffin commnds(void)
977c478bd9Sstevel@tonic-gate {
987c478bd9Sstevel@tonic-gate int c;
997c478bd9Sstevel@tonic-gate struct blk *p, *q;
1007c478bd9Sstevel@tonic-gate long l;
1017c478bd9Sstevel@tonic-gate int sign;
1027c478bd9Sstevel@tonic-gate struct blk **ptr, *s, *t;
1037c478bd9Sstevel@tonic-gate struct sym *sp;
1047c478bd9Sstevel@tonic-gate int sk, sk1, sk2;
1057c478bd9Sstevel@tonic-gate int n, d;
1067c478bd9Sstevel@tonic-gate int scalev; /* scaling value for converting blks to integers */
1077c478bd9Sstevel@tonic-gate
1087c478bd9Sstevel@tonic-gate for (; ; ) {
1097c478bd9Sstevel@tonic-gate if (((c = readc()) >= '0' && c <= '9') ||
1107c478bd9Sstevel@tonic-gate (c >= 'A' && c <= 'F') || c == '.') {
1117c478bd9Sstevel@tonic-gate unreadc(c);
1127c478bd9Sstevel@tonic-gate p = readin();
1137c478bd9Sstevel@tonic-gate pushp(p);
1147c478bd9Sstevel@tonic-gate continue;
1157c478bd9Sstevel@tonic-gate }
1167c478bd9Sstevel@tonic-gate switch (c) {
1177c478bd9Sstevel@tonic-gate case ' ':
1187c478bd9Sstevel@tonic-gate case '\n':
1197c478bd9Sstevel@tonic-gate case 0377:
1207c478bd9Sstevel@tonic-gate case EOF:
1217c478bd9Sstevel@tonic-gate continue;
1227c478bd9Sstevel@tonic-gate case 'Y':
1237c478bd9Sstevel@tonic-gate sdump("stk", *stkptr);
1247c478bd9Sstevel@tonic-gate printf(gettext
1257c478bd9Sstevel@tonic-gate ("all %ld rel %ld headmor %ld\n"), all, rel,
1267c478bd9Sstevel@tonic-gate headmor);
1277c478bd9Sstevel@tonic-gate printf(gettext("nbytes %ld\n"), nbytes);
1287c478bd9Sstevel@tonic-gate printf(gettext
1297c478bd9Sstevel@tonic-gate ("longest %ld active %ld maxsize %ld\n"), longest,
1307c478bd9Sstevel@tonic-gate active, maxsize);
1317c478bd9Sstevel@tonic-gate printf(gettext
1327c478bd9Sstevel@tonic-gate ("new all %d rel %d copy %d more %d lbytes %d\n"),
1337c478bd9Sstevel@tonic-gate lall, lrel, lcopy, lmore, lbytes);
1347c478bd9Sstevel@tonic-gate lall = lrel = lcopy = lmore = lbytes = 0;
1357c478bd9Sstevel@tonic-gate continue;
1367c478bd9Sstevel@tonic-gate case '_':
1377c478bd9Sstevel@tonic-gate p = readin();
1387c478bd9Sstevel@tonic-gate savk = sunputc(p);
1397c478bd9Sstevel@tonic-gate chsign(p);
1407c478bd9Sstevel@tonic-gate sputc(p, savk);
1417c478bd9Sstevel@tonic-gate pushp(p);
1427c478bd9Sstevel@tonic-gate continue;
1437c478bd9Sstevel@tonic-gate case '-':
1447c478bd9Sstevel@tonic-gate subt();
1457c478bd9Sstevel@tonic-gate continue;
1467c478bd9Sstevel@tonic-gate case '+':
1477c478bd9Sstevel@tonic-gate if (eqk() != 0)
1487c478bd9Sstevel@tonic-gate continue;
1497c478bd9Sstevel@tonic-gate binop('+');
1507c478bd9Sstevel@tonic-gate continue;
1517c478bd9Sstevel@tonic-gate case '*':
1527c478bd9Sstevel@tonic-gate arg1 = pop();
1537c478bd9Sstevel@tonic-gate EMPTY;
1547c478bd9Sstevel@tonic-gate arg2 = pop();
1557c478bd9Sstevel@tonic-gate EMPTYR(arg1);
1567c478bd9Sstevel@tonic-gate sk1 = sunputc(arg1);
1577c478bd9Sstevel@tonic-gate sk2 = sunputc(arg2);
1587c478bd9Sstevel@tonic-gate binop('*');
1597c478bd9Sstevel@tonic-gate p = pop();
1607c478bd9Sstevel@tonic-gate sunputc(p);
1617c478bd9Sstevel@tonic-gate savk = n = sk1 + sk2;
1627c478bd9Sstevel@tonic-gate if (n > k && n > sk1 && n > sk2) {
1637c478bd9Sstevel@tonic-gate sk = sk1;
1647c478bd9Sstevel@tonic-gate if (sk < sk2)
1657c478bd9Sstevel@tonic-gate sk = sk2;
1667c478bd9Sstevel@tonic-gate if (sk < k)
1677c478bd9Sstevel@tonic-gate sk = k;
1687c478bd9Sstevel@tonic-gate p = removc(p, n - sk);
1697c478bd9Sstevel@tonic-gate savk = sk;
1707c478bd9Sstevel@tonic-gate }
1717c478bd9Sstevel@tonic-gate sputc(p, savk);
1727c478bd9Sstevel@tonic-gate pushp(p);
1737c478bd9Sstevel@tonic-gate continue;
1747c478bd9Sstevel@tonic-gate case '/':
1757c478bd9Sstevel@tonic-gate casediv:
1767c478bd9Sstevel@tonic-gate if (dscale() != 0)
1777c478bd9Sstevel@tonic-gate continue;
1787c478bd9Sstevel@tonic-gate binop('/');
1797c478bd9Sstevel@tonic-gate if (irem != 0)
1807c478bd9Sstevel@tonic-gate release(irem);
1817c478bd9Sstevel@tonic-gate release(rem);
1827c478bd9Sstevel@tonic-gate continue;
1837c478bd9Sstevel@tonic-gate case '%':
1847c478bd9Sstevel@tonic-gate if (dscale() != 0)
1857c478bd9Sstevel@tonic-gate continue;
1867c478bd9Sstevel@tonic-gate binop('/');
1877c478bd9Sstevel@tonic-gate p = pop();
1887c478bd9Sstevel@tonic-gate release(p);
1897c478bd9Sstevel@tonic-gate if (irem == 0) {
1907c478bd9Sstevel@tonic-gate sputc(rem, skr + k);
1917c478bd9Sstevel@tonic-gate pushp(rem);
1927c478bd9Sstevel@tonic-gate continue;
1937c478bd9Sstevel@tonic-gate }
1947c478bd9Sstevel@tonic-gate p = add0(rem, skd - (skr + k));
1957c478bd9Sstevel@tonic-gate q = add(p, irem);
1967c478bd9Sstevel@tonic-gate release(p);
1977c478bd9Sstevel@tonic-gate release(irem);
1987c478bd9Sstevel@tonic-gate sputc(q, skd);
1997c478bd9Sstevel@tonic-gate pushp(q);
2007c478bd9Sstevel@tonic-gate continue;
2017c478bd9Sstevel@tonic-gate case 'v':
2027c478bd9Sstevel@tonic-gate p = pop();
2037c478bd9Sstevel@tonic-gate EMPTY;
2047c478bd9Sstevel@tonic-gate savk = sunputc(p);
2057c478bd9Sstevel@tonic-gate if (length(p) == 0) {
2067c478bd9Sstevel@tonic-gate sputc(p, savk);
2077c478bd9Sstevel@tonic-gate pushp(p);
2087c478bd9Sstevel@tonic-gate continue;
2097c478bd9Sstevel@tonic-gate }
2107c478bd9Sstevel@tonic-gate if ((c = sbackc(p)) < 0) {
2117c478bd9Sstevel@tonic-gate error(gettext("sqrt of neg number\n"));
2127c478bd9Sstevel@tonic-gate }
2137c478bd9Sstevel@tonic-gate if (k < savk)
2147c478bd9Sstevel@tonic-gate n = savk;
2157c478bd9Sstevel@tonic-gate else {
2167c478bd9Sstevel@tonic-gate n = k * 2 - savk;
2177c478bd9Sstevel@tonic-gate savk = k;
2187c478bd9Sstevel@tonic-gate }
2197c478bd9Sstevel@tonic-gate arg1 = add0(p, n);
2207c478bd9Sstevel@tonic-gate arg2 = sqrt(arg1);
2217c478bd9Sstevel@tonic-gate sputc(arg2, savk);
2227c478bd9Sstevel@tonic-gate pushp(arg2);
2237c478bd9Sstevel@tonic-gate continue;
2247c478bd9Sstevel@tonic-gate case '^':
2257c478bd9Sstevel@tonic-gate neg = 0;
2267c478bd9Sstevel@tonic-gate arg1 = pop();
2277c478bd9Sstevel@tonic-gate EMPTY;
2287c478bd9Sstevel@tonic-gate if (sunputc(arg1) != 0)
2297c478bd9Sstevel@tonic-gate error(gettext("exp not an integer\n"));
2307c478bd9Sstevel@tonic-gate arg2 = pop();
2317c478bd9Sstevel@tonic-gate EMPTYR(arg1);
2327c478bd9Sstevel@tonic-gate if (sfbeg(arg1) == 0 && sbackc(arg1) < 0) {
2337c478bd9Sstevel@tonic-gate neg++;
2347c478bd9Sstevel@tonic-gate chsign(arg1);
2357c478bd9Sstevel@tonic-gate }
2367c478bd9Sstevel@tonic-gate if (length(arg1) >= 3)
2377c478bd9Sstevel@tonic-gate error(gettext("exp too big\n"));
2387c478bd9Sstevel@tonic-gate savk = sunputc(arg2);
2397c478bd9Sstevel@tonic-gate p = exp(arg2, arg1);
2407c478bd9Sstevel@tonic-gate release(arg2);
2417c478bd9Sstevel@tonic-gate rewind(arg1);
2427c478bd9Sstevel@tonic-gate c = sgetc(arg1);
2437c478bd9Sstevel@tonic-gate if (c == EOF)
2447c478bd9Sstevel@tonic-gate c = 0;
2457c478bd9Sstevel@tonic-gate else if (sfeof(arg1) == 0)
2467c478bd9Sstevel@tonic-gate c = sgetc(arg1) * 100 + c;
2477c478bd9Sstevel@tonic-gate d = c * savk;
2487c478bd9Sstevel@tonic-gate release(arg1);
2497c478bd9Sstevel@tonic-gate if (k >= savk)
2507c478bd9Sstevel@tonic-gate n = k;
2517c478bd9Sstevel@tonic-gate else
2527c478bd9Sstevel@tonic-gate n = savk;
2537c478bd9Sstevel@tonic-gate if (n < d) {
2547c478bd9Sstevel@tonic-gate q = removc(p, d - n);
2557c478bd9Sstevel@tonic-gate sputc(q, n);
2567c478bd9Sstevel@tonic-gate pushp(q);
2577c478bd9Sstevel@tonic-gate } else {
2587c478bd9Sstevel@tonic-gate sputc(p, d);
2597c478bd9Sstevel@tonic-gate pushp(p);
2607c478bd9Sstevel@tonic-gate }
2617c478bd9Sstevel@tonic-gate if (neg == 0)
2627c478bd9Sstevel@tonic-gate continue;
2637c478bd9Sstevel@tonic-gate p = pop();
2647c478bd9Sstevel@tonic-gate q = salloc(2);
2657c478bd9Sstevel@tonic-gate sputc(q, 1);
2667c478bd9Sstevel@tonic-gate sputc(q, 0);
2677c478bd9Sstevel@tonic-gate pushp(q);
2687c478bd9Sstevel@tonic-gate pushp(p);
2697c478bd9Sstevel@tonic-gate goto casediv;
2707c478bd9Sstevel@tonic-gate case 'z':
2717c478bd9Sstevel@tonic-gate p = salloc(2);
2727c478bd9Sstevel@tonic-gate n = stkptr - stkbeg;
2737c478bd9Sstevel@tonic-gate if (n >= 100) {
2747c478bd9Sstevel@tonic-gate sputc(p, n / 100);
2757c478bd9Sstevel@tonic-gate n %= 100;
2767c478bd9Sstevel@tonic-gate }
2777c478bd9Sstevel@tonic-gate sputc(p, n);
2787c478bd9Sstevel@tonic-gate sputc(p, 0);
2797c478bd9Sstevel@tonic-gate pushp(p);
2807c478bd9Sstevel@tonic-gate continue;
2817c478bd9Sstevel@tonic-gate case 'Z':
2827c478bd9Sstevel@tonic-gate p = pop();
2837c478bd9Sstevel@tonic-gate EMPTY;
2847c478bd9Sstevel@tonic-gate n = (length(p) - 1) << 1;
2857c478bd9Sstevel@tonic-gate fsfile(p);
2867c478bd9Sstevel@tonic-gate sbackc(p);
2877c478bd9Sstevel@tonic-gate if (sfbeg(p) == 0) {
2887c478bd9Sstevel@tonic-gate if ((c = sbackc(p)) < 0) {
2897c478bd9Sstevel@tonic-gate n -= 2;
2907c478bd9Sstevel@tonic-gate if (sfbeg(p) == 1)
2917c478bd9Sstevel@tonic-gate n += 1;
2927c478bd9Sstevel@tonic-gate else {
2937c478bd9Sstevel@tonic-gate if ((c = sbackc(p)) == 0)
2947c478bd9Sstevel@tonic-gate n += 1;
2957c478bd9Sstevel@tonic-gate else if (c > 90)
2967c478bd9Sstevel@tonic-gate n -= 1;
2977c478bd9Sstevel@tonic-gate }
2987c478bd9Sstevel@tonic-gate } else
2997c478bd9Sstevel@tonic-gate if (c < 10)
3007c478bd9Sstevel@tonic-gate n -= 1;
3017c478bd9Sstevel@tonic-gate }
3027c478bd9Sstevel@tonic-gate release(p);
3037c478bd9Sstevel@tonic-gate q = salloc(1);
3047c478bd9Sstevel@tonic-gate if (n >= 100) {
3057c478bd9Sstevel@tonic-gate sputc(q, n%100);
3067c478bd9Sstevel@tonic-gate n /= 100;
3077c478bd9Sstevel@tonic-gate }
3087c478bd9Sstevel@tonic-gate sputc(q, n);
3097c478bd9Sstevel@tonic-gate sputc(q, 0);
3107c478bd9Sstevel@tonic-gate pushp(q);
3117c478bd9Sstevel@tonic-gate continue;
3127c478bd9Sstevel@tonic-gate case 'i':
3137c478bd9Sstevel@tonic-gate p = pop();
3147c478bd9Sstevel@tonic-gate EMPTY;
3157c478bd9Sstevel@tonic-gate p = scalint(p);
3167c478bd9Sstevel@tonic-gate
3177c478bd9Sstevel@tonic-gate /*
3187c478bd9Sstevel@tonic-gate * POSIX.2
3197c478bd9Sstevel@tonic-gate * input base must be between 2 and 16
3207c478bd9Sstevel@tonic-gate */
3217c478bd9Sstevel@tonic-gate n = length(p);
3227c478bd9Sstevel@tonic-gate q = copy(p, n);
3237c478bd9Sstevel@tonic-gate fsfile(q);
3247c478bd9Sstevel@tonic-gate c = sbackc(q);
3257c478bd9Sstevel@tonic-gate if (sfbeg(q) == 0)
3267c478bd9Sstevel@tonic-gate error(gettext("input base is too large\n"));
3277c478bd9Sstevel@tonic-gate if (c < 2)
3287c478bd9Sstevel@tonic-gate error(gettext("input base is too small\n"));
3297c478bd9Sstevel@tonic-gate if (c > 16)
3307c478bd9Sstevel@tonic-gate error(gettext("input base is too large\n"));
3317c478bd9Sstevel@tonic-gate release(q);
3327c478bd9Sstevel@tonic-gate
3337c478bd9Sstevel@tonic-gate release(inbas);
3347c478bd9Sstevel@tonic-gate inbas = p;
3357c478bd9Sstevel@tonic-gate continue;
3367c478bd9Sstevel@tonic-gate case 'I':
3377c478bd9Sstevel@tonic-gate p = copy(inbas, length(inbas) + 1);
3387c478bd9Sstevel@tonic-gate sputc(p, 0);
3397c478bd9Sstevel@tonic-gate pushp(p);
3407c478bd9Sstevel@tonic-gate continue;
3417c478bd9Sstevel@tonic-gate case 'o':
3427c478bd9Sstevel@tonic-gate p = pop();
3437c478bd9Sstevel@tonic-gate EMPTY;
3447c478bd9Sstevel@tonic-gate p = scalint(p);
3457c478bd9Sstevel@tonic-gate sign = 0;
3467c478bd9Sstevel@tonic-gate n = length(p);
3477c478bd9Sstevel@tonic-gate q = copy(p, n);
3487c478bd9Sstevel@tonic-gate fsfile(q);
3497c478bd9Sstevel@tonic-gate l = c = sbackc(q);
3507c478bd9Sstevel@tonic-gate if (n != 1) {
3517c478bd9Sstevel@tonic-gate if (c < 0) {
3527c478bd9Sstevel@tonic-gate sign = 1;
3537c478bd9Sstevel@tonic-gate chsign(q);
3547c478bd9Sstevel@tonic-gate n = length(q);
3557c478bd9Sstevel@tonic-gate fsfile(q);
3567c478bd9Sstevel@tonic-gate l = c = sbackc(q);
3577c478bd9Sstevel@tonic-gate }
3587c478bd9Sstevel@tonic-gate if (n != 1) {
3597c478bd9Sstevel@tonic-gate while (sfbeg(q) == 0)
3607c478bd9Sstevel@tonic-gate l = l * 100 + sbackc(q);
3617c478bd9Sstevel@tonic-gate }
3627c478bd9Sstevel@tonic-gate }
3637c478bd9Sstevel@tonic-gate
3647c478bd9Sstevel@tonic-gate /*
3657c478bd9Sstevel@tonic-gate * POSIX.2
3667c478bd9Sstevel@tonic-gate * Check that output base is less than or equal
3677c478bd9Sstevel@tonic-gate * BC_BASE_MAX.
3687c478bd9Sstevel@tonic-gate */
3697c478bd9Sstevel@tonic-gate if (l > BC_BASE_MAX)
3707c478bd9Sstevel@tonic-gate error(gettext("output base is too large\n"));
3717c478bd9Sstevel@tonic-gate
3727c478bd9Sstevel@tonic-gate logo = log2(l);
3737c478bd9Sstevel@tonic-gate obase = l;
3747c478bd9Sstevel@tonic-gate release(basptr);
3757c478bd9Sstevel@tonic-gate if (sign == 1)
3767c478bd9Sstevel@tonic-gate obase = -l;
3777c478bd9Sstevel@tonic-gate basptr = p;
3787c478bd9Sstevel@tonic-gate outdit = bigot;
3797c478bd9Sstevel@tonic-gate if (n == 1 && sign == 0) {
3807c478bd9Sstevel@tonic-gate if (c <= 16) {
3817c478bd9Sstevel@tonic-gate outdit = hexot;
3827c478bd9Sstevel@tonic-gate fw = 1;
3837c478bd9Sstevel@tonic-gate fw1 = 0;
3847c478bd9Sstevel@tonic-gate
3857c478bd9Sstevel@tonic-gate /*
3867c478bd9Sstevel@tonic-gate * POSIX.2
3877c478bd9Sstevel@tonic-gate * Line length is 70 characters,
3887c478bd9Sstevel@tonic-gate * including newline.
3897c478bd9Sstevel@tonic-gate */
3907c478bd9Sstevel@tonic-gate ll = 70;
3917c478bd9Sstevel@tonic-gate release(q);
3927c478bd9Sstevel@tonic-gate continue;
3937c478bd9Sstevel@tonic-gate }
3947c478bd9Sstevel@tonic-gate }
3957c478bd9Sstevel@tonic-gate n = 0;
3967c478bd9Sstevel@tonic-gate if (sign == 1)
3977c478bd9Sstevel@tonic-gate n++;
3987c478bd9Sstevel@tonic-gate p = salloc(1);
3997c478bd9Sstevel@tonic-gate sputc(p, -1);
4007c478bd9Sstevel@tonic-gate t = add(p, q);
4017c478bd9Sstevel@tonic-gate n += length(t) * 2;
4027c478bd9Sstevel@tonic-gate fsfile(t);
4037c478bd9Sstevel@tonic-gate if ((c = sbackc(t)) > 9)
4047c478bd9Sstevel@tonic-gate n++;
4057c478bd9Sstevel@tonic-gate release(t);
4067c478bd9Sstevel@tonic-gate release(q);
4077c478bd9Sstevel@tonic-gate release(p);
4087c478bd9Sstevel@tonic-gate fw = n;
4097c478bd9Sstevel@tonic-gate fw1 = n-1;
4107c478bd9Sstevel@tonic-gate
4117c478bd9Sstevel@tonic-gate /*
4127c478bd9Sstevel@tonic-gate * POSIX.2
4137c478bd9Sstevel@tonic-gate * Line length is 70 characters including newline
4147c478bd9Sstevel@tonic-gate */
4157c478bd9Sstevel@tonic-gate ll = 70;
4167c478bd9Sstevel@tonic-gate if (fw >= ll)
4177c478bd9Sstevel@tonic-gate continue;
4187c478bd9Sstevel@tonic-gate ll = (70 / fw) * fw;
4197c478bd9Sstevel@tonic-gate continue;
4207c478bd9Sstevel@tonic-gate case 'O':
4217c478bd9Sstevel@tonic-gate p = copy(basptr, length(basptr) + 1);
4227c478bd9Sstevel@tonic-gate sputc(p, 0);
4237c478bd9Sstevel@tonic-gate pushp(p);
4247c478bd9Sstevel@tonic-gate continue;
4257c478bd9Sstevel@tonic-gate case '[':
4267c478bd9Sstevel@tonic-gate n = 0;
4277c478bd9Sstevel@tonic-gate p = salloc(0);
4287c478bd9Sstevel@tonic-gate for (; ; ) {
4297c478bd9Sstevel@tonic-gate if ((c = readc()) == ']') {
4307c478bd9Sstevel@tonic-gate if (n == 0)
4317c478bd9Sstevel@tonic-gate break;
4327c478bd9Sstevel@tonic-gate n--;
4337c478bd9Sstevel@tonic-gate }
4347c478bd9Sstevel@tonic-gate sputc(p, c);
4357c478bd9Sstevel@tonic-gate if (c == '[')
4367c478bd9Sstevel@tonic-gate n++;
4377c478bd9Sstevel@tonic-gate }
4387c478bd9Sstevel@tonic-gate pushp(p);
4397c478bd9Sstevel@tonic-gate continue;
4407c478bd9Sstevel@tonic-gate case 'k':
4417c478bd9Sstevel@tonic-gate p = pop();
4427c478bd9Sstevel@tonic-gate EMPTY;
4437c478bd9Sstevel@tonic-gate p = scalint(p);
4447c478bd9Sstevel@tonic-gate
4457c478bd9Sstevel@tonic-gate /*
4467c478bd9Sstevel@tonic-gate * POSIX.2
4477c478bd9Sstevel@tonic-gate * Make sure scaling factor is between 0 and
4487c478bd9Sstevel@tonic-gate * BC_SCALE_MAX. Copy p to q and figure the
4497c478bd9Sstevel@tonic-gate * scaling factor.
4507c478bd9Sstevel@tonic-gate */
4517c478bd9Sstevel@tonic-gate n = length(p);
4527c478bd9Sstevel@tonic-gate q = copy(p, n);
4537c478bd9Sstevel@tonic-gate fsfile(q);
4547c478bd9Sstevel@tonic-gate c = 0;
4557c478bd9Sstevel@tonic-gate if ((sfbeg(q) == 0) && ((c = sbackc(q)) < 0))
4567c478bd9Sstevel@tonic-gate error(gettext("invalid scale factor\n"));
4577c478bd9Sstevel@tonic-gate
4587c478bd9Sstevel@tonic-gate scalev = 1;
4597c478bd9Sstevel@tonic-gate while (c < BC_SCALE_MAX && sfbeg(q) == 0)
4607c478bd9Sstevel@tonic-gate c = (c * (scalev *= 100)) + sbackc(q);
4617c478bd9Sstevel@tonic-gate
4627c478bd9Sstevel@tonic-gate if (c > BC_SCALE_MAX)
4637c478bd9Sstevel@tonic-gate error(gettext("scale factor is too large\n"));
4647c478bd9Sstevel@tonic-gate release(q);
4657c478bd9Sstevel@tonic-gate
4667c478bd9Sstevel@tonic-gate rewind(p);
4677c478bd9Sstevel@tonic-gate k = sfeof(p) ? 0 : sgetc(p);
4687c478bd9Sstevel@tonic-gate release(scalptr);
4697c478bd9Sstevel@tonic-gate scalptr = p;
4707c478bd9Sstevel@tonic-gate continue;
4717c478bd9Sstevel@tonic-gate
4727c478bd9Sstevel@tonic-gate case 'K':
4737c478bd9Sstevel@tonic-gate p = copy(scalptr, length(scalptr) + 1);
4747c478bd9Sstevel@tonic-gate sputc(p, 0);
4757c478bd9Sstevel@tonic-gate pushp(p);
4767c478bd9Sstevel@tonic-gate continue;
4777c478bd9Sstevel@tonic-gate case 'X':
4787c478bd9Sstevel@tonic-gate p = pop();
4797c478bd9Sstevel@tonic-gate EMPTY;
4807c478bd9Sstevel@tonic-gate fsfile(p);
4817c478bd9Sstevel@tonic-gate n = sbackc(p);
4827c478bd9Sstevel@tonic-gate release(p);
4837c478bd9Sstevel@tonic-gate p = salloc(2);
4847c478bd9Sstevel@tonic-gate sputc(p, n);
4857c478bd9Sstevel@tonic-gate sputc(p, 0);
4867c478bd9Sstevel@tonic-gate pushp(p);
4877c478bd9Sstevel@tonic-gate continue;
4887c478bd9Sstevel@tonic-gate case 'Q':
4897c478bd9Sstevel@tonic-gate p = pop();
4907c478bd9Sstevel@tonic-gate EMPTY;
4917c478bd9Sstevel@tonic-gate if (length(p) > 2) {
4927c478bd9Sstevel@tonic-gate error("Q?\n");
4937c478bd9Sstevel@tonic-gate }
4947c478bd9Sstevel@tonic-gate rewind(p);
4957c478bd9Sstevel@tonic-gate if ((c = sgetc(p)) < 0) {
4967c478bd9Sstevel@tonic-gate error(gettext("neg Q\n"));
4977c478bd9Sstevel@tonic-gate }
4987c478bd9Sstevel@tonic-gate release(p);
4997c478bd9Sstevel@tonic-gate while (c-- > 0) {
5007c478bd9Sstevel@tonic-gate if (readptr == &readstk[0]) {
5017c478bd9Sstevel@tonic-gate error("readstk?\n");
5027c478bd9Sstevel@tonic-gate }
5037c478bd9Sstevel@tonic-gate if (*readptr != 0)
5047c478bd9Sstevel@tonic-gate release(*readptr);
5057c478bd9Sstevel@tonic-gate readptr--;
5067c478bd9Sstevel@tonic-gate }
5077c478bd9Sstevel@tonic-gate continue;
5087c478bd9Sstevel@tonic-gate case 'q':
5097c478bd9Sstevel@tonic-gate if (readptr <= &readstk[1])
5107c478bd9Sstevel@tonic-gate exit(0);
5117c478bd9Sstevel@tonic-gate if (*readptr != 0)
5127c478bd9Sstevel@tonic-gate release(*readptr);
5137c478bd9Sstevel@tonic-gate readptr--;
5147c478bd9Sstevel@tonic-gate if (*readptr != 0)
5157c478bd9Sstevel@tonic-gate release(*readptr);
5167c478bd9Sstevel@tonic-gate readptr--;
5177c478bd9Sstevel@tonic-gate continue;
5187c478bd9Sstevel@tonic-gate case 'f':
5197c478bd9Sstevel@tonic-gate if (stkptr == &stack[0])
5207c478bd9Sstevel@tonic-gate printf(gettext("empty stack\n"));
5217c478bd9Sstevel@tonic-gate else {
5227c478bd9Sstevel@tonic-gate for (ptr = stkptr; ptr > &stack[0]; ) {
5237c478bd9Sstevel@tonic-gate print(*ptr--);
5247c478bd9Sstevel@tonic-gate }
5257c478bd9Sstevel@tonic-gate }
5267c478bd9Sstevel@tonic-gate continue;
5277c478bd9Sstevel@tonic-gate case 'p':
5287c478bd9Sstevel@tonic-gate if (stkptr == &stack[0])
5297c478bd9Sstevel@tonic-gate printf(gettext("empty stack\n"));
5307c478bd9Sstevel@tonic-gate else {
5317c478bd9Sstevel@tonic-gate print(*stkptr);
5327c478bd9Sstevel@tonic-gate }
5337c478bd9Sstevel@tonic-gate continue;
5347c478bd9Sstevel@tonic-gate case 'P':
5357c478bd9Sstevel@tonic-gate p = pop();
5367c478bd9Sstevel@tonic-gate EMPTY;
5377c478bd9Sstevel@tonic-gate sputc(p, 0);
5387c478bd9Sstevel@tonic-gate printf("%s", p->beg);
5397c478bd9Sstevel@tonic-gate release(p);
5407c478bd9Sstevel@tonic-gate continue;
5417c478bd9Sstevel@tonic-gate case 'd':
5427c478bd9Sstevel@tonic-gate if (stkptr == &stack[0]) {
5437c478bd9Sstevel@tonic-gate printf(gettext("empty stack\n"));
5447c478bd9Sstevel@tonic-gate continue;
5457c478bd9Sstevel@tonic-gate }
5467c478bd9Sstevel@tonic-gate q = *stkptr;
5477c478bd9Sstevel@tonic-gate n = length(q);
5487c478bd9Sstevel@tonic-gate p = copy(*stkptr, n);
5497c478bd9Sstevel@tonic-gate pushp(p);
5507c478bd9Sstevel@tonic-gate continue;
5517c478bd9Sstevel@tonic-gate case 'c':
5527c478bd9Sstevel@tonic-gate while (stkerr == 0) {
5537c478bd9Sstevel@tonic-gate p = pop();
5547c478bd9Sstevel@tonic-gate if (stkerr == 0)
5557c478bd9Sstevel@tonic-gate release(p);
5567c478bd9Sstevel@tonic-gate }
5577c478bd9Sstevel@tonic-gate continue;
5587c478bd9Sstevel@tonic-gate case 'S':
5597c478bd9Sstevel@tonic-gate if (stkptr == &stack[0]) {
5607c478bd9Sstevel@tonic-gate error(gettext("save: args\n"));
5617c478bd9Sstevel@tonic-gate }
5627c478bd9Sstevel@tonic-gate c = readc() & 0377;
5637c478bd9Sstevel@tonic-gate sptr = stable[c];
5647c478bd9Sstevel@tonic-gate sp = stable[c] = sfree;
5657c478bd9Sstevel@tonic-gate sfree = sfree->next;
5667c478bd9Sstevel@tonic-gate if (sfree == 0)
5677c478bd9Sstevel@tonic-gate goto sempty;
5687c478bd9Sstevel@tonic-gate sp->next = sptr;
5697c478bd9Sstevel@tonic-gate p = pop();
5707c478bd9Sstevel@tonic-gate EMPTY;
5717c478bd9Sstevel@tonic-gate if (c >= ARRAYST) {
5727c478bd9Sstevel@tonic-gate q = copy(p, length(p) + PTRSZ);
5737c478bd9Sstevel@tonic-gate for (n = 0; n < PTRSZ; n++) {
5747c478bd9Sstevel@tonic-gate sputc(q, 0);
5757c478bd9Sstevel@tonic-gate }
5767c478bd9Sstevel@tonic-gate release(p);
5777c478bd9Sstevel@tonic-gate p = q;
5787c478bd9Sstevel@tonic-gate }
5797c478bd9Sstevel@tonic-gate sp->val = p;
5807c478bd9Sstevel@tonic-gate continue;
5817c478bd9Sstevel@tonic-gate sempty:
5827c478bd9Sstevel@tonic-gate error(gettext("symbol table overflow\n"));
5837c478bd9Sstevel@tonic-gate case 's':
5847c478bd9Sstevel@tonic-gate if (stkptr == &stack[0]) {
5857c478bd9Sstevel@tonic-gate error(gettext("save:args\n"));
5867c478bd9Sstevel@tonic-gate }
5877c478bd9Sstevel@tonic-gate c = readc() & 0377;
5887c478bd9Sstevel@tonic-gate sptr = stable[c];
5897c478bd9Sstevel@tonic-gate if (sptr != 0) {
5907c478bd9Sstevel@tonic-gate p = sptr->val;
5917c478bd9Sstevel@tonic-gate if (c >= ARRAYST) {
5927c478bd9Sstevel@tonic-gate rewind(p);
5937c478bd9Sstevel@tonic-gate while (sfeof(p) == 0)
5947c478bd9Sstevel@tonic-gate release(getwd(p));
5957c478bd9Sstevel@tonic-gate }
5967c478bd9Sstevel@tonic-gate release(p);
5977c478bd9Sstevel@tonic-gate } else {
5987c478bd9Sstevel@tonic-gate sptr = stable[c] = sfree;
5997c478bd9Sstevel@tonic-gate sfree = sfree->next;
6007c478bd9Sstevel@tonic-gate if (sfree == 0)
6017c478bd9Sstevel@tonic-gate goto sempty;
6027c478bd9Sstevel@tonic-gate sptr->next = 0;
6037c478bd9Sstevel@tonic-gate }
6047c478bd9Sstevel@tonic-gate p = pop();
6057c478bd9Sstevel@tonic-gate sptr->val = p;
6067c478bd9Sstevel@tonic-gate continue;
6077c478bd9Sstevel@tonic-gate case 'l':
6087c478bd9Sstevel@tonic-gate load();
6097c478bd9Sstevel@tonic-gate continue;
6107c478bd9Sstevel@tonic-gate case 'L':
6117c478bd9Sstevel@tonic-gate c = readc() & 0377;
6127c478bd9Sstevel@tonic-gate sptr = stable[c];
6137c478bd9Sstevel@tonic-gate if (sptr == 0) {
6147c478bd9Sstevel@tonic-gate error("L?\n");
6157c478bd9Sstevel@tonic-gate }
6167c478bd9Sstevel@tonic-gate stable[c] = sptr->next;
6177c478bd9Sstevel@tonic-gate sptr->next = sfree;
6187c478bd9Sstevel@tonic-gate sfree = sptr;
6197c478bd9Sstevel@tonic-gate p = sptr->val;
6207c478bd9Sstevel@tonic-gate if (c >= ARRAYST) {
6217c478bd9Sstevel@tonic-gate rewind(p);
6227c478bd9Sstevel@tonic-gate while (sfeof(p) == 0) {
6237c478bd9Sstevel@tonic-gate q = getwd(p);
6247c478bd9Sstevel@tonic-gate if (q != 0)
6257c478bd9Sstevel@tonic-gate release(q);
6267c478bd9Sstevel@tonic-gate }
6277c478bd9Sstevel@tonic-gate }
6287c478bd9Sstevel@tonic-gate pushp(p);
6297c478bd9Sstevel@tonic-gate continue;
6307c478bd9Sstevel@tonic-gate case ':':
6317c478bd9Sstevel@tonic-gate p = pop();
6327c478bd9Sstevel@tonic-gate EMPTY;
6337c478bd9Sstevel@tonic-gate q = scalint(p);
6347c478bd9Sstevel@tonic-gate fsfile(q);
6357c478bd9Sstevel@tonic-gate
6367c478bd9Sstevel@tonic-gate /*
6377c478bd9Sstevel@tonic-gate * POSIX.2
6387c478bd9Sstevel@tonic-gate * Make sure index is between 0 and BC_DIM_MAX-1
6397c478bd9Sstevel@tonic-gate */
6407c478bd9Sstevel@tonic-gate c = 0;
6417c478bd9Sstevel@tonic-gate if ((sfbeg(q) == 0) && ((c = sbackc(q)) < 0))
6427c478bd9Sstevel@tonic-gate error(gettext("invalid index\n"));
6437c478bd9Sstevel@tonic-gate scalev = 1;
6447c478bd9Sstevel@tonic-gate while (c < BC_DIM_MAX && sfbeg(q) == 0)
6457c478bd9Sstevel@tonic-gate c = (c * (scalev *= 100)) + sbackc(q);
6467c478bd9Sstevel@tonic-gate
6477c478bd9Sstevel@tonic-gate if (c >= BC_DIM_MAX)
6487c478bd9Sstevel@tonic-gate error(gettext("index is too large\n"));
6497c478bd9Sstevel@tonic-gate
6507c478bd9Sstevel@tonic-gate release(q);
6517c478bd9Sstevel@tonic-gate n = readc() & 0377;
6527c478bd9Sstevel@tonic-gate sptr = stable[n];
6537c478bd9Sstevel@tonic-gate if (sptr == 0) {
6547c478bd9Sstevel@tonic-gate sptr = stable[n] = sfree;
6557c478bd9Sstevel@tonic-gate sfree = sfree->next;
6567c478bd9Sstevel@tonic-gate if (sfree == 0)
6577c478bd9Sstevel@tonic-gate goto sempty;
6587c478bd9Sstevel@tonic-gate sptr->next = 0;
6597c478bd9Sstevel@tonic-gate p = salloc((c + PTRSZ) * PTRSZ);
6607c478bd9Sstevel@tonic-gate zero(p);
6617c478bd9Sstevel@tonic-gate } else {
6627c478bd9Sstevel@tonic-gate p = sptr->val;
6637c478bd9Sstevel@tonic-gate if (length(p) - PTRSZ < c * PTRSZ) {
6647c478bd9Sstevel@tonic-gate q = copy(p, (c + PTRSZ) * PTRSZ);
6657c478bd9Sstevel@tonic-gate release(p);
6667c478bd9Sstevel@tonic-gate p = q;
6677c478bd9Sstevel@tonic-gate }
6687c478bd9Sstevel@tonic-gate }
6697c478bd9Sstevel@tonic-gate seekc(p, c * PTRSZ);
6707c478bd9Sstevel@tonic-gate q = lookwd(p);
6717c478bd9Sstevel@tonic-gate if (q != NULL)
6727c478bd9Sstevel@tonic-gate release(q);
6737c478bd9Sstevel@tonic-gate s = pop();
6747c478bd9Sstevel@tonic-gate EMPTY;
6757c478bd9Sstevel@tonic-gate salterwd((struct wblk *)p, s);
6767c478bd9Sstevel@tonic-gate sptr->val = p;
6777c478bd9Sstevel@tonic-gate continue;
6787c478bd9Sstevel@tonic-gate
6797c478bd9Sstevel@tonic-gate case ';':
6807c478bd9Sstevel@tonic-gate p = pop();
6817c478bd9Sstevel@tonic-gate EMPTY;
6827c478bd9Sstevel@tonic-gate q = scalint(p);
6837c478bd9Sstevel@tonic-gate fsfile(q);
6847c478bd9Sstevel@tonic-gate
6857c478bd9Sstevel@tonic-gate /*
6867c478bd9Sstevel@tonic-gate * POSIX.2
6877c478bd9Sstevel@tonic-gate * Make sure index is between 0 and BC_DIM_MAX-1
6887c478bd9Sstevel@tonic-gate */
6897c478bd9Sstevel@tonic-gate c = 0;
6907c478bd9Sstevel@tonic-gate if ((sfbeg(q) == 0) && ((c = sbackc(q)) < 0))
6917c478bd9Sstevel@tonic-gate error(gettext("invalid index\n"));
6927c478bd9Sstevel@tonic-gate scalev = 1;
6937c478bd9Sstevel@tonic-gate while (c < BC_DIM_MAX && sfbeg(q) == 0)
6947c478bd9Sstevel@tonic-gate c = (c * (scalev *= 100)) + sbackc(q);
6957c478bd9Sstevel@tonic-gate
6967c478bd9Sstevel@tonic-gate if (c >= BC_DIM_MAX)
6977c478bd9Sstevel@tonic-gate error(gettext("index is too large\n"));
6987c478bd9Sstevel@tonic-gate
6997c478bd9Sstevel@tonic-gate release(q);
7007c478bd9Sstevel@tonic-gate n = readc() & 0377;
7017c478bd9Sstevel@tonic-gate sptr = stable[n];
7027c478bd9Sstevel@tonic-gate if (sptr != 0) {
7037c478bd9Sstevel@tonic-gate p = sptr->val;
7047c478bd9Sstevel@tonic-gate if (length(p) - PTRSZ >= c * PTRSZ) {
7057c478bd9Sstevel@tonic-gate seekc(p, c * PTRSZ);
7067c478bd9Sstevel@tonic-gate s = getwd(p);
7077c478bd9Sstevel@tonic-gate if (s != 0) {
7087c478bd9Sstevel@tonic-gate q = copy(s, length(s));
7097c478bd9Sstevel@tonic-gate pushp(q);
7107c478bd9Sstevel@tonic-gate continue;
7117c478bd9Sstevel@tonic-gate }
7127c478bd9Sstevel@tonic-gate }
7137c478bd9Sstevel@tonic-gate }
7147c478bd9Sstevel@tonic-gate q = salloc(1); /* uninitializd array elt prints as 0 */
7157c478bd9Sstevel@tonic-gate sputc(q, 0);
7167c478bd9Sstevel@tonic-gate pushp(q);
7177c478bd9Sstevel@tonic-gate continue;
7187c478bd9Sstevel@tonic-gate case 'x':
7197c478bd9Sstevel@tonic-gate execute:
7207c478bd9Sstevel@tonic-gate p = pop();
7217c478bd9Sstevel@tonic-gate EMPTY;
7227c478bd9Sstevel@tonic-gate if ((readptr != &readstk[0]) && (*readptr != 0)) {
7237c478bd9Sstevel@tonic-gate if ((*readptr)->rd == (*readptr)->wt)
7247c478bd9Sstevel@tonic-gate release(*readptr);
7257c478bd9Sstevel@tonic-gate else {
7267c478bd9Sstevel@tonic-gate if (readptr++ == &readstk[RDSKSZ]) {
7277c478bd9Sstevel@tonic-gate error(gettext
7287c478bd9Sstevel@tonic-gate ("nesting depth\n"));
7297c478bd9Sstevel@tonic-gate }
7307c478bd9Sstevel@tonic-gate }
7317c478bd9Sstevel@tonic-gate } else
7327c478bd9Sstevel@tonic-gate readptr++;
7337c478bd9Sstevel@tonic-gate *readptr = p;
7347c478bd9Sstevel@tonic-gate if (p != 0)
7357c478bd9Sstevel@tonic-gate rewind(p);
7367c478bd9Sstevel@tonic-gate else {
7377c478bd9Sstevel@tonic-gate if ((c = readc()) != '\n')
7387c478bd9Sstevel@tonic-gate unreadc(c);
7397c478bd9Sstevel@tonic-gate }
7407c478bd9Sstevel@tonic-gate continue;
7417c478bd9Sstevel@tonic-gate case '?':
7427c478bd9Sstevel@tonic-gate if (++readptr == &readstk[RDSKSZ]) {
7437c478bd9Sstevel@tonic-gate error(gettext("nesting depth\n"));
7447c478bd9Sstevel@tonic-gate }
7457c478bd9Sstevel@tonic-gate *readptr = 0;
7467c478bd9Sstevel@tonic-gate fsave = curfile;
7477c478bd9Sstevel@tonic-gate curfile = stdin;
7487c478bd9Sstevel@tonic-gate while ((c = readc()) == '!')
7497c478bd9Sstevel@tonic-gate command();
7507c478bd9Sstevel@tonic-gate p = salloc(0);
7517c478bd9Sstevel@tonic-gate sputc(p, c);
7527c478bd9Sstevel@tonic-gate while ((c = readc()) != '\n') {
7537c478bd9Sstevel@tonic-gate sputc(p, c);
7547c478bd9Sstevel@tonic-gate if (c == '\\')
7557c478bd9Sstevel@tonic-gate sputc(p, readc());
7567c478bd9Sstevel@tonic-gate }
7577c478bd9Sstevel@tonic-gate curfile = fsave;
7587c478bd9Sstevel@tonic-gate *readptr = p;
7597c478bd9Sstevel@tonic-gate continue;
7607c478bd9Sstevel@tonic-gate case '!':
7617c478bd9Sstevel@tonic-gate if (command() == 1)
7627c478bd9Sstevel@tonic-gate goto execute;
7637c478bd9Sstevel@tonic-gate continue;
7647c478bd9Sstevel@tonic-gate case '<':
7657c478bd9Sstevel@tonic-gate case '>':
7667c478bd9Sstevel@tonic-gate case '=':
7677c478bd9Sstevel@tonic-gate if (cond(c) == 1)
7687c478bd9Sstevel@tonic-gate goto execute;
7697c478bd9Sstevel@tonic-gate continue;
7707c478bd9Sstevel@tonic-gate default:
7717c478bd9Sstevel@tonic-gate printf(gettext("%o is unimplemented\n"), c);
7727c478bd9Sstevel@tonic-gate }
7737c478bd9Sstevel@tonic-gate }
7747c478bd9Sstevel@tonic-gate }
7757c478bd9Sstevel@tonic-gate
7767c478bd9Sstevel@tonic-gate struct blk *
dcdiv(struct blk * ddivd,struct blk * ddivr)777*b390fe2cSmuffin dcdiv(struct blk *ddivd, struct blk *ddivr)
7787c478bd9Sstevel@tonic-gate {
7797c478bd9Sstevel@tonic-gate int divsign, remsign, offset, divcarry;
7807c478bd9Sstevel@tonic-gate int carry, dig, magic, d, dd, under;
7817c478bd9Sstevel@tonic-gate long c, td, cc;
7827c478bd9Sstevel@tonic-gate struct blk *ps, *px;
7837c478bd9Sstevel@tonic-gate struct blk *p, *divd, *divr;
7847c478bd9Sstevel@tonic-gate
7857c478bd9Sstevel@tonic-gate rem = 0;
7867c478bd9Sstevel@tonic-gate p = salloc(0);
7877c478bd9Sstevel@tonic-gate if (length(ddivr) == 0) {
7887c478bd9Sstevel@tonic-gate pushp(ddivr);
7897c478bd9Sstevel@tonic-gate printf(gettext("divide by 0\n"));
7907c478bd9Sstevel@tonic-gate return (p);
7917c478bd9Sstevel@tonic-gate }
7927c478bd9Sstevel@tonic-gate divsign = remsign = 0;
7937c478bd9Sstevel@tonic-gate divr = ddivr;
7947c478bd9Sstevel@tonic-gate fsfile(divr);
7957c478bd9Sstevel@tonic-gate if (sbackc(divr) == -1) {
7967c478bd9Sstevel@tonic-gate divr = copy(ddivr, length(ddivr));
7977c478bd9Sstevel@tonic-gate chsign(divr);
7987c478bd9Sstevel@tonic-gate divsign = ~divsign;
7997c478bd9Sstevel@tonic-gate }
8007c478bd9Sstevel@tonic-gate divd = copy(ddivd, length(ddivd));
8017c478bd9Sstevel@tonic-gate fsfile(divd);
8027c478bd9Sstevel@tonic-gate if (sfbeg(divd) == 0 && sbackc(divd) == -1) {
8037c478bd9Sstevel@tonic-gate chsign(divd);
8047c478bd9Sstevel@tonic-gate divsign = ~divsign;
8057c478bd9Sstevel@tonic-gate remsign = ~remsign;
8067c478bd9Sstevel@tonic-gate }
8077c478bd9Sstevel@tonic-gate offset = length(divd) - length(divr);
8087c478bd9Sstevel@tonic-gate if (offset < 0)
8097c478bd9Sstevel@tonic-gate goto ddone;
8107c478bd9Sstevel@tonic-gate seekc(p, offset + 1);
8117c478bd9Sstevel@tonic-gate sputc(divd, 0);
8127c478bd9Sstevel@tonic-gate magic = 0;
8137c478bd9Sstevel@tonic-gate fsfile(divr);
8147c478bd9Sstevel@tonic-gate c = sbackc(divr);
8157c478bd9Sstevel@tonic-gate if (c < 10)
8167c478bd9Sstevel@tonic-gate magic++;
8177c478bd9Sstevel@tonic-gate c = c * 100 + (sfbeg(divr)?0:sbackc(divr));
8187c478bd9Sstevel@tonic-gate if (magic > 0) {
8197c478bd9Sstevel@tonic-gate c = (c * 100 +(sfbeg(divr)?0:sbackc(divr)))*2;
8207c478bd9Sstevel@tonic-gate c /= 25;
8217c478bd9Sstevel@tonic-gate }
8227c478bd9Sstevel@tonic-gate while (offset >= 0) {
8237c478bd9Sstevel@tonic-gate fsfile(divd);
8247c478bd9Sstevel@tonic-gate td = sbackc(divd) * 100;
8257c478bd9Sstevel@tonic-gate dd = sfbeg(divd)?0:sbackc(divd);
8267c478bd9Sstevel@tonic-gate td = (td + dd) * 100;
8277c478bd9Sstevel@tonic-gate dd = sfbeg(divd)?0:sbackc(divd);
8287c478bd9Sstevel@tonic-gate td = td + dd;
8297c478bd9Sstevel@tonic-gate cc = c;
8307c478bd9Sstevel@tonic-gate if (offset == 0)
8317c478bd9Sstevel@tonic-gate td++;
8327c478bd9Sstevel@tonic-gate else
8337c478bd9Sstevel@tonic-gate cc++;
8347c478bd9Sstevel@tonic-gate if (magic != 0)
8357c478bd9Sstevel@tonic-gate td = td<<3;
8367c478bd9Sstevel@tonic-gate dig = td/cc;
8377c478bd9Sstevel@tonic-gate under = 0;
8387c478bd9Sstevel@tonic-gate if (td%cc < 8 && dig > 0 && magic) {
8397c478bd9Sstevel@tonic-gate dig--;
8407c478bd9Sstevel@tonic-gate under = 1;
8417c478bd9Sstevel@tonic-gate }
8427c478bd9Sstevel@tonic-gate rewind(divr);
8437c478bd9Sstevel@tonic-gate rewind(divxyz);
8447c478bd9Sstevel@tonic-gate carry = 0;
8457c478bd9Sstevel@tonic-gate while (sfeof(divr) == 0) {
8467c478bd9Sstevel@tonic-gate d = sgetc(divr) * dig + carry;
8477c478bd9Sstevel@tonic-gate carry = d / 100;
8487c478bd9Sstevel@tonic-gate salterc(divxyz, d % 100);
8497c478bd9Sstevel@tonic-gate }
8507c478bd9Sstevel@tonic-gate salterc(divxyz, carry);
8517c478bd9Sstevel@tonic-gate rewind(divxyz);
8527c478bd9Sstevel@tonic-gate seekc(divd, offset);
8537c478bd9Sstevel@tonic-gate carry = 0;
8547c478bd9Sstevel@tonic-gate while (sfeof(divd) == 0) {
8557c478bd9Sstevel@tonic-gate d = slookc(divd);
8567c478bd9Sstevel@tonic-gate d = d - (sfeof(divxyz) ? 0 : sgetc(divxyz)) - carry;
8577c478bd9Sstevel@tonic-gate carry = 0;
8587c478bd9Sstevel@tonic-gate if (d < 0) {
8597c478bd9Sstevel@tonic-gate d += 100;
8607c478bd9Sstevel@tonic-gate carry = 1;
8617c478bd9Sstevel@tonic-gate }
8627c478bd9Sstevel@tonic-gate salterc(divd, d);
8637c478bd9Sstevel@tonic-gate }
8647c478bd9Sstevel@tonic-gate divcarry = carry;
8657c478bd9Sstevel@tonic-gate sbackc(p);
8667c478bd9Sstevel@tonic-gate salterc(p, dig);
8677c478bd9Sstevel@tonic-gate sbackc(p);
8687c478bd9Sstevel@tonic-gate fsfile(divd);
8697c478bd9Sstevel@tonic-gate d = sbackc(divd);
8707c478bd9Sstevel@tonic-gate if ((d != 0) && /* !divcarry */ (offset != 0)) {
8717c478bd9Sstevel@tonic-gate d = sbackc(divd) + 100;
8727c478bd9Sstevel@tonic-gate salterc(divd, d);
8737c478bd9Sstevel@tonic-gate }
8747c478bd9Sstevel@tonic-gate if (--offset >= 0) {
8757c478bd9Sstevel@tonic-gate divd->wt--;
8767c478bd9Sstevel@tonic-gate }
8777c478bd9Sstevel@tonic-gate }
8787c478bd9Sstevel@tonic-gate if (under) { /* undershot last - adjust */
8797c478bd9Sstevel@tonic-gate px = copy(divr, length(divr)); /* 11/88 don't corrupt ddivr */
8807c478bd9Sstevel@tonic-gate chsign(px);
8817c478bd9Sstevel@tonic-gate ps = add(px, divd);
8827c478bd9Sstevel@tonic-gate fsfile(ps);
8837c478bd9Sstevel@tonic-gate if (length(ps) > 0 && sbackc(ps) < 0) {
8847c478bd9Sstevel@tonic-gate release(ps); /* only adjust in really undershot */
8857c478bd9Sstevel@tonic-gate } else {
8867c478bd9Sstevel@tonic-gate release(divd);
8877c478bd9Sstevel@tonic-gate salterc(p, dig + 1);
8887c478bd9Sstevel@tonic-gate divd = ps;
8897c478bd9Sstevel@tonic-gate }
8907c478bd9Sstevel@tonic-gate }
8917c478bd9Sstevel@tonic-gate if (divcarry != 0) {
8927c478bd9Sstevel@tonic-gate salterc(p, dig - 1);
8937c478bd9Sstevel@tonic-gate salterc(divd, -1);
8947c478bd9Sstevel@tonic-gate ps = add(divr, divd);
8957c478bd9Sstevel@tonic-gate release(divd);
8967c478bd9Sstevel@tonic-gate divd = ps;
8977c478bd9Sstevel@tonic-gate }
8987c478bd9Sstevel@tonic-gate
8997c478bd9Sstevel@tonic-gate rewind(p);
9007c478bd9Sstevel@tonic-gate divcarry = 0;
9017c478bd9Sstevel@tonic-gate while (sfeof(p) == 0) {
9027c478bd9Sstevel@tonic-gate d = slookc(p) + divcarry;
9037c478bd9Sstevel@tonic-gate divcarry = 0;
9047c478bd9Sstevel@tonic-gate if (d >= 100) {
9057c478bd9Sstevel@tonic-gate d -= 100;
9067c478bd9Sstevel@tonic-gate divcarry = 1;
9077c478bd9Sstevel@tonic-gate }
9087c478bd9Sstevel@tonic-gate salterc(p, d);
9097c478bd9Sstevel@tonic-gate }
9107c478bd9Sstevel@tonic-gate if (divcarry != 0)
9117c478bd9Sstevel@tonic-gate salterc(p, divcarry);
9127c478bd9Sstevel@tonic-gate fsfile(p);
9137c478bd9Sstevel@tonic-gate while (sfbeg(p) == 0) {
9147c478bd9Sstevel@tonic-gate if (sbackc(p) == 0)
9157c478bd9Sstevel@tonic-gate truncate(p);
9167c478bd9Sstevel@tonic-gate else break;
9177c478bd9Sstevel@tonic-gate }
9187c478bd9Sstevel@tonic-gate if (divsign < 0)
9197c478bd9Sstevel@tonic-gate chsign(p);
9207c478bd9Sstevel@tonic-gate fsfile(divd);
9217c478bd9Sstevel@tonic-gate while (sfbeg(divd) == 0) {
9227c478bd9Sstevel@tonic-gate if (sbackc(divd) == 0)
9237c478bd9Sstevel@tonic-gate truncate(divd);
9247c478bd9Sstevel@tonic-gate else break;
9257c478bd9Sstevel@tonic-gate }
9267c478bd9Sstevel@tonic-gate ddone:
9277c478bd9Sstevel@tonic-gate if (remsign < 0)
9287c478bd9Sstevel@tonic-gate chsign(divd);
9297c478bd9Sstevel@tonic-gate if (divr != ddivr)
9307c478bd9Sstevel@tonic-gate release(divr);
9317c478bd9Sstevel@tonic-gate rem = divd;
9327c478bd9Sstevel@tonic-gate return (p);
9337c478bd9Sstevel@tonic-gate }
9347c478bd9Sstevel@tonic-gate
9357c478bd9Sstevel@tonic-gate int
dscale(void)936*b390fe2cSmuffin dscale(void)
937*b390fe2cSmuffin {
9387c478bd9Sstevel@tonic-gate struct blk *dd, *dr, *r;
9397c478bd9Sstevel@tonic-gate int c;
9407c478bd9Sstevel@tonic-gate
9417c478bd9Sstevel@tonic-gate dr = pop();
9427c478bd9Sstevel@tonic-gate EMPTYS;
9437c478bd9Sstevel@tonic-gate dd = pop();
9447c478bd9Sstevel@tonic-gate EMPTYSR(dr);
9457c478bd9Sstevel@tonic-gate fsfile(dd);
9467c478bd9Sstevel@tonic-gate skd = sunputc(dd);
9477c478bd9Sstevel@tonic-gate fsfile(dr);
9487c478bd9Sstevel@tonic-gate skr = sunputc(dr);
9497c478bd9Sstevel@tonic-gate if (sfbeg(dr) == 1 || (sfbeg(dr) == 0 && sbackc(dr) == 0)) {
9507c478bd9Sstevel@tonic-gate sputc(dr, skr);
9517c478bd9Sstevel@tonic-gate pushp(dr);
9527c478bd9Sstevel@tonic-gate printf(gettext("divide by 0\n"));
9537c478bd9Sstevel@tonic-gate return (1);
9547c478bd9Sstevel@tonic-gate }
9557c478bd9Sstevel@tonic-gate if (sfbeg(dd) == 1 || (sfbeg(dd) == 0 && sbackc(dd) == 0)) {
9567c478bd9Sstevel@tonic-gate #ifdef XPG6
9577c478bd9Sstevel@tonic-gate sputc(dd, k);
9587c478bd9Sstevel@tonic-gate #else
9597c478bd9Sstevel@tonic-gate sputc(dd, skd);
9607c478bd9Sstevel@tonic-gate #endif
9617c478bd9Sstevel@tonic-gate pushp(dd);
9627c478bd9Sstevel@tonic-gate return (1);
9637c478bd9Sstevel@tonic-gate }
9647c478bd9Sstevel@tonic-gate c = k-skd+skr;
9657c478bd9Sstevel@tonic-gate if (c < 0)
9667c478bd9Sstevel@tonic-gate r = removr(dd, -c);
9677c478bd9Sstevel@tonic-gate else {
9687c478bd9Sstevel@tonic-gate r = add0(dd, c);
9697c478bd9Sstevel@tonic-gate irem = 0;
9707c478bd9Sstevel@tonic-gate }
9717c478bd9Sstevel@tonic-gate arg1 = r;
9727c478bd9Sstevel@tonic-gate arg2 = dr;
9737c478bd9Sstevel@tonic-gate savk = k;
9747c478bd9Sstevel@tonic-gate return (0);
9757c478bd9Sstevel@tonic-gate }
9767c478bd9Sstevel@tonic-gate
9777c478bd9Sstevel@tonic-gate struct blk *
removr(struct blk * p,int n)9787c478bd9Sstevel@tonic-gate removr(struct blk *p, int n)
9797c478bd9Sstevel@tonic-gate {
9807c478bd9Sstevel@tonic-gate int nn, neg;
9817c478bd9Sstevel@tonic-gate struct blk *q, *s, *r;
9827c478bd9Sstevel@tonic-gate fsfile(p);
9837c478bd9Sstevel@tonic-gate neg = sbackc(p);
9847c478bd9Sstevel@tonic-gate if (neg < 0)
9857c478bd9Sstevel@tonic-gate chsign(p);
9867c478bd9Sstevel@tonic-gate rewind(p);
9877c478bd9Sstevel@tonic-gate nn = (n + 1) / 2;
9887c478bd9Sstevel@tonic-gate q = salloc(nn);
9897c478bd9Sstevel@tonic-gate while (n > 1) {
9907c478bd9Sstevel@tonic-gate sputc(q, sgetc(p));
9917c478bd9Sstevel@tonic-gate n -= 2;
9927c478bd9Sstevel@tonic-gate }
9937c478bd9Sstevel@tonic-gate r = salloc(2);
9947c478bd9Sstevel@tonic-gate while (sfeof(p) == 0)
9957c478bd9Sstevel@tonic-gate sputc(r, sgetc(p));
9967c478bd9Sstevel@tonic-gate release(p);
9977c478bd9Sstevel@tonic-gate if (n == 1) {
998*b390fe2cSmuffin s = dcdiv(r, tenptr);
9997c478bd9Sstevel@tonic-gate release(r);
10007c478bd9Sstevel@tonic-gate rewind(rem);
10017c478bd9Sstevel@tonic-gate if (sfeof(rem) == 0)
10027c478bd9Sstevel@tonic-gate sputc(q, sgetc(rem));
10037c478bd9Sstevel@tonic-gate release(rem);
10047c478bd9Sstevel@tonic-gate if (neg < 0) {
10057c478bd9Sstevel@tonic-gate chsign(s);
10067c478bd9Sstevel@tonic-gate chsign(q);
10077c478bd9Sstevel@tonic-gate irem = q;
10087c478bd9Sstevel@tonic-gate return (s);
10097c478bd9Sstevel@tonic-gate }
10107c478bd9Sstevel@tonic-gate irem = q;
10117c478bd9Sstevel@tonic-gate return (s);
10127c478bd9Sstevel@tonic-gate }
10137c478bd9Sstevel@tonic-gate if (neg < 0) {
10147c478bd9Sstevel@tonic-gate chsign(r);
10157c478bd9Sstevel@tonic-gate chsign(q);
10167c478bd9Sstevel@tonic-gate irem = q;
10177c478bd9Sstevel@tonic-gate return (r);
10187c478bd9Sstevel@tonic-gate }
10197c478bd9Sstevel@tonic-gate irem = q;
10207c478bd9Sstevel@tonic-gate return (r);
10217c478bd9Sstevel@tonic-gate }
10227c478bd9Sstevel@tonic-gate
10237c478bd9Sstevel@tonic-gate struct blk *
sqrt(struct blk * p)10247c478bd9Sstevel@tonic-gate sqrt(struct blk *p)
10257c478bd9Sstevel@tonic-gate {
10267c478bd9Sstevel@tonic-gate struct blk *r, *q, *s, *t;
10277c478bd9Sstevel@tonic-gate int c, n, nn;
10287c478bd9Sstevel@tonic-gate
10297c478bd9Sstevel@tonic-gate n = length(p);
10307c478bd9Sstevel@tonic-gate fsfile(p);
10317c478bd9Sstevel@tonic-gate c = sbackc(p);
10327c478bd9Sstevel@tonic-gate if ((n & 1) != 1)
10337c478bd9Sstevel@tonic-gate c = c * 100 + (sfbeg(p) ? 0 : sbackc(p));
10347c478bd9Sstevel@tonic-gate n = (n + 1) >> 1;
10357c478bd9Sstevel@tonic-gate r = salloc(n);
10367c478bd9Sstevel@tonic-gate zero(r);
10377c478bd9Sstevel@tonic-gate seekc(r, n);
10387c478bd9Sstevel@tonic-gate nn = 1;
10397c478bd9Sstevel@tonic-gate while ((c -= nn) >= 0)
10407c478bd9Sstevel@tonic-gate nn += 2;
10417c478bd9Sstevel@tonic-gate c = (nn + 1) >> 1;
10427c478bd9Sstevel@tonic-gate fsfile(r);
10437c478bd9Sstevel@tonic-gate sbackc(r);
10447c478bd9Sstevel@tonic-gate if (c >= 100) {
10457c478bd9Sstevel@tonic-gate c -= 100;
10467c478bd9Sstevel@tonic-gate salterc(r, c);
10477c478bd9Sstevel@tonic-gate sputc(r, 1);
10487c478bd9Sstevel@tonic-gate } else
10497c478bd9Sstevel@tonic-gate salterc(r, c);
10507c478bd9Sstevel@tonic-gate for (; ; ) {
1051*b390fe2cSmuffin q = dcdiv(p, r);
10527c478bd9Sstevel@tonic-gate s = add(q, r);
10537c478bd9Sstevel@tonic-gate release(q);
10547c478bd9Sstevel@tonic-gate release(rem);
1055*b390fe2cSmuffin q = dcdiv(s, sqtemp);
10567c478bd9Sstevel@tonic-gate release(s);
10577c478bd9Sstevel@tonic-gate release(rem);
10587c478bd9Sstevel@tonic-gate s = copy(r, length(r));
10597c478bd9Sstevel@tonic-gate chsign(s);
10607c478bd9Sstevel@tonic-gate t = add(s, q);
10617c478bd9Sstevel@tonic-gate release(s);
10627c478bd9Sstevel@tonic-gate fsfile(t);
10637c478bd9Sstevel@tonic-gate nn = sfbeg(t) ? 0 : sbackc(t);
10647c478bd9Sstevel@tonic-gate if (nn >= 0)
10657c478bd9Sstevel@tonic-gate break;
10667c478bd9Sstevel@tonic-gate release(r);
10677c478bd9Sstevel@tonic-gate release(t);
10687c478bd9Sstevel@tonic-gate r = q;
10697c478bd9Sstevel@tonic-gate }
10707c478bd9Sstevel@tonic-gate release(t);
10717c478bd9Sstevel@tonic-gate release(q);
10727c478bd9Sstevel@tonic-gate release(p);
10737c478bd9Sstevel@tonic-gate return (r);
10747c478bd9Sstevel@tonic-gate }
10757c478bd9Sstevel@tonic-gate
10767c478bd9Sstevel@tonic-gate struct blk *
exp(struct blk * base,struct blk * ex)10777c478bd9Sstevel@tonic-gate exp(struct blk *base, struct blk *ex)
10787c478bd9Sstevel@tonic-gate {
10797c478bd9Sstevel@tonic-gate struct blk *r, *e, *p, *e1, *t, *cp;
10807c478bd9Sstevel@tonic-gate int temp, c, n;
10817c478bd9Sstevel@tonic-gate r = salloc(1);
10827c478bd9Sstevel@tonic-gate sputc(r, 1);
10837c478bd9Sstevel@tonic-gate p = copy(base, length(base));
10847c478bd9Sstevel@tonic-gate e = copy(ex, length(ex));
10857c478bd9Sstevel@tonic-gate fsfile(e);
10867c478bd9Sstevel@tonic-gate if (sfbeg(e) != 0)
10877c478bd9Sstevel@tonic-gate goto edone;
10887c478bd9Sstevel@tonic-gate temp = 0;
10897c478bd9Sstevel@tonic-gate c = sbackc(e);
10907c478bd9Sstevel@tonic-gate if (c < 0) {
10917c478bd9Sstevel@tonic-gate temp++;
10927c478bd9Sstevel@tonic-gate chsign(e);
10937c478bd9Sstevel@tonic-gate }
10947c478bd9Sstevel@tonic-gate while (length(e) != 0) {
1095*b390fe2cSmuffin e1 = dcdiv(e, sqtemp);
10967c478bd9Sstevel@tonic-gate release(e);
10977c478bd9Sstevel@tonic-gate e = e1;
10987c478bd9Sstevel@tonic-gate n = length(rem);
10997c478bd9Sstevel@tonic-gate release(rem);
11007c478bd9Sstevel@tonic-gate if (n != 0) {
11017c478bd9Sstevel@tonic-gate e1 = mult(p, r);
11027c478bd9Sstevel@tonic-gate release(r);
11037c478bd9Sstevel@tonic-gate r = e1;
11047c478bd9Sstevel@tonic-gate }
11057c478bd9Sstevel@tonic-gate t = copy(p, length(p));
11067c478bd9Sstevel@tonic-gate cp = mult(p, t);
11077c478bd9Sstevel@tonic-gate release(p);
11087c478bd9Sstevel@tonic-gate release(t);
11097c478bd9Sstevel@tonic-gate p = cp;
11107c478bd9Sstevel@tonic-gate }
11117c478bd9Sstevel@tonic-gate if (temp != 0) {
11127c478bd9Sstevel@tonic-gate if ((c = length(base)) == 0) {
11137c478bd9Sstevel@tonic-gate goto edone;
11147c478bd9Sstevel@tonic-gate }
11157c478bd9Sstevel@tonic-gate if (c > 1)
11167c478bd9Sstevel@tonic-gate create(r);
11177c478bd9Sstevel@tonic-gate else {
11187c478bd9Sstevel@tonic-gate rewind(base);
11197c478bd9Sstevel@tonic-gate if ((c = sgetc(base)) <= 1) {
11207c478bd9Sstevel@tonic-gate create(r);
11217c478bd9Sstevel@tonic-gate sputc(r, c);
11227c478bd9Sstevel@tonic-gate } else
11237c478bd9Sstevel@tonic-gate create(r);
11247c478bd9Sstevel@tonic-gate }
11257c478bd9Sstevel@tonic-gate }
11267c478bd9Sstevel@tonic-gate edone:
11277c478bd9Sstevel@tonic-gate release(p);
11287c478bd9Sstevel@tonic-gate release(e);
11297c478bd9Sstevel@tonic-gate return (r);
11307c478bd9Sstevel@tonic-gate }
11317c478bd9Sstevel@tonic-gate
11327c478bd9Sstevel@tonic-gate void
init(int argc,char ** argv)11337c478bd9Sstevel@tonic-gate init(int argc, char **argv)
11347c478bd9Sstevel@tonic-gate {
11357c478bd9Sstevel@tonic-gate struct sym *sp;
11367c478bd9Sstevel@tonic-gate char *dcmalloc();
11377c478bd9Sstevel@tonic-gate struct stat tsb;
11387c478bd9Sstevel@tonic-gate
11397c478bd9Sstevel@tonic-gate if (signal(SIGINT, SIG_IGN) != SIG_IGN)
11407c478bd9Sstevel@tonic-gate signal(SIGINT, onintr);
11417c478bd9Sstevel@tonic-gate setbuf(stdout, (char *)NULL);
11427c478bd9Sstevel@tonic-gate svargc = --argc;
11437c478bd9Sstevel@tonic-gate svargv = argv;
11447c478bd9Sstevel@tonic-gate while (svargc > 0 && svargv[1][0] == '-') {
11457c478bd9Sstevel@tonic-gate switch (svargv[1][1]) {
11467c478bd9Sstevel@tonic-gate default:
11477c478bd9Sstevel@tonic-gate dbg = 1;
11487c478bd9Sstevel@tonic-gate }
11497c478bd9Sstevel@tonic-gate svargc--;
11507c478bd9Sstevel@tonic-gate svargv++;
11517c478bd9Sstevel@tonic-gate }
11527c478bd9Sstevel@tonic-gate
11537c478bd9Sstevel@tonic-gate ifile = 1;
11547c478bd9Sstevel@tonic-gate
11557c478bd9Sstevel@tonic-gate if (svargc <= 0)
11567c478bd9Sstevel@tonic-gate curfile = stdin;
11577c478bd9Sstevel@tonic-gate else {
11587c478bd9Sstevel@tonic-gate if (stat(svargv[1], &tsb) < 0) {
11597c478bd9Sstevel@tonic-gate printf(gettext("Cannot stat %s: "), svargv[1]);
11607c478bd9Sstevel@tonic-gate perror("");
11617c478bd9Sstevel@tonic-gate exit(1);
11627c478bd9Sstevel@tonic-gate }
11637c478bd9Sstevel@tonic-gate
11647c478bd9Sstevel@tonic-gate if (S_ISREG(tsb.st_mode)) {
11657c478bd9Sstevel@tonic-gate if ((curfile = fopen(svargv[1], "r")) == NULL) {
11667c478bd9Sstevel@tonic-gate printf(gettext("can't open file %s\n"), \
11677c478bd9Sstevel@tonic-gate svargv[1]);
11687c478bd9Sstevel@tonic-gate exit(1);
11697c478bd9Sstevel@tonic-gate }
11707c478bd9Sstevel@tonic-gate } else {
11717c478bd9Sstevel@tonic-gate printf(gettext("invalid file type: %s\n"), \
11727c478bd9Sstevel@tonic-gate svargv[1]);
11737c478bd9Sstevel@tonic-gate exit(1);
11747c478bd9Sstevel@tonic-gate }
11757c478bd9Sstevel@tonic-gate }
11767c478bd9Sstevel@tonic-gate
11777c478bd9Sstevel@tonic-gate dummy = dcmalloc(0);
11787c478bd9Sstevel@tonic-gate scalptr = salloc(1);
11797c478bd9Sstevel@tonic-gate sputc(scalptr, 0);
11807c478bd9Sstevel@tonic-gate basptr = salloc(1);
11817c478bd9Sstevel@tonic-gate sputc(basptr, 10);
11827c478bd9Sstevel@tonic-gate obase = 10;
11837c478bd9Sstevel@tonic-gate log10 = log2(10L);
11847c478bd9Sstevel@tonic-gate
11857c478bd9Sstevel@tonic-gate /*
11867c478bd9Sstevel@tonic-gate * POSIX.2
11877c478bd9Sstevel@tonic-gate * default line length is 70 characters including newline
11887c478bd9Sstevel@tonic-gate */
11897c478bd9Sstevel@tonic-gate ll = 70;
11907c478bd9Sstevel@tonic-gate fw = 1;
11917c478bd9Sstevel@tonic-gate fw1 = 0;
11927c478bd9Sstevel@tonic-gate tenptr = salloc(1);
11937c478bd9Sstevel@tonic-gate sputc(tenptr, 10);
11947c478bd9Sstevel@tonic-gate obase = 10;
11957c478bd9Sstevel@tonic-gate inbas = salloc(1);
11967c478bd9Sstevel@tonic-gate sputc(inbas, 10);
11977c478bd9Sstevel@tonic-gate sqtemp = salloc(1);
11987c478bd9Sstevel@tonic-gate sputc(sqtemp, 2);
11997c478bd9Sstevel@tonic-gate chptr = salloc(0);
12007c478bd9Sstevel@tonic-gate strptr = salloc(0);
12017c478bd9Sstevel@tonic-gate divxyz = salloc(0);
12027c478bd9Sstevel@tonic-gate stkbeg = stkptr = &stack[0];
12037c478bd9Sstevel@tonic-gate stkend = &stack[STKSZ];
12047c478bd9Sstevel@tonic-gate stkerr = 0;
12057c478bd9Sstevel@tonic-gate readptr = &readstk[0];
12067c478bd9Sstevel@tonic-gate k = 0;
12077c478bd9Sstevel@tonic-gate sp = sptr = &symlst[0];
12087c478bd9Sstevel@tonic-gate while (sptr < &symlst[TBLSZ]) {
12097c478bd9Sstevel@tonic-gate sptr->next = ++sp;
12107c478bd9Sstevel@tonic-gate sptr++;
12117c478bd9Sstevel@tonic-gate }
12127c478bd9Sstevel@tonic-gate sptr->next = 0;
12137c478bd9Sstevel@tonic-gate sfree = &symlst[0];
12147c478bd9Sstevel@tonic-gate }
12157c478bd9Sstevel@tonic-gate
12167c478bd9Sstevel@tonic-gate void
onintr(int sig)12177c478bd9Sstevel@tonic-gate onintr(int sig)
12187c478bd9Sstevel@tonic-gate {
12197c478bd9Sstevel@tonic-gate
12207c478bd9Sstevel@tonic-gate signal(sig, onintr);
12217c478bd9Sstevel@tonic-gate while (readptr != &readstk[0]) {
12227c478bd9Sstevel@tonic-gate if (*readptr != 0)
12237c478bd9Sstevel@tonic-gate release(*readptr);
12247c478bd9Sstevel@tonic-gate readptr--;
12257c478bd9Sstevel@tonic-gate }
12267c478bd9Sstevel@tonic-gate curfile = stdin;
12277c478bd9Sstevel@tonic-gate commnds();
12287c478bd9Sstevel@tonic-gate }
12297c478bd9Sstevel@tonic-gate
12307c478bd9Sstevel@tonic-gate void
pushp(struct blk * p)12317c478bd9Sstevel@tonic-gate pushp(struct blk *p)
12327c478bd9Sstevel@tonic-gate {
12337c478bd9Sstevel@tonic-gate if (stkptr == stkend)
12347c478bd9Sstevel@tonic-gate printf(gettext("out of stack space\n"));
12357c478bd9Sstevel@tonic-gate else {
12367c478bd9Sstevel@tonic-gate stkerr = 0;
12377c478bd9Sstevel@tonic-gate *++stkptr = p;
12387c478bd9Sstevel@tonic-gate }
12397c478bd9Sstevel@tonic-gate }
12407c478bd9Sstevel@tonic-gate
12417c478bd9Sstevel@tonic-gate struct blk *
pop(void)1242*b390fe2cSmuffin pop(void)
1243*b390fe2cSmuffin {
12447c478bd9Sstevel@tonic-gate if (stkptr == stack) {
12457c478bd9Sstevel@tonic-gate stkerr = 1;
12467c478bd9Sstevel@tonic-gate return (0);
12477c478bd9Sstevel@tonic-gate }
12487c478bd9Sstevel@tonic-gate return (*stkptr--);
12497c478bd9Sstevel@tonic-gate }
12507c478bd9Sstevel@tonic-gate
12517c478bd9Sstevel@tonic-gate struct blk *
readin(void)1252*b390fe2cSmuffin readin(void)
1253*b390fe2cSmuffin {
12547c478bd9Sstevel@tonic-gate struct blk *p, *q;
12557c478bd9Sstevel@tonic-gate int dp, dpct;
12567c478bd9Sstevel@tonic-gate int c;
12577c478bd9Sstevel@tonic-gate
12587c478bd9Sstevel@tonic-gate dp = dpct = 0;
12597c478bd9Sstevel@tonic-gate p = salloc(0);
12607c478bd9Sstevel@tonic-gate for (; ; ) {
12617c478bd9Sstevel@tonic-gate c = readc();
12627c478bd9Sstevel@tonic-gate switch (c) {
12637c478bd9Sstevel@tonic-gate case '.':
12647c478bd9Sstevel@tonic-gate if (dp != 0)
12657c478bd9Sstevel@tonic-gate goto gotnum;
12667c478bd9Sstevel@tonic-gate dp++;
12677c478bd9Sstevel@tonic-gate continue;
12687c478bd9Sstevel@tonic-gate case '\\':
12697c478bd9Sstevel@tonic-gate readc();
12707c478bd9Sstevel@tonic-gate continue;
12717c478bd9Sstevel@tonic-gate default:
12727c478bd9Sstevel@tonic-gate if (c >= 'A' && c <= 'F')
12737c478bd9Sstevel@tonic-gate c = c - 'A' + 10;
12747c478bd9Sstevel@tonic-gate else
12757c478bd9Sstevel@tonic-gate if (c >= '0' && c <= '9')
12767c478bd9Sstevel@tonic-gate c -= '0';
12777c478bd9Sstevel@tonic-gate else
12787c478bd9Sstevel@tonic-gate goto gotnum;
12797c478bd9Sstevel@tonic-gate if (dp != 0) {
12807c478bd9Sstevel@tonic-gate if (dpct >= 99)
12817c478bd9Sstevel@tonic-gate continue;
12827c478bd9Sstevel@tonic-gate dpct++;
12837c478bd9Sstevel@tonic-gate }
12847c478bd9Sstevel@tonic-gate create(chptr);
12857c478bd9Sstevel@tonic-gate if (c != 0)
12867c478bd9Sstevel@tonic-gate sputc(chptr, c);
12877c478bd9Sstevel@tonic-gate q = mult(p, inbas);
12887c478bd9Sstevel@tonic-gate release(p);
12897c478bd9Sstevel@tonic-gate p = add(chptr, q);
12907c478bd9Sstevel@tonic-gate release(q);
12917c478bd9Sstevel@tonic-gate }
12927c478bd9Sstevel@tonic-gate }
12937c478bd9Sstevel@tonic-gate gotnum:
12947c478bd9Sstevel@tonic-gate unreadc(c);
12957c478bd9Sstevel@tonic-gate if (dp == 0) {
12967c478bd9Sstevel@tonic-gate sputc(p, 0);
12977c478bd9Sstevel@tonic-gate return (p);
12987c478bd9Sstevel@tonic-gate } else {
12997c478bd9Sstevel@tonic-gate /* if not base 10, then scale fractional input to precision */
13007c478bd9Sstevel@tonic-gate if (((int)*(inbas->beg)) != 10) {
13017c478bd9Sstevel@tonic-gate while (dpct < k) {
13027c478bd9Sstevel@tonic-gate create(chptr);
13037c478bd9Sstevel@tonic-gate q = mult(p, inbas);
13047c478bd9Sstevel@tonic-gate release(p);
13057c478bd9Sstevel@tonic-gate p = add(chptr, q);
13067c478bd9Sstevel@tonic-gate release(q);
13077c478bd9Sstevel@tonic-gate dpct++;
13087c478bd9Sstevel@tonic-gate }
13097c478bd9Sstevel@tonic-gate }
13107c478bd9Sstevel@tonic-gate q = scale(p, dpct);
13117c478bd9Sstevel@tonic-gate return (q);
13127c478bd9Sstevel@tonic-gate }
13137c478bd9Sstevel@tonic-gate }
13147c478bd9Sstevel@tonic-gate
13157c478bd9Sstevel@tonic-gate /*
13167c478bd9Sstevel@tonic-gate * returns pointer to struct with ct 0's & p
13177c478bd9Sstevel@tonic-gate */
13187c478bd9Sstevel@tonic-gate struct blk *
add0(struct blk * p,int ct)13197c478bd9Sstevel@tonic-gate add0(struct blk *p, int ct)
13207c478bd9Sstevel@tonic-gate {
13217c478bd9Sstevel@tonic-gate struct blk *q, *t;
13227c478bd9Sstevel@tonic-gate
13237c478bd9Sstevel@tonic-gate q = salloc(length(p) + (ct + 1) / 2);
13247c478bd9Sstevel@tonic-gate while (ct > 1) {
13257c478bd9Sstevel@tonic-gate sputc(q, 0);
13267c478bd9Sstevel@tonic-gate ct -= 2;
13277c478bd9Sstevel@tonic-gate }
13287c478bd9Sstevel@tonic-gate rewind(p);
13297c478bd9Sstevel@tonic-gate while (sfeof(p) == 0) {
13307c478bd9Sstevel@tonic-gate sputc(q, sgetc(p));
13317c478bd9Sstevel@tonic-gate }
13327c478bd9Sstevel@tonic-gate release(p);
13337c478bd9Sstevel@tonic-gate if (ct == 1) {
13347c478bd9Sstevel@tonic-gate t = mult(tenptr, q);
13357c478bd9Sstevel@tonic-gate release(q);
13367c478bd9Sstevel@tonic-gate return (t);
13377c478bd9Sstevel@tonic-gate }
13387c478bd9Sstevel@tonic-gate return (q);
13397c478bd9Sstevel@tonic-gate }
13407c478bd9Sstevel@tonic-gate
13417c478bd9Sstevel@tonic-gate struct blk *
mult(struct blk * p,struct blk * q)13427c478bd9Sstevel@tonic-gate mult(struct blk *p, struct blk *q)
13437c478bd9Sstevel@tonic-gate {
13447c478bd9Sstevel@tonic-gate struct blk *mp, *mq, *mr;
13457c478bd9Sstevel@tonic-gate int sign, offset, carry;
13467c478bd9Sstevel@tonic-gate int cq, cp, mt, mcr;
13477c478bd9Sstevel@tonic-gate
13487c478bd9Sstevel@tonic-gate offset = sign = 0;
13497c478bd9Sstevel@tonic-gate fsfile(p);
13507c478bd9Sstevel@tonic-gate mp = p;
13517c478bd9Sstevel@tonic-gate if (sfbeg(p) == 0) {
13527c478bd9Sstevel@tonic-gate if (sbackc(p) < 0) {
13537c478bd9Sstevel@tonic-gate mp = copy(p, length(p));
13547c478bd9Sstevel@tonic-gate chsign(mp);
13557c478bd9Sstevel@tonic-gate sign = ~sign;
13567c478bd9Sstevel@tonic-gate }
13577c478bd9Sstevel@tonic-gate }
13587c478bd9Sstevel@tonic-gate fsfile(q);
13597c478bd9Sstevel@tonic-gate mq = q;
13607c478bd9Sstevel@tonic-gate if (sfbeg(q) == 0) {
13617c478bd9Sstevel@tonic-gate if (sbackc(q) < 0) {
13627c478bd9Sstevel@tonic-gate mq = copy(q, length(q));
13637c478bd9Sstevel@tonic-gate chsign(mq);
13647c478bd9Sstevel@tonic-gate sign = ~sign;
13657c478bd9Sstevel@tonic-gate }
13667c478bd9Sstevel@tonic-gate }
13677c478bd9Sstevel@tonic-gate mr = salloc(length(mp) + length(mq));
13687c478bd9Sstevel@tonic-gate zero(mr);
13697c478bd9Sstevel@tonic-gate rewind(mq);
13707c478bd9Sstevel@tonic-gate while (sfeof(mq) == 0) {
13717c478bd9Sstevel@tonic-gate cq = sgetc(mq);
13727c478bd9Sstevel@tonic-gate rewind(mp);
13737c478bd9Sstevel@tonic-gate rewind(mr);
13747c478bd9Sstevel@tonic-gate mr->rd += offset;
13757c478bd9Sstevel@tonic-gate carry = 0;
13767c478bd9Sstevel@tonic-gate while (sfeof(mp) == 0) {
13777c478bd9Sstevel@tonic-gate cp = sgetc(mp);
13787c478bd9Sstevel@tonic-gate mcr = sfeof(mr) ? 0 : slookc(mr);
13797c478bd9Sstevel@tonic-gate mt = cp*cq + carry + mcr;
13807c478bd9Sstevel@tonic-gate carry = mt / 100;
13817c478bd9Sstevel@tonic-gate salterc(mr, mt % 100);
13827c478bd9Sstevel@tonic-gate }
13837c478bd9Sstevel@tonic-gate offset++;
13847c478bd9Sstevel@tonic-gate if (carry != 0) {
13857c478bd9Sstevel@tonic-gate mcr = sfeof(mr) ? 0 : slookc(mr);
13867c478bd9Sstevel@tonic-gate salterc(mr, mcr + carry);
13877c478bd9Sstevel@tonic-gate }
13887c478bd9Sstevel@tonic-gate }
13897c478bd9Sstevel@tonic-gate if (sign < 0) {
13907c478bd9Sstevel@tonic-gate chsign(mr);
13917c478bd9Sstevel@tonic-gate }
13927c478bd9Sstevel@tonic-gate if (mp != p)
13937c478bd9Sstevel@tonic-gate release(mp);
13947c478bd9Sstevel@tonic-gate if (mq != q)
13957c478bd9Sstevel@tonic-gate release(mq);
13967c478bd9Sstevel@tonic-gate return (mr);
13977c478bd9Sstevel@tonic-gate }
13987c478bd9Sstevel@tonic-gate
13997c478bd9Sstevel@tonic-gate void
chsign(struct blk * p)14007c478bd9Sstevel@tonic-gate chsign(struct blk *p)
14017c478bd9Sstevel@tonic-gate {
14027c478bd9Sstevel@tonic-gate int carry;
14037c478bd9Sstevel@tonic-gate char ct;
14047c478bd9Sstevel@tonic-gate
14057c478bd9Sstevel@tonic-gate carry = 0;
14067c478bd9Sstevel@tonic-gate rewind(p);
14077c478bd9Sstevel@tonic-gate while (sfeof(p) == 0) {
14087c478bd9Sstevel@tonic-gate ct = 100 - slookc(p) - carry;
14097c478bd9Sstevel@tonic-gate carry = 1;
14107c478bd9Sstevel@tonic-gate if (ct >= 100) {
14117c478bd9Sstevel@tonic-gate ct -= 100;
14127c478bd9Sstevel@tonic-gate carry = 0;
14137c478bd9Sstevel@tonic-gate }
14147c478bd9Sstevel@tonic-gate salterc(p, ct);
14157c478bd9Sstevel@tonic-gate }
14167c478bd9Sstevel@tonic-gate if (carry != 0) {
14177c478bd9Sstevel@tonic-gate sputc(p, -1);
14187c478bd9Sstevel@tonic-gate fsfile(p);
14197c478bd9Sstevel@tonic-gate sbackc(p);
14207c478bd9Sstevel@tonic-gate ct = sbackc(p);
14217c478bd9Sstevel@tonic-gate if (ct == 99) {
14227c478bd9Sstevel@tonic-gate truncate(p);
14237c478bd9Sstevel@tonic-gate sputc(p, -1);
14247c478bd9Sstevel@tonic-gate }
14257c478bd9Sstevel@tonic-gate } else {
14267c478bd9Sstevel@tonic-gate fsfile(p);
14277c478bd9Sstevel@tonic-gate ct = sbackc(p);
14287c478bd9Sstevel@tonic-gate if (ct == 0)
14297c478bd9Sstevel@tonic-gate truncate(p);
14307c478bd9Sstevel@tonic-gate }
14317c478bd9Sstevel@tonic-gate }
14327c478bd9Sstevel@tonic-gate
14337c478bd9Sstevel@tonic-gate char
readc(void)1434*b390fe2cSmuffin readc(void)
1435*b390fe2cSmuffin {
14367c478bd9Sstevel@tonic-gate loop:
14377c478bd9Sstevel@tonic-gate if ((readptr != &readstk[0]) && (*readptr != 0)) {
14387c478bd9Sstevel@tonic-gate if (sfeof(*readptr) == 0)
14397c478bd9Sstevel@tonic-gate return (lastchar = sgetc(*readptr));
14407c478bd9Sstevel@tonic-gate release(*readptr);
14417c478bd9Sstevel@tonic-gate readptr--;
14427c478bd9Sstevel@tonic-gate goto loop;
14437c478bd9Sstevel@tonic-gate }
14447c478bd9Sstevel@tonic-gate lastchar = getc(curfile);
14457c478bd9Sstevel@tonic-gate if (lastchar != EOF)
14467c478bd9Sstevel@tonic-gate return (lastchar);
14477c478bd9Sstevel@tonic-gate if (readptr != &readptr[0]) {
14487c478bd9Sstevel@tonic-gate readptr--;
14497c478bd9Sstevel@tonic-gate if (*readptr == 0)
14507c478bd9Sstevel@tonic-gate curfile = stdin;
14517c478bd9Sstevel@tonic-gate goto loop;
14527c478bd9Sstevel@tonic-gate }
14537c478bd9Sstevel@tonic-gate if (curfile != stdin) {
14547c478bd9Sstevel@tonic-gate fclose(curfile);
14557c478bd9Sstevel@tonic-gate curfile = stdin;
14567c478bd9Sstevel@tonic-gate goto loop;
14577c478bd9Sstevel@tonic-gate }
14587c478bd9Sstevel@tonic-gate exit(0);
14597c478bd9Sstevel@tonic-gate }
14607c478bd9Sstevel@tonic-gate
14617c478bd9Sstevel@tonic-gate void
unreadc(char c)14627c478bd9Sstevel@tonic-gate unreadc(char c)
14637c478bd9Sstevel@tonic-gate {
14647c478bd9Sstevel@tonic-gate
14657c478bd9Sstevel@tonic-gate if ((readptr != &readstk[0]) && (*readptr != 0)) {
14667c478bd9Sstevel@tonic-gate sungetc(*readptr, c);
14677c478bd9Sstevel@tonic-gate } else
14687c478bd9Sstevel@tonic-gate ungetc(c, curfile);
14697c478bd9Sstevel@tonic-gate }
14707c478bd9Sstevel@tonic-gate
14717c478bd9Sstevel@tonic-gate void
binop(char c)14727c478bd9Sstevel@tonic-gate binop(char c)
14737c478bd9Sstevel@tonic-gate {
14747c478bd9Sstevel@tonic-gate struct blk *r;
14757c478bd9Sstevel@tonic-gate
14767c478bd9Sstevel@tonic-gate switch (c) {
14777c478bd9Sstevel@tonic-gate case '+':
14787c478bd9Sstevel@tonic-gate r = add(arg1, arg2);
14797c478bd9Sstevel@tonic-gate break;
14807c478bd9Sstevel@tonic-gate case '*':
14817c478bd9Sstevel@tonic-gate r = mult(arg1, arg2);
14827c478bd9Sstevel@tonic-gate break;
14837c478bd9Sstevel@tonic-gate case '/':
1484*b390fe2cSmuffin r = dcdiv(arg1, arg2);
14857c478bd9Sstevel@tonic-gate break;
14867c478bd9Sstevel@tonic-gate }
14877c478bd9Sstevel@tonic-gate release(arg1);
14887c478bd9Sstevel@tonic-gate release(arg2);
14897c478bd9Sstevel@tonic-gate sputc(r, savk);
14907c478bd9Sstevel@tonic-gate pushp(r);
14917c478bd9Sstevel@tonic-gate }
14927c478bd9Sstevel@tonic-gate
14937c478bd9Sstevel@tonic-gate void
print(struct blk * hptr)14947c478bd9Sstevel@tonic-gate print(struct blk *hptr)
14957c478bd9Sstevel@tonic-gate {
14967c478bd9Sstevel@tonic-gate struct blk *p, *q, *dec;
14977c478bd9Sstevel@tonic-gate int sc; /* scale */
14987c478bd9Sstevel@tonic-gate int dig, dout, ct;
14997c478bd9Sstevel@tonic-gate
15007c478bd9Sstevel@tonic-gate rewind(hptr);
15017c478bd9Sstevel@tonic-gate while (sfeof(hptr) == 0) {
15027c478bd9Sstevel@tonic-gate if (sgetc(hptr) > 99) {
15037c478bd9Sstevel@tonic-gate rewind(hptr);
15047c478bd9Sstevel@tonic-gate while (sfeof(hptr) == 0) {
15057c478bd9Sstevel@tonic-gate printf("%c", sgetc(hptr));
15067c478bd9Sstevel@tonic-gate }
15077c478bd9Sstevel@tonic-gate printf("\n");
15087c478bd9Sstevel@tonic-gate return;
15097c478bd9Sstevel@tonic-gate }
15107c478bd9Sstevel@tonic-gate }
15117c478bd9Sstevel@tonic-gate fsfile(hptr);
15127c478bd9Sstevel@tonic-gate sc = sbackc(hptr); /* read scale off end of blk */
15137c478bd9Sstevel@tonic-gate if (sfbeg(hptr) != 0) {
15147c478bd9Sstevel@tonic-gate printf("0\n");
15157c478bd9Sstevel@tonic-gate return;
15167c478bd9Sstevel@tonic-gate }
15177c478bd9Sstevel@tonic-gate count = ll;
15187c478bd9Sstevel@tonic-gate p = copy(hptr, length(hptr));
15197c478bd9Sstevel@tonic-gate sunputc(p);
15207c478bd9Sstevel@tonic-gate fsfile(p);
15217c478bd9Sstevel@tonic-gate if (sbackc(p) < 0) {
15227c478bd9Sstevel@tonic-gate chsign(p);
15237c478bd9Sstevel@tonic-gate OUTC('-');
15247c478bd9Sstevel@tonic-gate }
15257c478bd9Sstevel@tonic-gate if ((obase == 0) || (obase == -1)) {
15267c478bd9Sstevel@tonic-gate oneot(p, sc, 'd');
15277c478bd9Sstevel@tonic-gate return;
15287c478bd9Sstevel@tonic-gate }
15297c478bd9Sstevel@tonic-gate if (obase == 1) {
15307c478bd9Sstevel@tonic-gate oneot(p, sc, '1');
15317c478bd9Sstevel@tonic-gate return;
15327c478bd9Sstevel@tonic-gate }
15337c478bd9Sstevel@tonic-gate if (obase == 10) {
15347c478bd9Sstevel@tonic-gate tenot(p, sc);
15357c478bd9Sstevel@tonic-gate return;
15367c478bd9Sstevel@tonic-gate }
15377c478bd9Sstevel@tonic-gate create(strptr);
15387c478bd9Sstevel@tonic-gate dig = log10 * sc;
15397c478bd9Sstevel@tonic-gate dout = ((dig / 10) + dig) / logo;
15407c478bd9Sstevel@tonic-gate dec = getdec(p, sc);
15417c478bd9Sstevel@tonic-gate p = removc(p, sc);
15427c478bd9Sstevel@tonic-gate while (length(p) != 0) {
1543*b390fe2cSmuffin q = dcdiv(p, basptr);
15447c478bd9Sstevel@tonic-gate release(p);
15457c478bd9Sstevel@tonic-gate p = q;
15467c478bd9Sstevel@tonic-gate (*outdit)(rem, 0);
15477c478bd9Sstevel@tonic-gate if (obase > 16)
15487c478bd9Sstevel@tonic-gate sputc(strptr, ' ');
15497c478bd9Sstevel@tonic-gate }
15507c478bd9Sstevel@tonic-gate release(p);
15517c478bd9Sstevel@tonic-gate fsfile(strptr);
15527c478bd9Sstevel@tonic-gate while (sfbeg(strptr) == 0)
15537c478bd9Sstevel@tonic-gate OUTC(sbackc(strptr));
15547c478bd9Sstevel@tonic-gate if (sc == 0) {
15557c478bd9Sstevel@tonic-gate release(dec);
15567c478bd9Sstevel@tonic-gate printf("\n");
15577c478bd9Sstevel@tonic-gate return;
15587c478bd9Sstevel@tonic-gate }
15597c478bd9Sstevel@tonic-gate create(strptr);
15607c478bd9Sstevel@tonic-gate OUTC('.');
15617c478bd9Sstevel@tonic-gate ct = 0;
15627c478bd9Sstevel@tonic-gate do {
15637c478bd9Sstevel@tonic-gate if (ct != 0 && obase > 16)
15647c478bd9Sstevel@tonic-gate sputc(strptr, ' ');
15657c478bd9Sstevel@tonic-gate q = mult(basptr, dec);
15667c478bd9Sstevel@tonic-gate release(dec);
15677c478bd9Sstevel@tonic-gate dec = getdec(q, sc);
15687c478bd9Sstevel@tonic-gate p = removc(q, sc);
15697c478bd9Sstevel@tonic-gate (*outdit)(p, 1);
15707c478bd9Sstevel@tonic-gate } while (++ct < dout);
15717c478bd9Sstevel@tonic-gate release(dec);
15727c478bd9Sstevel@tonic-gate rewind(strptr);
15737c478bd9Sstevel@tonic-gate while (sfeof(strptr) == 0)
15747c478bd9Sstevel@tonic-gate OUTC(sgetc(strptr));
15757c478bd9Sstevel@tonic-gate printf("\n");
15767c478bd9Sstevel@tonic-gate }
15777c478bd9Sstevel@tonic-gate
15787c478bd9Sstevel@tonic-gate struct blk *
getdec(struct blk * p,int sc)15797c478bd9Sstevel@tonic-gate getdec(struct blk *p, int sc)
15807c478bd9Sstevel@tonic-gate {
15817c478bd9Sstevel@tonic-gate int cc;
15827c478bd9Sstevel@tonic-gate struct blk *q, *t, *s;
15837c478bd9Sstevel@tonic-gate
15847c478bd9Sstevel@tonic-gate rewind(p);
15857c478bd9Sstevel@tonic-gate if (length(p) * 2 < sc) {
15867c478bd9Sstevel@tonic-gate q = copy(p, length(p));
15877c478bd9Sstevel@tonic-gate return (q);
15887c478bd9Sstevel@tonic-gate }
15897c478bd9Sstevel@tonic-gate q = salloc(length(p));
15907c478bd9Sstevel@tonic-gate while (sc >= 1) {
15917c478bd9Sstevel@tonic-gate sputc(q, sgetc(p));
15927c478bd9Sstevel@tonic-gate sc -= 2;
15937c478bd9Sstevel@tonic-gate }
15947c478bd9Sstevel@tonic-gate if (sc != 0) {
15957c478bd9Sstevel@tonic-gate t = mult(q, tenptr);
15967c478bd9Sstevel@tonic-gate s = salloc(cc = length(q));
15977c478bd9Sstevel@tonic-gate release(q);
15987c478bd9Sstevel@tonic-gate rewind(t);
15997c478bd9Sstevel@tonic-gate while (cc-- > 0)
16007c478bd9Sstevel@tonic-gate sputc(s, sgetc(t));
16017c478bd9Sstevel@tonic-gate sputc(s, 0);
16027c478bd9Sstevel@tonic-gate release(t);
1603*b390fe2cSmuffin t = dcdiv(s, tenptr);
16047c478bd9Sstevel@tonic-gate release(s);
16057c478bd9Sstevel@tonic-gate release(rem);
16067c478bd9Sstevel@tonic-gate return (t);
16077c478bd9Sstevel@tonic-gate }
16087c478bd9Sstevel@tonic-gate return (q);
16097c478bd9Sstevel@tonic-gate }
16107c478bd9Sstevel@tonic-gate
16117c478bd9Sstevel@tonic-gate void
tenot(struct blk * p,int sc)16127c478bd9Sstevel@tonic-gate tenot(struct blk *p, int sc)
16137c478bd9Sstevel@tonic-gate {
16147c478bd9Sstevel@tonic-gate int c, f;
16157c478bd9Sstevel@tonic-gate
16167c478bd9Sstevel@tonic-gate fsfile(p);
16177c478bd9Sstevel@tonic-gate
16187c478bd9Sstevel@tonic-gate f = 0;
16197c478bd9Sstevel@tonic-gate
16207c478bd9Sstevel@tonic-gate /*
16217c478bd9Sstevel@tonic-gate * at this point, the number is stored as base 100 (two decimal
16227c478bd9Sstevel@tonic-gate * digits per char) stuck in a buf (character array) backwards.
16237c478bd9Sstevel@tonic-gate * sc indicates the scaling factor.
16247c478bd9Sstevel@tonic-gate */
16257c478bd9Sstevel@tonic-gate
16267c478bd9Sstevel@tonic-gate while ((sfbeg(p) == 0) && ((p->rd-p->beg-1)*2 >= sc)) {
16277c478bd9Sstevel@tonic-gate /*
16287c478bd9Sstevel@tonic-gate * get numbers from the buf until we are the beginning of
16297c478bd9Sstevel@tonic-gate * the buf (i.e., there are no more numbers) or the numbers
16307c478bd9Sstevel@tonic-gate * remaining fall within the scaled (to the right of the
16317c478bd9Sstevel@tonic-gate * decimal point) portion.
16327c478bd9Sstevel@tonic-gate */
16337c478bd9Sstevel@tonic-gate c = sbackc(p);
16347c478bd9Sstevel@tonic-gate
16357c478bd9Sstevel@tonic-gate /*
16367c478bd9Sstevel@tonic-gate * POSIX.2
16377c478bd9Sstevel@tonic-gate * as we output digits, we have to watch the line length (ll)
16387c478bd9Sstevel@tonic-gate * which should include a '\' and a newline.
16397c478bd9Sstevel@tonic-gate */
16407c478bd9Sstevel@tonic-gate if (c < 10) {
16417c478bd9Sstevel@tonic-gate /*
16427c478bd9Sstevel@tonic-gate * if the number is less than 10, we need to output
16437c478bd9Sstevel@tonic-gate * a space-holding '0' (unless this is the first time
16447c478bd9Sstevel@tonic-gate * through).
16457c478bd9Sstevel@tonic-gate */
16467c478bd9Sstevel@tonic-gate if (f == 1) {
16477c478bd9Sstevel@tonic-gate CHECKEND;
16487c478bd9Sstevel@tonic-gate printf("0");
16497c478bd9Sstevel@tonic-gate count--;
16507c478bd9Sstevel@tonic-gate }
16517c478bd9Sstevel@tonic-gate
16527c478bd9Sstevel@tonic-gate CHECKEND;
16537c478bd9Sstevel@tonic-gate printf("%d", c);
16547c478bd9Sstevel@tonic-gate count--;
16557c478bd9Sstevel@tonic-gate } else {
16567c478bd9Sstevel@tonic-gate CHECKEND;
16577c478bd9Sstevel@tonic-gate printf("%d", c / 10);
16587c478bd9Sstevel@tonic-gate count--;
16597c478bd9Sstevel@tonic-gate
16607c478bd9Sstevel@tonic-gate CHECKEND;
16617c478bd9Sstevel@tonic-gate printf("%d", c % 10);
16627c478bd9Sstevel@tonic-gate count--;
16637c478bd9Sstevel@tonic-gate }
16647c478bd9Sstevel@tonic-gate f = 1;
16657c478bd9Sstevel@tonic-gate }
16667c478bd9Sstevel@tonic-gate
16677c478bd9Sstevel@tonic-gate if (sc == 0) {
16687c478bd9Sstevel@tonic-gate /*
16697c478bd9Sstevel@tonic-gate * no scaling factor, so we must have exited loop because we
16707c478bd9Sstevel@tonic-gate * ran out of numbers.
16717c478bd9Sstevel@tonic-gate */
16727c478bd9Sstevel@tonic-gate printf("\n");
16737c478bd9Sstevel@tonic-gate release(p);
16747c478bd9Sstevel@tonic-gate return;
16757c478bd9Sstevel@tonic-gate }
16767c478bd9Sstevel@tonic-gate
16777c478bd9Sstevel@tonic-gate if ((p->rd - p->beg) * 2 > sc) {
16787c478bd9Sstevel@tonic-gate c = sbackc(p);
16797c478bd9Sstevel@tonic-gate
16807c478bd9Sstevel@tonic-gate CHECKEND;
16817c478bd9Sstevel@tonic-gate printf("%d", c / 10);
16827c478bd9Sstevel@tonic-gate count--;
16837c478bd9Sstevel@tonic-gate
16847c478bd9Sstevel@tonic-gate CHECKEND;
16857c478bd9Sstevel@tonic-gate printf(".");
16867c478bd9Sstevel@tonic-gate count--;
16877c478bd9Sstevel@tonic-gate
16887c478bd9Sstevel@tonic-gate CHECKEND;
16897c478bd9Sstevel@tonic-gate printf("%d", c % 10);
16907c478bd9Sstevel@tonic-gate count--;
16917c478bd9Sstevel@tonic-gate
16927c478bd9Sstevel@tonic-gate sc--;
16937c478bd9Sstevel@tonic-gate } else {
16947c478bd9Sstevel@tonic-gate CHECKEND;
16957c478bd9Sstevel@tonic-gate printf(".");
16967c478bd9Sstevel@tonic-gate count--;
16977c478bd9Sstevel@tonic-gate }
16987c478bd9Sstevel@tonic-gate
16997c478bd9Sstevel@tonic-gate if (sc > (p->rd - p->beg) * 2) {
17007c478bd9Sstevel@tonic-gate while (sc > (p->rd - p->beg) * 2) {
17017c478bd9Sstevel@tonic-gate CHECKEND;
17027c478bd9Sstevel@tonic-gate printf("0");
17037c478bd9Sstevel@tonic-gate count--;
17047c478bd9Sstevel@tonic-gate
17057c478bd9Sstevel@tonic-gate sc--;
17067c478bd9Sstevel@tonic-gate }
17077c478bd9Sstevel@tonic-gate }
17087c478bd9Sstevel@tonic-gate
17097c478bd9Sstevel@tonic-gate /* now go through the scaled portion of the number */
17107c478bd9Sstevel@tonic-gate while (sc > 1) {
17117c478bd9Sstevel@tonic-gate c = sbackc(p);
17127c478bd9Sstevel@tonic-gate if (c < 10) {
17137c478bd9Sstevel@tonic-gate CHECKEND;
17147c478bd9Sstevel@tonic-gate printf("0");
17157c478bd9Sstevel@tonic-gate count--;
17167c478bd9Sstevel@tonic-gate
17177c478bd9Sstevel@tonic-gate CHECKEND;
17187c478bd9Sstevel@tonic-gate printf("%d", c);
17197c478bd9Sstevel@tonic-gate count--;
17207c478bd9Sstevel@tonic-gate } else {
17217c478bd9Sstevel@tonic-gate CHECKEND;
17227c478bd9Sstevel@tonic-gate printf("%d", c / 10);
17237c478bd9Sstevel@tonic-gate count--;
17247c478bd9Sstevel@tonic-gate
17257c478bd9Sstevel@tonic-gate CHECKEND;
17267c478bd9Sstevel@tonic-gate printf("%d", c % 10);
17277c478bd9Sstevel@tonic-gate count--;
17287c478bd9Sstevel@tonic-gate }
17297c478bd9Sstevel@tonic-gate sc -= 2;
17307c478bd9Sstevel@tonic-gate }
17317c478bd9Sstevel@tonic-gate
17327c478bd9Sstevel@tonic-gate if (sc == 1) { /* just in case the scaling factor was odd */
17337c478bd9Sstevel@tonic-gate CHECKEND;
17347c478bd9Sstevel@tonic-gate printf("%d", sbackc(p) / 10);
17357c478bd9Sstevel@tonic-gate }
17367c478bd9Sstevel@tonic-gate
17377c478bd9Sstevel@tonic-gate printf("\n");
17387c478bd9Sstevel@tonic-gate release(p);
17397c478bd9Sstevel@tonic-gate }
17407c478bd9Sstevel@tonic-gate
17417c478bd9Sstevel@tonic-gate void
oneot(struct blk * p,int sc,char ch)17427c478bd9Sstevel@tonic-gate oneot(struct blk *p, int sc, char ch)
17437c478bd9Sstevel@tonic-gate {
17447c478bd9Sstevel@tonic-gate struct blk *q;
17457c478bd9Sstevel@tonic-gate
17467c478bd9Sstevel@tonic-gate q = removc(p, sc);
17477c478bd9Sstevel@tonic-gate create(strptr);
17487c478bd9Sstevel@tonic-gate sputc(strptr, -1);
17497c478bd9Sstevel@tonic-gate while (length(q) > 0) {
17507c478bd9Sstevel@tonic-gate p = add(strptr, q);
17517c478bd9Sstevel@tonic-gate release(q);
17527c478bd9Sstevel@tonic-gate q = p;
17537c478bd9Sstevel@tonic-gate OUTC(ch);
17547c478bd9Sstevel@tonic-gate }
17557c478bd9Sstevel@tonic-gate release(q);
17567c478bd9Sstevel@tonic-gate printf("\n");
17577c478bd9Sstevel@tonic-gate }
17587c478bd9Sstevel@tonic-gate
17597c478bd9Sstevel@tonic-gate void
hexot(struct blk * p,int flg)17607c478bd9Sstevel@tonic-gate hexot(struct blk *p, int flg)
17617c478bd9Sstevel@tonic-gate {
17627c478bd9Sstevel@tonic-gate int c;
17637c478bd9Sstevel@tonic-gate
17647c478bd9Sstevel@tonic-gate rewind(p);
17657c478bd9Sstevel@tonic-gate if (sfeof(p) != 0) {
17667c478bd9Sstevel@tonic-gate sputc(strptr, '0');
17677c478bd9Sstevel@tonic-gate release(p);
17687c478bd9Sstevel@tonic-gate return;
17697c478bd9Sstevel@tonic-gate }
17707c478bd9Sstevel@tonic-gate c = sgetc(p);
17717c478bd9Sstevel@tonic-gate release(p);
17727c478bd9Sstevel@tonic-gate if (c >= 16) {
17737c478bd9Sstevel@tonic-gate printf(gettext("hex digit > 16"));
17747c478bd9Sstevel@tonic-gate return;
17757c478bd9Sstevel@tonic-gate }
17767c478bd9Sstevel@tonic-gate sputc(strptr, c < 10 ? c + '0' : c - 10 + 'A');
17777c478bd9Sstevel@tonic-gate }
17787c478bd9Sstevel@tonic-gate
17797c478bd9Sstevel@tonic-gate void
bigot(struct blk * p,int flg)17807c478bd9Sstevel@tonic-gate bigot(struct blk *p, int flg)
17817c478bd9Sstevel@tonic-gate {
17827c478bd9Sstevel@tonic-gate struct blk *t, *q;
17837c478bd9Sstevel@tonic-gate int l;
17847c478bd9Sstevel@tonic-gate int neg;
17857c478bd9Sstevel@tonic-gate
17867c478bd9Sstevel@tonic-gate if (flg == 1)
17877c478bd9Sstevel@tonic-gate t = salloc(0);
17887c478bd9Sstevel@tonic-gate else {
17897c478bd9Sstevel@tonic-gate t = strptr;
17907c478bd9Sstevel@tonic-gate l = length(strptr) + fw - 1;
17917c478bd9Sstevel@tonic-gate }
17927c478bd9Sstevel@tonic-gate neg = 0;
17937c478bd9Sstevel@tonic-gate if (length(p) != 0) {
17947c478bd9Sstevel@tonic-gate fsfile(p);
17957c478bd9Sstevel@tonic-gate if (sbackc(p) < 0) {
17967c478bd9Sstevel@tonic-gate neg = 1;
17977c478bd9Sstevel@tonic-gate chsign(p);
17987c478bd9Sstevel@tonic-gate }
17997c478bd9Sstevel@tonic-gate while (length(p) != 0) {
1800*b390fe2cSmuffin q = dcdiv(p, tenptr);
18017c478bd9Sstevel@tonic-gate release(p);
18027c478bd9Sstevel@tonic-gate p = q;
18037c478bd9Sstevel@tonic-gate rewind(rem);
18047c478bd9Sstevel@tonic-gate sputc(t, sfeof(rem) ? '0' : sgetc(rem) + '0');
18057c478bd9Sstevel@tonic-gate release(rem);
18067c478bd9Sstevel@tonic-gate }
18077c478bd9Sstevel@tonic-gate }
18087c478bd9Sstevel@tonic-gate release(p);
18097c478bd9Sstevel@tonic-gate if (flg == 1) {
18107c478bd9Sstevel@tonic-gate l = fw1 - length(t);
18117c478bd9Sstevel@tonic-gate if (neg != 0) {
18127c478bd9Sstevel@tonic-gate l--;
18137c478bd9Sstevel@tonic-gate sputc(strptr, '-');
18147c478bd9Sstevel@tonic-gate }
18157c478bd9Sstevel@tonic-gate fsfile(t);
18167c478bd9Sstevel@tonic-gate while (l-- > 0)
18177c478bd9Sstevel@tonic-gate sputc(strptr, '0');
18187c478bd9Sstevel@tonic-gate while (sfbeg(t) == 0)
18197c478bd9Sstevel@tonic-gate sputc(strptr, sbackc(t));
18207c478bd9Sstevel@tonic-gate release(t);
18217c478bd9Sstevel@tonic-gate } else {
18227c478bd9Sstevel@tonic-gate l -= length(strptr);
18237c478bd9Sstevel@tonic-gate while (l-- > 0)
18247c478bd9Sstevel@tonic-gate sputc(strptr, '0');
18257c478bd9Sstevel@tonic-gate if (neg != 0) {
18267c478bd9Sstevel@tonic-gate sunputc(strptr);
18277c478bd9Sstevel@tonic-gate sputc(strptr, '-');
18287c478bd9Sstevel@tonic-gate }
18297c478bd9Sstevel@tonic-gate }
18307c478bd9Sstevel@tonic-gate }
18317c478bd9Sstevel@tonic-gate
18327c478bd9Sstevel@tonic-gate struct blk *
add(struct blk * a1,struct blk * a2)18337c478bd9Sstevel@tonic-gate add(struct blk *a1, struct blk *a2)
18347c478bd9Sstevel@tonic-gate {
18357c478bd9Sstevel@tonic-gate struct blk *p;
18367c478bd9Sstevel@tonic-gate int carry, n;
18377c478bd9Sstevel@tonic-gate int size;
18387c478bd9Sstevel@tonic-gate int c, n1, n2;
18397c478bd9Sstevel@tonic-gate
18407c478bd9Sstevel@tonic-gate size = length(a1) > length(a2) ? length(a1) : length(a2);
18417c478bd9Sstevel@tonic-gate p = salloc(size);
18427c478bd9Sstevel@tonic-gate rewind(a1);
18437c478bd9Sstevel@tonic-gate rewind(a2);
18447c478bd9Sstevel@tonic-gate carry = 0;
18457c478bd9Sstevel@tonic-gate while (--size >= 0) {
18467c478bd9Sstevel@tonic-gate n1 = sfeof(a1) ? 0 : sgetc(a1);
18477c478bd9Sstevel@tonic-gate n2 = sfeof(a2) ? 0 : sgetc(a2);
18487c478bd9Sstevel@tonic-gate n = n1 + n2 + carry;
18497c478bd9Sstevel@tonic-gate if (n >= 100) {
18507c478bd9Sstevel@tonic-gate carry = 1;
18517c478bd9Sstevel@tonic-gate n -= 100;
18527c478bd9Sstevel@tonic-gate } else
18537c478bd9Sstevel@tonic-gate if (n < 0) {
18547c478bd9Sstevel@tonic-gate carry = -1;
18557c478bd9Sstevel@tonic-gate n += 100;
18567c478bd9Sstevel@tonic-gate } else
18577c478bd9Sstevel@tonic-gate carry = 0;
18587c478bd9Sstevel@tonic-gate sputc(p, n);
18597c478bd9Sstevel@tonic-gate }
18607c478bd9Sstevel@tonic-gate if (carry != 0)
18617c478bd9Sstevel@tonic-gate sputc(p, carry);
18627c478bd9Sstevel@tonic-gate fsfile(p);
18637c478bd9Sstevel@tonic-gate if (sfbeg(p) == 0) {
18647c478bd9Sstevel@tonic-gate while (sfbeg(p) == 0 && (c = sbackc(p)) == 0);
18657c478bd9Sstevel@tonic-gate if (c != 0)
18667c478bd9Sstevel@tonic-gate salterc(p, c);
18677c478bd9Sstevel@tonic-gate truncate(p);
18687c478bd9Sstevel@tonic-gate }
18697c478bd9Sstevel@tonic-gate fsfile(p);
18707c478bd9Sstevel@tonic-gate if (sfbeg(p) == 0 && sbackc(p) == -1) {
18717c478bd9Sstevel@tonic-gate while ((c = sbackc(p)) == 99) {
18727c478bd9Sstevel@tonic-gate if (c == EOF)
18737c478bd9Sstevel@tonic-gate break;
18747c478bd9Sstevel@tonic-gate }
18757c478bd9Sstevel@tonic-gate sgetc(p);
18767c478bd9Sstevel@tonic-gate salterc(p, -1);
18777c478bd9Sstevel@tonic-gate truncate(p);
18787c478bd9Sstevel@tonic-gate }
18797c478bd9Sstevel@tonic-gate return (p);
18807c478bd9Sstevel@tonic-gate }
18817c478bd9Sstevel@tonic-gate
18827c478bd9Sstevel@tonic-gate int
eqk(void)1883*b390fe2cSmuffin eqk(void) {
18847c478bd9Sstevel@tonic-gate struct blk *p, *q;
18857c478bd9Sstevel@tonic-gate int skp, skq;
18867c478bd9Sstevel@tonic-gate
18877c478bd9Sstevel@tonic-gate p = pop();
18887c478bd9Sstevel@tonic-gate EMPTYS;
18897c478bd9Sstevel@tonic-gate q = pop();
18907c478bd9Sstevel@tonic-gate EMPTYSR(p);
18917c478bd9Sstevel@tonic-gate skp = sunputc(p);
18927c478bd9Sstevel@tonic-gate skq = sunputc(q);
18937c478bd9Sstevel@tonic-gate if (skp == skq) {
18947c478bd9Sstevel@tonic-gate arg1 = p;
18957c478bd9Sstevel@tonic-gate arg2 = q;
18967c478bd9Sstevel@tonic-gate savk = skp;
18977c478bd9Sstevel@tonic-gate return (0);
18987c478bd9Sstevel@tonic-gate } else
18997c478bd9Sstevel@tonic-gate if (skp < skq) {
19007c478bd9Sstevel@tonic-gate savk = skq;
19017c478bd9Sstevel@tonic-gate p = add0(p, skq - skp);
19027c478bd9Sstevel@tonic-gate } else {
19037c478bd9Sstevel@tonic-gate savk = skp;
19047c478bd9Sstevel@tonic-gate q = add0(q, skp - skq);
19057c478bd9Sstevel@tonic-gate }
19067c478bd9Sstevel@tonic-gate arg1 = p;
19077c478bd9Sstevel@tonic-gate arg2 = q;
19087c478bd9Sstevel@tonic-gate return (0);
19097c478bd9Sstevel@tonic-gate }
19107c478bd9Sstevel@tonic-gate
19117c478bd9Sstevel@tonic-gate struct blk *
removc(struct blk * p,int n)19127c478bd9Sstevel@tonic-gate removc(struct blk *p, int n)
19137c478bd9Sstevel@tonic-gate {
19147c478bd9Sstevel@tonic-gate struct blk *q, *r;
19157c478bd9Sstevel@tonic-gate
19167c478bd9Sstevel@tonic-gate rewind(p);
19177c478bd9Sstevel@tonic-gate while (n > 1) {
19187c478bd9Sstevel@tonic-gate sgetc(p);
19197c478bd9Sstevel@tonic-gate n -= 2;
19207c478bd9Sstevel@tonic-gate }
19217c478bd9Sstevel@tonic-gate q = salloc(2);
19227c478bd9Sstevel@tonic-gate while (sfeof(p) == 0)
19237c478bd9Sstevel@tonic-gate sputc(q, sgetc(p));
19247c478bd9Sstevel@tonic-gate if (n == 1) {
1925*b390fe2cSmuffin r = dcdiv(q, tenptr);
19267c478bd9Sstevel@tonic-gate release(q);
19277c478bd9Sstevel@tonic-gate release(rem);
19287c478bd9Sstevel@tonic-gate q = r;
19297c478bd9Sstevel@tonic-gate }
19307c478bd9Sstevel@tonic-gate release(p);
19317c478bd9Sstevel@tonic-gate return (q);
19327c478bd9Sstevel@tonic-gate }
19337c478bd9Sstevel@tonic-gate
19347c478bd9Sstevel@tonic-gate struct blk *
scalint(struct blk * p)19357c478bd9Sstevel@tonic-gate scalint(struct blk *p)
19367c478bd9Sstevel@tonic-gate {
19377c478bd9Sstevel@tonic-gate int n;
19387c478bd9Sstevel@tonic-gate
19397c478bd9Sstevel@tonic-gate n = sunputc(p);
19407c478bd9Sstevel@tonic-gate p = removc(p, n);
19417c478bd9Sstevel@tonic-gate return (p);
19427c478bd9Sstevel@tonic-gate }
19437c478bd9Sstevel@tonic-gate
19447c478bd9Sstevel@tonic-gate struct blk *
scale(struct blk * p,int n)19457c478bd9Sstevel@tonic-gate scale(struct blk *p, int n)
19467c478bd9Sstevel@tonic-gate {
19477c478bd9Sstevel@tonic-gate struct blk *q, *s, *t;
19487c478bd9Sstevel@tonic-gate
19497c478bd9Sstevel@tonic-gate t = add0(p, n);
19507c478bd9Sstevel@tonic-gate q = salloc(1);
19517c478bd9Sstevel@tonic-gate sputc(q, n);
19527c478bd9Sstevel@tonic-gate s = exp(inbas, q);
19537c478bd9Sstevel@tonic-gate release(q);
1954*b390fe2cSmuffin q = dcdiv(t, s);
19557c478bd9Sstevel@tonic-gate release(t);
19567c478bd9Sstevel@tonic-gate release(s);
19577c478bd9Sstevel@tonic-gate release(rem);
19587c478bd9Sstevel@tonic-gate sputc(q, n);
19597c478bd9Sstevel@tonic-gate return (q);
19607c478bd9Sstevel@tonic-gate }
19617c478bd9Sstevel@tonic-gate
19627c478bd9Sstevel@tonic-gate int
subt(void)1963*b390fe2cSmuffin subt(void)
1964*b390fe2cSmuffin {
19657c478bd9Sstevel@tonic-gate arg1 = pop();
19667c478bd9Sstevel@tonic-gate EMPTYS;
19677c478bd9Sstevel@tonic-gate savk = sunputc(arg1);
19687c478bd9Sstevel@tonic-gate chsign(arg1);
19697c478bd9Sstevel@tonic-gate sputc(arg1, savk);
19707c478bd9Sstevel@tonic-gate pushp(arg1);
19717c478bd9Sstevel@tonic-gate if (eqk() != 0)
19727c478bd9Sstevel@tonic-gate return (1);
19737c478bd9Sstevel@tonic-gate binop('+');
19747c478bd9Sstevel@tonic-gate return (0);
19757c478bd9Sstevel@tonic-gate }
19767c478bd9Sstevel@tonic-gate
19777c478bd9Sstevel@tonic-gate int
command(void)1978*b390fe2cSmuffin command(void)
1979*b390fe2cSmuffin {
19807c478bd9Sstevel@tonic-gate int c;
19817c478bd9Sstevel@tonic-gate char line[100], *sl;
19827c478bd9Sstevel@tonic-gate void (*savint)();
19837c478bd9Sstevel@tonic-gate pid_t pid, rpid;
19847c478bd9Sstevel@tonic-gate int retcode;
19857c478bd9Sstevel@tonic-gate
19867c478bd9Sstevel@tonic-gate switch (c = readc()) {
19877c478bd9Sstevel@tonic-gate case '<':
19887c478bd9Sstevel@tonic-gate return (cond(NL));
19897c478bd9Sstevel@tonic-gate case '>':
19907c478bd9Sstevel@tonic-gate return (cond(NG));
19917c478bd9Sstevel@tonic-gate case '=':
19927c478bd9Sstevel@tonic-gate return (cond(NE));
19937c478bd9Sstevel@tonic-gate default:
19947c478bd9Sstevel@tonic-gate sl = line;
19957c478bd9Sstevel@tonic-gate *sl++ = c;
19967c478bd9Sstevel@tonic-gate while ((c = readc()) != '\n')
19977c478bd9Sstevel@tonic-gate *sl++ = c;
19987c478bd9Sstevel@tonic-gate *sl = 0;
19997c478bd9Sstevel@tonic-gate if ((pid = fork()) == (pid_t)0) {
20007c478bd9Sstevel@tonic-gate execl("/usr/bin/sh", "sh", "-c", line, 0);
20017c478bd9Sstevel@tonic-gate exit(0100);
20027c478bd9Sstevel@tonic-gate }
20037c478bd9Sstevel@tonic-gate savint = signal(SIGINT, SIG_IGN);
20047c478bd9Sstevel@tonic-gate while ((rpid = wait(&retcode)) != pid && rpid != (pid_t)-1);
20057c478bd9Sstevel@tonic-gate signal(SIGINT, savint);
20067c478bd9Sstevel@tonic-gate printf(gettext("!\n"));
20077c478bd9Sstevel@tonic-gate return (0);
20087c478bd9Sstevel@tonic-gate }
20097c478bd9Sstevel@tonic-gate }
20107c478bd9Sstevel@tonic-gate
20117c478bd9Sstevel@tonic-gate int
cond(char c)20127c478bd9Sstevel@tonic-gate cond(char c)
20137c478bd9Sstevel@tonic-gate {
20147c478bd9Sstevel@tonic-gate struct blk *p;
20157c478bd9Sstevel@tonic-gate int cc;
20167c478bd9Sstevel@tonic-gate
20177c478bd9Sstevel@tonic-gate if (subt() != 0)
20187c478bd9Sstevel@tonic-gate return (1);
20197c478bd9Sstevel@tonic-gate p = pop();
20207c478bd9Sstevel@tonic-gate sunputc(p);
20217c478bd9Sstevel@tonic-gate if (length(p) == 0) {
20227c478bd9Sstevel@tonic-gate release(p);
20237c478bd9Sstevel@tonic-gate if (c == '<' || c == '>' || c == NE) {
20247c478bd9Sstevel@tonic-gate readc();
20257c478bd9Sstevel@tonic-gate return (0);
20267c478bd9Sstevel@tonic-gate }
20277c478bd9Sstevel@tonic-gate load();
20287c478bd9Sstevel@tonic-gate return (1);
20297c478bd9Sstevel@tonic-gate } else {
20307c478bd9Sstevel@tonic-gate if (c == '=') {
20317c478bd9Sstevel@tonic-gate release(p);
20327c478bd9Sstevel@tonic-gate readc();
20337c478bd9Sstevel@tonic-gate return (0);
20347c478bd9Sstevel@tonic-gate }
20357c478bd9Sstevel@tonic-gate }
20367c478bd9Sstevel@tonic-gate if (c == NE) {
20377c478bd9Sstevel@tonic-gate release(p);
20387c478bd9Sstevel@tonic-gate load();
20397c478bd9Sstevel@tonic-gate return (1);
20407c478bd9Sstevel@tonic-gate }
20417c478bd9Sstevel@tonic-gate fsfile(p);
20427c478bd9Sstevel@tonic-gate cc = sbackc(p);
20437c478bd9Sstevel@tonic-gate release(p);
20447c478bd9Sstevel@tonic-gate if ((cc < 0 && (c == '<' || c == NG)) ||
20457c478bd9Sstevel@tonic-gate (cc > 0) && (c == '>' || c == NL)) {
20467c478bd9Sstevel@tonic-gate readc();
20477c478bd9Sstevel@tonic-gate return (0);
20487c478bd9Sstevel@tonic-gate }
20497c478bd9Sstevel@tonic-gate load();
20507c478bd9Sstevel@tonic-gate return (1);
20517c478bd9Sstevel@tonic-gate }
20527c478bd9Sstevel@tonic-gate
20537c478bd9Sstevel@tonic-gate void
load(void)2054*b390fe2cSmuffin load(void)
2055*b390fe2cSmuffin {
20567c478bd9Sstevel@tonic-gate int c;
20577c478bd9Sstevel@tonic-gate struct blk *p, *q, *t, *s;
20587c478bd9Sstevel@tonic-gate
20597c478bd9Sstevel@tonic-gate c = readc() & 0377;
20607c478bd9Sstevel@tonic-gate sptr = stable[c];
20617c478bd9Sstevel@tonic-gate if (sptr != 0) {
20627c478bd9Sstevel@tonic-gate p = sptr->val;
20637c478bd9Sstevel@tonic-gate if (c >= ARRAYST) {
20647c478bd9Sstevel@tonic-gate q = salloc(length(p));
20657c478bd9Sstevel@tonic-gate rewind(p);
20667c478bd9Sstevel@tonic-gate while (sfeof(p) == 0) {
20677c478bd9Sstevel@tonic-gate s = getwd(p);
20687c478bd9Sstevel@tonic-gate if (s == 0)
20697c478bd9Sstevel@tonic-gate putwd(q, (struct blk *)NULL);
20707c478bd9Sstevel@tonic-gate else {
20717c478bd9Sstevel@tonic-gate t = copy(s, length(s));
20727c478bd9Sstevel@tonic-gate putwd(q, t);
20737c478bd9Sstevel@tonic-gate }
20747c478bd9Sstevel@tonic-gate }
20757c478bd9Sstevel@tonic-gate pushp(q);
20767c478bd9Sstevel@tonic-gate } else {
20777c478bd9Sstevel@tonic-gate q = copy(p, length(p));
20787c478bd9Sstevel@tonic-gate pushp(q);
20797c478bd9Sstevel@tonic-gate }
20807c478bd9Sstevel@tonic-gate } else {
20817c478bd9Sstevel@tonic-gate q = salloc(1);
20827c478bd9Sstevel@tonic-gate if (c <= LASTFUN) {
20837c478bd9Sstevel@tonic-gate printf(gettext
20847c478bd9Sstevel@tonic-gate ("function %c undefined\n"), c + 'a' - 1);
20857c478bd9Sstevel@tonic-gate sputc(q, 'c');
20867c478bd9Sstevel@tonic-gate sputc(q, '0');
20877c478bd9Sstevel@tonic-gate sputc(q, ' ');
20887c478bd9Sstevel@tonic-gate sputc(q, '1');
20897c478bd9Sstevel@tonic-gate sputc(q, 'Q');
20907c478bd9Sstevel@tonic-gate } else
20917c478bd9Sstevel@tonic-gate sputc(q, 0);
20927c478bd9Sstevel@tonic-gate pushp(q);
20937c478bd9Sstevel@tonic-gate }
20947c478bd9Sstevel@tonic-gate }
20957c478bd9Sstevel@tonic-gate
20967c478bd9Sstevel@tonic-gate int
log2(long n)20977c478bd9Sstevel@tonic-gate log2(long n)
20987c478bd9Sstevel@tonic-gate {
20997c478bd9Sstevel@tonic-gate int i;
21007c478bd9Sstevel@tonic-gate
21017c478bd9Sstevel@tonic-gate if (n == 0)
21027c478bd9Sstevel@tonic-gate return (0);
21037c478bd9Sstevel@tonic-gate i = 31;
21047c478bd9Sstevel@tonic-gate if (n < 0)
21057c478bd9Sstevel@tonic-gate return (i);
21067c478bd9Sstevel@tonic-gate while ((n = n << 1) > 0)
21077c478bd9Sstevel@tonic-gate i--;
21087c478bd9Sstevel@tonic-gate return (--i);
21097c478bd9Sstevel@tonic-gate }
21107c478bd9Sstevel@tonic-gate
21117c478bd9Sstevel@tonic-gate struct blk *
salloc(int size)21127c478bd9Sstevel@tonic-gate salloc(int size)
21137c478bd9Sstevel@tonic-gate {
21147c478bd9Sstevel@tonic-gate struct blk *hdr;
21157c478bd9Sstevel@tonic-gate char *ptr;
21167c478bd9Sstevel@tonic-gate char *dcmalloc();
21177c478bd9Sstevel@tonic-gate all++;
21187c478bd9Sstevel@tonic-gate lall++;
21197c478bd9Sstevel@tonic-gate if (all - rel > active)
21207c478bd9Sstevel@tonic-gate active = all - rel;
21217c478bd9Sstevel@tonic-gate nbytes += size;
21227c478bd9Sstevel@tonic-gate lbytes += size;
21237c478bd9Sstevel@tonic-gate if (nbytes > maxsize)
21247c478bd9Sstevel@tonic-gate maxsize = nbytes;
21257c478bd9Sstevel@tonic-gate if (size > longest)
21267c478bd9Sstevel@tonic-gate longest = size;
21277c478bd9Sstevel@tonic-gate ptr = dcmalloc((unsigned)size);
21287c478bd9Sstevel@tonic-gate if (ptr == 0) {
21297c478bd9Sstevel@tonic-gate garbage("salloc");
21307c478bd9Sstevel@tonic-gate if ((ptr = dcmalloc((unsigned)size)) == 0)
21317c478bd9Sstevel@tonic-gate ospace("salloc");
21327c478bd9Sstevel@tonic-gate }
21337c478bd9Sstevel@tonic-gate if ((hdr = hfree) == 0)
21347c478bd9Sstevel@tonic-gate hdr = morehd();
21357c478bd9Sstevel@tonic-gate hfree = (struct blk *)hdr->rd;
21367c478bd9Sstevel@tonic-gate hdr->rd = hdr->wt = hdr->beg = ptr;
21377c478bd9Sstevel@tonic-gate hdr->last = ptr + size;
21387c478bd9Sstevel@tonic-gate return (hdr);
21397c478bd9Sstevel@tonic-gate }
21407c478bd9Sstevel@tonic-gate
21417c478bd9Sstevel@tonic-gate struct blk *
morehd(void)2142*b390fe2cSmuffin morehd(void)
2143*b390fe2cSmuffin {
21447c478bd9Sstevel@tonic-gate struct blk *h, *kk;
21457c478bd9Sstevel@tonic-gate char *dcmalloc();
21467c478bd9Sstevel@tonic-gate
21477c478bd9Sstevel@tonic-gate headmor++;
21487c478bd9Sstevel@tonic-gate nbytes += HEADSZ;
21497c478bd9Sstevel@tonic-gate hfree = h = (struct blk *)dcmalloc(HEADSZ);
21507c478bd9Sstevel@tonic-gate if (hfree == 0) {
21517c478bd9Sstevel@tonic-gate garbage("morehd");
21527c478bd9Sstevel@tonic-gate if ((hfree = h = (struct blk *)dcmalloc(HEADSZ)) == 0)
21537c478bd9Sstevel@tonic-gate ospace("headers");
21547c478bd9Sstevel@tonic-gate }
21557c478bd9Sstevel@tonic-gate kk = h;
21567c478bd9Sstevel@tonic-gate while (h < hfree + (HEADSZ/BLK))
21577c478bd9Sstevel@tonic-gate (h++)->rd = (char *)++kk;
21587c478bd9Sstevel@tonic-gate (--h)->rd = 0;
21597c478bd9Sstevel@tonic-gate return (hfree);
21607c478bd9Sstevel@tonic-gate }
21617c478bd9Sstevel@tonic-gate
21627c478bd9Sstevel@tonic-gate struct blk *
copy(struct blk * hptr,int size)21637c478bd9Sstevel@tonic-gate copy(struct blk *hptr, int size)
21647c478bd9Sstevel@tonic-gate {
21657c478bd9Sstevel@tonic-gate struct blk *hdr;
21667c478bd9Sstevel@tonic-gate unsigned sz;
21677c478bd9Sstevel@tonic-gate char *ptr;
21687c478bd9Sstevel@tonic-gate
21697c478bd9Sstevel@tonic-gate all++;
21707c478bd9Sstevel@tonic-gate lall++;
21717c478bd9Sstevel@tonic-gate lcopy++;
21727c478bd9Sstevel@tonic-gate nbytes += size;
21737c478bd9Sstevel@tonic-gate lbytes += size;
21747c478bd9Sstevel@tonic-gate if (size > longest)
21757c478bd9Sstevel@tonic-gate longest = size;
21767c478bd9Sstevel@tonic-gate if (size > maxsize)
21777c478bd9Sstevel@tonic-gate maxsize = size;
21787c478bd9Sstevel@tonic-gate sz = length(hptr);
21797c478bd9Sstevel@tonic-gate ptr = nalloc(hptr->beg, (unsigned)size);
21807c478bd9Sstevel@tonic-gate if (ptr == 0) {
21817c478bd9Sstevel@tonic-gate garbage("copy");
21827c478bd9Sstevel@tonic-gate if ((ptr = nalloc(hptr->beg, (unsigned)size)) == NULL) {
21837c478bd9Sstevel@tonic-gate printf(gettext("copy size %d\n"), size);
21847c478bd9Sstevel@tonic-gate ospace("copy");
21857c478bd9Sstevel@tonic-gate }
21867c478bd9Sstevel@tonic-gate }
21877c478bd9Sstevel@tonic-gate if ((hdr = hfree) == 0)
21887c478bd9Sstevel@tonic-gate hdr = morehd();
21897c478bd9Sstevel@tonic-gate hfree = (struct blk *)hdr->rd;
21907c478bd9Sstevel@tonic-gate hdr->rd = hdr->beg = ptr;
21917c478bd9Sstevel@tonic-gate hdr->last = ptr + size;
21927c478bd9Sstevel@tonic-gate hdr->wt = ptr + sz;
21937c478bd9Sstevel@tonic-gate ptr = hdr->wt;
21947c478bd9Sstevel@tonic-gate while (ptr < hdr->last)
21957c478bd9Sstevel@tonic-gate *ptr++ = '\0';
21967c478bd9Sstevel@tonic-gate return (hdr);
21977c478bd9Sstevel@tonic-gate }
21987c478bd9Sstevel@tonic-gate
21997c478bd9Sstevel@tonic-gate void
sdump(char * s1,struct blk * hptr)22007c478bd9Sstevel@tonic-gate sdump(char *s1, struct blk *hptr)
22017c478bd9Sstevel@tonic-gate {
22027c478bd9Sstevel@tonic-gate char *p;
22037c478bd9Sstevel@tonic-gate
22047c478bd9Sstevel@tonic-gate if (hptr) {
22057c478bd9Sstevel@tonic-gate printf("%s %o rd %o wt %o beg %o last %o\n", s1, hptr,
22067c478bd9Sstevel@tonic-gate hptr->rd, hptr->wt, hptr->beg, hptr->last);
22077c478bd9Sstevel@tonic-gate p = hptr->beg;
22087c478bd9Sstevel@tonic-gate while (p < hptr->wt)
22097c478bd9Sstevel@tonic-gate printf("%d ", *p++);
22107c478bd9Sstevel@tonic-gate printf("\n");
22117c478bd9Sstevel@tonic-gate } else
22127c478bd9Sstevel@tonic-gate printf("%s %o\n", s1, hptr);
22137c478bd9Sstevel@tonic-gate }
22147c478bd9Sstevel@tonic-gate
22157c478bd9Sstevel@tonic-gate void
seekc(struct blk * hptr,int n)22167c478bd9Sstevel@tonic-gate seekc(struct blk *hptr, int n)
22177c478bd9Sstevel@tonic-gate {
22187c478bd9Sstevel@tonic-gate char *nn, *p;
22197c478bd9Sstevel@tonic-gate
22207c478bd9Sstevel@tonic-gate nn = hptr->beg + n;
22217c478bd9Sstevel@tonic-gate if (nn > hptr->last) {
22227c478bd9Sstevel@tonic-gate nbytes += nn - hptr->last;
22237c478bd9Sstevel@tonic-gate if (nbytes > maxsize)
22247c478bd9Sstevel@tonic-gate maxsize = nbytes;
22257c478bd9Sstevel@tonic-gate lbytes += nn - hptr->last;
22267c478bd9Sstevel@tonic-gate if (n > longest)
22277c478bd9Sstevel@tonic-gate longest = n;
22287c478bd9Sstevel@tonic-gate p = realloc(hptr->beg, (unsigned)n);
22297c478bd9Sstevel@tonic-gate if (p == 0) {
22307c478bd9Sstevel@tonic-gate hptr->beg = realloc(hptr->beg,
22317c478bd9Sstevel@tonic-gate (unsigned)(hptr->last - hptr->beg));
22327c478bd9Sstevel@tonic-gate garbage("seekc");
22337c478bd9Sstevel@tonic-gate if ((p = realloc(hptr->beg, (unsigned)n)) == 0)
22347c478bd9Sstevel@tonic-gate ospace("seekc");
22357c478bd9Sstevel@tonic-gate }
22367c478bd9Sstevel@tonic-gate hptr->beg = p;
22377c478bd9Sstevel@tonic-gate hptr->wt = hptr->last = hptr->rd = p + n;
22387c478bd9Sstevel@tonic-gate return;
22397c478bd9Sstevel@tonic-gate }
22407c478bd9Sstevel@tonic-gate hptr->rd = nn;
22417c478bd9Sstevel@tonic-gate if (nn > hptr->wt)
22427c478bd9Sstevel@tonic-gate hptr->wt = nn;
22437c478bd9Sstevel@tonic-gate }
22447c478bd9Sstevel@tonic-gate
22457c478bd9Sstevel@tonic-gate void
salterwd(struct wblk * hptr,struct blk * n)22467c478bd9Sstevel@tonic-gate salterwd(struct wblk *hptr, struct blk *n)
22477c478bd9Sstevel@tonic-gate {
22487c478bd9Sstevel@tonic-gate if (hptr->rdw == hptr->lastw)
22497c478bd9Sstevel@tonic-gate more((struct blk *)hptr);
22507c478bd9Sstevel@tonic-gate *hptr->rdw++ = n;
22517c478bd9Sstevel@tonic-gate if (hptr->rdw > hptr->wtw)
22527c478bd9Sstevel@tonic-gate hptr->wtw = hptr->rdw;
22537c478bd9Sstevel@tonic-gate }
22547c478bd9Sstevel@tonic-gate
22557c478bd9Sstevel@tonic-gate void
more(struct blk * hptr)22567c478bd9Sstevel@tonic-gate more(struct blk *hptr)
22577c478bd9Sstevel@tonic-gate {
22587c478bd9Sstevel@tonic-gate unsigned size;
22597c478bd9Sstevel@tonic-gate char *p;
22607c478bd9Sstevel@tonic-gate
22617c478bd9Sstevel@tonic-gate if ((size = (hptr->last - hptr->beg) * 2) == 0)
22627c478bd9Sstevel@tonic-gate size = 1;
22637c478bd9Sstevel@tonic-gate nbytes += size / 2;
22647c478bd9Sstevel@tonic-gate if (nbytes > maxsize)
22657c478bd9Sstevel@tonic-gate maxsize = nbytes;
22667c478bd9Sstevel@tonic-gate if (size > longest)
22677c478bd9Sstevel@tonic-gate longest = size;
22687c478bd9Sstevel@tonic-gate lbytes += size / 2;
22697c478bd9Sstevel@tonic-gate lmore++;
22707c478bd9Sstevel@tonic-gate p = realloc(hptr->beg, (unsigned)size);
22717c478bd9Sstevel@tonic-gate if (p == 0) {
22727c478bd9Sstevel@tonic-gate hptr->beg = realloc(hptr->beg,
22737c478bd9Sstevel@tonic-gate (unsigned)(hptr->last - hptr->beg));
22747c478bd9Sstevel@tonic-gate garbage("more");
22757c478bd9Sstevel@tonic-gate if ((p = realloc(hptr->beg, size)) == 0)
22767c478bd9Sstevel@tonic-gate ospace("more");
22777c478bd9Sstevel@tonic-gate }
22787c478bd9Sstevel@tonic-gate hptr->rd = hptr->rd - hptr->beg + p;
22797c478bd9Sstevel@tonic-gate hptr->wt = hptr->wt - hptr->beg + p;
22807c478bd9Sstevel@tonic-gate hptr->beg = p;
22817c478bd9Sstevel@tonic-gate hptr->last = p + size;
22827c478bd9Sstevel@tonic-gate }
22837c478bd9Sstevel@tonic-gate
22847c478bd9Sstevel@tonic-gate void
ospace(char * s)22857c478bd9Sstevel@tonic-gate ospace(char *s)
22867c478bd9Sstevel@tonic-gate {
22877c478bd9Sstevel@tonic-gate printf(gettext("out of space: %s\n"), s);
22887c478bd9Sstevel@tonic-gate printf(gettext("all %ld rel %ld headmor %ld\n"), all, rel, headmor);
22897c478bd9Sstevel@tonic-gate printf(gettext("nbytes %ld\n"), nbytes);
22907c478bd9Sstevel@tonic-gate sdump("stk", *stkptr);
22917c478bd9Sstevel@tonic-gate abort();
22927c478bd9Sstevel@tonic-gate }
22937c478bd9Sstevel@tonic-gate
22947c478bd9Sstevel@tonic-gate #define G1 gettext("array %o elt %d odd\n")
22957c478bd9Sstevel@tonic-gate #define G2 gettext("tmps %o p %o\n")
22967c478bd9Sstevel@tonic-gate void
garbage(char * s)22977c478bd9Sstevel@tonic-gate garbage(char *s)
22987c478bd9Sstevel@tonic-gate {
22997c478bd9Sstevel@tonic-gate int i;
23007c478bd9Sstevel@tonic-gate struct blk *p, *q;
23017c478bd9Sstevel@tonic-gate struct sym *tmps;
23027c478bd9Sstevel@tonic-gate int ct;
23037c478bd9Sstevel@tonic-gate
23047c478bd9Sstevel@tonic-gate printf(gettext("got to garbage %s\n"), s);
23057c478bd9Sstevel@tonic-gate for (i = 0; i < TBLSZ; i++) {
23067c478bd9Sstevel@tonic-gate tmps = stable[i];
23077c478bd9Sstevel@tonic-gate if (tmps != 0) {
23087c478bd9Sstevel@tonic-gate if (i < ARRAYST) {
23097c478bd9Sstevel@tonic-gate do {
23107c478bd9Sstevel@tonic-gate p = tmps->val;
23117c478bd9Sstevel@tonic-gate if (((int)p->beg & 01) != 0) {
23127c478bd9Sstevel@tonic-gate printf(gettext(
23137c478bd9Sstevel@tonic-gate "string %o\n"), i);
23147c478bd9Sstevel@tonic-gate sdump("odd beg", p);
23157c478bd9Sstevel@tonic-gate }
23167c478bd9Sstevel@tonic-gate redef(p);
23177c478bd9Sstevel@tonic-gate tmps = tmps->next;
23187c478bd9Sstevel@tonic-gate } while (tmps != 0);
23197c478bd9Sstevel@tonic-gate continue;
23207c478bd9Sstevel@tonic-gate } else {
23217c478bd9Sstevel@tonic-gate do {
23227c478bd9Sstevel@tonic-gate p = tmps->val;
23237c478bd9Sstevel@tonic-gate rewind(p);
23247c478bd9Sstevel@tonic-gate ct = 0;
23257c478bd9Sstevel@tonic-gate while ((q = getwd(p)) != NULL) {
23267c478bd9Sstevel@tonic-gate ct++;
23277c478bd9Sstevel@tonic-gate if (q != 0) {
23287c478bd9Sstevel@tonic-gate if (((int)q->beg & 01)
23297c478bd9Sstevel@tonic-gate != 0) {
23307c478bd9Sstevel@tonic-gate printf(G1,
23317c478bd9Sstevel@tonic-gate i - ARRAYST,
23327c478bd9Sstevel@tonic-gate ct);
23337c478bd9Sstevel@tonic-gate printf(G2,
23347c478bd9Sstevel@tonic-gate tmps, p);
23357c478bd9Sstevel@tonic-gate sdump("elt", q);
23367c478bd9Sstevel@tonic-gate }
23377c478bd9Sstevel@tonic-gate redef(q);
23387c478bd9Sstevel@tonic-gate }
23397c478bd9Sstevel@tonic-gate }
23407c478bd9Sstevel@tonic-gate tmps = tmps->next;
23417c478bd9Sstevel@tonic-gate } while (tmps != 0);
23427c478bd9Sstevel@tonic-gate }
23437c478bd9Sstevel@tonic-gate }
23447c478bd9Sstevel@tonic-gate }
23457c478bd9Sstevel@tonic-gate }
23467c478bd9Sstevel@tonic-gate
23477c478bd9Sstevel@tonic-gate void
redef(struct blk * p)23487c478bd9Sstevel@tonic-gate redef(struct blk *p)
23497c478bd9Sstevel@tonic-gate {
23507c478bd9Sstevel@tonic-gate int offset;
23517c478bd9Sstevel@tonic-gate char *newp;
23527c478bd9Sstevel@tonic-gate char *dcmalloc();
23537c478bd9Sstevel@tonic-gate
23547c478bd9Sstevel@tonic-gate if ((int)p->beg & 01) {
23557c478bd9Sstevel@tonic-gate printf(gettext("odd ptr %o hdr %o\n"), p->beg, p);
23567c478bd9Sstevel@tonic-gate ospace("redef-bad");
23577c478bd9Sstevel@tonic-gate }
23587c478bd9Sstevel@tonic-gate free(dummy);
23597c478bd9Sstevel@tonic-gate dummy = dcmalloc(0);
23607c478bd9Sstevel@tonic-gate if (dummy == NULL)
23617c478bd9Sstevel@tonic-gate ospace("dummy");
23627c478bd9Sstevel@tonic-gate newp = realloc(p->beg, (unsigned)(p->last - p->beg));
23637c478bd9Sstevel@tonic-gate if (newp == NULL)
23647c478bd9Sstevel@tonic-gate ospace("redef");
23657c478bd9Sstevel@tonic-gate offset = newp - p->beg;
23667c478bd9Sstevel@tonic-gate p->beg = newp;
23677c478bd9Sstevel@tonic-gate p->rd += offset;
23687c478bd9Sstevel@tonic-gate p->wt += offset;
23697c478bd9Sstevel@tonic-gate p->last += offset;
23707c478bd9Sstevel@tonic-gate }
23717c478bd9Sstevel@tonic-gate
23727c478bd9Sstevel@tonic-gate void
release(struct blk * p)23737c478bd9Sstevel@tonic-gate release(struct blk *p)
23747c478bd9Sstevel@tonic-gate {
23757c478bd9Sstevel@tonic-gate rel++;
23767c478bd9Sstevel@tonic-gate lrel++;
23777c478bd9Sstevel@tonic-gate nbytes -= p->last - p->beg;
23787c478bd9Sstevel@tonic-gate p->rd = (char *)hfree;
23797c478bd9Sstevel@tonic-gate hfree = p;
23807c478bd9Sstevel@tonic-gate free(p->beg);
23817c478bd9Sstevel@tonic-gate p->beg = NULL;
23827c478bd9Sstevel@tonic-gate }
23837c478bd9Sstevel@tonic-gate
23847c478bd9Sstevel@tonic-gate struct blk *
getwd(struct blk * p)23857c478bd9Sstevel@tonic-gate getwd(struct blk *p)
23867c478bd9Sstevel@tonic-gate {
23877c478bd9Sstevel@tonic-gate struct wblk *wp;
23887c478bd9Sstevel@tonic-gate
23897c478bd9Sstevel@tonic-gate wp = (struct wblk *)p;
23907c478bd9Sstevel@tonic-gate if (wp->rdw == wp->wtw)
23917c478bd9Sstevel@tonic-gate return (NULL);
23927c478bd9Sstevel@tonic-gate return (*wp->rdw++);
23937c478bd9Sstevel@tonic-gate }
23947c478bd9Sstevel@tonic-gate
23957c478bd9Sstevel@tonic-gate void
putwd(struct blk * p,struct blk * c)23967c478bd9Sstevel@tonic-gate putwd(struct blk *p, struct blk *c)
23977c478bd9Sstevel@tonic-gate {
23987c478bd9Sstevel@tonic-gate struct wblk *wp;
23997c478bd9Sstevel@tonic-gate
24007c478bd9Sstevel@tonic-gate wp = (struct wblk *)p;
24017c478bd9Sstevel@tonic-gate if (wp->wtw == wp->lastw)
24027c478bd9Sstevel@tonic-gate more(p);
24037c478bd9Sstevel@tonic-gate *wp->wtw++ = c;
24047c478bd9Sstevel@tonic-gate }
24057c478bd9Sstevel@tonic-gate
24067c478bd9Sstevel@tonic-gate struct blk *
lookwd(struct blk * p)24077c478bd9Sstevel@tonic-gate lookwd(struct blk *p)
24087c478bd9Sstevel@tonic-gate {
24097c478bd9Sstevel@tonic-gate struct wblk *wp;
24107c478bd9Sstevel@tonic-gate
24117c478bd9Sstevel@tonic-gate wp = (struct wblk *)p;
24127c478bd9Sstevel@tonic-gate if (wp->rdw == wp->wtw)
24137c478bd9Sstevel@tonic-gate return (NULL);
24147c478bd9Sstevel@tonic-gate return (*wp->rdw);
24157c478bd9Sstevel@tonic-gate }
24167c478bd9Sstevel@tonic-gate
24177c478bd9Sstevel@tonic-gate char *
nalloc(char * p,unsigned int nbytes)24187c478bd9Sstevel@tonic-gate nalloc(char *p, unsigned int nbytes)
24197c478bd9Sstevel@tonic-gate {
24207c478bd9Sstevel@tonic-gate char *dcmalloc();
24217c478bd9Sstevel@tonic-gate char *q, *r;
24227c478bd9Sstevel@tonic-gate q = r = dcmalloc(nbytes);
24237c478bd9Sstevel@tonic-gate if (q == 0)
24247c478bd9Sstevel@tonic-gate return (0);
24257c478bd9Sstevel@tonic-gate while (nbytes--)
24267c478bd9Sstevel@tonic-gate *q++ = *p++;
24277c478bd9Sstevel@tonic-gate return (r);
24287c478bd9Sstevel@tonic-gate }
24297c478bd9Sstevel@tonic-gate
24307c478bd9Sstevel@tonic-gate char *
dcmalloc(int size)24317c478bd9Sstevel@tonic-gate dcmalloc(int size)
24327c478bd9Sstevel@tonic-gate {
24337c478bd9Sstevel@tonic-gate return (malloc(size ? size : 1));
24347c478bd9Sstevel@tonic-gate }
2435