xref: /csrg-svn/old/roff/common_source/n2.c (revision 7066)
1*7066Srrh #ifndef lint
2*7066Srrh static char sccsid[] = "@(#)n2.c	4.1 06/07/82";
3*7066Srrh #endif lint
4*7066Srrh 
5*7066Srrh #include "tdef.h"
6*7066Srrh #include <sgtty.h>
7*7066Srrh extern
8*7066Srrh #include "d.h"
9*7066Srrh extern
10*7066Srrh #include "v.h"
11*7066Srrh #ifdef NROFF
12*7066Srrh extern
13*7066Srrh #include "tw.h"
14*7066Srrh #endif
15*7066Srrh #include "sdef.h"
16*7066Srrh #include <setjmp.h>
17*7066Srrh jmp_buf sjbuf;
18*7066Srrh 
19*7066Srrh /*
20*7066Srrh troff2.c
21*7066Srrh 
22*7066Srrh output, cleanup
23*7066Srrh */
24*7066Srrh 
25*7066Srrh extern struct s *frame, *stk, *nxf;
26*7066Srrh extern filep ip;
27*7066Srrh extern filep offset;
28*7066Srrh extern char *enda;
29*7066Srrh 
30*7066Srrh 
31*7066Srrh extern char obuf[OBUFSZ];
32*7066Srrh extern char *obufp;
33*7066Srrh extern int dilev;
34*7066Srrh extern int eschar;
35*7066Srrh extern int tlss;
36*7066Srrh extern int tflg;
37*7066Srrh extern int ascii;
38*7066Srrh extern int print;
39*7066Srrh extern char trtab[];
40*7066Srrh extern int waitf;
41*7066Srrh extern char ptname[];
42*7066Srrh extern int ptid;
43*7066Srrh extern int em;
44*7066Srrh extern int ds;
45*7066Srrh extern int mflg;
46*7066Srrh extern filep woff;
47*7066Srrh extern int nflush;
48*7066Srrh extern int lgf;
49*7066Srrh extern int app;
50*7066Srrh extern int nfo;
51*7066Srrh extern int donef;
52*7066Srrh extern int *pendw;
53*7066Srrh extern int nofeed;
54*7066Srrh extern int trap;
55*7066Srrh extern struct sgttyb ttys;
56*7066Srrh extern int ttysave;
57*7066Srrh extern int quiet;
58*7066Srrh extern int pendnf;
59*7066Srrh extern int ndone;
60*7066Srrh extern int lead;
61*7066Srrh extern int ralss;
62*7066Srrh extern int paper;
63*7066Srrh extern int gflag;
64*7066Srrh extern char *unlkp;
65*7066Srrh extern char nextf[];
66*7066Srrh extern int pipeflg;
67*7066Srrh extern int ejf;
68*7066Srrh extern int no_out;
69*7066Srrh extern int level;
70*7066Srrh extern int stopmesg;
71*7066Srrh extern int xxx;
72*7066Srrh int toolate;
73*7066Srrh int error;
74*7066Srrh #ifndef NROFF
75*7066Srrh extern int acctf;
76*7066Srrh #endif
77*7066Srrh 
78*7066Srrh pchar(c)
79*7066Srrh int c;
80*7066Srrh {
81*7066Srrh 	register i, j;
82*7066Srrh 
83*7066Srrh 	if((i=c) & MOT){pchar1(i); return;}
84*7066Srrh 	switch(j = i & CMASK){
85*7066Srrh 		case 0:
86*7066Srrh 		case IMP:
87*7066Srrh 		case RIGHT:
88*7066Srrh 		case LEFT:
89*7066Srrh 			return;
90*7066Srrh 		case HX:
91*7066Srrh 			j = (tlss>>9) | ((i&~0777)>>3);
92*7066Srrh 			if(i & 040000){
93*7066Srrh 				j &= ~(040000>>3);
94*7066Srrh 				if(j > dip->blss)dip->blss = j;
95*7066Srrh 			}else{
96*7066Srrh 				if(j > dip->alss)dip->alss = j;
97*7066Srrh 				ralss = dip->alss;
98*7066Srrh 			}
99*7066Srrh 			tlss = 0;
100*7066Srrh 			return;
101*7066Srrh 		case LX:
102*7066Srrh 			tlss = i;
103*7066Srrh 			return;
104*7066Srrh 		case PRESC:
105*7066Srrh 			if(dip == &d[0])j = eschar;
106*7066Srrh 		default:
107*7066Srrh 			i = (trtab[j] & BMASK) | (i & ~CMASK);
108*7066Srrh 	}
109*7066Srrh 	pchar1(i);
110*7066Srrh }
111*7066Srrh pchar1(c)
112*7066Srrh int c;
113*7066Srrh {
114*7066Srrh 	register i, j, *k;
115*7066Srrh 	extern int chtab[];
116*7066Srrh 
117*7066Srrh 	j = (i = c) & CMASK;
118*7066Srrh 	if(dip != &d[0]){
119*7066Srrh 		wbf(i);
120*7066Srrh 		dip->op = offset;
121*7066Srrh 		return;
122*7066Srrh 	}
123*7066Srrh 	if(!tflg && !print){
124*7066Srrh 		if(j == '\n')dip->alss = dip->blss = 0;
125*7066Srrh 		return;
126*7066Srrh 	}
127*7066Srrh 	if(no_out || (j == FILLER))return;
128*7066Srrh #ifndef NROFF
129*7066Srrh 	if(ascii){
130*7066Srrh 		if(i & MOT){
131*7066Srrh 			oput(' ');
132*7066Srrh 			return;
133*7066Srrh 		}
134*7066Srrh 		if(j < 0177){
135*7066Srrh 			oput(i);
136*7066Srrh 			return;
137*7066Srrh 		}
138*7066Srrh 		switch(j){
139*7066Srrh 			case 0200:
140*7066Srrh 			case 0210:
141*7066Srrh 				oput('-');
142*7066Srrh 				break;
143*7066Srrh 			case 0211:
144*7066Srrh 				oputs("fi");
145*7066Srrh 				break;
146*7066Srrh 			case 0212:
147*7066Srrh 				oputs("fl");
148*7066Srrh 				break;
149*7066Srrh 			case 0213:
150*7066Srrh 				oputs("ff");
151*7066Srrh 				break;
152*7066Srrh 			case 0214:
153*7066Srrh 				oputs("ffi");
154*7066Srrh 				break;
155*7066Srrh 			case 0215:
156*7066Srrh 				oputs("ffl");
157*7066Srrh 				break;
158*7066Srrh 			default:
159*7066Srrh 				for(k=chtab; *++k != j; k++)
160*7066Srrh 					if(*k == 0)return;
161*7066Srrh 				oput('\\');
162*7066Srrh 				oput('(');
163*7066Srrh 				oput(*--k & BMASK);
164*7066Srrh 				oput(*k >> BYTE);
165*7066Srrh 		}
166*7066Srrh 	}else
167*7066Srrh #endif
168*7066Srrh 	ptout(i);
169*7066Srrh }
170*7066Srrh oput(i)
171*7066Srrh char i;
172*7066Srrh {
173*7066Srrh 	*obufp++ = i;
174*7066Srrh 	if(obufp == (obuf + OBUFSZ + ascii - 1))flusho();
175*7066Srrh }
176*7066Srrh oputs(i)
177*7066Srrh char *i;
178*7066Srrh {
179*7066Srrh 	while(*i != 0)oput(*i++);
180*7066Srrh }
181*7066Srrh flusho(){
182*7066Srrh 	if(!ascii)*obufp++ = 0;
183*7066Srrh 	if(!ptid){
184*7066Srrh 		while((ptid=open(ptname,1)) < 0){
185*7066Srrh 			if(++waitf <=2)prstr("Waiting for Typesetter.\n");
186*7066Srrh 			sleep(15);
187*7066Srrh 		}
188*7066Srrh 	}
189*7066Srrh 	if(no_out == 0){
190*7066Srrh 		if (!toolate) {
191*7066Srrh 			toolate++;
192*7066Srrh #ifdef NROFF
193*7066Srrh 			if(t.bset || t.breset){
194*7066Srrh 				if(ttysave == -1) {
195*7066Srrh 					gtty(1, &ttys);
196*7066Srrh 					ttysave = ttys.sg_flags;
197*7066Srrh 				}
198*7066Srrh 				ttys.sg_flags &= ~t.breset;
199*7066Srrh 				ttys.sg_flags |= t.bset;
200*7066Srrh 				stty(1, &ttys);
201*7066Srrh 			}
202*7066Srrh 			{
203*7066Srrh 			char *p = t.twinit;
204*7066Srrh 			while (*p++)
205*7066Srrh 				;
206*7066Srrh 			write(ptid, t.twinit, p-t.twinit-1);
207*7066Srrh 			}
208*7066Srrh #endif
209*7066Srrh 		}
210*7066Srrh 		toolate += write(ptid, obuf, obufp-obuf);
211*7066Srrh 	}
212*7066Srrh 	obufp = obuf;
213*7066Srrh }
214*7066Srrh done(x) int x;{
215*7066Srrh 	register i;
216*7066Srrh 
217*7066Srrh 	error |= x;
218*7066Srrh 	level = 0;
219*7066Srrh 	app = ds = lgf = 0;
220*7066Srrh 	if(i=em){
221*7066Srrh 		donef = -1;
222*7066Srrh 		em = 0;
223*7066Srrh 		if(control(i,0))longjmp(sjbuf,1);
224*7066Srrh 	}
225*7066Srrh 	if(!nfo)done3(0);
226*7066Srrh 	mflg = 0;
227*7066Srrh 	dip = &d[0];
228*7066Srrh 	if(woff)wbt(0);
229*7066Srrh 	if(pendw)getword(1);
230*7066Srrh 	pendnf = 0;
231*7066Srrh 	if(donef == 1)done1(0);
232*7066Srrh 	donef = 1;
233*7066Srrh 	ip = 0;
234*7066Srrh 	frame = stk;
235*7066Srrh 	nxf = frame + 1;
236*7066Srrh 	if(!ejf)tbreak();
237*7066Srrh 	nflush++;
238*7066Srrh 	eject((struct s *)0);
239*7066Srrh 	longjmp(sjbuf,1);
240*7066Srrh }
241*7066Srrh done1(x) int x; {
242*7066Srrh 	error |= x;
243*7066Srrh 	if(v.nl){
244*7066Srrh 		trap = 0;
245*7066Srrh 		eject((struct s *)0);
246*7066Srrh 		longjmp(sjbuf,1);
247*7066Srrh 	}
248*7066Srrh 	if(nofeed){
249*7066Srrh 		ptlead();
250*7066Srrh 		flusho();
251*7066Srrh 		done3(0);
252*7066Srrh 	}else{
253*7066Srrh 		if(!gflag)lead += TRAILER;
254*7066Srrh 		done2(0);
255*7066Srrh 	}
256*7066Srrh }
257*7066Srrh done2(x) int x; {
258*7066Srrh 	register i;
259*7066Srrh 
260*7066Srrh 	ptlead();
261*7066Srrh #ifndef NROFF
262*7066Srrh 	if(!ascii){
263*7066Srrh 		oput(T_INIT);
264*7066Srrh 		oput(T_STOP);
265*7066Srrh 		if(!gflag)for(i=8; i>0; i--)oput(T_PAD);
266*7066Srrh 		if(stopmesg)prstr("Troff finished.\n");
267*7066Srrh 	}
268*7066Srrh #endif
269*7066Srrh 	flusho();
270*7066Srrh 	done3(x);
271*7066Srrh }
272*7066Srrh done3(x) int x;{
273*7066Srrh 	error |= x;
274*7066Srrh 	signal(SIGINT, SIG_IGN);
275*7066Srrh 	signal(SIGTERM, SIG_IGN);
276*7066Srrh 	unlink(unlkp);
277*7066Srrh #ifdef NROFF
278*7066Srrh 	twdone();
279*7066Srrh #endif
280*7066Srrh 	if(quiet){
281*7066Srrh 		ttys.sg_flags |= ECHO;
282*7066Srrh 		stty(0, &ttys);
283*7066Srrh 	}
284*7066Srrh 	if(ascii)mesg(1);
285*7066Srrh #ifndef NROFF
286*7066Srrh 	report();
287*7066Srrh #endif
288*7066Srrh 	exit(error);
289*7066Srrh }
290*7066Srrh edone(x) int x;{
291*7066Srrh 	frame = stk;
292*7066Srrh 	nxf = frame + 1;
293*7066Srrh 	ip = 0;
294*7066Srrh 	done(x);
295*7066Srrh }
296*7066Srrh #ifndef NROFF
297*7066Srrh report(){
298*7066Srrh 	struct {int use; int uid;} a;
299*7066Srrh 
300*7066Srrh 	if((ptid != 1) && paper ){
301*7066Srrh 		lseek(acctf,0L,2);
302*7066Srrh 		a.use = paper;
303*7066Srrh 		a.uid = getuid();
304*7066Srrh 		write(acctf,(char *)&a,sizeof(a));
305*7066Srrh 		close(acctf);
306*7066Srrh 	}
307*7066Srrh }
308*7066Srrh #endif
309*7066Srrh #ifdef NROFF
310*7066Srrh casepi(){
311*7066Srrh 	register i;
312*7066Srrh 	int id[2];
313*7066Srrh 
314*7066Srrh 	if(toolate || skip() || !getname() || (pipe(id) == -1) ||
315*7066Srrh 	   ((i=fork()) == -1)){
316*7066Srrh 		prstr("Pipe not created.\n");
317*7066Srrh 		return;
318*7066Srrh 	}
319*7066Srrh 	ptid = id[1];
320*7066Srrh 	if(i>0){
321*7066Srrh 		close(id[0]);
322*7066Srrh 		toolate++;
323*7066Srrh 		pipeflg++;
324*7066Srrh 		return;
325*7066Srrh 	}
326*7066Srrh 	close(0);
327*7066Srrh 	dup(id[0]);
328*7066Srrh 	close(id[1]);
329*7066Srrh 	execl(nextf,nextf,0);
330*7066Srrh 	prstr("Cannot exec: ");
331*7066Srrh 	prstr(nextf);
332*7066Srrh 	prstr("\n");
333*7066Srrh 	exit(-4);
334*7066Srrh }
335*7066Srrh #endif
336