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