GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.bin/mandoc/term_ascii.c Lines: 102 116 87.9 %
Date: 2017-11-13 Branches: 34 51 66.7 %

Line Branch Exec Source
1
/*	$OpenBSD: term_ascii.c,v 1.44 2017/08/23 10:50:11 schwarze Exp $ */
2
/*
3
 * Copyright (c) 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4
 * Copyright (c) 2014, 2015, 2017 Ingo Schwarze <schwarze@openbsd.org>
5
 *
6
 * Permission to use, copy, modify, and distribute this software for any
7
 * purpose with or without fee is hereby granted, provided that the above
8
 * copyright notice and this permission notice appear in all copies.
9
 *
10
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
11
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
13
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17
 */
18
#include <sys/types.h>
19
20
#include <assert.h>
21
#include <locale.h>
22
#include <stdint.h>
23
#include <stdio.h>
24
#include <stdlib.h>
25
#include <unistd.h>
26
#include <wchar.h>
27
28
#include "mandoc.h"
29
#include "mandoc_aux.h"
30
#include "out.h"
31
#include "term.h"
32
#include "manconf.h"
33
#include "main.h"
34
35
static	struct termp	 *ascii_init(enum termenc, const struct manoutput *);
36
static	int		  ascii_hspan(const struct termp *,
37
				const struct roffsu *);
