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