xref: /openbsd-src/usr.bin/mandoc/term_ascii.c (revision 2777ee89d0e541ec819d05abee114837837abbec)
1 /*	$OpenBSD: term_ascii.c,v 1.38 2015/11/12 21:49:29 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 ascii_sepline(void *arg)
155 {
156 	struct termp	*p;
157 	size_t		 i;
158 
159 	p = (struct termp *)arg;
160 	p->line += 3;
161 	putchar('\n');
162 	for (i = 0; i < p->defrmargin; i++)
163 		putchar('-');
164 	putchar('\n');
165 	putchar('\n');
166 }
167 
168 static size_t
169 ascii_width(const struct termp *p, int c)
170 {
171 
172 	return 1;
173 }
174 
175 void
176 ascii_free(void *arg)
177 {
178 
179 	term_free((struct termp *)arg);
180 }
181 
182 static void
183 ascii_letter(struct termp *p, int c)
184 {
185 
186 	putchar(c);
187 }
188 
189 static void
190 ascii_begin(struct termp *p)
191 {
192 
193 	(*p->headf)(p, p->argf);
194 }
195 
196 static void
197 ascii_end(struct termp *p)
198 {
199 
200 	(*p->footf)(p, p->argf);
201 }
202 
203 static void
204 ascii_endline(struct termp *p)
205 {
206 
207 	p->line++;
208 	putchar('\n');
209 }
210 
211 static void
212 ascii_advance(struct termp *p, size_t len)
213 {
214 	size_t		i;
215 
216 	for (i = 0; i < len; i++)
217 		putchar(' ');
218 }
219 
220 static int
221 ascii_hspan(const struct termp *p, const struct roffsu *su)
222 {
223 	double		 r;
224 
225 	switch (su->unit) {
226 	case SCALE_BU:
227 		r = su->scale;
228 		break;
229 	case SCALE_CM:
230 		r = su->scale * 240.0 / 2.54;
231 		break;
232 	case SCALE_FS:
233 		r = su->scale * 65536.0;
234 		break;
235 	case SCALE_IN:
236 		r = su->scale * 240.0;
237 		break;
238 	case SCALE_MM:
239 		r = su->scale * 0.24;
240 		break;
241 	case SCALE_VS:
242 	case SCALE_PC:
243 		r = su->scale * 40.0;
244 		break;
245 	case SCALE_PT:
246 		r = su->scale * 10.0 / 3.0;
247 		break;
248 	case SCALE_EN:
249 	case SCALE_EM:
250 		r = su->scale * 24.0;
251 		break;
252 	default:
253 		abort();
254 	}
255 	return r > 0.0 ? r + 0.01 : r - 0.01;
256 }
257 
258 const char *
259 ascii_uc2str(int uc)
260 {
261 	static const char nbrsp[2] = { ASCII_NBRSP, '\0' };
262 	static const char *tab[] = {
263 	"<NUL>","<SOH>","<STX>","<ETX>","<EOT>","<ENQ>","<ACK>","<BEL>",
264 	"<BS>",	"\t",	"<LF>",	"<VT>",	"<FF>",	"<CR>",	"<SO>",	"<SI>",
265 	"<DLE>","<DC1>","<DC2>","<DC3>","<DC4>","<NAK>","<SYN>","<ETB>",
266 	"<CAN>","<EM>",	"<SUB>","<ESC>","<FS>",	"<GS>",	"<RS>",	"<US>",
267 	" ",	"!",	"\"",	"#",	"$",	"%",	"&",	"'",
268 	"(",	")",	"*",	"+",	",",	"-",	".",	"/",
269 	"0",	"1",	"2",	"3",	"4",	"5",	"6",	"7",
270 	"8",	"9",	":",	";",	"<",	"=",	">",	"?",
271 	"@",	"A",	"B",	"C",	"D",	"E",	"F",	"G",
272 	"H",	"I",	"J",	"K",	"L",	"M",	"N",	"O",
273 	"P",	"Q",	"R",	"S",	"T",	"U",	"V",	"W",
274 	"X",	"Y",	"Z",	"[",	"\\",	"]",	"^",	"_",
275 	"`",	"a",	"b",	"c",	"d",	"e",	"f",	"g",
276 	"h",	"i",	"j",	"k",	"l",	"m",	"n",	"o",
277 	"p",	"q",	"r",	"s",	"t",	"u",	"v",	"w",
278 	"x",	"y",	"z",	"{",	"|",	"}",	"~",	"<DEL>",
279 	"<80>",	"<81>",	"<82>",	"<83>",	"<84>",	"<85>",	"<86>",	"<87>",
280 	"<88>",	"<89>",	"<8A>",	"<8B>",	"<8C>",	"<8D>",	"<8E>",	"<8F>",
281 	"<90>",	"<91>",	"<92>",	"<93>",	"<94>",	"<95>",	"<96>",	"<97>",
282 	"<99>",	"<99>",	"<9A>",	"<9B>",	"<9C>",	"<9D>",	"<9E>",	"<9F>",
283 	nbrsp,	"!",	"/\bc",	"GBP",	"o\bx",	"=\bY",	"|",	"<sec>",
284 	"\"",	"(C)",	"_\ba",	"<<",	"~",	"",	"(R)",	"-",
285 	"<deg>","+-",	"2",	"3",	"'",	",\bu",	"<par>",".",
286 	",",	"1",	"_\bo",	">>",	"1/4",	"1/2",	"3/4",	"?",
287 	"`\bA",	"'\bA",	"^\bA",	"~\bA",	"\"\bA","o\bA",	"AE",	",\bC",
288 	"`\bE",	"'\bE",	"^\bE",	"\"\bE","`\bI",	"'\bI",	"^\bI",	"\"\bI",
289 	"-\bD",	"~\bN",	"`\bO",	"'\bO",	"^\bO",	"~\bO",	"\"\bO","x",
290 	"/\bO",	"`\bU",	"'\bU",	"^\bU",	"\"\bU","'\bY",	"Th",	"ss",
291 	"`\ba",	"'\ba",	"^\ba",	"~\ba",	"\"\ba","o\ba",	"ae",	",\bc",
292 	"`\be",	"'\be",	"^\be",	"\"\be","`\bi",	"'\bi",	"^\bi",	"\"\bi",
293 	"d",	"~\bn",	"`\bo",	"'\bo",	"^\bo",	"~\bo",	"\"\bo","-:-",
294 	"/\bo",	"`\bu",	"'\bu",	"^\bu",	"\"\bu","'\by",	"th",	"\"\by",
295 	"A",	"a",	"A",	"a",	"A",	"a",	"'\bC",	"'\bc",
296 	"^\bC",	"^\bc",	"C",	"c",	"C",	"c",	"D",	"d",
297 	"/\bD",	"/\bd",	"E",	"e",	"E",	"e",	"E",	"e",
298 	"E",	"e",	"E",	"e",	"^\bG",	"^\bg",	"G",	"g",
299 	"G",	"g",	",\bG",	",\bg",	"^\bH",	"^\bh",	"/\bH",	"/\bh",
300 	"~\bI",	"~\bi",	"I",	"i",	"I",	"i",	"I",	"i",
301 	"I",	"i",	"IJ",	"ij",	"^\bJ",	"^\bj",	",\bK",	",\bk",
302 	"q",	"'\bL",	"'\bl",	",\bL",	",\bl",	"L",	"l",	"L",
303 	"l",	"/\bL",	"/\bl",	"'\bN",	"'\bn",	",\bN",	",\bn",	"N",
304 	"n",	"'n",	"Ng",	"ng",	"O",	"o",	"O",	"o",
305 	"O",	"o",	"OE",	"oe",	"'\bR",	"'\br",	",\bR",	",\br",
306 	"R",	"r",	"'\bS",	"'\bs",	"^\bS",	"^\bs",	",\bS",	",\bs",
307 	"S",	"s",	",\bT",	",\bt",	"T",	"t",	"/\bT",	"/\bt",
308 	"~\bU",	"~\bu",	"U",	"u",	"U",	"u",	"U",	"u",
309 	"U",	"u",	"U",	"u",	"^\bW",	"^\bw",	"^\bY",	"^\by",
310 	"\"\bY","'\bZ",	"'\bz",	"Z",	"z",	"Z",	"z",	"s",
311 	"b",	"B",	"B",	"b",	"6",	"6",	"O",	"C",
312 	"c",	"D",	"D",	"D",	"d",	"d",	"3",	"@",
313 	"E",	"F",	",\bf",	"G",	"G",	"hv",	"I",	"/\bI",
314 	"K",	"k",	"/\bl",	"l",	"W",	"N",	"n",	"~\bO",
315 	"O",	"o",	"OI",	"oi",	"P",	"p",	"YR",	"2",
316 	"2",	"SH",	"sh",	"t",	"T",	"t",	"T",	"U",
317 	"u",	"Y",	"V",	"Y",	"y",	"/\bZ",	"/\bz",	"ZH",
318 	"ZH",	"zh",	"zh",	"/\b2",	"5",	"5",	"ts",	"w",
319 	"|",	"||",	"|=",	"!",	"DZ",	"Dz",	"dz",	"LJ",
320 	"Lj",	"lj",	"NJ",	"Nj",	"nj",	"A",	"a",	"I",
321 	"i",	"O",	"o",	"U",	"u",	"U",	"u",	"U",
322 	"u",	"U",	"u",	"U",	"u",	"@",	"A",	"a",
323 	"A",	"a",	"AE",	"ae",	"/\bG",	"/\bg",	"G",	"g",
324 	"K",	"k",	"O",	"o",	"O",	"o",	"ZH",	"zh",
325 	"j",	"DZ",	"Dz",	"dz",	"'\bG",	"'\bg",	"HV",	"W",
326 	"`\bN",	"`\bn",	"A",	"a",	"'\bAE","'\bae","O",	"o"};
327 
328 	assert(uc >= 0);
329 	if ((size_t)uc < sizeof(tab)/sizeof(tab[0]))
330 		return tab[uc];
331 	return mchars_uc2str(uc);
332 }
333 
334 static size_t
335 locale_width(const struct termp *p, int c)
336 {
337 	int		rc;
338 
339 	if (c == ASCII_NBRSP)
340 		c = ' ';
341 	rc = wcwidth(c);
342 	if (rc < 0)
343 		rc = 0;
344 	return rc;
345 }
346 
347 static void
348 locale_advance(struct termp *p, size_t len)
349 {
350 	size_t		i;
351 
352 	for (i = 0; i < len; i++)
353 		putwchar(L' ');
354 }
355 
356 static void
357 locale_endline(struct termp *p)
358 {
359 
360 	p->line++;
361 	putwchar(L'\n');
362 }
363 
364 static void
365 locale_letter(struct termp *p, int c)
366 {
367 
368 	putwchar(c);
369 }
370