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