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