1 /* $Id: term_ascii.c,v 1.43 2015/02/16 14:11:41 schwarze Exp $ */
2 /*
3 * Copyright (c) 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2014 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 AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR 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 "config.h"
19
20 #include <sys/types.h>
21
22 #include <assert.h>
23 #if HAVE_WCHAR
24 #include <locale.h>
25 #endif
26 #include <stdint.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <unistd.h>
30 #if HAVE_WCHAR
31 #include <wchar.h>
32 #endif
33
34 #include "mandoc.h"
35 #include "mandoc_aux.h"
36 #include "out.h"
37 #include "term.h"
38 #include "main.h"
39
40 static struct termp *ascii_init(enum termenc,
41 const struct mchars *, char *);
42 static double ascii_hspan(const struct termp *,
43 const struct roffsu *);
44 static size_t ascii_width(const struct termp *, int);
45 static void ascii_advance(struct termp *, size_t);
46 static void ascii_begin(struct termp *);
47 static void ascii_end(struct termp *);
48 static void ascii_endline(struct termp *);
49 static void ascii_letter(struct termp *, int);
50 static void ascii_setwidth(struct termp *, int, size_t);
51
52 #if HAVE_WCHAR
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
59
60 static struct termp *
ascii_init(enum termenc enc,const struct mchars * mchars,char * outopts)61 ascii_init(enum termenc enc, const struct mchars *mchars, char *outopts)
62 {
63 const char *toks[5];
64 char *v;
65 struct termp *p;
66 const char *errstr;
67 int num;
68
69 p = mandoc_calloc(1, sizeof(struct termp));
70
71 p->symtab = mchars;
72 p->tabwidth = 5;
73 p->defrmargin = p->lastrmargin = 78;
74 p->fontq = mandoc_reallocarray(NULL,
75 (p->fontsz = 8), sizeof(enum termfont));
76 p->fontq[0] = p->fontl = TERMFONT_NONE;
77
78 p->begin = ascii_begin;
79 p->end = ascii_end;
80 p->hspan = ascii_hspan;
81 p->type = TERMTYPE_CHAR;
82
83 p->enc = TERMENC_ASCII;
84 p->advance = ascii_advance;
85 p->endline = ascii_endline;
86 p->letter = ascii_letter;
87 p->setwidth = ascii_setwidth;
88 p->width = ascii_width;
89
90 #if HAVE_WCHAR
91 if (TERMENC_ASCII != enc) {
92 v = TERMENC_LOCALE == enc ?
93 setlocale(LC_ALL, "") :
94 setlocale(LC_CTYPE, "en_US.UTF-8");
95 if (NULL != v && MB_CUR_MAX > 1) {
96 p->enc = enc;
97 p->advance = locale_advance;
98 p->endline = locale_endline;
99 p->letter = locale_letter;
100 p->width = locale_width;
101 }
102 }
103 #endif
104
105 toks[0] = "indent";
106 toks[1] = "width";
107 toks[2] = "mdoc";
108 toks[3] = "synopsis";
109 toks[4] = NULL;
110
111 while (outopts && *outopts)
112 switch (getsubopt(&outopts, UNCONST(toks), &v)) {
113 case 0:
114 num = strtonum(v, 0, 1000, &errstr);
115 if (!errstr)
116 p->defindent = num;
117 break;
118 case 1:
119 num = strtonum(v, 0, 1000, &errstr);
120 if (!errstr)
121 p->defrmargin = num;
122 break;
123 case 2:
124 /*
125 * Temporary, undocumented mode
126 * to imitate mdoc(7) output style.
127 */
128 p->mdocstyle = 1;
129 p->defindent = 5;
130 break;
131 case 3:
132 p->synopsisonly = 1;
133 break;
134 default:
135 break;
136 }
137
138 /* Enforce a lower boundary. */
139 if (p->defrmargin < 58)
140 p->defrmargin = 58;
141
142 return(p);
143 }
144
145 void *
ascii_alloc(const struct mchars * mchars,char * outopts)146 ascii_alloc(const struct mchars *mchars, char *outopts)
147 {
148
149 return(ascii_init(TERMENC_ASCII, mchars, outopts));
150 }
151
152 void *
utf8_alloc(const struct mchars * mchars,char * outopts)153 utf8_alloc(const struct mchars *mchars, char *outopts)
154 {
155
156 return(ascii_init(TERMENC_UTF8, mchars, outopts));
157 }
158
159 void *
locale_alloc(const struct mchars * mchars,char * outopts)160 locale_alloc(const struct mchars *mchars, char *outopts)
161 {
162
163 return(ascii_init(TERMENC_LOCALE, mchars, outopts));
164 }
165
166 static void
ascii_setwidth(struct termp * p,int iop,size_t width)167 ascii_setwidth(struct termp *p, int iop, size_t width)
168 {
169
170 p->rmargin = p->defrmargin;
171 if (iop > 0)
172 p->defrmargin += width;
173 else if (iop == 0)
174 p->defrmargin = width ? width : p->lastrmargin;
175 else if (p->defrmargin > width)
176 p->defrmargin -= width;
177 else
178 p->defrmargin = 0;
179 p->lastrmargin = p->rmargin;
180 p->rmargin = p->maxrmargin = p->defrmargin;
181 }
182
183 void
ascii_sepline(void * arg)184 ascii_sepline(void *arg)
185 {
186 struct termp *p;
187 size_t i;
188
189 p = (struct termp *)arg;
190 putchar('\n');
191 for (i = 0; i < p->defrmargin; i++)
192 putchar('-');
193 putchar('\n');
194 putchar('\n');
195 }
196
197 static size_t
ascii_width(const struct termp * p,int c)198 ascii_width(const struct termp *p, int c)
199 {
200
201 return(1);
202 }
203
204 void
ascii_free(void * arg)205 ascii_free(void *arg)
206 {
207
208 term_free((struct termp *)arg);
209 }
210
211 static void
ascii_letter(struct termp * p,int c)212 ascii_letter(struct termp *p, int c)
213 {
214
215 putchar(c);
216 }
217
218 static void
ascii_begin(struct termp * p)219 ascii_begin(struct termp *p)
220 {
221
222 (*p->headf)(p, p->argf);
223 }
224
225 static void
ascii_end(struct termp * p)226 ascii_end(struct termp *p)
227 {
228
229 (*p->footf)(p, p->argf);
230 }
231
232 static void
ascii_endline(struct termp * p)233 ascii_endline(struct termp *p)
234 {
235
236 putchar('\n');
237 }
238
239 static void
ascii_advance(struct termp * p,size_t len)240 ascii_advance(struct termp *p, size_t len)
241 {
242 size_t i;
243
244 for (i = 0; i < len; i++)
245 putchar(' ');
246 }
247
248 static double
ascii_hspan(const struct termp * p,const struct roffsu * su)249 ascii_hspan(const struct termp *p, const struct roffsu *su)
250 {
251 double r;
252
253 /*
254 * Approximate based on character width.
255 * None of these will be actually correct given that an inch on
256 * the screen depends on character size, terminal, etc., etc.
257 */
258 switch (su->unit) {
259 case SCALE_BU:
260 r = su->scale * 10.0 / 240.0;
261 break;
262 case SCALE_CM:
263 r = su->scale * 10.0 / 2.54;
264 break;
265 case SCALE_FS:
266 r = su->scale * 2730.666;
267 break;
268 case SCALE_IN:
269 r = su->scale * 10.0;
270 break;
271 case SCALE_MM:
272 r = su->scale / 100.0;
273 break;
274 case SCALE_PC:
275 r = su->scale * 10.0 / 6.0;
276 break;
277 case SCALE_PT:
278 r = su->scale * 10.0 / 72.0;
279 break;
280 case SCALE_VS:
281 r = su->scale * 2.0 - 1.0;
282 break;
283 case SCALE_EN:
284 /* FALLTHROUGH */
285 case SCALE_EM:
286 r = su->scale;
287 break;
288 default:
289 abort();
290 /* NOTREACHED */
291 }
292
293 return(r);
294 }
295
296 const char *
ascii_uc2str(int uc)297 ascii_uc2str(int uc)
298 {
299 static const char nbrsp[2] = { ASCII_NBRSP, '\0' };
300 static const char *tab[] = {
301 "<NUL>","<SOH>","<STX>","<ETX>","<EOT>","<ENQ>","<ACK>","<BEL>",
302 "<BS>", "\t", "<LF>", "<VT>", "<FF>", "<CR>", "<SO>", "<SI>",
303 "<DLE>","<DC1>","<DC2>","<DC3>","<DC4>","<NAK>","<SYN>","<ETB>",
304 "<CAN>","<EM>", "<SUB>","<ESC>","<FS>", "<GS>", "<RS>", "<US>",
305 " ", "!", "\"", "#", "$", "%", "&", "'",
306 "(", ")", "*", "+", ",", "-", ".", "/",
307 "0", "1", "2", "3", "4", "5", "6", "7",
308 "8", "9", ":", ";", "<", "=", ">", "?",
309 "@", "A", "B", "C", "D", "E", "F", "G",
310 "H", "I", "J", "K", "L", "M", "N", "O",
311 "P", "Q", "R", "S", "T", "U", "V", "W",
312 "X", "Y", "Z", "[", "\\", "]", "^", "_",
313 "`", "a", "b", "c", "d", "e", "f", "g",
314 "h", "i", "j", "k", "l", "m", "n", "o",
315 "p", "q", "r", "s", "t", "u", "v", "w",
316 "x", "y", "z", "{", "|", "}", "~", "<DEL>",
317 "<80>", "<81>", "<82>", "<83>", "<84>", "<85>", "<86>", "<87>",
318 "<88>", "<89>", "<8A>", "<8B>", "<8C>", "<8D>", "<8E>", "<8F>",
319 "<90>", "<91>", "<92>", "<93>", "<94>", "<95>", "<96>", "<97>",
320 "<99>", "<99>", "<9A>", "<9B>", "<9C>", "<9D>", "<9E>", "<9F>",
321 nbrsp, "!", "/\bc", "GBP", "o\bx", "=\bY", "|", "<sec>",
322 "\"", "(C)", "_\ba", "<<", "~", "", "(R)", "-",
323 "<deg>","+-", "2", "3", "'", ",\bu", "<par>",".",
324 ",", "1", "_\bo", ">>", "1/4", "1/2", "3/4", "?",
325 "`\bA", "'\bA", "^\bA", "~\bA", "\"\bA","o\bA", "AE", ",\bC",
326 "`\bE", "'\bE", "^\bE", "\"\bE","`\bI", "'\bI", "^\bI", "\"\bI",
327 "-\bD", "~\bN", "`\bO", "'\bO", "^\bO", "~\bO", "\"\bO","x",
328 "/\bO", "`\bU", "'\bU", "^\bU", "\"\bU","'\bY", "Th", "ss",
329 "`\ba", "'\ba", "^\ba", "~\ba", "\"\ba","o\ba", "ae", ",\bc",
330 "`\be", "'\be", "^\be", "\"\be","`\bi", "'\bi", "^\bi", "\"\bi",
331 "d", "~\bn", "`\bo", "'\bo", "^\bo", "~\bo", "\"\bo","-:-",
332 "/\bo", "`\bu", "'\bu", "^\bu", "\"\bu","'\by", "th", "\"\by",
333 "A", "a", "A", "a", "A", "a", "'\bC", "'\bc",
334 "^\bC", "^\bc", "C", "c", "C", "c", "D", "d",
335 "/\bD", "/\bd", "E", "e", "E", "e", "E", "e",
336 "E", "e", "E", "e", "^\bG", "^\bg", "G", "g",
337 "G", "g", ",\bG", ",\bg", "^\bH", "^\bh", "/\bH", "/\bh",
338 "~\bI", "~\bi", "I", "i", "I", "i", "I", "i",
339 "I", "i", "IJ", "ij", "^\bJ", "^\bj", ",\bK", ",\bk",
340 "q", "'\bL", "'\bl", ",\bL", ",\bl", "L", "l", "L",
341 "l", "/\bL", "/\bl", "'\bN", "'\bn", ",\bN", ",\bn", "N",
342 "n", "'n", "Ng", "ng", "O", "o", "O", "o",
343 "O", "o", "OE", "oe", "'\bR", "'\br", ",\bR", ",\br",
344 "R", "r", "'\bS", "'\bs", "^\bS", "^\bs", ",\bS", ",\bs",
345 "S", "s", ",\bT", ",\bt", "T", "t", "/\bT", "/\bt",
346 "~\bU", "~\bu", "U", "u", "U", "u", "U", "u",
347 "U", "u", "U", "u", "^\bW", "^\bw", "^\bY", "^\by",
348 "\"\bY","'\bZ", "'\bz", "Z", "z", "Z", "z", "s",
349 "b", "B", "B", "b", "6", "6", "O", "C",
350 "c", "D", "D", "D", "d", "d", "3", "@",
351 "E", "F", ",\bf", "G", "G", "hv", "I", "/\bI",
352 "K", "k", "/\bl", "l", "W", "N", "n", "~\bO",
353 "O", "o", "OI", "oi", "P", "p", "YR", "2",
354 "2", "SH", "sh", "t", "T", "t", "T", "U",
355 "u", "Y", "V", "Y", "y", "/\bZ", "/\bz", "ZH",
356 "ZH", "zh", "zh", "/\b2", "5", "5", "ts", "w",
357 "|", "||", "|=", "!", "DZ", "Dz", "dz", "LJ",
358 "Lj", "lj", "NJ", "Nj", "nj", "A", "a", "I",
359 "i", "O", "o", "U", "u", "U", "u", "U",
360 "u", "U", "u", "U", "u", "@", "A", "a",
361 "A", "a", "AE", "ae", "/\bG", "/\bg", "G", "g",
362 "K", "k", "O", "o", "O", "o", "ZH", "zh",
363 "j", "DZ", "Dz", "dz", "'\bG", "'\bg", "HV", "W",
364 "`\bN", "`\bn", "A", "a", "'\bAE","'\bae","O", "o"};
365
366 assert(uc >= 0);
367 if ((size_t)uc < sizeof(tab)/sizeof(tab[0]))
368 return(tab[uc]);
369 return(mchars_uc2str(uc));
370 }
371
372 #if HAVE_WCHAR
373 static size_t
locale_width(const struct termp * p,int c)374 locale_width(const struct termp *p, int c)
375 {
376 int rc;
377
378 if (c == ASCII_NBRSP)
379 c = ' ';
380 rc = wcwidth(c);
381 if (rc < 0)
382 rc = 0;
383 return(rc);
384 }
385
386 static void
locale_advance(struct termp * p,size_t len)387 locale_advance(struct termp *p, size_t len)
388 {
389 size_t i;
390
391 for (i = 0; i < len; i++)
392 putwchar(L' ');
393 }
394
395 static void
locale_endline(struct termp * p)396 locale_endline(struct termp *p)
397 {
398
399 putwchar(L'\n');
400 }
401
402 static void
locale_letter(struct termp * p,int c)403 locale_letter(struct termp *p, int c)
404 {
405
406 putwchar(c);
407 }
408 #endif
409