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
277c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28*e0dcd577SToomas Soome /* All Rights Reserved */
297c478bd9Sstevel@tonic-gate
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate * University Copyright- Copyright (c) 1982, 1986, 1988
327c478bd9Sstevel@tonic-gate * The Regents of the University of California
337c478bd9Sstevel@tonic-gate * All Rights Reserved
347c478bd9Sstevel@tonic-gate *
357c478bd9Sstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from
367c478bd9Sstevel@tonic-gate * software developed by the University of California, Berkeley, and its
377c478bd9Sstevel@tonic-gate * contributors.
387c478bd9Sstevel@tonic-gate */
397c478bd9Sstevel@tonic-gate
407c478bd9Sstevel@tonic-gate #include <ctype.h>
417c478bd9Sstevel@tonic-gate #include "tdef.h"
427c478bd9Sstevel@tonic-gate #ifdef NROFF
437c478bd9Sstevel@tonic-gate #include "tw.h"
447c478bd9Sstevel@tonic-gate #endif
457c478bd9Sstevel@tonic-gate #include "ext.h"
467c478bd9Sstevel@tonic-gate /*
477c478bd9Sstevel@tonic-gate * troff4.c
482a8bcb4eSToomas Soome *
497c478bd9Sstevel@tonic-gate * number registers, conversion, arithmetic
507c478bd9Sstevel@tonic-gate */
517c478bd9Sstevel@tonic-gate
527c478bd9Sstevel@tonic-gate
537c478bd9Sstevel@tonic-gate int regcnt = NNAMES;
547c478bd9Sstevel@tonic-gate int falsef = 0; /* on if inside false branch of if */
557c478bd9Sstevel@tonic-gate #define NHASH(i) ((i>>6)^i)&0177
567c478bd9Sstevel@tonic-gate struct numtab *nhash[128]; /* 128 == the 0177 on line above */
577c478bd9Sstevel@tonic-gate
58e5190c10Smuffin int
setn()597c478bd9Sstevel@tonic-gate setn()
607c478bd9Sstevel@tonic-gate {
61e5190c10Smuffin int i, j;
62e5190c10Smuffin tchar ii;
637c478bd9Sstevel@tonic-gate int f;
647c478bd9Sstevel@tonic-gate
657c478bd9Sstevel@tonic-gate f = nform = 0;
667c478bd9Sstevel@tonic-gate if ((i = cbits(ii = getach())) == '+')
677c478bd9Sstevel@tonic-gate f = 1;
687c478bd9Sstevel@tonic-gate else if (i == '-')
697c478bd9Sstevel@tonic-gate f = -1;
702a8bcb4eSToomas Soome else
717c478bd9Sstevel@tonic-gate ch = ii;
727c478bd9Sstevel@tonic-gate if (falsef)
737c478bd9Sstevel@tonic-gate f = 0;
747c478bd9Sstevel@tonic-gate if ((i = getsn()) == 0)
75e5190c10Smuffin return (0);
767c478bd9Sstevel@tonic-gate if ((i & 0177) == '.')
777c478bd9Sstevel@tonic-gate switch (i >> BYTE) {
782a8bcb4eSToomas Soome case 's':
792a8bcb4eSToomas Soome i = pts;
807c478bd9Sstevel@tonic-gate break;
812a8bcb4eSToomas Soome case 'v':
822a8bcb4eSToomas Soome i = lss;
837c478bd9Sstevel@tonic-gate break;
842a8bcb4eSToomas Soome case 'f':
852a8bcb4eSToomas Soome i = font;
867c478bd9Sstevel@tonic-gate break;
872a8bcb4eSToomas Soome case 'p':
882a8bcb4eSToomas Soome i = pl;
897c478bd9Sstevel@tonic-gate break;
902a8bcb4eSToomas Soome case 't':
912a8bcb4eSToomas Soome i = findt1();
927c478bd9Sstevel@tonic-gate break;
932a8bcb4eSToomas Soome case 'o':
942a8bcb4eSToomas Soome i = po;
957c478bd9Sstevel@tonic-gate break;
962a8bcb4eSToomas Soome case 'l':
972a8bcb4eSToomas Soome i = ll;
987c478bd9Sstevel@tonic-gate break;
992a8bcb4eSToomas Soome case 'i':
1002a8bcb4eSToomas Soome i = in;
1017c478bd9Sstevel@tonic-gate break;
1022a8bcb4eSToomas Soome case '$':
1032a8bcb4eSToomas Soome i = frame->nargs;
1047c478bd9Sstevel@tonic-gate break;
1052a8bcb4eSToomas Soome case 'A':
1062a8bcb4eSToomas Soome i = ascii;
1077c478bd9Sstevel@tonic-gate break;
1082a8bcb4eSToomas Soome case 'c':
1092a8bcb4eSToomas Soome i = numtab[CD].val;
1107c478bd9Sstevel@tonic-gate break;
1112a8bcb4eSToomas Soome case 'n':
1122a8bcb4eSToomas Soome i = lastl;
1137c478bd9Sstevel@tonic-gate break;
1142a8bcb4eSToomas Soome case 'a':
1152a8bcb4eSToomas Soome i = ralss;
1167c478bd9Sstevel@tonic-gate break;
1172a8bcb4eSToomas Soome case 'h':
1182a8bcb4eSToomas Soome i = dip->hnl;
1197c478bd9Sstevel@tonic-gate break;
1207c478bd9Sstevel@tonic-gate case 'd':
1217c478bd9Sstevel@tonic-gate if (dip != d)
1222a8bcb4eSToomas Soome i = dip->dnl;
1232a8bcb4eSToomas Soome else
1247c478bd9Sstevel@tonic-gate i = numtab[NL].val;
1257c478bd9Sstevel@tonic-gate break;
1262a8bcb4eSToomas Soome case 'u':
1272a8bcb4eSToomas Soome i = fi;
1287c478bd9Sstevel@tonic-gate break;
1292a8bcb4eSToomas Soome case 'j':
1302a8bcb4eSToomas Soome i = ad + 2 * admod;
1317c478bd9Sstevel@tonic-gate break;
1322a8bcb4eSToomas Soome case 'w':
1337c478bd9Sstevel@tonic-gate i = widthp;
1347c478bd9Sstevel@tonic-gate break;
1352a8bcb4eSToomas Soome case 'x':
1362a8bcb4eSToomas Soome i = nel;
1377c478bd9Sstevel@tonic-gate break;
1382a8bcb4eSToomas Soome case 'y':
1392a8bcb4eSToomas Soome i = un;
1407c478bd9Sstevel@tonic-gate break;
1412a8bcb4eSToomas Soome case 'T':
1422a8bcb4eSToomas Soome i = dotT;
1437c478bd9Sstevel@tonic-gate break; /*-Tterm used in nroff*/
1442a8bcb4eSToomas Soome case 'V':
1452a8bcb4eSToomas Soome i = VERT;
1467c478bd9Sstevel@tonic-gate break;
1472a8bcb4eSToomas Soome case 'H':
1482a8bcb4eSToomas Soome i = HOR;
1497c478bd9Sstevel@tonic-gate break;
1502a8bcb4eSToomas Soome case 'k':
1512a8bcb4eSToomas Soome i = ne;
1527c478bd9Sstevel@tonic-gate break;
1532a8bcb4eSToomas Soome case 'P':
1542a8bcb4eSToomas Soome i = print;
1557c478bd9Sstevel@tonic-gate break;
1562a8bcb4eSToomas Soome case 'L':
1572a8bcb4eSToomas Soome i = ls;
1587c478bd9Sstevel@tonic-gate break;
1592a8bcb4eSToomas Soome case 'R':
1602a8bcb4eSToomas Soome i = NN - regcnt;
1617c478bd9Sstevel@tonic-gate break;
1622a8bcb4eSToomas Soome case 'z':
1637c478bd9Sstevel@tonic-gate i = dip->curd;
1647c478bd9Sstevel@tonic-gate *pbp++ = (i >> BYTE) & BYTEMASK;
1657c478bd9Sstevel@tonic-gate *pbp++ = i & BYTEMASK;
166e5190c10Smuffin return (0);
1672a8bcb4eSToomas Soome case 'b':
1687c478bd9Sstevel@tonic-gate i = bdtab[font];
1697c478bd9Sstevel@tonic-gate break;
1707c478bd9Sstevel@tonic-gate case 'F':
1717c478bd9Sstevel@tonic-gate cpushback(cfname[ifi]);
172e5190c10Smuffin return (0);
1737c478bd9Sstevel@tonic-gate
1747c478bd9Sstevel@tonic-gate default:
1757c478bd9Sstevel@tonic-gate goto s0;
1767c478bd9Sstevel@tonic-gate }
1777c478bd9Sstevel@tonic-gate else {
1787c478bd9Sstevel@tonic-gate s0:
1797c478bd9Sstevel@tonic-gate if ((j = findr(i)) == -1)
1807c478bd9Sstevel@tonic-gate i = 0;
1817c478bd9Sstevel@tonic-gate else {
1827c478bd9Sstevel@tonic-gate i = numtab[j].val = (numtab[j].val+numtab[j].inc*f);
1837c478bd9Sstevel@tonic-gate nform = numtab[j].fmt;
1847c478bd9Sstevel@tonic-gate }
1857c478bd9Sstevel@tonic-gate }
1867c478bd9Sstevel@tonic-gate setn1(i, nform, (tchar) 0);
187e5190c10Smuffin
188e5190c10Smuffin return (0);
1897c478bd9Sstevel@tonic-gate }
1907c478bd9Sstevel@tonic-gate
1917c478bd9Sstevel@tonic-gate tchar numbuf[17];
1927c478bd9Sstevel@tonic-gate tchar *numbufp;
1937c478bd9Sstevel@tonic-gate
194e5190c10Smuffin int
wrc(i)1957c478bd9Sstevel@tonic-gate wrc(i)
1967c478bd9Sstevel@tonic-gate tchar i;
1977c478bd9Sstevel@tonic-gate {
1987c478bd9Sstevel@tonic-gate if (numbufp >= &numbuf[16])
1997c478bd9Sstevel@tonic-gate return(0);
2007c478bd9Sstevel@tonic-gate *numbufp++ = i;
2017c478bd9Sstevel@tonic-gate return(1);
2027c478bd9Sstevel@tonic-gate }
2037c478bd9Sstevel@tonic-gate
2047c478bd9Sstevel@tonic-gate
2057c478bd9Sstevel@tonic-gate
2067c478bd9Sstevel@tonic-gate /* insert into input number i, in format form, with size-font bits bits */
207e5190c10Smuffin int
setn1(int i,int form,tchar bits)208*e0dcd577SToomas Soome setn1(int i, int form, tchar bits)
2097c478bd9Sstevel@tonic-gate {
2107c478bd9Sstevel@tonic-gate extern int wrc();
2117c478bd9Sstevel@tonic-gate
2127c478bd9Sstevel@tonic-gate numbufp = numbuf;
2137c478bd9Sstevel@tonic-gate nrbits = bits;
2147c478bd9Sstevel@tonic-gate nform = form;
2157c478bd9Sstevel@tonic-gate fnumb(i, wrc);
2167c478bd9Sstevel@tonic-gate *numbufp = 0;
2177c478bd9Sstevel@tonic-gate pushback(numbuf);
218e5190c10Smuffin
219e5190c10Smuffin return (0);
2207c478bd9Sstevel@tonic-gate }
2217c478bd9Sstevel@tonic-gate
2227c478bd9Sstevel@tonic-gate
223e5190c10Smuffin int
nrehash()2247c478bd9Sstevel@tonic-gate nrehash()
2257c478bd9Sstevel@tonic-gate {
226e5190c10Smuffin struct numtab *p;
227e5190c10Smuffin int i;
2287c478bd9Sstevel@tonic-gate
2297c478bd9Sstevel@tonic-gate for (i=0; i<128; i++)
2307c478bd9Sstevel@tonic-gate nhash[i] = 0;
2317c478bd9Sstevel@tonic-gate for (p=numtab; p < &numtab[NN]; p++)
2327c478bd9Sstevel@tonic-gate p->link = 0;
2337c478bd9Sstevel@tonic-gate for (p=numtab; p < &numtab[NN]; p++) {
2347c478bd9Sstevel@tonic-gate if (p->r == 0)
2357c478bd9Sstevel@tonic-gate continue;
2367c478bd9Sstevel@tonic-gate i = NHASH(p->r);
2377c478bd9Sstevel@tonic-gate p->link = nhash[i];
2387c478bd9Sstevel@tonic-gate nhash[i] = p;
2397c478bd9Sstevel@tonic-gate }
240e5190c10Smuffin
241e5190c10Smuffin return (0);
2427c478bd9Sstevel@tonic-gate }
2437c478bd9Sstevel@tonic-gate
244e5190c10Smuffin int
nunhash(rp)2457c478bd9Sstevel@tonic-gate nunhash(rp)
246e5190c10Smuffin struct numtab *rp;
2472a8bcb4eSToomas Soome {
248e5190c10Smuffin struct numtab *p;
249e5190c10Smuffin struct numtab **lp;
2507c478bd9Sstevel@tonic-gate
2517c478bd9Sstevel@tonic-gate if (rp->r == 0)
252e5190c10Smuffin return (0);
2537c478bd9Sstevel@tonic-gate lp = &nhash[NHASH(rp->r)];
2547c478bd9Sstevel@tonic-gate p = *lp;
2557c478bd9Sstevel@tonic-gate while (p) {
2567c478bd9Sstevel@tonic-gate if (p == rp) {
2577c478bd9Sstevel@tonic-gate *lp = p->link;
2587c478bd9Sstevel@tonic-gate p->link = 0;
259e5190c10Smuffin return (0);
2607c478bd9Sstevel@tonic-gate }
2617c478bd9Sstevel@tonic-gate lp = &p->link;
2627c478bd9Sstevel@tonic-gate p = p->link;
2637c478bd9Sstevel@tonic-gate }
264e5190c10Smuffin return (0);
2657c478bd9Sstevel@tonic-gate }
2667c478bd9Sstevel@tonic-gate
267e5190c10Smuffin int
findr(i)2687c478bd9Sstevel@tonic-gate findr(i)
269e5190c10Smuffin int i;
2707c478bd9Sstevel@tonic-gate {
271e5190c10Smuffin struct numtab *p;
272e5190c10Smuffin int h = NHASH(i);
2737c478bd9Sstevel@tonic-gate
2747c478bd9Sstevel@tonic-gate if (i == 0)
2757c478bd9Sstevel@tonic-gate return(-1);
2767c478bd9Sstevel@tonic-gate for (p = nhash[h]; p; p = p->link)
2777c478bd9Sstevel@tonic-gate if (i == p->r)
2787c478bd9Sstevel@tonic-gate return(p - numtab);
2797c478bd9Sstevel@tonic-gate for (p = numtab; p < &numtab[NN]; p++) {
2807c478bd9Sstevel@tonic-gate if (p->r == 0) {
2817c478bd9Sstevel@tonic-gate p->r = i;
2827c478bd9Sstevel@tonic-gate p->link = nhash[h];
2837c478bd9Sstevel@tonic-gate nhash[h] = p;
2847c478bd9Sstevel@tonic-gate regcnt++;
2857c478bd9Sstevel@tonic-gate return(p - numtab);
2867c478bd9Sstevel@tonic-gate }
2877c478bd9Sstevel@tonic-gate }
2887c478bd9Sstevel@tonic-gate errprint(gettext("too many number registers (%d)."), NN);
2892a8bcb4eSToomas Soome done2(04);
2907c478bd9Sstevel@tonic-gate /* NOTREACHED */
291e5190c10Smuffin
292e5190c10Smuffin return (0);
2937c478bd9Sstevel@tonic-gate }
2947c478bd9Sstevel@tonic-gate
295e5190c10Smuffin int
usedr(i)2967c478bd9Sstevel@tonic-gate usedr(i) /* returns -1 if nr i has never been used */
297e5190c10Smuffin int i;
2987c478bd9Sstevel@tonic-gate {
299e5190c10Smuffin struct numtab *p;
3007c478bd9Sstevel@tonic-gate
3017c478bd9Sstevel@tonic-gate if (i == 0)
3027c478bd9Sstevel@tonic-gate return(-1);
3037c478bd9Sstevel@tonic-gate for (p = nhash[NHASH(i)]; p; p = p->link)
3047c478bd9Sstevel@tonic-gate if (i == p->r)
3057c478bd9Sstevel@tonic-gate return(p - numtab);
3067c478bd9Sstevel@tonic-gate return -1;
3077c478bd9Sstevel@tonic-gate }
3087c478bd9Sstevel@tonic-gate
3097c478bd9Sstevel@tonic-gate
310e5190c10Smuffin int
3117c478bd9Sstevel@tonic-gate fnumb(i, f)
312e5190c10Smuffin int i, (*f)();
3137c478bd9Sstevel@tonic-gate {
314e5190c10Smuffin int j;
3157c478bd9Sstevel@tonic-gate
3167c478bd9Sstevel@tonic-gate j = 0;
3177c478bd9Sstevel@tonic-gate if (i < 0) {
3187c478bd9Sstevel@tonic-gate j = (*f)('-' | nrbits);
3197c478bd9Sstevel@tonic-gate i = -i;
3207c478bd9Sstevel@tonic-gate }
3217c478bd9Sstevel@tonic-gate switch (nform) {
3227c478bd9Sstevel@tonic-gate default:
3237c478bd9Sstevel@tonic-gate case '1':
3242a8bcb4eSToomas Soome case 0:
3257c478bd9Sstevel@tonic-gate return decml(i, f) + j;
3267c478bd9Sstevel@tonic-gate break;
3277c478bd9Sstevel@tonic-gate case 'i':
3282a8bcb4eSToomas Soome case 'I':
3297c478bd9Sstevel@tonic-gate return roman(i, f) + j;
3307c478bd9Sstevel@tonic-gate break;
3317c478bd9Sstevel@tonic-gate case 'a':
3322a8bcb4eSToomas Soome case 'A':
3337c478bd9Sstevel@tonic-gate return abc(i, f) + j;
3347c478bd9Sstevel@tonic-gate break;
3357c478bd9Sstevel@tonic-gate }
336e5190c10Smuffin
337e5190c10Smuffin return (0);
3387c478bd9Sstevel@tonic-gate }
3397c478bd9Sstevel@tonic-gate
3407c478bd9Sstevel@tonic-gate
341e5190c10Smuffin int
3427c478bd9Sstevel@tonic-gate decml(i, f)
343e5190c10Smuffin int i, (*f)();
3447c478bd9Sstevel@tonic-gate {
345e5190c10Smuffin int j, k;
3467c478bd9Sstevel@tonic-gate
3477c478bd9Sstevel@tonic-gate k = 0;
3487c478bd9Sstevel@tonic-gate nform--;
3497c478bd9Sstevel@tonic-gate if ((j = i / 10) || (nform > 0))
3507c478bd9Sstevel@tonic-gate k = decml(j, f);
3517c478bd9Sstevel@tonic-gate return(k + (*f)((i % 10 + '0') | nrbits));
3527c478bd9Sstevel@tonic-gate }
3537c478bd9Sstevel@tonic-gate
3547c478bd9Sstevel@tonic-gate
355e5190c10Smuffin int
3567c478bd9Sstevel@tonic-gate roman(i, f)
3577c478bd9Sstevel@tonic-gate int i, (*f)();
3587c478bd9Sstevel@tonic-gate {
3597c478bd9Sstevel@tonic-gate
3607c478bd9Sstevel@tonic-gate if (!i)
3617c478bd9Sstevel@tonic-gate return((*f)('0' | nrbits));
3627c478bd9Sstevel@tonic-gate if (nform == 'i')
3637c478bd9Sstevel@tonic-gate return(roman0(i, f, "ixcmz", "vldw"));
3642a8bcb4eSToomas Soome else
3657c478bd9Sstevel@tonic-gate return(roman0(i, f, "IXCMZ", "VLDW"));
3667c478bd9Sstevel@tonic-gate }
3677c478bd9Sstevel@tonic-gate
3687c478bd9Sstevel@tonic-gate
369e5190c10Smuffin int
3707c478bd9Sstevel@tonic-gate roman0(i, f, onesp, fivesp)
3717c478bd9Sstevel@tonic-gate int i, (*f)();
3727c478bd9Sstevel@tonic-gate char *onesp, *fivesp;
3737c478bd9Sstevel@tonic-gate {
374e5190c10Smuffin int q, rem, k;
3757c478bd9Sstevel@tonic-gate
3767c478bd9Sstevel@tonic-gate k = 0;
3777c478bd9Sstevel@tonic-gate if (!i)
3787c478bd9Sstevel@tonic-gate return(0);
3797c478bd9Sstevel@tonic-gate k = roman0(i / 10, f, onesp + 1, fivesp + 1);
3807c478bd9Sstevel@tonic-gate q = (i = i % 10) / 5;
3817c478bd9Sstevel@tonic-gate rem = i % 5;
3827c478bd9Sstevel@tonic-gate if (rem == 4) {
3837c478bd9Sstevel@tonic-gate k += (*f)(*onesp | nrbits);
3847c478bd9Sstevel@tonic-gate if (q)
3857c478bd9Sstevel@tonic-gate i = *(onesp + 1);
3862a8bcb4eSToomas Soome else
3877c478bd9Sstevel@tonic-gate i = *fivesp;
3887c478bd9Sstevel@tonic-gate return(k += (*f)(i | nrbits));
3897c478bd9Sstevel@tonic-gate }
3907c478bd9Sstevel@tonic-gate if (q)
3917c478bd9Sstevel@tonic-gate k += (*f)(*fivesp | nrbits);
3927c478bd9Sstevel@tonic-gate while (--rem >= 0)
3937c478bd9Sstevel@tonic-gate k += (*f)(*onesp | nrbits);
3947c478bd9Sstevel@tonic-gate return(k);
3957c478bd9Sstevel@tonic-gate }
3967c478bd9Sstevel@tonic-gate
3977c478bd9Sstevel@tonic-gate
398e5190c10Smuffin int
3997c478bd9Sstevel@tonic-gate abc(i, f)
4007c478bd9Sstevel@tonic-gate int i, (*f)();
4017c478bd9Sstevel@tonic-gate {
4027c478bd9Sstevel@tonic-gate if (!i)
4037c478bd9Sstevel@tonic-gate return((*f)('0' | nrbits));
4042a8bcb4eSToomas Soome else
4057c478bd9Sstevel@tonic-gate return(abc0(i - 1, f));
4067c478bd9Sstevel@tonic-gate }
4077c478bd9Sstevel@tonic-gate
4087c478bd9Sstevel@tonic-gate
409e5190c10Smuffin int
4107c478bd9Sstevel@tonic-gate abc0(i, f)
4117c478bd9Sstevel@tonic-gate int i, (*f)();
4127c478bd9Sstevel@tonic-gate {
413e5190c10Smuffin int j, k;
4147c478bd9Sstevel@tonic-gate
4157c478bd9Sstevel@tonic-gate k = 0;
4167c478bd9Sstevel@tonic-gate if (j = i / 26)
4177c478bd9Sstevel@tonic-gate k = abc0(j - 1, f);
4187c478bd9Sstevel@tonic-gate return(k + (*f)((i % 26 + nform) | nrbits));
4197c478bd9Sstevel@tonic-gate }
4207c478bd9Sstevel@tonic-gate
atoi0()4217c478bd9Sstevel@tonic-gate long atoi0()
4227c478bd9Sstevel@tonic-gate {
423e5190c10Smuffin int c, k, cnt;
424e5190c10Smuffin tchar ii;
4257c478bd9Sstevel@tonic-gate long i, acc;
4267c478bd9Sstevel@tonic-gate extern long ckph();
4277c478bd9Sstevel@tonic-gate
4282a8bcb4eSToomas Soome i = 0;
4297c478bd9Sstevel@tonic-gate acc = 0;
4307c478bd9Sstevel@tonic-gate nonumb = 0;
4317c478bd9Sstevel@tonic-gate cnt = -1;
4327c478bd9Sstevel@tonic-gate a0:
4337c478bd9Sstevel@tonic-gate cnt++;
4347c478bd9Sstevel@tonic-gate ii = getch();
4357c478bd9Sstevel@tonic-gate c = cbits(ii);
4367c478bd9Sstevel@tonic-gate switch (c) {
4377c478bd9Sstevel@tonic-gate default:
4387c478bd9Sstevel@tonic-gate ch = ii;
4397c478bd9Sstevel@tonic-gate if (cnt)
4407c478bd9Sstevel@tonic-gate break;
4417c478bd9Sstevel@tonic-gate case '+':
4427c478bd9Sstevel@tonic-gate i = ckph();
4437c478bd9Sstevel@tonic-gate if (nonumb)
4447c478bd9Sstevel@tonic-gate break;
4457c478bd9Sstevel@tonic-gate acc += i;
4467c478bd9Sstevel@tonic-gate goto a0;
4477c478bd9Sstevel@tonic-gate case '-':
4487c478bd9Sstevel@tonic-gate i = ckph();
4497c478bd9Sstevel@tonic-gate if (nonumb)
4507c478bd9Sstevel@tonic-gate break;
4517c478bd9Sstevel@tonic-gate acc -= i;
4527c478bd9Sstevel@tonic-gate goto a0;
4537c478bd9Sstevel@tonic-gate case '*':
4547c478bd9Sstevel@tonic-gate i = ckph();
4557c478bd9Sstevel@tonic-gate if (nonumb)
4567c478bd9Sstevel@tonic-gate break;
4577c478bd9Sstevel@tonic-gate acc *= i;
4587c478bd9Sstevel@tonic-gate goto a0;
4597c478bd9Sstevel@tonic-gate case '/':
4607c478bd9Sstevel@tonic-gate i = ckph();
4617c478bd9Sstevel@tonic-gate if (nonumb)
4627c478bd9Sstevel@tonic-gate break;
4637c478bd9Sstevel@tonic-gate if (i == 0) {
4647c478bd9Sstevel@tonic-gate flusho();
4657c478bd9Sstevel@tonic-gate errprint(gettext("divide by zero."));
4667c478bd9Sstevel@tonic-gate acc = 0;
4672a8bcb4eSToomas Soome } else
4687c478bd9Sstevel@tonic-gate acc /= i;
4697c478bd9Sstevel@tonic-gate goto a0;
4707c478bd9Sstevel@tonic-gate case '%':
4717c478bd9Sstevel@tonic-gate i = ckph();
4727c478bd9Sstevel@tonic-gate if (nonumb)
4737c478bd9Sstevel@tonic-gate break;
4747c478bd9Sstevel@tonic-gate acc %= i;
4757c478bd9Sstevel@tonic-gate goto a0;
4767c478bd9Sstevel@tonic-gate case '&': /*and*/
4777c478bd9Sstevel@tonic-gate i = ckph();
4787c478bd9Sstevel@tonic-gate if (nonumb)
4797c478bd9Sstevel@tonic-gate break;
4807c478bd9Sstevel@tonic-gate if ((acc > 0) && (i > 0))
4812a8bcb4eSToomas Soome acc = 1;
4822a8bcb4eSToomas Soome else
4837c478bd9Sstevel@tonic-gate acc = 0;
4847c478bd9Sstevel@tonic-gate goto a0;
4857c478bd9Sstevel@tonic-gate case ':': /*or*/
4867c478bd9Sstevel@tonic-gate i = ckph();
4877c478bd9Sstevel@tonic-gate if (nonumb)
4887c478bd9Sstevel@tonic-gate break;
4897c478bd9Sstevel@tonic-gate if ((acc > 0) || (i > 0))
4902a8bcb4eSToomas Soome acc = 1;
4912a8bcb4eSToomas Soome else
4927c478bd9Sstevel@tonic-gate acc = 0;
4937c478bd9Sstevel@tonic-gate goto a0;
4947c478bd9Sstevel@tonic-gate case '=':
4957c478bd9Sstevel@tonic-gate if (cbits(ii = getch()) != '=')
4967c478bd9Sstevel@tonic-gate ch = ii;
4977c478bd9Sstevel@tonic-gate i = ckph();
4987c478bd9Sstevel@tonic-gate if (nonumb) {
4992a8bcb4eSToomas Soome acc = 0;
5007c478bd9Sstevel@tonic-gate break;
5017c478bd9Sstevel@tonic-gate }
5027c478bd9Sstevel@tonic-gate if (i == acc)
5037c478bd9Sstevel@tonic-gate acc = 1;
5042a8bcb4eSToomas Soome else
5057c478bd9Sstevel@tonic-gate acc = 0;
5067c478bd9Sstevel@tonic-gate goto a0;
5077c478bd9Sstevel@tonic-gate case '>':
5087c478bd9Sstevel@tonic-gate k = 0;
5097c478bd9Sstevel@tonic-gate if (cbits(ii = getch()) == '=')
5102a8bcb4eSToomas Soome k++;
5112a8bcb4eSToomas Soome else
5127c478bd9Sstevel@tonic-gate ch = ii;
5137c478bd9Sstevel@tonic-gate i = ckph();
5147c478bd9Sstevel@tonic-gate if (nonumb) {
5152a8bcb4eSToomas Soome acc = 0;
5167c478bd9Sstevel@tonic-gate break;
5177c478bd9Sstevel@tonic-gate }
5187c478bd9Sstevel@tonic-gate if (acc > (i - k))
5192a8bcb4eSToomas Soome acc = 1;
5202a8bcb4eSToomas Soome else
5217c478bd9Sstevel@tonic-gate acc = 0;
5227c478bd9Sstevel@tonic-gate goto a0;
5237c478bd9Sstevel@tonic-gate case '<':
5247c478bd9Sstevel@tonic-gate k = 0;
5257c478bd9Sstevel@tonic-gate if (cbits(ii = getch()) == '=')
5262a8bcb4eSToomas Soome k++;
5272a8bcb4eSToomas Soome else
5287c478bd9Sstevel@tonic-gate ch = ii;
5297c478bd9Sstevel@tonic-gate i = ckph();
5307c478bd9Sstevel@tonic-gate if (nonumb) {
5312a8bcb4eSToomas Soome acc = 0;
5327c478bd9Sstevel@tonic-gate break;
5337c478bd9Sstevel@tonic-gate }
5347c478bd9Sstevel@tonic-gate if (acc < (i + k))
5352a8bcb4eSToomas Soome acc = 1;
5362a8bcb4eSToomas Soome else
5377c478bd9Sstevel@tonic-gate acc = 0;
5387c478bd9Sstevel@tonic-gate goto a0;
5392a8bcb4eSToomas Soome case ')':
5407c478bd9Sstevel@tonic-gate break;
5417c478bd9Sstevel@tonic-gate case '(':
5427c478bd9Sstevel@tonic-gate acc = atoi0();
5437c478bd9Sstevel@tonic-gate goto a0;
5447c478bd9Sstevel@tonic-gate }
5457c478bd9Sstevel@tonic-gate return(acc);
5467c478bd9Sstevel@tonic-gate }
5477c478bd9Sstevel@tonic-gate
5487c478bd9Sstevel@tonic-gate
ckph()5497c478bd9Sstevel@tonic-gate long ckph()
5507c478bd9Sstevel@tonic-gate {
551e5190c10Smuffin tchar i;
552e5190c10Smuffin long j;
5537c478bd9Sstevel@tonic-gate extern long atoi0();
5547c478bd9Sstevel@tonic-gate extern long atoi1();
5557c478bd9Sstevel@tonic-gate
5567c478bd9Sstevel@tonic-gate if (cbits(i = getch()) == '(')
5577c478bd9Sstevel@tonic-gate j = atoi0();
5587c478bd9Sstevel@tonic-gate else {
5597c478bd9Sstevel@tonic-gate j = atoi1(i);
5607c478bd9Sstevel@tonic-gate }
5617c478bd9Sstevel@tonic-gate return(j);
5627c478bd9Sstevel@tonic-gate }
5637c478bd9Sstevel@tonic-gate
5647c478bd9Sstevel@tonic-gate
atoi1(ii)5657c478bd9Sstevel@tonic-gate long atoi1(ii)
566e5190c10Smuffin tchar ii;
5677c478bd9Sstevel@tonic-gate {
568e5190c10Smuffin int i, j, digits;
569e5190c10Smuffin long acc;
5707c478bd9Sstevel@tonic-gate int neg, abs, field;
5717c478bd9Sstevel@tonic-gate
5727c478bd9Sstevel@tonic-gate neg = abs = field = digits = 0;
5737c478bd9Sstevel@tonic-gate acc = 0;
5747c478bd9Sstevel@tonic-gate for (;;) {
5757c478bd9Sstevel@tonic-gate i = cbits(ii);
5767c478bd9Sstevel@tonic-gate switch (i) {
5777c478bd9Sstevel@tonic-gate default:
5787c478bd9Sstevel@tonic-gate break;
5797c478bd9Sstevel@tonic-gate case '+':
5807c478bd9Sstevel@tonic-gate ii = getch();
5817c478bd9Sstevel@tonic-gate continue;
5827c478bd9Sstevel@tonic-gate case '-':
5837c478bd9Sstevel@tonic-gate neg = 1;
5847c478bd9Sstevel@tonic-gate ii = getch();
5857c478bd9Sstevel@tonic-gate continue;
5867c478bd9Sstevel@tonic-gate case '|':
5877c478bd9Sstevel@tonic-gate abs = 1 + neg;
5887c478bd9Sstevel@tonic-gate neg = 0;
5897c478bd9Sstevel@tonic-gate ii = getch();
5907c478bd9Sstevel@tonic-gate continue;
5917c478bd9Sstevel@tonic-gate }
5927c478bd9Sstevel@tonic-gate break;
5937c478bd9Sstevel@tonic-gate }
5947c478bd9Sstevel@tonic-gate a1:
5957c478bd9Sstevel@tonic-gate while (i >= '0' && i <= '9') {
5967c478bd9Sstevel@tonic-gate field++;
5977c478bd9Sstevel@tonic-gate digits++;
5987c478bd9Sstevel@tonic-gate acc = 10 * acc + i - '0';
5997c478bd9Sstevel@tonic-gate ii = getch();
6007c478bd9Sstevel@tonic-gate i = cbits(ii);
6017c478bd9Sstevel@tonic-gate }
6027c478bd9Sstevel@tonic-gate if (i == '.') {
6037c478bd9Sstevel@tonic-gate field++;
6047c478bd9Sstevel@tonic-gate digits = 0;
6057c478bd9Sstevel@tonic-gate ii = getch();
6067c478bd9Sstevel@tonic-gate i = cbits(ii);
6077c478bd9Sstevel@tonic-gate goto a1;
6087c478bd9Sstevel@tonic-gate }
6097c478bd9Sstevel@tonic-gate if (!field) {
6107c478bd9Sstevel@tonic-gate ch = ii;
6117c478bd9Sstevel@tonic-gate goto a2;
6127c478bd9Sstevel@tonic-gate }
6137c478bd9Sstevel@tonic-gate switch (i) {
6147c478bd9Sstevel@tonic-gate case 'u':
6157c478bd9Sstevel@tonic-gate i = j = 1; /* should this be related to HOR?? */
6167c478bd9Sstevel@tonic-gate break;
6177c478bd9Sstevel@tonic-gate case 'v': /*VSs - vert spacing*/
6187c478bd9Sstevel@tonic-gate j = lss;
6197c478bd9Sstevel@tonic-gate i = 1;
6207c478bd9Sstevel@tonic-gate break;
6217c478bd9Sstevel@tonic-gate case 'm': /*Ems*/
6227c478bd9Sstevel@tonic-gate j = EM;
6237c478bd9Sstevel@tonic-gate i = 1;
6247c478bd9Sstevel@tonic-gate break;
6257c478bd9Sstevel@tonic-gate case 'n': /*Ens*/
6267c478bd9Sstevel@tonic-gate j = EM;
6277c478bd9Sstevel@tonic-gate #ifndef NROFF
6287c478bd9Sstevel@tonic-gate i = 2;
6297c478bd9Sstevel@tonic-gate #endif
6307c478bd9Sstevel@tonic-gate #ifdef NROFF
6317c478bd9Sstevel@tonic-gate i = 1; /*Same as Ems in NROFF*/
6327c478bd9Sstevel@tonic-gate #endif
6337c478bd9Sstevel@tonic-gate break;
6347c478bd9Sstevel@tonic-gate case 'p': /*Points*/
6357c478bd9Sstevel@tonic-gate j = INCH;
6367c478bd9Sstevel@tonic-gate i = 72;
6377c478bd9Sstevel@tonic-gate break;
6387c478bd9Sstevel@tonic-gate case 'i': /*Inches*/
6397c478bd9Sstevel@tonic-gate j = INCH;
6407c478bd9Sstevel@tonic-gate i = 1;
6417c478bd9Sstevel@tonic-gate break;
6427c478bd9Sstevel@tonic-gate case 'c': /*Centimeters*/
6437c478bd9Sstevel@tonic-gate /* if INCH is too big, this will overflow */
6447c478bd9Sstevel@tonic-gate j = INCH * 50;
6457c478bd9Sstevel@tonic-gate i = 127;
6467c478bd9Sstevel@tonic-gate break;
6477c478bd9Sstevel@tonic-gate case 'P': /*Picas*/
6487c478bd9Sstevel@tonic-gate j = INCH;
6497c478bd9Sstevel@tonic-gate i = 6;
6507c478bd9Sstevel@tonic-gate break;
6517c478bd9Sstevel@tonic-gate default:
6527c478bd9Sstevel@tonic-gate j = dfact;
6537c478bd9Sstevel@tonic-gate ch = ii;
6547c478bd9Sstevel@tonic-gate i = dfactd;
6557c478bd9Sstevel@tonic-gate }
6562a8bcb4eSToomas Soome if (neg)
6577c478bd9Sstevel@tonic-gate acc = -acc;
6587c478bd9Sstevel@tonic-gate if (!noscale) {
6597c478bd9Sstevel@tonic-gate acc = (acc * j) / i;
6607c478bd9Sstevel@tonic-gate }
6617c478bd9Sstevel@tonic-gate if ((field != digits) && (digits > 0))
6627c478bd9Sstevel@tonic-gate while (digits--)
6637c478bd9Sstevel@tonic-gate acc /= 10;
6647c478bd9Sstevel@tonic-gate if (abs) {
6657c478bd9Sstevel@tonic-gate if (dip != d)
6662a8bcb4eSToomas Soome j = dip->dnl;
6672a8bcb4eSToomas Soome else
6687c478bd9Sstevel@tonic-gate j = numtab[NL].val;
6697c478bd9Sstevel@tonic-gate if (!vflag) {
6707c478bd9Sstevel@tonic-gate j = numtab[HP].val;
6717c478bd9Sstevel@tonic-gate }
6727c478bd9Sstevel@tonic-gate if (abs == 2)
6737c478bd9Sstevel@tonic-gate j = -j;
6747c478bd9Sstevel@tonic-gate acc -= j;
6757c478bd9Sstevel@tonic-gate }
6767c478bd9Sstevel@tonic-gate a2:
6777c478bd9Sstevel@tonic-gate nonumb = !field;
6787c478bd9Sstevel@tonic-gate return(acc);
6797c478bd9Sstevel@tonic-gate }
6807c478bd9Sstevel@tonic-gate
6817c478bd9Sstevel@tonic-gate
682e5190c10Smuffin int
caserr()6837c478bd9Sstevel@tonic-gate caserr()
6847c478bd9Sstevel@tonic-gate {
685e5190c10Smuffin int i, j;
686e5190c10Smuffin struct numtab *p;
6877c478bd9Sstevel@tonic-gate
6887c478bd9Sstevel@tonic-gate lgf++;
6897c478bd9Sstevel@tonic-gate while (!skip() && (i = getrq()) ) {
6907c478bd9Sstevel@tonic-gate j = usedr(i);
6917c478bd9Sstevel@tonic-gate if (j < 0)
6927c478bd9Sstevel@tonic-gate continue;
6937c478bd9Sstevel@tonic-gate p = &numtab[j];
6947c478bd9Sstevel@tonic-gate nunhash(p);
6957c478bd9Sstevel@tonic-gate p->r = p->val = p->inc = p->fmt = 0;
6967c478bd9Sstevel@tonic-gate regcnt--;
6977c478bd9Sstevel@tonic-gate }
698e5190c10Smuffin
699e5190c10Smuffin return (0);
7007c478bd9Sstevel@tonic-gate }
7017c478bd9Sstevel@tonic-gate
7027c478bd9Sstevel@tonic-gate
703e5190c10Smuffin int
casenr()7047c478bd9Sstevel@tonic-gate casenr()
7057c478bd9Sstevel@tonic-gate {
706e5190c10Smuffin int i, j;
7077c478bd9Sstevel@tonic-gate
7087c478bd9Sstevel@tonic-gate lgf++;
7097c478bd9Sstevel@tonic-gate skip();
7107c478bd9Sstevel@tonic-gate if ((i = findr(getrq())) == -1)
7117c478bd9Sstevel@tonic-gate goto rtn;
7127c478bd9Sstevel@tonic-gate skip();
7137c478bd9Sstevel@tonic-gate j = inumb(&numtab[i].val);
7147c478bd9Sstevel@tonic-gate if (nonumb)
7157c478bd9Sstevel@tonic-gate goto rtn;
7167c478bd9Sstevel@tonic-gate numtab[i].val = j;
7177c478bd9Sstevel@tonic-gate skip();
7187c478bd9Sstevel@tonic-gate j = atoi();
7197c478bd9Sstevel@tonic-gate if (nonumb)
7207c478bd9Sstevel@tonic-gate goto rtn;
7217c478bd9Sstevel@tonic-gate numtab[i].inc = j;
7227c478bd9Sstevel@tonic-gate rtn:
723e5190c10Smuffin return (0);
7247c478bd9Sstevel@tonic-gate }
7257c478bd9Sstevel@tonic-gate
7267c478bd9Sstevel@tonic-gate
727e5190c10Smuffin int
caseaf()7287c478bd9Sstevel@tonic-gate caseaf()
7297c478bd9Sstevel@tonic-gate {
730e5190c10Smuffin int i, k;
731e5190c10Smuffin tchar j, jj;
7327c478bd9Sstevel@tonic-gate
7337c478bd9Sstevel@tonic-gate lgf++;
7347c478bd9Sstevel@tonic-gate if (skip() || !(i = getrq()) || skip())
735e5190c10Smuffin return (0);
7367c478bd9Sstevel@tonic-gate k = 0;
7377c478bd9Sstevel@tonic-gate j = getch();
7387c478bd9Sstevel@tonic-gate if (!ischar(jj = cbits(j)) || !isalpha(jj)) {
7397c478bd9Sstevel@tonic-gate ch = j;
7407c478bd9Sstevel@tonic-gate while ((j = cbits(getch())) >= '0' && j <= '9')
7417c478bd9Sstevel@tonic-gate k++;
7427c478bd9Sstevel@tonic-gate }
7437c478bd9Sstevel@tonic-gate if (!k)
7447c478bd9Sstevel@tonic-gate k = j;
7457c478bd9Sstevel@tonic-gate numtab[findr(i)].fmt = k & BYTEMASK;
746e5190c10Smuffin
747e5190c10Smuffin return (0);
7487c478bd9Sstevel@tonic-gate }
7497c478bd9Sstevel@tonic-gate
750e5190c10Smuffin int
setaf()7517c478bd9Sstevel@tonic-gate setaf() /* return format of number register */
7527c478bd9Sstevel@tonic-gate {
753e5190c10Smuffin int i, j;
7547c478bd9Sstevel@tonic-gate
7557c478bd9Sstevel@tonic-gate i = usedr(getsn());
7567c478bd9Sstevel@tonic-gate if (i == -1)
757e5190c10Smuffin return (0);
7587c478bd9Sstevel@tonic-gate if (numtab[i].fmt > 20) /* it was probably a, A, i or I */
7597c478bd9Sstevel@tonic-gate *pbp++ = numtab[i].fmt;
7607c478bd9Sstevel@tonic-gate else
7617c478bd9Sstevel@tonic-gate for (j = (numtab[i].fmt ? numtab[i].fmt : 1); j; j--)
7627c478bd9Sstevel@tonic-gate *pbp++ = '0';
763e5190c10Smuffin
764e5190c10Smuffin return (0);
7657c478bd9Sstevel@tonic-gate }
7667c478bd9Sstevel@tonic-gate
7677c478bd9Sstevel@tonic-gate
768e5190c10Smuffin int
vnumb(i)7697c478bd9Sstevel@tonic-gate vnumb(i)
7707c478bd9Sstevel@tonic-gate int *i;
7717c478bd9Sstevel@tonic-gate {
7727c478bd9Sstevel@tonic-gate vflag++;
7737c478bd9Sstevel@tonic-gate dfact = lss;
7747c478bd9Sstevel@tonic-gate res = VERT;
7757c478bd9Sstevel@tonic-gate return(inumb(i));
7767c478bd9Sstevel@tonic-gate }
7777c478bd9Sstevel@tonic-gate
7787c478bd9Sstevel@tonic-gate
779e5190c10Smuffin int
hnumb(i)7807c478bd9Sstevel@tonic-gate hnumb(i)
7817c478bd9Sstevel@tonic-gate int *i;
7827c478bd9Sstevel@tonic-gate {
7837c478bd9Sstevel@tonic-gate dfact = EM;
7847c478bd9Sstevel@tonic-gate res = HOR;
7857c478bd9Sstevel@tonic-gate return(inumb(i));
7867c478bd9Sstevel@tonic-gate }
7877c478bd9Sstevel@tonic-gate
7887c478bd9Sstevel@tonic-gate
789e5190c10Smuffin int
inumb(n)7907c478bd9Sstevel@tonic-gate inumb(n)
7917c478bd9Sstevel@tonic-gate int *n;
7927c478bd9Sstevel@tonic-gate {
793e5190c10Smuffin int i, j, f;
794e5190c10Smuffin tchar ii;
7957c478bd9Sstevel@tonic-gate
7967c478bd9Sstevel@tonic-gate f = 0;
7977c478bd9Sstevel@tonic-gate if (n) {
7987c478bd9Sstevel@tonic-gate if ((j = cbits(ii = getch())) == '+')
7997c478bd9Sstevel@tonic-gate f = 1;
8007c478bd9Sstevel@tonic-gate else if (j == '-')
8017c478bd9Sstevel@tonic-gate f = -1;
8022a8bcb4eSToomas Soome else
8037c478bd9Sstevel@tonic-gate ch = ii;
8047c478bd9Sstevel@tonic-gate }
8057c478bd9Sstevel@tonic-gate i = atoi();
8067c478bd9Sstevel@tonic-gate if (n && f)
8077c478bd9Sstevel@tonic-gate i = *n + f * i;
8087c478bd9Sstevel@tonic-gate i = quant(i, res);
8097c478bd9Sstevel@tonic-gate vflag = 0;
8107c478bd9Sstevel@tonic-gate res = dfactd = dfact = 1;
8117c478bd9Sstevel@tonic-gate if (nonumb)
8127c478bd9Sstevel@tonic-gate i = 0;
8137c478bd9Sstevel@tonic-gate return(i);
8147c478bd9Sstevel@tonic-gate }
8157c478bd9Sstevel@tonic-gate
8167c478bd9Sstevel@tonic-gate
817e5190c10Smuffin int
quant(n,m)8187c478bd9Sstevel@tonic-gate quant(n, m)
8197c478bd9Sstevel@tonic-gate int n, m;
8207c478bd9Sstevel@tonic-gate {
821e5190c10Smuffin int i, neg;
8227c478bd9Sstevel@tonic-gate
8237c478bd9Sstevel@tonic-gate neg = 0;
8247c478bd9Sstevel@tonic-gate if (n < 0) {
8257c478bd9Sstevel@tonic-gate neg++;
8267c478bd9Sstevel@tonic-gate n = -n;
8277c478bd9Sstevel@tonic-gate }
8287c478bd9Sstevel@tonic-gate /* better as i = ((n + (m/2))/m)*m */
8297c478bd9Sstevel@tonic-gate i = n / m;
8307c478bd9Sstevel@tonic-gate if ((n - m * i) > (m / 2))
8317c478bd9Sstevel@tonic-gate i += 1;
8327c478bd9Sstevel@tonic-gate i *= m;
8337c478bd9Sstevel@tonic-gate if (neg)
8347c478bd9Sstevel@tonic-gate i = -i;
8357c478bd9Sstevel@tonic-gate return(i);
8367c478bd9Sstevel@tonic-gate }
8377c478bd9Sstevel@tonic-gate
8387c478bd9Sstevel@tonic-gate
839