xref: /illumos-gate/usr/src/cmd/troff/n1.c (revision 9fed930b)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
287c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
327c478bd9Sstevel@tonic-gate  * The Regents of the University of California
337c478bd9Sstevel@tonic-gate  * All Rights Reserved
347c478bd9Sstevel@tonic-gate  *
357c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
367c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
377c478bd9Sstevel@tonic-gate  * contributors.
387c478bd9Sstevel@tonic-gate  */
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate /*
417c478bd9Sstevel@tonic-gate  * n1.c
427c478bd9Sstevel@tonic-gate  *
437c478bd9Sstevel@tonic-gate  *	consume options, initialization, main loop,
447c478bd9Sstevel@tonic-gate  *	input routines, escape function calling
457c478bd9Sstevel@tonic-gate  */
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate #include <ctype.h>
487c478bd9Sstevel@tonic-gate #include <signal.h>
497c478bd9Sstevel@tonic-gate #include <sys/types.h>
507c478bd9Sstevel@tonic-gate #include <sys/stat.h>
517c478bd9Sstevel@tonic-gate #include <setjmp.h>
527c478bd9Sstevel@tonic-gate #include <time.h>
537c478bd9Sstevel@tonic-gate #include <stdarg.h>
547c478bd9Sstevel@tonic-gate #include <locale.h>
557c478bd9Sstevel@tonic-gate #include <fcntl.h>
567c478bd9Sstevel@tonic-gate #include <stdlib.h>
577c478bd9Sstevel@tonic-gate #include <string.h>
587c478bd9Sstevel@tonic-gate #ifdef 	EUC
597c478bd9Sstevel@tonic-gate #ifdef	NROFF
607c478bd9Sstevel@tonic-gate #include <stddef.h>
617c478bd9Sstevel@tonic-gate #include <limits.h>
627c478bd9Sstevel@tonic-gate #endif	/* NROFF */
637c478bd9Sstevel@tonic-gate #endif	/* EUC */
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate #include "tdef.h"
667c478bd9Sstevel@tonic-gate #include "ext.h"
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate #ifdef NROFF
697c478bd9Sstevel@tonic-gate #include "tw.h"
707c478bd9Sstevel@tonic-gate #endif
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate #define	MAX_RECURSION_DEPTH	512
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate jmp_buf sjbuf;
757c478bd9Sstevel@tonic-gate extern	void	fdprintf(int, char *, ...);
767c478bd9Sstevel@tonic-gate extern	char	*roff_sprintf(char *, char *, ...);
777c478bd9Sstevel@tonic-gate filep	ipl[NSO];
787c478bd9Sstevel@tonic-gate long	offl[NSO];
797c478bd9Sstevel@tonic-gate long	ioff;
807c478bd9Sstevel@tonic-gate char	*ttyp;
817c478bd9Sstevel@tonic-gate char	cfname[NSO+1][NS];	/*file name stack*/
827c478bd9Sstevel@tonic-gate int	cfline[NSO];		/*input line count stack*/
837c478bd9Sstevel@tonic-gate char	*progname;	/* program name (troff) */
847c478bd9Sstevel@tonic-gate #ifdef	EUC
857c478bd9Sstevel@tonic-gate #ifdef	NROFF
867c478bd9Sstevel@tonic-gate char	mbbuf1[MB_LEN_MAX + 1];
877c478bd9Sstevel@tonic-gate char	*mbbuf1p = mbbuf1;
887c478bd9Sstevel@tonic-gate wchar_t	twc = 0;
897c478bd9Sstevel@tonic-gate #endif	/* NROFF */
907c478bd9Sstevel@tonic-gate #endif	/* EUC */
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate #ifdef	DEBUG
937c478bd9Sstevel@tonic-gate int	debug = 0;	/*debug flag*/
94e5190c10Smuffin #endif	/* DEBUG */
95e5190c10Smuffin 
96e5190c10Smuffin static char *sprintn(char *, long, int);
97e5190c10Smuffin static int printn(long, int);
987c478bd9Sstevel@tonic-gate 
99e5190c10Smuffin int
main(int argc,char ** argv)100e5190c10Smuffin main(int argc, char **argv)
1017c478bd9Sstevel@tonic-gate {
102e5190c10Smuffin 	char	*p, *q;
103e5190c10Smuffin 	int	j;
104e5190c10Smuffin 	tchar i;
1057c478bd9Sstevel@tonic-gate 	int eileenct;		/*count to test for "Eileen's loop"*/
1067c478bd9Sstevel@tonic-gate 	extern void catch(), kcatch();
107*9fed930bSToomas Soome 	char	**oargv;
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate 	(void)setlocale(LC_ALL, "");
1107c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
1117c478bd9Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST"
1127c478bd9Sstevel@tonic-gate #endif
1137c478bd9Sstevel@tonic-gate 	(void)textdomain(TEXT_DOMAIN);
1147c478bd9Sstevel@tonic-gate 	progname = argv[0];
1157c478bd9Sstevel@tonic-gate 	if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
1167c478bd9Sstevel@tonic-gate 		signal(SIGHUP, catch);
1177c478bd9Sstevel@tonic-gate 	if (signal(SIGINT, catch) == SIG_IGN) {
1187c478bd9Sstevel@tonic-gate 		signal(SIGHUP, SIG_IGN);
1197c478bd9Sstevel@tonic-gate 		signal(SIGINT, SIG_IGN);
1207c478bd9Sstevel@tonic-gate 		signal(SIGQUIT, SIG_IGN);
1217c478bd9Sstevel@tonic-gate 	}
1227c478bd9Sstevel@tonic-gate 	signal(SIGPIPE, catch);
1237c478bd9Sstevel@tonic-gate 	signal(SIGTERM, kcatch);
1247c478bd9Sstevel@tonic-gate 	oargv = argv;
1257c478bd9Sstevel@tonic-gate 	strcpy(cfname[0], "<standard input>");
1267c478bd9Sstevel@tonic-gate 	mrehash();
1277c478bd9Sstevel@tonic-gate 	nrehash();
1287c478bd9Sstevel@tonic-gate 	init0();
1297c478bd9Sstevel@tonic-gate #ifdef EUC
1307c478bd9Sstevel@tonic-gate #ifdef NROFF
1317c478bd9Sstevel@tonic-gate 	(void)localize();
1327c478bd9Sstevel@tonic-gate #endif /* NROFF */
1337c478bd9Sstevel@tonic-gate #endif /* EUC */
1347c478bd9Sstevel@tonic-gate 	if ((p = getenv("TYPESETTER")) != 0)
1357c478bd9Sstevel@tonic-gate 		strcpy(devname, p);
1367c478bd9Sstevel@tonic-gate 	while (--argc > 0 && (++argv)[0][0] == '-')
1377c478bd9Sstevel@tonic-gate 		switch (argv[0][1]) {
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 		case 'F':	/* switch font tables from default */
1407c478bd9Sstevel@tonic-gate 			if (argv[0][2] != '\0') {
1417c478bd9Sstevel@tonic-gate 				strcpy(termtab, &argv[0][2]);
1427c478bd9Sstevel@tonic-gate 				strcpy(fontfile, &argv[0][2]);
1437c478bd9Sstevel@tonic-gate 			} else {
1447c478bd9Sstevel@tonic-gate 				argv++; argc--;
145*9fed930bSToomas Soome 				if (argv[0] != NULL) {
1467c478bd9Sstevel@tonic-gate 					strcpy(termtab, argv[0]);
1477c478bd9Sstevel@tonic-gate 					strcpy(fontfile, argv[0]);
1487c478bd9Sstevel@tonic-gate 				} else
1497c478bd9Sstevel@tonic-gate 					errprint(gettext("missing the font directory"));
1507c478bd9Sstevel@tonic-gate 			}
1517c478bd9Sstevel@tonic-gate 			continue;
1527c478bd9Sstevel@tonic-gate 		case 0:
1537c478bd9Sstevel@tonic-gate 			goto start;
1547c478bd9Sstevel@tonic-gate 		case 'i':
1557c478bd9Sstevel@tonic-gate 			stdi++;
1567c478bd9Sstevel@tonic-gate 			continue;
1577c478bd9Sstevel@tonic-gate 		case 'q':
1587c478bd9Sstevel@tonic-gate #ifdef	NROFF
1597c478bd9Sstevel@tonic-gate 			quiet++;
1607c478bd9Sstevel@tonic-gate 			save_tty();
1617c478bd9Sstevel@tonic-gate #else
1627c478bd9Sstevel@tonic-gate 			errprint(gettext("-q option ignored in troff"));
163e5190c10Smuffin #endif	/* NROFF */
1647c478bd9Sstevel@tonic-gate 			continue;
1657c478bd9Sstevel@tonic-gate 		case 'n':
1667c478bd9Sstevel@tonic-gate 			npn = ctoi(&argv[0][2]);
1677c478bd9Sstevel@tonic-gate 			continue;
1687c478bd9Sstevel@tonic-gate 		case 'u':	/* set emboldening amount */
1697c478bd9Sstevel@tonic-gate 			bdtab[3] = ctoi(&argv[0][2]);
1707c478bd9Sstevel@tonic-gate 			if (bdtab[3] < 0 || bdtab[3] > 50)
1717c478bd9Sstevel@tonic-gate 				bdtab[3] = 0;
1727c478bd9Sstevel@tonic-gate 			continue;
1737c478bd9Sstevel@tonic-gate 		case 's':
1747c478bd9Sstevel@tonic-gate 			if (!(stop = ctoi(&argv[0][2])))
1757c478bd9Sstevel@tonic-gate 				stop++;
1767c478bd9Sstevel@tonic-gate 			continue;
1777c478bd9Sstevel@tonic-gate 		case 't':
1787c478bd9Sstevel@tonic-gate 			ptid = 1;
1797c478bd9Sstevel@tonic-gate 			continue;
1807c478bd9Sstevel@tonic-gate 		case 'r':
181*9fed930bSToomas Soome 			if (strlen(&argv[0][2]) >= 2) {
182*9fed930bSToomas Soome 				eibuf = roff_sprintf(ibuf+strlen(ibuf),
183*9fed930bSToomas Soome 				    ".nr %c %s\n", argv[0][2], &argv[0][3]);
184*9fed930bSToomas Soome 			} else {
1857c478bd9Sstevel@tonic-gate 				errprint(gettext("wrong options"));
186*9fed930bSToomas Soome 			}
1877c478bd9Sstevel@tonic-gate 			continue;
1887c478bd9Sstevel@tonic-gate 		case 'c':
1897c478bd9Sstevel@tonic-gate 		case 'm':
1907c478bd9Sstevel@tonic-gate 			if (mflg++ >= NMF) {
1917c478bd9Sstevel@tonic-gate 				errprint(gettext("Too many macro packages: %s"),
1927c478bd9Sstevel@tonic-gate 					 argv[0]);
1937c478bd9Sstevel@tonic-gate 				continue;
1947c478bd9Sstevel@tonic-gate 			}
1957c478bd9Sstevel@tonic-gate 		        if (argv[0][2] == '\0') {
1967c478bd9Sstevel@tonic-gate 				errprint(gettext("No library provided with -m"));
1977c478bd9Sstevel@tonic-gate 				done(02);
1987c478bd9Sstevel@tonic-gate 			}
199*9fed930bSToomas Soome 			p = getenv("TROFFMACS");
200*9fed930bSToomas Soome 			if (p != NULL && *p != '\0') {
2017c478bd9Sstevel@tonic-gate 			     if (tryfile(getenv("TROFFMACS"), &argv[0][2], nmfi))
2027c478bd9Sstevel@tonic-gate 			       nmfi++;
2037c478bd9Sstevel@tonic-gate 			} else
2047c478bd9Sstevel@tonic-gate 			  if (tryfile("/usr/share/lib/tmac/", &argv[0][2], nmfi)
2057c478bd9Sstevel@tonic-gate 			  || tryfile("/usr/share/lib/tmac/tmac.", &argv[0][2], nmfi))
2067c478bd9Sstevel@tonic-gate 				nmfi++;
2077c478bd9Sstevel@tonic-gate 			  else {
2087c478bd9Sstevel@tonic-gate 				errprint(gettext("Cannot find library %s\n"),
2097c478bd9Sstevel@tonic-gate 					argv[0]);
2107c478bd9Sstevel@tonic-gate 				done(02);
2117c478bd9Sstevel@tonic-gate 			  }
2127c478bd9Sstevel@tonic-gate 			continue;
2137c478bd9Sstevel@tonic-gate 		case 'o':
2147c478bd9Sstevel@tonic-gate 			getpn(&argv[0][2]);
2157c478bd9Sstevel@tonic-gate 			continue;
2167c478bd9Sstevel@tonic-gate 		case 'T':
2177c478bd9Sstevel@tonic-gate 			strcpy(devname, &argv[0][2]);
2187c478bd9Sstevel@tonic-gate 			dotT++;
2197c478bd9Sstevel@tonic-gate 			continue;
2207c478bd9Sstevel@tonic-gate #ifdef NROFF
2217c478bd9Sstevel@tonic-gate 		case 'h':
2227c478bd9Sstevel@tonic-gate 			hflg++;
2237c478bd9Sstevel@tonic-gate 			continue;
2247c478bd9Sstevel@tonic-gate 		case 'z':
2257c478bd9Sstevel@tonic-gate 			no_out++;
2267c478bd9Sstevel@tonic-gate 			continue;
2277c478bd9Sstevel@tonic-gate 		case 'e':
2287c478bd9Sstevel@tonic-gate 			eqflg++;
2297c478bd9Sstevel@tonic-gate 			continue;
2307c478bd9Sstevel@tonic-gate #endif
2317c478bd9Sstevel@tonic-gate #ifndef NROFF
2327c478bd9Sstevel@tonic-gate 		case 'z':
2337c478bd9Sstevel@tonic-gate 			no_out++;
2347c478bd9Sstevel@tonic-gate 		case 'a':
2357c478bd9Sstevel@tonic-gate 			ascii = 1;
2367c478bd9Sstevel@tonic-gate 			nofeed++;
2377c478bd9Sstevel@tonic-gate 			continue;
2387c478bd9Sstevel@tonic-gate 		case 'f':
2397c478bd9Sstevel@tonic-gate 			nofeed++;
2407c478bd9Sstevel@tonic-gate 			continue;
2417c478bd9Sstevel@tonic-gate #endif
2427c478bd9Sstevel@tonic-gate 		case '#':
2437c478bd9Sstevel@tonic-gate #ifdef	DEBUG
2447c478bd9Sstevel@tonic-gate 			debug = ctoi(&argv[0][2]);
2457c478bd9Sstevel@tonic-gate #else
2467c478bd9Sstevel@tonic-gate 			errprint("DEBUG not enabled");
247e5190c10Smuffin #endif	/* DEBUG */
2487c478bd9Sstevel@tonic-gate 			continue;
2497c478bd9Sstevel@tonic-gate 		default:
2507c478bd9Sstevel@tonic-gate 			errprint(gettext("unknown option %s"), argv[0]);
2517c478bd9Sstevel@tonic-gate 			done(02);
2527c478bd9Sstevel@tonic-gate 		}
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate start:
2557c478bd9Sstevel@tonic-gate 	init1(oargv[0][0]);
2567c478bd9Sstevel@tonic-gate 	argp = argv;
2577c478bd9Sstevel@tonic-gate 	rargc = argc;
2587c478bd9Sstevel@tonic-gate 	nmfi = 0;
2597c478bd9Sstevel@tonic-gate 	init2();
2607c478bd9Sstevel@tonic-gate 	setjmp(sjbuf);
2617c478bd9Sstevel@tonic-gate 	eileenct = 0;		/*reset count for "Eileen's loop"*/
2627c478bd9Sstevel@tonic-gate loop:
2637c478bd9Sstevel@tonic-gate 	copyf = lgf = nb = nflush = nlflg = 0;
2647c478bd9Sstevel@tonic-gate 	if (ip && rbf0(ip) == 0 && ejf && frame->pframe <= ejl) {
2657c478bd9Sstevel@tonic-gate 		nflush++;
2667c478bd9Sstevel@tonic-gate 		trap = 0;
2677c478bd9Sstevel@tonic-gate 		eject((struct s *)0);
2687c478bd9Sstevel@tonic-gate #ifdef	DEBUG
2697c478bd9Sstevel@tonic-gate 	if (debug & DB_LOOP)
2707c478bd9Sstevel@tonic-gate 		fdprintf(stderr, "loop: NL=%d, ejf=%d, lss=%d, eileenct=%d\n",
2717c478bd9Sstevel@tonic-gate 			numtab[NL].val, ejf, lss, eileenct);
272e5190c10Smuffin #endif	/* DEBUG */
2737c478bd9Sstevel@tonic-gate 		if (eileenct > 20) {
2747c478bd9Sstevel@tonic-gate 			errprint(gettext("job looping; check abuse of macros"));
2757c478bd9Sstevel@tonic-gate 			ejf = 0;	/*try to break Eileen's loop*/
2767c478bd9Sstevel@tonic-gate 			eileenct = 0;
2777c478bd9Sstevel@tonic-gate 		} else
2787c478bd9Sstevel@tonic-gate 			eileenct++;
2797c478bd9Sstevel@tonic-gate 		goto loop;
2807c478bd9Sstevel@tonic-gate 	}
2817c478bd9Sstevel@tonic-gate 	eileenct = 0;		/*reset count for "Eileen's loop"*/
2827c478bd9Sstevel@tonic-gate 	i = getch();
2837c478bd9Sstevel@tonic-gate 	if (pendt)
2847c478bd9Sstevel@tonic-gate 		goto Lt;
2857c478bd9Sstevel@tonic-gate 	if ((j = cbits(i)) == XPAR) {
2867c478bd9Sstevel@tonic-gate 		copyf++;
2877c478bd9Sstevel@tonic-gate 		tflg++;
2887c478bd9Sstevel@tonic-gate 		while (cbits(i) != '\n')
2897c478bd9Sstevel@tonic-gate 			pchar(i = getch());
2907c478bd9Sstevel@tonic-gate 		tflg = 0;
2917c478bd9Sstevel@tonic-gate 		copyf--;
2927c478bd9Sstevel@tonic-gate 		goto loop;
2937c478bd9Sstevel@tonic-gate 	}
2947c478bd9Sstevel@tonic-gate 	if (j == cc || j == c2) {
2957c478bd9Sstevel@tonic-gate 		if (j == c2)
2967c478bd9Sstevel@tonic-gate 			nb++;
2977c478bd9Sstevel@tonic-gate 		copyf++;
2987c478bd9Sstevel@tonic-gate 		while ((j = cbits(i = getch())) == ' ' || j == '\t')
2997c478bd9Sstevel@tonic-gate 			;
3007c478bd9Sstevel@tonic-gate 		ch = i;
3017c478bd9Sstevel@tonic-gate 		copyf--;
3027c478bd9Sstevel@tonic-gate 		control(getrq(), 1);
3037c478bd9Sstevel@tonic-gate 		flushi();
3047c478bd9Sstevel@tonic-gate 		goto loop;
3057c478bd9Sstevel@tonic-gate 	}
3067c478bd9Sstevel@tonic-gate Lt:
3077c478bd9Sstevel@tonic-gate 	ch = i;
3087c478bd9Sstevel@tonic-gate 	text();
3097c478bd9Sstevel@tonic-gate 	if (nlflg)
3107c478bd9Sstevel@tonic-gate 		numtab[HP].val = 0;
3117c478bd9Sstevel@tonic-gate 	goto loop;
3127c478bd9Sstevel@tonic-gate }
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 
315e5190c10Smuffin int
tryfile(pat,fn,idx)3167c478bd9Sstevel@tonic-gate tryfile(pat, fn, idx)
317e5190c10Smuffin char *pat, *fn;
3187c478bd9Sstevel@tonic-gate int idx;
3197c478bd9Sstevel@tonic-gate {
3207c478bd9Sstevel@tonic-gate 	strcpy(mfiles[idx], pat);
3217c478bd9Sstevel@tonic-gate 	strcat(mfiles[idx], fn);
3227c478bd9Sstevel@tonic-gate 	if (access(mfiles[idx], 4) == -1)
3237c478bd9Sstevel@tonic-gate 		return(0);
3247c478bd9Sstevel@tonic-gate 	else return(1);
3257c478bd9Sstevel@tonic-gate }
3267c478bd9Sstevel@tonic-gate 
catch()3277c478bd9Sstevel@tonic-gate void catch()
3287c478bd9Sstevel@tonic-gate {
3297c478bd9Sstevel@tonic-gate 	done3(01);
3307c478bd9Sstevel@tonic-gate }
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate 
kcatch()3337c478bd9Sstevel@tonic-gate void kcatch()
3347c478bd9Sstevel@tonic-gate {
3357c478bd9Sstevel@tonic-gate 	signal(SIGTERM, SIG_IGN);
3367c478bd9Sstevel@tonic-gate 	done3(01);
3377c478bd9Sstevel@tonic-gate }
3387c478bd9Sstevel@tonic-gate 
339e5190c10Smuffin int
init0()3407c478bd9Sstevel@tonic-gate init0()
3417c478bd9Sstevel@tonic-gate {
3427c478bd9Sstevel@tonic-gate 	eibuf = ibufp = ibuf;
3437c478bd9Sstevel@tonic-gate 	ibuf[0] = 0;
3447c478bd9Sstevel@tonic-gate 	numtab[NL].val = -1;
345e5190c10Smuffin 	return (0);
3467c478bd9Sstevel@tonic-gate }
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate 
349e5190c10Smuffin int
init1(a)3507c478bd9Sstevel@tonic-gate init1(a)
3517c478bd9Sstevel@tonic-gate char	a;
3527c478bd9Sstevel@tonic-gate {
353e5190c10Smuffin 	char	*p;
354e5190c10Smuffin 	int i;
3557c478bd9Sstevel@tonic-gate 
3567c478bd9Sstevel@tonic-gate 	p = tmp_name;
3577c478bd9Sstevel@tonic-gate 	if (a == 'a')
3587c478bd9Sstevel@tonic-gate 		p = &p[9];
3597c478bd9Sstevel@tonic-gate 	if ((ibf = mkstemp(p)) == -1) {
3607c478bd9Sstevel@tonic-gate 		errprint(gettext("cannot create temp file."));
3617c478bd9Sstevel@tonic-gate 		exit(-1);
3627c478bd9Sstevel@tonic-gate 	}
3637c478bd9Sstevel@tonic-gate 	unlkp = p;
3647c478bd9Sstevel@tonic-gate 	for (i = NTRTAB; --i; )
3657c478bd9Sstevel@tonic-gate 		trtab[i] = i;
3667c478bd9Sstevel@tonic-gate 	trtab[UNPAD] = ' ';
367e5190c10Smuffin 	return (0);
3687c478bd9Sstevel@tonic-gate }
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate 
371e5190c10Smuffin int
init2()3727c478bd9Sstevel@tonic-gate init2()
3737c478bd9Sstevel@tonic-gate {
374e5190c10Smuffin 	int	i, j;
3757c478bd9Sstevel@tonic-gate 	extern char	*setbrk();
3767c478bd9Sstevel@tonic-gate 	extern char	*ttyname();
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate 	ttyod = 2;
3797c478bd9Sstevel@tonic-gate 	if ((ttyp=ttyname(j=0)) != 0 || (ttyp=ttyname(j=1)) != 0 || (ttyp=ttyname(j=2)) != 0)
3807c478bd9Sstevel@tonic-gate 		;
3817c478bd9Sstevel@tonic-gate 	else
3827c478bd9Sstevel@tonic-gate 		ttyp = "notty";
3837c478bd9Sstevel@tonic-gate 	iflg = j;
3847c478bd9Sstevel@tonic-gate 	if (ascii)
3857c478bd9Sstevel@tonic-gate 		mesg(0);
3867c478bd9Sstevel@tonic-gate 	obufp = obuf;
3877c478bd9Sstevel@tonic-gate 	ptinit();
3887c478bd9Sstevel@tonic-gate 	mchbits();
3897c478bd9Sstevel@tonic-gate 	cvtime();
3907c478bd9Sstevel@tonic-gate 	numtab[PID].val = getpid();
3917c478bd9Sstevel@tonic-gate 	olinep = oline;
3927c478bd9Sstevel@tonic-gate 	ioff = 0;
3937c478bd9Sstevel@tonic-gate 	numtab[HP].val = init = 0;
3947c478bd9Sstevel@tonic-gate 	numtab[NL].val = -1;
3957c478bd9Sstevel@tonic-gate 	nfo = 0;
3967c478bd9Sstevel@tonic-gate 	ifile = 0;
3977c478bd9Sstevel@tonic-gate 	copyf = raw = 0;
3987c478bd9Sstevel@tonic-gate 	eibuf = roff_sprintf(ibuf+strlen(ibuf), ".ds .T %s\n", devname);
3997c478bd9Sstevel@tonic-gate 	numtab[CD].val = -1;	/* compensation */
4007c478bd9Sstevel@tonic-gate 	cpushback(ibuf);
4017c478bd9Sstevel@tonic-gate 	ibufp = ibuf;
4027c478bd9Sstevel@tonic-gate 	nx = mflg;
4037c478bd9Sstevel@tonic-gate 	frame = stk = (struct s *)setbrk(DELTA);
4047c478bd9Sstevel@tonic-gate 	dip = &d[0];
4057c478bd9Sstevel@tonic-gate 	nxf = frame + 1;
4067c478bd9Sstevel@tonic-gate #ifdef INCORE
4077c478bd9Sstevel@tonic-gate 	for (i = 0; i < NEV; i++) {
4087c478bd9Sstevel@tonic-gate 		extern tchar corebuf[];
4097c478bd9Sstevel@tonic-gate 		*(struct env *)&corebuf[i * sizeof(env)/sizeof(tchar)] = env;
4107c478bd9Sstevel@tonic-gate 	}
4117c478bd9Sstevel@tonic-gate #else
4127c478bd9Sstevel@tonic-gate 	for (i = NEV; i--; )
4137c478bd9Sstevel@tonic-gate 		write(ibf, (char *) & env, sizeof(env));
4147c478bd9Sstevel@tonic-gate #endif
415e5190c10Smuffin 	return (0);
4167c478bd9Sstevel@tonic-gate }
4177c478bd9Sstevel@tonic-gate 
418e5190c10Smuffin int
cvtime()4197c478bd9Sstevel@tonic-gate cvtime()
4207c478bd9Sstevel@tonic-gate {
4217c478bd9Sstevel@tonic-gate 	time_t	tt;
422e5190c10Smuffin 	struct tm *tm;
4237c478bd9Sstevel@tonic-gate 
4247c478bd9Sstevel@tonic-gate 	tt = time((time_t *) 0);
4257c478bd9Sstevel@tonic-gate 	tm = localtime(&tt);
4267c478bd9Sstevel@tonic-gate 	numtab[DY].val = tm->tm_mday;
4277c478bd9Sstevel@tonic-gate 	numtab[DW].val = tm->tm_wday + 1;
4287c478bd9Sstevel@tonic-gate 	numtab[YR].val = tm->tm_year;
4297c478bd9Sstevel@tonic-gate 	numtab[MO].val = tm->tm_mon + 1;
4307c478bd9Sstevel@tonic-gate 
431e5190c10Smuffin 	return (0);
4327c478bd9Sstevel@tonic-gate }
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate 
435e5190c10Smuffin int
ctoi(s)4367c478bd9Sstevel@tonic-gate ctoi(s)
437e5190c10Smuffin 	char *s;
4387c478bd9Sstevel@tonic-gate {
439e5190c10Smuffin 	int	n;
4407c478bd9Sstevel@tonic-gate 
4417c478bd9Sstevel@tonic-gate 	while (*s == ' ')
4427c478bd9Sstevel@tonic-gate 		s++;
4437c478bd9Sstevel@tonic-gate 	n = 0;
4447c478bd9Sstevel@tonic-gate 	while (isdigit((unsigned char)*s))
4457c478bd9Sstevel@tonic-gate 		n = 10 * n + *s++ - '0';
4467c478bd9Sstevel@tonic-gate 	return n;
4477c478bd9Sstevel@tonic-gate }
4487c478bd9Sstevel@tonic-gate 
4497c478bd9Sstevel@tonic-gate 
450e5190c10Smuffin int
mesg(f)4517c478bd9Sstevel@tonic-gate mesg(f)
4527c478bd9Sstevel@tonic-gate int	f;
4537c478bd9Sstevel@tonic-gate {
4547c478bd9Sstevel@tonic-gate 	static int	mode;
4557c478bd9Sstevel@tonic-gate 	struct stat stbuf;
4567c478bd9Sstevel@tonic-gate 
4577c478bd9Sstevel@tonic-gate 	if (!f) {
4587c478bd9Sstevel@tonic-gate 		stat(ttyp, &stbuf);
4597c478bd9Sstevel@tonic-gate 		mode = stbuf.st_mode;
4607c478bd9Sstevel@tonic-gate 		chmod(ttyp, mode & ~0122);	/* turn off writing for others */
4617c478bd9Sstevel@tonic-gate 	} else {
4627c478bd9Sstevel@tonic-gate 		if (ttyp && *ttyp && mode)
4637c478bd9Sstevel@tonic-gate 			chmod(ttyp, mode);
4647c478bd9Sstevel@tonic-gate 	}
465e5190c10Smuffin 
466e5190c10Smuffin 	return (0);
4677c478bd9Sstevel@tonic-gate }
4687c478bd9Sstevel@tonic-gate 
469e5190c10Smuffin int
errprint(s,s1,s2,s3,s4,s5)4707c478bd9Sstevel@tonic-gate errprint(s, s1, s2, s3, s4, s5)	/* error message printer */
4717c478bd9Sstevel@tonic-gate 	char *s, *s1, *s2, *s3, *s4, *s5;
4727c478bd9Sstevel@tonic-gate {
4737c478bd9Sstevel@tonic-gate 	fdprintf(stderr, "%s: ", progname);
4747c478bd9Sstevel@tonic-gate 	fdprintf(stderr, s, s1, s2, s3, s4, s5);
4757c478bd9Sstevel@tonic-gate 	if (numtab[CD].val > 0)
4767c478bd9Sstevel@tonic-gate 		fdprintf(stderr, gettext("; line %d, file %s"), numtab[CD].val,
4777c478bd9Sstevel@tonic-gate 			 cfname[ifi]);
4787c478bd9Sstevel@tonic-gate 	fdprintf(stderr, "\n");
4797c478bd9Sstevel@tonic-gate 	stackdump();
4807c478bd9Sstevel@tonic-gate #ifdef	DEBUG
4817c478bd9Sstevel@tonic-gate 	if (debug)
4827c478bd9Sstevel@tonic-gate 		abort();
483e5190c10Smuffin #endif	/* DEBUG */
484e5190c10Smuffin 	return (0);
4857c478bd9Sstevel@tonic-gate }
4867c478bd9Sstevel@tonic-gate 
4877c478bd9Sstevel@tonic-gate 
4887c478bd9Sstevel@tonic-gate /*
4897c478bd9Sstevel@tonic-gate  * Scaled down version of C Library printf.
4907c478bd9Sstevel@tonic-gate  * Only %s %u %d (==%u) %o %c %x %D are recognized.
4917c478bd9Sstevel@tonic-gate  */
4927c478bd9Sstevel@tonic-gate #undef putchar
4937c478bd9Sstevel@tonic-gate #define	putchar(n)	(*pfbp++ = (n))	/* NO CHECKING! */
4947c478bd9Sstevel@tonic-gate 
4957c478bd9Sstevel@tonic-gate static char	pfbuf[NTM];
4967c478bd9Sstevel@tonic-gate static char	*pfbp = pfbuf;
4977c478bd9Sstevel@tonic-gate int	stderr	 = 2;	/* NOT stdio value */
4987c478bd9Sstevel@tonic-gate 
4997c478bd9Sstevel@tonic-gate void
fdprintf(int fd,char * fmt,...)5007c478bd9Sstevel@tonic-gate fdprintf(int fd, char *fmt, ...)
5017c478bd9Sstevel@tonic-gate {
502e5190c10Smuffin 	int	c;
5037c478bd9Sstevel@tonic-gate 	char	*s;
504e5190c10Smuffin 	int	i;
5057c478bd9Sstevel@tonic-gate 	va_list	ap;
5067c478bd9Sstevel@tonic-gate 
5077c478bd9Sstevel@tonic-gate 	pfbp = pfbuf;
5087c478bd9Sstevel@tonic-gate 	va_start(ap, fmt);
5097c478bd9Sstevel@tonic-gate loop:
5107c478bd9Sstevel@tonic-gate 	while ((c = *fmt++) != '%') {
5117c478bd9Sstevel@tonic-gate 		if (c == '\0') {
5127c478bd9Sstevel@tonic-gate 			if (fd == stderr)
5137c478bd9Sstevel@tonic-gate 				write(stderr, pfbuf, pfbp - pfbuf);
5147c478bd9Sstevel@tonic-gate 			else {
5157c478bd9Sstevel@tonic-gate 				*pfbp = 0;
5167c478bd9Sstevel@tonic-gate 				pfbp = pfbuf;
5177c478bd9Sstevel@tonic-gate 				while (*pfbp) {
5187c478bd9Sstevel@tonic-gate 					*obufp++ = *pfbp++;
5197c478bd9Sstevel@tonic-gate 					if (obufp >= &obuf[OBUFSZ])
5207c478bd9Sstevel@tonic-gate 						flusho();
5217c478bd9Sstevel@tonic-gate 				}
5227c478bd9Sstevel@tonic-gate 			}
5237c478bd9Sstevel@tonic-gate 			va_end(ap);
5247c478bd9Sstevel@tonic-gate 			return;
5257c478bd9Sstevel@tonic-gate 		}
5267c478bd9Sstevel@tonic-gate 		putchar(c);
5277c478bd9Sstevel@tonic-gate 	}
5287c478bd9Sstevel@tonic-gate 	c = *fmt++;
5297c478bd9Sstevel@tonic-gate 	if (c == 'd') {
5307c478bd9Sstevel@tonic-gate 		i = va_arg(ap, int);
5317c478bd9Sstevel@tonic-gate 		if (i < 0) {
5327c478bd9Sstevel@tonic-gate 			putchar('-');
5337c478bd9Sstevel@tonic-gate 			i = -i;
5347c478bd9Sstevel@tonic-gate 		}
5357c478bd9Sstevel@tonic-gate 		printn((long)i, 10);
5367c478bd9Sstevel@tonic-gate 	} else if (c == 'u' || c == 'o' || c == 'x')
5377c478bd9Sstevel@tonic-gate 		printn(va_arg(ap, long), c == 'o' ? 8 : (c == 'x' ? 16 : 10));
5387c478bd9Sstevel@tonic-gate 	else if (c == 'c') {
5397c478bd9Sstevel@tonic-gate 		if (c > 0177 || c < 040)
5407c478bd9Sstevel@tonic-gate 			putchar('\\');
5417c478bd9Sstevel@tonic-gate 		putchar(va_arg(ap, int) & 0177);
5427c478bd9Sstevel@tonic-gate 	} else if (c == 's') {
5437c478bd9Sstevel@tonic-gate 		s = va_arg(ap, char *);
5447c478bd9Sstevel@tonic-gate 		while (c = *s++)
5457c478bd9Sstevel@tonic-gate 			putchar(c);
5467c478bd9Sstevel@tonic-gate 	} else if (c == 'D') {
5477c478bd9Sstevel@tonic-gate 		printn(va_arg(ap, long), 10);
5487c478bd9Sstevel@tonic-gate 	} else if (c == 'O') {
5497c478bd9Sstevel@tonic-gate 		printn(va_arg(ap, long), 8);
5507c478bd9Sstevel@tonic-gate 	}
5517c478bd9Sstevel@tonic-gate 	goto loop;
5527c478bd9Sstevel@tonic-gate }
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate 
5557c478bd9Sstevel@tonic-gate /*
5567c478bd9Sstevel@tonic-gate  * Print an unsigned integer in base b.
5577c478bd9Sstevel@tonic-gate  */
558e5190c10Smuffin static int
printn(n,b)559e5190c10Smuffin printn(n, b)
560e5190c10Smuffin 	long	n;
561e5190c10Smuffin 	int	b;
5627c478bd9Sstevel@tonic-gate {
563e5190c10Smuffin 	long	a;
5647c478bd9Sstevel@tonic-gate 
5657c478bd9Sstevel@tonic-gate 	if (n < 0) {	/* shouldn't happen */
5667c478bd9Sstevel@tonic-gate 		putchar('-');
5677c478bd9Sstevel@tonic-gate 		n = -n;
5687c478bd9Sstevel@tonic-gate 	}
5697c478bd9Sstevel@tonic-gate 	if (a = n / b)
5707c478bd9Sstevel@tonic-gate 		printn(a, b);
5717c478bd9Sstevel@tonic-gate 	putchar("0123456789ABCDEF"[(int)(n%b)]);
572e5190c10Smuffin 
573e5190c10Smuffin 	return (0);
5747c478bd9Sstevel@tonic-gate }
5757c478bd9Sstevel@tonic-gate 
5767c478bd9Sstevel@tonic-gate /* scaled down version of library roff_sprintf */
5777c478bd9Sstevel@tonic-gate /* same limits as fdprintf */
5787c478bd9Sstevel@tonic-gate /* returns pointer to \0 that ends the string */
5797c478bd9Sstevel@tonic-gate 
5807c478bd9Sstevel@tonic-gate /* VARARGS2 */
roff_sprintf(char * str,char * fmt,...)5817c478bd9Sstevel@tonic-gate char *roff_sprintf(char *str, char *fmt, ...)
5827c478bd9Sstevel@tonic-gate {
583e5190c10Smuffin 	int	c;
5847c478bd9Sstevel@tonic-gate 	char	*s;
585e5190c10Smuffin 	int	i;
5867c478bd9Sstevel@tonic-gate 	va_list ap;
5877c478bd9Sstevel@tonic-gate 
5887c478bd9Sstevel@tonic-gate 	va_start(ap, fmt);
5897c478bd9Sstevel@tonic-gate loop:
5907c478bd9Sstevel@tonic-gate 	while ((c = *fmt++) != '%') {
5917c478bd9Sstevel@tonic-gate 		if (c == '\0') {
5927c478bd9Sstevel@tonic-gate 			*str = 0;
5937c478bd9Sstevel@tonic-gate 			va_end(ap);
5947c478bd9Sstevel@tonic-gate 			return str;
5957c478bd9Sstevel@tonic-gate 		}
5967c478bd9Sstevel@tonic-gate 		*str++ = c;
5977c478bd9Sstevel@tonic-gate 	}
5987c478bd9Sstevel@tonic-gate 	c = *fmt++;
5997c478bd9Sstevel@tonic-gate 	if (c == 'd') {
6007c478bd9Sstevel@tonic-gate 		i = va_arg(ap, int);
6017c478bd9Sstevel@tonic-gate 		if (i < 0) {
6027c478bd9Sstevel@tonic-gate 			*str++ = '-';
6037c478bd9Sstevel@tonic-gate 			i = -i;
6047c478bd9Sstevel@tonic-gate 		}
6057c478bd9Sstevel@tonic-gate 		str = sprintn(str, (long)i, 10);
6067c478bd9Sstevel@tonic-gate 	} else if (c == 'u' || c == 'o' || c == 'x')
6077c478bd9Sstevel@tonic-gate 		str = sprintn(str, va_arg(ap, long), c == 'o' ? 8 : (c == 'x' ? 16 : 10));
6087c478bd9Sstevel@tonic-gate 	else if (c == 'c') {
6097c478bd9Sstevel@tonic-gate 		if (c > 0177 || c < 040)
6107c478bd9Sstevel@tonic-gate 			*str++ = '\\';
6117c478bd9Sstevel@tonic-gate 		*str++ = va_arg(ap, int) & 0177;
6127c478bd9Sstevel@tonic-gate 	} else if (c == 's') {
6137c478bd9Sstevel@tonic-gate 		s = va_arg(ap, char *);
6147c478bd9Sstevel@tonic-gate 		while (c = *s++)
6157c478bd9Sstevel@tonic-gate 			*str++ = c;
6167c478bd9Sstevel@tonic-gate 	} else if (c == 'D') {
6177c478bd9Sstevel@tonic-gate 		str = sprintn(str, va_arg(ap, long), 10);
6187c478bd9Sstevel@tonic-gate 	} else if (c == 'O') {
6197c478bd9Sstevel@tonic-gate 		str = sprintn(str, va_arg(ap, unsigned) , 8);
6207c478bd9Sstevel@tonic-gate 	}
6217c478bd9Sstevel@tonic-gate 	goto loop;
6227c478bd9Sstevel@tonic-gate }
6237c478bd9Sstevel@tonic-gate 
6247c478bd9Sstevel@tonic-gate /*
6257c478bd9Sstevel@tonic-gate  * Print an unsigned integer in base b.
6267c478bd9Sstevel@tonic-gate  */
sprintn(s,n,b)6277c478bd9Sstevel@tonic-gate static char *sprintn(s, n, b)
628e5190c10Smuffin 	char *s;
629e5190c10Smuffin 	long n;
630e5190c10Smuffin 	int b;
6317c478bd9Sstevel@tonic-gate {
632e5190c10Smuffin 	long	a;
6337c478bd9Sstevel@tonic-gate 
6347c478bd9Sstevel@tonic-gate 	if (n < 0) {	/* shouldn't happen */
6357c478bd9Sstevel@tonic-gate 		*s++ = '-';
6367c478bd9Sstevel@tonic-gate 		n = -n;
6377c478bd9Sstevel@tonic-gate 	}
6387c478bd9Sstevel@tonic-gate 	if (a = n / b)
6397c478bd9Sstevel@tonic-gate 		s = sprintn(s, a, b);
6407c478bd9Sstevel@tonic-gate 	*s++ = "0123456789ABCDEF"[(int)(n%b)];
6417c478bd9Sstevel@tonic-gate 	return s;
6427c478bd9Sstevel@tonic-gate }
6437c478bd9Sstevel@tonic-gate 
6447c478bd9Sstevel@tonic-gate 
645e5190c10Smuffin int
control(a,b)6467c478bd9Sstevel@tonic-gate control(a, b)
647e5190c10Smuffin int	a, b;
6487c478bd9Sstevel@tonic-gate {
649e5190c10Smuffin 	int	j;
6507c478bd9Sstevel@tonic-gate 
6517c478bd9Sstevel@tonic-gate 	if (a == 0 || (j = findmn(a)) == -1)
6527c478bd9Sstevel@tonic-gate 		return(0);
6537c478bd9Sstevel@tonic-gate 
6547c478bd9Sstevel@tonic-gate 	/*
6557c478bd9Sstevel@tonic-gate 	 * Attempt to find endless recursion at runtime. Arbitrary
6567c478bd9Sstevel@tonic-gate 	 * recursion limit of MAX_RECURSION_DEPTH was chosen as
6577c478bd9Sstevel@tonic-gate 	 * it is extremely unlikely that a correct nroff/troff
6587c478bd9Sstevel@tonic-gate 	 * invocation would exceed this value.
6597c478bd9Sstevel@tonic-gate 	 */
6607c478bd9Sstevel@tonic-gate 
6617c478bd9Sstevel@tonic-gate 	if (frame != stk) {
6627c478bd9Sstevel@tonic-gate 		int frame_cnt = 0;
6637c478bd9Sstevel@tonic-gate 		struct s *p;
6647c478bd9Sstevel@tonic-gate 
6657c478bd9Sstevel@tonic-gate 		for (p = frame; p != stk; p = p->pframe)
6667c478bd9Sstevel@tonic-gate 			frame_cnt++;
6677c478bd9Sstevel@tonic-gate 		if (frame_cnt > MAX_RECURSION_DEPTH) {
6687c478bd9Sstevel@tonic-gate 			errprint(
6697c478bd9Sstevel@tonic-gate 			    gettext("Exceeded maximum stack size (%d) when "
6707c478bd9Sstevel@tonic-gate 			    "executing macro %c%c. Stack dump follows"),
6717c478bd9Sstevel@tonic-gate 			    MAX_RECURSION_DEPTH,
6727c478bd9Sstevel@tonic-gate 			    frame->mname & 0177, (frame->mname >> BYTE) & 0177);
6737c478bd9Sstevel@tonic-gate 			edone(02);
6747c478bd9Sstevel@tonic-gate 		}
6757c478bd9Sstevel@tonic-gate 	}
6767c478bd9Sstevel@tonic-gate 
6777c478bd9Sstevel@tonic-gate #ifdef	DEBUG
6787c478bd9Sstevel@tonic-gate 	if (debug & DB_MAC)
6797c478bd9Sstevel@tonic-gate 		fdprintf(stderr, "control: macro %c%c, contab[%d]\n",
6807c478bd9Sstevel@tonic-gate 			a&0177, (a>>BYTE)&0177 ? (a>>BYTE)&0177 : ' ', j);
681e5190c10Smuffin #endif	/* DEBUG */
6827c478bd9Sstevel@tonic-gate 	if (contab[j].f == 0) {
6837c478bd9Sstevel@tonic-gate 		nxf->nargs = 0;
6847c478bd9Sstevel@tonic-gate 		if (b)
6857c478bd9Sstevel@tonic-gate 			collect();
6867c478bd9Sstevel@tonic-gate 		flushi();
6877c478bd9Sstevel@tonic-gate 		return pushi((filep)contab[j].mx, a);
6887c478bd9Sstevel@tonic-gate 	} else if (b)
6897c478bd9Sstevel@tonic-gate 		return((*contab[j].f)(0));
6907c478bd9Sstevel@tonic-gate 	else
6917c478bd9Sstevel@tonic-gate 		return(0);
6927c478bd9Sstevel@tonic-gate }
6937c478bd9Sstevel@tonic-gate 
694e5190c10Smuffin int
getrq()6957c478bd9Sstevel@tonic-gate getrq()
6967c478bd9Sstevel@tonic-gate {
697e5190c10Smuffin 	int	i, j;
6987c478bd9Sstevel@tonic-gate 
6997c478bd9Sstevel@tonic-gate 	if (((i = getach()) == 0) || ((j = getach()) == 0))
7007c478bd9Sstevel@tonic-gate 		goto rtn;
7017c478bd9Sstevel@tonic-gate 	i = PAIR(i, j);
7027c478bd9Sstevel@tonic-gate rtn:
7037c478bd9Sstevel@tonic-gate 	return(i);
7047c478bd9Sstevel@tonic-gate }
7057c478bd9Sstevel@tonic-gate 
7067c478bd9Sstevel@tonic-gate /*
7077c478bd9Sstevel@tonic-gate  * table encodes some special characters, to speed up tests
7087c478bd9Sstevel@tonic-gate  * in getchar, viz FLSS, RPT, f, \b, \n, fc, tabch, ldrch
7097c478bd9Sstevel@tonic-gate  */
7107c478bd9Sstevel@tonic-gate 
7117c478bd9Sstevel@tonic-gate char
7127c478bd9Sstevel@tonic-gate gchtab[] = {
7137c478bd9Sstevel@tonic-gate 	000,004,000,000,010,000,000,000, /* fc, ldr */
7147c478bd9Sstevel@tonic-gate 	001,002,001,000,001,000,000,000, /* \b, tab, nl, RPT */
7157c478bd9Sstevel@tonic-gate 	000,000,000,000,000,000,000,000,
7167c478bd9Sstevel@tonic-gate 	000,001,000,000,000,000,000,000, /* FLSS */
7177c478bd9Sstevel@tonic-gate 	000,000,000,000,000,000,000,000,
7187c478bd9Sstevel@tonic-gate 	000,000,000,000,000,000,000,000,
7197c478bd9Sstevel@tonic-gate 	000,000,000,000,000,000,000,000,
7207c478bd9Sstevel@tonic-gate 	000,000,000,000,000,000,000,000,
7217c478bd9Sstevel@tonic-gate 	000,000,000,000,000,000,000,000,
7227c478bd9Sstevel@tonic-gate 	000,000,000,000,000,000,000,000,
7237c478bd9Sstevel@tonic-gate 	000,000,000,000,000,000,000,000,
7247c478bd9Sstevel@tonic-gate 	000,000,000,000,000,000,000,000,
7257c478bd9Sstevel@tonic-gate 	000,000,000,000,000,000,001,000, /* f */
7267c478bd9Sstevel@tonic-gate 	000,000,000,000,000,000,000,000,
7277c478bd9Sstevel@tonic-gate 	000,000,000,000,000,000,000,000,
7287c478bd9Sstevel@tonic-gate 	000,000,000,000,000,000,000,000,
7297c478bd9Sstevel@tonic-gate };
7307c478bd9Sstevel@tonic-gate 
7317c478bd9Sstevel@tonic-gate tchar
getch()7327c478bd9Sstevel@tonic-gate getch()
7337c478bd9Sstevel@tonic-gate {
734e5190c10Smuffin 	int	k;
735e5190c10Smuffin 	tchar i, j;
7367c478bd9Sstevel@tonic-gate 	tchar setht(), setslant();
7377c478bd9Sstevel@tonic-gate 
7387c478bd9Sstevel@tonic-gate g0:
7397c478bd9Sstevel@tonic-gate 	if (i = ch) {
7407c478bd9Sstevel@tonic-gate #ifdef	DEBUG
7417c478bd9Sstevel@tonic-gate 		if (debug & DB_GETC)
7427c478bd9Sstevel@tonic-gate 			fdprintf(stderr, "getch: ch is %x (%c)\n",
7437c478bd9Sstevel@tonic-gate 				ch, (ch&0177) < 040 ? 0177 : ch&0177);
744e5190c10Smuffin #endif	/* DEBUG */
7457c478bd9Sstevel@tonic-gate 		if (cbits(i) == '\n')
7467c478bd9Sstevel@tonic-gate 			nlflg++;
7477c478bd9Sstevel@tonic-gate 		ch = 0;
7487c478bd9Sstevel@tonic-gate 		return(i);
7497c478bd9Sstevel@tonic-gate 	}
7507c478bd9Sstevel@tonic-gate 
7517c478bd9Sstevel@tonic-gate #ifdef	DEBUG
7527c478bd9Sstevel@tonic-gate 	if (nlflg)
7537c478bd9Sstevel@tonic-gate 		if (debug & DB_GETC)
7547c478bd9Sstevel@tonic-gate 			fdprintf(stderr,"getch: nlflg is %x\n", nlflg);
755e5190c10Smuffin #endif	/* DEBUG */
7567c478bd9Sstevel@tonic-gate 	if (nlflg)
7577c478bd9Sstevel@tonic-gate 		return('\n');
7587c478bd9Sstevel@tonic-gate 	i = getch0();
7597c478bd9Sstevel@tonic-gate #ifdef	DEBUG
7607c478bd9Sstevel@tonic-gate 	if (debug & DB_GETC)
7617c478bd9Sstevel@tonic-gate 		fdprintf(stderr, "getch: getch0 returns %x (%c)\n",
7627c478bd9Sstevel@tonic-gate 			i, (i&0177) < 040 ? 0177 : i&0177);
763e5190c10Smuffin #endif	/* DEBUG */
7647c478bd9Sstevel@tonic-gate 	if (ismot(i))
7657c478bd9Sstevel@tonic-gate 		return(i);
7667c478bd9Sstevel@tonic-gate 	k = cbits(i);
7677c478bd9Sstevel@tonic-gate 	if (k != ESC) {
7687c478bd9Sstevel@tonic-gate 		/*
7697c478bd9Sstevel@tonic-gate 		 * gchtab[] has only 128 entries
7707c478bd9Sstevel@tonic-gate 		 * if k is out of the range, it should be
7717c478bd9Sstevel@tonic-gate 		 * handled as gchtab[k] == 0
7727c478bd9Sstevel@tonic-gate 		 */
7737c478bd9Sstevel@tonic-gate 		if (!isascii(k) || gchtab[k]==0)
7747c478bd9Sstevel@tonic-gate 			return(i);
7757c478bd9Sstevel@tonic-gate 		if (k == '\n') {
7767c478bd9Sstevel@tonic-gate 			if (cbits(i) == '\n') {
7777c478bd9Sstevel@tonic-gate 				nlflg++;
7787c478bd9Sstevel@tonic-gate 				if (ip == 0)
7797c478bd9Sstevel@tonic-gate 					numtab[CD].val++; /* line number */
7807c478bd9Sstevel@tonic-gate 			}
7817c478bd9Sstevel@tonic-gate 			return(k);
7827c478bd9Sstevel@tonic-gate 		}
7837c478bd9Sstevel@tonic-gate 		if (k == FLSS) {
7847c478bd9Sstevel@tonic-gate 			copyf++;
7857c478bd9Sstevel@tonic-gate 			raw++;
7867c478bd9Sstevel@tonic-gate 			i = getch0();
7877c478bd9Sstevel@tonic-gate 			if (!fi)
7887c478bd9Sstevel@tonic-gate 				flss = i;
7897c478bd9Sstevel@tonic-gate 			copyf--;
7907c478bd9Sstevel@tonic-gate 			raw--;
7917c478bd9Sstevel@tonic-gate 			goto g0;
7927c478bd9Sstevel@tonic-gate 		}
7937c478bd9Sstevel@tonic-gate 		if (k == RPT) {
7947c478bd9Sstevel@tonic-gate 			setrpt();
7957c478bd9Sstevel@tonic-gate 			goto g0;
7967c478bd9Sstevel@tonic-gate 		}
7977c478bd9Sstevel@tonic-gate 		if (!copyf) {
7987c478bd9Sstevel@tonic-gate 			if (k == 'f' && lg && !lgf) {
7997c478bd9Sstevel@tonic-gate 				i = getlg(i);
8007c478bd9Sstevel@tonic-gate 				return(i);
8017c478bd9Sstevel@tonic-gate 			}
8027c478bd9Sstevel@tonic-gate 			if (k == fc || k == tabch || k == ldrch) {
8037c478bd9Sstevel@tonic-gate 				if ((i = setfield(k)) == 0)
8047c478bd9Sstevel@tonic-gate 					goto g0;
8057c478bd9Sstevel@tonic-gate 				else
8067c478bd9Sstevel@tonic-gate 					return(i);
8077c478bd9Sstevel@tonic-gate 			}
8087c478bd9Sstevel@tonic-gate 			if (k == '\b') {
8097c478bd9Sstevel@tonic-gate 				i = makem(-width(' ' | chbits));
8107c478bd9Sstevel@tonic-gate 				return(i);
8117c478bd9Sstevel@tonic-gate 			}
8127c478bd9Sstevel@tonic-gate 		}
8137c478bd9Sstevel@tonic-gate 		return(i);
8147c478bd9Sstevel@tonic-gate 	}
8157c478bd9Sstevel@tonic-gate 	k = cbits(j = getch0());
8167c478bd9Sstevel@tonic-gate 	if (ismot(j))
8177c478bd9Sstevel@tonic-gate 		return(j);
8187c478bd9Sstevel@tonic-gate 	switch (k) {
8197c478bd9Sstevel@tonic-gate 
8207c478bd9Sstevel@tonic-gate 	case 'X':	/* \X'...' for copy through */
8217c478bd9Sstevel@tonic-gate 		setxon();
8227c478bd9Sstevel@tonic-gate 		goto g0;
8237c478bd9Sstevel@tonic-gate 	case '\n':	/* concealed newline */
8247c478bd9Sstevel@tonic-gate 		goto g0;
8257c478bd9Sstevel@tonic-gate 	case 'n':	/* number register */
8267c478bd9Sstevel@tonic-gate 		setn();
8277c478bd9Sstevel@tonic-gate 		goto g0;
8287c478bd9Sstevel@tonic-gate 	case '*':	/* string indicator */
8297c478bd9Sstevel@tonic-gate 		setstr();
8307c478bd9Sstevel@tonic-gate 		goto g0;
8317c478bd9Sstevel@tonic-gate 	case '$':	/* argument indicator */
8327c478bd9Sstevel@tonic-gate 		seta();
8337c478bd9Sstevel@tonic-gate 		goto g0;
8347c478bd9Sstevel@tonic-gate 	case '{':	/* LEFT */
8357c478bd9Sstevel@tonic-gate 		i = LEFT;
8367c478bd9Sstevel@tonic-gate 		goto gx;
8377c478bd9Sstevel@tonic-gate 	case '}':	/* RIGHT */
8387c478bd9Sstevel@tonic-gate 		i = RIGHT;
8397c478bd9Sstevel@tonic-gate 		goto gx;
8407c478bd9Sstevel@tonic-gate 	case '"':	/* comment */
8417c478bd9Sstevel@tonic-gate 		while (cbits(i = getch0()) != '\n')
8427c478bd9Sstevel@tonic-gate 			;
8437c478bd9Sstevel@tonic-gate 		nlflg++;
8447c478bd9Sstevel@tonic-gate 		if (ip == 0)
8457c478bd9Sstevel@tonic-gate 			numtab[CD].val++;
8467c478bd9Sstevel@tonic-gate 		return(i);
8477c478bd9Sstevel@tonic-gate 	case ESC:	/* double backslash */
8487c478bd9Sstevel@tonic-gate 		i = eschar;
8497c478bd9Sstevel@tonic-gate 		goto gx;
8507c478bd9Sstevel@tonic-gate 	case 'e':	/* printable version of current eschar */
8517c478bd9Sstevel@tonic-gate 		i = PRESC;
8527c478bd9Sstevel@tonic-gate 		goto gx;
8537c478bd9Sstevel@tonic-gate 	case ' ':	/* unpaddable space */
8547c478bd9Sstevel@tonic-gate 		i = UNPAD;
8557c478bd9Sstevel@tonic-gate 		goto gx;
8567c478bd9Sstevel@tonic-gate 	case '\'':	/* \(aa */
8577c478bd9Sstevel@tonic-gate 		i = ACUTE;
8587c478bd9Sstevel@tonic-gate 		goto gx;
8597c478bd9Sstevel@tonic-gate 	case '`':	/* \(ga */
8607c478bd9Sstevel@tonic-gate 		i = GRAVE;
8617c478bd9Sstevel@tonic-gate 		goto gx;
8627c478bd9Sstevel@tonic-gate 	case '_':	/* \(ul */
8637c478bd9Sstevel@tonic-gate 		i = UNDERLINE;
8647c478bd9Sstevel@tonic-gate 		goto gx;
8657c478bd9Sstevel@tonic-gate 	case '-':	/* current font minus */
8667c478bd9Sstevel@tonic-gate 		i = MINUS;
8677c478bd9Sstevel@tonic-gate 		goto gx;
8687c478bd9Sstevel@tonic-gate 	case '&':	/* filler */
8697c478bd9Sstevel@tonic-gate 		i = FILLER;
8707c478bd9Sstevel@tonic-gate 		goto gx;
8717c478bd9Sstevel@tonic-gate 	case 'c':	/* to be continued */
8727c478bd9Sstevel@tonic-gate 		i = CONT;
8737c478bd9Sstevel@tonic-gate 		goto gx;
8747c478bd9Sstevel@tonic-gate 	case '!':	/* transparent indicator */
8757c478bd9Sstevel@tonic-gate 		i = XPAR;
8767c478bd9Sstevel@tonic-gate 		goto gx;
8777c478bd9Sstevel@tonic-gate 	case 't':	/* tab */
8787c478bd9Sstevel@tonic-gate 		i = '\t';
8797c478bd9Sstevel@tonic-gate 		return(i);
8807c478bd9Sstevel@tonic-gate 	case 'a':	/* leader (SOH) */
8817c478bd9Sstevel@tonic-gate 		i = LEADER;
8827c478bd9Sstevel@tonic-gate 		return(i);
8837c478bd9Sstevel@tonic-gate 	case '%':	/* ohc */
8847c478bd9Sstevel@tonic-gate 		i = OHC;
8857c478bd9Sstevel@tonic-gate 		return(i);
8867c478bd9Sstevel@tonic-gate 	case 'g':	/* return format of a number register */
8877c478bd9Sstevel@tonic-gate 		setaf();
8887c478bd9Sstevel@tonic-gate 		goto g0;
8897c478bd9Sstevel@tonic-gate 	case 'N':	/* absolute character number */
8907c478bd9Sstevel@tonic-gate 		i = setabs();
8917c478bd9Sstevel@tonic-gate 		goto gx;
8927c478bd9Sstevel@tonic-gate 	case '.':	/* . */
8937c478bd9Sstevel@tonic-gate 		i = '.';
8947c478bd9Sstevel@tonic-gate gx:
8957c478bd9Sstevel@tonic-gate 		setsfbits(i, sfbits(j));
8967c478bd9Sstevel@tonic-gate 		return(i);
8977c478bd9Sstevel@tonic-gate 	}
8987c478bd9Sstevel@tonic-gate 	if (copyf) {
8997c478bd9Sstevel@tonic-gate 		*pbp++ = j;
9007c478bd9Sstevel@tonic-gate 		return(eschar);
9017c478bd9Sstevel@tonic-gate 	}
9027c478bd9Sstevel@tonic-gate 	switch (k) {
9037c478bd9Sstevel@tonic-gate 
9047c478bd9Sstevel@tonic-gate 	case 'p':	/* spread */
9057c478bd9Sstevel@tonic-gate 		spread++;
9067c478bd9Sstevel@tonic-gate 		goto g0;
9077c478bd9Sstevel@tonic-gate 	case '(':	/* special char name */
9087c478bd9Sstevel@tonic-gate 		if ((i = setch()) == 0)
9097c478bd9Sstevel@tonic-gate 			goto g0;
9107c478bd9Sstevel@tonic-gate 		return(i);
9117c478bd9Sstevel@tonic-gate 	case 's':	/* size indicator */
9127c478bd9Sstevel@tonic-gate 		setps();
9137c478bd9Sstevel@tonic-gate 		goto g0;
9147c478bd9Sstevel@tonic-gate 	case 'H':	/* character height */
9157c478bd9Sstevel@tonic-gate 		return(setht());
9167c478bd9Sstevel@tonic-gate 	case 'S':	/* slant */
9177c478bd9Sstevel@tonic-gate 		return(setslant());
9187c478bd9Sstevel@tonic-gate 	case 'f':	/* font indicator */
9197c478bd9Sstevel@tonic-gate 		setfont(0);
9207c478bd9Sstevel@tonic-gate 		goto g0;
9217c478bd9Sstevel@tonic-gate 	case 'w':	/* width function */
9227c478bd9Sstevel@tonic-gate 		setwd();
9237c478bd9Sstevel@tonic-gate 		goto g0;
9247c478bd9Sstevel@tonic-gate 	case 'v':	/* vert mot */
9257c478bd9Sstevel@tonic-gate 		if (i = vmot())
9267c478bd9Sstevel@tonic-gate 			return(i);
9277c478bd9Sstevel@tonic-gate 		goto g0;
9287c478bd9Sstevel@tonic-gate 	case 'h': 	/* horiz mot */
9297c478bd9Sstevel@tonic-gate 		if (i = hmot())
9307c478bd9Sstevel@tonic-gate 			return(i);
9317c478bd9Sstevel@tonic-gate 		goto g0;
9327c478bd9Sstevel@tonic-gate 	case 'z':	/* zero with char */
9337c478bd9Sstevel@tonic-gate 		return(setz());
9347c478bd9Sstevel@tonic-gate 	case 'l':	/* hor line */
9357c478bd9Sstevel@tonic-gate 		setline();
9367c478bd9Sstevel@tonic-gate 		goto g0;
9377c478bd9Sstevel@tonic-gate 	case 'L':	/* vert line */
9387c478bd9Sstevel@tonic-gate 		setvline();
9397c478bd9Sstevel@tonic-gate 		goto g0;
9407c478bd9Sstevel@tonic-gate 	case 'D':	/* drawing function */
9417c478bd9Sstevel@tonic-gate 		setdraw();
9427c478bd9Sstevel@tonic-gate 		goto g0;
9437c478bd9Sstevel@tonic-gate 	case 'b':	/* bracket */
9447c478bd9Sstevel@tonic-gate 		setbra();
9457c478bd9Sstevel@tonic-gate 		goto g0;
9467c478bd9Sstevel@tonic-gate 	case 'o':	/* overstrike */
9477c478bd9Sstevel@tonic-gate 		setov();
9487c478bd9Sstevel@tonic-gate 		goto g0;
9497c478bd9Sstevel@tonic-gate 	case 'k':	/* mark hor place */
9507c478bd9Sstevel@tonic-gate 		if ((k = findr(getsn())) != -1) {
9517c478bd9Sstevel@tonic-gate 			numtab[k].val = numtab[HP].val;
9527c478bd9Sstevel@tonic-gate 		}
9537c478bd9Sstevel@tonic-gate 		goto g0;
9547c478bd9Sstevel@tonic-gate 	case '0':	/* number space */
9557c478bd9Sstevel@tonic-gate 		return(makem(width('0' | chbits)));
9567c478bd9Sstevel@tonic-gate #ifdef NROFF
9577c478bd9Sstevel@tonic-gate 	case '|':
9587c478bd9Sstevel@tonic-gate 	case '^':
9597c478bd9Sstevel@tonic-gate 		goto g0;
9607c478bd9Sstevel@tonic-gate #else
9617c478bd9Sstevel@tonic-gate 	case '|':	/* narrow space */
9627c478bd9Sstevel@tonic-gate 		return(makem((int)(EM)/6));
9637c478bd9Sstevel@tonic-gate 	case '^':	/* half narrow space */
9647c478bd9Sstevel@tonic-gate 		return(makem((int)(EM)/12));
9657c478bd9Sstevel@tonic-gate #endif
9667c478bd9Sstevel@tonic-gate 	case 'x':	/* extra line space */
9677c478bd9Sstevel@tonic-gate 		if (i = xlss())
9687c478bd9Sstevel@tonic-gate 			return(i);
9697c478bd9Sstevel@tonic-gate 		goto g0;
9707c478bd9Sstevel@tonic-gate 	case 'u':	/* half em up */
9717c478bd9Sstevel@tonic-gate 	case 'r':	/* full em up */
9727c478bd9Sstevel@tonic-gate 	case 'd':	/* half em down */
9737c478bd9Sstevel@tonic-gate 		return(sethl(k));
9747c478bd9Sstevel@tonic-gate 	default:
9757c478bd9Sstevel@tonic-gate 		return(j);
9767c478bd9Sstevel@tonic-gate 	}
9777c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
9787c478bd9Sstevel@tonic-gate }
9797c478bd9Sstevel@tonic-gate 
980e5190c10Smuffin int
setxon()9817c478bd9Sstevel@tonic-gate setxon()	/* \X'...' for copy through */
9827c478bd9Sstevel@tonic-gate {
9837c478bd9Sstevel@tonic-gate 	tchar xbuf[NC];
984e5190c10Smuffin 	tchar *i;
9857c478bd9Sstevel@tonic-gate 	tchar c;
9867c478bd9Sstevel@tonic-gate 	int delim, k;
9877c478bd9Sstevel@tonic-gate 
9887c478bd9Sstevel@tonic-gate 	if (ismot(c = getch()))
989e5190c10Smuffin 		return (0);
9907c478bd9Sstevel@tonic-gate 	delim = cbits(c);
9917c478bd9Sstevel@tonic-gate 	i = xbuf;
9927c478bd9Sstevel@tonic-gate 	*i++ = XON;
9937c478bd9Sstevel@tonic-gate 	while ((k = cbits(c = getch())) != delim && k != '\n' && i < xbuf+NC-1) {
9947c478bd9Sstevel@tonic-gate 		if (k == ' ')
9957c478bd9Sstevel@tonic-gate 			setcbits(c, UNPAD);
9967c478bd9Sstevel@tonic-gate 		*i++ = c | ZBIT;
9977c478bd9Sstevel@tonic-gate 	}
9987c478bd9Sstevel@tonic-gate 	*i++ = XOFF;
9997c478bd9Sstevel@tonic-gate 	*i = 0;
10007c478bd9Sstevel@tonic-gate 	pushback(xbuf);
1001e5190c10Smuffin 
1002e5190c10Smuffin 	return (0);
10037c478bd9Sstevel@tonic-gate }
10047c478bd9Sstevel@tonic-gate 
10057c478bd9Sstevel@tonic-gate 
10067c478bd9Sstevel@tonic-gate char	ifilt[32] = {
10077c478bd9Sstevel@tonic-gate 	0, 001, 002, 003, 0, 005, 006, 007, 010, 011, 012};
10087c478bd9Sstevel@tonic-gate 
getch0()10097c478bd9Sstevel@tonic-gate tchar getch0()
10107c478bd9Sstevel@tonic-gate {
1011e5190c10Smuffin 	int	j;
1012e5190c10Smuffin 	tchar i;
10137c478bd9Sstevel@tonic-gate #ifdef	EUC
10147c478bd9Sstevel@tonic-gate #ifdef	NROFF
1015e5190c10Smuffin 	int	n;
10167c478bd9Sstevel@tonic-gate 	int col_index;
10177c478bd9Sstevel@tonic-gate #endif	/* NROFF */
10187c478bd9Sstevel@tonic-gate #endif	/* EUC */
10197c478bd9Sstevel@tonic-gate 
10207c478bd9Sstevel@tonic-gate again:
10217c478bd9Sstevel@tonic-gate 	if (pbp > lastpbp)
10227c478bd9Sstevel@tonic-gate 		i = *--pbp;
10237c478bd9Sstevel@tonic-gate 	else if (ip) {
10247c478bd9Sstevel@tonic-gate #ifdef INCORE
10257c478bd9Sstevel@tonic-gate 		extern tchar corebuf[];
10267c478bd9Sstevel@tonic-gate 		i = corebuf[ip];
10277c478bd9Sstevel@tonic-gate 		if (i == 0)
10287c478bd9Sstevel@tonic-gate 			i = rbf();
10297c478bd9Sstevel@tonic-gate 		else {
10307c478bd9Sstevel@tonic-gate 			if ((++ip & (BLK - 1)) == 0) {
10317c478bd9Sstevel@tonic-gate 				--ip;
10327c478bd9Sstevel@tonic-gate 				(void)rbf();
10337c478bd9Sstevel@tonic-gate 			}
10347c478bd9Sstevel@tonic-gate 		}
10357c478bd9Sstevel@tonic-gate #else
10367c478bd9Sstevel@tonic-gate 		i = rbf();
10377c478bd9Sstevel@tonic-gate #endif
10387c478bd9Sstevel@tonic-gate 	} else {
10397c478bd9Sstevel@tonic-gate 		if (donef || ndone)
10407c478bd9Sstevel@tonic-gate 			done(0);
10417c478bd9Sstevel@tonic-gate 		if (nx || ibufp >= eibuf) {
10427c478bd9Sstevel@tonic-gate 			if (nfo==0) {
10437c478bd9Sstevel@tonic-gate g0:
10447c478bd9Sstevel@tonic-gate 				if (nextfile()) {
10457c478bd9Sstevel@tonic-gate 					if (ip)
10467c478bd9Sstevel@tonic-gate 						goto again;
10477c478bd9Sstevel@tonic-gate 					if (ibufp < eibuf)
10487c478bd9Sstevel@tonic-gate 						goto g2;
10497c478bd9Sstevel@tonic-gate 				}
10507c478bd9Sstevel@tonic-gate 			}
10517c478bd9Sstevel@tonic-gate 			nx = 0;
10527c478bd9Sstevel@tonic-gate 			if ((j = read(ifile, ibuf, IBUFSZ)) <= 0)
10537c478bd9Sstevel@tonic-gate 				goto g0;
10547c478bd9Sstevel@tonic-gate 			ibufp = ibuf;
10557c478bd9Sstevel@tonic-gate 			eibuf = ibuf + j;
10567c478bd9Sstevel@tonic-gate 			if (ip)
10577c478bd9Sstevel@tonic-gate 				goto again;
10587c478bd9Sstevel@tonic-gate 		}
10597c478bd9Sstevel@tonic-gate g2:
10607c478bd9Sstevel@tonic-gate #ifndef	EUC
10617c478bd9Sstevel@tonic-gate 		i = *ibufp++ & 0177;
10627c478bd9Sstevel@tonic-gate 		ioff++;
10637c478bd9Sstevel@tonic-gate 		if (i >= 040 && i < 0177)
10647c478bd9Sstevel@tonic-gate #else
10657c478bd9Sstevel@tonic-gate #ifndef	NROFF
10667c478bd9Sstevel@tonic-gate 		i = *ibufp++ & 0177;
10677c478bd9Sstevel@tonic-gate 		ioff++;
10687c478bd9Sstevel@tonic-gate 		if (i >= 040 && i < 0177)
10697c478bd9Sstevel@tonic-gate #else
10707c478bd9Sstevel@tonic-gate 		i = *ibufp++ & 0377;
10717c478bd9Sstevel@tonic-gate 		*mbbuf1p++ = i;
10727c478bd9Sstevel@tonic-gate 		*mbbuf1p = 0;
10737c478bd9Sstevel@tonic-gate 		if (!multi_locale) {
10747c478bd9Sstevel@tonic-gate 			twc = 0;
10757c478bd9Sstevel@tonic-gate 			mbbuf1p = mbbuf1;
10767c478bd9Sstevel@tonic-gate 		} else if ((n = mbtowc(&twc, mbbuf1, MB_CUR_MAX)) <= 0) {
10777c478bd9Sstevel@tonic-gate 			if (mbbuf1p >= mbbuf1 + MB_CUR_MAX) {
10787c478bd9Sstevel@tonic-gate 				i &= ~(MBMASK | CSMASK);
10797c478bd9Sstevel@tonic-gate 				twc = 0;
10807c478bd9Sstevel@tonic-gate 				mbbuf1p = mbbuf1;
10817c478bd9Sstevel@tonic-gate 				*mbbuf1p = 0;
10827c478bd9Sstevel@tonic-gate 			} else {
10837c478bd9Sstevel@tonic-gate 				i |= (MIDDLEOFMB);
10847c478bd9Sstevel@tonic-gate 			}
10857c478bd9Sstevel@tonic-gate 		} else {
10867c478bd9Sstevel@tonic-gate 			if (n > 1)
10877c478bd9Sstevel@tonic-gate 				i |= (LASTOFMB);
10887c478bd9Sstevel@tonic-gate 			else
10897c478bd9Sstevel@tonic-gate 				i |= (BYTE_CHR);
10907c478bd9Sstevel@tonic-gate 			if (isascii(twc)) {
10917c478bd9Sstevel@tonic-gate 				col_index = 0;
10927c478bd9Sstevel@tonic-gate 			} else {
10937c478bd9Sstevel@tonic-gate 				if ((col_index = wcwidth(twc)) < 0)
10947c478bd9Sstevel@tonic-gate 					col_index = 0;
10957c478bd9Sstevel@tonic-gate 			}
10967c478bd9Sstevel@tonic-gate 			setcsbits(i, col_index);
10977c478bd9Sstevel@tonic-gate 			twc = 0;
10987c478bd9Sstevel@tonic-gate 			mbbuf1p = mbbuf1;
10997c478bd9Sstevel@tonic-gate 		}
11007c478bd9Sstevel@tonic-gate 		ioff++;
11017c478bd9Sstevel@tonic-gate 		if (i >= 040 && i != 0177)
11027c478bd9Sstevel@tonic-gate #endif	/* NROFF */
11037c478bd9Sstevel@tonic-gate #endif	/* EUC */
11047c478bd9Sstevel@tonic-gate 			goto g4;
11057c478bd9Sstevel@tonic-gate 		if (i != 0177)
11067c478bd9Sstevel@tonic-gate 			i = ifilt[i];
11077c478bd9Sstevel@tonic-gate 	}
11087c478bd9Sstevel@tonic-gate 	if (cbits(i) == IMP && !raw)
11097c478bd9Sstevel@tonic-gate 		goto again;
11107c478bd9Sstevel@tonic-gate 	if ((i == 0 || i == 0177) && !init && !raw) {
11117c478bd9Sstevel@tonic-gate 		goto again;
11127c478bd9Sstevel@tonic-gate 	}
11137c478bd9Sstevel@tonic-gate g4:
11147c478bd9Sstevel@tonic-gate #ifndef EUC
11157c478bd9Sstevel@tonic-gate 	if (copyf == 0 && (i & ~BYTEMASK) == 0)
11167c478bd9Sstevel@tonic-gate #else
11177c478bd9Sstevel@tonic-gate #ifndef NROFF
11187c478bd9Sstevel@tonic-gate 	if (copyf == 0 && (i & ~BYTEMASK) == 0)
11197c478bd9Sstevel@tonic-gate #else
11207c478bd9Sstevel@tonic-gate 	if (copyf == 0 && (i & ~CHMASK) == 0)
11217c478bd9Sstevel@tonic-gate #endif /* NROFF */
11227c478bd9Sstevel@tonic-gate #endif /* EUC */
11237c478bd9Sstevel@tonic-gate 		i |= chbits;
11247c478bd9Sstevel@tonic-gate #ifdef EUC
11257c478bd9Sstevel@tonic-gate #ifdef NROFF
11267c478bd9Sstevel@tonic-gate 	if (multi_locale)
11277c478bd9Sstevel@tonic-gate 		if (i & MBMASK1)
11287c478bd9Sstevel@tonic-gate 			i |= ZBIT;
11297c478bd9Sstevel@tonic-gate #endif /* NROFF */
11307c478bd9Sstevel@tonic-gate #endif /* EUC */
11317c478bd9Sstevel@tonic-gate 	if (cbits(i) == eschar && !raw)
11327c478bd9Sstevel@tonic-gate 		setcbits(i, ESC);
11337c478bd9Sstevel@tonic-gate 	return(i);
11347c478bd9Sstevel@tonic-gate }
11357c478bd9Sstevel@tonic-gate 
1136e5190c10Smuffin int
pushback(b)11377c478bd9Sstevel@tonic-gate pushback(b)
1138e5190c10Smuffin tchar *b;
11397c478bd9Sstevel@tonic-gate {
1140e5190c10Smuffin 	tchar *ob = b;
11417c478bd9Sstevel@tonic-gate 
11427c478bd9Sstevel@tonic-gate 	while (*b++)
11437c478bd9Sstevel@tonic-gate 		;
11447c478bd9Sstevel@tonic-gate 	b--;
11457c478bd9Sstevel@tonic-gate 	while (b > ob && pbp < &pbbuf[NC-3])
11467c478bd9Sstevel@tonic-gate 		*pbp++ = *--b;
11477c478bd9Sstevel@tonic-gate 	if (pbp >= &pbbuf[NC-3]) {
11487c478bd9Sstevel@tonic-gate 		errprint(gettext("pushback overflow"));
11497c478bd9Sstevel@tonic-gate 		done(2);
11507c478bd9Sstevel@tonic-gate 	}
1151e5190c10Smuffin 
1152e5190c10Smuffin 	return (0);
11537c478bd9Sstevel@tonic-gate }
11547c478bd9Sstevel@tonic-gate 
1155e5190c10Smuffin int
cpushback(b)11567c478bd9Sstevel@tonic-gate cpushback(b)
1157e5190c10Smuffin char *b;
11587c478bd9Sstevel@tonic-gate {
1159e5190c10Smuffin 	char *ob = b;
11607c478bd9Sstevel@tonic-gate 
11617c478bd9Sstevel@tonic-gate 	while (*b++)
11627c478bd9Sstevel@tonic-gate 		;
11637c478bd9Sstevel@tonic-gate 	b--;
11647c478bd9Sstevel@tonic-gate 	while (b > ob && pbp < &pbbuf[NC-3])
11657c478bd9Sstevel@tonic-gate 		*pbp++ = *--b;
11667c478bd9Sstevel@tonic-gate 	if (pbp >= &pbbuf[NC-3]) {
11677c478bd9Sstevel@tonic-gate 		errprint(gettext("cpushback overflow"));
11687c478bd9Sstevel@tonic-gate 		done(2);
11697c478bd9Sstevel@tonic-gate 	}
1170e5190c10Smuffin 
1171e5190c10Smuffin 	return (0);
11727c478bd9Sstevel@tonic-gate }
11737c478bd9Sstevel@tonic-gate 
1174e5190c10Smuffin int
nextfile()11757c478bd9Sstevel@tonic-gate nextfile()
11767c478bd9Sstevel@tonic-gate {
1177e5190c10Smuffin 	char	*p;
11787c478bd9Sstevel@tonic-gate 
11797c478bd9Sstevel@tonic-gate n0:
11807c478bd9Sstevel@tonic-gate 	if (ifile)
11817c478bd9Sstevel@tonic-gate 		close(ifile);
11827c478bd9Sstevel@tonic-gate 	if (nx  ||  nmfi < mflg) {
11837c478bd9Sstevel@tonic-gate 		p = mfiles[nmfi++];
11847c478bd9Sstevel@tonic-gate 		if (*p != 0)
11857c478bd9Sstevel@tonic-gate 			goto n1;
11867c478bd9Sstevel@tonic-gate 	}
11877c478bd9Sstevel@tonic-gate 	if (ifi > 0) {
11887c478bd9Sstevel@tonic-gate 		if (popf())
11897c478bd9Sstevel@tonic-gate 			goto n0; /* popf error */
11907c478bd9Sstevel@tonic-gate 		return(1); /* popf ok */
11917c478bd9Sstevel@tonic-gate 	}
11927c478bd9Sstevel@tonic-gate 	if (rargc-- <= 0) {
11937c478bd9Sstevel@tonic-gate 		if ((nfo -= mflg) && !stdi)
11947c478bd9Sstevel@tonic-gate 			done(0);
11957c478bd9Sstevel@tonic-gate 		nfo++;
11967c478bd9Sstevel@tonic-gate 		numtab[CD].val = ifile = stdi = mflg = 0;
11977c478bd9Sstevel@tonic-gate 		strcpy(cfname[ifi], "<standard input>");
11987c478bd9Sstevel@tonic-gate 		ioff = 0;
11997c478bd9Sstevel@tonic-gate 		return(0);
12007c478bd9Sstevel@tonic-gate 	}
12017c478bd9Sstevel@tonic-gate 	p = (argp++)[0];
12027c478bd9Sstevel@tonic-gate n1:
12037c478bd9Sstevel@tonic-gate 	numtab[CD].val = 0;
12047c478bd9Sstevel@tonic-gate 	if (p[0] == '-' && p[1] == 0) {
12057c478bd9Sstevel@tonic-gate 		ifile = 0;
12067c478bd9Sstevel@tonic-gate 		strcpy(cfname[ifi], "<standard input>");
12077c478bd9Sstevel@tonic-gate 	} else if ((ifile = open(p, 0)) < 0) {
12087c478bd9Sstevel@tonic-gate 		errprint(gettext("cannot open file %s"), p);
12097c478bd9Sstevel@tonic-gate 		nfo -= mflg;
12107c478bd9Sstevel@tonic-gate 		done(02);
12117c478bd9Sstevel@tonic-gate 	} else
12127c478bd9Sstevel@tonic-gate 		strcpy(cfname[ifi],p);
12137c478bd9Sstevel@tonic-gate 	nfo++;
12147c478bd9Sstevel@tonic-gate 	ioff = 0;
12157c478bd