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