xref: /csrg-svn/old/roff/common_source/n2.c (revision 48302)
1*48302Sbostic /*-
2*48302Sbostic  * Copyright (c) 1991 The Regents of the University of California.
3*48302Sbostic  * All rights reserved.
4*48302Sbostic  *
5*48302Sbostic  * %sccs.include.proprietary.c%
6*48302Sbostic  */
7*48302Sbostic 
87066Srrh #ifndef lint
9*48302Sbostic static char sccsid[] = "@(#)n2.c	4.2 (Berkeley) 04/18/91";
10*48302Sbostic #endif /* not lint */
117066Srrh 
127066Srrh #include "tdef.h"
137066Srrh #include <sgtty.h>
147066Srrh extern
157066Srrh #include "d.h"
167066Srrh extern
177066Srrh #include "v.h"
187066Srrh #ifdef NROFF
197066Srrh extern
207066Srrh #include "tw.h"
217066Srrh #endif
227066Srrh #include "sdef.h"
237066Srrh #include <setjmp.h>
247066Srrh jmp_buf sjbuf;
257066Srrh 
267066Srrh /*
277066Srrh troff2.c
287066Srrh 
297066Srrh output, cleanup
307066Srrh */
317066Srrh 
327066Srrh extern struct s *frame, *stk, *nxf;
337066Srrh extern filep ip;
347066Srrh extern filep offset;
357066Srrh extern char *enda;
367066Srrh 
377066Srrh 
387066Srrh extern char obuf[OBUFSZ];
397066Srrh extern char *obufp;
407066Srrh extern int dilev;
417066Srrh extern int eschar;
427066Srrh extern int tlss;
437066Srrh extern int tflg;
447066Srrh extern int ascii;
457066Srrh extern int print;
467066Srrh extern char trtab[];
477066Srrh extern int waitf;
487066Srrh extern char ptname[];
497066Srrh extern int ptid;
507066Srrh extern int em;
517066Srrh extern int ds;
527066Srrh extern int mflg;
537066Srrh extern filep woff;
547066Srrh extern int nflush;
557066Srrh extern int lgf;
567066Srrh extern int app;
577066Srrh extern int nfo;
587066Srrh extern int donef;
597066Srrh extern int *pendw;
607066Srrh extern int nofeed;
617066Srrh extern int trap;
627066Srrh extern struct sgttyb ttys;
637066Srrh extern int ttysave;
647066Srrh extern int quiet;
657066Srrh extern int pendnf;
667066Srrh extern int ndone;
677066Srrh extern int lead;
687066Srrh extern int ralss;
697066Srrh extern int paper;
707066Srrh extern int gflag;
717066Srrh extern char *unlkp;
727066Srrh extern char nextf[];
737066Srrh extern int pipeflg;
747066Srrh extern int ejf;
757066Srrh extern int no_out;
767066Srrh extern int level;
777066Srrh extern int stopmesg;
787066Srrh extern int xxx;
797066Srrh int toolate;
807066Srrh int error;
817066Srrh #ifndef NROFF
827066Srrh extern int acctf;
837066Srrh #endif
847066Srrh 
pchar(c)857066Srrh pchar(c)
867066Srrh int c;
877066Srrh {
887066Srrh 	register i, j;
897066Srrh 
907066Srrh 	if((i=c) & MOT){pchar1(i); return;}
917066Srrh 	switch(j = i & CMASK){
927066Srrh 		case 0:
937066Srrh 		case IMP:
947066Srrh 		case RIGHT:
957066Srrh 		case LEFT:
967066Srrh 			return;
977066Srrh 		case HX:
987066Srrh 			j = (tlss>>9) | ((i&~0777)>>3);
997066Srrh 			if(i & 040000){
1007066Srrh 				j &= ~(040000>>3);
1017066Srrh 				if(j > dip->blss)dip->blss = j;
1027066Srrh 			}else{
1037066Srrh 				if(j > dip->alss)dip->alss = j;
1047066Srrh 				ralss = dip->alss;
1057066Srrh 			}
1067066Srrh 			tlss = 0;
1077066Srrh 			return;
1087066Srrh 		case LX:
1097066Srrh 			tlss = i;
1107066Srrh 			return;
1117066Srrh 		case PRESC:
1127066Srrh 			if(dip == &d[0])j = eschar;
1137066Srrh 		default:
1147066Srrh 			i = (trtab[j] & BMASK) | (i & ~CMASK);
1157066Srrh 	}
1167066Srrh 	pchar1(i);
1177066Srrh }
pchar1(c)1187066Srrh pchar1(c)
1197066Srrh int c;
1207066Srrh {
1217066Srrh 	register i, j, *k;
1227066Srrh 	extern int chtab[];
1237066Srrh 
1247066Srrh 	j = (i = c) & CMASK;
1257066Srrh 	if(dip != &d[0]){
1267066Srrh 		wbf(i);
1277066Srrh 		dip->op = offset;
1287066Srrh 		return;
1297066Srrh 	}
1307066Srrh 	if(!tflg && !print){
1317066Srrh 		if(j == '\n')dip->alss = dip->blss = 0;
1327066Srrh 		return;
1337066Srrh 	}
1347066Srrh 	if(no_out || (j == FILLER))return;
1357066Srrh #ifndef NROFF
1367066Srrh 	if(ascii){
1377066Srrh 		if(i & MOT){
1387066Srrh 			oput(' ');
1397066Srrh 			return;
1407066Srrh 		}
1417066Srrh 		if(j < 0177){
1427066Srrh 			oput(i);
1437066Srrh 			return;
1447066Srrh 		}
1457066Srrh 		switch(j){
1467066Srrh 			case 0200:
1477066Srrh 			case 0210:
1487066Srrh 				oput('-');
1497066Srrh 				break;
1507066Srrh 			case 0211:
1517066Srrh 				oputs("fi");
1527066Srrh 				break;
1537066Srrh 			case 0212:
1547066Srrh 				oputs("fl");
1557066Srrh 				break;
1567066Srrh 			case 0213:
1577066Srrh 				oputs("ff");
1587066Srrh 				break;
1597066Srrh 			case 0214:
1607066Srrh 				oputs("ffi");
1617066Srrh 				break;
1627066Srrh 			case 0215:
1637066Srrh 				oputs("ffl");
1647066Srrh 				break;
1657066Srrh 			default:
1667066Srrh 				for(k=chtab; *++k != j; k++)
1677066Srrh 					if(*k == 0)return;
1687066Srrh 				oput('\\');
1697066Srrh 				oput('(');
1707066Srrh 				oput(*--k & BMASK);
1717066Srrh 				oput(*k >> BYTE);
1727066Srrh 		}
1737066Srrh 	}else
1747066Srrh #endif
1757066Srrh 	ptout(i);
1767066Srrh }
oput(i)1777066Srrh oput(i)
1787066Srrh char i;
1797066Srrh {
1807066Srrh 	*obufp++ = i;
1817066Srrh 	if(obufp == (obuf + OBUFSZ + ascii - 1))flusho();
1827066Srrh }
oputs(i)1837066Srrh oputs(i)
1847066Srrh char *i;
1857066Srrh {
1867066Srrh 	while(*i != 0)oput(*i++);
1877066Srrh }
flusho()1887066Srrh flusho(){
1897066Srrh 	if(!ascii)*obufp++ = 0;
1907066Srrh 	if(!ptid){
1917066Srrh 		while((ptid=open(ptname,1)) < 0){
1927066Srrh 			if(++waitf <=2)prstr("Waiting for Typesetter.\n");
1937066Srrh 			sleep(15);
1947066Srrh 		}
1957066Srrh 	}
1967066Srrh 	if(no_out == 0){
1977066Srrh 		if (!toolate) {
1987066Srrh 			toolate++;
1997066Srrh #ifdef NROFF
2007066Srrh 			if(t.bset || t.breset){
2017066Srrh 				if(ttysave == -1) {
2027066Srrh 					gtty(1, &ttys);
2037066Srrh 					ttysave = ttys.sg_flags;
2047066Srrh 				}
2057066Srrh 				ttys.sg_flags &= ~t.breset;
2067066Srrh 				ttys.sg_flags |= t.bset;
2077066Srrh 				stty(1, &ttys);
2087066Srrh 			}
2097066Srrh 			{
2107066Srrh 			char *p = t.twinit;
2117066Srrh 			while (*p++)
2127066Srrh 				;
2137066Srrh 			write(ptid, t.twinit, p-t.twinit-1);
2147066Srrh 			}
2157066Srrh #endif
2167066Srrh 		}
2177066Srrh 		toolate += write(ptid, obuf, obufp-obuf);
2187066Srrh 	}
2197066Srrh 	obufp = obuf;
2207066Srrh }
done(x)2217066Srrh done(x) int x;{
2227066Srrh 	register i;
2237066Srrh 
2247066Srrh 	error |= x;
2257066Srrh 	level = 0;
2267066Srrh 	app = ds = lgf = 0;
2277066Srrh 	if(i=em){
2287066Srrh 		donef = -1;
2297066Srrh 		em = 0;
2307066Srrh 		if(control(i,0))longjmp(sjbuf,1);
2317066Srrh 	}
2327066Srrh 	if(!nfo)done3(0);
2337066Srrh 	mflg = 0;
2347066Srrh 	dip = &d[0];
2357066Srrh 	if(woff)wbt(0);
2367066Srrh 	if(pendw)getword(1);
2377066Srrh 	pendnf = 0;
2387066Srrh 	if(donef == 1)done1(0);
2397066Srrh 	donef = 1;
2407066Srrh 	ip = 0;
2417066Srrh 	frame = stk;
2427066Srrh 	nxf = frame + 1;
2437066Srrh 	if(!ejf)tbreak();
2447066Srrh 	nflush++;
2457066Srrh 	eject((struct s *)0);
2467066Srrh 	longjmp(sjbuf,1);
2477066Srrh }
done1(x)2487066Srrh done1(x) int x; {
2497066Srrh 	error |= x;
2507066Srrh 	if(v.nl){
2517066Srrh 		trap = 0;
2527066Srrh 		eject((struct s *)0);
2537066Srrh 		longjmp(sjbuf,1);
2547066Srrh 	}
2557066Srrh 	if(nofeed){
2567066Srrh 		ptlead();
2577066Srrh 		flusho();
2587066Srrh 		done3(0);
2597066Srrh 	}else{
2607066Srrh 		if(!gflag)lead += TRAILER;
2617066Srrh 		done2(0);
2627066Srrh 	}
2637066Srrh }
done2(x)2647066Srrh done2(x) int x; {
2657066Srrh 	register i;
2667066Srrh 
2677066Srrh 	ptlead();
2687066Srrh #ifndef NROFF
2697066Srrh 	if(!ascii){
2707066Srrh 		oput(T_INIT);
2717066Srrh 		oput(T_STOP);
2727066Srrh 		if(!gflag)for(i=8; i>0; i--)oput(T_PAD);
2737066Srrh 		if(stopmesg)prstr("Troff finished.\n");
2747066Srrh 	}
2757066Srrh #endif
2767066Srrh 	flusho();
2777066Srrh 	done3(x);
2787066Srrh }
done3(x)2797066Srrh done3(x) int x;{
2807066Srrh 	error |= x;
2817066Srrh 	signal(SIGINT, SIG_IGN);
2827066Srrh 	signal(SIGTERM, SIG_IGN);
2837066Srrh 	unlink(unlkp);
2847066Srrh #ifdef NROFF
2857066Srrh 	twdone();
2867066Srrh #endif
2877066Srrh 	if(quiet){
2887066Srrh 		ttys.sg_flags |= ECHO;
2897066Srrh 		stty(0, &ttys);
2907066Srrh 	}
2917066Srrh 	if(ascii)mesg(1);
2927066Srrh #ifndef NROFF
2937066Srrh 	report();
2947066Srrh #endif
2957066Srrh 	exit(error);
2967066Srrh }
edone(x)2977066Srrh edone(x) int x;{
2987066Srrh 	frame = stk;
2997066Srrh 	nxf = frame + 1;
3007066Srrh 	ip = 0;
3017066Srrh 	done(x);
3027066Srrh }
3037066Srrh #ifndef NROFF
report()3047066Srrh report(){
3057066Srrh 	struct {int use; int uid;} a;
3067066Srrh 
3077066Srrh 	if((ptid != 1) && paper ){
3087066Srrh 		lseek(acctf,0L,2);
3097066Srrh 		a.use = paper;
3107066Srrh 		a.uid = getuid();
3117066Srrh 		write(acctf,(char *)&a,sizeof(a));
3127066Srrh 		close(acctf);
3137066Srrh 	}
3147066Srrh }
3157066Srrh #endif
3167066Srrh #ifdef NROFF
casepi()3177066Srrh casepi(){
3187066Srrh 	register i;
3197066Srrh 	int id[2];
3207066Srrh 
3217066Srrh 	if(toolate || skip() || !getname() || (pipe(id) == -1) ||
3227066Srrh 	   ((i=fork()) == -1)){
3237066Srrh 		prstr("Pipe not created.\n");
3247066Srrh 		return;
3257066Srrh 	}
3267066Srrh 	ptid = id[1];
3277066Srrh 	if(i>0){
3287066Srrh 		close(id[0]);
3297066Srrh 		toolate++;
3307066Srrh 		pipeflg++;
3317066Srrh 		return;
3327066Srrh 	}
3337066Srrh 	close(0);
3347066Srrh 	dup(id[0]);
3357066Srrh 	close(id[1]);
3367066Srrh 	execl(nextf,nextf,0);
3377066Srrh 	prstr("Cannot exec: ");
3387066Srrh 	prstr(nextf);
3397066Srrh 	prstr("\n");
3407066Srrh 	exit(-4);
3417066Srrh }
3427066Srrh #endif
343