38
static	size_t		  ascii_width(const struct termp *, int);
39
static	void		  ascii_advance(struct termp *, size_t);
40
static	void		  ascii_begin(struct termp *);
41
static	void		  ascii_end(struct termp *);
42
static	void		  ascii_endline(struct termp *);
43
static	void		  ascii_letter(struct termp *, int);
44
static	void		  ascii_setwidth(struct termp *, int, int);
45
46
static	void		  locale_advance(struct termp *, size_t);
47
static	void		  locale_endline(struct termp *);
48
static	void		  locale_letter(struct termp *, int);
49
static	size_t		  locale_width(const struct termp *, int);
50
51
52
static struct termp *
53
ascii_init(enum termenc enc, const struct manoutput *outopts)
54
{
55
	char		*v;
56
	struct termp	*p;
57
58
4300
	p = mandoc_calloc(1, sizeof(*p));
59
2150
	p->tcol = p->tcols = mandoc_calloc(1, sizeof(*p->tcol));
60
2150
	p->maxtcol = 1;
61
62
2150
	p->line = 1;
63
2150
	p->defrmargin = p->lastrmargin = 78;
64
2150
	p->fontq = mandoc_reallocarray(NULL,
65
2150
	     (p->fontsz = 8), sizeof(*p->fontq));
66
2150
	p->fontq[0] = p->fontl = TERMFONT_NONE;
67
68
2150
	p->begin = ascii_begin;
69
2150
	p->end = ascii_end;
70
2150
	p->hspan = ascii_hspan;
71
2150
	p->type = TERMTYPE_CHAR;
72
73
2150
	p->enc = TERMENC_ASCII;
74
2150
	p->advance = ascii_advance;
75
2150
	p->endline = ascii_endline;
76
2150
	p->letter = ascii_letter;
77
2150
	p->setwidth = ascii_setwidth;
78
2150
	p->width = ascii_width;
79
80
2150
	if (TERMENC_ASCII != enc) {
81
82
		/*
83
		 * Do not change any of this to LC_ALL.  It might break
84
		 * the formatting by subtly changing the behaviour of
85
		 * various functions, for example strftime(3).  As a
86
		 * worst case, it might even cause buffer overflows.
87
		 */
88
89
66
		v = TERMENC_LOCALE == enc ?
90
		    setlocale(LC_CTYPE, "") :
91
66
		    setlocale(LC_CTYPE, "en_US.UTF-8");
92

132
		if (NULL != v && MB_CUR_MAX > 1) {
93
66
			p->enc = enc;
94
66
			p->advance = locale_advance;
95
66
			p->endline = locale_endline;
96
66
			p->letter = locale_letter;
97
66
			p->width = locale_width;
98
66
		}
99
	}
100
101
2150
	if (outopts->mdoc) {
102
633
		p->mdocstyle = 1;
103
633
		p->defindent = 5;
104
633
	}
105
2150
	if (outopts->indent)
106
		p->defindent = outopts->indent;
107
2150
	if (outopts->width)
108
		p->defrmargin = outopts->width;
109
2150
	if (outopts->synopsisonly)
110
		p->synopsisonly = 1;
111
112
2150
	return p;
113
}
114
115
void *
116
ascii_alloc(const struct manoutput *outopts)
117
{
118
119
4168
	return ascii_init(TERMENC_ASCII, outopts);
120
}
121
122
void *
123
utf8_alloc(const struct manoutput *outopts)
124
{
125
126
132
	return ascii_init(TERMENC_UTF8, outopts);
127
}
128
129
void *
130
locale_alloc(const struct manoutput *outopts)
131
{
132
133
	return ascii_init(TERMENC_LOCALE, outopts);
134
}
135
136
static void
137
ascii_setwidth(struct termp *p, int iop, int width)
138
{
139
140
60
	width /= 24;
141
30
	p->tcol->rmargin = p->defrmargin;
142
30
	if (iop > 0)
143
6
		p->defrmargin += width;
144
24
	else if (iop == 0)
145
72
		p->defrmargin = width ? (size_t)width : p->lastrmargin;
146
	else if (p->defrmargin > (size_t)width)
147
		p->defrmargin -= width;
148
	else
149
		p->defrmargin = 0;
150
30
	p->lastrmargin = p->tcol->rmargin;
151
30
	p->tcol->rmargin = p->maxrmargin = p->defrmargin;
152
30
}
153
154
void
155
terminal_sepline(void *arg)
156
{
157
	struct termp	*p;
158
	size_t		 i;
159
160
	p = (struct termp *)arg;
161
	(*p->endline)(p);
162
	for (i = 0; i < p->defrmargin; i++)
163
		(*p->letter)(p, '-');
164
	(*p->endline)(p);
165
	(*p->endline)(p);
166
}
167
168
static size_t
169
ascii_width(const struct termp *p, int c)
170
{
171
172
4789000
	return 1;
173
}
174
175
void
176
ascii_free(void *arg)
177
{
178
179
4300
	term_free((struct termp *)arg);
180
2150
}
181
182
static void
183
ascii_letter(struct termp *p, int c)
184
{
185
186
2988930
	putchar(c);
187
996310
}
188
189
static void
190
ascii_begin(struct termp *p)
191
{
192
193
4300
	(*p->headf)(p, p->argf);
194
2150
}
195
196
static void
197
ascii_end(struct termp *p)
198
{
199
200
4300
	(*p->footf)(p, p->argf);
201
2150
}
202
203
static void
204
ascii_endline(struct termp *p)
205
{
206
207
110766
	p->line++;
208
55383
	p->tcol->offset -= p->ti;
209
55383
	p->ti = 0;
210
110766
	putchar('\n');
211
55383
}
212
213
static void
214
ascii_advance(struct termp *p, size_t len)
215
{
216
	size_t		i;
217
218
1534465
	for (i = 0; i < len; i++)
219
1036726
		putchar(' ');
220
165913
}
221
222
static int
223
ascii_hspan(const struct termp *p, const struct roffsu *su)
224
{
225
	double		 r;
226
227


23448
	switch (su->unit) {
228
	case SCALE_BU:
229
3
		r = su->scale;
230
3
		break;
231
	case SCALE_CM:
232
6
		r = su->scale * 240.0 / 2.54;
233
6
		break;
234
	case SCALE_FS:
235
3
		r = su->scale * 65536.0;
236
3
		break;
237
	case SCALE_IN:
238
6702
		r = su->scale * 240.0;
239
6702
		break;
240
	case SCALE_MM:
241
15
		r = su->scale * 0.24;
242
15
		break;
243
	case SCALE_VS:
244
	case SCALE_PC:
245
3
		r = su->scale * 40.0;
246
3
		break;
247
	case SCALE_PT:
248
3
		r = su->scale * 10.0 / 3.0;
249
3
		break;
250
	case SCALE_EN:
251
	case SCALE_EM:
252
4989
		r = su->scale * 24.0;
253
4989
		break;
254
	default:
255
		abort();
256
	}
257
11724
	return r > 0.0 ? r + 0.01 : r - 0.01;
258
}
259
260
const char *
261
ascii_uc2str(int uc)
262
{
263
	static const char nbrsp[2] = { ASCII_NBRSP, '\0' };
264
	static const char *tab[] = {
265
	"<NUL>","<SOH>","<STX>","<ETX>","<EOT>","<ENQ>","<ACK>","<BEL>",
266
	"<BS>",	"\t",	"<LF>",	"<VT>",	"<FF>",	"<CR>",	"<SO>",	"<SI>",
267
	"<DLE>","<DC1>","<DC2>","<DC3>","<DC4>","<NAK>","<SYN>","<ETB>",
268
	"<CAN>","<EM>",	"<SUB>","<ESC>","<FS>",	"<GS>",	"<RS>",	"<US>",
269
	" ",	"!",	"\"",	"#",	"$",	"%",	"&",	"'",
270
	"(",	")",	"*",	"+",	",",	"-",	".",	"/",
271
	"0",	"1",	"2",	"3",	"4",	"5",	"6",	"7",
272
	"8",	"9",	":",	";",	"<",	"=",	">",	"?",
273
	"@",	"A",	"B",	"C",	"D",	"E",	"F",	"G",
274
	"H",	"I",	"J",	"K",	"L",	"M",	"N",	"O",
275
	"P",	"Q",	"R",	"S",	"T",	"U",	"V",	"W",
276
	"X",	"Y",	"Z",	"[",	"\\",	"]",	"^",	"_",
277
	"`",	"a",	"b",	"c",	"d",	"e",	"f",	"g",
278
	"h",	"i",	"j",	"k",	"l",	"m",	"n",	"o",
279
	"p",	"q",	"r",	"s",	"t",	"u",	"v",	"w",
280
	"x",	"y",	"z",	"{",	"|",	"}",	"~",	"<DEL>",
281
	"<80>",	"<81>",	"<82>",	"<83>",	"<84>",	"<85>",	"<86>",	"<87>",
282
	"<88>",	"<89>",	"<8A>",	"<8B>",	"<8C>",	"<8D>",	"<8E>",	"<8F>",
283
	"<90>",	"<91>",	"<92>",	"<93>",	"<94>",	"<95>",	"<96>",	"<97>",
284
	"<98>",	"<99>",	"<9A>",	"<9B>",	"<9C>",	"<9D>",	"<9E>",	"<9F>",
285
	nbrsp,	"!",	"/\bc",	"GBP",	"o\bx",	"=\bY",	"|",	"<section>",
286
	"\"",	"(C)",	"_\ba",	"<<",	"~",	"",	"(R)",	"-",
287
	"<degree>","+-","^2",	"^3",	"'","<micro>","<paragraph>",".",
288
	",",	"^1",	"_\bo",	">>",	"1/4",	"1/2",	"3/4",	"?",
289
	"`\bA",	"'\bA",	"^\bA",	"~\bA",	"\"\bA","o\bA",	"AE",	",\bC",
290
	"`\bE",	"'\bE",	"^\bE",	"\"\bE","`\bI",	"'\bI",	"^\bI",	"\"\bI",
291
	"Dh",	"~\bN",	"`\bO",	"'\bO",	"^\bO",	"~\bO",	"\"\bO","x",
292
	"/\bO",	"`\bU",	"'\bU",	"^\bU",	"\"\bU","'\bY",	"Th",	"ss",
293
	"`\ba",	"'\ba",	"^\ba",	"~\ba",	"\"\ba","o\ba",	"ae",	",\bc",
294
	"`\be",	"'\be",	"^\be",	"\"\be","`\bi",	"'\bi",	"^\bi",	"\"\bi",
295
	"dh",	"~\bn",	"`\bo",	"'\bo",	"^\bo",	"~\bo",	"\"\bo","/",
296
	"/\bo",	"`\bu",	"'\bu",	"^\bu",	"\"\bu","'\by",	"th",	"\"\by",
297
	"A",	"a",	"A",	"a",	"A",	"a",	"'\bC",	"'\bc",
298
	"^\bC",	"^\bc",	"C",	"c",	"C",	"c",	"D",	"d",
299
	"/\bD",	"/\bd",	"E",	"e",	"E",	"e",	"E",	"e",
300
	"E",	"e",	"E",	"e",	"^\bG",	"^\bg",	"G",	"g",
301
	"G",	"g",	",\bG",	",\bg",	"^\bH",	"^\bh",	"/\bH",	"/\bh",
302
	"~\bI",	"~\bi",	"I",	"i",	"I",	"i",	"I",	"i",
303
	"I",	"i",	"IJ",	"ij",	"^\bJ",	"^\bj",	",\bK",	",\bk",
304
	"q",	"'\bL",	"'\bl",	",\bL",	",\bl",	"L",	"l",	"L",
305
	"l",	"/\bL",	"/\bl",	"'\bN",	"'\bn",	",\bN",	",\bn",	"N",
306
	"n",	"'n",	"Ng",	"ng",	"O",	"o",	"O",	"o",
307
	"O",	"o",	"OE",	"oe",	"'\bR",	"'\br",	",\bR",	",\br",
308
	"R",	"r",	"'\bS",	"'\bs",	"^\bS",	"^\bs",	",\bS",	",\bs",
309
	"S",	"s",	",\bT",	",\bt",	"T",	"t",	"/\bT",	"/\bt",
310
	"~\bU",	"~\bu",	"U",	"u",	"U",	"u",	"U",	"u",
311
	"U",	"u",	"U",	"u",	"^\bW",	"^\bw",	"^\bY",	"^\by",
312
	"\"\bY","'\bZ",	"'\bz",	"Z",	"z",	"Z",	"z",	"s",
313
	"b",	"B",	"B",	"b",	"6",	"6",	"O",	"C",
314
	"c",	"D",	"D",	"D",	"d",	"d",	"3",	"@",
315
	"E",	"F",	",\bf",	"G",	"G",	"hv",	"I",	"/\bI",
316
	"K",	"k",	"/\bl",	"l",	"W",	"N",	"n",	"~\bO",
317
	"O",	"o",	"OI",	"oi",	"P",	"p",	"YR",	"2",
318
	"2",	"SH",	"sh",	"t",	"T",	"t",	"T",	"U",
319
	"u",	"Y",	"V",	"Y",	"y",	"/\bZ",	"/\bz",	"ZH",
320
	"ZH",	"zh",	"zh",	"/\b2",	"5",	"5",	"ts",	"w",
321
	"|",	"||",	"|=",	"!",	"DZ",	"Dz",	"dz",	"LJ",
322
	"Lj",	"lj",	"NJ",	"Nj",	"nj",	"A",	"a",	"I",
323
	"i",	"O",	"o",	"U",	"u",	"U",	"u",	"U",
324
	"u",	"U",	"u",	"U",	"u",	"@",	"A",	"a",
325
	"A",	"a",	"AE",	"ae",	"/\bG",	"/\bg",	"G",	"g",
326
	"K",	"k",	"O",	"o",	"O",	"o",	"ZH",	"zh",
327
	"j",	"DZ",	"Dz",	"dz",	"'\bG",	"'\bg",	"HV",	"W",
328
	"`\bN",	"`\bn",	"A",	"a",	"'\bAE","'\bae","O",	"o"};
329
330
4930
	assert(uc >= 0);
331
2465
	if ((size_t)uc < sizeof(tab)/sizeof(tab[0]))
332
1005
		return tab[uc];
333
1460
	return mchars_uc2str(uc);
334
2465
}
335
336
static size_t
337
locale_width(const struct termp *p, int c)
338
{
339
	int		rc;
340
341
251094
	if (c == ASCII_NBRSP)
342
1299
		c = ' ';
343
125547
	rc = wcwidth(c);
344
125547
	if (rc < 0)
345
		rc = 0;
346
125547
	return rc;
347
}
348
349
static void
350
locale_advance(struct termp *p, size_t len)
351
{
352
	size_t		i;
353
354
84882
	for (i = 0; i < len; i++)
355
26763
		putwchar(L' ');
356
10452
}
357
358
static void
359
locale_endline(struct termp *p)
360
{
361
362
4704
	p->line++;
363
2352
	p->tcol->offset -= p->ti;
364
2352
	p->ti = 0;
365
2352
	putwchar(L'\n');
366
2352
}
367
368
static void
369
locale_letter(struct termp *p, int c)
370
{
371
372
99030
	putwchar(c);
373
49515
}