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