115381Sslatteng #include	<ctype.h>
215381Sslatteng #include "tdef.h"
315381Sslatteng extern
415381Sslatteng #include "d.h"
515381Sslatteng extern
615381Sslatteng #include "v.h"
715381Sslatteng #ifdef NROFF
815381Sslatteng extern
915381Sslatteng #include "tw.h"
1015381Sslatteng #endif
1115381Sslatteng #include "s.h"
1215381Sslatteng /*
1315381Sslatteng troff4.c
1415381Sslatteng 
1515381Sslatteng number registers, conversion, arithmetic
1615381Sslatteng */
1715381Sslatteng 
1815381Sslatteng #include <sgtty.h>
1915381Sslatteng #include "ext.h"
2015381Sslatteng 
2115381Sslatteng int	regcnt = NNAMES;
2215381Sslatteng int	falsef	= 0;	/* on if inside false branch of if */
2315381Sslatteng 
setn()2415381Sslatteng setn()
2515381Sslatteng {
2615381Sslatteng 	register i, j;
2715381Sslatteng 	tchar ii;
2815381Sslatteng 	int	f;
2915381Sslatteng 
3015381Sslatteng 	f = nform = 0;
31*15400Sslatteng 	lgf++;					/* don't catch ligatures */
3215381Sslatteng 	if ((i = cbits(ii = getch())) == '+')
3315381Sslatteng 		f = 1;
3415381Sslatteng 	else if (i == '-')
3515381Sslatteng 		f = -1;
3615381Sslatteng 	else
3715381Sslatteng 		ch = ii;
38*15400Sslatteng 	lgf--;					/* ok, catch `em again */
3915381Sslatteng 	if (falsef)
4015381Sslatteng 		f = 0;
4115381Sslatteng 	if ((i = getsn()) == 0)
4215381Sslatteng 		return;
4315381Sslatteng 	if ((i & 0177) == '.')
4415381Sslatteng 		switch (i >> BYTE) {
4515381Sslatteng 		case 's':
4615381Sslatteng 			i = pts;
4715381Sslatteng 			break;
4815381Sslatteng 		case 'v':
4915381Sslatteng 			i = lss;
5015381Sslatteng 			break;
5115381Sslatteng 		case 'f':
5215381Sslatteng 			i = font;
5315381Sslatteng 			break;
5415381Sslatteng 		case 'p':
5515381Sslatteng 			i = pl;
5615381Sslatteng 			break;
5715381Sslatteng 		case 't':
5815381Sslatteng 			i = findt1();
5915381Sslatteng 			break;
6015381Sslatteng 		case 'o':
6115381Sslatteng 			i = po;
6215381Sslatteng 			break;
6315381Sslatteng 		case 'l':
6415381Sslatteng 			i = ll;
6515381Sslatteng 			break;
6615381Sslatteng 		case 'i':
6715381Sslatteng 			i = in;
6815381Sslatteng 			break;
6915381Sslatteng 		case '$':
7015381Sslatteng 			i = frame->nargs;
7115381Sslatteng 			break;
7215381Sslatteng 		case 'A':
7315381Sslatteng 			i = ascii;
7415381Sslatteng 			break;
7515381Sslatteng 		case 'c':
7615381Sslatteng 			i = v.cd;
7715381Sslatteng 			break;
7815381Sslatteng 		case 'n':
7915381Sslatteng 			i = lastl;
8015381Sslatteng 			break;
8115381Sslatteng 		case 'a':
8215381Sslatteng 			i = ralss;
8315381Sslatteng 			break;
8415381Sslatteng 		case 'h':
8515381Sslatteng 			i = dip->hnl;
8615381Sslatteng 			break;
8715381Sslatteng 		case 'd':
8815381Sslatteng 			if (dip != d)
8915381Sslatteng 				i = dip->dnl;
9015381Sslatteng 			else
9115381Sslatteng 				i = v.nl;
9215381Sslatteng 			break;
9315381Sslatteng 		case 'u':
9415381Sslatteng 			i = fi;
9515381Sslatteng 			break;
9615381Sslatteng 		case 'j':
9715381Sslatteng 			i = ad + 2 * admod;
9815381Sslatteng 			break;
9915381Sslatteng 		case 'w':
10015381Sslatteng 			i = width(*(pinchar-1));	/* XXX */
10115381Sslatteng 			break;
10215381Sslatteng 		case 'x':
10315381Sslatteng 			i = nel;
10415381Sslatteng 			break;
10515381Sslatteng 		case 'y':
10615381Sslatteng 			i = un;
10715381Sslatteng 			break;
10815381Sslatteng 		case 'T':
10915381Sslatteng 			i = dotT;
11015381Sslatteng 			break; /*-Tterm used in nroff*/
11115381Sslatteng 		case 'V':
11215381Sslatteng 			i = VERT;
11315381Sslatteng 			break;
11415381Sslatteng 		case 'H':
11515381Sslatteng 			i = HOR;
11615381Sslatteng 			break;
11715381Sslatteng 		case 'k':
11815381Sslatteng 			i = ne;
11915381Sslatteng 			break;
12015381Sslatteng 		case 'P':
12115381Sslatteng 			i = print;
12215381Sslatteng 			break;
12315381Sslatteng 		case 'L':
12415381Sslatteng 			i = ls;
12515381Sslatteng 			break;
12615381Sslatteng 		case 'R':
12715381Sslatteng 			i = NN - regcnt;
12815381Sslatteng 			break;
12915381Sslatteng 		case 'z':
13015381Sslatteng 			i = dip->curd;
13115381Sslatteng 			cbuf[0] = i & BMASK;
13215381Sslatteng 			cbuf[1] = (i >> BYTE) & BMASK;
13315381Sslatteng 			cbuf[2] = 0;
13415381Sslatteng 			cp = cbuf;
13515381Sslatteng 			return;
13615381Sslatteng 		case 'b':
13715381Sslatteng 			i = bdtab[font];
13815381Sslatteng 			break;
13915381Sslatteng 
14015381Sslatteng 		default:
14115381Sslatteng 			goto s0;
14215381Sslatteng 		}
14315381Sslatteng 	else {
14415381Sslatteng s0:
14515381Sslatteng 		if ((j = findr(i)) == -1)
14615381Sslatteng 			i = 0;
14715381Sslatteng 		else {
14815381Sslatteng 			i = (vlist[j] = (vlist[j] + inc[j] * f));
14915381Sslatteng 			nform = fmt[j];
15015381Sslatteng 		}
15115381Sslatteng 	}
15215381Sslatteng 	setn1(i);
15315381Sslatteng 	cp = cbuf;
15415381Sslatteng }
15515381Sslatteng 
15615381Sslatteng 
setn1(i)15715381Sslatteng setn1(i)
15815381Sslatteng int	i;
15915381Sslatteng {
16015381Sslatteng 	extern int	wrc();
16115381Sslatteng 
16215381Sslatteng 	cp = cbuf;
16315381Sslatteng 	nrbits = 0;
16415381Sslatteng 	fnumb(i, wrc);
16515381Sslatteng 	*cp = 0;
16615381Sslatteng 	cp = cbuf;
16715381Sslatteng }
16815381Sslatteng 
16915381Sslatteng 
findr(i)17015381Sslatteng findr(i)
17115381Sslatteng register int	i;
17215381Sslatteng {
17315381Sslatteng 	register j;
17415381Sslatteng 	register int *p;
17515381Sslatteng 
17615381Sslatteng 	if (i == 0)
17715381Sslatteng 		return(-1);
17815381Sslatteng 	for (p = r; p < &r[NN]; p++) {
17915381Sslatteng 		if (i == *p)
18015381Sslatteng 			break;
18115381Sslatteng 	}
18215381Sslatteng 	if (p != &r[NN])
18315381Sslatteng 		return(p - r);
18415381Sslatteng 	for (p = r; p < &r[NN]; p++) {
18515381Sslatteng 		if (*p == 0) {
18615381Sslatteng 			*p = i;
18715381Sslatteng 			regcnt++;
18815381Sslatteng 			break;
18915381Sslatteng 		}
19015381Sslatteng 	}
19115381Sslatteng 	if (p == &r[NN]) {
19215381Sslatteng 		fprintf(stderr, "troff: too many number registers (%d).\n", NN);
19315381Sslatteng 		done2(04);
19415381Sslatteng 	}
19515381Sslatteng 	return(p - r);
19615381Sslatteng }
19715381Sslatteng 
usedr(i)19815381Sslatteng usedr(i)	/* returns -1 if nr i has never been used */
19915381Sslatteng register int	i;
20015381Sslatteng {
20115381Sslatteng 	register j;
20215381Sslatteng 	register int *p;
20315381Sslatteng 
20415381Sslatteng 	if (i == 0)
20515381Sslatteng 		return(-1);
20615381Sslatteng 	for (p = r; p < &r[NN]; p++) {
20715381Sslatteng 		if (i == *p)
20815381Sslatteng 			break;
20915381Sslatteng 	}
21015381Sslatteng 	if (p != &r[NN])
21115381Sslatteng 		return(p - r);
21215381Sslatteng 	else
21315381Sslatteng 		return -1;
21415381Sslatteng }
21515381Sslatteng 
21615381Sslatteng 
21715381Sslatteng fnumb(i, f)
21815381Sslatteng int	i, (*f)();
21915381Sslatteng {
22015381Sslatteng 	register j;
22115381Sslatteng 
22215381Sslatteng 	j = 0;
22315381Sslatteng 	if (i < 0) {
22415381Sslatteng 		j = (*f)('-' | nrbits);
22515381Sslatteng 		i = -i;
22615381Sslatteng 	}
22715381Sslatteng 	switch (nform) {
22815381Sslatteng 	default:
22915381Sslatteng 	case '1':
23015381Sslatteng 	case 0:
23115381Sslatteng 		return(decml(i, f) + j);
23215381Sslatteng 	case 'i':
23315381Sslatteng 	case 'I':
23415381Sslatteng 		return(roman(i, f) + j);
23515381Sslatteng 	case 'a':
23615381Sslatteng 	case 'A':
23715381Sslatteng 		return(abc(i, f) + j);
23815381Sslatteng 	}
23915381Sslatteng }
24015381Sslatteng 
24115381Sslatteng 
24215381Sslatteng decml(i, f)
24315381Sslatteng int	i, (*f)();
24415381Sslatteng {
24515381Sslatteng 	register j, k;
24615381Sslatteng 
24715381Sslatteng 	k = 0;
24815381Sslatteng 	nform--;
24915381Sslatteng 	if ((j = i / 10) || (nform > 0))
25015381Sslatteng 		k = decml(j, f);
25115381Sslatteng 	return(k + (*f)((i % 10 + '0') | nrbits));
25215381Sslatteng }
25315381Sslatteng 
25415381Sslatteng 
25515381Sslatteng roman(i, f)
25615381Sslatteng int	i, (*f)();
25715381Sslatteng {
25815381Sslatteng 
25915381Sslatteng 	if (!i)
26015381Sslatteng 		return((*f)('0' | nrbits));
26115381Sslatteng 	if (nform == 'i')
26215381Sslatteng 		return(roman0(i, f, "ixcmz", "vldw"));
26315381Sslatteng 	else
26415381Sslatteng 		return(roman0(i, f, "IXCMZ", "VLDW"));
26515381Sslatteng }
26615381Sslatteng 
26715381Sslatteng 
26815381Sslatteng roman0(i, f, onesp, fivesp)
26915381Sslatteng int	i, (*f)();
27015381Sslatteng char	*onesp, *fivesp;
27115381Sslatteng {
27215381Sslatteng 	register q, rem, k;
27315381Sslatteng 
27415381Sslatteng 	k = 0;
27515381Sslatteng 	if (!i)
27615381Sslatteng 		return(0);
27715381Sslatteng 	k = roman0(i / 10, f, onesp + 1, fivesp + 1);
27815381Sslatteng 	q = (i = i % 10) / 5;
27915381Sslatteng 	rem = i % 5;
28015381Sslatteng 	if (rem == 4) {
28115381Sslatteng 		k += (*f)(*onesp | nrbits);
28215381Sslatteng 		if (q)
28315381Sslatteng 			i = *(onesp + 1);
28415381Sslatteng 		else
28515381Sslatteng 			i = *fivesp;
28615381Sslatteng 		return(k += (*f)(i | nrbits));
28715381Sslatteng 	}
28815381Sslatteng 	if (q)
28915381Sslatteng 		k += (*f)(*fivesp | nrbits);
29015381Sslatteng 	while (--rem >= 0)
29115381Sslatteng 		k += (*f)(*onesp | nrbits);
29215381Sslatteng 	return(k);
29315381Sslatteng }
29415381Sslatteng 
29515381Sslatteng 
29615381Sslatteng abc(i, f)
29715381Sslatteng int	i, (*f)();
29815381Sslatteng {
29915381Sslatteng 	if (!i)
30015381Sslatteng 		return((*f)('0' | nrbits));
30115381Sslatteng 	else
30215381Sslatteng 		return(abc0(i - 1, f));
30315381Sslatteng }
30415381Sslatteng 
30515381Sslatteng 
30615381Sslatteng abc0(i, f)
30715381Sslatteng int	i, (*f)();
30815381Sslatteng {
30915381Sslatteng 	register j, k;
31015381Sslatteng 
31115381Sslatteng 	k = 0;
31215381Sslatteng 	if (j = i / 26)
31315381Sslatteng 		k = abc0(j - 1, f);
31415381Sslatteng 	return(k + (*f)((i % 26 + nform) | nrbits));
31515381Sslatteng }
31615381Sslatteng 
31715381Sslatteng 
wrc(i)31815381Sslatteng wrc(i)
31915381Sslatteng tchar i;
32015381Sslatteng {
32115381Sslatteng 	if (cp >= &cbuf[NC])
32215381Sslatteng 		return(0);
32315381Sslatteng 	*cp++ = i;
32415381Sslatteng 	return(1);
32515381Sslatteng }
32615381Sslatteng 
32715381Sslatteng 
atoi0()32815381Sslatteng long	atoi0()
32915381Sslatteng {
33015381Sslatteng 	register c, k, cnt;
33115381Sslatteng 	tchar ii;
33215381Sslatteng 	long	i, acc;
33315381Sslatteng 	extern long	ckph();
33415381Sslatteng 
33515381Sslatteng 	i = 0;
33615381Sslatteng 	acc = 0;
33715381Sslatteng 	nonumb = 0;
33815381Sslatteng 	cnt = -1;
33915381Sslatteng a0:
34015381Sslatteng 	cnt++;
34115381Sslatteng 	ii = getch();
34215381Sslatteng 	c = cbits(ii);
34315381Sslatteng 	switch (c) {
34415381Sslatteng 	default:
34515381Sslatteng 		ch = ii;
34615381Sslatteng 		if (cnt)
34715381Sslatteng 			break;
34815381Sslatteng 	case '+':
34915381Sslatteng 		i = ckph();
35015381Sslatteng 		if (nonumb)
35115381Sslatteng 			break;
35215381Sslatteng 		acc += i;
35315381Sslatteng 		goto a0;
35415381Sslatteng 	case '-':
35515381Sslatteng 		i = ckph();
35615381Sslatteng 		if (nonumb)
35715381Sslatteng 			break;
35815381Sslatteng 		acc -= i;
35915381Sslatteng 		goto a0;
36015381Sslatteng 	case '*':
36115381Sslatteng 		i = ckph();
36215381Sslatteng 		if (nonumb)
36315381Sslatteng 			break;
36415381Sslatteng 		acc *= i;
36515381Sslatteng 		goto a0;
36615381Sslatteng 	case '/':
36715381Sslatteng 		i = ckph();
36815381Sslatteng 		if (nonumb)
36915381Sslatteng 			break;
37015381Sslatteng 		if (i == 0) {
37115381Sslatteng 			flusho();
37215381Sslatteng 			fprintf(stderr, "troff: divide by zero.\n");
37315381Sslatteng 			acc = 0;
37415381Sslatteng 		} else
37515381Sslatteng 			acc /= i;
37615381Sslatteng 		goto a0;
37715381Sslatteng 	case '%':
37815381Sslatteng 		i = ckph();
37915381Sslatteng 		if (nonumb)
38015381Sslatteng 			break;
38115381Sslatteng 		acc %= i;
38215381Sslatteng 		goto a0;
38315381Sslatteng 	case '&':	/*and*/
38415381Sslatteng 		i = ckph();
38515381Sslatteng 		if (nonumb)
38615381Sslatteng 			break;
38715381Sslatteng 		if ((acc > 0) && (i > 0))
38815381Sslatteng 			acc = 1;
38915381Sslatteng 		else
39015381Sslatteng 			acc = 0;
39115381Sslatteng 		goto a0;
39215381Sslatteng 	case ':':	/*or*/
39315381Sslatteng 		i = ckph();
39415381Sslatteng 		if (nonumb)
39515381Sslatteng 			break;
39615381Sslatteng 		if ((acc > 0) || (i > 0))
39715381Sslatteng 			acc = 1;
39815381Sslatteng 		else
39915381Sslatteng 			acc = 0;
40015381Sslatteng 		goto a0;
40115381Sslatteng 	case '=':
40215381Sslatteng 		if (cbits(ii = getch()) != '=')
40315381Sslatteng 			ch = ii;
40415381Sslatteng 		i = ckph();
40515381Sslatteng 		if (nonumb) {
40615381Sslatteng 			acc = 0;
40715381Sslatteng 			break;
40815381Sslatteng 		}
40915381Sslatteng 		if (i == acc)
41015381Sslatteng 			acc = 1;
41115381Sslatteng 		else
41215381Sslatteng 			acc = 0;
41315381Sslatteng 		goto a0;
41415381Sslatteng 	case '>':
41515381Sslatteng 		k = 0;
41615381Sslatteng 		if (cbits(ii = getch()) == '=')
41715381Sslatteng 			k++;
41815381Sslatteng 		else
41915381Sslatteng 			ch = ii;
42015381Sslatteng 		i = ckph();
42115381Sslatteng 		if (nonumb) {
42215381Sslatteng 			acc = 0;
42315381Sslatteng 			break;
42415381Sslatteng 		}
42515381Sslatteng 		if (acc > (i - k))
42615381Sslatteng 			acc = 1;
42715381Sslatteng 		else
42815381Sslatteng 			acc = 0;
42915381Sslatteng 		goto a0;
43015381Sslatteng 	case '<':
43115381Sslatteng 		k = 0;
43215381Sslatteng 		if (cbits(ii = getch()) == '=')
43315381Sslatteng 			k++;
43415381Sslatteng 		else
43515381Sslatteng 			ch = ii;
43615381Sslatteng 		i = ckph();
43715381Sslatteng 		if (nonumb) {
43815381Sslatteng 			acc = 0;
43915381Sslatteng 			break;
44015381Sslatteng 		}
44115381Sslatteng 		if (acc < (i + k))
44215381Sslatteng 			acc = 1;
44315381Sslatteng 		else
44415381Sslatteng 			acc = 0;
44515381Sslatteng 		goto a0;
44615381Sslatteng 	case ')':
44715381Sslatteng 		break;
44815381Sslatteng 	case '(':
44915381Sslatteng 		acc = atoi0();
45015381Sslatteng 		goto a0;
45115381Sslatteng 	}
45215381Sslatteng 	return(acc);
45315381Sslatteng }
45415381Sslatteng 
45515381Sslatteng 
ckph()45615381Sslatteng long	ckph()
45715381Sslatteng {
45815381Sslatteng 	tchar i;
45915381Sslatteng 	long	j;
46015381Sslatteng 	extern long	atoi0();
46115381Sslatteng 	extern long	atoi1();
46215381Sslatteng 
46315381Sslatteng 	if (cbits(i = getch()) == '(')
46415381Sslatteng 		j = atoi0();
46515381Sslatteng 	else {
46615381Sslatteng 		ch = i;
46715381Sslatteng 		j = atoi1();
46815381Sslatteng 	}
46915381Sslatteng 	return(j);
47015381Sslatteng }
47115381Sslatteng 
47215381Sslatteng 
atoi1()47315381Sslatteng long	atoi1()
47415381Sslatteng {
47515381Sslatteng 	register i, j, digits;
47615381Sslatteng 	tchar ii;
47715381Sslatteng 	long	acc;
47815381Sslatteng 	int	neg, abs, field;
47915381Sslatteng 
48015381Sslatteng 	neg = abs = field = digits = 0;
48115381Sslatteng 	acc = 0;
48215381Sslatteng a0:
48315381Sslatteng 	ii = getch();
48415381Sslatteng 	i = cbits(ii);
48515381Sslatteng 	switch (i) {
48615381Sslatteng 	default:
48715381Sslatteng 		ch = ii;
48815381Sslatteng 		break;
48915381Sslatteng 	case '+':
49015381Sslatteng 		goto a0;
49115381Sslatteng 	case '-':
49215381Sslatteng 		neg = 1;
49315381Sslatteng 		goto a0;
49415381Sslatteng 	case '|':
49515381Sslatteng 		abs = 1 + neg;
49615381Sslatteng 		neg = 0;
49715381Sslatteng 		goto a0;
49815381Sslatteng 	}
49915381Sslatteng a1:
50015381Sslatteng 	while (((j = (cbits(ii = getch())) - '0') >= 0) && (j <= 9)) {
50115381Sslatteng 		field++;
50215381Sslatteng 		digits++;
50315381Sslatteng 		acc = 10 * acc + j;
50415381Sslatteng 	}
50515381Sslatteng 	if (cbits(ii) == '.') {
50615381Sslatteng 		field++;
50715381Sslatteng 		digits = 0;
50815381Sslatteng 		goto a1;
50915381Sslatteng 	}
51015381Sslatteng 	ch = ii;
51115381Sslatteng 	if (!field)
51215381Sslatteng 		goto a2;
51315381Sslatteng 	ii = getch();
51415381Sslatteng 	switch (i = cbits(ii)) {
51515381Sslatteng 	case 'u':
51615381Sslatteng 		i = j = 1;	/* should this be related to HOR?? */
51715381Sslatteng 		break;
51815381Sslatteng 	case 'v':	/*VSs - vert spacing*/
51915381Sslatteng 		j = lss;
52015381Sslatteng 		i = 1;
52115381Sslatteng 		break;
52215381Sslatteng 	case 'm':	/*Ems*/
52315381Sslatteng 		j = EM;
52415381Sslatteng 		i = 1;
52515381Sslatteng 		break;
52615381Sslatteng 	case 'n':	/*Ens*/
52715381Sslatteng 		j = EM;
52815381Sslatteng #ifndef NROFF
52915381Sslatteng 		i = 2;
53015381Sslatteng #endif
53115381Sslatteng #ifdef NROFF
53215381Sslatteng 		i = 1;	/*Same as Ems in NROFF*/
53315381Sslatteng #endif
53415381Sslatteng 		break;
53515381Sslatteng 	case 'p':	/*Points*/
53615381Sslatteng 		j = INCH;
53715381Sslatteng 		i = 72;
53815381Sslatteng 		break;
53915381Sslatteng 	case 'i':	/*Inches*/
54015381Sslatteng 		j = INCH;
54115381Sslatteng 		i = 1;
54215381Sslatteng 		break;
54315381Sslatteng 	case 'c':	/*Centimeters*/
54415381Sslatteng 		/* if INCH is too big, this will overflow */
54515381Sslatteng 		j = INCH * 50;
54615381Sslatteng 		i = 127;
54715381Sslatteng 		break;
54815381Sslatteng 	case 'P':	/*Picas*/
54915381Sslatteng 		j = INCH;
55015381Sslatteng 		i = 6;
55115381Sslatteng 		break;
55215381Sslatteng 	default:
55315381Sslatteng 		j = dfact;
55415381Sslatteng 		ch = ii;
55515381Sslatteng 		i = dfactd;
55615381Sslatteng 	}
55715381Sslatteng 	if (neg)
55815381Sslatteng 		acc = -acc;
55915381Sslatteng 	if (!noscale) {
56015381Sslatteng 		acc = (acc * j) / i;
56115381Sslatteng 	}
56215381Sslatteng 	if ((field != digits) && (digits > 0))
56315381Sslatteng 		while (digits--)
56415381Sslatteng 			acc /= 10;
56515381Sslatteng 	if (abs) {
56615381Sslatteng 		if (dip != d)
56715381Sslatteng 			j = dip->dnl;
56815381Sslatteng 		else
56915381Sslatteng 			j = v.nl;
57015381Sslatteng 		if (!vflag) {
57115381Sslatteng 			j = v.hp = sumhp();	/* XXX */
57215381Sslatteng 		}
57315381Sslatteng 		if (abs == 2)
57415381Sslatteng 			j = -j;
57515381Sslatteng 		acc -= j;
57615381Sslatteng 	}
57715381Sslatteng a2:
57815381Sslatteng 	nonumb = !field;
57915381Sslatteng 	return(acc);
58015381Sslatteng }
58115381Sslatteng 
58215381Sslatteng 
caserr()58315381Sslatteng caserr()
58415381Sslatteng {
58515381Sslatteng 	register i, j;
58615381Sslatteng 
58715381Sslatteng 	lgf++;
58815381Sslatteng 	while (!skip() && (i = getrq()) ) {
58915381Sslatteng 		for (j = NNAMES; j < NN; j++) {  /*NNAMES predefined names*/
59015381Sslatteng 			if (i == r[j])
59115381Sslatteng 				break;
59215381Sslatteng 		}
59315381Sslatteng 		if (j != NN) {
59415381Sslatteng 			r[j] = vlist[j] = inc[j] = fmt[j] = 0;
59515381Sslatteng 			regcnt--;
59615381Sslatteng 		}
59715381Sslatteng 	}
59815381Sslatteng }
59915381Sslatteng 
60015381Sslatteng 
casenr()60115381Sslatteng casenr()
60215381Sslatteng {
60315381Sslatteng 	register i, j;
60415381Sslatteng 
60515381Sslatteng 	lgf++;
60615381Sslatteng 	skip();
60715381Sslatteng 	if ((i = findr(getrq())) == -1)
60815381Sslatteng 		goto rtn;
60915381Sslatteng 	skip();
61015381Sslatteng 	j = inumb(&vlist[i]);
61115381Sslatteng 	if (nonumb)
61215381Sslatteng 		goto rtn;
61315381Sslatteng 	vlist[i] = j;
61415381Sslatteng 	skip();
61515381Sslatteng 	j = atoi();
61615381Sslatteng 	if (nonumb)
61715381Sslatteng 		goto rtn;
61815381Sslatteng 	inc[i] = j;
61915381Sslatteng rtn:
62015381Sslatteng 	return;
62115381Sslatteng }
62215381Sslatteng 
62315381Sslatteng 
caseaf()62415381Sslatteng caseaf()
62515381Sslatteng {
62615381Sslatteng 	register i, k;
62715381Sslatteng 	tchar j;
62815381Sslatteng 
62915381Sslatteng 	lgf++;
63015381Sslatteng 	if (skip() || !(i = getrq()) || skip())
63115381Sslatteng 		return;
63215381Sslatteng 	k = 0;
63315381Sslatteng 	j = getch();
63415381Sslatteng 	if (!isalpha(cbits(j))) {
63515381Sslatteng 		ch = j;
63615381Sslatteng 		while ((j = cbits(getch())) >= '0' &&  j <= '9')
63715381Sslatteng 			k++;
63815381Sslatteng 	}
63915381Sslatteng 	if (!k)
64015381Sslatteng 		k = j;
64115381Sslatteng 	fmt[findr(i)] = k & BMASK;
64215381Sslatteng }
64315381Sslatteng 
setaf()64415381Sslatteng setaf()	/* return format of number register */
64515381Sslatteng {
64615381Sslatteng 	register int i, j;
64715381Sslatteng 
64815381Sslatteng 	i = usedr(getsn());
64915381Sslatteng 	if (i == -1)
65015381Sslatteng 		return;
65115381Sslatteng 	cp = cbuf;
65215381Sslatteng 	if (fmt[i] > 20)	/* it was probably a, A, i or I */
65315381Sslatteng 		*cp++ = fmt[i];
65415381Sslatteng 	else
65515381Sslatteng 		for (j = (fmt[i] ? fmt[i] : 1); j; j--)
65615381Sslatteng 			*cp++ = '0';
65715381Sslatteng 	*cp = 0;
65815381Sslatteng 	cp = cbuf;
65915381Sslatteng }
66015381Sslatteng 
66115381Sslatteng 
vnumb(i)66215381Sslatteng vnumb(i)
66315381Sslatteng int	*i;
66415381Sslatteng {
66515381Sslatteng 	vflag++;
66615381Sslatteng 	dfact = lss;
66715381Sslatteng 	res = VERT;
66815381Sslatteng 	return(inumb(i));
66915381Sslatteng }
67015381Sslatteng 
67115381Sslatteng 
hnumb(i)67215381Sslatteng hnumb(i)
67315381Sslatteng int	*i;
67415381Sslatteng {
67515381Sslatteng 	dfact = EM;
67615381Sslatteng 	res = HOR;
67715381Sslatteng 	return(inumb(i));
67815381Sslatteng }
67915381Sslatteng 
68015381Sslatteng 
inumb(n)68115381Sslatteng inumb(n)
68215381Sslatteng int	*n;
68315381Sslatteng {
68415381Sslatteng 	register i, j, f;
68515381Sslatteng 	tchar ii;
68615381Sslatteng 
68715381Sslatteng 	f = 0;
68815381Sslatteng 	if (n) {
68915381Sslatteng 		if ((j = cbits(ii = getch())) == '+')
69015381Sslatteng 			f = 1;
69115381Sslatteng 		else if (j == '-')
69215381Sslatteng 			f = -1;
69315381Sslatteng 		else
69415381Sslatteng 			ch = ii;
69515381Sslatteng 	}
69615381Sslatteng 	i = atoi();
69715381Sslatteng 	if (n && f)
69815381Sslatteng 		i = *n + f * i;
69915381Sslatteng 	i = quant(i, res);
70015381Sslatteng 	vflag = 0;
70115381Sslatteng 	res = dfactd = dfact = 1;
70215381Sslatteng 	if (nonumb)
70315381Sslatteng 		i = 0;
70415381Sslatteng 	return(i);
70515381Sslatteng }
70615381Sslatteng 
70715381Sslatteng 
quant(n,m)70815381Sslatteng quant(n, m)
70915381Sslatteng int	n, m;
71015381Sslatteng {
71115381Sslatteng 	register i, neg;
71215381Sslatteng 
71315381Sslatteng 	neg = 0;
71415381Sslatteng 	if (n < 0) {
71515381Sslatteng 		neg++;
71615381Sslatteng 		n = -n;
71715381Sslatteng 	}
71815381Sslatteng 	/* better as i = ((n + (m/2))/m)*m */
71915381Sslatteng 	i = n / m;
72015381Sslatteng 	if ((n - m * i) > (m / 2))
72115381Sslatteng 		i += 1;
72215381Sslatteng 	i *= m;
72315381Sslatteng 	if (neg)
72415381Sslatteng 		i = -i;
72515381Sslatteng 	return(i);
72615381Sslatteng }
72715381Sslatteng 
72815381Sslatteng 
729