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