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