mandoc: update to 1.14.4
[unleashed.git] / bin / mandoc / chars.c
blobfb9ded8baef6fb89fd9e1e7122d667b1ef105df3
1 /* $Id: chars.c,v 1.73 2017/08/23 13:01:29 schwarze Exp $ */
2 /*
3 * Copyright (c) 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2011, 2014, 2015, 2017 Ingo Schwarze <schwarze@openbsd.org>
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.
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.
18 #include "config.h"
20 #include <sys/types.h>
22 #include <assert.h>
23 #include <ctype.h>
24 #include <stddef.h>
25 #include <stdint.h>
26 #include <stdlib.h>
27 #include <string.h>
29 #include "mandoc.h"
30 #include "mandoc_aux.h"
31 #include "mandoc_ohash.h"
32 #include "libmandoc.h"
34 struct ln {
35 const char roffcode[16];
36 const char *ascii;
37 int unicode;
40 /* Special break control characters. */
41 static const char ascii_nbrsp[2] = { ASCII_NBRSP, '\0' };
42 static const char ascii_break[2] = { ASCII_BREAK, '\0' };
44 static struct ln lines[] = {
46 /* Spacing. */
47 { " ", ascii_nbrsp, 0x00a0 },
48 { "~", ascii_nbrsp, 0x00a0 },
49 { "0", " ", 0x2002 },
50 { "|", "", 0 },
51 { "^", "", 0 },
52 { "&", "", 0 },
53 { "%", "", 0 },
54 { ":", ascii_break, 0 },
55 /* XXX The following three do not really belong here. */
56 { "t", "", 0 },
57 { "c", "", 0 },
58 { "}", "", 0 },
60 /* Lines. */
61 { "ba", "|", 0x007c },
62 { "br", "|", 0x2502 },
63 { "ul", "_", 0x005f },
64 { "ru", "_", 0x005f },
65 { "rn", "-", 0x203e },
66 { "bb", "|", 0x00a6 },
67 { "sl", "/", 0x002f },
68 { "rs", "\\", 0x005c },
70 /* Text markers. */
71 { "ci", "O", 0x25cb },
72 { "bu", "+\bo", 0x2022 },
73 { "dd", "<**>", 0x2021 },
74 { "dg", "<*>", 0x2020 },
75 { "lz", "<>", 0x25ca },
76 { "sq", "[]", 0x25a1 },
77 { "ps", "<paragraph>", 0x00b6 },
78 { "sc", "<section>", 0x00a7 },
79 { "lh", "<=", 0x261c },
80 { "rh", "=>", 0x261e },
81 { "at", "@", 0x0040 },
82 { "sh", "#", 0x0023 },
83 { "CR", "<cr>", 0x21b5 },
84 { "OK", "\\/", 0x2713 },
85 { "CL", "<club>", 0x2663 },
86 { "SP", "<spade>", 0x2660 },
87 { "HE", "<heart>", 0x2665 },
88 { "DI", "<diamond>", 0x2666 },
90 /* Legal symbols. */
91 { "co", "(C)", 0x00a9 },
92 { "rg", "(R)", 0x00ae },
93 { "tm", "tm", 0x2122 },
95 /* Punctuation. */
96 { "em", "--", 0x2014 },
97 { "en", "-", 0x2013 },
98 { "hy", "-", 0x2010 },
99 { "e", "\\", 0x005c },
100 { ".", ".", 0x002e },
101 { "r!", "!", 0x00a1 },
102 { "r?", "?", 0x00bf },
104 /* Quotes. */
105 { "Bq", ",,", 0x201e },
106 { "bq", ",", 0x201a },
107 { "lq", "\"", 0x201c },
108 { "rq", "\"", 0x201d },
109 { "Lq", "\"", 0x201c },
110 { "Rq", "\"", 0x201d },
111 { "oq", "`", 0x2018 },
112 { "cq", "\'", 0x2019 },
113 { "aq", "\'", 0x0027 },
114 { "dq", "\"", 0x0022 },
115 { "Fo", "<<", 0x00ab },
116 { "Fc", ">>", 0x00bb },
117 { "fo", "<", 0x2039 },
118 { "fc", ">", 0x203a },
120 /* Brackets. */
121 { "lB", "[", 0x005b },
122 { "rB", "]", 0x005d },
123 { "lC", "{", 0x007b },
124 { "rC", "}", 0x007d },
125 { "la", "<", 0x27e8 },
126 { "ra", ">", 0x27e9 },
127 { "bv", "|", 0x23aa },
128 { "braceex", "|", 0x23aa },
129 { "bracketlefttp", "|", 0x23a1 },
130 { "bracketleftbt", "|", 0x23a3 },
131 { "bracketleftex", "|", 0x23a2 },
132 { "bracketrighttp", "|", 0x23a4 },
133 { "bracketrightbt", "|", 0x23a6 },
134 { "bracketrightex", "|", 0x23a5 },
135 { "lt", ",-", 0x23a7 },
136 { "bracelefttp", ",-", 0x23a7 },
137 { "lk", "{", 0x23a8 },
138 { "braceleftmid", "{", 0x23a8 },
139 { "lb", "`-", 0x23a9 },
140 { "braceleftbt", "`-", 0x23a9 },
141 { "braceleftex", "|", 0x23aa },
142 { "rt", "-.", 0x23ab },
143 { "bracerighttp", "-.", 0x23ab },
144 { "rk", "}", 0x23ac },
145 { "bracerightmid", "}", 0x23ac },
146 { "rb", "-\'", 0x23ad },
147 { "bracerightbt", "-\'", 0x23ad },
148 { "bracerightex", "|", 0x23aa },
149 { "parenlefttp", "/", 0x239b },
150 { "parenleftbt", "\\", 0x239d },
151 { "parenleftex", "|", 0x239c },
152 { "parenrighttp", "\\", 0x239e },
153 { "parenrightbt", "/", 0x23a0 },
154 { "parenrightex", "|", 0x239f },
156 /* Arrows and lines. */
157 { "<-", "<-", 0x2190 },
158 { "->", "->", 0x2192 },
159 { "<>", "<->", 0x2194 },
160 { "da", "|\bv", 0x2193 },
161 { "ua", "|\b^", 0x2191 },
162 { "va", "^v", 0x2195 },
163 { "lA", "<=", 0x21d0 },
164 { "rA", "=>", 0x21d2 },
165 { "hA", "<=>", 0x21d4 },
166 { "uA", "=\b^", 0x21d1 },
167 { "dA", "=\bv", 0x21d3 },
168 { "vA", "^=v", 0x21d5 },
169 { "an", "-", 0x23af },
171 /* Logic. */
172 { "AN", "^", 0x2227 },
173 { "OR", "v", 0x2228 },
174 { "no", "~", 0x00ac },
175 { "tno", "~", 0x00ac },
176 { "te", "<there\037exists>", 0x2203 },
177 { "fa", "<for\037all>", 0x2200 },
178 { "st", "<such\037that>", 0x220b },
179 { "tf", "<therefore>", 0x2234 },
180 { "3d", "<therefore>", 0x2234 },
181 { "or", "|", 0x007c },
183 /* Mathematicals. */
184 { "pl", "+", 0x002b },
185 { "mi", "-", 0x2212 },
186 { "-", "-", 0x002d },
187 { "-+", "-+", 0x2213 },
188 { "+-", "+-", 0x00b1 },
189 { "t+-", "+-", 0x00b1 },
190 { "pc", ".", 0x00b7 },
191 { "md", ".", 0x22c5 },
192 { "mu", "x", 0x00d7 },
193 { "tmu", "x", 0x00d7 },
194 { "c*", "O\bx", 0x2297 },
195 { "c+", "O\b+", 0x2295 },
196 { "di", "/", 0x00f7 },
197 { "tdi", "/", 0x00f7 },
198 { "f/", "/", 0x2044 },
199 { "**", "*", 0x2217 },
200 { "<=", "<=", 0x2264 },
201 { ">=", ">=", 0x2265 },
202 { "<<", "<<", 0x226a },
203 { ">>", ">>", 0x226b },
204 { "eq", "=", 0x003d },
205 { "!=", "!=", 0x2260 },
206 { "==", "==", 0x2261 },
207 { "ne", "!==", 0x2262 },
208 { "ap", "~", 0x223c },
209 { "|=", "-~", 0x2243 },
210 { "=~", "=~", 0x2245 },
211 { "~~", "~~", 0x2248 },
212 { "~=", "~=", 0x2248 },
213 { "pt", "<proportional\037to>", 0x221d },
214 { "es", "{}", 0x2205 },
215 { "mo", "<element\037of>", 0x2208 },
216 { "nm", "<not\037element\037of>", 0x2209 },
217 { "sb", "<proper\037subset>", 0x2282 },
218 { "nb", "<not\037subset>", 0x2284 },
219 { "sp", "<proper\037superset>", 0x2283 },
220 { "nc", "<not\037superset>", 0x2285 },
221 { "ib", "<subset\037or\037equal>", 0x2286 },
222 { "ip", "<superset\037or\037equal>", 0x2287 },
223 { "ca", "<intersection>", 0x2229 },
224 { "cu", "<union>", 0x222a },
225 { "/_", "<angle>", 0x2220 },
226 { "pp", "<perpendicular>", 0x22a5 },
227 { "is", "<integral>", 0x222b },
228 { "integral", "<integral>", 0x222b },
229 { "sum", "<sum>", 0x2211 },
230 { "product", "<product>", 0x220f },
231 { "coproduct", "<coproduct>", 0x2210 },
232 { "gr", "<nabla>", 0x2207 },
233 { "sr", "<sqrt>", 0x221a },
234 { "sqrt", "<sqrt>", 0x221a },
235 { "lc", "|~", 0x2308 },
236 { "rc", "~|", 0x2309 },
237 { "lf", "|_", 0x230a },
238 { "rf", "_|", 0x230b },
239 { "if", "<infinity>", 0x221e },
240 { "Ah", "<Aleph>", 0x2135 },
241 { "Im", "<Im>", 0x2111 },
242 { "Re", "<Re>", 0x211c },
243 { "wp", "P", 0x2118 },
244 { "pd", "<del>", 0x2202 },
245 { "-h", "/h", 0x210f },
246 { "hbar", "/h", 0x210f },
247 { "12", "1/2", 0x00bd },
248 { "14", "1/4", 0x00bc },
249 { "34", "3/4", 0x00be },
250 { "18", "1/8", 0x215B },
251 { "38", "3/8", 0x215C },
252 { "58", "5/8", 0x215D },
253 { "78", "7/8", 0x215E },
254 { "S1", "^1", 0x00B9 },
255 { "S2", "^2", 0x00B2 },
256 { "S3", "^3", 0x00B3 },
258 /* Ligatures. */
259 { "ff", "ff", 0xfb00 },
260 { "fi", "fi", 0xfb01 },
261 { "fl", "fl", 0xfb02 },
262 { "Fi", "ffi", 0xfb03 },
263 { "Fl", "ffl", 0xfb04 },
264 { "AE", "AE", 0x00c6 },
265 { "ae", "ae", 0x00e6 },
266 { "OE", "OE", 0x0152 },
267 { "oe", "oe", 0x0153 },
268 { "ss", "ss", 0x00df },
269 { "IJ", "IJ", 0x0132 },
270 { "ij", "ij", 0x0133 },
272 /* Accents. */
273 { "a\"", "\"", 0x02dd },
274 { "a-", "-", 0x00af },
275 { "a.", ".", 0x02d9 },
276 { "a^", "^", 0x005e },
277 { "aa", "\'", 0x00b4 },
278 { "\'", "\'", 0x00b4 },
279 { "ga", "`", 0x0060 },
280 { "`", "`", 0x0060 },
281 { "ab", "'\b`", 0x02d8 },
282 { "ac", ",", 0x00b8 },
283 { "ad", "\"", 0x00a8 },
284 { "ah", "v", 0x02c7 },
285 { "ao", "o", 0x02da },
286 { "a~", "~", 0x007e },
287 { "ho", ",", 0x02db },
288 { "ha", "^", 0x005e },
289 { "ti", "~", 0x007e },
291 /* Accented letters. */
292 { "'A", "'\bA", 0x00c1 },
293 { "'E", "'\bE", 0x00c9 },
294 { "'I", "'\bI", 0x00cd },
295 { "'O", "'\bO", 0x00d3 },
296 { "'U", "'\bU", 0x00da },
297 { "'a", "'\ba", 0x00e1 },
298 { "'e", "'\be", 0x00e9 },
299 { "'i", "'\bi", 0x00ed },
300 { "'o", "'\bo", 0x00f3 },
301 { "'u", "'\bu", 0x00fa },
302 { "`A", "`\bA", 0x00c0 },
303 { "`E", "`\bE", 0x00c8 },
304 { "`I", "`\bI", 0x00cc },
305 { "`O", "`\bO", 0x00d2 },
306 { "`U", "`\bU", 0x00d9 },
307 { "`a", "`\ba", 0x00e0 },
308 { "`e", "`\be", 0x00e8 },
309 { "`i", "`\bi", 0x00ec },
310 { "`o", "`\bo", 0x00f2 },
311 { "`u", "`\bu", 0x00f9 },
312 { "~A", "~\bA", 0x00c3 },
313 { "~N", "~\bN", 0x00d1 },
314 { "~O", "~\bO", 0x00d5 },
315 { "~a", "~\ba", 0x00e3 },
316 { "~n", "~\bn", 0x00f1 },
317 { "~o", "~\bo", 0x00f5 },
318 { ":A", "\"\bA", 0x00c4 },
319 { ":E", "\"\bE", 0x00cb },
320 { ":I", "\"\bI", 0x00cf },
321 { ":O", "\"\bO", 0x00d6 },
322 { ":U", "\"\bU", 0x00dc },
323 { ":a", "\"\ba", 0x00e4 },
324 { ":e", "\"\be", 0x00eb },
325 { ":i", "\"\bi", 0x00ef },
326 { ":o", "\"\bo", 0x00f6 },
327 { ":u", "\"\bu", 0x00fc },
328 { ":y", "\"\by", 0x00ff },
329 { "^A", "^\bA", 0x00c2 },
330 { "^E", "^\bE", 0x00ca },
331 { "^I", "^\bI", 0x00ce },
332 { "^O", "^\bO", 0x00d4 },
333 { "^U", "^\bU", 0x00db },
334 { "^a", "^\ba", 0x00e2 },
335 { "^e", "^\be", 0x00ea },
336 { "^i", "^\bi", 0x00ee },
337 { "^o", "^\bo", 0x00f4 },
338 { "^u", "^\bu", 0x00fb },
339 { ",C", ",\bC", 0x00c7 },
340 { ",c", ",\bc", 0x00e7 },
341 { "/L", "/\bL", 0x0141 },
342 { "/l", "/\bl", 0x0142 },
343 { "/O", "/\bO", 0x00d8 },
344 { "/o", "/\bo", 0x00f8 },
345 { "oA", "o\bA", 0x00c5 },
346 { "oa", "o\ba", 0x00e5 },
348 /* Special letters. */
349 { "-D", "Dh", 0x00d0 },
350 { "Sd", "dh", 0x00f0 },
351 { "TP", "Th", 0x00de },
352 { "Tp", "th", 0x00fe },
353 { ".i", "i", 0x0131 },
354 { ".j", "j", 0x0237 },
356 /* Currency. */
357 { "Do", "$", 0x0024 },
358 { "ct", "/\bc", 0x00a2 },
359 { "Eu", "EUR", 0x20ac },
360 { "eu", "EUR", 0x20ac },
361 { "Ye", "=\bY", 0x00a5 },
362 { "Po", "GBP", 0x00a3 },
363 { "Cs", "o\bx", 0x00a4 },
364 { "Fn", ",\bf", 0x0192 },
366 /* Units. */
367 { "de", "<degree>", 0x00b0 },
368 { "%0", "<permille>", 0x2030 },
369 { "fm", "\'", 0x2032 },
370 { "sd", "''", 0x2033 },
371 { "mc", "<micro>", 0x00b5 },
372 { "Of", "_\ba", 0x00aa },
373 { "Om", "_\bo", 0x00ba },
375 /* Greek characters. */
376 { "*A", "A", 0x0391 },
377 { "*B", "B", 0x0392 },
378 { "*G", "<Gamma>", 0x0393 },
379 { "*D", "<Delta>", 0x0394 },
380 { "*E", "E", 0x0395 },
381 { "*Z", "Z", 0x0396 },
382 { "*Y", "H", 0x0397 },
383 { "*H", "<Theta>", 0x0398 },
384 { "*I", "I", 0x0399 },
385 { "*K", "K", 0x039a },
386 { "*L", "<Lambda>", 0x039b },
387 { "*M", "M", 0x039c },
388 { "*N", "N", 0x039d },
389 { "*C", "<Xi>", 0x039e },
390 { "*O", "O", 0x039f },
391 { "*P", "<Pi>", 0x03a0 },
392 { "*R", "P", 0x03a1 },
393 { "*S", "<Sigma>", 0x03a3 },
394 { "*T", "T", 0x03a4 },
395 { "*U", "Y", 0x03a5 },
396 { "*F", "<Phi>", 0x03a6 },
397 { "*X", "X", 0x03a7 },
398 { "*Q", "<Psi>", 0x03a8 },
399 { "*W", "<Omega>", 0x03a9 },
400 { "*a", "<alpha>", 0x03b1 },
401 { "*b", "<beta>", 0x03b2 },
402 { "*g", "<gamma>", 0x03b3 },
403 { "*d", "<delta>", 0x03b4 },
404 { "*e", "<epsilon>", 0x03b5 },
405 { "*z", "<zeta>", 0x03b6 },
406 { "*y", "<eta>", 0x03b7 },
407 { "*h", "<theta>", 0x03b8 },
408 { "*i", "<iota>", 0x03b9 },
409 { "*k", "<kappa>", 0x03ba },
410 { "*l", "<lambda>", 0x03bb },
411 { "*m", "<mu>", 0x03bc },
412 { "*n", "<nu>", 0x03bd },
413 { "*c", "<xi>", 0x03be },
414 { "*o", "o", 0x03bf },
415 { "*p", "<pi>", 0x03c0 },
416 { "*r", "<rho>", 0x03c1 },
417 { "*s", "<sigma>", 0x03c3 },
418 { "*t", "<tau>", 0x03c4 },
419 { "*u", "<upsilon>", 0x03c5 },
420 { "*f", "<phi>", 0x03d5 },
421 { "*x", "<chi>", 0x03c7 },
422 { "*q", "<psi>", 0x03c8 },
423 { "*w", "<omega>", 0x03c9 },
424 { "+h", "<theta>", 0x03d1 },
425 { "+f", "<phi>", 0x03c6 },
426 { "+p", "<pi>", 0x03d6 },
427 { "+e", "<epsilon>", 0x03f5 },
428 { "ts", "<sigma>", 0x03c2 },
431 static struct ohash mchars;
434 void
435 mchars_free(void)
438 ohash_delete(&mchars);
441 void
442 mchars_alloc(void)
444 size_t i;
445 unsigned int slot;
447 mandoc_ohash_init(&mchars, 9, offsetof(struct ln, roffcode));
448 for (i = 0; i < sizeof(lines)/sizeof(lines[0]); i++) {
449 slot = ohash_qlookup(&mchars, lines[i].roffcode);
450 assert(ohash_find(&mchars, slot) == NULL);
451 ohash_insert(&mchars, slot, lines + i);
456 mchars_spec2cp(const char *p, size_t sz)
458 const struct ln *ln;
459 const char *end;
461 end = p + sz;
462 ln = ohash_find(&mchars, ohash_qlookupi(&mchars, p, &end));
463 return ln != NULL ? ln->unicode : sz == 1 ? (unsigned char)*p : -1;
467 mchars_num2char(const char *p, size_t sz)
469 int i;
471 i = mandoc_strntoi(p, sz, 10);
472 return i >= 0 && i < 256 ? i : -1;
476 mchars_num2uc(const char *p, size_t sz)
478 int i;
480 i = mandoc_strntoi(p, sz, 16);
481 assert(i >= 0 && i <= 0x10FFFF);
482 return i;
485 const char *
486 mchars_spec2str(const char *p, size_t sz, size_t *rsz)
488 const struct ln *ln;
489 const char *end;
491 end = p + sz;
492 ln = ohash_find(&mchars, ohash_qlookupi(&mchars, p, &end));
493 if (ln == NULL) {
494 *rsz = 1;
495 return sz == 1 ? p : NULL;
498 *rsz = strlen(ln->ascii);
499 return ln->ascii;
502 const char *
503 mchars_uc2str(int uc)
505 size_t i;
507 for (i = 0; i < sizeof(lines)/sizeof(lines[0]); i++)
508 if (uc == lines[i].unicode)
509 return lines[i].ascii;
510 return "<?>";