We should also install grap.defines!
[unix-history] / usr / src / local / ditroff / ditroff.okeeffe / n2.c
CommitLineData
5bc24ac3 1#ifndef lint
655e9788 2static char sccsid[] = "@(#)n2.c 2.1 (CWI) 85/07/18";
5bc24ac3 3#endif lint
655e9788
JA
4/*
5 * n2.c
6 *
7 * output, cleanup
8 */
5bc24ac3
JA
9
10#include "tdef.h"
11#include <sgtty.h>
655e9788 12#include <signal.h>
5bc24ac3 13#ifdef NROFF
5bc24ac3
JA
14#include "tw.h"
15#endif
5bc24ac3 16#include <setjmp.h>
5bc24ac3 17#include "ext.h"
655e9788
JA
18
19extern jmp_buf sjbuf;
5bc24ac3
JA
20int toolate;
21int error;
22
23pchar(i)
655e9788 24 register tchar i;
5bc24ac3 25{
655e9788 26 register int j;
5bc24ac3
JA
27 static int hx = 0; /* records if have seen HX */
28
29 if (hx) {
30 hx = 0;
31 j = absmot(i);
32 if (isnmot(i)) {
33 if (j > dip->blss)
34 dip->blss = j;
35 } else {
36 if (j > dip->alss)
37 dip->alss = j;
38 ralss = dip->alss;
39 }
40 return;
41 }
42 if (ismot(i)) {
43 pchar1(i);
44 return;
45 }
46 switch (j = cbits(i)) {
47 case 0:
48 case IMP:
49 case RIGHT:
50 case LEFT:
51 return;
52 case HX:
53 hx = 1;
54 return;
55 case PRESC:
56 if (dip == &d[0])
57 j = eschar; /* fall through */
58 default:
59 setcbits(i, trtab[j]);
60 }
61 pchar1(i);
62}
63
64
65pchar1(i)
655e9788 66 register tchar i;
5bc24ac3 67{
655e9788 68 register j;
5bc24ac3
JA
69
70 j = cbits(i);
71 if (dip != &d[0]) {
72 wbf(i);
73 dip->op = offset;
74 return;
75 }
76 if (!tflg && !print) {
77 if (j == '\n')
78 dip->alss = dip->blss = 0;
79 return;
80 }
81 if (no_out || j == FILLER)
82 return;
83 if (tflg) { /* transparent mode, undiverted */
655e9788 84 fdprintf(ptid, "%c", j);
5bc24ac3
JA
85 return;
86 }
87#ifndef NROFF
655e9788
JA
88 if (ascii)
89 outascii(i);
90 else
91#endif
92 ptout(i);
93}
94
95outascii(i) /* print i in best-guess ascii */
96 tchar i;
97{
98 static int seendraw;
99 int j = cbits(i);
100
101 if (ismot(i)) {
102 if(!seendraw) {
103 oput(' ');
104 }
105 return;
106 }
107 if (j < 0177 && j >= 040) {
108 if(!seendraw) {
109 oput(j);
110 return;
111 } else {
112 switch(j) {
113 case DRAWCIRCLE: oputs("CIRCLE ");
114 break;
115 case DRAWELLIPSE: oputs("ELLIPSE ");
116 break;
117 case DRAWLINE: oputs("LINE ");
118 break;
119 case DRAWSPLINE: oputs("SPLINE ");
120 break;
121 case DRAWARC: oputs("ARC ");
122 break;
123 case '.':oputs(".");
124 /*
125 if(seendraw == 2)
126 seendraw = 0;
127 else
128 seendraw++;
129 */
130 break;
131 default: oputs("UNKNOWN "); flusho();
132 errprint("Unknown 0%o %c function", j,j);
133 break;
5bc24ac3
JA
134 }
135 return;
136 }
655e9788
JA
137 }
138 if( j < 040) {
139 switch(j) {
140 case SLANT:
141 case CHARHT:
142 case WORDSP:
143 case HX:
5bc24ac3 144 return;
655e9788
JA
145 case DRAWFCN:
146 if(seendraw == 1){
147 seendraw = 0;
148 } else {
149 oputs("DRAWFUNCTION ");flusho();
150 seendraw++;
5bc24ac3 151 }
655e9788
JA
152 /*
153 errprint("Seendraw %d", seendraw);
154 */
155 return;
156 case '\n':
157 oput(j);
158 return;
159 default:
160 errprint("Unknown (2) 0%o function", j);
5bc24ac3 161 return;
5bc24ac3 162 }
655e9788
JA
163 }
164 if (j == HYPHEN || j == MINUS)
165 oput('-');
166 else if (j == LIG_FI)
167 oputs("fi");
168 else if (j == LIG_FL)
169 oputs("fl");
170 else if (j == LIG_FF)
171 oputs("ff");
172 else if (j == LIG_FFI)
173 oputs("ffi");
174 else if (j == LIG_FFL)
175 oputs("ffl");
176 else {
177 oput('\\');
178 oput('(');
179 oput(chname[chtab[j-128]]);
180 oput(chname[chtab[j-128]+1]);
181 }
5bc24ac3
JA
182}
183
184
655e9788
JA
185/*
186 * now a macro
5bc24ac3 187oput(i)
655e9788 188 register int i;
5bc24ac3
JA
189{
190 *obufp++ = i;
191 if (obufp >= &obuf[OBUFSZ])
192 flusho();
193}
655e9788 194*/
5bc24ac3
JA
195
196oputs(i)
197register char *i;
198{
199 while (*i != 0)
200 oput(*i++);
201}
202
203
204flusho()
205{
206 if (obufp == obuf)
207 return;
208 if (no_out == 0) {
209 if (!toolate) {
210 toolate++;
211#ifdef NROFF
212 if (t.bset || t.breset) {
213 if (ttysave == -1) {
214 gtty(1, &ttys);
215 ttysave = ttys.sg_flags;
216 }
217 ttys.sg_flags &= ~t.breset;
218 ttys.sg_flags |= t.bset;
219 stty(1, &ttys);
220 }
221 {
222 char *p = t.twinit;
223 while (*p++)
224 ;
655e9788
JA
225 if (p - t.twinit > 1)
226 write(ptid, t.twinit, p - t.twinit - 1);
5bc24ac3
JA
227 }
228#endif
229 }
230 toolate += write(ptid, obuf, obufp - obuf);
231 }
232 obufp = obuf;
233}
234
235
236done(x)
237int x;
238{
239 register i;
240
241 error |= x;
5bc24ac3
JA
242 app = ds = lgf = 0;
243 if (i = em) {
244 donef = -1;
245 em = 0;
246 if (control(i, 0))
247 longjmp(sjbuf, 1);
248 }
249 if (!nfo)
250 done3(0);
251 mflg = 0;
252 dip = &d[0];
253 if (woff)
254 wbt((tchar)0);
255 if (pendw)
256 getword(1);
257 pendnf = 0;
258 if (donef == 1)
259 done1(0);
260 donef = 1;
261 ip = 0;
262 frame = stk;
263 nxf = frame + 1;
264 if (!ejf)
265 tbreak();
266 nflush++;
267 eject((struct s *)0);
268 longjmp(sjbuf, 1);
269}
270
271
272done1(x)
273int x;
274{
275 error |= x;
655e9788 276 if (numtab[NL].val) {
5bc24ac3
JA
277 trap = 0;
278 eject((struct s *)0);
279 longjmp(sjbuf, 1);
280 }
281 if (nofeed) {
282 ptlead();
283 flusho();
284 done3(0);
285 } else {
286 if (!gflag)
287 pttrailer();
288 done2(0);
289 }
290}
291
292
293done2(x)
294int x;
295{
296 ptlead();
297#ifndef NROFF
298 if (!ascii)
299 ptstop();
300#endif
301 flusho();
302 done3(x);
303}
304
305done3(x)
306int x;
307{
308 error |= x;
309 signal(SIGINT, SIG_IGN);
310 signal(SIGTERM, SIG_IGN);
311 unlink(unlkp);
312#ifdef NROFF
313 twdone();
314#endif
315 if (quiet) {
316 ttys.sg_flags |= ECHO;
317 stty(0, &ttys);
318 }
319 if (ascii)
320 mesg(1);
321 exit(error);
322}
323
324
325edone(x)
326int x;
327{
328 frame = stk;
329 nxf = frame + 1;
330 ip = 0;
331 done(x);
332}
333
334
335
336casepi()
337{
338 register i;
339 int id[2];
340
341 if (toolate || skip() || !getname() || pipe(id) == -1 || (i = fork()) == -1) {
655e9788 342 errprint("Pipe not created.");
5bc24ac3
JA
343 return;
344 }
345 ptid = id[1];
346 if (i > 0) {
347 close(id[0]);
348 toolate++;
349 pipeflg++;
350 return;
351 }
352 close(0);
353 dup(id[0]);
354 close(id[1]);
355 execl(nextf, nextf, 0);
655e9788 356 errprint("Cannot exec %s", nextf);
5bc24ac3
JA
357 exit(-4);
358}