aboutsummaryrefslogtreecommitdiffstats
path: root/troff/nroff.d/n6.c
blob: a62101202ba9287acb6231a6efdd334bc0ece619 (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
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
/*
 * 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 2003 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 "n6.c	1.12	05/06/08 SMI"	*/

/*
 * Portions Copyright (c) 2005 Gunnar Ritter, Freiburg i. Br., Germany
 *
 * Sccsid @(#)n6.c	1.51 (gritter) 6/19/11
 */

/*
 * Changes Copyright (c) 2014 Carsten Kunze <carsten.kunze at arcor.de>
 */

/*
 * 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.
 */

#include <stdio.h>
#include <string.h>
//#ifdef	EUC
#include <limits.h>
#include <stdlib.h>
#include <wchar.h>
//#endif
#include <ctype.h>
#include "tdef.h"
#include "tw.h"
#include "pt.h"
#include "ext.h"

/*
 * n6.c -- width functions, sizes and fonts
*/

int	initbdtab[NFONT+1] ={ 0, 0, 0, 3, 3, 0, };
static int	sbold = 0;
int	initfontlab[NFONT+1] = { 0, 'R', 'I', 'B', PAIR('B','I'), 'S', 0 };

extern	int	nchtab;

int 
width(register tchar j)
{
	register int i, k;

	if (isadjspc(j))
		return(0);
	if (j & (ZBIT|MOT)) {
		if (iszbit(j))
			return(0);
		if (isvmot(j))
			return(0);
		k = absmot(j);
		if (isnmot(j))
			k = -k;
		return(k);
	}
	i = cbits(j);
	if (isxfunc(j, CHAR))
		return(charout[sbits(j)].width);
	if (i < ' ') {
		if (i == '\b')
			return(-widthp);
		if (i == PRESC)
			i = eschar;
		else if (iscontrol(i))
			return(0);
	}
	if ((size_t)i==ohc)
		return(0);
#ifdef EUC
	if (multi_locale && i >= nchtab + _SPECCHAR_ST) {
		i = tr2un(i, fbits(j));
		if ((i = wcwidth(i)) < 0)
			i = 0;
		k = t.Char * csi_width[i];
		widthp = k;
		return(k);
	}
#endif /* EUC */
	i = trtab[i];
	if (i < 32)
		return(0);
	k = t.width[i] * t.Char;
	widthp = k;
	return(k);
}


tchar 
setch(int delim)
{
	register int j;
	char	temp[40];
	register char	*s;

	s = temp;
	if (delim == 'C') {
		do {
			j = getach();
			if (s < &temp[sizeof temp - 1])
				*s++ = j;
		} while (j != 0 && (s == &temp[1] || j != temp[0]));
		if (s - temp == 3)
			return temp[1];
		else if (s - temp == 4) {
			temp[0] = temp[1];
			temp[1] = temp[2];
			s = &temp[2];
		} else {
			*s = 0;
			if (j != temp[0])
				nodelim(temp[0]);
			else if (warn & WARN_CHAR) {
				errprint("missing glyph \\C%s", temp);
			}
			return 0;
		}
	} else if (delim == '[' && (j = getach()) != ']') {
		*s++ = j;
		while ((j = getach()) != ']' && j != 0)
			if (s < &temp[sizeof temp - 1])
				*s++ = j;
		if (s - temp == 1)
			return temp[0];
		else if (s - temp != 2) {
			*s = '\0';
			if (j != ']')
				nodelim(']');
			else {
				size_t l = strlen(temp);
				if (gemu) {
					if (l == 5 && *temp == 'u'
					    && isxdigit((unsigned)temp[1])
					    && isxdigit((unsigned)temp[2])
					    && isxdigit((unsigned)temp[3])
					    && isxdigit((unsigned)temp[4])) {
						int n;
						n = strtol(temp + 1, NULL, 16);
						if (n)
							return setuc0(n);
					} else if ((l == 6 || (l == 7
					    && isdigit((unsigned)temp[6])))
					    && isdigit((unsigned)temp[5])
					    && isdigit((unsigned)temp[4])
					    && !strncmp(temp, "char", 4)) {
						int i = atoi(temp + 4);
						if (i <= 255)
							return i + nchtab +
							    _SPECCHAR_ST;
					}
				}
				if ((j = findch(temp)) > 0)
					return j | chbits;
				else if (warn & WARN_CHAR)
					errprint("missing glyph \\[%s]", temp);
				return 0;
			}
		}
	} else {
		if ((*s++ = getach()) == 0 || (*s++ = getach()) == 0)
			return(0);
	}
	*s = '\0';
	if ((j = findch(temp)) > 0)
		return j | chbits;
	else {
		if (warn & WARN_CHAR)
			errprint("missing glyph \\%c%s", delim, temp);
		return 0;
	}
}

tchar 
setabs (void)		/* set absolute char from \C'...' */
{			/* for now, a no-op */
	int n;

	getch();
	n = 0;
	n = inumb(&n);
	getch();
	if (nonumb || n + nchtab + _SPECCHAR_ST >= NCHARS)
		return 0;
	return n + nchtab + _SPECCHAR_ST;
}

int
tr2un(tchar c, int f __unused)
{
	int	k;

	k = cbits(c);
	if (k >= nchtab + _SPECCHAR_ST)
		return k - nchtab - _SPECCHAR_ST;
	if (k & ~0177)
		return 0;
	return k;
}

int 
findft(register int i, int required)
{
	register int k;

	if ((k = i - '0') >= 0 && k <= nfonts && k < smnt)
		return(k);
	for (k = 0; fontlab[k] != i; k++)
		if (k > nfonts) {
			if (required && warn & WARN_FONT)
				errprint("%s: no such font", macname(i));
			return(-1);
		}
	return(k);
}


void
caseps(void)
{
}


void
mchbits(void)
{
	chbits = 0;
	setfbits(chbits, font);
	ses = sps = width(' ' | chbits);
}


void
setps(void)
{
	tchar	c;
	register int i, j, k;

	noschr++;
	i = cbits(c = getch());
	if (noschr) noschr--;
	if (ismot(c) && xflag)
		return;
	if (ischar(i) && isdigit(i)) {		/* \sd or \sdd */
		i -= '0';
		if (i == 0)		/* \s0 */
			;
		else if (i <= 3 && ischar(j = cbits(ch = getch())) &&
		    isdigit(j)) {	/* \sdd */
			ch = 0;
		}
	} else if (i == '(') {		/* \s(dd */
		getch();
		getch();
	} else if (i == '+' || i == '-') {	/* \s+, \s- */
		j = cbits(c = getch());
		if (ischar(j) && isdigit(j)) {		/* \s+d, \s-d */
			;
		} else if (j == '(') {		/* \s+(dd, \s-(dd */
			getch();
			getch();
		} else if (xflag) {	/* \s+[dd], */
			k = j == '[' ? ']' : j;			/* \s-'dd' */
			setcbits(c, k);
			hatoi();
			if (nonumb)
				return;
			if (!issame(getch(), c))
				nodelim(k);
		}
	} else if (xflag) {  /* \s'+dd', \s[dd] */
		if (i == '[') {
			i = ']';
			setcbits(c, i);
		}
		j = inumb(&apts);
		if (nonumb)
			return;
		if (!issame(getch(), c))
			nodelim(i);
	}
}


tchar 
setht (void)		/* set character height from \H'...' */
{
	getch();
	inumb(&apts);
	getch();
	return(0);
}


tchar 
setslant (void)		/* set slant from \S'...' */
{
	int	n;

	getch();
	n = 0;
	n = inumb(&n);
	getch();
	return(0);
}


void
caseft(void)
{
	skip(0);
	setfont(1);
}


void
setfont(int a)
{
	register int i, j;

	if (a)
		i = getrq(3);
	else 
		i = getsn(0);
	if (!i || i == 'P') {
		j = font1;
		goto s0;
	}
	if (/* i == 'S' || */ i == '0')
		return;
	if ((j = findft(i, 0)) == -1) {
		if (xflag) {
			font1 = font;
		}
		return;
	}
s0:
	font1 = font;
	font = j;
	mchbits();
}


void
setwd(void)
{
	register int base, wid;
	register tchar i;
	tchar	delim;
	int	emsz, k;
	int	savhp, savapts, savapts1, savfont, savfont1, savpts, savpts1;
	int	savlgf;

	base = numtab[ST].val = wid = numtab[CT].val = 0;
	if (ismot(i = getch()))
		return;
	delim = i;
	savhp = numtab[HP].val;
	numtab[HP].val = 0;
	savapts = apts;
	savapts1 = apts1;
	savfont = font;
	savfont1 = font1;
	savpts = pts;
	savpts1 = pts1;
	savlgf = lgf;
	lgf = 0;
	setwdf++;
	while (i = getch(), !issame(i, delim) && !nlflg) {
		k = width(i);
		wid += k;
		numtab[HP].val += k;
		if (!ismot(i)) {
			emsz = (INCH * pts + 36) / 72;
		} else if (isvmot(i)) {
			k = absmot(i);
			if (isnmot(i))
				k = -k;
			base -= k;
			emsz = 0;
		} else 
			continue;
		if (base < numtab[SB].val)
			numtab[SB].val = base;
		if ((k = base + emsz) > numtab[ST].val)
			numtab[ST].val = k;
	}
	if (!issame(i, delim))
		nodelim(delim);
	setn1(wid, 0, (tchar) 0);
	setnr("rst", 0, 0);
	setnr("rsb", 0, 0);
	numtab[HP].val = savhp;
	apts = savapts;
	apts1 = savapts1;
	font = savfont;
	font1 = savfont1;
	pts = savpts;
	pts1 = savpts1;
	lgf = savlgf;
	mchbits();
	setwdf = 0;
}


tchar 
vmot(void)
{
	dfact = lss;
	vflag++;
	return(mot());
}


tchar 
hmot(void)
{
	dfact = EM;
	return(mot());
}


tchar 
mot(void)
{
	register int j, n;
	register tchar i;
	tchar	c, delim;

	j = HOR;
	noschr++;
	delim = getch(); /*eat delim*/
	if (noschr) noschr--;
	if ((n = hatoi())) {
		if (vflag)
			j = VERT;
		i = makem(quant(n, j));
	} else
		i = 0;
	noschr++;
	c = getch();
	if (noschr) noschr--;
	if (!issame(c, delim))
		nodelim(delim);
	vflag = 0;
	dfact = 1;
	return(i);
}


tchar 
sethl(int k)
{
	register int j;
	tchar i;

	j = t.Halfline;
	if (k == 'u')
		j = -j;
	else if (k == 'r')
		j = -2 * j;
	vflag++;
	i = makem(j);
	vflag = 0;
	return(i);
}


tchar 
makem(int i)
{
	register tchar j;

	if ((j = i) < 0)
		j = -j;
	j = sabsmot(j) | MOT;
	if (i < 0)
		j |= NMOT;
	if (vflag)
		j |= VMOT;
	return(j);
}


tchar 
getlg(tchar i)
{
	return(i);
}


void
caselg(void)
{
}

void
caseflig(void)
{
}

void
casefp(void)
{
	register int i, j;

	skip(1);
	if ((i = cbits(getch()) - '0') < 0 || i > nfonts)
		return;
	if (skip(1) || !(j = getrq(3)))
		return;
	fontlab[i] = j;
}

void
casefps(void)
{
	skip(1);
	getname();
	casefp();
}


void
casecs(void)
{
}


void
casebd(void)
{
	register int i, j = 0, k;

	k = 0;
bd0:
	if (skip(1) || !(i = getrq(0)) || (j = findft(i, 1)) == -1) {
		if (k)
			goto bd1;
		else 
			return;
	}
	if (j == smnt) {
		k = smnt;
		goto bd0;
	}
	if (k) {
		sbold = j;
		j = k;
	}
bd1:
	skip(0);
	noscale++;
	bdtab[j] = hatoi();
	noscale = 0;
}


void
casevs(void)
{
	register int i;

	skip(0);
	vflag++;
	dfact = INCH; /*default scaling is points!*/
	dfactd = 72;
	res = VERT;
	i = inumb(&lss);
	if (nonumb)
		i = lss1;
	if (xflag && i < 0) {
		if (warn & WARN_RANGE)
			errprint("negative vertical spacing ignored");
		i = lss1;
	}
	if (i < VERT)
		i = VERT;	/* was VERT */
	lss1 = lss;
	lss = i;
}




void
casess(void)
{
}


tchar 
xlss(void)
{
	/* stores \x'...' into
	 * two successive tchars.
	 * the first contains HX, the second the value,
	 * encoded as a vertical motion.
	 * decoding is done in n2.c by pchar().
	 */
	int	i;

	getch();
	dfact = lss;
	i = quant(hatoi(), VERT);
	dfact = 1;
	getch();
	if (i >= 0)
		pbbuf[pbp++] = MOT | VMOT | sabsmot(i);
	else
		pbbuf[pbp++] = MOT | VMOT | NMOT | sabsmot(-i);
	return(HX);
}

tchar
setuc0(int n)
{
	if (n & ~0177) {
#ifdef	EUC
		int	k;
		k = (n + nchtab + _SPECCHAR_ST) | chbits;
		if (k >= NCHARS)
			morechars(k);
		return k;
#else
		return 0;
#endif
	} else
		return n | chbits;
}

static void
discard(void)
{
	int	c, delim;

	if ((delim = getach()) != 0)
		do {
			if ((c = getach()) == 0) {
				if (cbits(ch) == ' ')
					ch = 0;
				else
					break;
			}
		} while (c != delim);
}

tchar
setanchor(void)
{
	discard();
	return 0;
}

tchar
setlink(void)
{
	if ((linkin = !linkin))
		discard();
	return 0;
}

tchar
setulink(void)
{
	if ((linkin = !linkin))
		discard();
	return 0;
}

void
casedummy(void){;}