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