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