xref: /openbsd-src/usr.bin/mandoc/term_ascii.c (revision 46035553bfdd96e63c94e32da0210227ec2e3cf1)
1 /* $OpenBSD: term_ascii.c,v 1.52 2020/09/09 13:40:24 schwarze Exp $ */
2 /*
3  * Copyright (c) 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4  * Copyright (c) 2014,2015,2017,2018,2020 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 <langinfo.h>
22 #include <locale.h>
23 #include <stdint.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <wchar.h>
29 
30 #include "mandoc.h"
31 #include "mandoc_aux.h"
32 #include "out.h"
33 #include "term.h"
34 #include "manconf.h"
35 #include "main.h"
36 
37 #define UTF8_LOCALE	"en_US.UTF-8"
38 
39 static	struct termp	 *ascii_init(enum termenc, const struct manoutput *);
40 static	int		  ascii_hspan(const struct termp *,
41 				const struct roffsu *);
42 static	size_t		  ascii_width(const struct termp *, int);
43 static	void		  ascii_advance(struct termp *, size_t);
44 static	void		  ascii_begin(struct termp *);
45 static	void		  ascii_end(struct termp *);
46 static	void		  ascii_endline(struct termp *);
47 static	void		  ascii_letter(struct termp *, int);
48 static	void		  ascii_setwidth(struct termp *, int, int);
49 
50 static	void		  locale_advance(struct termp *, size_t);
51 static	void		  locale_endline(struct termp *);
52 static	void		  locale_letter(struct termp *, int);
53 static	size_t		  locale_width(const struct termp *, int);
54 
55 
56 static struct termp *
57 ascii_init(enum termenc enc, const struct manoutput *outopts)
58 {
59 	char		*v;
60 	struct termp	*p;
61 
62 	p = mandoc_calloc(1, sizeof(*p));
63 	p->tcol = p->tcols = mandoc_calloc(1, sizeof(*p->tcol));
64 	p->maxtcol = 1;
65 
66 	p->line = 1;
67 	p->defrmargin = p->lastrmargin = 78;
68 	p->fontq = mandoc_reallocarray(NULL,
69 	     (p->fontsz = 8), sizeof(*p->fontq));
70 	p->fontq[0] = p->fontl = TERMFONT_NONE;
71 
72 	p->begin = ascii_begin;
73 	p->end = ascii_end;
74 	p->hspan = ascii_hspan;
75 	p->type = TERMTYPE_CHAR;
76 
77 	p->enc = TERMENC_ASCII;
78 	p->advance = ascii_advance;
79 	p->endline = ascii_endline;
80 	p->letter = ascii_letter;
81 	p->setwidth = ascii_setwidth;
82 	p->width = ascii_width;
83 
84 	if (enc != TERMENC_ASCII) {
85 
86 		/*
87 		 * Do not change any of this to LC_ALL.  It might break
88 		 * the formatting by subtly changing the behaviour of
89 		 * various functions, for example strftime(3).  As a
90 		 * worst case, it might even cause buffer overflows.
91 		 */
92 
93 		v = enc == TERMENC_LOCALE ?
94 		    setlocale(LC_CTYPE, "") :
95 		    setlocale(LC_CTYPE, UTF8_LOCALE);
96 
97 		/*
98 		 * We only support UTF-8,
99 		 * so revert to ASCII for anything else.
100 		 */
101 
102 		if (v != NULL &&
103 		    strcmp(nl_langinfo(CODESET), "UTF-8") != 0)
104 			v = setlocale(LC_CTYPE, "C");
105 
106 		if (v != NULL && MB_CUR_MAX > 1) {
107 			p->enc = TERMENC_UTF8;
108 			p->advance = locale_advance;
109 			p->endline = locale_endline;
110 			p->letter = locale_letter;
111 			p->width = locale_width;
112 		}
113 	}
114 
115 	if (outopts->mdoc) {
116 		p->mdocstyle = 1;
117 		p->defindent = 5;
118 	}
119 	if (outopts->indent)
120 		p->defindent = outopts->indent;
121 	if (outopts->width)
122 		p->defrmargin = outopts->width;
123 	if (outopts->synopsisonly)
124 		p->synopsisonly = 1;
125 
126 	assert(p->defindent < UINT16_MAX);
127 	assert(p->defrmargin < UINT16_MAX);
128 	return p;
129 }
130 
131 void *
132 ascii_alloc(const struct manoutput *outopts)
133 {
134 
135 	return ascii_init(TERMENC_ASCII, outopts);
136 }
137 
138 void *
139 utf8_alloc(const struct manoutput *outopts)
140 {
141 
142 	return ascii_init(TERMENC_UTF8, outopts);
143 }
144 
145 void *
146 locale_alloc(const struct manoutput *outopts)
147 {
148 
149 	return ascii_init(TERMENC_LOCALE, outopts);
150 }
151 
152 static void
153 ascii_setwidth(struct termp *p, int iop, int width)
154 {
155 
156 	width /= 24;
157 	p->tcol->rmargin = p->defrmargin;
158 	if (iop > 0)
159 		p->defrmargin += width;
160 	else if (iop == 0)
161 		p->defrmargin = width ? (size_t)width : p->lastrmargin;
162 	else if (p->defrmargin > (size_t)width)
163 		p->defrmargin -= width;
164 	else
165 		p->defrmargin = 0;
166 	if (p->defrmargin > 1000)
167 		p->defrmargin = 1000;
168 	p->lastrmargin = p->tcol->rmargin;
169 	p->tcol->rmargin = p->maxrmargin = p->defrmargin;
170 }
171 
172 void
173 terminal_sepline(void *arg)
174 {
175 	struct termp	*p;
176 	size_t		 i;
177 
178 	p = (struct termp *)arg;
179 	(*p->endline)(p);
180 	for (i = 0; i < p->defrmargin; i++)
181 		(*p->letter)(p, '-');
182 	(*p->endline)(p);
183 	(*p->endline)(p);
184 }
185 
186 static size_t
187 ascii_width(const struct termp *p, int c)
188 {
189 	return c != ASCII_BREAK;
190 }
191 
192 void
193 ascii_free(void *arg)
194 {
195 
196 	term_free((struct termp *)arg);
197 }
198 
199 static void
200 ascii_letter(struct termp *p, int c)
201 {
202 
203 	putchar(c);
204 }
205 
206 static void
207 ascii_begin(struct termp *p)
208 {
209 
210 	(*p->headf)(p, p->argf);
211 }
212 
213 static void
214 ascii_end(struct termp *p)
215 {
216 
217 	(*p->footf)(p, p->argf);
218 }
219 
220 static void
221 ascii_endline(struct termp *p)
222 {
223 
224 	p->line++;
225 	if ((int)p->tcol->offset > p->ti)
226 		p->tcol->offset -= p->ti;
227 	else
228 		p->tcol->offset = 0;
229 	p->ti = 0;
230 	putchar('\n');
231 }
232 
233 static void
234 ascii_advance(struct termp *p, size_t len)
235 {
236 	size_t		i;
237 
238 	/*
239 	 * XXX We used to have "assert(len < UINT16_MAX)" here.
240 	 * that is not quite right because the input document
241 	 * can trigger that by merely providing large input.
242 	 * For now, simply truncate.
243 	 */
244 	if (len > 256)
245 		len = 256;
246 	for (i = 0; i < len; i++)
247 		putchar(' ');
248 }
249 
250 static int
251 ascii_hspan(const struct termp *p, const struct roffsu *su)
252 {
253 	double		 r;
254 
255 	switch (su->unit) {
256 	case SCALE_BU:
257 		r = su->scale;
258 		break;
259 	case SCALE_CM:
260 		r = su->scale * 240.0 / 2.54;
261 		break;
262 	case SCALE_FS:
263 		r = su->scale * 65536.0;
264 		break;
265 	case SCALE_IN:
266 		r = su->scale * 240.0;
267 		break;
268 	case SCALE_MM:
269 		r = su->scale * 0.24;
270 		break;
271 	case SCALE_VS:
272 	case SCALE_PC:
273 		r = su->scale * 40.0;
274 		break;
275 	case SCALE_PT:
276 		r = su->scale * 10.0 / 3.0;
277 		break;
278 	case SCALE_EN:
279 	case SCALE_EM:
280 		r = su->scale * 24.0;
281 		break;
282 	default:
283 		abort();
284 	}
285 	return r > 0.0 ? r + 0.01 : r - 0.01;
286 }
287 
288 const char *
289 ascii_uc2str(int uc)
290 {
291 	static const char nbrsp[2] = { ASCII_NBRSP, '\0' };
292 	static const char *tab[] = {
293 	"<NUL>","<SOH>","<STX>","<ETX>","<EOT>","<ENQ>","<ACK>","<BEL>",
294 	"<BS>",	"\t",	"<LF>",	"<VT>",	"<FF>",	"<CR>",	"<SO>",	"<SI>",
295 	"<DLE>","<DC1>","<DC2>","<DC3>","<DC4>","<NAK>","<SYN>","<ETB>",
296 	"<CAN>","<EM>",	"<SUB>","<ESC>","<FS>",	"<GS>",	"<RS>",	"<US>",
297 	" ",	"!",	"\"",	"#",	"$",	"%",	"&",	"'",
298 	"(",	")",	"*",	"+",	",",	"-",	".",	"/",
299 	"0",	"1",	"2",	"3",	"4",	"5",	"6",	"7",
300 	"8",	"9",	":",	";",	"<",	"=",	">",	"?",
301 	"@",	"A",	"B",	"C",	"D",	"E",	"F",	"G",
302 	"H",	"I",	"J",	"K",	"L",	"M",	"N",	"O",
303 	"P",	"Q",	"R",	"S",	"T",	"U",	"V",	"W",
304 	"X",	"Y",	"Z",	"[",	"\\",	"]",	"^",	"_",
305 	"`",	"a",	"b",	"c",	"d",	"e",	"f",	"g",
306 	"h",	"i",	"j",	"k",	"l",	"m",	"n",	"o",
307 	"p",	"q",	"r",	"s",	"t",	"u",	"v",	"w",
308 	"x",	"y",	"z",	"{",	"|",	"}",	"~",	"<DEL>",
309 	"<80>",	"<81>",	"<82>",	"<83>",	"<84>",	"<85>",	"<86>",	"<87>",
310 	"<88>",	"<89>",	"<8A>",	"<8B>",	"<8C>",	"<8D>",	"<8E>",	"<8F>",
311 	"<90>",	"<91>",	"<92>",	"<93>",	"<94>",	"<95>",	"<96>",	"<97>",
312 	"<98>",	"<99>",	"<9A>",	"<9B>",	"<9C>",	"<9D>",	"<9E>",	"<9F>",
313 	nbrsp,	"!",	"/\bc",	"-\bL",	"o\bx",	"=\bY",	"|",	"<section>",
314 	"\"",	"(C)",	"_\ba",	"<<",	"~",	"",	"(R)",	"-",
315 	"<degree>","+-","^2",	"^3",	"'","<micro>","<paragraph>",".",
316 	",",	"^1",	"_\bo",	">>",	"1/4",	"1/2",	"3/4",	"?",
317 	"`\bA",	"'\bA",	"^\bA",	"~\bA",	"\"\bA","o\bA",	"AE",	",\bC",
318 	"`\bE",	"'\bE",	"^\bE",	"\"\bE","`\bI",	"'\bI",	"^\bI",	"\"\bI",
319 	"Dh",	"~\bN",	"`\bO",	"'\bO",	"^\bO",	"~\bO",	"\"\bO","x",
320 	"/\bO",	"`\bU",	"'\bU",	"^\bU",	"\"\bU","'\bY",	"Th",	"ss",
321 	"`\ba",	"'\ba",	"^\ba",	"~\ba",	"\"\ba","o\ba",	"ae",	",\bc",
322 	"`\be",	"'\be",	"^\be",	"\"\be","`\bi",	"'\bi",	"^\bi",	"\"\bi",
323 	"dh",	"~\bn",	"`\bo",	"'\bo",	"^\bo",	"~\bo",	"\"\bo","/",
324 	"/\bo",	"`\bu",	"'\bu",	"^\bu",	"\"\bu","'\by",	"th",	"\"\by",
325 	"A",	"a",	"A",	"a",	"A",	"a",	"'\bC",	"'\bc",
326 	"^\bC",	"^\bc",	"C",	"c",	"C",	"c",	"D",	"d",
327 	"/\bD",	"/\bd",	"E",	"e",	"E",	"e",	"E",	"e",
328 	"E",	"e",	"E",	"e",	"^\bG",	"^\bg",	"G",	"g",
329 	"G",	"g",	",\bG",	",\bg",	"^\bH",	"^\bh",	"/\bH",	"/\bh",
330 	"~\bI",	"~\bi",	"I",	"i",	"I",	"i",	"I",	"i",
331 	"I",	"i",	"IJ",	"ij",	"^\bJ",	"^\bj",	",\bK",	",\bk",
332 	"q",	"'\bL",	"'\bl",	",\bL",	",\bl",	"L",	"l",	"L",
333 	"l",	"/\bL",	"/\bl",	"'\bN",	"'\bn",	",\bN",	",\bn",	"N",
334 	"n",	"'n",	"Ng",	"ng",	"O",	"o",	"O",	"o",
335 	"O",	"o",	"OE",	"oe",	"'\bR",	"'\br",	",\bR",	",\br",
336 	"R",	"r",	"'\bS",	"'\bs",	"^\bS",	"^\bs",	",\bS",	",\bs",
337 	"S",	"s",	",\bT",	",\bt",	"T",	"t",	"/\bT",	"/\bt",
338 	"~\bU",	"~\bu",	"U",	"u",	"U",	"u",	"U",	"u",
339 	"U",	"u",	"U",	"u",	"^\bW",	"^\bw",	"^\bY",	"^\by",
340 	"\"\bY","'\bZ",	"'\bz",	"Z",	"z",	"Z",	"z",	"s",
341 	"b",	"B",	"B",	"b",	"6",	"6",	"O",	"C",
342 	"c",	"D",	"D",	"D",	"d",	"d",	"3",	"@",
343 	"E",	"F",	",\bf",	"G",	"G",	"hv",	"I",	"/\bI",
344 	"K",	"k",	"/\bl",	"l",	"W",	"N",	"n",	"~\bO",
345 	"O",	"o",	"OI",	"oi",	"P",	"p",	"YR",	"2",
346 	"2",	"SH",	"sh",	"t",	"T",	"t",	"T",	"U",
347 	"u",	"Y",	"V",	"Y",	"y",	"/\bZ",	"/\bz",	"ZH",
348 	"ZH",	"zh",	"zh",	"/\b2",	"5",	"5",	"ts",	"w",
349 	"|",	"||",	"|=",	"!",	"DZ",	"Dz",	"dz",	"LJ",
350 	"Lj",	"lj",	"NJ",	"Nj",	"nj",	"A",	"a",	"I",
351 	"i",	"O",	"o",	"U",	"u",	"U",	"u",	"U",
352 	"u",	"U",	"u",	"U",	"u",	"@",	"A",	"a",
353 	"A",	"a",	"AE",	"ae",	"/\bG",	"/\bg",	"G",	"g",
354 	"K",	"k",	"O",	"o",	"O",	"o",	"ZH",	"zh",
355 	"j",	"DZ",	"Dz",	"dz",	"'\bG",	"'\bg",	"HV",	"W",
356 	"`\bN",	"`\bn",	"A",	"a",	"'\bAE","'\bae","O",	"o"};
357 
358 	assert(uc >= 0);
359 	if ((size_t)uc < sizeof(tab)/sizeof(tab[0]))
360 		return tab[uc];
361 	return mchars_uc2str(uc);
362 }
363 
364 static size_t
365 locale_width(const struct termp *p, int c)
366 {
367 	int		rc;
368 
369 	if (c == ASCII_NBRSP)
370 		c = ' ';
371 	rc = wcwidth(c);
372 	if (rc < 0)
373 		rc = 0;
374 	return rc;
375 }
376 
377 static void
378 locale_advance(struct termp *p, size_t len)
379 {
380 	size_t		i;
381 
382 	/*
383 	 * XXX We used to have "assert(len < UINT16_MAX)" here.
384 	 * that is not quite right because the input document
385 	 * can trigger that by merely providing large input.
386 	 * For now, simply truncate.
387 	 */
388 	if (len > 256)
389 		len = 256;
390 	for (i = 0; i < len; i++)
391 		putwchar(L' ');
392 }
393 
394 static void
395 locale_endline(struct termp *p)
396 {
397 
398 	p->line++;
399 	if ((int)p->tcol->offset > p->ti)
400 		p->tcol->offset -= p->ti;
401 	else
402 		p->tcol->offset = 0;
403 	p->ti = 0;
404 	putwchar(L'\n');
405 }
406 
407 static void
408 locale_letter(struct termp *p, int c)
409 {
410 
411 	putwchar(c);
412 }
413