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