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