xref: /csrg-svn/old/roff/common_source/n1.c (revision 46851)
17063Srrh #ifndef lint
2*46851Scael static char sccsid[] = "@(#)n1.c	4.11 03/01/91";
37063Srrh #endif lint
47063Srrh 
57171Smckusick #include "tdef.h"
642856Skarels #include "pathnames.h"
713262Srrh #include <sys/types.h>
87063Srrh #include <sys/stat.h>
930776Sbostic #include <time.h>
107063Srrh extern
117063Srrh #include "d.h"
127063Srrh extern
137063Srrh #include "v.h"
147063Srrh #ifdef NROFF
157063Srrh extern
167063Srrh #include "tw.h"
177063Srrh #endif
187063Srrh #include "sdef.h"
197063Srrh #include <setjmp.h>
207063Srrh jmp_buf sjbuf;
217063Srrh #include	<sgtty.h>
227063Srrh /*
237063Srrh troff1.c
247063Srrh 
257063Srrh consume options, initialization, main loop,
267063Srrh input routines, escape function calling
277063Srrh */
287063Srrh 
297063Srrh int	inchar[LNSIZE], *pinchar = inchar;	/* XXX */
307063Srrh extern struct s *frame, *stk, *nxf;
317063Srrh extern struct s *ejl, *litlev;
327063Srrh extern filep ip;
337063Srrh extern filep offset;
347063Srrh extern filep nextb;
357063Srrh 
367063Srrh 
377063Srrh extern int stdi;
387063Srrh extern int waitf;
397063Srrh extern int nofeed;
407063Srrh extern int quiet;
417063Srrh extern int ptid;
427063Srrh extern int ascii;
437063Srrh extern int npn;
447063Srrh extern int xflg;
457063Srrh extern int stop;
467063Srrh extern char ibuf[IBUFSZ];
477063Srrh extern char xbuf[IBUFSZ];
487063Srrh extern char *ibufp;
497063Srrh extern char *xbufp;
507063Srrh extern char *eibuf;
517063Srrh extern char *xeibuf;
527063Srrh extern int cbuf[NC];
537063Srrh extern int *cp;
547063Srrh extern int *vlist;
557063Srrh extern int nx;
567063Srrh extern int mflg;
577063Srrh extern int ch;
587063Srrh extern int pto;
597063Srrh extern int pfrom;
607063Srrh extern int cps;
617063Srrh extern int chbits;
627063Srrh extern int ibf;
637063Srrh extern int ttyod;
647063Srrh extern struct sgttyb ttys;
657063Srrh extern int iflg;
667063Srrh extern int init;
677063Srrh extern int rargc;
687063Srrh extern char **argp;
697063Srrh extern char trtab[256];
707063Srrh extern int lgf;
717063Srrh extern int copyf;
727063Srrh extern int eschar;
737063Srrh extern int ch0;
747063Srrh extern int cwidth;
757063Srrh extern int nlflg;
767063Srrh extern int *ap;
777063Srrh extern int donef;
787063Srrh extern int nflush;
797063Srrh extern int nchar;
807063Srrh extern int rchar;
817063Srrh extern int nfo;
827063Srrh extern int ifile;
837063Srrh extern int fc;
847063Srrh extern int padc;
857063Srrh extern int tabc;
867063Srrh extern int dotc;
877063Srrh extern int raw;
887063Srrh extern int tabtab[NTAB];
897063Srrh extern char nextf[];
907063Srrh extern int nfi;
917063Srrh #ifdef NROFF
927063Srrh extern char termtab[];
937063Srrh extern int tti;
947063Srrh #endif
957063Srrh extern int ifl[NSO];
967063Srrh extern int ifi;
977063Srrh extern int pendt;
987063Srrh extern int flss;
997063Srrh extern int fi;
1007063Srrh extern int lg;
1017063Srrh extern char ptname[];
1027063Srrh extern int print;
1037063Srrh extern int nonumb;
1047063Srrh extern int pnlist[];
1057063Srrh extern int *pnp;
1067063Srrh extern int nb;
1077063Srrh extern int trap;
1087063Srrh extern int tflg;
1097063Srrh extern int ejf;
1107063Srrh extern int lit;
1117063Srrh extern int cc;
1127063Srrh extern int c2;
1137063Srrh extern int spread;
1147063Srrh extern int gflag;
1157063Srrh extern int oline[];
1167063Srrh extern int *olinep;
1177063Srrh extern int dpn;
1187063Srrh extern int noscale;
1197063Srrh extern char *unlkp;
1207063Srrh extern int pts;
1217063Srrh extern int level;
1227063Srrh extern int ttysave;
1237063Srrh extern int tdelim;
1247063Srrh extern int dotT;
1257063Srrh extern int tabch, ldrch;
1267063Srrh extern int eqflg;
1277063Srrh extern no_out;
1287063Srrh extern int hflg;
1297063Srrh #ifndef NROFF
1307063Srrh extern char codetab[];
1317063Srrh extern int spbits;
1327063Srrh #endif
1337063Srrh extern int xxx;
1347063Srrh int stopmesg;
1357063Srrh filep ipl[NSO];
1367063Srrh long offl[NSO];
1377063Srrh long ioff;
1387063Srrh char *ttyp;
1397063Srrh extern struct contab {
1407063Srrh 	int rq;
1417063Srrh 	union {
1427063Srrh 		int (*f)();
1437063Srrh 		unsigned mx;
1447063Srrh 	}x;
1457063Srrh }contab[NM];
1467063Srrh int ms[] = {31,28,31,30,31,30,31,31,30,31,30,31};
1477063Srrh #ifndef NROFF
1487063Srrh int acctf;
1497063Srrh #endif
1507063Srrh 
1517063Srrh main(argc,argv)
1527063Srrh int argc;
1537063Srrh char **argv;
1547063Srrh {
1557063Srrh 	char *p, *q;
1567063Srrh 	register i, j;
1577063Srrh 	extern catch(), fpecatch(), kcatch();
1587063Srrh 
1597063Srrh 	signal(SIGHUP,catch);
1607063Srrh 	if(signal(SIGINT,catch) == SIG_IGN){
1617063Srrh 		signal(SIGHUP,SIG_IGN);
1627063Srrh 		signal(SIGINT,SIG_IGN);
1637063Srrh 		signal(SIGQUIT,SIG_IGN);
1647063Srrh 	}
1657063Srrh 	signal(SIGFPE,fpecatch);
1667063Srrh 	signal(SIGPIPE,catch);
1677063Srrh 	signal(SIGTERM,kcatch);
1687063Srrh 	init1(argv[0][0]);
1697063Srrh options:
1707063Srrh 	while(--argc > 0 && (++argv)[0][0]=='-')
1717063Srrh 		switch(argv[0][1]){
1727063Srrh 
1737063Srrh 		case 0:
1747063Srrh 			goto start;
1757063Srrh 		case 'i':
1767063Srrh 			stdi++;
1777063Srrh 			continue;
1787063Srrh 		case 'q':
1797063Srrh 			quiet++;
1807063Srrh 			if(gtty(0, &ttys) >= 0)
1817063Srrh 				ttysave = ttys.sg_flags;
1827063Srrh 			continue;
1837063Srrh 		case 'n':
1847063Srrh 			npn = cnum(&argv[0][2]);
1857063Srrh 			continue;
1867063Srrh 		case 'p':
1877063Srrh 			xflg = 0;
1887063Srrh 			cps = cnum(&argv[0][2]);
1897063Srrh 			continue;
1907063Srrh 		case 'S':
1917063Srrh 			stopmesg++;
1927063Srrh 			continue;
1937063Srrh 		case 's':
1947063Srrh 			if(!(stop = cnum(&argv[0][2])))stop++;
1957063Srrh 			continue;
1967063Srrh 		case 'r':
1977063Srrh 			vlist[findr(argv[0][2])] = cnum(&argv[0][3]);
1987063Srrh 			continue;
1997063Srrh 		case 'm':
2007063Srrh 			p = &nextf[nfi];
2017063Srrh 			q = &argv[0][2];
2027063Srrh 			while((*p++ = *q++) != 0);
2037063Srrh 			if (access(nextf, 4) < 0) {
20442856Skarels 				char local[NS];
20542856Skarels 
20642856Skarels 				strcpy(local, _PATH_LOCAL_TMAC);
2077063Srrh 				strcat(local, &argv[0][2]);
2087063Srrh 				if (access(local, 4) == 0)
2097063Srrh 					strcpy(nextf, local);
2107063Srrh 			}
2117063Srrh 			mflg++;
2127063Srrh 			continue;
2137063Srrh 		case 'o':
2147063Srrh 			getpn(&argv[0][2]);
2157063Srrh 			continue;
2167063Srrh #ifdef NROFF
2177063Srrh 		case 'h':
2187063Srrh 			hflg++;
2197063Srrh 			continue;
2207063Srrh 		case 'z':
2217063Srrh 			no_out++;
2227063Srrh 			continue;
2237063Srrh 		case 'e':
2247063Srrh 			eqflg++;
2257063Srrh 			continue;
2267063Srrh 		case 'T':
2277063Srrh 			p = &termtab[tti];
2287063Srrh 			q = &argv[0][2];
2297063Srrh 			if(!((*q) & 0177))continue;
2307063Srrh 			while((*p++ = *q++) != 0);
2317063Srrh 			dotT++;
2327063Srrh 			continue;
2337063Srrh #endif
2347063Srrh #ifndef NROFF
2357063Srrh 		case 'z':
2367063Srrh 			no_out++;
2377063Srrh 		case 'a':
2387063Srrh 			ascii = 1;
2397063Srrh 			nofeed++;
2407063Srrh 		case 't':
2417063Srrh 			ptid = 1;
2427063Srrh 			continue;
2437063Srrh 		case 'w':
2447063Srrh 			waitf = 1;
2457063Srrh 			continue;
2467063Srrh 		case 'f':
2477063Srrh 			nofeed++;
2487063Srrh 			continue;
2497063Srrh 		case 'x':
2507063Srrh 			xflg = 0;
2517063Srrh 			continue;
2527063Srrh 		case 'b':
2537063Srrh 			if(open(ptname,1) < 0)prstr("Busy.\n");
2547063Srrh 			else prstr("Available.\n");
2557063Srrh 			done3(0);
2567063Srrh 		case 'g':
2577063Srrh 			stop = ptid = gflag = 1;
2587063Srrh 			dpn = 0;
2597063Srrh 			continue;
26011247Sshannon 		case 'F':
26111247Sshannon 			{
26211247Sshannon 			  extern char *fontfile;
26311247Sshannon 			  fontfile = &argv[0][2];
26411247Sshannon 			}
26511247Sshannon 			continue;
2667063Srrh #endif
2677063Srrh 		default:
2687063Srrh 			pto = cnum(&argv[0][1]);
2697063Srrh 			continue;
2707063Srrh 		}
2717063Srrh 
2727063Srrh 	if(argv[0][0] == '+'){
2737063Srrh 		pfrom = cnum(&argv[0][1]);
2747063Srrh 		print = 0;
2757063Srrh 		if(argc > 0)goto options;
2767063Srrh 	}
2777063Srrh 
2787063Srrh start:
2797063Srrh 	argp = argv;
2807063Srrh 	rargc = argc;
2817063Srrh 	init2();
2827063Srrh 	setjmp(sjbuf);
2837063Srrh loop:
2847063Srrh 	copyf = lgf = nb = nflush = nlflg = 0;
2857063Srrh 	if(ip && (rbf0(ip)==0) && ejf && (frame->pframe <= ejl)){
2867063Srrh 		nflush++;
2877063Srrh 		trap = 0;
2887063Srrh 		eject((struct s *)0);
2897063Srrh 		goto loop;
2907063Srrh 	}
2917063Srrh 	i = getch();
2927063Srrh 	if(pendt)goto lt;
2937063Srrh 	if(lit && (frame <= litlev)){
2947063Srrh 		lit--;
2957063Srrh 		goto lt;
2967063Srrh 	}
2977063Srrh 	if((j = (i & CMASK)) == XPAR){
2987063Srrh 		copyf++;
2997063Srrh 		tflg++;
3007063Srrh 		for(;(i & CMASK) != '\n';)pchar(i = getch());
3017063Srrh 		tflg = 0;
3027063Srrh 		copyf--;
3037063Srrh 		goto loop;
3047063Srrh 	}
3057063Srrh 	if((j == cc) || (j == c2)){
3067063Srrh 		if(j == c2)nb++;
3077063Srrh 		copyf++;
3087063Srrh 		while(((j=((i=getch()) & CMASK)) == ' ') ||
3097063Srrh 			(j == '\t'));
3107063Srrh 		ch = i;
3117063Srrh 		copyf--;
3127063Srrh 		control(getrq(),1);
3137063Srrh 		flushi();
3147063Srrh 		goto loop;
3157063Srrh 	}
3167063Srrh lt:
3177063Srrh 	ch = i;
3187063Srrh 	text();
3197063Srrh 	goto loop;
3207063Srrh }
3217063Srrh catch(){
3227063Srrh /*
3237063Srrh 	prstr("Interrupt\n");
3247063Srrh */
3257063Srrh 	done3(01);
3267063Srrh }
3277063Srrh fpecatch(){
3287063Srrh 	prstrfl("Floating Exception.\n");
3297063Srrh 	signal(SIGFPE,fpecatch);
3307063Srrh }
3317063Srrh kcatch(){
3327063Srrh 	signal(SIGTERM,SIG_IGN);
3337063Srrh 	done3(01);
3347063Srrh }
3357063Srrh #ifndef NROFF
3367063Srrh acctg() {
337*46851Scael 	static char *acct_file = _PATH_TRACCT;
3387063Srrh 	acctf = open(acct_file,1);
3397063Srrh 	setuid(getuid());
3407063Srrh }
3417063Srrh #endif
3427063Srrh init1(a)
3437063Srrh char a;
3447063Srrh {
3457063Srrh 	register char *p;
3467063Srrh 	char *mktemp();
34735269Sbostic 	static char tempname[] = "/tmp/taXXXXX";
3487063Srrh 	register i;
3497063Srrh 
3507063Srrh #ifndef NROFF
3517063Srrh 	acctg();/*open troff actg file while mode 4755*/
3527063Srrh #endif
35335269Sbostic 	p = mktemp(tempname);
3547063Srrh 	if(a == 'a')p = &p[5];
3557063Srrh 	if((close(creat(p, 0600))) < 0){
3567063Srrh 		prstr("Cannot create temp file.\n");
3577063Srrh 		exit(-1);
3587063Srrh 	}
3597063Srrh 	ibf = open(p, 2);
3607063Srrh 	for(i=256; --i;)trtab[i]=i;
3617063Srrh 	trtab[UNPAD] = ' ';
3627063Srrh 	mchbits();
3637063Srrh 	if(a != 'a')unlkp = p;
3647063Srrh }
3657063Srrh init2()
3667063Srrh {
3677063Srrh 	register i,j;
3687063Srrh 	extern int block;
3697063Srrh 	extern char *setbrk();
3707063Srrh 	extern char *ttyname();
3717063Srrh 
3727063Srrh 	ttyod = 2;
3737063Srrh 	if(((ttyp=ttyname(j=0)) != (char *)0) ||
3747063Srrh 	   ((ttyp=ttyname(j=1)) != (char *)0) ||
3757063Srrh 	   ((ttyp=ttyname(j=2)) != (char *)0)
3767063Srrh 	  );else ttyp = "notty";
3777063Srrh 	iflg = j;
3787063Srrh 	if(ascii)mesg(0);
3797063Srrh 
3807063Srrh 	if((!ptid) && (!waitf)){
3817063Srrh 		if((ptid = open(ptname,1)) < 0){
3827063Srrh 			prstr("Typesetter busy.\n");
3837063Srrh 			done3(-2);
3847063Srrh 		}
3857063Srrh 	}
3867063Srrh 	ptinit();
3877063Srrh 	for(i=NEV; i--;)write(ibf, (char *)&block, EVS*sizeof(int));
3887063Srrh 	olinep = oline;
3897063Srrh 	ibufp = eibuf = ibuf;
3907063Srrh 	v.hp = init = 0;
3917063Srrh 	pinchar = inchar;	/* XXX */
3927063Srrh 	ioff = 0;
3937063Srrh 	v.nl = -1;
3947063Srrh 	cvtime();
3957063Srrh 	frame = stk = (struct s *)setbrk(DELTA);
3967063Srrh 	dip = &d[0];
3977063Srrh 	nxf = frame + 1;
3987063Srrh 	nx = mflg;
3997063Srrh }
40030776Sbostic cvtime()
40130776Sbostic {
40230776Sbostic 	extern time_t time();
40330776Sbostic 	time_t t;
40430776Sbostic 	register struct tm *tmp;
4057063Srrh 
40630776Sbostic 	t = time((time_t *)0);
40730776Sbostic 	tmp = localtime(&t);
40830776Sbostic 	v.dy = tmp->tm_mday;
40930776Sbostic 	v.dw = tmp->tm_wday + 1;
41030776Sbostic 	v.yr = tmp->tm_year;
41130776Sbostic 	v.mo = tmp->tm_mon + 1;
4127063Srrh }
4137063Srrh cnum(a)
4147063Srrh char *a;
4157063Srrh {
4167063Srrh 	register i;
4177063Srrh 
4187063Srrh 	ibufp = a;
41917673Sralph 	eibuf = (char *) MAXPTR;
4207063Srrh 	i = atoi();
4217063Srrh 	ch = 0;
4227063Srrh 	return(i);
4237063Srrh }
4247063Srrh mesg(f)
4257063Srrh int f;
4267063Srrh {
4277063Srrh 	static int mode;
4287063Srrh 
4297063Srrh 	if (ttyp==0)
4307063Srrh 		return;
4317063Srrh 	if(!f){
4327063Srrh 		stat(ttyp,cbuf);
4337063Srrh 		mode = ((struct stat *)(cbuf))->st_mode;
4347063Srrh 		chmod(ttyp,mode & ~022);
4357063Srrh 	}else{
4367063Srrh 		chmod(ttyp,mode);
4377063Srrh 	}
4387063Srrh }
4397063Srrh prstrfl(s)
4407063Srrh char *s;
4417063Srrh {
4427063Srrh 	flusho();
4437063Srrh 	prstr(s);
4447063Srrh }
4457063Srrh prstr(s)
4467063Srrh char *s;
4477063Srrh {
4487063Srrh 	register i;
4497063Srrh 	register char *j;
4507063Srrh 
4517063Srrh 	j = s;
4527063Srrh 	for(i=0;*s;i++)s++;
4537063Srrh 	write(ttyod,j,i);
4547063Srrh }
4557063Srrh control(a,b)
4567063Srrh int a,b;
4577063Srrh {
4587063Srrh 	register i,j;
4597063Srrh 	extern filep boff();
4607063Srrh 
4617063Srrh 	i = a;
4627063Srrh 	if((i == 0) || ((j = findmn(i)) == -1))return(0);
4637063Srrh 	if(contab[j].rq & MMASK){
4647063Srrh 		nxf->nargs = 0;
4657063Srrh 		if(b)collect();
4667063Srrh 		flushi();
4677063Srrh 		return(pushi(((filep)contab[j].x.mx)<<BLKBITS));
4687063Srrh 	}else{
4697063Srrh 		if(!b)return(0);
4707063Srrh 		return((*contab[j].x.f)(0));
4717063Srrh 	}
4727063Srrh }
4737063Srrh 
4747063Srrh getrq(){
4757063Srrh 	register i,j;
4767063Srrh 
4777063Srrh 	if(((i=getach()) == 0) ||
4787063Srrh 	   ((j=getach()) == 0))goto rtn;
4797063Srrh 	i = PAIR(i,j);
4807063Srrh rtn:
4817063Srrh 	return(i);
4827063Srrh }
4837063Srrh getch(){
4847063Srrh 	register int i, j, k;
4857063Srrh 
4867063Srrh 	level++;
4877063Srrh g0:
4887063Srrh 	if(ch){
4897063Srrh 		if(((i = ch) & CMASK) == '\n')nlflg++;
4907063Srrh 		ch = 0;
4917063Srrh 		level--;
4927063Srrh 		return(i);
4937063Srrh 	}
4947063Srrh 
4957063Srrh 	if(nlflg){
4967063Srrh 		level--;
4977063Srrh 		return('\n');
4987063Srrh 	}
4997063Srrh 
5007063Srrh 	if((k = (i = getch0()) & CMASK) != ESC){
5017063Srrh 		if(i & MOT)goto g2;
5027063Srrh 		if(k == FLSS){
5037063Srrh 			copyf++; raw++;
5047063Srrh 			i = getch0();
5057063Srrh 			if(!fi)flss = i;
5067063Srrh 			copyf--; raw--;
5077063Srrh 			goto g0;
5087063Srrh 		}
5097063Srrh 		if(k == RPT){
5107063Srrh 			setrpt();
5117063Srrh 			goto g0;
5127063Srrh 		}
5137063Srrh 		if(!copyf){
5147063Srrh 			if((k == 'f') && lg && !lgf){
5157063Srrh 				i = getlg(i);
5167063Srrh 				goto g2;
5177063Srrh 			}
5187063Srrh 			if((k == fc) || (k == tabch) || (k == ldrch)){
5197063Srrh 				if((i=setfield(k)) == 0)goto g0; else goto g2;
5207063Srrh 			}
5217063Srrh 			if(k == 010){
5227063Srrh 				i = makem(-width(' ' | chbits));
5237063Srrh 				goto g2;
5247063Srrh 			}
5257063Srrh 		}
5267063Srrh 		goto g2;
5277063Srrh 	}
5287063Srrh 	k = (j = getch0()) & CMASK;
5297063Srrh 	if(j & MOT){
5307063Srrh 		i = j;
5317063Srrh 		goto g2;
5327063Srrh 	}
5337063Srrh /*
5347063Srrh 	if(k == tdelim){
5357063Srrh 		i = TDELIM;
5367063Srrh 		tdelim = IMP;
5377063Srrh 		goto g2;
5387063Srrh 	}
5397063Srrh */
5407063Srrh 	switch(k){
5417063Srrh 
5427063Srrh 		case '\n':	/*concealed newline*/
5437063Srrh 			goto g0;
5447063Srrh 		case 'n':	/*number register*/
5457063Srrh 			setn();
5467063Srrh 			goto g0;
5477063Srrh 		case '*':	/*string indicator*/
5487063Srrh 			setstr();
5497063Srrh 			goto g0;
5507063Srrh 		case '$':	/*argument indicator*/
5517063Srrh 			seta();
5527063Srrh 			goto g0;
5537063Srrh 		case '{':	/*LEFT*/
5547063Srrh 			i = LEFT;
5557063Srrh 			goto gx;
5567063Srrh 		case '}':	/*RIGHT*/
5577063Srrh 			i = RIGHT;
5587063Srrh 			goto gx;
5597063Srrh 		case '"':	/*comment*/
5607063Srrh 			while(((i=getch0()) & CMASK ) != '\n');
5617063Srrh 			goto g2;
5627063Srrh 		case ESC:	/*double backslash*/
5637063Srrh 			i = eschar;
5647063Srrh 			goto gx;
5657063Srrh 		case 'e':	/*printable version of current eschar*/
5667063Srrh 			i = PRESC;
5677063Srrh 			goto gx;
5687063Srrh 		case ' ':	/*unpaddable space*/
5697063Srrh 			i = UNPAD;
5707063Srrh 			goto gx;
5717063Srrh 		case '|':	/*narrow space*/
5727063Srrh 			i = NARSP;
5737063Srrh 			goto gx;
5747063Srrh 		case '^':	/*half of narrow space*/
5757063Srrh 			i = HNSP;
5767063Srrh 			goto gx;
5777063Srrh 		case '\'':	/*\(aa*/
5787063Srrh 			i = 0222;
5797063Srrh 			goto gx;
5807063Srrh 		case '`':	/*\(ga*/
5817063Srrh 			i = 0223;
5827063Srrh 			goto gx;
5837063Srrh 		case '_':	/*\(ul*/
5847063Srrh 			i = 0224;
5857063Srrh 			goto gx;
5867063Srrh 		case '-':	/*current font minus*/
5877063Srrh 			i = 0210;
5887063Srrh 			goto gx;
5897063Srrh 		case '&':	/*filler*/
5907063Srrh 			i = FILLER;
5917063Srrh 			goto gx;
5927063Srrh 		case 'c':	/*to be continued*/
5937063Srrh 			i = CONT;
5947063Srrh 			goto gx;
5957063Srrh 		case ':':	/*lem's char*/
5967063Srrh 			i = COLON;
5977063Srrh 			goto gx;
5987063Srrh 		case '!':	/*transparent indicator*/
5997063Srrh 			i = XPAR;
6007063Srrh 			goto gx;
6017063Srrh 		case 't':	/*tab*/
6027063Srrh 			i = '\t';
6037063Srrh 			goto g2;
6047063Srrh 		case 'a':	/*leader (SOH)*/
6057063Srrh 			i = LEADER;
6067063Srrh 			goto g2;
6077063Srrh 		case '%':	/*ohc*/
6087063Srrh 			i = OHC;
6097063Srrh 			goto g2;
6107063Srrh 		case '.':	/*.*/
6117063Srrh 			i = '.';
6127063Srrh 		gx:
6137063Srrh 			i = (j & ~CMASK) | i;
6147063Srrh 			goto g2;
6157063Srrh 	}
6167063Srrh 	if(!copyf)
6177063Srrh 		switch(k){
6187063Srrh 
6197063Srrh 			case 'p':	/*spread*/
6207063Srrh 				spread++;
6217063Srrh 				goto g0;
6227063Srrh 			case '(':	/*special char name*/
6237063Srrh 				if((i=setch()) == 0)goto g0;
6247063Srrh 				break;
6257063Srrh 			case 's':	/*size indicator*/
6267063Srrh 				setps();
6277063Srrh 				goto g0;
6287063Srrh 			case 'f':	/*font indicator*/
6297063Srrh 				setfont(0);
6307063Srrh 				goto g0;
6317063Srrh 			case 'w':	/*width function*/
6327063Srrh 				setwd();
6337063Srrh 				goto g0;
6347063Srrh 			case 'v':	/*vert mot*/
6357063Srrh 				if(i = vmot())break;
6367063Srrh 				goto g0;
6377063Srrh 			case 'h': 	/*horiz mot*/
6387063Srrh 				if(i = hmot())break;
6397063Srrh 				goto g0;
6407063Srrh 			case 'z':	/*zero with char*/
6417063Srrh 				i = setz();
6427063Srrh 				break;
6437063Srrh 			case 'l':	/*hor line*/
6447063Srrh 				setline();
6457063Srrh 				goto g0;
6467063Srrh 			case 'L':	/*vert line*/
6477063Srrh 				setvline();
6487063Srrh 				goto g0;
6497063Srrh 			case 'b':	/*bracket*/
6507063Srrh 				setbra();
6517063Srrh 				goto g0;
6527063Srrh 			case 'o':	/*overstrike*/
6537063Srrh 				setov();
6547063Srrh 				goto g0;
6557063Srrh 			case 'k':	/*mark hor place*/
6567063Srrh 				if((i=findr(getsn())) == -1)goto g0;
6577063Srrh 				vlist[i] = v.hp = sumhp();	/* XXX */
6587063Srrh 				goto g0;
6597063Srrh 			case 'j':	/*mark output hor place*/
6607063Srrh 				if(!(i=getach()))goto g0;
6617063Srrh 				i = (i<<BYTE) | JREG;
6627063Srrh 				break;
6637063Srrh 			case '0':	/*number space*/
6647063Srrh 				i = makem(width('0' | chbits));
6657063Srrh 				break;
6667063Srrh 			case 'x':	/*extra line space*/
6677063Srrh 				if(i = xlss())break;
6687063Srrh 				goto g0;
6697063Srrh 			case 'u':	/*half em up*/
6707063Srrh 			case 'r':	/*full em up*/
6717063Srrh 			case 'd':	/*half em down*/
6727063Srrh 				i = sethl(k);
6737063Srrh 				break;
6747063Srrh 			default:
6757063Srrh 				i = j;
6767063Srrh 		}
6777063Srrh 	else{
6787063Srrh 		ch0 = j;
6797063Srrh 		i = eschar;
6807063Srrh 	}
6817063Srrh g2:
6827063Srrh 	if((i & CMASK) == '\n'){
6837063Srrh 		nlflg++;
6847063Srrh 		v.hp = 0;
6857063Srrh 		pinchar = inchar;	/* XXX */
6867063Srrh 		if(ip == 0)v.cd++;
6877063Srrh 	}
6887063Srrh 	if(!--level){
6897063Srrh 		/* j = width(i); */
6907063Srrh 		/* v.hp += j; */
6917063Srrh 		/* cwidth = j; */
6927063Srrh 		if (pinchar >= inchar + LNSIZE) {	/* XXX */
6937063Srrh 			inchar[0] = makem(sumhp());
6947063Srrh 			pinchar = &inchar[1];
6957063Srrh 		}
6967063Srrh 		*pinchar++ = i;	/* XXX */
6977063Srrh 	}
6987063Srrh 	return(i);
6997063Srrh }
7007063Srrh 
7017063Srrh sumhp()	/* XXX - add up widths in inchar array */
7027063Srrh {
7037063Srrh 	register int n;
7047063Srrh 	register int *p;
7057063Srrh 
7067063Srrh 	n = 0;
7077063Srrh 	for (p = inchar; p < pinchar; p++)
7087063Srrh 		n += width(*p);
7097063Srrh 	return(n);
7107063Srrh }
7117063Srrh char ifilt[32] = {0,001,002,003,0,005,006,007,010,011,012};
7127063Srrh getch0(){
7137063Srrh 	register int i, j;
7147063Srrh 
7157063Srrh 	if(ch0){i=ch0; ch0=0; return(i);}
7167063Srrh 	if(nchar){nchar--; return(rchar);}
7177063Srrh 
7187063Srrh again:
7197063Srrh 	if(cp){
7207063Srrh 		if((i = *cp++) == 0){
7217063Srrh 			cp = 0;
7227063Srrh 			goto again;
7237063Srrh 		}
7247063Srrh 	}else if(ap){
7257063Srrh 		if((i = *ap++) == 0){
7267063Srrh 			ap = 0;
7277063Srrh 			goto again;
7287063Srrh 		}
7297063Srrh 	}else if(ip){
7307063Srrh 		if(ip == -1)i = rdtty();
7317063Srrh 		else i = rbf();
7327063Srrh 	}else{
7337063Srrh 		if(donef)done(0);
73431915Sbostic 		if(nx || ((ibufp >= eibuf) && (eibuf != (char *) MAXPTR))){
7357063Srrh 			if(nfo)goto g1;
7367063Srrh 		g0:
7377063Srrh 			if(nextfile()){
7387063Srrh 				if(ip)goto again;
7397063Srrh 				if(ibufp < eibuf)goto g2;
7407063Srrh 			}
7417063Srrh 		g1:
7427063Srrh 			nx = 0;
7437063Srrh 			if((j=read(ifile,ibuf,IBUFSZ)) <= 0)goto g0;
7447063Srrh 			ibufp = ibuf;
7457063Srrh 			eibuf = ibuf + j;
7467063Srrh 			if(ip)goto again;
7477063Srrh 		}
7487063Srrh 	g2:
7497063Srrh 		i = *ibufp++ & 0177;
7507063Srrh 		ioff++;
7517063Srrh 		if(i >= 040)goto g4; else i = ifilt[i];
7527063Srrh 	}
7537063Srrh 	if(raw)return(i);
7547063Srrh 	if((j = i & CMASK) == IMP)goto again;
7557063Srrh 	if((i == 0) && !init)goto again;
7567063Srrh g4:
7577063Srrh 	if((copyf == 0) && ((i & ~BMASK) == 0) && ((i & CMASK) < 0370))
7587063Srrh #ifndef NROFF
7597063Srrh 		if(spbits && (i>31) && ((codetab[i-32] & 0200))) i |= spbits;
7607063Srrh 		else
7617063Srrh #endif
7627063Srrh 		i |= chbits;
7637063Srrh 	if((i & CMASK) == eschar)i = (i & ~CMASK) | ESC;
7647063Srrh 	return(i);
7657063Srrh }
7667063Srrh nextfile(){
7677063Srrh 	register char *p;
7687063Srrh 
7697063Srrh n0:
7707063Srrh 	if(ifile)close(ifile);
7717063Srrh 	if(nx){
7727063Srrh 		p = nextf;
7737063Srrh 		if(*p != 0)goto n1;
7747063Srrh 	}
7757063Srrh 	if(ifi > 0){
7767063Srrh 		if(popf())goto n0; /*popf error*/
7777063Srrh 		return(1); /*popf ok*/
7787063Srrh 	}
7797063Srrh 	if(rargc-- <= 0)goto n2;
7807063Srrh 	p = (argp++)[0];
7817063Srrh n1:
7827063Srrh 	if((p[0] == '-') && (p[1] == 0)){
7837063Srrh 		ifile = 0;
7847063Srrh 	}else if((ifile=open(p,0)) < 0){
7857063Srrh 		prstr("Cannot open ");
7867063Srrh 		prstr(p);
7877063Srrh 		prstr("\n");
7887063Srrh 		nfo -= mflg;
7897063Srrh 		done(02);
7907063Srrh 	}
7917063Srrh 	nfo++;
7927063Srrh 	v.cd = 0;
7937063Srrh 	ioff = 0;
7947063Srrh 	return(0);
7957063Srrh n2:
7967063Srrh 	if((nfo -= mflg) && !stdi)done(0);
7977063Srrh 	nfo++;
7987063Srrh 	v.cd = ifile =  stdi = mflg = 0;
7997063Srrh 	ioff = 0;
8007063Srrh 	return(0);
8017063Srrh }
8027063Srrh popf(){
8037063Srrh 	register i;
8047063Srrh 	register char *p, *q;
8057063Srrh 	extern char *ttyname();
8067063Srrh 
8077063Srrh 	ioff = offl[--ifi];
8087063Srrh 	ip = ipl[ifi];
8097063Srrh 	if((ifile = ifl[ifi]) == 0){
8107063Srrh 		p = xbuf;
8117063Srrh 		q = ibuf;
8127063Srrh 		ibufp = xbufp;
8137063Srrh 		eibuf = xeibuf;
8147063Srrh 		while(q < eibuf)*q++ = *p++;
8157063Srrh 		return(0);
8167063Srrh 	}
8177063Srrh 	if((lseek(ifile,(long)(ioff & ~(IBUFSZ-1)),0) < 0) ||
8187063Srrh 	   ((i = read(ifile,ibuf,IBUFSZ)) < 0))return(1);
8197063Srrh 	eibuf = ibuf + i;
8207063Srrh 	ibufp = ibuf;
8217063Srrh 	if(ttyname(ifile) == (char *)0)
8227063Srrh 		if((ibufp = ibuf + (int)(ioff & (IBUFSZ-1)))  >= eibuf)return(1);
8237063Srrh 	return(0);
8247063Srrh }
8257063Srrh flushi(){
8267063Srrh 	if(nflush)return;
8277063Srrh 	ch = 0;
8287063Srrh 	if((ch0 & CMASK) == '\n')nlflg++;
8297063Srrh 	ch0 = 0;
8307063Srrh 	copyf++;
8317063Srrh 	while(!nlflg){
8327063Srrh 		if(donef && (frame == stk))break;
8337063Srrh 		getch();
8347063Srrh 	}
8357063Srrh 	copyf--;
8367063Srrh 	v.hp = 0;
8377063Srrh 	pinchar = inchar;	/* XXX */
8387063Srrh }
8397063Srrh getach(){
8407063Srrh 	register i;
8417063Srrh 
8427063Srrh 	lgf++;
8437063Srrh 	if(((i = getch()) & MOT) ||
8447063Srrh 	    ((i&CMASK) == ' ') ||
8457063Srrh 	    ((i&CMASK) == '\n')||
8467063Srrh 	    (i & 0200)){
8477063Srrh 			ch = i;
8487063Srrh 			i = 0;
8497063Srrh 	}
8507063Srrh 	lgf--;
8517063Srrh 	return(i & 0177);
8527063Srrh }
8537063Srrh casenx(){
8547063Srrh 	lgf++;
8557063Srrh 	skip();
8567063Srrh 	getname();
8577063Srrh 	nx++;
8587063Srrh 	nextfile();
8597063Srrh 	nlflg++;
8607063Srrh 	ip = 0;
8617063Srrh 	ap = 0;
8627063Srrh 	nchar = pendt = 0;
8637063Srrh 	frame = stk;
8647063Srrh 	nxf = frame + 1;
8657063Srrh }
8667063Srrh getname(){
8677063Srrh 	register int i, j, k;
8687063Srrh 
8697063Srrh 	lgf++;
8707063Srrh 	for(k=0; k < (NS-1); k++){
8717063Srrh 		if(((j=(i=getch()) & CMASK) <= ' ') ||
8727063Srrh 			(j > 0176))break;
8737063Srrh 		nextf[k] = j;
8747063Srrh 	}
8757063Srrh 	nextf[k] = 0;
8767063Srrh 	ch = i;
8777063Srrh 	lgf--;
8787063Srrh 	return(nextf[0]);
8797063Srrh }
8807063Srrh caseso(){
8817063Srrh 	register i;
8827063Srrh 	register char *p, *q;
8837063Srrh 
8847063Srrh 	lgf++;
8857063Srrh 	nextf[0] = 0;
8867063Srrh 	if(skip() || !getname() || ((i=open(nextf,0)) <0) || (ifi >= NSO)) {
8877063Srrh 		prstr("can't open file ");
8887063Srrh 		prstr(nextf);
8897063Srrh 		prstr("\n");
8907063Srrh 		done(02);
8917063Srrh 	}
8927063Srrh 	flushi();
8937063Srrh 	ifl[ifi] = ifile;
8947063Srrh 	ifile = i;
8957063Srrh 	offl[ifi] = ioff;
8967063Srrh 	ioff = 0;
8977063Srrh 	ipl[ifi] = ip;
8987063Srrh 	ip = 0;
8997063Srrh 	nx++;
9007063Srrh 	nflush++;
9017063Srrh 	if(!ifl[ifi++]){
9027063Srrh 		p = ibuf;
9037063Srrh 		q = xbuf;
9047063Srrh 		xbufp = ibufp;
9057063Srrh 		xeibuf = eibuf;
9067063Srrh 		while(p < eibuf)*q++ = *p++;
9077063Srrh 	}
9087063Srrh }
9097063Srrh 
9107063Srrh casecf(){	/* copy file without change */
9117063Srrh 	int fd, i, n;
9127063Srrh 	char buf[OBUFSZ];
9137063Srrh 
9147063Srrh 	flusho();
9157063Srrh 	lgf++;
9167063Srrh 	nextf[0] = 0;
9177063Srrh 	if(skip() || !getname() || ((fd=open(nextf,0)) <0) || (ifi >= NSO)) {
9187063Srrh 		prstr("can't open file ");
9197063Srrh 		prstr(nextf);
9207063Srrh 		prstr("\n");
9217063Srrh 		done(02);
9227063Srrh 	}
9237063Srrh 	while ((n = read(fd, buf, OBUFSZ)) > 0)
9247063Srrh 		for (i = 0; i < n; i++)
9257063Srrh 			oput(buf[i]);
9267063Srrh 	flusho();
9277063Srrh 	close(fd);
9287063Srrh }
9297063Srrh getpn(a)
9307063Srrh char *a;
9317063Srrh {
9327063Srrh 	register i, neg;
9337063Srrh 	long atoi1();
9347063Srrh 
9357063Srrh 	if((*a & 0177) == 0)return;
9367063Srrh 	neg = 0;
9377063Srrh 	ibufp = a;
93817673Sralph 	eibuf = (char *) MAXPTR;
9397063Srrh 	noscale++;
9407063Srrh 	while((i = getch() & CMASK) != 0)switch(i){
9417063Srrh 		case '+':
9427063Srrh 		case ',':
9437063Srrh 			continue;
9447063Srrh 		case '-':
9457063Srrh 			neg = MOT;
9467063Srrh 			goto d2;
9477063Srrh 		default:
9487063Srrh 			ch = i;
9497063Srrh 		d2:
9507063Srrh 			i = atoi1();
9517063Srrh 			if(nonumb)goto fini;
9527063Srrh 			else{
9537063Srrh 				*pnp++ = i | neg;
9547063Srrh 				neg = 0;
9557063Srrh 				if(pnp >= &pnlist[NPN-2]){
9567063Srrh 					prstr("Too many page numbers\n");
9577063Srrh 					done3(-3);
9587063Srrh 				}
9597063Srrh 			}
9607063Srrh 		}
9617063Srrh fini:
9627063Srrh 	if(neg)*pnp++ = -2;
9637063Srrh 	*pnp = -1;
9647063Srrh 	ch = noscale = print = 0;
9657063Srrh 	pnp = pnlist;
9667063Srrh 	if(*pnp != -1)chkpn();
9677063Srrh }
9687063Srrh setrpt(){
9697063Srrh 	register i, j;
9707063Srrh 
9717063Srrh 	copyf++;raw++;
9727063Srrh 	i = getch0();
9737063Srrh 	copyf--;raw--;
9747063Srrh 	if((i < 0) ||
9757063Srrh 	   (((j = getch0()) & CMASK) == RPT))return;
9767063Srrh 	rchar = j;
9777063Srrh 	nchar = i & BMASK;
9787063Srrh }
979