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