1 /* $Id: term_ascii.c,v 1.66 2020/09/09 13:45:05 schwarze Exp $ */
3 * Copyright (c) 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2014,2015,2017,2018,2020 Ingo Schwarze <schwarze@openbsd.org>
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.
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.
20 #include <sys/types.h>
37 #include "mandoc_aux.h"
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);
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);
63 ascii_init(enum termenc enc
, const struct manoutput
*outopts
)
70 p
= mandoc_calloc(1, sizeof(*p
));
71 p
->tcol
= p
->tcols
= mandoc_calloc(1, sizeof(*p
->tcol
));
75 p
->defrmargin
= p
->lastrmargin
= 78;
76 p
->fontq
= mandoc_reallocarray(NULL
,
77 (p
->fontsz
= 8), sizeof(*p
->fontq
));
78 p
->fontq
[0] = p
->fontl
= TERMFONT_NONE
;
80 p
->begin
= ascii_begin
;
82 p
->hspan
= ascii_hspan
;
83 p
->type
= TERMTYPE_CHAR
;
85 p
->enc
= TERMENC_ASCII
;
86 p
->advance
= ascii_advance
;
87 p
->endline
= ascii_endline
;
88 p
->letter
= ascii_letter
;
89 p
->setwidth
= ascii_setwidth
;
90 p
->width
= ascii_width
;
93 if (enc
!= TERMENC_ASCII
) {
96 * Do not change any of this to LC_ALL. It might break
97 * the formatting by subtly changing the behaviour of
98 * various functions, for example strftime(3). As a
99 * worst case, it might even cause buffer overflows.
102 v
= enc
== TERMENC_LOCALE
?
103 setlocale(LC_CTYPE
, "") :
104 setlocale(LC_CTYPE
, UTF8_LOCALE
);
107 * We only support UTF-8,
108 * so revert to ASCII for anything else.
112 strcmp(nl_langinfo(CODESET
), "UTF-8") != 0)
113 v
= setlocale(LC_CTYPE
, "C");
115 if (v
!= NULL
&& MB_CUR_MAX
> 1) {
116 p
->enc
= TERMENC_UTF8
;
117 p
->advance
= locale_advance
;
118 p
->endline
= locale_endline
;
119 p
->letter
= locale_letter
;
120 p
->width
= locale_width
;
130 p
->defindent
= outopts
->indent
;
132 p
->defrmargin
= outopts
->width
;
133 if (outopts
->synopsisonly
)
136 assert(p
->defindent
< UINT16_MAX
);
137 assert(p
->defrmargin
< UINT16_MAX
);
142 ascii_alloc(const struct manoutput
*outopts
)
145 return ascii_init(TERMENC_ASCII
, outopts
);
149 utf8_alloc(const struct manoutput
*outopts
)
152 return ascii_init(TERMENC_UTF8
, outopts
);
156 locale_alloc(const struct manoutput
*outopts
)
159 return ascii_init(TERMENC_LOCALE
, outopts
);
163 ascii_setwidth(struct termp
*p
, int iop
, int width
)
167 p
->tcol
->rmargin
= p
->defrmargin
;
169 p
->defrmargin
+= width
;
171 p
->defrmargin
= width
? (size_t)width
: p
->lastrmargin
;
172 else if (p
->defrmargin
> (size_t)width
)
173 p
->defrmargin
-= width
;
176 if (p
->defrmargin
> 1000)
177 p
->defrmargin
= 1000;
178 p
->lastrmargin
= p
->tcol
->rmargin
;
179 p
->tcol
->rmargin
= p
->maxrmargin
= p
->defrmargin
;
183 terminal_sepline(void *arg
)
188 p
= (struct termp
*)arg
;
190 for (i
= 0; i
< p
->defrmargin
; i
++)
191 (*p
->letter
)(p
, '-');
197 ascii_width(const struct termp
*p
, int c
)
199 return c
!= ASCII_BREAK
;
203 ascii_free(void *arg
)
206 term_free((struct termp
*)arg
);
210 ascii_letter(struct termp
*p
, int c
)
217 ascii_begin(struct termp
*p
)
220 (*p
->headf
)(p
, p
->argf
);
224 ascii_end(struct termp
*p
)
227 (*p
->footf
)(p
, p
->argf
);
231 ascii_endline(struct termp
*p
)
235 if ((int)p
->tcol
->offset
> p
->ti
)
236 p
->tcol
->offset
-= p
->ti
;
244 ascii_advance(struct termp
*p
, size_t len
)
249 * XXX We used to have "assert(len < UINT16_MAX)" here.
250 * that is not quite right because the input document
251 * can trigger that by merely providing large input.
252 * For now, simply truncate.
256 for (i
= 0; i
< len
; i
++)
261 ascii_hspan(const struct termp
*p
, const struct roffsu
*su
)
270 r
= su
->scale
* 240.0 / 2.54;
273 r
= su
->scale
* 65536.0;
276 r
= su
->scale
* 240.0;
279 r
= su
->scale
* 0.24;
283 r
= su
->scale
* 40.0;
286 r
= su
->scale
* 10.0 / 3.0;
290 r
= su
->scale
* 24.0;
295 return r
> 0.0 ? r
+ 0.01 : r
- 0.01;
301 static const char nbrsp
[2] = { ASCII_NBRSP
, '\0' };
302 static const char *tab
[] = {
303 "<NUL>","<SOH>","<STX>","<ETX>","<EOT>","<ENQ>","<ACK>","<BEL>",
304 "<BS>", "\t", "<LF>", "<VT>", "<FF>", "<CR>", "<SO>", "<SI>",
305 "<DLE>","<DC1>","<DC2>","<DC3>","<DC4>","<NAK>","<SYN>","<ETB>",
306 "<CAN>","<EM>", "<SUB>","<ESC>","<FS>", "<GS>", "<RS>", "<US>",
307 " ", "!", "\"", "#", "$", "%", "&", "'",
308 "(", ")", "*", "+", ",", "-", ".", "/",
309 "0", "1", "2", "3", "4", "5", "6", "7",
310 "8", "9", ":", ";", "<", "=", ">", "?",
311 "@", "A", "B", "C", "D", "E", "F", "G",
312 "H", "I", "J", "K", "L", "M", "N", "O",
313 "P", "Q", "R", "S", "T", "U", "V", "W",
314 "X", "Y", "Z", "[", "\\", "]", "^", "_",
315 "`", "a", "b", "c", "d", "e", "f", "g",
316 "h", "i", "j", "k", "l", "m", "n", "o",
317 "p", "q", "r", "s", "t", "u", "v", "w",
318 "x", "y", "z", "{", "|", "}", "~", "<DEL>",
319 "<80>", "<81>", "<82>", "<83>", "<84>", "<85>", "<86>", "<87>",
320 "<88>", "<89>", "<8A>", "<8B>", "<8C>", "<8D>", "<8E>", "<8F>",
321 "<90>", "<91>", "<92>", "<93>", "<94>", "<95>", "<96>", "<97>",
322 "<98>", "<99>", "<9A>", "<9B>", "<9C>", "<9D>", "<9E>", "<9F>",
323 nbrsp
, "!", "/\bc", "-\bL", "o\bx", "=\bY", "|", "<section>",
324 "\"", "(C)", "_\ba", "<<", "~", "", "(R)", "-",
325 "<degree>","+-","^2", "^3", "'","<micro>","<paragraph>",".",
326 ",", "^1", "_\bo", ">>", "1/4", "1/2", "3/4", "?",
327 "`\bA", "'\bA", "^\bA", "~\bA", "\"\bA","o\bA", "AE", ",\bC",
328 "`\bE", "'\bE", "^\bE", "\"\bE","`\bI", "'\bI", "^\bI", "\"\bI",
329 "Dh", "~\bN", "`\bO", "'\bO", "^\bO", "~\bO", "\"\bO","x",
330 "/\bO", "`\bU", "'\bU", "^\bU", "\"\bU","'\bY", "Th", "ss",
331 "`\ba", "'\ba", "^\ba", "~\ba", "\"\ba","o\ba", "ae", ",\bc",
332 "`\be", "'\be", "^\be", "\"\be","`\bi", "'\bi", "^\bi", "\"\bi",
333 "dh", "~\bn", "`\bo", "'\bo", "^\bo", "~\bo", "\"\bo","/",
334 "/\bo", "`\bu", "'\bu", "^\bu", "\"\bu","'\by", "th", "\"\by",
335 "A", "a", "A", "a", "A", "a", "'\bC", "'\bc",
336 "^\bC", "^\bc", "C", "c", "C", "c", "D", "d",
337 "/\bD", "/\bd", "E", "e", "E", "e", "E", "e",
338 "E", "e", "E", "e", "^\bG", "^\bg", "G", "g",
339 "G", "g", ",\bG", ",\bg", "^\bH", "^\bh", "/\bH", "/\bh",
340 "~\bI", "~\bi", "I", "i", "I", "i", "I", "i",
341 "I", "i", "IJ", "ij", "^\bJ", "^\bj", ",\bK", ",\bk",
342 "q", "'\bL", "'\bl", ",\bL", ",\bl", "L", "l", "L",
343 "l", "/\bL", "/\bl", "'\bN", "'\bn", ",\bN", ",\bn", "N",
344 "n", "'n", "Ng", "ng", "O", "o", "O", "o",
345 "O", "o", "OE", "oe", "'\bR", "'\br", ",\bR", ",\br",
346 "R", "r", "'\bS", "'\bs", "^\bS", "^\bs", ",\bS", ",\bs",
347 "S", "s", ",\bT", ",\bt", "T", "t", "/\bT", "/\bt",
348 "~\bU", "~\bu", "U", "u", "U", "u", "U", "u",
349 "U", "u", "U", "u", "^\bW", "^\bw", "^\bY", "^\by",
350 "\"\bY","'\bZ", "'\bz", "Z", "z", "Z", "z", "s",
351 "b", "B", "B", "b", "6", "6", "O", "C",
352 "c", "D", "D", "D", "d", "d", "3", "@",
353 "E", "F", ",\bf", "G", "G", "hv", "I", "/\bI",
354 "K", "k", "/\bl", "l", "W", "N", "n", "~\bO",
355 "O", "o", "OI", "oi", "P", "p", "YR", "2",
356 "2", "SH", "sh", "t", "T", "t", "T", "U",
357 "u", "Y", "V", "Y", "y", "/\bZ", "/\bz", "ZH",
358 "ZH", "zh", "zh", "/\b2", "5", "5", "ts", "w",
359 "|", "||", "|=", "!", "DZ", "Dz", "dz", "LJ",
360 "Lj", "lj", "NJ", "Nj", "nj", "A", "a", "I",
361 "i", "O", "o", "U", "u", "U", "u", "U",
362 "u", "U", "u", "U", "u", "@", "A", "a",
363 "A", "a", "AE", "ae", "/\bG", "/\bg", "G", "g",
364 "K", "k", "O", "o", "O", "o", "ZH", "zh",
365 "j", "DZ", "Dz", "dz", "'\bG", "'\bg", "HV", "W",
366 "`\bN", "`\bn", "A", "a", "'\bAE","'\bae","O", "o"};
369 if ((size_t)uc
< sizeof(tab
)/sizeof(tab
[0]))
371 return mchars_uc2str(uc
);
376 locale_width(const struct termp
*p
, int c
)
380 if (c
== ASCII_NBRSP
)
389 locale_advance(struct termp
*p
, size_t len
)
394 * XXX We used to have "assert(len < UINT16_MAX)" here.
395 * that is not quite right because the input document
396 * can trigger that by merely providing large input.
397 * For now, simply truncate.
401 for (i
= 0; i
< len
; i
++)
406 locale_endline(struct termp
*p
)
410 if ((int)p
->tcol
->offset
> p
->ti
)
411 p
->tcol
->offset
-= p
->ti
;
419 locale_letter(struct termp
*p
, int c
)