xref: /onnv-gate/usr/src/cmd/troff/n1.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 2004 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 char *xxxvers = "@(#)roff:n1.c	2.13";
430Sstevel@tonic-gate /*
440Sstevel@tonic-gate  * n1.c
450Sstevel@tonic-gate  *
460Sstevel@tonic-gate  *	consume options, initialization, main loop,
470Sstevel@tonic-gate  *	input routines, escape function calling
480Sstevel@tonic-gate  */
490Sstevel@tonic-gate 
500Sstevel@tonic-gate #include <ctype.h>
510Sstevel@tonic-gate #include <signal.h>
520Sstevel@tonic-gate #include <sys/types.h>
530Sstevel@tonic-gate #include <sys/stat.h>
540Sstevel@tonic-gate #include <setjmp.h>
550Sstevel@tonic-gate #include <time.h>
560Sstevel@tonic-gate #include <stdarg.h>
570Sstevel@tonic-gate #include <locale.h>
580Sstevel@tonic-gate #include <fcntl.h>
590Sstevel@tonic-gate #include <stdlib.h>
600Sstevel@tonic-gate #include <string.h>
610Sstevel@tonic-gate #ifdef 	EUC
620Sstevel@tonic-gate #ifdef	NROFF
630Sstevel@tonic-gate #include <stddef.h>
640Sstevel@tonic-gate #include <limits.h>
650Sstevel@tonic-gate #endif	/* NROFF */
660Sstevel@tonic-gate #endif	/* EUC */
670Sstevel@tonic-gate 
680Sstevel@tonic-gate #include "tdef.h"
690Sstevel@tonic-gate #include "ext.h"
700Sstevel@tonic-gate 
710Sstevel@tonic-gate #ifdef NROFF
720Sstevel@tonic-gate #include "tw.h"
730Sstevel@tonic-gate #endif
740Sstevel@tonic-gate 
750Sstevel@tonic-gate #define	MAX_RECURSION_DEPTH	512
760Sstevel@tonic-gate 
770Sstevel@tonic-gate jmp_buf sjbuf;
780Sstevel@tonic-gate extern	void	fdprintf(int, char *, ...);
790Sstevel@tonic-gate extern	char	*roff_sprintf(char *, char *, ...);
800Sstevel@tonic-gate filep	ipl[NSO];
810Sstevel@tonic-gate long	offl[NSO];
820Sstevel@tonic-gate long	ioff;
830Sstevel@tonic-gate char	*ttyp;
840Sstevel@tonic-gate char	cfname[NSO+1][NS];	/*file name stack*/
850Sstevel@tonic-gate int	cfline[NSO];		/*input line count stack*/
860Sstevel@tonic-gate char	*progname;	/* program name (troff) */
870Sstevel@tonic-gate #ifdef	EUC
880Sstevel@tonic-gate #ifdef	NROFF
890Sstevel@tonic-gate char	mbbuf1[MB_LEN_MAX + 1];
900Sstevel@tonic-gate char	*mbbuf1p = mbbuf1;
910Sstevel@tonic-gate wchar_t	twc = 0;
920Sstevel@tonic-gate #endif	/* NROFF */
930Sstevel@tonic-gate #endif	/* EUC */
940Sstevel@tonic-gate 
950Sstevel@tonic-gate #ifdef	DEBUG
960Sstevel@tonic-gate int	debug = 0;	/*debug flag*/
97*217Smuffin #endif	/* DEBUG */
98*217Smuffin 
99*217Smuffin static char *sprintn(char *, long, int);
100*217Smuffin static int printn(long, int);
1010Sstevel@tonic-gate 
102*217Smuffin int
main(int argc,char ** argv)103*217Smuffin main(int argc, char **argv)
1040Sstevel@tonic-gate {
105*217Smuffin 	char	*p, *q;
106*217Smuffin 	int	j;
107*217Smuffin 	tchar i;
1080Sstevel@tonic-gate 	int eileenct;		/*count to test for "Eileen's loop"*/
1090Sstevel@tonic-gate 	extern void catch(), kcatch();
1100Sstevel@tonic-gate 	char	**oargv, *getenv();
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate 	(void)setlocale(LC_ALL, "");
1130Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
1140Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST"
1150Sstevel@tonic-gate #endif
1160Sstevel@tonic-gate 	(void)textdomain(TEXT_DOMAIN);
1170Sstevel@tonic-gate 	progname = argv[0];
1180Sstevel@tonic-gate 	if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
1190Sstevel@tonic-gate 		signal(SIGHUP, catch);
1200Sstevel@tonic-gate 	if (signal(SIGINT, catch) == SIG_IGN) {
1210Sstevel@tonic-gate 		signal(SIGHUP, SIG_IGN);
1220Sstevel@tonic-gate 		signal(SIGINT, SIG_IGN);
1230Sstevel@tonic-gate 		signal(SIGQUIT, SIG_IGN);
1240Sstevel@tonic-gate 	}
1250Sstevel@tonic-gate 	signal(SIGPIPE, catch);
1260Sstevel@tonic-gate 	signal(SIGTERM, kcatch);
1270Sstevel@tonic-gate 	oargv = argv;
1280Sstevel@tonic-gate 	strcpy(cfname[0], "<standard input>");
1290Sstevel@tonic-gate 	mrehash();
1300Sstevel@tonic-gate 	nrehash();
1310Sstevel@tonic-gate 	init0();
1320Sstevel@tonic-gate #ifdef EUC
1330Sstevel@tonic-gate #ifdef NROFF
1340Sstevel@tonic-gate 	(void)localize();
1350Sstevel@tonic-gate #endif /* NROFF */
1360Sstevel@tonic-gate #endif /* EUC */
1370Sstevel@tonic-gate 	if ((p = getenv("TYPESETTER")) != 0)
1380Sstevel@tonic-gate 		strcpy(devname, p);
1390Sstevel@tonic-gate 	while (--argc > 0 && (++argv)[0][0] == '-')
1400Sstevel@tonic-gate 		switch (argv[0][1]) {
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate 		case 'F':	/* switch font tables from default */
1430Sstevel@tonic-gate 			if (argv[0][2] != '\0') {
1440Sstevel@tonic-gate 				strcpy(termtab, &argv[0][2]);
1450Sstevel@tonic-gate 				strcpy(fontfile, &argv[0][2]);
1460Sstevel@tonic-gate 			} else {
1470Sstevel@tonic-gate 				argv++; argc--;
1480Sstevel@tonic-gate 				if (argv[0] != '\0') {
1490Sstevel@tonic-gate 					strcpy(termtab, argv[0]);
1500Sstevel@tonic-gate 					strcpy(fontfile, argv[0]);
1510Sstevel@tonic-gate 				} else
1520Sstevel@tonic-gate 					errprint(gettext("missing the font directory"));
1530Sstevel@tonic-gate 			}
1540Sstevel@tonic-gate 			continue;
1550Sstevel@tonic-gate 		case 0:
1560Sstevel@tonic-gate 			goto start;
1570Sstevel@tonic-gate 		case 'i':
1580Sstevel@tonic-gate 			stdi++;
1590Sstevel@tonic-gate 			continue;
1600Sstevel@tonic-gate 		case 'q':
1610Sstevel@tonic-gate #ifdef	NROFF
1620Sstevel@tonic-gate 			quiet++;
1630Sstevel@tonic-gate 			save_tty();
1640Sstevel@tonic-gate #else
1650Sstevel@tonic-gate 			errprint(gettext("-q option ignored in troff"));
166*217Smuffin #endif	/* NROFF */
1670Sstevel@tonic-gate 			continue;
1680Sstevel@tonic-gate 		case 'n':
1690Sstevel@tonic-gate 			npn = ctoi(&argv[0][2]);
1700Sstevel@tonic-gate 			continue;
1710Sstevel@tonic-gate 		case 'u':	/* set emboldening amount */
1720Sstevel@tonic-gate 			bdtab[3] = ctoi(&argv[0][2]);
1730Sstevel@tonic-gate 			if (bdtab[3] < 0 || bdtab[3] > 50)
1740Sstevel@tonic-gate 				bdtab[3] = 0;
1750Sstevel@tonic-gate 			continue;
1760Sstevel@tonic-gate 		case 's':
1770Sstevel@tonic-gate 			if (!(stop = ctoi(&argv[0][2])))
1780Sstevel@tonic-gate 				stop++;
1790Sstevel@tonic-gate 			continue;
1800Sstevel@tonic-gate 		case 't':
1810Sstevel@tonic-gate 			ptid = 1;
1820Sstevel@tonic-gate 			continue;
1830Sstevel@tonic-gate 		case 'r':
1840Sstevel@tonic-gate 			if (&argv[0][2] != '\0' && strlen(&argv[0][2]) >= 2 && &argv[0][3] != '\0')
1850Sstevel@tonic-gate 			eibuf = roff_sprintf(ibuf+strlen(ibuf), ".nr %c %s\n",
1860Sstevel@tonic-gate 				argv[0][2], &argv[0][3]);
1870Sstevel@tonic-gate 			else
1880Sstevel@tonic-gate 				errprint(gettext("wrong options"));
1890Sstevel@tonic-gate 			continue;
1900Sstevel@tonic-gate 		case 'c':
1910Sstevel@tonic-gate 		case 'm':
1920Sstevel@tonic-gate 			if (mflg++ >= NMF) {
1930Sstevel@tonic-gate 				errprint(gettext("Too many macro packages: %s"),
1940Sstevel@tonic-gate 					 argv[0]);
1950Sstevel@tonic-gate 				continue;
1960Sstevel@tonic-gate 			}
1970Sstevel@tonic-gate 		        if (argv[0][2] == '\0') {
1980Sstevel@tonic-gate 				errprint(gettext("No library provided with -m"));
1990Sstevel@tonic-gate 				done(02);
2000Sstevel@tonic-gate 			}
2010Sstevel@tonic-gate 			if (getenv("TROFFMACS") != '\0') {
2020Sstevel@tonic-gate 			     if (tryfile(getenv("TROFFMACS"), &argv[0][2], nmfi))
2030Sstevel@tonic-gate 			       nmfi++;
2040Sstevel@tonic-gate 			} else
2050Sstevel@tonic-gate 			  if (tryfile("/usr/share/lib/tmac/", &argv[0][2], nmfi)
2060Sstevel@tonic-gate 			  || tryfile("/usr/share/lib/tmac/tmac.", &argv[0][2], nmfi))
2070Sstevel@tonic-gate 				nmfi++;
2080Sstevel@tonic-gate 			  else {
2090Sstevel@tonic-gate 				errprint(gettext("Cannot find library %s\n"),
2100Sstevel@tonic-gate 					argv[0]);
2110Sstevel@tonic-gate 				done(02);
2120Sstevel@tonic-gate 			  }
2130Sstevel@tonic-gate 			continue;
2140Sstevel@tonic-gate 		case 'o':
2150Sstevel@tonic-gate 			getpn(&argv[0][2]);
2160Sstevel@tonic-gate 			continue;
2170Sstevel@tonic-gate 		case 'T':
2180Sstevel@tonic-gate 			strcpy(devname, &argv[0][2]);
2190Sstevel@tonic-gate 			dotT++;
2200Sstevel@tonic-gate 			continue;
2210Sstevel@tonic-gate #ifdef NROFF
2220Sstevel@tonic-gate 		case 'h':
2230Sstevel@tonic-gate 			hflg++;
2240Sstevel@tonic-gate 			continue;
2250Sstevel@tonic-gate 		case 'z':
2260Sstevel@tonic-gate 			no_out++;
2270Sstevel@tonic-gate 			continue;
2280Sstevel@tonic-gate 		case 'e':
2290Sstevel@tonic-gate 			eqflg++;
2300Sstevel@tonic-gate 			continue;
2310Sstevel@tonic-gate #endif
2320Sstevel@tonic-gate #ifndef NROFF
2330Sstevel@tonic-gate 		case 'z':
2340Sstevel@tonic-gate 			no_out++;
2350Sstevel@tonic-gate 		case 'a':
2360Sstevel@tonic-gate 			ascii = 1;
2370Sstevel@tonic-gate 			nofeed++;
2380Sstevel@tonic-gate 			continue;
2390Sstevel@tonic-gate 		case 'f':
2400Sstevel@tonic-gate 			nofeed++;
2410Sstevel@tonic-gate 			continue;
2420Sstevel@tonic-gate #endif
2430Sstevel@tonic-gate 		case '#':
2440Sstevel@tonic-gate #ifdef	DEBUG
2450Sstevel@tonic-gate 			debug = ctoi(&argv[0][2]);
2460Sstevel@tonic-gate #else
2470Sstevel@tonic-gate 			errprint("DEBUG not enabled");
248*217Smuffin #endif	/* DEBUG */
2490Sstevel@tonic-gate 			continue;
2500Sstevel@tonic-gate 		default:
2510Sstevel@tonic-gate 			errprint(gettext("unknown option %s"), argv[0]);
2520Sstevel@tonic-gate 			done(02);
2530Sstevel@tonic-gate 		}
2540Sstevel@tonic-gate 
2550Sstevel@tonic-gate start:
2560Sstevel@tonic-gate 	init1(oargv[0][0]);
2570Sstevel@tonic-gate 	argp = argv;
2580Sstevel@tonic-gate 	rargc = argc;
2590Sstevel@tonic-gate 	nmfi = 0;
2600Sstevel@tonic-gate 	init2();
2610Sstevel@tonic-gate 	setjmp(sjbuf);
2620Sstevel@tonic-gate 	eileenct = 0;		/*reset count for "Eileen's loop"*/
2630Sstevel@tonic-gate loop:
2640Sstevel@tonic-gate 	copyf = lgf = nb = nflush = nlflg = 0;
2650Sstevel@tonic-gate 	if (ip && rbf0(ip) == 0 && ejf && frame->pframe <= ejl) {
2660Sstevel@tonic-gate 		nflush++;
2670Sstevel@tonic-gate 		trap = 0;
2680Sstevel@tonic-gate 		eject((struct s *)0);
2690Sstevel@tonic-gate #ifdef	DEBUG
2700Sstevel@tonic-gate 	if (debug & DB_LOOP)
2710Sstevel@tonic-gate 		fdprintf(stderr, "loop: NL=%d, ejf=%d, lss=%d, eileenct=%d\n",
2720Sstevel@tonic-gate 			numtab[NL].val, ejf, lss, eileenct);
273*217Smuffin #endif	/* DEBUG */
2740Sstevel@tonic-gate 		if (eileenct > 20) {
2750Sstevel@tonic-gate 			errprint(gettext("job looping; check abuse of macros"));
2760Sstevel@tonic-gate 			ejf = 0;	/*try to break Eileen's loop*/
2770Sstevel@tonic-gate 			eileenct = 0;
2780Sstevel@tonic-gate 		} else
2790Sstevel@tonic-gate 			eileenct++;
2800Sstevel@tonic-gate 		goto loop;
2810Sstevel@tonic-gate 	}
2820Sstevel@tonic-gate 	eileenct = 0;		/*reset count for "Eileen's loop"*/
2830Sstevel@tonic-gate 	i = getch();
2840Sstevel@tonic-gate 	if (pendt)
2850Sstevel@tonic-gate 		goto Lt;
2860Sstevel@tonic-gate 	if ((j = cbits(i)) == XPAR) {
2870Sstevel@tonic-gate 		copyf++;
2880Sstevel@tonic-gate 		tflg++;
2890Sstevel@tonic-gate 		while (cbits(i) != '\n')
2900Sstevel@tonic-gate 			pchar(i = getch());
2910Sstevel@tonic-gate 		tflg = 0;
2920Sstevel@tonic-gate 		copyf--;
2930Sstevel@tonic-gate 		goto loop;
2940Sstevel@tonic-gate 	}
2950Sstevel@tonic-gate 	if (j == cc || j == c2) {
2960Sstevel@tonic-gate 		if (j == c2)
2970Sstevel@tonic-gate 			nb++;
2980Sstevel@tonic-gate 		copyf++;
2990Sstevel@tonic-gate 		while ((j = cbits(i = getch())) == ' ' || j == '\t')
3000Sstevel@tonic-gate 			;
3010Sstevel@tonic-gate 		ch = i;
3020Sstevel@tonic-gate 		copyf--;
3030Sstevel@tonic-gate 		control(getrq(), 1);
3040Sstevel@tonic-gate 		flushi();
3050Sstevel@tonic-gate 		goto loop;
3060Sstevel@tonic-gate 	}
3070Sstevel@tonic-gate Lt:
3080Sstevel@tonic-gate 	ch = i;
3090Sstevel@tonic-gate 	text();
3100Sstevel@tonic-gate 	if (nlflg)
3110Sstevel@tonic-gate 		numtab[HP].val = 0;
3120Sstevel@tonic-gate 	goto loop;
3130Sstevel@tonic-gate }
3140Sstevel@tonic-gate 
3150Sstevel@tonic-gate 
316*217Smuffin int
tryfile(pat,fn,idx)3170Sstevel@tonic-gate tryfile(pat, fn, idx)
318*217Smuffin char *pat, *fn;
3190Sstevel@tonic-gate int idx;
3200Sstevel@tonic-gate {
3210Sstevel@tonic-gate 	strcpy(mfiles[idx], pat);
3220Sstevel@tonic-gate 	strcat(mfiles[idx], fn);
3230Sstevel@tonic-gate 	if (access(mfiles[idx], 4) == -1)
3240Sstevel@tonic-gate 		return(0);
3250Sstevel@tonic-gate 	else return(1);
3260Sstevel@tonic-gate }
3270Sstevel@tonic-gate 
catch()3280Sstevel@tonic-gate void catch()
3290Sstevel@tonic-gate {
3300Sstevel@tonic-gate 	done3(01);
3310Sstevel@tonic-gate }
3320Sstevel@tonic-gate 
3330Sstevel@tonic-gate 
kcatch()3340Sstevel@tonic-gate void kcatch()
3350Sstevel@tonic-gate {
3360Sstevel@tonic-gate 	signal(SIGTERM, SIG_IGN);
3370Sstevel@tonic-gate 	done3(01);
3380Sstevel@tonic-gate }
3390Sstevel@tonic-gate 
340*217Smuffin int
init0()3410Sstevel@tonic-gate init0()
3420Sstevel@tonic-gate {
3430Sstevel@tonic-gate 	eibuf = ibufp = ibuf;
3440Sstevel@tonic-gate 	ibuf[0] = 0;
3450Sstevel@tonic-gate 	numtab[NL].val = -1;
346*217Smuffin 	return (0);
3470Sstevel@tonic-gate }
3480Sstevel@tonic-gate 
3490Sstevel@tonic-gate 
350*217Smuffin int
init1(a)3510Sstevel@tonic-gate init1(a)
3520Sstevel@tonic-gate char	a;
3530Sstevel@tonic-gate {
354*217Smuffin 	char	*p;
355*217Smuffin 	int i;
3560Sstevel@tonic-gate 
3570Sstevel@tonic-gate 	p = tmp_name;
3580Sstevel@tonic-gate 	if (a == 'a')
3590Sstevel@tonic-gate 		p = &p[9];
3600Sstevel@tonic-gate 	if ((ibf = mkstemp(p)) == -1) {
3610Sstevel@tonic-gate 		errprint(gettext("cannot create temp file."));
3620Sstevel@tonic-gate 		exit(-1);
3630Sstevel@tonic-gate 	}
3640Sstevel@tonic-gate 	unlkp = p;
3650Sstevel@tonic-gate 	for (i = NTRTAB; --i; )
3660Sstevel@tonic-gate 		trtab[i] = i;
3670Sstevel@tonic-gate 	trtab[UNPAD] = ' ';
368*217Smuffin 	return (0);
3690Sstevel@tonic-gate }
3700Sstevel@tonic-gate 
3710Sstevel@tonic-gate 
372*217Smuffin int
init2()3730Sstevel@tonic-gate init2()
3740Sstevel@tonic-gate {
375*217Smuffin 	int	i, j;
3760Sstevel@tonic-gate 	extern char	*setbrk();
3770Sstevel@tonic-gate 	extern char	*ttyname();
3780Sstevel@tonic-gate 
3790Sstevel@tonic-gate 	ttyod = 2;
3800Sstevel@tonic-gate 	if ((ttyp=ttyname(j=0)) != 0 || (ttyp=ttyname(j=1)) != 0 || (ttyp=ttyname(j=2)) != 0)
3810Sstevel@tonic-gate 		;
3820Sstevel@tonic-gate 	else
3830Sstevel@tonic-gate 		ttyp = "notty";
3840Sstevel@tonic-gate 	iflg = j;
3850Sstevel@tonic-gate 	if (ascii)
3860Sstevel@tonic-gate 		mesg(0);
3870Sstevel@tonic-gate 	obufp = obuf;
3880Sstevel@tonic-gate 	ptinit();
3890Sstevel@tonic-gate 	mchbits();
3900Sstevel@tonic-gate 	cvtime();
3910Sstevel@tonic-gate 	numtab[PID].val = getpid();
3920Sstevel@tonic-gate 	olinep = oline;
3930Sstevel@tonic-gate 	ioff = 0;
3940Sstevel@tonic-gate 	numtab[HP].val = init = 0;
3950Sstevel@tonic-gate 	numtab[NL].val = -1;
3960Sstevel@tonic-gate 	nfo = 0;
3970Sstevel@tonic-gate 	ifile = 0;
3980Sstevel@tonic-gate 	copyf = raw = 0;
3990Sstevel@tonic-gate 	eibuf = roff_sprintf(ibuf+strlen(ibuf), ".ds .T %s\n", devname);
4000Sstevel@tonic-gate 	numtab[CD].val = -1;	/* compensation */
4010Sstevel@tonic-gate 	cpushback(ibuf);
4020Sstevel@tonic-gate 	ibufp = ibuf;
4030Sstevel@tonic-gate 	nx = mflg;
4040Sstevel@tonic-gate 	frame = stk = (struct s *)setbrk(DELTA);
4050Sstevel@tonic-gate 	dip = &d[0];
4060Sstevel@tonic-gate 	nxf = frame + 1;
4070Sstevel@tonic-gate #ifdef INCORE
4080Sstevel@tonic-gate 	for (i = 0; i < NEV; i++) {
4090Sstevel@tonic-gate 		extern tchar corebuf[];
4100Sstevel@tonic-gate 		*(struct env *)&corebuf[i * sizeof(env)/sizeof(tchar)] = env;
4110Sstevel@tonic-gate 	}
4120Sstevel@tonic-gate #else
4130Sstevel@tonic-gate 	for (i = NEV; i--; )
4140Sstevel@tonic-gate 		write(ibf, (char *) & env, sizeof(env));
4150Sstevel@tonic-gate #endif
416*217Smuffin 	return (0);
4170Sstevel@tonic-gate }
4180Sstevel@tonic-gate 
419*217Smuffin int
cvtime()4200Sstevel@tonic-gate cvtime()
4210Sstevel@tonic-gate {
4220Sstevel@tonic-gate 	time_t	tt;
423*217Smuffin 	struct tm *tm;
4240Sstevel@tonic-gate 
4250Sstevel@tonic-gate 	tt = time((time_t *) 0);
4260Sstevel@tonic-gate 	tm = localtime(&tt);
4270Sstevel@tonic-gate 	numtab[DY].val = tm->tm_mday;
4280Sstevel@tonic-gate 	numtab[DW].val = tm->tm_wday + 1;
4290Sstevel@tonic-gate 	numtab[YR].val = tm->tm_year;
4300Sstevel@tonic-gate 	numtab[MO].val = tm->tm_mon + 1;
4310Sstevel@tonic-gate 
432*217Smuffin 	return (0);
4330Sstevel@tonic-gate }
4340Sstevel@tonic-gate 
4350Sstevel@tonic-gate 
436*217Smuffin int
ctoi(s)4370Sstevel@tonic-gate ctoi(s)
438*217Smuffin 	char *s;
4390Sstevel@tonic-gate {
440*217Smuffin 	int	n;
4410Sstevel@tonic-gate 
4420Sstevel@tonic-gate 	while (*s == ' ')
4430Sstevel@tonic-gate 		s++;
4440Sstevel@tonic-gate 	n = 0;
4450Sstevel@tonic-gate 	while (isdigit((unsigned char)*s))
4460Sstevel@tonic-gate 		n = 10 * n + *s++ - '0';
4470Sstevel@tonic-gate 	return n;
4480Sstevel@tonic-gate }
4490Sstevel@tonic-gate 
4500Sstevel@tonic-gate 
451*217Smuffin int
mesg(f)4520Sstevel@tonic-gate mesg(f)
4530Sstevel@tonic-gate int	f;
4540Sstevel@tonic-gate {
4550Sstevel@tonic-gate 	static int	mode;
4560Sstevel@tonic-gate 	struct stat stbuf;
4570Sstevel@tonic-gate 
4580Sstevel@tonic-gate 	if (!f) {
4590Sstevel@tonic-gate 		stat(ttyp, &stbuf);
4600Sstevel@tonic-gate 		mode = stbuf.st_mode;
4610Sstevel@tonic-gate 		chmod(ttyp, mode & ~0122);	/* turn off writing for others */
4620Sstevel@tonic-gate 	} else {
4630Sstevel@tonic-gate 		if (ttyp && *ttyp && mode)
4640Sstevel@tonic-gate 			chmod(ttyp, mode);
4650Sstevel@tonic-gate 	}
466*217Smuffin 
467*217Smuffin 	return (0);
4680Sstevel@tonic-gate }
4690Sstevel@tonic-gate 
470*217Smuffin int
errprint(s,s1,s2,s3,s4,s5)4710Sstevel@tonic-gate errprint(s, s1, s2, s3, s4, s5)	/* error message printer */
4720Sstevel@tonic-gate 	char *s, *s1, *s2, *s3, *s4, *s5;
4730Sstevel@tonic-gate {
4740Sstevel@tonic-gate 	fdprintf(stderr, "%s: ", progname);
4750Sstevel@tonic-gate 	fdprintf(stderr, s, s1, s2, s3, s4, s5);
4760Sstevel@tonic-gate 	if (numtab[CD].val > 0)
4770Sstevel@tonic-gate 		fdprintf(stderr, gettext("; line %d, file %s"), numtab[CD].val,
4780Sstevel@tonic-gate 			 cfname[ifi]);
4790Sstevel@tonic-gate 	fdprintf(stderr, "\n");
4800Sstevel@tonic-gate 	stackdump();
4810Sstevel@tonic-gate #ifdef	DEBUG
4820Sstevel@tonic-gate 	if (debug)
4830Sstevel@tonic-gate 		abort();
484*217Smuffin #endif	/* DEBUG */
485*217Smuffin 	return (0);
4860Sstevel@tonic-gate }
4870Sstevel@tonic-gate 
4880Sstevel@tonic-gate 
4890Sstevel@tonic-gate /*
4900Sstevel@tonic-gate  * Scaled down version of C Library printf.
4910Sstevel@tonic-gate  * Only %s %u %d (==%u) %o %c %x %D are recognized.
4920Sstevel@tonic-gate  */
4930Sstevel@tonic-gate #undef putchar
4940Sstevel@tonic-gate #define	putchar(n)	(*pfbp++ = (n))	/* NO CHECKING! */
4950Sstevel@tonic-gate 
4960Sstevel@tonic-gate static char	pfbuf[NTM];
4970Sstevel@tonic-gate static char	*pfbp = pfbuf;
4980Sstevel@tonic-gate int	stderr	 = 2;	/* NOT stdio value */
4990Sstevel@tonic-gate 
5000Sstevel@tonic-gate void
fdprintf(int fd,char * fmt,...)5010Sstevel@tonic-gate fdprintf(int fd, char *fmt, ...)
5020Sstevel@tonic-gate {
503*217Smuffin 	int	c;
5040Sstevel@tonic-gate 	char	*s;
505*217Smuffin 	int	i;
5060Sstevel@tonic-gate 	va_list	ap;
5070Sstevel@tonic-gate 
5080Sstevel@tonic-gate 	pfbp = pfbuf;
5090Sstevel@tonic-gate 	va_start(ap, fmt);
5100Sstevel@tonic-gate loop:
5110Sstevel@tonic-gate 	while ((c = *fmt++) != '%') {
5120Sstevel@tonic-gate 		if (c == '\0') {
5130Sstevel@tonic-gate 			if (fd == stderr)
5140Sstevel@tonic-gate 				write(stderr, pfbuf, pfbp - pfbuf);
5150Sstevel@tonic-gate 			else {
5160Sstevel@tonic-gate 				*pfbp = 0;
5170Sstevel@tonic-gate 				pfbp = pfbuf;
5180Sstevel@tonic-gate 				while (*pfbp) {
5190Sstevel@tonic-gate 					*obufp++ = *pfbp++;
5200Sstevel@tonic-gate 					if (obufp >= &obuf[OBUFSZ])
5210Sstevel@tonic-gate 						flusho();
5220Sstevel@tonic-gate 				}
5230Sstevel@tonic-gate 			}
5240Sstevel@tonic-gate 			va_end(ap);
5250Sstevel@tonic-gate 			return;
5260Sstevel@tonic-gate 		}
5270Sstevel@tonic-gate 		putchar(c);
5280Sstevel@tonic-gate 	}
5290Sstevel@tonic-gate 	c = *fmt++;
5300Sstevel@tonic-gate 	if (c == 'd') {
5310Sstevel@tonic-gate 		i = va_arg(ap, int);
5320Sstevel@tonic-gate 		if (i < 0) {
5330Sstevel@tonic-gate 			putchar('-');
5340Sstevel@tonic-gate 			i = -i;
5350Sstevel@tonic-gate 		}
5360Sstevel@tonic-gate 		printn((long)i, 10);
5370Sstevel@tonic-gate 	} else if (c == 'u' || c == 'o' || c == 'x')
5380Sstevel@tonic-gate 		printn(va_arg(ap, long), c == 'o' ? 8 : (c == 'x' ? 16 : 10));
5390Sstevel@tonic-gate 	else if (c == 'c') {
5400Sstevel@tonic-gate 		if (c > 0177 || c < 040)
5410Sstevel@tonic-gate 			putchar('\\');
5420Sstevel@tonic-gate 		putchar(va_arg(ap, int) & 0177);
5430Sstevel@tonic-gate 	} else if (c == 's') {
5440Sstevel@tonic-gate 		s = va_arg(ap, char *);
5450Sstevel@tonic-gate 		while (c = *s++)
5460Sstevel@tonic-gate 			putchar(c);
5470Sstevel@tonic-gate 	} else if (c == 'D') {
5480Sstevel@tonic-gate 		printn(va_arg(ap, long), 10);
5490Sstevel@tonic-gate 	} else if (c == 'O') {
5500Sstevel@tonic-gate 		printn(va_arg(ap, long), 8);
5510Sstevel@tonic-gate 	}
5520Sstevel@tonic-gate 	goto loop;
5530Sstevel@tonic-gate }
5540Sstevel@tonic-gate 
5550Sstevel@tonic-gate 
5560Sstevel@tonic-gate /*
5570Sstevel@tonic-gate  * Print an unsigned integer in base b.
5580Sstevel@tonic-gate  */
559*217Smuffin static int
printn(n,b)560*217Smuffin printn(n, b)
561*217Smuffin 	long	n;
562*217Smuffin 	int	b;
5630Sstevel@tonic-gate {
564*217Smuffin 	long	a;
5650Sstevel@tonic-gate 
5660Sstevel@tonic-gate 	if (n < 0) {	/* shouldn't happen */
5670Sstevel@tonic-gate 		putchar('-');
5680Sstevel@tonic-gate 		n = -n;
5690Sstevel@tonic-gate 	}
5700Sstevel@tonic-gate 	if (a = n / b)
5710Sstevel@tonic-gate 		printn(a, b);
5720Sstevel@tonic-gate 	putchar("0123456789ABCDEF"[(int)(n%b)]);
573*217Smuffin 
574*217Smuffin 	return (0);
5750Sstevel@tonic-gate }
5760Sstevel@tonic-gate 
5770Sstevel@tonic-gate /* scaled down version of library roff_sprintf */
5780Sstevel@tonic-gate /* same limits as fdprintf */
5790Sstevel@tonic-gate /* returns pointer to \0 that ends the string */
5800Sstevel@tonic-gate 
5810Sstevel@tonic-gate /* VARARGS2 */
roff_sprintf(char * str,char * fmt,...)5820Sstevel@tonic-gate char *roff_sprintf(char *str, char *fmt, ...)
5830Sstevel@tonic-gate {
584*217Smuffin 	int	c;
5850Sstevel@tonic-gate 	char	*s;
586*217Smuffin 	int	i;
5870Sstevel@tonic-gate 	va_list ap;
5880Sstevel@tonic-gate 
5890Sstevel@tonic-gate 	va_start(ap, fmt);
5900Sstevel@tonic-gate loop:
5910Sstevel@tonic-gate 	while ((c = *fmt++) != '%') {
5920Sstevel@tonic-gate 		if (c == '\0') {
5930Sstevel@tonic-gate 			*str = 0;
5940Sstevel@tonic-gate 			va_end(ap);
5950Sstevel@tonic-gate 			return str;
5960Sstevel@tonic-gate 		}
5970Sstevel@tonic-gate 		*str++ = c;
5980Sstevel@tonic-gate 	}
5990Sstevel@tonic-gate 	c = *fmt++;
6000Sstevel@tonic-gate 	if (c == 'd') {
6010Sstevel@tonic-gate 		i = va_arg(ap, int);
6020Sstevel@tonic-gate 		if (i < 0) {
6030Sstevel@tonic-gate 			*str++ = '-';
6040Sstevel@tonic-gate 			i = -i;
6050Sstevel@tonic-gate 		}
6060Sstevel@tonic-gate 		str = sprintn(str, (long)i, 10);
6070Sstevel@tonic-gate 	} else if (c == 'u' || c == 'o' || c == 'x')
6080Sstevel@tonic-gate 		str = sprintn(str, va_arg(ap, long), c == 'o' ? 8 : (c == 'x' ? 16 : 10));
6090Sstevel@tonic-gate 	else if (c == 'c') {
6100Sstevel@tonic-gate 		if (c > 0177 || c < 040)
6110Sstevel@tonic-gate 			*str++ = '\\';
6120Sstevel@tonic-gate 		*str++ = va_arg(ap, int) & 0177;
6130Sstevel@tonic-gate 	} else if (c == 's') {
6140Sstevel@tonic-gate 		s = va_arg(ap, char *);
6150Sstevel@tonic-gate 		while (c = *s++)
6160Sstevel@tonic-gate 			*str++ = c;
6170Sstevel@tonic-gate 	} else if (c == 'D') {
6180Sstevel@tonic-gate 		str = sprintn(str, va_arg(ap, long), 10);
6190Sstevel@tonic-gate 	} else if (c == 'O') {
6200Sstevel@tonic-gate 		str = sprintn(str, va_arg(ap, unsigned) , 8);
6210Sstevel@tonic-gate 	}
6220Sstevel@tonic-gate 	goto loop;
6230Sstevel@tonic-gate }
6240Sstevel@tonic-gate 
6250Sstevel@tonic-gate /*
6260Sstevel@tonic-gate  * Print an unsigned integer in base b.
6270Sstevel@tonic-gate  */
sprintn(s,n,b)6280Sstevel@tonic-gate static char *sprintn(s, n, b)
629*217Smuffin 	char *s;
630*217Smuffin 	long n;
631*217Smuffin 	int b;
6320Sstevel@tonic-gate {
633*217Smuffin 	long	a;
6340Sstevel@tonic-gate 
6350Sstevel@tonic-gate 	if (n < 0) {	/* shouldn't happen */
6360Sstevel@tonic-gate 		*s++ = '-';
6370Sstevel@tonic-gate 		n = -n;
6380Sstevel@tonic-gate 	}
6390Sstevel@tonic-gate 	if (a = n / b)
6400Sstevel@tonic-gate 		s = sprintn(s, a, b);
6410Sstevel@tonic-gate 	*s++ = "0123456789ABCDEF"[(int)(n%b)];
6420Sstevel@tonic-gate 	return s;
6430Sstevel@tonic-gate }
6440Sstevel@tonic-gate 
6450Sstevel@tonic-gate 
646*217Smuffin int
control(a,b)6470Sstevel@tonic-gate control(a, b)
648*217Smuffin int	a, b;
6490Sstevel@tonic-gate {
650*217Smuffin 	int	j;
6510Sstevel@tonic-gate 
6520Sstevel@tonic-gate 	if (a == 0 || (j = findmn(a)) == -1)
6530Sstevel@tonic-gate 		return(0);
6540Sstevel@tonic-gate 
6550Sstevel@tonic-gate 	/*
6560Sstevel@tonic-gate 	 * Attempt to find endless recursion at runtime. Arbitrary
6570Sstevel@tonic-gate 	 * recursion limit of MAX_RECURSION_DEPTH was chosen as
6580Sstevel@tonic-gate 	 * it is extremely unlikely that a correct nroff/troff
6590Sstevel@tonic-gate 	 * invocation would exceed this value.
6600Sstevel@tonic-gate 	 */
6610Sstevel@tonic-gate 
6620Sstevel@tonic-gate 	if (frame != stk) {
6630Sstevel@tonic-gate 		int frame_cnt = 0;
6640Sstevel@tonic-gate 		struct s *p;
6650Sstevel@tonic-gate 
6660Sstevel@tonic-gate 		for (p = frame; p != stk; p = p->pframe)
6670Sstevel@tonic-gate 			frame_cnt++;
6680Sstevel@tonic-gate 		if (frame_cnt > MAX_RECURSION_DEPTH) {
6690Sstevel@tonic-gate 			errprint(
6700Sstevel@tonic-gate 			    gettext("Exceeded maximum stack size (%d) when "
6710Sstevel@tonic-gate 			    "executing macro %c%c. Stack dump follows"),
6720Sstevel@tonic-gate 			    MAX_RECURSION_DEPTH,
6730Sstevel@tonic-gate 			    frame->mname & 0177, (frame->mname >> BYTE) & 0177);
6740Sstevel@tonic-gate 			edone(02);
6750Sstevel@tonic-gate 		}
6760Sstevel@tonic-gate 	}
6770Sstevel@tonic-gate 
6780Sstevel@tonic-gate #ifdef	DEBUG
6790Sstevel@tonic-gate 	if (debug & DB_MAC)
6800Sstevel@tonic-gate 		fdprintf(stderr, "control: macro %c%c, contab[%d]\n",
6810Sstevel@tonic-gate 			a&0177, (a>>BYTE)&0177 ? (a>>BYTE)&0177 : ' ', j);
682*217Smuffin #endif	/* DEBUG */
6830Sstevel@tonic-gate 	if (contab[j].f == 0) {
6840Sstevel@tonic-gate 		nxf->nargs = 0;
6850Sstevel@tonic-gate 		if (b)
6860Sstevel@tonic-gate 			collect();
6870Sstevel@tonic-gate 		flushi();
6880Sstevel@tonic-gate 		return pushi((filep)contab[j].mx, a);
6890Sstevel@tonic-gate 	} else if (b)
6900Sstevel@tonic-gate 		return((*contab[j].f)(0));
6910Sstevel@tonic-gate 	else
6920Sstevel@tonic-gate 		return(0);
6930Sstevel@tonic-gate }
6940Sstevel@tonic-gate 
695*217Smuffin int
getrq()6960Sstevel@tonic-gate getrq()
6970Sstevel@tonic-gate {
698*217Smuffin 	int	i, j;
6990Sstevel@tonic-gate 
7000Sstevel@tonic-gate 	if (((i = getach()) == 0) || ((j = getach()) == 0))
7010Sstevel@tonic-gate 		goto rtn;
7020Sstevel@tonic-gate 	i = PAIR(i, j);
7030Sstevel@tonic-gate rtn:
7040Sstevel@tonic-gate 	return(i);
7050Sstevel@tonic-gate }
7060Sstevel@tonic-gate 
7070Sstevel@tonic-gate /*
7080Sstevel@tonic-gate  * table encodes some special characters, to speed up tests
7090Sstevel@tonic-gate  * in getchar, viz FLSS, RPT, f, \b, \n, fc, tabch, ldrch
7100Sstevel@tonic-gate  */
7110Sstevel@tonic-gate 
7120Sstevel@tonic-gate char
7130Sstevel@tonic-gate gchtab[] = {
7140Sstevel@tonic-gate 	000,004,000,000,010,000,000,000, /* fc, ldr */
7150Sstevel@tonic-gate 	001,002,001,000,001,000,000,000, /* \b, tab, nl, RPT */
7160Sstevel@tonic-gate 	000,000,000,000,000,000,000,000,
7170Sstevel@tonic-gate 	000,001,000,000,000,000,000,000, /* FLSS */
7180Sstevel@tonic-gate 	000,000,000,000,000,000,000,000,
7190Sstevel@tonic-gate 	000,000,000,000,000,000,000,000,
7200Sstevel@tonic-gate 	000,000,000,000,000,000,000,000,
7210Sstevel@tonic-gate 	000,000,000,000,000,000,000,000,
7220Sstevel@tonic-gate 	000,000,000,000,000,000,000,000,
7230Sstevel@tonic-gate 	000,000,000,000,000,000,000,000,
7240Sstevel@tonic-gate 	000,000,000,000,000,000,000,000,
7250Sstevel@tonic-gate 	000,000,000,000,000,000,000,000,
7260Sstevel@tonic-gate 	000,000,000,000,000,000,001,000, /* f */
7270Sstevel@tonic-gate 	000,000,000,000,000,000,000,000,
7280Sstevel@tonic-gate 	000,000,000,000,000,000,000,000,
7290Sstevel@tonic-gate 	000,000,000,000,000,000,000,000,
7300Sstevel@tonic-gate };
7310Sstevel@tonic-gate 
7320Sstevel@tonic-gate tchar
getch()7330Sstevel@tonic-gate getch()
7340Sstevel@tonic-gate {
735*217Smuffin 	int	k;
736*217Smuffin 	tchar i, j;
7370Sstevel@tonic-gate 	tchar setht(), setslant();
7380Sstevel@tonic-gate 
7390Sstevel@tonic-gate g0:
7400Sstevel@tonic-gate 	if (i = ch) {
7410Sstevel@tonic-gate #ifdef	DEBUG
7420Sstevel@tonic-gate 		if (debug & DB_GETC)
7430Sstevel@tonic-gate 			fdprintf(stderr, "getch: ch is %x (%c)\n",
7440Sstevel@tonic-gate 				ch, (ch&0177) < 040 ? 0177 : ch&0177);
745*217Smuffin #endif	/* DEBUG */
7460Sstevel@tonic-gate 		if (cbits(i) == '\n')
7470Sstevel@tonic-gate 			nlflg++;
7480Sstevel@tonic-gate 		ch = 0;
7490Sstevel@tonic-gate 		return(i);
7500Sstevel@tonic-gate 	}
7510Sstevel@tonic-gate 
7520Sstevel@tonic-gate #ifdef	DEBUG
7530Sstevel@tonic-gate 	if (nlflg)
7540Sstevel@tonic-gate 		if (debug & DB_GETC)
7550Sstevel@tonic-gate 			fdprintf(stderr,"getch: nlflg is %x\n", nlflg);
756*217Smuffin #endif	/* DEBUG */
7570Sstevel@tonic-gate 	if (nlflg)
7580Sstevel@tonic-gate 		return('\n');
7590Sstevel@tonic-gate 	i = getch0();
7600Sstevel@tonic-gate #ifdef	DEBUG
7610Sstevel@tonic-gate 	if (debug & DB_GETC)
7620Sstevel@tonic-gate 		fdprintf(stderr, "getch: getch0 returns %x (%c)\n",
7630Sstevel@tonic-gate 			i, (i&0177) < 040 ? 0177 : i&0177);
764*217Smuffin #endif	/* DEBUG */
7650Sstevel@tonic-gate 	if (ismot(i))
7660Sstevel@tonic-gate 		return(i);
7670Sstevel@tonic-gate 	k = cbits(i);
7680Sstevel@tonic-gate 	if (k != ESC) {
7690Sstevel@tonic-gate 		/*
7700Sstevel@tonic-gate 		 * gchtab[] has only 128 entries
7710Sstevel@tonic-gate 		 * if k is out of the range, it should be
7720Sstevel@tonic-gate 		 * handled as gchtab[k] == 0
7730Sstevel@tonic-gate 		 */
7740Sstevel@tonic-gate 		if (!isascii(k) || gchtab[k]==0)
7750Sstevel@tonic-gate 			return(i);
7760Sstevel@tonic-gate 		if (k == '\n') {
7770Sstevel@tonic-gate 			if (cbits(i) == '\n') {
7780Sstevel@tonic-gate 				nlflg++;
7790Sstevel@tonic-gate 				if (ip == 0)
7800Sstevel@tonic-gate 					numtab[CD].val++; /* line number */
7810Sstevel@tonic-gate 			}
7820Sstevel@tonic-gate 			return(k);
7830Sstevel@tonic-gate 		}
7840Sstevel@tonic-gate 		if (k == FLSS) {
7850Sstevel@tonic-gate 			copyf++;
7860Sstevel@tonic-gate 			raw++;
7870Sstevel@tonic-gate 			i = getch0();
7880Sstevel@tonic-gate 			if (!fi)
7890Sstevel@tonic-gate 				flss = i;
7900Sstevel@tonic-gate 			copyf--;
7910Sstevel@tonic-gate 			raw--;
7920Sstevel@tonic-gate 			goto g0;
7930Sstevel@tonic-gate 		}
7940Sstevel@tonic-gate 		if (k == RPT) {
7950Sstevel@tonic-gate 			setrpt();
7960Sstevel@tonic-gate 			goto g0;
7970Sstevel@tonic-gate 		}
7980Sstevel@tonic-gate 		if (!copyf) {
7990Sstevel@tonic-gate 			if (k == 'f' && lg && !lgf) {
8000Sstevel@tonic-gate 				i = getlg(i);
8010Sstevel@tonic-gate 				return(i);
8020Sstevel@tonic-gate 			}
8030Sstevel@tonic-gate 			if (k == fc || k == tabch || k == ldrch) {
8040Sstevel@tonic-gate 				if ((i = setfield(k)) == 0)
8050Sstevel@tonic-gate 					goto g0;
8060Sstevel@tonic-gate 				else
8070Sstevel@tonic-gate 					return(i);
8080Sstevel@tonic-gate 			}
8090Sstevel@tonic-gate 			if (k == '\b') {
8100Sstevel@tonic-gate 				i = makem(-width(' ' | chbits));
8110Sstevel@tonic-gate 				return(i);
8120Sstevel@tonic-gate 			}
8130Sstevel@tonic-gate 		}
8140Sstevel@tonic-gate 		return(i);
8150Sstevel@tonic-gate 	}
8160Sstevel@tonic-gate 	k = cbits(j = getch0());
8170Sstevel@tonic-gate 	if (ismot(j))
8180Sstevel@tonic-gate 		return(j);
8190Sstevel@tonic-gate 	switch (k) {
8200Sstevel@tonic-gate 
8210Sstevel@tonic-gate 	case 'X':	/* \X'...' for copy through */
8220Sstevel@tonic-gate 		setxon();
8230Sstevel@tonic-gate 		goto g0;
8240Sstevel@tonic-gate 	case '\n':	/* concealed newline */
8250Sstevel@tonic-gate 		goto g0;
8260Sstevel@tonic-gate 	case 'n':	/* number register */
8270Sstevel@tonic-gate 		setn();
8280Sstevel@tonic-gate 		goto g0;
8290Sstevel@tonic-gate 	case '*':	/* string indicator */
8300Sstevel@tonic-gate 		setstr();
8310Sstevel@tonic-gate 		goto g0;
8320Sstevel@tonic-gate 	case '$':	/* argument indicator */
8330Sstevel@tonic-gate 		seta();
8340Sstevel@tonic-gate 		goto g0;
8350Sstevel@tonic-gate 	case '{':	/* LEFT */
8360Sstevel@tonic-gate 		i = LEFT;
8370Sstevel@tonic-gate 		goto gx;
8380Sstevel@tonic-gate 	case '}':	/* RIGHT */
8390Sstevel@tonic-gate 		i = RIGHT;
8400Sstevel@tonic-gate 		goto gx;
8410Sstevel@tonic-gate 	case '"':	/* comment */
8420Sstevel@tonic-gate 		while (cbits(i = getch0()) != '\n')
8430Sstevel@tonic-gate 			;
8440Sstevel@tonic-gate 		nlflg++;
8450Sstevel@tonic-gate 		if (ip == 0)
8460Sstevel@tonic-gate 			numtab[CD].val++;
8470Sstevel@tonic-gate 		return(i);
8480Sstevel@tonic-gate 	case ESC:	/* double backslash */
8490Sstevel@tonic-gate 		i = eschar;
8500Sstevel@tonic-gate 		goto gx;
8510Sstevel@tonic-gate 	case 'e':	/* printable version of current eschar */
8520Sstevel@tonic-gate 		i = PRESC;
8530Sstevel@tonic-gate 		goto gx;
8540Sstevel@tonic-gate 	case ' ':	/* unpaddable space */
8550Sstevel@tonic-gate 		i = UNPAD;
8560Sstevel@tonic-gate 		goto gx;
8570Sstevel@tonic-gate 	case '\'':	/* \(aa */
8580Sstevel@tonic-gate 		i = ACUTE;
8590Sstevel@tonic-gate 		goto gx;
8600Sstevel@tonic-gate 	case '`':	/* \(ga */
8610Sstevel@tonic-gate 		i = GRAVE;
8620Sstevel@tonic-gate 		goto gx;
8630Sstevel@tonic-gate 	case '_':	/* \(ul */
8640Sstevel@tonic-gate 		i = UNDERLINE;
8650Sstevel@tonic-gate 		goto gx;
8660Sstevel@tonic-gate 	case '-':	/* current font minus */
8670Sstevel@tonic-gate 		i = MINUS;
8680Sstevel@tonic-gate 		goto gx;
8690Sstevel@tonic-gate 	case '&':	/* filler */
8700Sstevel@tonic-gate 		i = FILLER;
8710Sstevel@tonic-gate 		goto gx;
8720Sstevel@tonic-gate 	case 'c':	/* to be continued */
8730Sstevel@tonic-gate 		i = CONT;
8740Sstevel@tonic-gate 		goto gx;
8750Sstevel@tonic-gate 	case '!':	/* transparent indicator */
8760Sstevel@tonic-gate 		i = XPAR;
8770Sstevel@tonic-gate 		goto gx;
8780Sstevel@tonic-gate 	case 't':	/* tab */
8790Sstevel@tonic-gate 		i = '\t';
8800Sstevel@tonic-gate 		return(i);
8810Sstevel@tonic-gate 	case 'a':	/* leader (SOH) */
8820Sstevel@tonic-gate 		i = LEADER;
8830Sstevel@tonic-gate 		return(i);
8840Sstevel@tonic-gate 	case '%':	/* ohc */
8850Sstevel@tonic-gate 		i = OHC;
8860Sstevel@tonic-gate 		return(i);
8870Sstevel@tonic-gate 	case 'g':	/* return format of a number register */
8880Sstevel@tonic-gate 		setaf();
8890Sstevel@tonic-gate 		goto g0;
8900Sstevel@tonic-gate 	case 'N':	/* absolute character number */
8910Sstevel@tonic-gate 		i = setabs();
8920Sstevel@tonic-gate 		goto gx;
8930Sstevel@tonic-gate 	case '.':	/* . */
8940Sstevel@tonic-gate 		i = '.';
8950Sstevel@tonic-gate gx:
8960Sstevel@tonic-gate 		setsfbits(i, sfbits(j));
8970Sstevel@tonic-gate 		return(i);
8980Sstevel@tonic-gate 	}
8990Sstevel@tonic-gate 	if (copyf) {
9000Sstevel@tonic-gate 		*pbp++ = j;
9010Sstevel@tonic-gate 		return(eschar);
9020Sstevel@tonic-gate 	}
9030Sstevel@tonic-gate 	switch (k) {
9040Sstevel@tonic-gate 
9050Sstevel@tonic-gate 	case 'p':	/* spread */
9060Sstevel@tonic-gate 		spread++;
9070Sstevel@tonic-gate 		goto g0;
9080Sstevel@tonic-gate 	case '(':	/* special char name */
9090Sstevel@tonic-gate 		if ((i = setch()) == 0)
9100Sstevel@tonic-gate 			goto g0;
9110Sstevel@tonic-gate 		return(i);
9120Sstevel@tonic-gate 	case 's':	/* size indicator */
9130Sstevel@tonic-gate 		setps();
9140Sstevel@tonic-gate 		goto g0;
9150Sstevel@tonic-gate 	case 'H':	/* character height */
9160Sstevel@tonic-gate 		return(setht());
9170Sstevel@tonic-gate 	case 'S':	/* slant */
9180Sstevel@tonic-gate 		return(setslant());
9190Sstevel@tonic-gate 	case 'f':	/* font indicator */
9200Sstevel@tonic-gate 		setfont(0);
9210Sstevel@tonic-gate 		goto g0;
9220Sstevel@tonic-gate 	case 'w':	/* width function */
9230Sstevel@tonic-gate 		setwd();
9240Sstevel@tonic-gate 		goto g0;
9250Sstevel@tonic-gate 	case 'v':	/* vert mot */
9260Sstevel@tonic-gate 		if (i = vmot())
9270Sstevel@tonic-gate 			return(i);
9280Sstevel@tonic-gate 		goto g0;
9290Sstevel@tonic-gate 	case 'h': 	/* horiz mot */
9300Sstevel@tonic-gate 		if (i = hmot())
9310Sstevel@tonic-gate 			return(i);
9320Sstevel@tonic-gate 		goto g0;
9330Sstevel@tonic-gate 	case 'z':	/* zero with char */
9340Sstevel@tonic-gate 		return(setz());
9350Sstevel@tonic-gate 	case 'l':	/* hor line */
9360Sstevel@tonic-gate 		setline();
9370Sstevel@tonic-gate 		goto g0;
9380Sstevel@tonic-gate 	case 'L':	/* vert line */
9390Sstevel@tonic-gate 		setvline();
9400Sstevel@tonic-gate 		goto g0;
9410Sstevel@tonic-gate 	case 'D':	/* drawing function */
9420Sstevel@tonic-gate 		setdraw();
9430Sstevel@tonic-gate 		goto g0;
9440Sstevel@tonic-gate 	case 'b':	/* bracket */
9450Sstevel@tonic-gate 		setbra();
9460Sstevel@tonic-gate 		goto g0;
9470Sstevel@tonic-gate 	case 'o':	/* overstrike */
9480Sstevel@tonic-gate 		setov();
9490Sstevel@tonic-gate 		goto g0;
9500Sstevel@tonic-gate 	case 'k':	/* mark hor place */
9510Sstevel@tonic-gate 		if ((k = findr(getsn())) != -1) {
9520Sstevel@tonic-gate 			numtab[k].val = numtab[HP].val;
9530Sstevel@tonic-gate 		}
9540Sstevel@tonic-gate 		goto g0;
9550Sstevel@tonic-gate 	case '0':	/* number space */
9560Sstevel@tonic-gate 		return(makem(width('0' | chbits)));
9570Sstevel@tonic-gate #ifdef NROFF
9580Sstevel@tonic-gate 	case '|':
9590Sstevel@tonic-gate 	case '^':
9600Sstevel@tonic-gate 		goto g0;
9610Sstevel@tonic-gate #else
9620Sstevel@tonic-gate 	case '|':	/* narrow space */
9630Sstevel@tonic-gate 		return(makem((int)(EM)/6));
9640Sstevel@tonic-gate 	case '^':	/* half narrow space */
9650Sstevel@tonic-gate 		return(makem((int)(EM)/12));
9660Sstevel@tonic-gate #endif
9670Sstevel@tonic-gate 	case 'x':	/* extra line space */
9680Sstevel@tonic-gate 		if (i = xlss())
9690Sstevel@tonic-gate 			return(i);
9700Sstevel@tonic-gate 		goto g0;
9710Sstevel@tonic-gate 	case 'u':	/* half em up */
9720Sstevel@tonic-gate 	case 'r':	/* full em up */
9730Sstevel@tonic-gate 	case 'd':	/* half em down */
9740Sstevel@tonic-gate 		return(sethl(k));
9750Sstevel@tonic-gate 	default:
9760Sstevel@tonic-gate 		return(j);
9770Sstevel@tonic-gate 	}
9780Sstevel@tonic-gate 	/* NOTREACHED */
9790Sstevel@tonic-gate }
9800Sstevel@tonic-gate 
981*217Smuffin int
setxon()9820Sstevel@tonic-gate setxon()	/* \X'...' for copy through */
9830Sstevel@tonic-gate {
9840Sstevel@tonic-gate 	tchar xbuf[NC];
985*217Smuffin 	tchar *i;
9860Sstevel@tonic-gate 	tchar c;
9870Sstevel@tonic-gate 	int delim, k;
9880Sstevel@tonic-gate 
9890Sstevel@tonic-gate 	if (ismot(c = getch()))
990*217Smuffin 		return (0);
9910Sstevel@tonic-gate 	delim = cbits(c);
9920Sstevel@tonic-gate 	i = xbuf;
9930Sstevel@tonic-gate 	*i++ = XON;
9940Sstevel@tonic-gate 	while ((k = cbits(c = getch())) != delim && k != '\n' && i < xbuf+NC-1) {
9950Sstevel@tonic-gate 		if (k == ' ')
9960Sstevel@tonic-gate 			setcbits(c, UNPAD);
9970Sstevel@tonic-gate 		*i++ = c | ZBIT;
9980Sstevel@tonic-gate 	}
9990Sstevel@tonic-gate 	*i++ = XOFF;
10000Sstevel@tonic-gate 	*i = 0;
10010Sstevel@tonic-gate 	pushback(xbuf);
1002*217Smuffin 
1003*217Smuffin 	return (0);
10040Sstevel@tonic-gate }
10050Sstevel@tonic-gate 
10060Sstevel@tonic-gate 
10070Sstevel@tonic-gate char	ifilt[32] = {
10080Sstevel@tonic-gate 	0, 001, 002, 003, 0, 005, 006, 007, 010, 011, 012};
10090Sstevel@tonic-gate 
getch0()10100Sstevel@tonic-gate tchar getch0()
10110Sstevel@tonic-gate {
1012*217Smuffin 	int	j;
1013*217Smuffin 	tchar i;
10140Sstevel@tonic-gate #ifdef	EUC
10150Sstevel@tonic-gate #ifdef	NROFF
1016*217Smuffin 	int	n;
10170Sstevel@tonic-gate 	int col_index;
10180Sstevel@tonic-gate #endif	/* NROFF */
10190Sstevel@tonic-gate #endif	/* EUC */
10200Sstevel@tonic-gate 
10210Sstevel@tonic-gate again:
10220Sstevel@tonic-gate 	if (pbp > lastpbp)
10230Sstevel@tonic-gate 		i = *--pbp;
10240Sstevel@tonic-gate 	else if (ip) {
10250Sstevel@tonic-gate #ifdef INCORE
10260Sstevel@tonic-gate 		extern tchar corebuf[];
10270Sstevel@tonic-gate 		i = corebuf[ip];
10280Sstevel@tonic-gate 		if (i == 0)
10290Sstevel@tonic-gate 			i = rbf();
10300Sstevel@tonic-gate 		else {
10310Sstevel@tonic-gate 			if ((++ip & (BLK - 1)) == 0) {
10320Sstevel@tonic-gate 				--ip;
10330Sstevel@tonic-gate 				(void)rbf();
10340Sstevel@tonic-gate 			}
10350Sstevel@tonic-gate 		}
10360Sstevel@tonic-gate #else
10370Sstevel@tonic-gate 		i = rbf();
10380Sstevel@tonic-gate #endif
10390Sstevel@tonic-gate 	} else {
10400Sstevel@tonic-gate 		if (donef || ndone)
10410Sstevel@tonic-gate 			done(0);
10420Sstevel@tonic-gate 		if (nx || ibufp >= eibuf) {
10430Sstevel@tonic-gate 			if (nfo==0) {
10440Sstevel@tonic-gate g0:
10450Sstevel@tonic-gate 				if (nextfile()) {
10460Sstevel@tonic-gate 					if (ip)
10470Sstevel@tonic-gate 						goto again;
10480Sstevel@tonic-gate 					if (ibufp < eibuf)
10490Sstevel@tonic-gate 						goto g2;
10500Sstevel@tonic-gate 				}
10510Sstevel@tonic-gate 			}
10520Sstevel@tonic-gate 			nx = 0;
10530Sstevel@tonic-gate 			if ((j = read(ifile, ibuf, IBUFSZ)) <= 0)
10540Sstevel@tonic-gate 				goto g0;
10550Sstevel@tonic-gate 			ibufp = ibuf;
10560Sstevel@tonic-gate 			eibuf = ibuf + j;
10570Sstevel@tonic-gate 			if (ip)
10580Sstevel@tonic-gate 				goto again;
10590Sstevel@tonic-gate 		}
10600Sstevel@tonic-gate g2:
10610Sstevel@tonic-gate #ifndef	EUC
10620Sstevel@tonic-gate 		i = *ibufp++ & 0177;
10630Sstevel@tonic-gate 		ioff++;
10640Sstevel@tonic-gate 		if (i >= 040 && i < 0177)
10650Sstevel@tonic-gate #else
10660Sstevel@tonic-gate #ifndef	NROFF
10670Sstevel@tonic-gate 		i = *ibufp++ & 0177;
10680Sstevel@tonic-gate 		ioff++;
10690Sstevel@tonic-gate 		if (i >= 040 && i < 0177)
10700Sstevel@tonic-gate #else
10710Sstevel@tonic-gate 		i = *ibufp++ & 0377;
10720Sstevel@tonic-gate 		*mbbuf1p++ = i;
10730Sstevel@tonic-gate 		*mbbuf1p = 0;
10740Sstevel@tonic-gate 		if (!multi_locale) {
10750Sstevel@tonic-gate 			twc = 0;
10760Sstevel@tonic-gate 			mbbuf1p = mbbuf1;
10770Sstevel@tonic-gate 		} else if ((n = mbtowc(&twc, mbbuf1, MB_CUR_MAX)) <= 0) {
10780Sstevel@tonic-gate 			if (mbbuf1p >= mbbuf1 + MB_CUR_MAX) {
10790Sstevel@tonic-gate 				i &= ~(MBMASK | CSMASK);
10800Sstevel@tonic-gate 				twc = 0;
10810Sstevel@tonic-gate 				mbbuf1p = mbbuf1;
10820Sstevel@tonic-gate 				*mbbuf1p = 0;
10830Sstevel@tonic-gate 			} else {
10840Sstevel@tonic-gate 				i |= (MIDDLEOFMB);
10850Sstevel@tonic-gate 			}
10860Sstevel@tonic-gate 		} else {
10870Sstevel@tonic-gate 			if (n > 1)
10880Sstevel@tonic-gate 				i |= (LASTOFMB);
10890Sstevel@tonic-gate 			else
10900Sstevel@tonic-gate 				i |= (BYTE_CHR);
10910Sstevel@tonic-gate 			if (isascii(twc)) {
10920Sstevel@tonic-gate 				col_index = 0;
10930Sstevel@tonic-gate 			} else {
10940Sstevel@tonic-gate 				if ((col_index = wcwidth(twc)) < 0)
10950Sstevel@tonic-gate 					col_index = 0;
10960Sstevel@tonic-gate 			}
10970Sstevel@tonic-gate 			setcsbits(i, col_index);
10980Sstevel@tonic-gate 			twc = 0;
10990Sstevel@tonic-gate 			mbbuf1p = mbbuf1;
11000Sstevel@tonic-gate 		}
11010Sstevel@tonic-gate 		ioff++;
11020Sstevel@tonic-gate 		if (i >= 040 && i != 0177)
11030Sstevel@tonic-gate #endif	/* NROFF */
11040Sstevel@tonic-gate #endif	/* EUC */
11050Sstevel@tonic-gate 			goto g4;
11060Sstevel@tonic-gate 		if (i != 0177)
11070Sstevel@tonic-gate 			i = ifilt[i];
11080Sstevel@tonic-gate 	}
11090Sstevel@tonic-gate 	if (cbits(i) == IMP && !raw)
11100Sstevel@tonic-gate 		goto again;
11110Sstevel@tonic-gate 	if ((i == 0 || i == 0177) && !init && !raw) {
11120Sstevel@tonic-gate 		goto again;
11130Sstevel@tonic-gate 	}
11140Sstevel@tonic-gate g4:
11150Sstevel@tonic-gate #ifndef EUC
11160Sstevel@tonic-gate 	if (copyf == 0 && (i & ~BYTEMASK) == 0)
11170Sstevel@tonic-gate #else
11180Sstevel@tonic-gate #ifndef NROFF
11190Sstevel@tonic-gate 	if (copyf == 0 && (i & ~BYTEMASK) == 0)
11200Sstevel@tonic-gate #else
11210Sstevel@tonic-gate 	if (copyf == 0 && (i & ~CHMASK) == 0)
11220Sstevel@tonic-gate #endif /* NROFF */
11230Sstevel@tonic-gate #endif /* EUC */
11240Sstevel@tonic-gate 		i |= chbits;
11250Sstevel@tonic-gate #ifdef EUC
11260Sstevel@tonic-gate #ifdef NROFF
11270Sstevel@tonic-gate 	if (multi_locale)
11280Sstevel@tonic-gate 		if (i & MBMASK1)
11290Sstevel@tonic-gate 			i |= ZBIT;
11300Sstevel@tonic-gate #endif /* NROFF */
11310Sstevel@tonic-gate #endif /* EUC */
11320Sstevel@tonic-gate 	if (cbits(i) == eschar && !raw)
11330Sstevel@tonic-gate 		setcbits(i, ESC);
11340Sstevel@tonic-gate 	return(i);
11350Sstevel@tonic-gate }
11360Sstevel@tonic-gate 
1137*217Smuffin int
pushback(b)11380Sstevel@tonic-gate pushback(b)
1139*217Smuffin tchar *b;
11400Sstevel@tonic-gate {
1141*217Smuffin 	tchar *ob = b;
11420Sstevel@tonic-gate 
11430Sstevel@tonic-gate 	while (*b++)
11440Sstevel@tonic-gate 		;
11450Sstevel@tonic-gate 	b--;
11460Sstevel@tonic-gate 	while (b > ob && pbp < &pbbuf[NC-3])
11470Sstevel@tonic-gate 		*pbp++ = *--b;
11480Sstevel@tonic-gate 	if (pbp >= &pbbuf[NC-3]) {
11490Sstevel@tonic-gate 		errprint(gettext("pushback overflow"));
11500Sstevel@tonic-gate 		done(2);
11510Sstevel@tonic-gate 	}
1152*217Smuffin 
1153*217Smuffin 	return (0);
11540Sstevel@tonic-gate }
11550Sstevel@tonic-gate 
1156*217Smuffin int
cpushback(b)11570Sstevel@tonic-gate cpushback(b)
1158*217Smuffin char *b;
11590Sstevel@tonic-gate {
1160*217Smuffin 	char *ob = b;
11610Sstevel@tonic-gate 
11620Sstevel@tonic-gate 	while (*b++)
11630Sstevel@tonic-gate 		;
11640Sstevel@tonic-gate 	b--;
11650Sstevel@tonic-gate 	while (b > ob && pbp < &pbbuf[NC-3])
11660Sstevel@tonic-gate 		*pbp++ = *--b;
11670Sstevel@tonic-gate 	if (pbp >= &pbbuf[NC-3]) {
11680Sstevel@tonic-gate 		errprint(gettext("cpushback overflow"));
11690Sstevel@tonic-gate 		done(2);
11700Sstevel@tonic-gate 	}
1171*217Smuffin 
1172*217Smuffin 	return (0);
11730Sstevel@tonic-gate }
11740Sstevel@tonic-gate 
1175*217Smuffin int
nextfile()11760Sstevel@tonic-gate nextfile()
11770Sstevel@tonic-gate {
1178*217Smuffin 	char	*p;
11790Sstevel@tonic-gate 
11800Sstevel@tonic-gate n0:
11810Sstevel@tonic-gate 	if (ifile)
11820Sstevel@tonic-gate 		close(ifile);
11830Sstevel@tonic-gate 	if (nx  ||  nmfi < mflg) {
11840Sstevel@tonic-gate 		p = mfiles[nmfi++];
11850Sstevel@tonic-gate 		if (*p != 0)
11860Sstevel@tonic-gate 			goto n1;
11870Sstevel@tonic-gate 	}
11880Sstevel@tonic-gate 	if (ifi > 0) {
11890Sstevel@tonic-gate 		if (popf())
11900Sstevel@tonic-gate 			goto n0; /* popf error */
11910Sstevel@tonic-gate 		return(1); /* popf ok */
11920Sstevel@tonic-gate 	}
11930Sstevel@tonic-gate 	if (rargc-- <= 0) {
11940Sstevel@tonic-gate 		if ((nfo -= mflg) && !stdi)
11950Sstevel@tonic-gate 			done(0);
11960Sstevel@tonic-gate 		nfo++;
11970Sstevel@tonic-gate 		numtab[CD].val = ifile = stdi = mflg = 0;
11980Sstevel@tonic-gate 		strcpy(cfname[ifi], "<standard input>");
11990Sstevel@tonic-gate 		ioff = 0;
12000Sstevel@tonic-gate 		return(0);
12010Sstevel@tonic-gate 	}
12020Sstevel@tonic-gate 	p = (argp++)[0];
12030Sstevel@tonic-gate n1:
12040Sstevel@tonic-gate 	numtab[CD].val = 0;
12050Sstevel@tonic-gate 	if (p[0] == '-' && p[1] == 0) {
12060Sstevel@tonic-gate 		ifile = 0;
12070Sstevel@tonic-gate 		strcpy(cfname[ifi], "<standard input>");
12080Sstevel@tonic-gate 	} else if ((ifile = open(p, 0)) < 0) {
12090Sstevel@tonic-gate 		errprint(gettext("cannot open file %s"), p);
12100Sstevel@tonic-gate 		nfo -= mflg;
12110Sstevel@tonic-gate 		done(02);
12120Sstevel@tonic-gate 	} else
12130Sstevel@tonic-gate 		strcpy(cfname[ifi],p);
12140Sstevel@tonic-gate 	nfo++;
12150Sstevel@tonic-gate 	ioff = 0;
12160Sstevel@tonic-gate 	return(0);
12170Sstevel@tonic-gate }
12180Sstevel@tonic-gate 
12190Sstevel@tonic-gate 
1220*217Smuffin int
popf()12210Sstevel@tonic-gate popf()
12220Sstevel@tonic-gate {
1223*217Smuffin 	int	i;
1224*217Smuffin 	char	*p, *q;
12250Sstevel@tonic-gate 	extern char	*ttyname();
12260Sstevel@tonic-gate 
12270Sstevel@tonic-gate 	ioff = offl[--ifi];
12280Sstevel@tonic-gate 	numtab[CD].val = cfline[ifi];		/*restore line counter*/
12290Sstevel@tonic-gate 	ip = ipl[ifi];
12300Sstevel@tonic-gate 	if ((ifile = ifl[ifi]) == 0) {
12310Sstevel@tonic-gate 		p = xbuf;
12320Sstevel@tonic-gate 		q = ibuf;
12330Sstevel@tonic-gate 		ibufp = xbufp;
12340Sstevel@tonic-gate 		eibuf = xeibuf;
12350Sstevel@tonic-gate 		while (q < eibuf)
12360Sstevel@tonic-gate 			*q++ = *p++;
12370Sstevel@tonic-gate 		return(0);
12380Sstevel@tonic-gate 	}
12390Sstevel@tonic-gate 	if (lseek(ifile, (long)(ioff & ~(IBUFSZ-1)), 0) == (long) -1
12400Sstevel@tonic-gate 	   || (i = read(ifile, ibuf, IBUFSZ)) < 0)
12410Sstevel@tonic-gate 		return(1);
12420Sstevel@tonic-gate 	eibuf = ibuf + i;
12430Sstevel@tonic-gate 	ibufp = ibuf;
12440Sstevel@tonic-gate 	if (ttyname(ifile) == 0)
12450Sstevel@tonic-gate 		/* was >= ... */
12460Sstevel@tonic-gate 		if ((ibufp = ibuf + (int)(ioff & (IBUFSZ - 1))) > eibuf)
12470Sstevel@tonic-gate 			return(1);
12480Sstevel@tonic-gate 	return(0);
12490Sstevel@tonic-gate }
12500Sstevel@tonic-gate 
12510Sstevel@tonic-gate 
1252*217Smuffin int
flushi()12530Sstevel@tonic-gate flushi()
12540Sstevel@tonic-gate {
12550Sstevel@tonic-gate 	if (nflush)
1256*217Smuffin 		return (0);
12570Sstevel@tonic-gate 	ch = 0;
12580Sstevel@tonic-gate 	copyf++;
12590Sstevel@tonic-gate 	while (!nlflg) {
12600Sstevel@tonic-gate 		if (donef && (frame == stk))
12610Sstevel@tonic-gate 			break;
12620Sstevel@tonic-gate 		getch();
12630Sstevel@tonic-gate 	}
12640Sstevel@tonic-gate 	copyf--;
1265*217Smuffin 
1266*217Smuffin 	return (0);
12670Sstevel@tonic-gate }
12680Sstevel@tonic-gate 
12690Sstevel@tonic-gate 
1270*217Smuffin int
getach()12710Sstevel@tonic-gate getach()
12720Sstevel@tonic-gate {
1273*217Smuffin 	tchar i;
1274*217Smuffin 	int	j;
12750Sstevel@tonic-gate 
12760Sstevel@tonic-gate 	lgf++;
12770Sstevel@tonic-gate 	j = cbits(i = getch());
12780Sstevel@tonic-gate #ifndef	EUC
12790Sstevel@tonic-gate 	if (ismot(i) || j == ' ' || j == '\n' || j & 0200) {
12800Sstevel@tonic-gate #else
12810Sstevel@tonic-gate #ifndef	NROFF
12820Sstevel@tonic-gate 	if (ismot(i) || j == ' ' || j == '\n' || j & 0200) {
12830Sstevel@tonic-gate #else
12840Sstevel@tonic-gate 	if (ismot(i) || j == ' ' || j == '\n' || j > 0200) {
12850Sstevel@tonic-gate #endif	/* NROFF */
12860Sstevel@tonic-gate #endif	/* EUC */
12870Sstevel@tonic-gate 
12880Sstevel@tonic-gate 		ch = i;
12890Sstevel@tonic-gate 		j = 0;
12900Sstevel@tonic-gate 	}
12910Sstevel@tonic-gate 	lgf--;
12920Sstevel@tonic-gate 	return(j & 0177);
12930Sstevel@tonic-gate }
12940Sstevel@tonic-gate 
12950Sstevel@tonic-gate 
1296*217Smuffin int
12970Sstevel@tonic-gate casenx()
12980Sstevel@tonic-gate {
12990Sstevel@tonic-gate 	lgf++;
13000Sstevel@tonic-gate 	skip();
13010Sstevel@tonic-gate 	getname();
13020Sstevel@tonic-gate 	nx++;
13030Sstevel@tonic-gate 	if (nmfi > 0)
13040Sstevel@tonic-gate 		nmfi--;
13050Sstevel@tonic-gate 	strcpy(mfiles[nmfi], nextf);
13060Sstevel@tonic-gate 	nextfile();
13070Sstevel@tonic-gate 	nlflg++;
13080Sstevel@tonic-gate 	ip = 0;
13090Sstevel@tonic-gate 	pendt = 0;
13100Sstevel@tonic-gate 	frame = stk;
13110Sstevel@tonic-gate 	nxf = frame + 1;
1312*217Smuffin 
1313*217Smuffin 	return (0);
13140Sstevel@tonic-gate }
13150Sstevel@tonic-gate 
13160Sstevel@tonic-gate 
1317*217Smuffin int
13180Sstevel@tonic-gate getname()
13190Sstevel@tonic-gate {
1320*217Smuffin 	int	j, k;
13210Sstevel@tonic-gate 	tchar i;
13220Sstevel@tonic-gate 
13230Sstevel@tonic-gate 	lgf++;
13240Sstevel@tonic-gate 	for (k = 0; k < (NS - 1); k++) {
13250Sstevel@tonic-gate #ifndef EUC
13260Sstevel@tonic-gate 		if (((j = cbits(i = getch())) <= ' ') || (j > 0176))
13270Sstevel@tonic-gate #else
13280Sstevel@tonic-gate #ifndef NROFF
13290Sstevel@tonic-gate 		if (((j = cbits(i = getch())) <= ' ') || (j > 0176))
13300Sstevel@tonic-gate #else
13310Sstevel@tonic-gate 		if (((j = cbits(i = getch())) <= ' ') || (j == 0177))
13320Sstevel@tonic-gate #endif /* NROFF */
13330Sstevel@tonic-gate #endif /* EUC */
13340Sstevel@tonic-gate 			break;
13350Sstevel@tonic-gate 		nextf[k] = j & BYTEMASK;
13360Sstevel@tonic-gate 	}
13370Sstevel@tonic-gate 	nextf[k] = 0;
13380Sstevel@tonic-gate 	ch = i;
13390Sstevel@tonic-gate 	lgf--;
1340*217Smuffin 	return((int)nextf[0]);
13410Sstevel@tonic-gate }
13420Sstevel@tonic-gate 
13430Sstevel@tonic-gate 
1344*217Smuffin int
13450Sstevel@tonic-gate caseso()
13460Sstevel@tonic-gate {
1347*217Smuffin 	int	i;
1348*217Smuffin 	char	*p, *q;
13490Sstevel@tonic-gate 
13500Sstevel@tonic-gate 	lgf++;
13510Sstevel@tonic-gate 	nextf[0] = 0;
13520Sstevel@tonic-gate 	if (skip() || !getname() || ((i = open(nextf, 0)) < 0) || (ifi >= NSO)) {
13530Sstevel@tonic-gate 		errprint(gettext("can't open file %s"), nextf);
13540Sstevel@tonic-gate 		done(02);
13550Sstevel@tonic-gate 	}
13560Sstevel@tonic-gate 	strcpy(cfname[ifi+1], nextf);
13570Sstevel@tonic-gate 	cfline[ifi] = numtab[CD].val;		/*hold line counter*/
13580Sstevel@tonic-gate 	numtab[CD].val = 0;
13590Sstevel@tonic-gate 	flushi();
13600Sstevel@tonic-gate 	ifl[ifi] = ifile;
13610Sstevel@tonic-gate 	ifile = i;
13620Sstevel@tonic-gate 	offl[ifi] = ioff;
13630Sstevel@tonic-gate 	ioff = 0;
13640Sstevel@tonic-gate 	ipl[ifi] = ip;
13650Sstevel@tonic-gate 	ip = 0;
13660Sstevel@tonic-gate 	nx++;
13670Sstevel@tonic-gate 	nflush++;
13680Sstevel@tonic-gate 	if (!ifl[ifi++]) {
13690Sstevel@tonic-gate 		p = ibuf;
13700Sstevel@tonic-gate 		q = xbuf;
13710Sstevel@tonic-gate 		xbufp = ibufp;
13720Sstevel@tonic-gate 		xeibuf = eibuf;
13730Sstevel@tonic-gate 		while (p < eibuf)
13740Sstevel@tonic-gate 			*q++ = *p++;
13750Sstevel@tonic-gate 	}
1376*217Smuffin 
1377*217Smuffin 	return (0);
13780Sstevel@tonic-gate }
13790Sstevel@tonic-gate 
1380*217Smuffin int
13810Sstevel@tonic-gate caself()	/* set line number and file */
13820Sstevel@tonic-gate {
13830Sstevel@tonic-gate 	int n;
13840Sstevel@tonic-gate 
13850Sstevel@tonic-gate 	if (skip())
1386*217Smuffin 		return (0);
13870Sstevel@tonic-gate 	n = atoi();
13880Sstevel@tonic-gate 	cfline[ifi] = numtab[CD].val = n - 2;
13890Sstevel@tonic-gate 	if (skip())
1390*217Smuffin 		return (0);
13910Sstevel@tonic-gate 	if (getname())
13920Sstevel@tonic-gate 		strcpy(cfname[ifi], nextf);
1393*217Smuffin 
1394*217Smuffin 	return (0);
13950Sstevel@tonic-gate }
13960Sstevel@tonic-gate 
13970Sstevel@tonic-gate 
1398*217Smuffin int
13990Sstevel@tonic-gate casecf()
14000Sstevel@tonic-gate {	/* copy file without change */
14010Sstevel@tonic-gate #ifndef NROFF
14020Sstevel@tonic-gate 	int	fd, n;
14030Sstevel@tonic-gate 	char	buf[512];
14040Sstevel@tonic-gate 	extern int hpos, esc, po;
14050Sstevel@tonic-gate 	nextf[0] = 0;
14060Sstevel@tonic-gate 	if (skip() || !getname() || (fd = open(nextf, 0)) < 0) {
14070Sstevel@tonic-gate 		errprint(gettext("can't open file %s"), nextf);
14080Sstevel@tonic-gate 		done(02);
14090Sstevel@tonic-gate 	}
14100Sstevel@tonic-gate 	tbreak();
14110Sstevel@tonic-gate 	/* make it into a clean state, be sure that everything is out */
14120Sstevel@tonic-gate 	hpos = po;
14130Sstevel@tonic-gate 	esc = un;
14140Sstevel@tonic-gate 	ptesc();
14150Sstevel@tonic-gate 	ptlead();
14160Sstevel@tonic-gate 	ptps();
14170Sstevel@tonic-gate 	ptfont();
14180Sstevel@tonic-gate 	flusho();
14190Sstevel@tonic-gate 	while ((n = read(fd, buf, sizeof buf)) > 0)
14200Sstevel@tonic-gate 		write(ptid, buf, n);
14210Sstevel@tonic-gate 	close(fd);
14220Sstevel@tonic-gate #endif
1423*217Smuffin 	return (0);
14240Sstevel@tonic-gate }
14250Sstevel@tonic-gate 
14260Sstevel@tonic-gate 
1427*217Smuffin int
14280Sstevel@tonic-gate casesy()	/* call system */
14290Sstevel@tonic-gate {
14300Sstevel@tonic-gate 	char	sybuf[NTM];
14310Sstevel@tonic-gate 	int	i;
14320Sstevel@tonic-gate 
14330Sstevel@tonic-gate 	lgf++;
14340Sstevel@tonic-gate 	copyf++;
14350Sstevel@tonic-gate 	skip();
14360Sstevel@tonic-gate 	for (i = 0; i < NTM - 2; i++)
14370Sstevel@tonic-gate 		if ((sybuf[i] = getch()) == '\n')
14380Sstevel@tonic-gate 			break;
14390Sstevel@tonic-gate 	sybuf[i] = 0;
14400Sstevel@tonic-gate 	system(sybuf);
14410Sstevel@tonic-gate 	copyf--;
14420Sstevel@tonic-gate 	lgf--;
1443*217Smuffin 
1444*217Smuffin 	return (0);
14450Sstevel@tonic-gate }
14460Sstevel@tonic-gate 
14470Sstevel@tonic-gate 
1448*217Smuffin int
14490Sstevel@tonic-gate getpn(a)
1450*217Smuffin 	char *a;
14510Sstevel@tonic-gate {
1452*217Smuffin 	int n, neg;
14530Sstevel@tonic-gate 
14540Sstevel@tonic-gate 	if (*a == 0)
1455*217Smuffin 		return (0);
14560Sstevel@tonic-gate 	neg = 0;
14570Sstevel@tonic-gate 	for ( ; *a; a++)
14580Sstevel@tonic-gate 		switch (*a) {
14590Sstevel@tonic-gate 		case '+':
14600Sstevel@tonic-gate 		case ',':
14610Sstevel@tonic-gate 			continue;
14620Sstevel@tonic-gate 		case '-':
14630Sstevel@tonic-gate 			neg = 1;
14640Sstevel@tonic-gate 			continue;
14650Sstevel@tonic-gate 		default:
14660Sstevel@tonic-gate 			n = 0;
14670Sstevel@tonic-gate 			if (isdigit((unsigned char)*a)) {
14680Sstevel@tonic-gate 				do
14690Sstevel@tonic-gate 					n = 10 * n + *a++ - '0';
14700Sstevel@tonic-gate 				while (isdigit((unsigned char)*a));
14710Sstevel@tonic-gate 				a--;
14720Sstevel@tonic-gate 			} else
14730Sstevel@tonic-gate 				n = 9999;
14740Sstevel@tonic-gate 			*pnp++ = neg ? -n : n;
14750Sstevel@tonic-gate 			neg = 0;
14760Sstevel@tonic-gate 			if (pnp >= &pnlist[NPN-2]) {
14770Sstevel@tonic-gate 				errprint(gettext("too many page numbers"));
14780Sstevel@tonic-gate 				done3(-3);
14790Sstevel@tonic-gate 			}
14800Sstevel@tonic-gate 		}
14810Sstevel@tonic-gate 	if (neg)
14820Sstevel@tonic-gate 		*pnp++ = -9999;
14830Sstevel@tonic-gate 	*pnp = -32767;
14840Sstevel@tonic-gate 	print = 0;
14850Sstevel@tonic-gate 	pnp = pnlist;
14860Sstevel@tonic-gate 	if (*pnp != -32767)
14870Sstevel@tonic-gate 		chkpn();
1488*217Smuffin 
1489*217Smuffin 	return (0);
14900Sstevel@tonic-gate }
14910Sstevel@tonic-gate 
14920Sstevel@tonic-gate 
1493*217Smuffin int
14940Sstevel@tonic-gate setrpt()
14950Sstevel@tonic-gate {
14960Sstevel@tonic-gate 	tchar i, j;
14970Sstevel@tonic-gate 
14980Sstevel@tonic-gate 	copyf++;
14990Sstevel@tonic-gate 	raw++;
15000Sstevel@tonic-gate 	i = getch0();
15010Sstevel@tonic-gate 	copyf--;
15020Sstevel@tonic-gate 	raw--;
15030Sstevel@tonic-gate 	if (i < 0 || cbits(j = getch0()) == RPT)
1504*217Smuffin 		return (0);
15050Sstevel@tonic-gate 	i &= BYTEMASK;
15060Sstevel@tonic-gate 	while (i>0 && pbp < &pbbuf[NC-3]) {
15070Sstevel@tonic-gate 		i--;
15080Sstevel@tonic-gate 		*pbp++ = j;
15090Sstevel@tonic-gate 	}
1510*217Smuffin 
1511*217Smuffin 	return (0);
15120Sstevel@tonic-gate }
15130Sstevel@tonic-gate 
15140Sstevel@tonic-gate 
1515*217Smuffin int
15160Sstevel@tonic-gate casedb()
15170Sstevel@tonic-gate {
15180Sstevel@tonic-gate #ifdef	DEBUG
15190Sstevel@tonic-gate 	debug = 0;
15200Sstevel@tonic-gate 	if (skip())
1521*217Smuffin 		return (0);
15220Sstevel@tonic-gate 	noscale++;
15230Sstevel@tonic-gate 	debug = max(atoi(), 0);
15240Sstevel@tonic-gate 	noscale = 0;
1525*217Smuffin #endif	/* DEBUG */
1526*217Smuffin 
1527*217Smuffin 	return (0);
15280Sstevel@tonic-gate }
1529