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