aboutsummaryrefslogtreecommitdiff
path: root/troff/n2.c
blob: 74bb7e3ffc2af5e62e6422201fca60e6c45a4912 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 1989 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
/*	  All Rights Reserved  	*/


/*	from OpenSolaris "n2.c	1.9	05/06/08 SMI"	*/

/*
 * Portions Copyright (c) 2005 Gunnar Ritter, Freiburg i. Br., Germany
 *
 * Sccsid @(#)n2.c	1.47 (gritter) 5/25/08
 */

/*
 * University Copyright- Copyright (c) 1982, 1986, 1988
 * The Regents of the University of California
 * All Rights Reserved
 *
 * University Acknowledgment- Portions of this document are derived from
 * software developed by the University of California, Berkeley, and its
 * contributors.
 */

/*
 * n2.c
 *
 * output, cleanup
 */

#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <setjmp.h>
#ifdef EUC
#include <limits.h>
#include <ctype.h>
#include <unistd.h>
#endif /* EUC */
#include "tdef.h"
#ifdef NROFF
#include "tw.h"
#endif
#include "pt.h"
#include "ext.h"

extern	jmp_buf	sjbuf;
static int	toolate;
int	error;

static void	outtp(tchar);

int
pchar(register tchar i)
{
	register int j;
	static int hx = 0;	/* records if have seen HX */
	static int xon = 0;	/* records if have seen XON */
	static int drawfcn = 0;	/* records if have seen DRAWFCN */

	if (hx) {
		hx = 0;
		j = absmot(i);
		if (isnmot(i)) {
			if (j > dip->blss)
				dip->blss = j;
		} else {
			if (j > dip->alss)
				dip->alss = j;
			ralss = dip->alss;
		}
		return 1;
	}
	if (ismot(i)) {
		pchar1(i); 
		return 1;
	}
	switch (j = cbits(i)) {
	case 0:
	case IMP:
	case RIGHT:
	case LEFT:
		if (xflag) {
			i = j = FILLER;	/* avoid kerning in output routine */
			goto dfl;
		}
		return 1;
	case HX:
		hx = 1;
		if (xflag) {
			i = j = FILLER;	/* avoid kerning in output routine */
			goto dfl;
		}
		return 1;
	case XON:
		xon = 1;
		goto dfl;
	case XOFF:
		xon = 0;
		goto dfl;
	case DRAWFCN:
		drawfcn = !drawfcn;
		goto dfl;
	case PRESC:
		if (dip == &d[0])
			j = eschar;	/* fall through */
	default:
	dfl:
#ifndef NROFF
		if (html) {
			if (!xflag || !isdi(i)) {
				setcbits(i, j >= NCHARS ? j :
				    tflg ? trnttab[j] : trtab[j]);
				if (xon == 0 && drawfcn == 0 && i < NCHARS)
					setcbits(i, ftrans(fbits(i),
					    cbits(i)));
			}
		} else
#endif
		if (!xflag || !isdi(i)) {
			setcbits(i, tflg ? trnttab[j] : trtab[j]);
			if (xon == 0 && drawfcn == 0)
				setcbits(i, ftrans(fbits(i), cbits(i)));
		}
	}
#ifdef	NROFF
	if (xon && xflag)
		return 1;
#endif	/* NROFF */
	pchar1(i);
	return 1;
}


void
pchar1(register tchar i)
{
	static int	_olt;
	tchar	_olp[1];
	register int j;
	filep	savip;
	extern void ptout(tchar);

	j = cbits(i);
	if (dip != &d[0]) {
		if (i == FLSS)
			dip->flss++;
		else if (dip->flss > 0)
			dip->flss--;
		else if (!ismot(i) && (cbits(i) > 32 || cbits(i) == XFUNC) &&
				!tflg)
			i |= DIBIT;
		wbf(i);
		dip->op = offset;
		return;
	}
	if (!tflg && !print) {
		if (j == '\n')
			dip->alss = dip->blss = 0;
		return;
	}
	if (no_out)
		return;
	if (tflg) {	/* transparent mode, undiverted */
		outtp(i);
		return;
	}
	if (cbits(i) == XFUNC) {
		switch (fbits(i)) {
		case OLT:
			olt = realloc(olt, (nolt + 1) * sizeof *olt);
			_olt = 1;
			return;
		case CHAR:
#ifndef	NROFF
			if (!ascii)
				break;
#endif	/* !NROFF */
			savip = ip;
			ip = charout[sbits(i)].op;
			app++;
			fmtchar++;
			while ((i = rbf()) != 0 && cbits(i) != '\n' &&
					cbits(i) != FLSS)
				pchar(i);
			fmtchar--;
			app--;
			ip = savip;
			return;
		}
	}
/*	if (cbits(i) == 'x')
		fmtchar = fmtchar; */
	if (_olt) {
		_olp[0] = i;
		olt[nolt++] = fetchrq(_olp);
		_olt = 0;
	}
#ifndef NROFF
	if (ascii)
		outascii(i);
	else
#endif
		ptout(i);
}

static void
outtp(tchar i)
{
#ifndef NROFF
	int	j = cbits(i);

#ifdef	EUC
	if (iscopy(i))
		fdprintf(ptid, "%lc", j);
	else
#endif	/* EUC */
		fdprintf(ptid, "%c", j);
#endif
}

