xref: /onnv-gate/usr/src/cmd/troff/n2.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 1989 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 /*
430Sstevel@tonic-gate  * n2.c
440Sstevel@tonic-gate  *
450Sstevel@tonic-gate  * output, cleanup
460Sstevel@tonic-gate  */
470Sstevel@tonic-gate 
480Sstevel@tonic-gate #include <signal.h>
490Sstevel@tonic-gate #include "tdef.h"
500Sstevel@tonic-gate #ifdef NROFF
510Sstevel@tonic-gate #include "tw.h"
520Sstevel@tonic-gate #endif
530Sstevel@tonic-gate #include <setjmp.h>
540Sstevel@tonic-gate #include "ext.h"
550Sstevel@tonic-gate #ifdef EUC
560Sstevel@tonic-gate #ifdef NROFF
570Sstevel@tonic-gate #include <stddef.h>
580Sstevel@tonic-gate #include <widec.h>
590Sstevel@tonic-gate #include <limits.h>
600Sstevel@tonic-gate #include <ctype.h>
610Sstevel@tonic-gate 
620Sstevel@tonic-gate char mbobuf[MB_LEN_MAX] = {0};
630Sstevel@tonic-gate wchar_t wchar;
640Sstevel@tonic-gate int	nmb1 = 0;
650Sstevel@tonic-gate #endif /* NROFF */
660Sstevel@tonic-gate #endif /* EUC */
670Sstevel@tonic-gate 
680Sstevel@tonic-gate extern	jmp_buf	sjbuf;
690Sstevel@tonic-gate int	toolate;
700Sstevel@tonic-gate int	error;
710Sstevel@tonic-gate 
72*217Smuffin int
pchar(i)730Sstevel@tonic-gate pchar(i)
74*217Smuffin 	tchar i;
750Sstevel@tonic-gate {
76*217Smuffin 	int j;
770Sstevel@tonic-gate 	static int hx = 0;	/* records if have seen HX */
780Sstevel@tonic-gate 
790Sstevel@tonic-gate 	if (hx) {
800Sstevel@tonic-gate 		hx = 0;
810Sstevel@tonic-gate 		j = absmot(i);
820Sstevel@tonic-gate 		if (isnmot(i)) {
830Sstevel@tonic-gate 			if (j > dip->blss)
840Sstevel@tonic-gate 				dip->blss = j;
850Sstevel@tonic-gate 		} else {
860Sstevel@tonic-gate 			if (j > dip->alss)
870Sstevel@tonic-gate 				dip->alss = j;
880Sstevel@tonic-gate 			ralss = dip->alss;
890Sstevel@tonic-gate 		}
90*217Smuffin 		return (0);
910Sstevel@tonic-gate 	}
920Sstevel@tonic-gate 	if (ismot(i)) {
930Sstevel@tonic-gate 		pchar1(i);
94*217Smuffin 		return (0);
950Sstevel@tonic-gate 	}
960Sstevel@tonic-gate 	switch (j = cbits(i)) {
970Sstevel@tonic-gate 	case 0:
980Sstevel@tonic-gate 	case IMP:
990Sstevel@tonic-gate 	case RIGHT:
1000Sstevel@tonic-gate 	case LEFT:
101*217Smuffin 		return (0);
1020Sstevel@tonic-gate 	case HX:
1030Sstevel@tonic-gate 		hx = 1;
104*217Smuffin 		return (0);
1050Sstevel@tonic-gate 	case PRESC:
1060Sstevel@tonic-gate 		if (dip == &d[0])
1070Sstevel@tonic-gate 			j = eschar;	/* fall through */
1080Sstevel@tonic-gate 	default:
1090Sstevel@tonic-gate #ifndef EUC
1100Sstevel@tonic-gate 		setcbits(i, trtab[j]);
1110Sstevel@tonic-gate #else
1120Sstevel@tonic-gate #ifndef NROFF
1130Sstevel@tonic-gate 		setcbits(i, trtab[j]);
1140Sstevel@tonic-gate #else
1150Sstevel@tonic-gate 		if (!multi_locale || (!(j & CSMASK) && !(j & MBMASK1)))
1160Sstevel@tonic-gate 			setcbits(i, trtab[j]);
1170Sstevel@tonic-gate #endif /* NROFF */
1180Sstevel@tonic-gate #endif /* EUC */
1190Sstevel@tonic-gate 	}
1200Sstevel@tonic-gate 	pchar1(i);
121*217Smuffin 
122*217Smuffin 	return (0);
1230Sstevel@tonic-gate }
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate 
126*217Smuffin int
pchar1(i)1270Sstevel@tonic-gate pchar1(i)
128*217Smuffin 	tchar i;
1290Sstevel@tonic-gate {
130*217Smuffin 	int	j;
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate 	j = cbits(i);
1330Sstevel@tonic-gate 	if (dip != &d[0]) {
1340Sstevel@tonic-gate 		wbf(i);
1350Sstevel@tonic-gate 		dip->op = offset;
136*217Smuffin 		return (0);
1370Sstevel@tonic-gate 	}
1380Sstevel@tonic-gate 	if (!tflg && !print) {
1390Sstevel@tonic-gate 		if (j == '\n')
1400Sstevel@tonic-gate 			dip->alss = dip->blss = 0;
141*217Smuffin 		return (0);
1420Sstevel@tonic-gate 	}
1430Sstevel@tonic-gate 	if (no_out || j == FILLER)
144*217Smuffin 		return (0);
1450Sstevel@tonic-gate 	if (tflg) {	/* transparent mode, undiverted */
1460Sstevel@tonic-gate 		fdprintf(ptid, "%c", j);
147*217Smuffin 		return (0);
1480Sstevel@tonic-gate 	}
1490Sstevel@tonic-gate #ifndef NROFF
1500Sstevel@tonic-gate 	if (ascii)
1510Sstevel@tonic-gate 		outascii(i);
1520Sstevel@tonic-gate 	else
1530Sstevel@tonic-gate #endif
1540Sstevel@tonic-gate 		ptout(i);
155*217Smuffin 
156*217Smuffin 	return (0);
1570Sstevel@tonic-gate }
1580Sstevel@tonic-gate 
159*217Smuffin int
outascii(i)1600Sstevel@tonic-gate outascii(i)	/* print i in best-guess ascii */
1610Sstevel@tonic-gate 	tchar i;
1620Sstevel@tonic-gate {
1630Sstevel@tonic-gate 	int j = cbits(i);
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate 	if (ismot(i)) {
1660Sstevel@tonic-gate 		oput(' ');
167*217Smuffin 		return (0);
1680Sstevel@tonic-gate 	}
1690Sstevel@tonic-gate 	if (j < 0177 && j >= ' ' || j == '\n') {
1700Sstevel@tonic-gate 		oput(j);
171*217Smuffin 		return (0);
1720Sstevel@tonic-gate 	}
1730Sstevel@tonic-gate 	if (j == DRAWFCN)
1740Sstevel@tonic-gate 		oputs("\\D");
1750Sstevel@tonic-gate 	else if (j == HYPHEN || j == MINUS)
1760Sstevel@tonic-gate 		oput('-');
1770Sstevel@tonic-gate 	else if (j == XON)
1780Sstevel@tonic-gate 		oputs("\\X");
1790Sstevel@tonic-gate 	else if (j == LIG_FI)
1800Sstevel@tonic-gate 		oputs("fi");
1810Sstevel@tonic-gate 	else if (j == LIG_FL)
1820Sstevel@tonic-gate 		oputs("fl");
1830Sstevel@tonic-gate 	else if (j == LIG_FF)
1840Sstevel@tonic-gate 		oputs("ff");
1850Sstevel@tonic-gate 	else if (j == LIG_FFI)
1860Sstevel@tonic-gate 		oputs("ffi");
1870Sstevel@tonic-gate 	else if (j == LIG_FFL)
1880Sstevel@tonic-gate 		oputs("ffl");
1890Sstevel@tonic-gate 	else if (j == WORDSP)
1900Sstevel@tonic-gate 		;	/* nothing at all */
1910Sstevel@tonic-gate 	else if (j > 0177) {
1920Sstevel@tonic-gate 		oput('\\');
1930Sstevel@tonic-gate 		oput('(');
1940Sstevel@tonic-gate 		oput(chname[chtab[j-128]]);
1950Sstevel@tonic-gate 		oput(chname[chtab[j-128]+1]);
1960Sstevel@tonic-gate 	}
197*217Smuffin 
198*217Smuffin 	return (0);
1990Sstevel@tonic-gate }
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate 
2020Sstevel@tonic-gate /*
2030Sstevel@tonic-gate  * now a macro
204*217Smuffin int
2050Sstevel@tonic-gate oput(i)
206*217Smuffin 	int	i;
2070Sstevel@tonic-gate {
2080Sstevel@tonic-gate 	*obufp++ = i;
2090Sstevel@tonic-gate 	if (obufp >= &obuf[OBUFSZ])
2100Sstevel@tonic-gate 		flusho();
211*217Smuffin 
212*217Smuffin 	return (0);
2130Sstevel@tonic-gate }
2140Sstevel@tonic-gate */
2150Sstevel@tonic-gate 
216*217Smuffin int
oputs(i)2170Sstevel@tonic-gate oputs(i)
218*217Smuffin char	*i;
2190Sstevel@tonic-gate {
2200Sstevel@tonic-gate 	while (*i != 0)
2210Sstevel@tonic-gate 		oput(*i++);
222*217Smuffin 
223*217Smuffin 	return (0);
2240Sstevel@tonic-gate }
2250Sstevel@tonic-gate 
2260Sstevel@tonic-gate 
227*217Smuffin int
flusho()2280Sstevel@tonic-gate flusho()
2290Sstevel@tonic-gate {
2300Sstevel@tonic-gate 	if (obufp == obuf)
231*217Smuffin 		return (0);
2320Sstevel@tonic-gate 	if (no_out == 0) {
2330Sstevel@tonic-gate 		if (!toolate) {
2340Sstevel@tonic-gate 			toolate++;
2350Sstevel@tonic-gate #ifdef NROFF
2360Sstevel@tonic-gate 			set_tty();
2370Sstevel@tonic-gate 			{
2380Sstevel@tonic-gate 				char	*p = t.twinit;
2390Sstevel@tonic-gate 				while (*p++)
2400Sstevel@tonic-gate 					;
2410Sstevel@tonic-gate 				if (p - t.twinit > 1)
2420Sstevel@tonic-gate 					write(ptid, t.twinit, p - t.twinit - 1);
2430Sstevel@tonic-gate 			}
2440Sstevel@tonic-gate #endif
2450Sstevel@tonic-gate 		}
2460Sstevel@tonic-gate 		toolate += write(ptid, obuf, obufp - obuf);
2470Sstevel@tonic-gate 	}
2480Sstevel@tonic-gate 	obufp = obuf;
249*217Smuffin 
250*217Smuffin 	return (0);
2510Sstevel@tonic-gate }
2520Sstevel@tonic-gate 
2530Sstevel@tonic-gate 
254*217Smuffin int
done(x)2550Sstevel@tonic-gate done(x)
2560Sstevel@tonic-gate int	x;
2570Sstevel@tonic-gate {
258*217Smuffin 	int	i;
2590Sstevel@tonic-gate 
2600Sstevel@tonic-gate 	error |= x;
2610Sstevel@tonic-gate 	app = ds = lgf = 0;
2620Sstevel@tonic-gate 	if (i = em) {
2630Sstevel@tonic-gate 		donef = -1;
2640Sstevel@tonic-gate 		em = 0;
2650Sstevel@tonic-gate 		if (control(i, 0))
2660Sstevel@tonic-gate 			longjmp(sjbuf, 1);
2670Sstevel@tonic-gate 	}
2680Sstevel@tonic-gate 	if (!nfo)
2690Sstevel@tonic-gate 		done3(0);
2700Sstevel@tonic-gate 	mflg = 0;
2710Sstevel@tonic-gate 	dip = &d[0];
2720Sstevel@tonic-gate 	if (woff)
2730Sstevel@tonic-gate 		wbt((tchar)0);
2740Sstevel@tonic-gate 	if (pendw)
2750Sstevel@tonic-gate 		getword(1);
2760Sstevel@tonic-gate 	pendnf = 0;
2770Sstevel@tonic-gate 	if (donef == 1)
2780Sstevel@tonic-gate 		done1(0);
2790Sstevel@tonic-gate 	donef = 1;
2800Sstevel@tonic-gate 	ip = 0;
2810Sstevel@tonic-gate 	frame = stk;
2820Sstevel@tonic-gate 	nxf = frame + 1;
2830Sstevel@tonic-gate 	if (!ejf)
2840Sstevel@tonic-gate 		tbreak();
2850Sstevel@tonic-gate 	nflush++;
2860Sstevel@tonic-gate 	eject((struct s *)0);
2870Sstevel@tonic-gate 	longjmp(sjbuf, 1);
288*217Smuffin 
289*217Smuffin 	return (0);
2900Sstevel@tonic-gate }
2910Sstevel@tonic-gate 
2920Sstevel@tonic-gate 
293*217Smuffin int
done1(x)2940Sstevel@tonic-gate done1(x)
2950Sstevel@tonic-gate int	x;
2960Sstevel@tonic-gate {
2970Sstevel@tonic-gate 	error |= x;
2980Sstevel@tonic-gate 	if (numtab[NL].val) {
2990Sstevel@tonic-gate 		trap = 0;
3000Sstevel@tonic-gate 		eject((struct s *)0);
3010Sstevel@tonic-gate 		longjmp(sjbuf, 1);
3020Sstevel@tonic-gate 	}
3030Sstevel@tonic-gate 	if (nofeed) {
3040Sstevel@tonic-gate 		ptlead();
3050Sstevel@tonic-gate 		flusho();
3060Sstevel@tonic-gate 		done3(0);
3070Sstevel@tonic-gate 	} else {
3080Sstevel@tonic-gate 		if (!gflag)
3090Sstevel@tonic-gate 			pttrailer();
3100Sstevel@tonic-gate 		done2(0);
3110Sstevel@tonic-gate 	}
312*217Smuffin 
313*217Smuffin 	return (0);
3140Sstevel@tonic-gate }
3150Sstevel@tonic-gate 
3160Sstevel@tonic-gate 
317*217Smuffin int
done2(x)3180Sstevel@tonic-gate done2(x)
3190Sstevel@tonic-gate int	x;
3200Sstevel@tonic-gate {
3210Sstevel@tonic-gate 	ptlead();
3220Sstevel@tonic-gate #ifndef NROFF
3230Sstevel@tonic-gate 	if (!ascii)
3240Sstevel@tonic-gate 		ptstop();
3250Sstevel@tonic-gate #endif
3260Sstevel@tonic-gate 	flusho();
3270Sstevel@tonic-gate 	done3(x);
328*217Smuffin 
329*217Smuffin 	return (0);
3300Sstevel@tonic-gate }
3310Sstevel@tonic-gate 
332*217Smuffin int
done3(x)3330Sstevel@tonic-gate done3(x)
3340Sstevel@tonic-gate int	x;
3350Sstevel@tonic-gate {
3360Sstevel@tonic-gate 	error |= x;
3370Sstevel@tonic-gate 	signal(SIGINT, SIG_IGN);
3380Sstevel@tonic-gate 	signal(SIGTERM, SIG_IGN);
3390Sstevel@tonic-gate 	unlink(unlkp);
3400Sstevel@tonic-gate #ifdef NROFF
3410Sstevel@tonic-gate 	twdone();
3420Sstevel@tonic-gate #endif
3430Sstevel@tonic-gate 	if (ascii)
3440Sstevel@tonic-gate 		mesg(1);
3450Sstevel@tonic-gate 	exit(error);
346*217Smuffin 
347*217Smuffin 	return (0);
3480Sstevel@tonic-gate }
3490Sstevel@tonic-gate 
3500Sstevel@tonic-gate 
351*217Smuffin int
edone(x)3520Sstevel@tonic-gate edone(x)
3530Sstevel@tonic-gate int	x;
3540Sstevel@tonic-gate {
3550Sstevel@tonic-gate 	frame = stk;
3560Sstevel@tonic-gate 	nxf = frame + 1;
3570Sstevel@tonic-gate 	ip = 0;
3580Sstevel@tonic-gate 	done(x);
359*217Smuffin 
360*217Smuffin 	return (0);
3610Sstevel@tonic-gate }
3620Sstevel@tonic-gate 
3630Sstevel@tonic-gate 
364*217Smuffin int
casepi()3650Sstevel@tonic-gate casepi()
3660Sstevel@tonic-gate {
367*217Smuffin 	int	i;
3680Sstevel@tonic-gate 	int	id[2];
3690Sstevel@tonic-gate 
3700Sstevel@tonic-gate 	if (toolate || skip() || !getname() || pipe(id) == -1 || (i = fork()) == -1) {
3710Sstevel@tonic-gate 		errprint(gettext("Pipe not created."));
372*217Smuffin 		return (0);
3730Sstevel@tonic-gate 	}
3740Sstevel@tonic-gate 	ptid = id[1];
3750Sstevel@tonic-gate 	if (i > 0) {
3760Sstevel@tonic-gate 		close(id[0]);
3770Sstevel@tonic-gate 		toolate++;
3780Sstevel@tonic-gate 		pipeflg++;
379*217Smuffin 		return (0);
3800Sstevel@tonic-gate 	}
3810Sstevel@tonic-gate 	close(0);
3820Sstevel@tonic-gate 	dup(id[0]);
3830Sstevel@tonic-gate 	close(id[1]);
3840Sstevel@tonic-gate 	execl(nextf, nextf, 0);
3850Sstevel@tonic-gate 	errprint(gettext("Cannot exec %s"), nextf);
3860Sstevel@tonic-gate 	exit(-4);
387*217Smuffin 
388*217Smuffin 	return (0);
3890Sstevel@tonic-gate }
390