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