xref: /onnv-gate/usr/src/cmd/troff/n7.c (revision 217:758a5315b0d7)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
230Sstevel@tonic-gate  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
280Sstevel@tonic-gate /*	  All Rights Reserved  	*/
290Sstevel@tonic-gate 
300Sstevel@tonic-gate /*
310Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
320Sstevel@tonic-gate  * The Regents of the University of California
330Sstevel@tonic-gate  * All Rights Reserved
340Sstevel@tonic-gate  *
350Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
360Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
370Sstevel@tonic-gate  * contributors.
380Sstevel@tonic-gate  */
390Sstevel@tonic-gate 
40*217Smuffin #pragma ident	"%Z%%M%	%I%	%E% SMI"
41*217Smuffin 
420Sstevel@tonic-gate #ifdef EUC
430Sstevel@tonic-gate #ifdef NROFF
440Sstevel@tonic-gate #include <stdlib.h>
450Sstevel@tonic-gate #include <widec.h>
460Sstevel@tonic-gate #include <limits.h>
470Sstevel@tonic-gate #endif /* NROFF */
480Sstevel@tonic-gate #endif /* EUC */
490Sstevel@tonic-gate #include "tdef.h"
500Sstevel@tonic-gate #ifdef NROFF
510Sstevel@tonic-gate #include "tw.h"
520Sstevel@tonic-gate #endif
530Sstevel@tonic-gate #ifdef NROFF
540Sstevel@tonic-gate #define GETCH gettch
550Sstevel@tonic-gate tchar	gettch();
560Sstevel@tonic-gate #endif
570Sstevel@tonic-gate #ifndef NROFF
580Sstevel@tonic-gate #define GETCH getch
590Sstevel@tonic-gate #endif
600Sstevel@tonic-gate 
610Sstevel@tonic-gate /*
620Sstevel@tonic-gate  * troff7.c
630Sstevel@tonic-gate  *
640Sstevel@tonic-gate  * text
650Sstevel@tonic-gate  */
660Sstevel@tonic-gate 
670Sstevel@tonic-gate #include <ctype.h>
680Sstevel@tonic-gate #ifdef EUC
690Sstevel@tonic-gate #ifdef NROFF
700Sstevel@tonic-gate #include <wctype.h>
710Sstevel@tonic-gate #endif /* NROFF */
720Sstevel@tonic-gate #endif /* EUC */
730Sstevel@tonic-gate #include "ext.h"
740Sstevel@tonic-gate #ifdef EUC
750Sstevel@tonic-gate #ifdef NROFF
760Sstevel@tonic-gate char	mbbuf2[MB_LEN_MAX + 1];
770Sstevel@tonic-gate char	*mbbuf2p = mbbuf2;
780Sstevel@tonic-gate tchar	mtbuf[MB_LEN_MAX + 1];
790Sstevel@tonic-gate tchar	*mtbufp;
800Sstevel@tonic-gate int	pendmb = 0;
810Sstevel@tonic-gate wchar_t	cwc, owc, wceoll;
820Sstevel@tonic-gate #endif /* NROFF */
830Sstevel@tonic-gate #endif /* EUC */
840Sstevel@tonic-gate int	brflg;
850Sstevel@tonic-gate 
86*217Smuffin int
tbreak()870Sstevel@tonic-gate tbreak()
880Sstevel@tonic-gate {
89*217Smuffin 	int	pad, k;
90*217Smuffin 	tchar	*i, j;
91*217Smuffin 	int resol = 0;
920Sstevel@tonic-gate #ifdef EUC
930Sstevel@tonic-gate #ifdef NROFF
94*217Smuffin 	tchar	l;
950Sstevel@tonic-gate #endif /* NROFF */
960Sstevel@tonic-gate #endif /* EUC */
970Sstevel@tonic-gate 
980Sstevel@tonic-gate 	trap = 0;
990Sstevel@tonic-gate 	if (nb)
100*217Smuffin 		return (0);
1010Sstevel@tonic-gate 	if (dip == d && numtab[NL].val == -1) {
1020Sstevel@tonic-gate 		newline(1);
103*217Smuffin 		return (0);
1040Sstevel@tonic-gate 	}
1050Sstevel@tonic-gate 	if (!nc) {
1060Sstevel@tonic-gate 		setnel();
1070Sstevel@tonic-gate 		if (!wch)
108*217Smuffin 			return (0);
1090Sstevel@tonic-gate 		if (pendw)
1100Sstevel@tonic-gate 			getword(1);
1110Sstevel@tonic-gate 		movword();
1120Sstevel@tonic-gate 	} else if (pendw && !brflg) {
1130Sstevel@tonic-gate 		getword(1);
1140Sstevel@tonic-gate 		movword();
1150Sstevel@tonic-gate 	}
1160Sstevel@tonic-gate 	*linep = dip->nls = 0;
1170Sstevel@tonic-gate #ifdef NROFF
1180Sstevel@tonic-gate 	if (dip == d)
1190Sstevel@tonic-gate 		horiz(po);
1200Sstevel@tonic-gate #endif
1210Sstevel@tonic-gate 	if (lnmod)
1220Sstevel@tonic-gate 		donum();
1230Sstevel@tonic-gate 	lastl = ne;
1240Sstevel@tonic-gate 	if (brflg != 1) {
1250Sstevel@tonic-gate 		totout = 0;
1260Sstevel@tonic-gate 	} else if (ad) {
1270Sstevel@tonic-gate 		if ((lastl = ll - un) < ne)
1280Sstevel@tonic-gate 			lastl = ne;
1290Sstevel@tonic-gate 	}
1300Sstevel@tonic-gate 	if (admod && ad && (brflg != 2)) {
1310Sstevel@tonic-gate 		lastl = ne;
1320Sstevel@tonic-gate 		adsp = adrem = 0;
1330Sstevel@tonic-gate 		if (admod == 1)
1340Sstevel@tonic-gate 			un +=  quant(nel / 2, HOR);
1350Sstevel@tonic-gate 		else if (admod == 2)
1360Sstevel@tonic-gate 			un += nel;
1370Sstevel@tonic-gate 	}
1380Sstevel@tonic-gate 	totout++;
1390Sstevel@tonic-gate 	brflg = 0;
1400Sstevel@tonic-gate 	if (lastl + un > dip->maxl)
1410Sstevel@tonic-gate 		dip->maxl = lastl + un;
1420Sstevel@tonic-gate 	horiz(un);
1430Sstevel@tonic-gate #ifdef NROFF
1440Sstevel@tonic-gate 	if (adrem % t.Adj)
1450Sstevel@tonic-gate 		resol = t.Hor;
1460Sstevel@tonic-gate 	else
1470Sstevel@tonic-gate 		resol = t.Adj;
1480Sstevel@tonic-gate #else
1490Sstevel@tonic-gate 	resol = HOR;
1500Sstevel@tonic-gate #endif
1510Sstevel@tonic-gate 	adrem = (adrem / resol) * resol;
1520Sstevel@tonic-gate 	for (i = line; nc > 0; ) {
1530Sstevel@tonic-gate #ifndef EUC
1540Sstevel@tonic-gate 		if ((cbits(j = *i++)) == ' ') {
1550Sstevel@tonic-gate #else
1560Sstevel@tonic-gate #ifndef NROFF
1570Sstevel@tonic-gate 		if ((cbits(j = *i++)) == ' ') {
1580Sstevel@tonic-gate #else
1590Sstevel@tonic-gate 		if ((cbits(j = *i++) & ~MBMASK) == ' ') {
1600Sstevel@tonic-gate #endif /* NROFF */
1610Sstevel@tonic-gate #endif /* EUC */
1620Sstevel@tonic-gate 			pad = 0;
1630Sstevel@tonic-gate 			do {
1640Sstevel@tonic-gate 				pad += width(j);
1650Sstevel@tonic-gate 				nc--;
1660Sstevel@tonic-gate #ifndef EUC
1670Sstevel@tonic-gate 			} while ((cbits(j = *i++)) == ' ');
1680Sstevel@tonic-gate #else
1690Sstevel@tonic-gate #ifndef NROFF
1700Sstevel@tonic-gate 			} while ((cbits(j = *i++)) == ' ');
1710Sstevel@tonic-gate #else
1720Sstevel@tonic-gate 			} while ((cbits(j = *i++) & ~MBMASK) == ' ');
1730Sstevel@tonic-gate #endif /* NROFF */
1740Sstevel@tonic-gate #endif /* EUC */
1750Sstevel@tonic-gate 			i--;
1760Sstevel@tonic-gate 			pad += adsp;
1770Sstevel@tonic-gate 			--nwd;
1780Sstevel@tonic-gate 			if (adrem) {
1790Sstevel@tonic-gate 				if (adrem < 0) {
1800Sstevel@tonic-gate 					pad -= resol;
1810Sstevel@tonic-gate 					adrem += resol;
1820Sstevel@tonic-gate 				} else if ((totout & 01) || adrem / resol >= nwd) {
1830Sstevel@tonic-gate 					pad += resol;
1840Sstevel@tonic-gate 					adrem -= resol;
1850Sstevel@tonic-gate 				}
1860Sstevel@tonic-gate 			}
1870Sstevel@tonic-gate 			pchar((tchar) WORDSP);
1880Sstevel@tonic-gate 			horiz(pad);
1890Sstevel@tonic-gate 		} else {
1900Sstevel@tonic-gate 			pchar(j);
1910Sstevel@tonic-gate 			nc--;
1920Sstevel@tonic-gate 		}
1930Sstevel@tonic-gate 	}
1940Sstevel@tonic-gate 	if (ic) {
1950Sstevel@tonic-gate 		if ((k = ll - un - lastl + ics) > 0)
1960Sstevel@tonic-gate 			horiz(k);
1970Sstevel@tonic-gate 		pchar(ic);
1980Sstevel@tonic-gate 	}
1990Sstevel@tonic-gate 	if (icf)
2000Sstevel@tonic-gate 		icf++;
2010Sstevel@tonic-gate 	else
2020Sstevel@tonic-gate 		ic = 0;
2030Sstevel@tonic-gate 	ne = nwd = 0;
2040Sstevel@tonic-gate 	un = in;
2050Sstevel@tonic-gate 	setnel();
2060Sstevel@tonic-gate 	newline(0);
2070Sstevel@tonic-gate 	if (dip != d) {
2080Sstevel@tonic-gate 		if (dip->dnl > dip->hnl)
2090Sstevel@tonic-gate 			dip->hnl = dip->dnl;
2100Sstevel@tonic-gate 	} else {
2110Sstevel@tonic-gate 		if (numtab[NL].val > dip->hnl)
2120Sstevel@tonic-gate 			dip->hnl = numtab[NL].val;
2130Sstevel@tonic-gate 	}
2140Sstevel@tonic-gate 	for (k = ls - 1; k > 0 && !trap; k--)
2150Sstevel@tonic-gate 		newline(0);
2160Sstevel@tonic-gate 	spread = 0;
217*217Smuffin 
218*217Smuffin 	return (0);
2190Sstevel@tonic-gate }
2200Sstevel@tonic-gate 
221*217Smuffin int
donum()2220Sstevel@tonic-gate donum()
2230Sstevel@tonic-gate {
224*217Smuffin 	int	i, nw;
225*217Smuffin 	extern int pchar();
2260Sstevel@tonic-gate 
2270Sstevel@tonic-gate 	nrbits = nmbits;
2280Sstevel@tonic-gate 	nw = width('1' | nrbits);
2290Sstevel@tonic-gate 	if (nn) {
2300Sstevel@tonic-gate 		nn--;
2310Sstevel@tonic-gate 		goto d1;
2320Sstevel@tonic-gate 	}
2330Sstevel@tonic-gate 	if (numtab[LN].val % ndf) {
2340Sstevel@tonic-gate 		numtab[LN].val++;
2350Sstevel@tonic-gate d1:
2360Sstevel@tonic-gate 		un += nw * (3 + nms + ni);
237*217Smuffin 		return (0);
2380Sstevel@tonic-gate 	}
2390Sstevel@tonic-gate 	i = 0;
2400Sstevel@tonic-gate 	if (numtab[LN].val < 100)
2410Sstevel@tonic-gate 		i++;
2420Sstevel@tonic-gate 	if (numtab[LN].val < 10)
2430Sstevel@tonic-gate 		i++;
2440Sstevel@tonic-gate 	horiz(nw * (ni + i));
2450Sstevel@tonic-gate 	nform = 0;
2460Sstevel@tonic-gate 	fnumb(numtab[LN].val, pchar);
2470Sstevel@tonic-gate 	un += nw * nms;
2480Sstevel@tonic-gate 	numtab[LN].val++;
249*217Smuffin 
250*217Smuffin 	return (0);
2510Sstevel@tonic-gate }
2520Sstevel@tonic-gate 
2530Sstevel@tonic-gate extern int logf;
254*217Smuffin 
255*217Smuffin int
text()2560Sstevel@tonic-gate text()
2570Sstevel@tonic-gate {
258*217Smuffin 	tchar i;
2590Sstevel@tonic-gate 	static int	spcnt;
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate 	nflush++;
2620Sstevel@tonic-gate 	numtab[HP].val = 0;
2630Sstevel@tonic-gate 	if ((dip == d) && (numtab[NL].val == -1)) {
2640Sstevel@tonic-gate 		newline(1);
265*217Smuffin 		return (0);
2660Sstevel@tonic-gate 	}
2670Sstevel@tonic-gate 	setnel();
2680Sstevel@tonic-gate 	if (ce || !fi) {
2690Sstevel@tonic-gate 		nofill();
270*217Smuffin 		return (0);
2710Sstevel@tonic-gate 	}
2720Sstevel@tonic-gate 	if (pendw)
2730Sstevel@tonic-gate 		goto t4;
2740Sstevel@tonic-gate 	if (pendt)
2750Sstevel@tonic-gate 		if (spcnt)
2760Sstevel@tonic-gate 			goto t2;
2770Sstevel@tonic-gate 		else
2780Sstevel@tonic-gate 			goto t3;
2790Sstevel@tonic-gate 	pendt++;
2800Sstevel@tonic-gate 	if (spcnt)
2810Sstevel@tonic-gate 		goto t2;
2820Sstevel@tonic-gate 	while ((cbits(i = GETCH())) == ' ') {
2830Sstevel@tonic-gate 		spcnt++;
2840Sstevel@tonic-gate 		numtab[HP].val += sps;
2850Sstevel@tonic-gate 		widthp = sps;
2860Sstevel@tonic-gate 	}
2870Sstevel@tonic-gate 	if (nlflg) {
2880Sstevel@tonic-gate t1:
2890Sstevel@tonic-gate 		nflush = pendt = ch = spcnt = 0;
2900Sstevel@tonic-gate 		callsp();
291*217Smuffin 		return (0);
2920Sstevel@tonic-gate 	}
2930Sstevel@tonic-gate 	ch = i;
2940Sstevel@tonic-gate 	if (spcnt) {
2950Sstevel@tonic-gate t2:
2960Sstevel@tonic-gate 		tbreak();
2970Sstevel@tonic-gate 		if (nc || wch)
2980Sstevel@tonic-gate 			goto rtn;
2990Sstevel@tonic-gate 		un += spcnt * sps;
3000Sstevel@tonic-gate 		spcnt = 0;
3010Sstevel@tonic-gate 		setnel();
3020Sstevel@tonic-gate 		if (trap)
3030Sstevel@tonic-gate 			goto rtn;
3040Sstevel@tonic-gate 		if (nlflg)
3050Sstevel@tonic-gate 			goto t1;
3060Sstevel@tonic-gate 	}
3070Sstevel@tonic-gate t3:
3080Sstevel@tonic-gate 	if (spread)
3090Sstevel@tonic-gate 		goto t5;
3100Sstevel@tonic-gate 	if (pendw || !wch)
3110Sstevel@tonic-gate t4:
3120Sstevel@tonic-gate 		if (getword(0))
3130Sstevel@tonic-gate 			goto t6;
3140Sstevel@tonic-gate 	if (!movword())
3150Sstevel@tonic-gate 		goto t3;
3160Sstevel@tonic-gate t5:
3170Sstevel@tonic-gate 	if (nlflg)
3180Sstevel@tonic-gate 		pendt = 0;
3190Sstevel@tonic-gate 	adsp = adrem = 0;
3200Sstevel@tonic-gate 	if (ad) {
3210Sstevel@tonic-gate 		if (nwd == 1)
3220Sstevel@tonic-gate 			adsp = nel;
3230Sstevel@tonic-gate 		else
3240Sstevel@tonic-gate 			adsp = nel / (nwd - 1);
3250Sstevel@tonic-gate 		adsp = (adsp / HOR) * HOR;
3260Sstevel@tonic-gate 		adrem = nel - adsp*(nwd-1);
3270Sstevel@tonic-gate 	}
3280Sstevel@tonic-gate 	brflg = 1;
3290Sstevel@tonic-gate 	tbreak();
3300Sstevel@tonic-gate 	spread = 0;
3310Sstevel@tonic-gate 	if (!trap)
3320Sstevel@tonic-gate 		goto t3;
3330Sstevel@tonic-gate 	if (!nlflg)
3340Sstevel@tonic-gate 		goto rtn;
3350Sstevel@tonic-gate t6:
3360Sstevel@tonic-gate 	pendt = 0;
3370Sstevel@tonic-gate 	ckul();
3380Sstevel@tonic-gate rtn:
3390Sstevel@tonic-gate 	nflush = 0;
340*217Smuffin 
341*217Smuffin 	return (0);
3420Sstevel@tonic-gate }
3430Sstevel@tonic-gate 
3440Sstevel@tonic-gate 
345*217Smuffin int
nofill()3460Sstevel@tonic-gate nofill()
3470Sstevel@tonic-gate {
348*217Smuffin 	int	j;
349*217Smuffin 	tchar i;
3500Sstevel@tonic-gate 
3510Sstevel@tonic-gate 	if (!pendnf) {
3520Sstevel@tonic-gate 		over = 0;
3530Sstevel@tonic-gate 		tbreak();
3540Sstevel@tonic-gate 		if (trap)
3550Sstevel@tonic-gate 			goto rtn;
3560Sstevel@tonic-gate 		if (nlflg) {
3570Sstevel@tonic-gate 			ch = nflush = 0;
3580Sstevel@tonic-gate 			callsp();
359*217Smuffin 			return (0);
3600Sstevel@tonic-gate 		}
3610Sstevel@tonic-gate 		adsp = adrem = 0;
3620Sstevel@tonic-gate 		nwd = 10000;
3630Sstevel@tonic-gate 	}
3640Sstevel@tonic-gate 	while ((j = (cbits(i = GETCH()))) != '\n') {
3650Sstevel@tonic-gate 		if (j == ohc)
3660Sstevel@tonic-gate 			continue;
3670Sstevel@tonic-gate 		if (j == CONT) {
3680Sstevel@tonic-gate 			pendnf++;
3690Sstevel@tonic-gate 			nflush = 0;
3700Sstevel@tonic-gate 			flushi();
3710Sstevel@tonic-gate 			ckul();
372*217Smuffin 			return (0);
3730Sstevel@tonic-gate 		}
3740Sstevel@tonic-gate 		j = width(i);
3750Sstevel@tonic-gate 		widthp = j;
3760Sstevel@tonic-gate 		numtab[HP].val += j;
3770Sstevel@tonic-gate 		storeline(i, j);
3780Sstevel@tonic-gate 	}
3790Sstevel@tonic-gate 	if (ce) {
3800Sstevel@tonic-gate 		ce--;
3810Sstevel@tonic-gate 		if ((i = quant(nel / 2, HOR)) > 0)
3820Sstevel@tonic-gate 			un += i;
3830Sstevel@tonic-gate 	}
3840Sstevel@tonic-gate 	if (!nc)
3850Sstevel@tonic-gate 		storeline((tchar)FILLER, 0);
3860Sstevel@tonic-gate 	brflg = 2;
3870Sstevel@tonic-gate 	tbreak();
3880Sstevel@tonic-gate 	ckul();
3890Sstevel@tonic-gate rtn:
3900Sstevel@tonic-gate 	pendnf = nflush = 0;
391*217Smuffin 
392*217Smuffin 	return (0);
3930Sstevel@tonic-gate }
3940Sstevel@tonic-gate 
3950Sstevel@tonic-gate 
396*217Smuffin int
callsp()3970Sstevel@tonic-gate callsp()
3980Sstevel@tonic-gate {
399*217Smuffin 	int	i;
4000Sstevel@tonic-gate 
4010Sstevel@tonic-gate 	if (flss)
4020Sstevel@tonic-gate 		i = flss;
4030Sstevel@tonic-gate 	else
4040Sstevel@tonic-gate 		i = lss;
4050Sstevel@tonic-gate 	flss = 0;
4060Sstevel@tonic-gate 	casesp(i);
407*217Smuffin 
408*217Smuffin 	return (0);
4090Sstevel@tonic-gate }
4100Sstevel@tonic-gate 
4110Sstevel@tonic-gate 
412*217Smuffin int
ckul()4130Sstevel@tonic-gate ckul()
4140Sstevel@tonic-gate {
4150Sstevel@tonic-gate 	if (ul && (--ul == 0)) {
4160Sstevel@tonic-gate 		cu = 0;
4170Sstevel@tonic-gate 		font = sfont;
4180Sstevel@tonic-gate 		mchbits();
4190Sstevel@tonic-gate 	}
4200Sstevel@tonic-gate 	if (it && (--it == 0) && itmac)
4210Sstevel@tonic-gate 		control(itmac, 0);
422*217Smuffin 
423*217Smuffin 	return (0);
4240Sstevel@tonic-gate }
4250Sstevel@tonic-gate 
4260Sstevel@tonic-gate 
427*217Smuffin int
storeline(c,w)4280Sstevel@tonic-gate storeline(c, w)
429*217Smuffin tchar c;
4300Sstevel@tonic-gate {
4310Sstevel@tonic-gate 	if (linep >= line + lnsize - 1) {
4320Sstevel@tonic-gate 		if (!over) {
4330Sstevel@tonic-gate 			flusho();
4340Sstevel@tonic-gate 			errprint(gettext("Line overflow."));
4350Sstevel@tonic-gate 			over++;
4360Sstevel@tonic-gate 			c = LEFTHAND;
4370Sstevel@tonic-gate 			w = -1;
4380Sstevel@tonic-gate 			goto s1;
4390Sstevel@tonic-gate 		}
440*217Smuffin 		return (0);
4410Sstevel@tonic-gate 	}
4420Sstevel@tonic-gate s1:
4430Sstevel@tonic-gate 	if (w == -1)
4440Sstevel@tonic-gate 		w = width(c);
4450Sstevel@tonic-gate 	ne += w;
4460Sstevel@tonic-gate 	nel -= w;
4470Sstevel@tonic-gate 	*linep++ = c;
4480Sstevel@tonic-gate 	nc++;
449*217Smuffin 
450*217Smuffin 	return (0);
4510Sstevel@tonic-gate }
4520Sstevel@tonic-gate 
4530Sstevel@tonic-gate 
454*217Smuffin int
newline(a)4550Sstevel@tonic-gate newline(a)
4560Sstevel@tonic-gate int	a;
4570Sstevel@tonic-gate {
458*217Smuffin 	int	i, j, nlss;
4590Sstevel@tonic-gate 	int	opn;
4600Sstevel@tonic-gate 
4610Sstevel@tonic-gate 	if (a)
4620Sstevel@tonic-gate 		goto nl1;
4630Sstevel@tonic-gate 	if (dip != d) {
4640Sstevel@tonic-gate 		j = lss;
4650Sstevel@tonic-gate 		pchar1((tchar)FLSS);
4660Sstevel@tonic-gate 		if (flss)
4670Sstevel@tonic-gate 			lss = flss;
4680Sstevel@tonic-gate 		i = lss + dip->blss;
4690Sstevel@tonic-gate 		dip->dnl += i;
4700Sstevel@tonic-gate 		pchar1((tchar)i);
4710Sstevel@tonic-gate 		pchar1((tchar)'\n');
4720Sstevel@tonic-gate 		lss = j;
4730Sstevel@tonic-gate 		dip->blss = flss = 0;
4740Sstevel@tonic-gate 		if (dip->alss) {
4750Sstevel@tonic-gate 			pchar1((tchar)FLSS);
4760Sstevel@tonic-gate 			pchar1((tchar)dip->alss);
4770Sstevel@tonic-gate 			pchar1((tchar)'\n');
4780Sstevel@tonic-gate 			dip->dnl += dip->alss;
4790Sstevel@tonic-gate 			dip->alss = 0;
4800Sstevel@tonic-gate 		}
4810Sstevel@tonic-gate 		if (dip->ditrap && !dip->ditf && dip->dnl >= dip->ditrap && dip->dimac)
4820Sstevel@tonic-gate 			if (control(dip->dimac, 0)) {
4830Sstevel@tonic-gate 				trap++;
4840Sstevel@tonic-gate 				dip->ditf++;
4850Sstevel@tonic-gate 			}
486*217Smuffin 		return (0);
4870Sstevel@tonic-gate 	}
4880Sstevel@tonic-gate 	j = lss;
4890Sstevel@tonic-gate 	if (flss)
4900Sstevel@tonic-gate 		lss = flss;
4910Sstevel@tonic-gate 	nlss = dip->alss + dip->blss + lss;
4920Sstevel@tonic-gate 	numtab[NL].val += nlss;
4930Sstevel@tonic-gate #ifndef NROFF
4940Sstevel@tonic-gate 	if (ascii) {
4950Sstevel@tonic-gate 		dip->alss = dip->blss = 0;
4960Sstevel@tonic-gate 	}
4970Sstevel@tonic-gate #endif
4980Sstevel@tonic-gate 	pchar1((tchar)'\n');
4990Sstevel@tonic-gate 	flss = 0;
5000Sstevel@tonic-gate 	lss = j;
5010Sstevel@tonic-gate 	if (numtab[NL].val < pl)
5020Sstevel@tonic-gate 		goto nl2;
5030Sstevel@tonic-gate nl1:
5040Sstevel@tonic-gate 	ejf = dip->hnl = numtab[NL].val = 0;
5050Sstevel@tonic-gate 	ejl = frame;
5060Sstevel@tonic-gate 	if (donef) {
5070Sstevel@tonic-gate 		if ((!nc && !wch) || ndone)
5080Sstevel@tonic-gate 			done1(0);
5090Sstevel@tonic-gate 		ndone++;
5100Sstevel@tonic-gate 		donef = 0;
5110Sstevel@tonic-gate 		if (frame == stk)
5120Sstevel@tonic-gate 			nflush++;
5130Sstevel@tonic-gate 	}
5140Sstevel@tonic-gate 	opn = numtab[PN].val;
5150Sstevel@tonic-gate 	numtab[PN].val++;
5160Sstevel@tonic-gate 	if (npnflg) {
5170Sstevel@tonic-gate 		numtab[PN].val = npn;
5180Sstevel@tonic-gate 		npn = npnflg = 0;
5190Sstevel@tonic-gate 	}
5200Sstevel@tonic-gate nlpn:
5210Sstevel@tonic-gate 	if (numtab[PN].val == pfrom) {
5220Sstevel@tonic-gate 		print++;
5230Sstevel@tonic-gate 		pfrom = -1;
5240Sstevel@tonic-gate 	} else if (opn == pto) {
5250Sstevel@tonic-gate 		print = 0;
5260Sstevel@tonic-gate 		opn = -1;
5270Sstevel@tonic-gate 		chkpn();
5280Sstevel@tonic-gate 		goto nlpn;
5290Sstevel@tonic-gate 	}
5300Sstevel@tonic-gate 	if (print)
5310Sstevel@tonic-gate 		newpage(numtab[PN].val);	/* supposedly in a clean state so can pause */
5320Sstevel@tonic-gate 	if (stop && print) {
5330Sstevel@tonic-gate 		dpn++;
5340Sstevel@tonic-gate 		if (dpn >= stop) {
5350Sstevel@tonic-gate 			dpn = 0;
5360Sstevel@tonic-gate 			dostop();
5370Sstevel@tonic-gate 		}
5380Sstevel@tonic-gate 	}
5390Sstevel@tonic-gate nl2:
5400Sstevel@tonic-gate 	trap = 0;
5410Sstevel@tonic-gate 	if (numtab[NL].val == 0) {
5420Sstevel@tonic-gate 		if ((j = findn(0)) != NTRAP)
5430Sstevel@tonic-gate 			trap = control(mlist[j], 0);
5440Sstevel@tonic-gate 	} else if ((i = findt(numtab[NL].val - nlss)) <= nlss) {
5450Sstevel@tonic-gate 		if ((j = findn1(numtab[NL].val - nlss + i)) == NTRAP) {
5460Sstevel@tonic-gate 			flusho();
5470Sstevel@tonic-gate 			errprint(gettext("Trap botch."));
5480Sstevel@tonic-gate 			done2(-5);
5490Sstevel@tonic-gate 		}
5500Sstevel@tonic-gate 		trap = control(mlist[j], 0);
5510Sstevel@tonic-gate 	}
552*217Smuffin 
553*217Smuffin 	return (0);
5540Sstevel@tonic-gate }
5550Sstevel@tonic-gate 
5560Sstevel@tonic-gate 
557*217Smuffin int
findn1(a)5580Sstevel@tonic-gate findn1(a)
5590Sstevel@tonic-gate int	a;
5600Sstevel@tonic-gate {
561*217Smuffin 	int	i, j;
5620Sstevel@tonic-gate 
5630Sstevel@tonic-gate 	for (i = 0; i < NTRAP; i++) {
5640Sstevel@tonic-gate 		if (mlist[i]) {
5650Sstevel@tonic-gate 			if ((j = nlist[i]) < 0)
5660Sstevel@tonic-gate 				j += pl;
5670Sstevel@tonic-gate 			if (j == a)
5680Sstevel@tonic-gate 				break;
5690Sstevel@tonic-gate 		}
5700Sstevel@tonic-gate 	}
5710Sstevel@tonic-gate 	return(i);
5720Sstevel@tonic-gate }
5730Sstevel@tonic-gate 
574*217Smuffin int
chkpn()5750Sstevel@tonic-gate chkpn()
5760Sstevel@tonic-gate {
5770Sstevel@tonic-gate 	pto = *(pnp++);
5780Sstevel@tonic-gate 	pfrom = pto>=0 ? pto : -pto;
5790Sstevel@tonic-gate 	if (pto == -32767) {
5800Sstevel@tonic-gate 		flusho();
5810Sstevel@tonic-gate 		done1(0);
5820Sstevel@tonic-gate 	}
5830Sstevel@tonic-gate 	if (pto < 0) {
5840Sstevel@tonic-gate 		pto = -pto;
5850Sstevel@tonic-gate 		print++;
5860Sstevel@tonic-gate 		pfrom = 0;
5870Sstevel@tonic-gate 	}
588*217Smuffin 
589*217Smuffin 	return (0);
5900Sstevel@tonic-gate }
5910Sstevel@tonic-gate 
5920Sstevel@tonic-gate 
593*217Smuffin int
findt(a)5940Sstevel@tonic-gate findt(a)
5950Sstevel@tonic-gate int	a;
5960Sstevel@tonic-gate {
597*217Smuffin 	int	i, j, k;
5980Sstevel@tonic-gate 
5990Sstevel@tonic-gate 	k = 32767;
6000Sstevel@tonic-gate 	if (dip != d) {
6010Sstevel@tonic-gate 		if (dip->dimac && (i = dip->ditrap - a) > 0)
6020Sstevel@tonic-gate 			k = i;
6030Sstevel@tonic-gate 		return(k);
6040Sstevel@tonic-gate 	}
6050Sstevel@tonic-gate 	for (i = 0; i < NTRAP; i++) {
6060Sstevel@tonic-gate 		if (mlist[i]) {
6070Sstevel@tonic-gate 			if ((j = nlist[i]) < 0)
6080Sstevel@tonic-gate 				j += pl;
6090Sstevel@tonic-gate 			if ((j -= a) <= 0)
6100Sstevel@tonic-gate 				continue;
6110Sstevel@tonic-gate 			if (j < k)
6120Sstevel@tonic-gate 				k = j;
6130Sstevel@tonic-gate 		}
6140Sstevel@tonic-gate 	}
6150Sstevel@tonic-gate 	i = pl - a;
6160Sstevel@tonic-gate 	if (k > i)
6170Sstevel@tonic-gate 		k = i;
6180Sstevel@tonic-gate 	return(k);
6190Sstevel@tonic-gate }
6200Sstevel@tonic-gate 
6210Sstevel@tonic-gate 
622*217Smuffin int
findt1()6230Sstevel@tonic-gate findt1()
6240Sstevel@tonic-gate {
625*217Smuffin 	int	i;
6260Sstevel@tonic-gate 
6270Sstevel@tonic-gate 	if (dip != d)
6280Sstevel@tonic-gate 		i = dip->dnl;
6290Sstevel@tonic-gate 	else
6300Sstevel@tonic-gate 		i = numtab[NL].val;
6310Sstevel@tonic-gate 	return(findt(i));
6320Sstevel@tonic-gate }
6330Sstevel@tonic-gate 
6340Sstevel@tonic-gate 
635*217Smuffin int
eject(a)6360Sstevel@tonic-gate eject(a)
6370Sstevel@tonic-gate struct s *a;
6380Sstevel@tonic-gate {
639*217Smuffin 	int	savlss;
6400Sstevel@tonic-gate 
6410Sstevel@tonic-gate 	if (dip != d)
642*217Smuffin 		return (0);
6430Sstevel@tonic-gate 	ejf++;
6440Sstevel@tonic-gate 	if (a)
6450Sstevel@tonic-gate 		ejl = a;
6460Sstevel@tonic-gate 	else
6470Sstevel@tonic-gate 		ejl = frame;
6480Sstevel@tonic-gate 	if (trap)
649*217Smuffin 		return (0);
6500Sstevel@tonic-gate e1:
6510Sstevel@tonic-gate 	savlss = lss;
6520Sstevel@tonic-gate 	lss = findt(numtab[NL].val);
6530Sstevel@tonic-gate 	newline(0);
6540Sstevel@tonic-gate 	lss = savlss;
6550Sstevel@tonic-gate 	if (numtab[NL].val && !trap)
6560Sstevel@tonic-gate 		goto e1;
657*217Smuffin 
658*217Smuffin 	return (0);
6590Sstevel@tonic-gate }
6600Sstevel@tonic-gate 
6610Sstevel@tonic-gate 
662*217Smuffin int
movword()6630Sstevel@tonic-gate movword()
6640Sstevel@tonic-gate {
665*217Smuffin 	int	w;
666*217Smuffin 	tchar i, *wp;
6670Sstevel@tonic-gate 	int	savwch, hys;
6680Sstevel@tonic-gate 
6690Sstevel@tonic-gate 	over = 0;
6700Sstevel@tonic-gate 	wp = wordp;
6710Sstevel@tonic-gate 	if (!nwd) {
6720Sstevel@tonic-gate #ifndef EUC
6730Sstevel@tonic-gate 		while (cbits(i = *wp++) == ' ') {
6740Sstevel@tonic-gate #else
6750Sstevel@tonic-gate #ifndef NROFF
6760Sstevel@tonic-gate 		while (cbits(i = *wp++) == ' ') {
6770Sstevel@tonic-gate #else
6780Sstevel@tonic-gate 		while ((cbits(i = *wp++) & ~MBMASK) == ' ') {
6790Sstevel@tonic-gate #endif /* NROFF */
6800Sstevel@tonic-gate #endif /* EUC */
6810Sstevel@tonic-gate 			wch--;
6820Sstevel@tonic-gate 			wne -= sps;
6830Sstevel@tonic-gate 		}
6840Sstevel@tonic-gate 		wp--;
6850Sstevel@tonic-gate 	}
6860Sstevel@tonic-gate 	if (wne > nel && !hyoff && hyf && (!nwd || nel > 3 * sps) &&
6870Sstevel@tonic-gate 	   (!(hyf & 02) || (findt1() > lss)))
6880Sstevel@tonic-gate 		hyphen(wp);
6890Sstevel@tonic-gate 	savwch = wch;
6900Sstevel@tonic-gate 	hyp = hyptr;
6910Sstevel@tonic-gate 	nhyp = 0;
6920Sstevel@tonic-gate 	while (*hyp && *hyp <= wp)
6930Sstevel@tonic-gate 		hyp++;
6940Sstevel@tonic-gate 	while (wch) {
6950Sstevel@tonic-gate 		if (hyoff != 1 && *hyp == wp) {
6960Sstevel@tonic-gate 			hyp++;
6970Sstevel@tonic-gate 			if (!wdstart || (wp > wdstart + 1 && wp < wdend &&
6980Sstevel@tonic-gate 			   (!(hyf & 04) || wp < wdend - 1) &&		/* 04 => last 2 */
6990Sstevel@tonic-gate 			   (!(hyf & 010) || wp > wdstart + 2))) {	/* 010 => 1st 2 */
7000Sstevel@tonic-gate 				nhyp++;
7010Sstevel@tonic-gate 				storeline((tchar)IMP, 0);
7020Sstevel@tonic-gate 			}
7030Sstevel@tonic-gate 		}
7040Sstevel@tonic-gate 		i = *wp++;
7050Sstevel@tonic-gate 		w = width(i);
7060Sstevel@tonic-gate 		wne -= w;
7070Sstevel@tonic-gate 		wch--;
7080Sstevel@tonic-gate 		storeline(i, w);
7090Sstevel@tonic-gate 	}
7100Sstevel@tonic-gate 	if (nel >= 0) {
7110Sstevel@tonic-gate 		nwd++;
7120Sstevel@tonic-gate 		return(0);	/* line didn't fill up */
7130Sstevel@tonic-gate 	}
7140Sstevel@tonic-gate #ifndef NROFF
7150Sstevel@tonic-gate 	xbits((tchar)HYPHEN, 1);
7160Sstevel@tonic-gate #endif
7170Sstevel@tonic-gate 	hys = width((tchar)HYPHEN);
7180Sstevel@tonic-gate m1:
7190Sstevel@tonic-gate 	if (!nhyp) {
7200Sstevel@tonic-gate 		if (!nwd)
7210Sstevel@tonic-gate 			goto m3;
7220Sstevel@tonic-gate 		if (wch == savwch)
7230Sstevel@tonic-gate 			goto m4;
7240Sstevel@tonic-gate 	}
7250Sstevel@tonic-gate 	if (*--linep != IMP)
7260Sstevel@tonic-gate 		goto m5;
7270Sstevel@tonic-gate 	if (!(--nhyp))
7280Sstevel@tonic-gate 		if (!nwd)
7290Sstevel@tonic-gate 			goto m2;
7300Sstevel@tonic-gate 	if (nel < hys) {
7310Sstevel@tonic-gate 		nc--;
7320Sstevel@tonic-gate 		goto m1;
7330Sstevel@tonic-gate 	}
7340Sstevel@tonic-gate m2:
7350Sstevel@tonic-gate 	if ((i = cbits(*(linep - 1))) != '-' && i != EMDASH) {
7360Sstevel@tonic-gate 		*linep = (*(linep - 1) & SFMASK) | HYPHEN;
7370Sstevel@tonic-gate 		w = width(*linep);
7380Sstevel@tonic-gate 		nel -= w;
7390Sstevel@tonic-gate 		ne += w;
7400Sstevel@tonic-gate 		linep++;
7410Sstevel@tonic-gate 	}
7420Sstevel@tonic-gate m3:
7430Sstevel@tonic-gate 	nwd++;
7440Sstevel@tonic-gate m4:
7450Sstevel@tonic-gate 	wordp = wp;
7460Sstevel@tonic-gate 	return(1);	/* line filled up */
7470Sstevel@tonic-gate m5:
7480Sstevel@tonic-gate 	nc--;
7490Sstevel@tonic-gate 	w = width(*linep);
7500Sstevel@tonic-gate 	ne -= w;
7510Sstevel@tonic-gate 	nel += w;
7520Sstevel@tonic-gate 	wne += w;
7530Sstevel@tonic-gate 	wch++;
7540Sstevel@tonic-gate 	wp--;
7550Sstevel@tonic-gate 	goto m1;
7560Sstevel@tonic-gate }
7570Sstevel@tonic-gate 
7580Sstevel@tonic-gate 
759*217Smuffin int
7600Sstevel@tonic-gate horiz(i)
7610Sstevel@tonic-gate int	i;
7620Sstevel@tonic-gate {
7630Sstevel@tonic-gate 	vflag = 0;
7640Sstevel@tonic-gate 	if (i)
7650Sstevel@tonic-gate 		pchar(makem(i));
766*217Smuffin 
767*217Smuffin 	return (0);
7680Sstevel@tonic-gate }
7690Sstevel@tonic-gate 
7700Sstevel@tonic-gate 
771*217Smuffin int
7720Sstevel@tonic-gate setnel()
7730Sstevel@tonic-gate {
7740Sstevel@tonic-gate 	if (!nc) {
7750Sstevel@tonic-gate 		linep = line;
7760Sstevel@tonic-gate 		if (un1 >= 0) {
7770Sstevel@tonic-gate 			un = un1;
7780Sstevel@tonic-gate 			un1 = -1;
7790Sstevel@tonic-gate 		}
7800Sstevel@tonic-gate 		nel = ll - un;
7810Sstevel@tonic-gate 		ne = adsp = adrem = 0;
7820Sstevel@tonic-gate 	}
783*217Smuffin 
784*217Smuffin 	return (0);
7850Sstevel@tonic-gate }
7860Sstevel@tonic-gate 
787*217Smuffin int
7880Sstevel@tonic-gate getword(x)
7890Sstevel@tonic-gate int	x;
7900Sstevel@tonic-gate {
791*217Smuffin 	int j, k;
792*217Smuffin 	tchar i, *wp;
7930Sstevel@tonic-gate 	int noword;
7940Sstevel@tonic-gate #ifdef EUC
7950Sstevel@tonic-gate #ifdef NROFF
7960Sstevel@tonic-gate 	wchar_t *wddelim;
7970Sstevel@tonic-gate 	char mbbuf3[MB_LEN_MAX + 1];
7980Sstevel@tonic-gate 	char *mbbuf3p;
7990Sstevel@tonic-gate 	int wbf, n;
8000Sstevel@tonic-gate 	tchar m;
8010Sstevel@tonic-gate #endif /* NROFF */
8020Sstevel@tonic-gate #endif /* EUC */
8030Sstevel@tonic-gate 
8040Sstevel@tonic-gate 	noword = 0;
8050Sstevel@tonic-gate 	if (x)
8060Sstevel@tonic-gate 		if (pendw) {
8070Sstevel@tonic-gate 			*pendw = 0;
8080Sstevel@tonic-gate 			goto rtn;
8090Sstevel@tonic-gate 		}
8100Sstevel@tonic-gate 	if (wordp = pendw)
8110Sstevel@tonic-gate 		goto g1;
8120Sstevel@tonic-gate 	hyp = hyptr;
8130Sstevel@tonic-gate 	wordp = word;
8140Sstevel@tonic-gate 	over = wne = wch = 0;
8150Sstevel@tonic-gate 	hyoff = 0;
8160Sstevel@tonic-gate #ifdef EUC
8170Sstevel@tonic-gate #ifdef NROFF
8180Sstevel@tonic-gate 	mtbufp = mtbuf;
8190Sstevel@tonic-gate 	if (pendmb) {
8200Sstevel@tonic-gate 		while(*mtbufp) {
8210Sstevel@tonic-gate 			switch(*mtbufp & MBMASK) {
8220Sstevel@tonic-gate 			case LASTOFMB:
8230Sstevel@tonic-gate 			case BYTE_CHR:
8240Sstevel@tonic-gate 				storeword(*mtbufp++, -1);
8250Sstevel@tonic-gate 				break;
8260Sstevel@tonic-gate 
8270Sstevel@tonic-gate 			default:
8280Sstevel@tonic-gate 				storeword(*mtbufp++, 0);
8290Sstevel@tonic-gate 			}
8300Sstevel@tonic-gate 		}
8310Sstevel@tonic-gate 		mtbufp = mtbuf;
8320Sstevel@tonic-gate 		pendmb = 0;
8330Sstevel@tonic-gate 		goto g1;
8340Sstevel@tonic-gate 	}
8350Sstevel@tonic-gate #endif /* NROFF */
8360Sstevel@tonic-gate #endif /* EUC */
8370Sstevel@tonic-gate 	while (1) {	/* picks up 1st char of word */
8380Sstevel@tonic-gate 		j = cbits(i = GETCH());
8390Sstevel@tonic-gate #ifdef EUC
8400Sstevel@tonic-gate #ifdef NROFF
8410Sstevel@tonic-gate 		if (multi_locale)
8420Sstevel@tonic-gate 			if (collectmb(i))
8430Sstevel@tonic-gate 				continue;
8440Sstevel@tonic-gate #endif /* NROFF */
8450Sstevel@tonic-gate #endif /* EUC */
8460Sstevel@tonic-gate 		if (j == '\n') {
8470Sstevel@tonic-gate 			wne = wch = 0;
8480Sstevel@tonic-gate 			noword = 1;
8490Sstevel@tonic-gate 			goto rtn;
8500Sstevel@tonic-gate 		}
8510Sstevel@tonic-gate 		if (j == ohc) {
8520Sstevel@tonic-gate 			hyoff = 1;	/* 1 => don't hyphenate */
8530Sstevel@tonic-gate 			continue;
8540Sstevel@tonic-gate 		}
8550Sstevel@tonic-gate 		if (j == ' ') {
8560Sstevel@tonic-gate 			numtab[HP].val += sps;
8570Sstevel@tonic-gate 			widthp = sps;
8580Sstevel@tonic-gate 			storeword(i, sps);
8590Sstevel@tonic-gate 			continue;
8600Sstevel@tonic-gate 		}
8610Sstevel@tonic-gate 		break;
8620Sstevel@tonic-gate 	}
8630Sstevel@tonic-gate #ifdef EUC
8640Sstevel@tonic-gate #ifdef NROFF
8650Sstevel@tonic-gate 	if (!multi_locale)
8660Sstevel@tonic-gate 		goto a0;
8670Sstevel@tonic-gate 	if (wddlm && iswprint(wceoll) && iswprint(cwc) &&
8680Sstevel@tonic-gate 	    (!iswascii(wceoll) || !iswascii(cwc)) &&
8690Sstevel@tonic-gate 	    !iswspace(wceoll) && !iswspace(cwc)) {
8700Sstevel@tonic-gate 		wddelim = (*wddlm)(wceoll, cwc, 1);
8710Sstevel@tonic-gate 		wceoll = 0;
8720Sstevel@tonic-gate 		if (*wddelim != ' ') {
8730Sstevel@tonic-gate 			if (!*wddelim) {
8740Sstevel@tonic-gate 				storeword(((*wdbdg)(wceoll, cwc, 1) < 3) ?
8750Sstevel@tonic-gate 					  ZWDELIM(1) : ZWDELIM(2), 0);
8760Sstevel@tonic-gate 			} else {
8770Sstevel@tonic-gate 				while (*wddelim) {
8780Sstevel@tonic-gate 					if ((n = wctomb(mbbuf3, *wddelim++))
8790Sstevel@tonic-gate 					    > 0) {
8800Sstevel@tonic-gate 						mbbuf3[n] = 0;
8810Sstevel@tonic-gate 						n--;
8820Sstevel@tonic-gate 						mbbuf3p = mbbuf3 + n;
8830Sstevel@tonic-gate 						while(n) {
8840Sstevel@tonic-gate 							m = *(mbbuf3p-- - n--) &
8850Sstevel@tonic-gate 							    0xff | MIDDLEOFMB |
8860Sstevel@tonic-gate 							    ZBIT;
8870Sstevel@tonic-gate 							storeword(m, 0);
8880Sstevel@tonic-gate 						}
8890Sstevel@tonic-gate 						m = *mbbuf3p & 0xff | LASTOFMB;
8900Sstevel@tonic-gate 						storeword(m, -1);
8910Sstevel@tonic-gate 					} else {
8920Sstevel@tonic-gate 						storeword(' ' | chbits, sps);
8930Sstevel@tonic-gate 						break;
8940Sstevel@tonic-gate 					}
8950Sstevel@tonic-gate 				}
8960Sstevel@tonic-gate 			}
8970Sstevel@tonic-gate 			spflg = 0;
8980Sstevel@tonic-gate 			goto g0;
8990Sstevel@tonic-gate 		}
9000Sstevel@tonic-gate 	}
9010Sstevel@tonic-gate a0:
9020Sstevel@tonic-gate #endif /* NROFF */
9030Sstevel@tonic-gate #endif /* EUC */
9040Sstevel@tonic-gate 	storeword(' ' | chbits, sps);
9050Sstevel@tonic-gate 	if (spflg) {
9060Sstevel@tonic-gate 		storeword(' ' | chbits, sps);
9070Sstevel@tonic-gate 		spflg = 0;
9080Sstevel@tonic-gate 	}
9090Sstevel@tonic-gate g0:
9100Sstevel@tonic-gate 	if (j == CONT) {
9110Sstevel@tonic-gate 		pendw = wordp;
9120Sstevel@tonic-gate 		nflush = 0;
9130Sstevel@tonic-gate 		flushi();
9140Sstevel@tonic-gate 		return(1);
9150Sstevel@tonic-gate 	}
9160Sstevel@tonic-gate 	if (hyoff != 1) {
9170Sstevel@tonic-gate 		if (j == ohc) {
9180Sstevel@tonic-gate 			hyoff = 2;
9190Sstevel@tonic-gate 			*hyp++ = wordp;
9200Sstevel@tonic-gate 			if (hyp > (hyptr + NHYP - 1))
9210Sstevel@tonic-gate 				hyp = hyptr + NHYP - 1;
9220Sstevel@tonic-gate 			goto g1;
9230Sstevel@tonic-gate 		}
9240Sstevel@tonic-gate 		if (j == '-' || j == EMDASH)
9250Sstevel@tonic-gate 			if (wordp > word + 1) {
9260Sstevel@tonic-gate 				hyoff = 2;
9270Sstevel@tonic-gate 				*hyp++ = wordp + 1;
9280Sstevel@tonic-gate 				if (hyp > (hyptr + NHYP - 1))
9290Sstevel@tonic-gate 					hyp = hyptr + NHYP - 1;
9300Sstevel@tonic-gate 			}
9310Sstevel@tonic-gate 	}
9320Sstevel@tonic-gate 	j = width(i);
9330Sstevel@tonic-gate 	numtab[HP].val += j;
9340Sstevel@tonic-gate #ifndef EUC
9350Sstevel@tonic-gate 	storeword(i, j);
9360Sstevel@tonic-gate #else
9370Sstevel@tonic-gate #ifndef NROFF
9380Sstevel@tonic-gate 	storeword(i, j);
9390Sstevel@tonic-gate #else
9400Sstevel@tonic-gate 	if (multi_locale) {
9410Sstevel@tonic-gate 		mtbufp = mtbuf;
9420Sstevel@tonic-gate 		while(*mtbufp) {
9430Sstevel@tonic-gate 			switch(*mtbufp & MBMASK) {
9440Sstevel@tonic-gate 			case LASTOFMB:
9450Sstevel@tonic-gate 			case BYTE_CHR:
9460Sstevel@tonic-gate 				storeword(*mtbufp++, j);
9470Sstevel@tonic-gate 				break;
9480Sstevel@tonic-gate 
9490Sstevel@tonic-gate 			default:
9500Sstevel@tonic-gate 				storeword(*mtbufp++, 0);
9510Sstevel@tonic-gate 			}
9520Sstevel@tonic-gate 		}
9530Sstevel@tonic-gate 		mtbufp = mtbuf;
9540Sstevel@tonic-gate 	} else {
9550Sstevel@tonic-gate 		storeword(i, j);
9560Sstevel@tonic-gate 	}
9570Sstevel@tonic-gate #endif /* NROFF */
9580Sstevel@tonic-gate #endif /* EUC */
9590Sstevel@tonic-gate g1:
9600Sstevel@tonic-gate 	j = cbits(i = GETCH());
9610Sstevel@tonic-gate #ifdef EUC
9620Sstevel@tonic-gate #ifdef NROFF
9630Sstevel@tonic-gate 	if (multi_locale)
9640Sstevel@tonic-gate 		if (collectmb(i))
9650Sstevel@tonic-gate 			goto g1;
9660Sstevel@tonic-gate #endif /* NROFF */
9670Sstevel@tonic-gate #endif /* EUC */
9680Sstevel@tonic-gate 	if (j != ' ') {
9690Sstevel@tonic-gate 		static char *sentchar = ".?!:";	/* sentence terminators */
9700Sstevel@tonic-gate 		if (j != '\n')
9710Sstevel@tonic-gate #ifdef EUC
9720Sstevel@tonic-gate #ifdef NROFF
9730Sstevel@tonic-gate 			if (!multi_locale)
9740Sstevel@tonic-gate #endif /* NROFF */
9750Sstevel@tonic-gate #endif /* EUC */
9760Sstevel@tonic-gate 			goto g0;
9770Sstevel@tonic-gate #ifdef EUC
9780Sstevel@tonic-gate #ifdef NROFF
9790Sstevel@tonic-gate 			else {
9800Sstevel@tonic-gate 				if (!wdbdg || (iswascii(cwc) && iswascii(owc)))
9810Sstevel@tonic-gate 					goto g0;
9820Sstevel@tonic-gate 				if ((wbf = (*wdbdg)(owc, cwc, 1)) < 5) {
9830Sstevel@tonic-gate 					pendmb++;
9840Sstevel@tonic-gate 					storeword((wbf < 3) ? ZWDELIM(1) :
9850Sstevel@tonic-gate 						  ZWDELIM(2), 0);
9860Sstevel@tonic-gate 					*wordp = 0;
9870Sstevel@tonic-gate 					goto rtn;
9880Sstevel@tonic-gate 				} else goto g0;
9890Sstevel@tonic-gate 			}
9900Sstevel@tonic-gate #endif /* NROFF */
9910Sstevel@tonic-gate #endif /* EUC */
9920Sstevel@tonic-gate 		wp = wordp-1;	/* handle extra space at end of sentence */
9930Sstevel@tonic-gate 		while (wp >= word) {
9940Sstevel@tonic-gate 			j = cbits(*wp--);
9950Sstevel@tonic-gate 			if (j=='"' || j=='\'' || j==')' || j==']' || j=='*' || j==DAGGER)
9960Sstevel@tonic-gate 				continue;
9970Sstevel@tonic-gate 			for (k = 0; sentchar[k]; k++)
9980Sstevel@tonic-gate 				if (j == sentchar[k]) {
9990Sstevel@tonic-gate 					spflg++;
10000Sstevel@tonic-gate 					break;
10010Sstevel@tonic-gate 				}
10020Sstevel@tonic-gate 			break;
10030Sstevel@tonic-gate 		}
10040Sstevel@tonic-gate 	}
10050Sstevel@tonic-gate #ifdef EUC
10060Sstevel@tonic-gate #ifdef NROFF
10070Sstevel@tonic-gate 	wceoll = owc;
10080Sstevel@tonic-gate #endif /* NROFF */
10090Sstevel@tonic-gate #endif /* EUC */
10100Sstevel@tonic-gate 	*wordp = 0;
10110Sstevel@tonic-gate 	numtab[HP].val += sps;
10120Sstevel@tonic-gate rtn:
10130Sstevel@tonic-gate 	for (wp = word; *wp; wp++) {
10140Sstevel@tonic-gate 		j = cbits(*wp);
10150Sstevel@tonic-gate 		if (j == ' ')
10160Sstevel@tonic-gate 			continue;
10170Sstevel@tonic-gate 		if (!ischar(j) || (!isdigit(j) && j != '-'))
10180Sstevel@tonic-gate 			break;
10190Sstevel@tonic-gate 	}
10200Sstevel@tonic-gate 	if (*wp == 0)	/* all numbers, so don't hyphenate */
10210Sstevel@tonic-gate 		hyoff = 1;
10220Sstevel@tonic-gate 	wdstart = 0;
10230Sstevel@tonic-gate 	wordp = word;
10240Sstevel@tonic-gate 	pendw = 0;
10250Sstevel@tonic-gate 	*hyp++ = 0;
10260Sstevel@tonic-gate 	setnel();
10270Sstevel@tonic-gate 	return(noword);
10280Sstevel@tonic-gate }
10290Sstevel@tonic-gate 
10300Sstevel@tonic-gate 
1031*217Smuffin int
10320Sstevel@tonic-gate storeword(c, w)
1033*217Smuffin tchar c;
1034*217Smuffin int	w;
10350Sstevel@tonic-gate {
10360Sstevel@tonic-gate 
10370Sstevel@tonic-gate 	if (wordp >= &word[WDSIZE - 3]) {
10380Sstevel@tonic-gate 		if (!over) {
10390Sstevel@tonic-gate 			flusho();
10400Sstevel@tonic-gate 			errprint(gettext("Word overflow."));
10410Sstevel@tonic-gate 			over++;
10420Sstevel@tonic-gate 			c = LEFTHAND;
10430Sstevel@tonic-gate 			w = -1;
10440Sstevel@tonic-gate 			goto s1;
10450Sstevel@tonic-gate 		}
1046*217Smuffin 		return (0);
10470Sstevel@tonic-gate 	}
10480Sstevel@tonic-gate s1:
10490Sstevel@tonic-gate 	if (w == -1)
10500Sstevel@tonic-gate 		w = width(c);
10510Sstevel@tonic-gate 	widthp = w;
10520Sstevel@tonic-gate 	wne += w;
10530Sstevel@tonic-gate 	*wordp++ = c;
10540Sstevel@tonic-gate 	wch++;
1055*217Smuffin 
1056*217Smuffin 	return (0);
10570Sstevel@tonic-gate }
10580Sstevel@tonic-gate 
10590Sstevel@tonic-gate 
10600Sstevel@tonic-gate #ifdef NROFF
10610Sstevel@tonic-gate tchar gettch()
10620Sstevel@tonic-gate {
10630Sstevel@tonic-gate 	extern int c_isalnum;
10640Sstevel@tonic-gate 	tchar i;
10650Sstevel@tonic-gate 	int j;
10660Sstevel@tonic-gate 
10670Sstevel@tonic-gate 	i = getch();
10680Sstevel@tonic-gate 	j = cbits(i);
10690Sstevel@tonic-gate 	if (ismot(i) || fbits(i) != ulfont)
10700Sstevel@tonic-gate 		return(i);
10710Sstevel@tonic-gate 	if (cu) {
10720Sstevel@tonic-gate 		if (trtab[j] == ' ') {
10730Sstevel@tonic-gate 			setcbits(i, '_');
10740Sstevel@tonic-gate 			setfbits(i, FT);	/* default */
10750Sstevel@tonic-gate 		}
10760Sstevel@tonic-gate 		return(i);
10770Sstevel@tonic-gate 	}
10780Sstevel@tonic-gate 	/* should test here for characters that ought to be underlined */
10790Sstevel@tonic-gate 	/* in the old nroff, that was the 200 bit on the width! */
10800Sstevel@tonic-gate 	/* for now, just do letters, digits and certain special chars */
10810Sstevel@tonic-gate 	if (j <= 127) {
10820Sstevel@tonic-gate 		if (!isalnum(j))
10830Sstevel@tonic-gate 			setfbits(i, FT);
10840Sstevel@tonic-gate 	} else {
10850Sstevel@tonic-gate 		if (j < c_isalnum)
10860Sstevel@tonic-gate 			setfbits(i, FT);
10870Sstevel@tonic-gate 	}
10880Sstevel@tonic-gate 	return(i);
10890Sstevel@tonic-gate }
10900Sstevel@tonic-gate 
10910Sstevel@tonic-gate 
10920Sstevel@tonic-gate #endif
10930Sstevel@tonic-gate #ifdef EUC
10940Sstevel@tonic-gate #ifdef NROFF
1095*217Smuffin int
10960Sstevel@tonic-gate collectmb(i)
10970Sstevel@tonic-gate tchar	i;
10980Sstevel@tonic-gate {
10990Sstevel@tonic-gate 	int busy;
11000Sstevel@tonic-gate 
11010Sstevel@tonic-gate 	*mtbufp++ = i;
11020Sstevel@tonic-gate 	*mbbuf2p++ = i & BYTEMASK;
11030Sstevel@tonic-gate 	*mtbufp = *mbbuf2p = 0;
11040Sstevel@tonic-gate 	if (ismot(i)) {
11050Sstevel@tonic-gate 		mtbufp = mtbuf;
11060Sstevel@tonic-gate 		mbbuf2p = mbbuf2;
11070Sstevel@tonic-gate 		owc = 0;
11080Sstevel@tonic-gate 		cwc = 0;
11090Sstevel@tonic-gate 		return(busy = 0);
11100Sstevel@tonic-gate 	}
11110Sstevel@tonic-gate 	if ((i & MBMASK) == MIDDLEOFMB) {
11120Sstevel@tonic-gate 		if (mtbufp <= (mtbuf + MB_CUR_MAX)) {
11130Sstevel@tonic-gate 			busy = 1;
11140Sstevel@tonic-gate 		} else {
11150Sstevel@tonic-gate 			*(mtbufp - 1) &= ~MBMASK;
11160Sstevel@tonic-gate 			goto gotmb;
11170Sstevel@tonic-gate 		}
11180Sstevel@tonic-gate 	} else {
11190Sstevel@tonic-gate 		if ((i & MBMASK) == LASTOFMB)
11200Sstevel@tonic-gate 			*(mtbufp - 1) &= ~MBMASK;
11210Sstevel@tonic-gate gotmb:
11220Sstevel@tonic-gate 		mtbufp = mtbuf;
11230Sstevel@tonic-gate 		owc = cwc;
11240Sstevel@tonic-gate 		if (mbtowc(&cwc, mbbuf2, MB_CUR_MAX) <= 0) {
11250Sstevel@tonic-gate 			mtbufp = mtbuf;
11260Sstevel@tonic-gate 			while (*mtbufp) {
11270Sstevel@tonic-gate 				setcbits(*mtbufp, (*mtbufp & 0x1ff));
11280Sstevel@tonic-gate 				mtbufp++;
11290Sstevel@tonic-gate 			}
11300Sstevel@tonic-gate 			mtbufp = mtbuf;
11310Sstevel@tonic-gate 		}
11320Sstevel@tonic-gate 		mbbuf2p = mbbuf2;
11330Sstevel@tonic-gate 		busy = 0;
11340Sstevel@tonic-gate 	}
11350Sstevel@tonic-gate 	return(busy);
11360Sstevel@tonic-gate }
11370Sstevel@tonic-gate 
11380Sstevel@tonic-gate 
11390Sstevel@tonic-gate #endif /* NROFF */
11400Sstevel@tonic-gate #endif /* EUC */
1141