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