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