#ifndef	NROFF
static void
outmb(tchar i)
{
	extern int nchtab;
	int j = cbits(i);
#ifdef	EUC
	wchar_t	wc;
	char	mb[MB_LEN_MAX+1];
	int	n;
	int	f;
#endif	/* EUC */

	if (j < 0177) {
		oput(j);
		return;
	}
#ifdef	EUC
	if (iscopy(i))
		wc = cbits(i);
	else {
		if ((f = fbits(i)) == 0)
			f = font;
		wc = tr2un(j, f);
	}
	if (wc != -1 && (n = wctomb(mb, wc)) > 0) {
		mb[n] = 0;
		oputs(mb);
	} else
#endif	/* EUC */
	if (j < 128 + nchtab) {
		oput('\\');
		oput('(');
		oput(chname[chtab[j-128]]);
		oput(chname[chtab[j-128]+1]);
	}
}

void
outascii (	/* print i in best-guess ascii */
    tchar i
)
{
	int j = cbits(i);
	int f = fbits(i);
	int k;

	if (j == FILLER)
		return;
	if (isadjspc(i))
		return;
	if (ismot(i)) {
		oput(' ');
		return;
	}
	if ((j < 0177 && j >= ' ') || j == '\n') {
		oput(j);
		return;
	}
	if (f == 0)
		f = xfont;
	if (j == DRAWFCN)
		oputs("\\D");
	else if (j == HYPHEN || j == MINUS)
		oput('-');
	else if (j == XON)
		oputs("\\X");
	else if (islig(i) && lgrevtab && lgrevtab[f] && lgrevtab[f][j]) {
		for (k = 0; lgrevtab[f][j][k]; k++)
			outmb(sfmask(i) | lgrevtab[f][j][k]);
	} else if (j == WORDSP)
		;	/* nothing at all */
	else if (j > 0177)
		outmb(i);
}
#endif


/*
 * now a macro
oput(i)
	register int	i;
{
	*obufp++ = i;
	if (obufp >= &obuf[OBUFSZ])
		flusho();
}
*/

void
oputs(register const char *i)
{
	while (*i != 0)
		oput(*i++&0377);
}


void
flusho(void)
{
	if (obufp == obuf)
		return;
	if (no_out == 0) {
		if (!toolate) {
			toolate++;
#ifdef NROFF
			set_tty();
			{
				char	*p = t.twinit;
				while (*p++)
					;
				if (p - t.twinit > 1)
					write(ptid, t.twinit, p - t.twinit - 1);
			}
#endif
		}
		toolate += write(ptid, obuf, obufp - obuf);
	}
	obufp = obuf;
}

void
caseoutput(void)
{
	tchar	i;

	copyf++;
	if (!skip(0)) {
		if (cbits(i = getch()) == '"')
			i = getch();
		while (i != 0) {
			outtp(i);
			if (cbits(i) == '\n')
				break;
			i = getch();
		}
	}
	copyf--;
}


void
done(int x)
{
	register int i;

	error |= x;
	dl = app = ds = lgf = 0;
	if (pgchars && !pglines) {
		donep = 1;
		tbreak();
		donep = 0;
	}
	if ((i = em)) {
		donef = -1;
		em = 0;
		if (control(i, 0))
			longjmp(sjbuf, 1);
	}
	if (!nfo)
		done3(0);
	mflg = 0;
	dip = &d[0];
	if (woff)
		wbt((tchar)0);
	if (pendw)
		getword(1);
	pendnf = 0;
	if (donef == 1)
		done1(0);
	donef = 1;
	ip = 0;
	frame = stk;
	nxf = calloc(1, sizeof *nxf);
	if (!ejf)
		tbreak();
	nflush++;
	eject((struct s *)0);
	longjmp(sjbuf, 1);
}


void
done1(int x) 
{
	error |= x;
	if (numtab[NL].val) {
		trap = 0;
		eject((struct s *)0);
		longjmp(sjbuf, 1);
	}
	if (nofeed) {
		ptlead();
		flusho();
		done3(0);
	} else {
		pttrailer();
		done2(0);
	}
}


void
done2(int x) 
{
	ptlead();
#ifndef NROFF
	if (!ascii)
		ptstop();
#endif
	flusho();
	done3(x);
}

void
done3(int x)
{
	error |= x;
	signal(SIGINT, SIG_IGN);
	signal(SIGTERM, SIG_IGN);
#ifdef NROFF
	twdone();
#endif
	if (ascii)
		mesg(1);
	exit(error);
}


void
edone(int x)
{
	frame = stk;
	nxf = calloc(1, sizeof *nxf);
	ip = 0;
	done(x);
}



void
casepi(void)
{
	register pid_t i;
	int	id[2];

	if (skip(1))
		return;
	if (toolate || !getname() || pipe(id) == -1 || (i = fork()) == -1) {
		errprint("Pipe not created.");
		return;
	}
	ptid = id[1];
	if (i > 0) {
		close(id[0]);
		toolate++;
		pipeflg = i;
		return;
	}
	close(0);
	dup(id[0]);
	close(id[1]);
	execl(nextf, nextf, NULL);
	errprint("Cannot exec %s", nextf);
	exit(-4);
}