from wnj, use correct timezone
[unix-history] / usr / src / old / roff / common_source / n2.c
CommitLineData
392f1c67
RH
1#ifndef lint
2static char sccsid[] = "@(#)n2.c 4.1 %G%";
3#endif lint
4
5#include "tdef.h"
6#include <sgtty.h>
7extern
8#include "d.h"
9extern
10#include "v.h"
11#ifdef NROFF
12extern
13#include "tw.h"
14#endif
15#include "sdef.h"
16#include <setjmp.h>
17jmp_buf sjbuf;
18
19/*
20troff2.c
21
22output, cleanup
23*/
24
25extern struct s *frame, *stk, *nxf;
26extern filep ip;
27extern filep offset;
28extern char *enda;
29
30
31extern char obuf[OBUFSZ];
32extern char *obufp;
33extern int dilev;
34extern int eschar;
35extern int tlss;
36extern int tflg;
37extern int ascii;
38extern int print;
39extern char trtab[];
40extern int waitf;
41extern char ptname[];
42extern int ptid;
43extern int em;
44extern int ds;
45extern int mflg;
46extern filep woff;
47extern int nflush;
48extern int lgf;
49extern int app;
50extern int nfo;
51extern int donef;
52extern int *pendw;
53extern int nofeed;
54extern int trap;
55extern struct sgttyb ttys;
56extern int ttysave;
57extern int quiet;
58extern int pendnf;
59extern int ndone;
60extern int lead;
61extern int ralss;
62extern int paper;
63extern int gflag;
64extern char *unlkp;
65extern char nextf[];
66extern int pipeflg;
67extern int ejf;
68extern int no_out;
69extern int level;
70extern int stopmesg;
71extern int xxx;
72int toolate;
73int error;
74#ifndef NROFF
75extern int acctf;
76#endif
77
78pchar(c)
79int 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}
111pchar1(c)
112int 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}
170oput(i)
171char i;
172{
173 *obufp++ = i;
174 if(obufp == (obuf + OBUFSZ + ascii - 1))flusho();
175}
176oputs(i)
177char *i;
178{
179 while(*i != 0)oput(*i++);
180}
181flusho(){
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}
214done(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}
241done1(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}
257done2(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}
272done3(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}
290edone(x) int x;{
291 frame = stk;
292 nxf = frame + 1;
293 ip = 0;
294 done(x);
295}
296#ifndef NROFF
297report(){
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
310casepi(){
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