xref: /illumos-gate/usr/src/cmd/troff/n2.c (revision 355b4669e025ff377602b6fc7caaf30dbc218371)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 1989 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 /*
31  * University Copyright- Copyright (c) 1982, 1986, 1988
32  * The Regents of the University of California
33  * All Rights Reserved
34  *
35  * University Acknowledgment- Portions of this document are derived from
36  * software developed by the University of California, Berkeley, and its
37  * contributors.
38  */
39 
40 #pragma ident	"%Z%%M%	%I%	%E% SMI"
41 
42 /*
43  * n2.c
44  *
45  * output, cleanup
46  */
47 
48 #include <signal.h>
49 #include "tdef.h"
50 #ifdef NROFF
51 #include "tw.h"
52 #endif
53 #include <setjmp.h>
54 #include "ext.h"
55 #ifdef EUC
56 #ifdef NROFF
57 #include <stddef.h>
58 #include <widec.h>
59 #include <limits.h>
60 #include <ctype.h>
61 
62 char mbobuf[MB_LEN_MAX] = {0};
63 wchar_t wchar;
64 int	nmb1 = 0;
65 #endif /* NROFF */
66 #endif /* EUC */
67 
68 extern	jmp_buf	sjbuf;
69 int	toolate;
70 int	error;
71 
72 int
73 pchar(i)
74 	tchar i;
75 {
76 	int j;
77 	static int hx = 0;	/* records if have seen HX */
78 
79 	if (hx) {
80 		hx = 0;
81 		j = absmot(i);
82 		if (isnmot(i)) {
83 			if (j > dip->blss)
84 				dip->blss = j;
85 		} else {
86 			if (j > dip->alss)
87 				dip->alss = j;
88 			ralss = dip->alss;
89 		}
90 		return (0);
91 	}
92 	if (ismot(i)) {
93 		pchar1(i);
94 		return (0);
95 	}
96 	switch (j = cbits(i)) {
97 	case 0:
98 	case IMP:
99 	case RIGHT:
100 	case LEFT:
101 		return (0);
102 	case HX:
103 		hx = 1;
104 		return (0);
105 	case PRESC:
106 		if (dip == &d[0])
107 			j = eschar;	/* fall through */
108 	default:
109 #ifndef EUC
110 		setcbits(i, trtab[j]);
111 #else
112 #ifndef NROFF
113 		setcbits(i, trtab[j]);
114 #else
115 		if (!multi_locale || (!(j & CSMASK) && !(j & MBMASK1)))
116 			setcbits(i, trtab[j]);
117 #endif /* NROFF */
118 #endif /* EUC */
119 	}
120 	pchar1(i);
121 
122 	return (0);
123 }
124 
125 
126 int
127 pchar1(i)
128 	tchar i;
129 {
130 	int	j;
131 
132 	j = cbits(i);
133 	if (dip != &d[0]) {
134 		wbf(i);
135 		dip->op = offset;
136 		return (0);
137 	}
138 	if (!tflg && !print) {
139 		if (j == '\n')
140 			dip->alss = dip->blss = 0;
141 		return (0);
142 	}
143 	if (no_out || j == FILLER)
144 		return (0);
145 	if (tflg) {	/* transparent mode, undiverted */
146 		fdprintf(ptid, "%c", j);
147 		return (0);
148 	}
149 #ifndef NROFF
150 	if (ascii)
151 		outascii(i);
152 	else
153 #endif
154 		ptout(i);
155 
156 	return (0);
157 }
158 
159 int
160 outascii(i)	/* print i in best-guess ascii */
161 	tchar i;
162 {
163 	int j = cbits(i);
164 
165 	if (ismot(i)) {
166 		oput(' ');
167 		return (0);
168 	}
169 	if (j < 0177 && j >= ' ' || j == '\n') {
170 		oput(j);
171 		return (0);
172 	}
173 	if (j == DRAWFCN)
174 		oputs("\\D");
175 	else if (j == HYPHEN || j == MINUS)
176 		oput('-');
177 	else if (j == XON)
178 		oputs("\\X");
179 	else if (j == LIG_FI)
180 		oputs("fi");
181 	else if (j == LIG_FL)
182 		oputs("fl");
183 	else if (j == LIG_FF)
184 		oputs("ff");
185 	else if (j == LIG_FFI)
186 		oputs("ffi");
187 	else if (j == LIG_FFL)
188 		oputs("ffl");
189 	else if (j == WORDSP)
190 		;	/* nothing at all */
191 	else if (j > 0177) {
192 		oput('\\');
193 		oput('(');
194 		oput(chname[chtab[j-128]]);
195 		oput(chname[chtab[j-128]+1]);
196 	}
197 
198 	return (0);
199 }
200 
201 
202 /*
203  * now a macro
204 int
205 oput(i)
206 	int	i;
207 {
208 	*obufp++ = i;
209 	if (obufp >= &obuf[OBUFSZ])
210 		flusho();
211 
212 	return (0);
213 }
214 */
215 
216 int
217 oputs(i)
218 char	*i;
219 {
220 	while (*i != 0)
221 		oput(*i++);
222 
223 	return (0);
224 }
225 
226 
227 int
228 flusho()
229 {
230 	if (obufp == obuf)
231 		return (0);
232 	if (no_out == 0) {
233 		if (!toolate) {
234 			toolate++;
235 #ifdef NROFF
236 			set_tty();
237 			{
238 				char	*p = t.twinit;
239 				while (*p++)
240 					;
241 				if (p - t.twinit > 1)
242 					write(ptid, t.twinit, p - t.twinit - 1);
243 			}
244 #endif
245 		}
246 		toolate += write(ptid, obuf, obufp - obuf);
247 	}
248 	obufp = obuf;
249 
250 	return (0);
251 }
252 
253 
254 int
255 done(x)
256 int	x;
257 {
258 	int	i;
259 
260 	error |= x;
261 	app = ds = lgf = 0;
262 	if (i = em) {
263 		donef = -1;
264 		em = 0;
265 		if (control(i, 0))
266 			longjmp(sjbuf, 1);
267 	}
268 	if (!nfo)
269 		done3(0);
270 	mflg = 0;
271 	dip = &d[0];
272 	if (woff)
273 		wbt((tchar)0);
274 	if (pendw)
275 		getword(1);
276 	pendnf = 0;
277 	if (donef == 1)
278 		done1(0);
279 	donef = 1;
280 	ip = 0;
281 	frame = stk;
282 	nxf = frame + 1;
283 	if (!ejf)
284 		tbreak();
285 	nflush++;
286 	eject((struct s *)0);
287 	longjmp(sjbuf, 1);
288 
289 	return (0);
290 }
291 
292 
293 int
294 done1(x)
295 int	x;
296 {
297 	error |= x;
298 	if (numtab[NL].val) {
299 		trap = 0;
300 		eject((struct s *)0);
301 		longjmp(sjbuf, 1);
302 	}
303 	if (nofeed) {
304 		ptlead();
305 		flusho();
306 		done3(0);
307 	} else {
308 		if (!gflag)
309 			pttrailer();
310 		done2(0);
311 	}
312 
313 	return (0);
314 }
315 
316 
317 int
318 done2(x)
319 int	x;
320 {
321 	ptlead();
322 #ifndef NROFF
323 	if (!ascii)
324 		ptstop();
325 #endif
326 	flusho();
327 	done3(x);
328 
329 	return (0);
330 }
331 
332 int
333 done3(x)
334 int	x;
335 {
336 	error |= x;
337 	signal(SIGINT, SIG_IGN);
338 	signal(SIGTERM, SIG_IGN);
339 	unlink(unlkp);
340 #ifdef NROFF
341 	twdone();
342 #endif
343 	if (ascii)
344 		mesg(1);
345 	exit(error);
346 
347 	return (0);
348 }
349 
350 
351 int
352 edone(x)
353 int	x;
354 {
355 	frame = stk;
356 	nxf = frame + 1;
357 	ip = 0;
358 	done(x);
359 
360 	return (0);
361 }
362 
363 
364 int
365 casepi()
366 {
367 	int	i;
368 	int	id[2];
369 
370 	if (toolate || skip() || !getname() || pipe(id) == -1 || (i = fork()) == -1) {
371 		errprint(gettext("Pipe not created."));
372 		return (0);
373 	}
374 	ptid = id[1];
375 	if (i > 0) {
376 		close(id[0]);
377 		toolate++;
378 		pipeflg++;
379 		return (0);
380 	}
381 	close(0);
382 	dup(id[0]);
383 	close(id[1]);
384 	execl(nextf, nextf, 0);
385 	errprint(gettext("Cannot exec %s"), nextf);
386 	exit(-4);
387 
388 	return (0);
389 }
390