xref: /csrg-svn/old/roff/common_source/n1.c (revision 7171)
17063Srrh #ifndef lint
2*7171Smckusick static char sccsid[] = "@(#)n1.c	4.2 06/14/82";
37063Srrh #endif lint
47063Srrh 
5*7171Smckusick #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;
2557063Srrh #endif
2567063Srrh 		default:
2577063Srrh 			pto = cnum(&argv[0][1]);
2587063Srrh 			continue;
2597063Srrh 		}
2607063Srrh 
2617063Srrh 	if(argv[0][0] == '+'){
2627063Srrh 		pfrom = cnum(&argv[0][1]);
2637063Srrh 		print = 0;
2647063Srrh 		if(argc > 0)goto options;
2657063Srrh 	}
2667063Srrh 
2677063Srrh start:
2687063Srrh 	argp = argv;
2697063Srrh 	rargc = argc;
2707063Srrh 	init2();
2717063Srrh 	setjmp(sjbuf);
2727063Srrh loop:
2737063Srrh 	copyf = lgf = nb = nflush = nlflg = 0;
2747063Srrh 	if(ip && (rbf0(ip)==0) && ejf && (frame->pframe <= ejl)){
2757063Srrh 		nflush++;
2767063Srrh 		trap = 0;
2777063Srrh 		eject((struct s *)0);
2787063Srrh 		goto loop;
2797063Srrh 	}
2807063Srrh 	i = getch();
2817063Srrh 	if(pendt)goto lt;
2827063Srrh 	if(lit && (frame <= litlev)){
2837063Srrh 		lit--;
2847063Srrh 		goto lt;
2857063Srrh 	}
2867063Srrh 	if((j = (i & CMASK)) == XPAR){
2877063Srrh 		copyf++;
2887063Srrh 		tflg++;
2897063Srrh 		for(;(i & CMASK) != '\n';)pchar(i = getch());
2907063Srrh 		tflg = 0;
2917063Srrh 		copyf--;
2927063Srrh 		goto loop;
2937063Srrh 	}
2947063Srrh 	if((j == cc) || (j == c2)){
2957063Srrh 		if(j == c2)nb++;
2967063Srrh 		copyf++;
2977063Srrh 		while(((j=((i=getch()) & CMASK)) == ' ') ||
2987063Srrh 			(j == '\t'));
2997063Srrh 		ch = i;
3007063Srrh 		copyf--;
3017063Srrh 		control(getrq(),1);
3027063Srrh 		flushi();
3037063Srrh 		goto loop;
3047063Srrh 	}
3057063Srrh lt:
3067063Srrh 	ch = i;
3077063Srrh 	text();
3087063Srrh 	goto loop;
3097063Srrh }
3107063Srrh catch(){
3117063Srrh /*
3127063Srrh 	prstr("Interrupt\n");
3137063Srrh */
3147063Srrh 	done3(01);
3157063Srrh }
3167063Srrh fpecatch(){
3177063Srrh 	prstrfl("Floating Exception.\n");
3187063Srrh 	signal(SIGFPE,fpecatch);
3197063Srrh }
3207063Srrh kcatch(){
3217063Srrh 	signal(SIGTERM,SIG_IGN);
3227063Srrh 	done3(01);
3237063Srrh }
3247063Srrh #ifndef NROFF
3257063Srrh acctg() {
3267063Srrh 	static char *acct_file = "/usr/adm/tracct";
3277063Srrh 	acctf = open(acct_file,1);
3287063Srrh 	setuid(getuid());
3297063Srrh }
3307063Srrh #endif
3317063Srrh init1(a)
3327063Srrh char a;
3337063Srrh {
3347063Srrh 	register char *p;
3357063Srrh 	char *mktemp();
3367063Srrh 	register i;
3377063Srrh 
3387063Srrh #ifndef NROFF
3397063Srrh 	acctg();/*open troff actg file while mode 4755*/
3407063Srrh #endif
3417063Srrh 	p = mktemp("/tmp/taXXXXX");
3427063Srrh 	if(a == 'a')p = &p[5];
3437063Srrh 	if((close(creat(p, 0600))) < 0){
3447063Srrh 		prstr("Cannot create temp file.\n");
3457063Srrh 		exit(-1);
3467063Srrh 	}
3477063Srrh 	ibf = open(p, 2);
3487063Srrh 	for(i=256; --i;)trtab[i]=i;
3497063Srrh 	trtab[UNPAD] = ' ';
3507063Srrh 	mchbits();
3517063Srrh 	if(a != 'a')unlkp = p;
3527063Srrh }
3537063Srrh init2()
3547063Srrh {
3557063Srrh 	register i,j;
3567063Srrh 	extern int block;
3577063Srrh 	extern char *setbrk();
3587063Srrh 	extern char *ttyname();
3597063Srrh 
3607063Srrh 	ttyod = 2;
3617063Srrh 	if(((ttyp=ttyname(j=0)) != (char *)0) ||
3627063Srrh 	   ((ttyp=ttyname(j=1)) != (char *)0) ||
3637063Srrh 	   ((ttyp=ttyname(j=2)) != (char *)0)
3647063Srrh 	  );else ttyp = "notty";
3657063Srrh 	iflg = j;
3667063Srrh 	if(ascii)mesg(0);
3677063Srrh 
3687063Srrh 	if((!ptid) && (!waitf)){
3697063Srrh 		if((ptid = open(ptname,1)) < 0){
3707063Srrh 			prstr("Typesetter busy.\n");
3717063Srrh 			done3(-2);
3727063Srrh 		}
3737063Srrh 	}
3747063Srrh 	ptinit();
3757063Srrh 	for(i=NEV; i--;)write(ibf, (char *)&block, EVS*sizeof(int));
3767063Srrh 	olinep = oline;
3777063Srrh 	ibufp = eibuf = ibuf;
3787063Srrh 	v.hp = init = 0;
3797063Srrh 	pinchar = inchar;	/* XXX */
3807063Srrh 	ioff = 0;
3817063Srrh 	v.nl = -1;
3827063Srrh 	cvtime();
3837063Srrh 	frame = stk = (struct s *)setbrk(DELTA);
3847063Srrh 	dip = &d[0];
3857063Srrh 	nxf = frame + 1;
3867063Srrh 	nx = mflg;
3877063Srrh }
3887063Srrh cvtime(){
3897063Srrh 
3907063Srrh 	long tt;
3917063Srrh 	register i;
3927063Srrh 
3937063Srrh 	time(&tt);
3947063Srrh 	tt -= 3600*ZONE;	/*5hrs for EST*/
3957063Srrh 	v.dy = (tt/86400L) + 1;
3967063Srrh 	v.dw = (v.dy + 3)%7 + 1;
3977063Srrh 	for(v.yr=70;; v.yr++){
3987063Srrh 		if((v.yr)%4)ms[1]=28;else ms[1]=29;
3997063Srrh 		for(i=0;i<12;){
4007063Srrh 			if(v.dy<=ms[i]){
4017063Srrh 				v.mo = i+1;
4027063Srrh 				return;
4037063Srrh 			}
4047063Srrh 			v.dy -= ms[i++];
4057063Srrh 		}
4067063Srrh 	}
4077063Srrh }
4087063Srrh cnum(a)
4097063Srrh char *a;
4107063Srrh {
4117063Srrh 	register i;
4127063Srrh 
4137063Srrh 	ibufp = a;
4147063Srrh 	eibuf = MAXPTR;
4157063Srrh 	i = atoi();
4167063Srrh 	ch = 0;
4177063Srrh 	return(i);
4187063Srrh }
4197063Srrh mesg(f)
4207063Srrh int f;
4217063Srrh {
4227063Srrh 	static int mode;
4237063Srrh 
4247063Srrh 	if (ttyp==0)
4257063Srrh 		return;
4267063Srrh 	if(!f){
4277063Srrh 		stat(ttyp,cbuf);
4287063Srrh 		mode = ((struct stat *)(cbuf))->st_mode;
4297063Srrh 		chmod(ttyp,mode & ~022);
4307063Srrh 	}else{
4317063Srrh 		chmod(ttyp,mode);
4327063Srrh 	}
4337063Srrh }
4347063Srrh prstrfl(s)
4357063Srrh char *s;
4367063Srrh {
4377063Srrh 	flusho();
4387063Srrh 	prstr(s);
4397063Srrh }
4407063Srrh prstr(s)
4417063Srrh char *s;
4427063Srrh {
4437063Srrh 	register i;
4447063Srrh 	register char *j;
4457063Srrh 
4467063Srrh 	j = s;
4477063Srrh 	for(i=0;*s;i++)s++;
4487063Srrh 	write(ttyod,j,i);
4497063Srrh }
4507063Srrh control(a,b)
4517063Srrh int a,b;
4527063Srrh {
4537063Srrh 	register i,j;
4547063Srrh 	extern filep boff();
4557063Srrh 
4567063Srrh 	i = a;
4577063Srrh 	if((i == 0) || ((j = findmn(i)) == -1))return(0);
4587063Srrh 	if(contab[j].rq & MMASK){
4597063Srrh 		nxf->nargs = 0;
4607063Srrh 		if(b)collect();
4617063Srrh 		flushi();
4627063Srrh 		return(pushi(((filep)contab[j].x.mx)<<BLKBITS));
4637063Srrh 	}else{
4647063Srrh 		if(!b)return(0);
4657063Srrh 		return((*contab[j].x.f)(0));
4667063Srrh 	}
4677063Srrh }
4687063Srrh 
4697063Srrh getrq(){
4707063Srrh 	register i,j;
4717063Srrh 
4727063Srrh 	if(((i=getach()) == 0) ||
4737063Srrh 	   ((j=getach()) == 0))goto rtn;
4747063Srrh 	i = PAIR(i,j);
4757063Srrh rtn:
4767063Srrh 	return(i);
4777063Srrh }
4787063Srrh getch(){
4797063Srrh 	register int i, j, k;
4807063Srrh 
4817063Srrh 	level++;
4827063Srrh g0:
4837063Srrh 	if(ch){
4847063Srrh 		if(((i = ch) & CMASK) == '\n')nlflg++;
4857063Srrh 		ch = 0;
4867063Srrh 		level--;
4877063Srrh 		return(i);
4887063Srrh 	}
4897063Srrh 
4907063Srrh 	if(nlflg){
4917063Srrh 		level--;
4927063Srrh 		return('\n');
4937063Srrh 	}
4947063Srrh 
4957063Srrh 	if((k = (i = getch0()) & CMASK) != ESC){
4967063Srrh 		if(i & MOT)goto g2;
4977063Srrh 		if(k == FLSS){
4987063Srrh 			copyf++; raw++;
4997063Srrh 			i = getch0();
5007063Srrh 			if(!fi)flss = i;
5017063Srrh 			copyf--; raw--;
5027063Srrh 			goto g0;
5037063Srrh 		}
5047063Srrh 		if(k == RPT){
5057063Srrh 			setrpt();
5067063Srrh 			goto g0;
5077063Srrh 		}
5087063Srrh 		if(!copyf){
5097063Srrh 			if((k == 'f') && lg && !lgf){
5107063Srrh 				i = getlg(i);
5117063Srrh 				goto g2;
5127063Srrh 			}
5137063Srrh 			if((k == fc) || (k == tabch) || (k == ldrch)){
5147063Srrh 				if((i=setfield(k)) == 0)goto g0; else goto g2;
5157063Srrh 			}
5167063Srrh 			if(k == 010){
5177063Srrh 				i = makem(-width(' ' | chbits));
5187063Srrh 				goto g2;
5197063Srrh 			}
5207063Srrh 		}
5217063Srrh 		goto g2;
5227063Srrh 	}
5237063Srrh 	k = (j = getch0()) & CMASK;
5247063Srrh 	if(j & MOT){
5257063Srrh 		i = j;
5267063Srrh 		goto g2;
5277063Srrh 	}
5287063Srrh /*
5297063Srrh 	if(k == tdelim){
5307063Srrh 		i = TDELIM;
5317063Srrh 		tdelim = IMP;
5327063Srrh 		goto g2;
5337063Srrh 	}
5347063Srrh */
5357063Srrh 	switch(k){
5367063Srrh 
5377063Srrh 		case '\n':	/*concealed newline*/
5387063Srrh 			goto g0;
5397063Srrh 		case 'n':	/*number register*/
5407063Srrh 			setn();
5417063Srrh 			goto g0;
5427063Srrh 		case '*':	/*string indicator*/
5437063Srrh 			setstr();
5447063Srrh 			goto g0;
5457063Srrh 		case '$':	/*argument indicator*/
5467063Srrh 			seta();
5477063Srrh 			goto g0;
5487063Srrh 		case '{':	/*LEFT*/
5497063Srrh 			i = LEFT;
5507063Srrh 			goto gx;
5517063Srrh 		case '}':	/*RIGHT*/
5527063Srrh 			i = RIGHT;
5537063Srrh 			goto gx;
5547063Srrh 		case '"':	/*comment*/
5557063Srrh 			while(((i=getch0()) & CMASK ) != '\n');
5567063Srrh 			goto g2;
5577063Srrh 		case ESC:	/*double backslash*/
5587063Srrh 			i = eschar;
5597063Srrh 			goto gx;
5607063Srrh 		case 'e':	/*printable version of current eschar*/
5617063Srrh 			i = PRESC;
5627063Srrh 			goto gx;
5637063Srrh 		case ' ':	/*unpaddable space*/
5647063Srrh 			i = UNPAD;
5657063Srrh 			goto gx;
5667063Srrh 		case '|':	/*narrow space*/
5677063Srrh 			i = NARSP;
5687063Srrh 			goto gx;
5697063Srrh 		case '^':	/*half of narrow space*/
5707063Srrh 			i = HNSP;
5717063Srrh 			goto gx;
5727063Srrh 		case '\'':	/*\(aa*/
5737063Srrh 			i = 0222;
5747063Srrh 			goto gx;
5757063Srrh 		case '`':	/*\(ga*/
5767063Srrh 			i = 0223;
5777063Srrh 			goto gx;
5787063Srrh 		case '_':	/*\(ul*/
5797063Srrh 			i = 0224;
5807063Srrh 			goto gx;
5817063Srrh 		case '-':	/*current font minus*/
5827063Srrh 			i = 0210;
5837063Srrh 			goto gx;
5847063Srrh 		case '&':	/*filler*/
5857063Srrh 			i = FILLER;
5867063Srrh 			goto gx;
5877063Srrh 		case 'c':	/*to be continued*/
5887063Srrh 			i = CONT;
5897063Srrh 			goto gx;
5907063Srrh 		case ':':	/*lem's char*/
5917063Srrh 			i = COLON;
5927063Srrh 			goto gx;
5937063Srrh 		case '!':	/*transparent indicator*/
5947063Srrh 			i = XPAR;
5957063Srrh 			goto gx;
5967063Srrh 		case 't':	/*tab*/
5977063Srrh 			i = '\t';
5987063Srrh 			goto g2;
5997063Srrh 		case 'a':	/*leader (SOH)*/
6007063Srrh 			i = LEADER;
6017063Srrh 			goto g2;
6027063Srrh 		case '%':	/*ohc*/
6037063Srrh 			i = OHC;
6047063Srrh 			goto g2;
6057063Srrh 		case '.':	/*.*/
6067063Srrh 			i = '.';
6077063Srrh 		gx:
6087063Srrh 			i = (j & ~CMASK) | i;
6097063Srrh 			goto g2;
6107063Srrh 	}
6117063Srrh 	if(!copyf)
6127063Srrh 		switch(k){
6137063Srrh 
6147063Srrh 			case 'p':	/*spread*/
6157063Srrh 				spread++;
6167063Srrh 				goto g0;
6177063Srrh 			case '(':	/*special char name*/
6187063Srrh 				if((i=setch()) == 0)goto g0;
6197063Srrh 				break;
6207063Srrh 			case 's':	/*size indicator*/
6217063Srrh 				setps();
6227063Srrh 				goto g0;
6237063Srrh 			case 'f':	/*font indicator*/
6247063Srrh 				setfont(0);
6257063Srrh 				goto g0;
6267063Srrh 			case 'w':	/*width function*/
6277063Srrh 				setwd();
6287063Srrh 				goto g0;
6297063Srrh 			case 'v':	/*vert mot*/
6307063Srrh 				if(i = vmot())break;
6317063Srrh 				goto g0;
6327063Srrh 			case 'h': 	/*horiz mot*/
6337063Srrh 				if(i = hmot())break;
6347063Srrh 				goto g0;
6357063Srrh 			case 'z':	/*zero with char*/
6367063Srrh 				i = setz();
6377063Srrh 				break;
6387063Srrh 			case 'l':	/*hor line*/
6397063Srrh 				setline();
6407063Srrh 				goto g0;
6417063Srrh 			case 'L':	/*vert line*/
6427063Srrh 				setvline();
6437063Srrh 				goto g0;
6447063Srrh 			case 'b':	/*bracket*/
6457063Srrh 				setbra();
6467063Srrh 				goto g0;
6477063Srrh 			case 'o':	/*overstrike*/
6487063Srrh 				setov();
6497063Srrh 				goto g0;
6507063Srrh 			case 'k':	/*mark hor place*/
6517063Srrh 				if((i=findr(getsn())) == -1)goto g0;
6527063Srrh 				vlist[i] = v.hp = sumhp();	/* XXX */
6537063Srrh 				goto g0;
6547063Srrh 			case 'j':	/*mark output hor place*/
6557063Srrh 				if(!(i=getach()))goto g0;
6567063Srrh 				i = (i<<BYTE) | JREG;
6577063Srrh 				break;
6587063Srrh 			case '0':	/*number space*/
6597063Srrh 				i = makem(width('0' | chbits));
6607063Srrh 				break;
6617063Srrh 			case 'x':	/*extra line space*/
6627063Srrh 				if(i = xlss())break;
6637063Srrh 				goto g0;
6647063Srrh 			case 'u':	/*half em up*/
6657063Srrh 			case 'r':	/*full em up*/
6667063Srrh 			case 'd':	/*half em down*/
6677063Srrh 				i = sethl(k);
6687063Srrh 				break;
6697063Srrh 			default:
6707063Srrh 				i = j;
6717063Srrh 		}
6727063Srrh 	else{
6737063Srrh 		ch0 = j;
6747063Srrh 		i = eschar;
6757063Srrh 	}
6767063Srrh g2:
6777063Srrh 	if((i & CMASK) == '\n'){
6787063Srrh 		nlflg++;
6797063Srrh 		v.hp = 0;
6807063Srrh 		pinchar = inchar;	/* XXX */
6817063Srrh 		if(ip == 0)v.cd++;
6827063Srrh 	}
6837063Srrh 	if(!--level){
6847063Srrh 		/* j = width(i); */
6857063Srrh 		/* v.hp += j; */
6867063Srrh 		/* cwidth = j; */
6877063Srrh 		if (pinchar >= inchar + LNSIZE) {	/* XXX */
6887063Srrh 			inchar[0] = makem(sumhp());
6897063Srrh 			pinchar = &inchar[1];
6907063Srrh 		}
6917063Srrh 		*pinchar++ = i;	/* XXX */
6927063Srrh 	}
6937063Srrh 	return(i);
6947063Srrh }
6957063Srrh 
6967063Srrh sumhp()	/* XXX - add up widths in inchar array */
6977063Srrh {
6987063Srrh 	register int n;
6997063Srrh 	register int *p;
7007063Srrh 
7017063Srrh 	n = 0;
7027063Srrh 	for (p = inchar; p < pinchar; p++)
7037063Srrh 		n += width(*p);
7047063Srrh 	return(n);
7057063Srrh }
7067063Srrh char ifilt[32] = {0,001,002,003,0,005,006,007,010,011,012};
7077063Srrh getch0(){
7087063Srrh 	register int i, j;
7097063Srrh 
7107063Srrh 	if(ch0){i=ch0; ch0=0; return(i);}
7117063Srrh 	if(nchar){nchar--; return(rchar);}
7127063Srrh 
7137063Srrh again:
7147063Srrh 	if(cp){
7157063Srrh 		if((i = *cp++) == 0){
7167063Srrh 			cp = 0;
7177063Srrh 			goto again;
7187063Srrh 		}
7197063Srrh 	}else if(ap){
7207063Srrh 		if((i = *ap++) == 0){
7217063Srrh 			ap = 0;
7227063Srrh 			goto again;
7237063Srrh 		}
7247063Srrh 	}else if(ip){
7257063Srrh 		if(ip == -1)i = rdtty();
7267063Srrh 		else i = rbf();
7277063Srrh 	}else{
7287063Srrh 		if(donef)done(0);
7297063Srrh 		if(nx || ((ibufp >= eibuf) && (ibufp != MAXPTR))){
7307063Srrh 			if(nfo)goto g1;
7317063Srrh 		g0:
7327063Srrh 			if(nextfile()){
7337063Srrh 				if(ip)goto again;
7347063Srrh 				if(ibufp < eibuf)goto g2;
7357063Srrh 			}
7367063Srrh 		g1:
7377063Srrh 			nx = 0;
7387063Srrh 			if((j=read(ifile,ibuf,IBUFSZ)) <= 0)goto g0;
7397063Srrh 			ibufp = ibuf;
7407063Srrh 			eibuf = ibuf + j;
7417063Srrh 			if(ip)goto again;
7427063Srrh 		}
7437063Srrh 	g2:
7447063Srrh 		i = *ibufp++ & 0177;
7457063Srrh 		ioff++;
7467063Srrh 		if(i >= 040)goto g4; else i = ifilt[i];
7477063Srrh 	}
7487063Srrh 	if(raw)return(i);
7497063Srrh 	if((j = i & CMASK) == IMP)goto again;
7507063Srrh 	if((i == 0) && !init)goto again;
7517063Srrh g4:
7527063Srrh 	if((copyf == 0) && ((i & ~BMASK) == 0) && ((i & CMASK) < 0370))
7537063Srrh #ifndef NROFF
7547063Srrh 		if(spbits && (i>31) && ((codetab[i-32] & 0200))) i |= spbits;
7557063Srrh 		else
7567063Srrh #endif
7577063Srrh 		i |= chbits;
7587063Srrh 	if((i & CMASK) == eschar)i = (i & ~CMASK) | ESC;
7597063Srrh 	return(i);
7607063Srrh }
7617063Srrh nextfile(){
7627063Srrh 	register char *p;
7637063Srrh 
7647063Srrh n0:
7657063Srrh 	if(ifile)close(ifile);
7667063Srrh 	if(nx){
7677063Srrh 		p = nextf;
7687063Srrh 		if(*p != 0)goto n1;
7697063Srrh 	}
7707063Srrh 	if(ifi > 0){
7717063Srrh 		if(popf())goto n0; /*popf error*/
7727063Srrh 		return(1); /*popf ok*/
7737063Srrh 	}
7747063Srrh 	if(rargc-- <= 0)goto n2;
7757063Srrh 	p = (argp++)[0];
7767063Srrh n1:
7777063Srrh 	if((p[0] == '-') && (p[1] == 0)){
7787063Srrh 		ifile = 0;
7797063Srrh 	}else if((ifile=open(p,0)) < 0){
7807063Srrh 		prstr("Cannot open ");
7817063Srrh 		prstr(p);
7827063Srrh 		prstr("\n");
7837063Srrh 		nfo -= mflg;
7847063Srrh 		done(02);
7857063Srrh 	}
7867063Srrh 	nfo++;
7877063Srrh 	v.cd = 0;
7887063Srrh 	ioff = 0;
7897063Srrh 	return(0);
7907063Srrh n2:
7917063Srrh 	if((nfo -= mflg) && !stdi)done(0);
7927063Srrh 	nfo++;
7937063Srrh 	v.cd = ifile =  stdi = mflg = 0;
7947063Srrh 	ioff = 0;
7957063Srrh 	return(0);
7967063Srrh }
7977063Srrh popf(){
7987063Srrh 	register i;
7997063Srrh 	register char *p, *q;
8007063Srrh 	extern char *ttyname();
8017063Srrh 
8027063Srrh 	ioff = offl[--ifi];
8037063Srrh 	ip = ipl[ifi];
8047063Srrh 	if((ifile = ifl[ifi]) == 0){
8057063Srrh 		p = xbuf;
8067063Srrh 		q = ibuf;
8077063Srrh 		ibufp = xbufp;
8087063Srrh 		eibuf = xeibuf;
8097063Srrh 		while(q < eibuf)*q++ = *p++;
8107063Srrh 		return(0);
8117063Srrh 	}
8127063Srrh 	if((lseek(ifile,(long)(ioff & ~(IBUFSZ-1)),0) < 0) ||
8137063Srrh 	   ((i = read(ifile,ibuf,IBUFSZ)) < 0))return(1);
8147063Srrh 	eibuf = ibuf + i;
8157063Srrh 	ibufp = ibuf;
8167063Srrh 	if(ttyname(ifile) == (char *)0)
8177063Srrh 		if((ibufp = ibuf + (int)(ioff & (IBUFSZ-1)))  >= eibuf)return(1);
8187063Srrh 	return(0);
8197063Srrh }
8207063Srrh flushi(){
8217063Srrh 	if(nflush)return;
8227063Srrh 	ch = 0;
8237063Srrh 	if((ch0 & CMASK) == '\n')nlflg++;
8247063Srrh 	ch0 = 0;
8257063Srrh 	copyf++;
8267063Srrh 	while(!nlflg){
8277063Srrh 		if(donef && (frame == stk))break;
8287063Srrh 		getch();
8297063Srrh 	}
8307063Srrh 	copyf--;
8317063Srrh 	v.hp = 0;
8327063Srrh 	pinchar = inchar;	/* XXX */
8337063Srrh }
8347063Srrh getach(){
8357063Srrh 	register i;
8367063Srrh 
8377063Srrh 	lgf++;
8387063Srrh 	if(((i = getch()) & MOT) ||
8397063Srrh 	    ((i&CMASK) == ' ') ||
8407063Srrh 	    ((i&CMASK) == '\n')||
8417063Srrh 	    (i & 0200)){
8427063Srrh 			ch = i;
8437063Srrh 			i = 0;
8447063Srrh 	}
8457063Srrh 	lgf--;
8467063Srrh 	return(i & 0177);
8477063Srrh }
8487063Srrh casenx(){
8497063Srrh 	lgf++;
8507063Srrh 	skip();
8517063Srrh 	getname();
8527063Srrh 	nx++;
8537063Srrh 	nextfile();
8547063Srrh 	nlflg++;
8557063Srrh 	ip = 0;
8567063Srrh 	ap = 0;
8577063Srrh 	nchar = pendt = 0;
8587063Srrh 	frame = stk;
8597063Srrh 	nxf = frame + 1;
8607063Srrh }
8617063Srrh getname(){
8627063Srrh 	register int i, j, k;
8637063Srrh 
8647063Srrh 	lgf++;
8657063Srrh 	for(k=0; k < (NS-1); k++){
8667063Srrh 		if(((j=(i=getch()) & CMASK) <= ' ') ||
8677063Srrh 			(j > 0176))break;
8687063Srrh 		nextf[k] = j;
8697063Srrh 	}
8707063Srrh 	nextf[k] = 0;
8717063Srrh 	ch = i;
8727063Srrh 	lgf--;
8737063Srrh 	return(nextf[0]);
8747063Srrh }
8757063Srrh caseso(){
8767063Srrh 	register i;
8777063Srrh 	register char *p, *q;
8787063Srrh 
8797063Srrh 	lgf++;
8807063Srrh 	nextf[0] = 0;
8817063Srrh 	if(skip() || !getname() || ((i=open(nextf,0)) <0) || (ifi >= NSO)) {
8827063Srrh 		prstr("can't open file ");
8837063Srrh 		prstr(nextf);
8847063Srrh 		prstr("\n");
8857063Srrh 		done(02);
8867063Srrh 	}
8877063Srrh 	flushi();
8887063Srrh 	ifl[ifi] = ifile;
8897063Srrh 	ifile = i;
8907063Srrh 	offl[ifi] = ioff;
8917063Srrh 	ioff = 0;
8927063Srrh 	ipl[ifi] = ip;
8937063Srrh 	ip = 0;
8947063Srrh 	nx++;
8957063Srrh 	nflush++;
8967063Srrh 	if(!ifl[ifi++]){
8977063Srrh 		p = ibuf;
8987063Srrh 		q = xbuf;
8997063Srrh 		xbufp = ibufp;
9007063Srrh 		xeibuf = eibuf;
9017063Srrh 		while(p < eibuf)*q++ = *p++;
9027063Srrh 	}
9037063Srrh }
9047063Srrh 
9057063Srrh casecf(){	/* copy file without change */
9067063Srrh 	int fd, i, n;
9077063Srrh 	char buf[OBUFSZ];
9087063Srrh 
9097063Srrh 	flusho();
9107063Srrh 	lgf++;
9117063Srrh 	nextf[0] = 0;
9127063Srrh 	if(skip() || !getname() || ((fd=open(nextf,0)) <0) || (ifi >= NSO)) {
9137063Srrh 		prstr("can't open file ");
9147063Srrh 		prstr(nextf);
9157063Srrh 		prstr("\n");
9167063Srrh 		done(02);
9177063Srrh 	}
9187063Srrh 	while ((n = read(fd, buf, OBUFSZ)) > 0)
9197063Srrh 		for (i = 0; i < n; i++)
9207063Srrh 			oput(buf[i]);
9217063Srrh 	flusho();
9227063Srrh 	close(fd);
9237063Srrh }
9247063Srrh getpn(a)
9257063Srrh char *a;
9267063Srrh {
9277063Srrh 	register i, neg;
9287063Srrh 	long atoi1();
9297063Srrh 
9307063Srrh 	if((*a & 0177) == 0)return;
9317063Srrh 	neg = 0;
9327063Srrh 	ibufp = a;
9337063Srrh 	eibuf = MAXPTR;
9347063Srrh 	noscale++;
9357063Srrh 	while((i = getch() & CMASK) != 0)switch(i){
9367063Srrh 		case '+':
9377063Srrh 		case ',':
9387063Srrh 			continue;
9397063Srrh 		case '-':
9407063Srrh 			neg = MOT;
9417063Srrh 			goto d2;
9427063Srrh 		default:
9437063Srrh 			ch = i;
9447063Srrh 		d2:
9457063Srrh 			i = atoi1();
9467063Srrh 			if(nonumb)goto fini;
9477063Srrh 			else{
9487063Srrh 				*pnp++ = i | neg;
9497063Srrh 				neg = 0;
9507063Srrh 				if(pnp >= &pnlist[NPN-2]){
9517063Srrh 					prstr("Too many page numbers\n");
9527063Srrh 					done3(-3);
9537063Srrh 				}
9547063Srrh 			}
9557063Srrh 		}
9567063Srrh fini:
9577063Srrh 	if(neg)*pnp++ = -2;
9587063Srrh 	*pnp = -1;
9597063Srrh 	ch = noscale = print = 0;
9607063Srrh 	pnp = pnlist;
9617063Srrh 	if(*pnp != -1)chkpn();
9627063Srrh }
9637063Srrh setrpt(){
9647063Srrh 	register i, j;
9657063Srrh 
9667063Srrh 	copyf++;raw++;
9677063Srrh 	i = getch0();
9687063Srrh 	copyf--;raw--;
9697063Srrh 	if((i < 0) ||
9707063Srrh 	   (((j = getch0()) & CMASK) == RPT))return;
9717063Srrh 	rchar = j;
9727063Srrh 	nchar = i & BMASK;
9737063Srrh }
974