]> git.cameronkatri.com Git - mandoc.git/blob - chars.c
Two minor tweaks regarding the fallback from -u/-d to default mode:
[mandoc.git] / chars.c
1 /* $Id: chars.c,v 1.56 2014/03/23 11:25:25 schwarze Exp $ */
2 /*
3 * Copyright (c) 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2011 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 #ifdef HAVE_CONFIG_H
19 #include "config.h"
20 #endif
21
22 #include <assert.h>
23 #include <ctype.h>
24 #include <stdlib.h>
25 #include <string.h>
26
27 #include "mandoc.h"
28 #include "mandoc_aux.h"
29 #include "libmandoc.h"
30
31 #define PRINT_HI 126
32 #define PRINT_LO 32
33
34 struct ln {
35 struct ln *next;
36 const char *code;
37 const char *ascii;
38 int unicode;
39 };
40
41 #define LINES_MAX 330
42
43 #define CHAR(in, ch, code) \
44 { NULL, (in), (ch), (code) },
45
46 #define CHAR_TBL_START static struct ln lines[LINES_MAX] = {
47 #define CHAR_TBL_END };
48
49 #include "chars.in"
50
51 struct mchars {
52 struct ln **htab;
53 };
54
55 static const struct ln *find(const struct mchars *,
56 const char *, size_t);
57
58 void
59 mchars_free(struct mchars *arg)
60 {
61
62 free(arg->htab);
63 free(arg);
64 }
65
66 struct mchars *
67 mchars_alloc(void)
68 {
69 struct mchars *tab;
70 struct ln **htab;
71 struct ln *pp;
72 int i, hash;
73
74 /*
75 * Constructs a very basic chaining hashtable. The hash routine
76 * is simply the integral value of the first character.
77 * Subsequent entries are chained in the order they're processed.
78 */
79
80 tab = mandoc_malloc(sizeof(struct mchars));
81 htab = mandoc_calloc(PRINT_HI - PRINT_LO + 1, sizeof(struct ln *));
82
83 for (i = 0; i < LINES_MAX; i++) {
84 hash = (int)lines[i].code[0] - PRINT_LO;
85
86 if (NULL == (pp = htab[hash])) {
87 htab[hash] = &lines[i];
88 continue;
89 }
90
91 for ( ; pp->next; pp = pp->next)
92 /* Scan ahead. */ ;
93 pp->next = &lines[i];
94 }
95
96 tab->htab = htab;
97 return(tab);
98 }
99
100 int
101 mchars_spec2cp(const struct mchars *arg, const char *p, size_t sz)
102 {
103 const struct ln *ln;
104
105 ln = find(arg, p, sz);
106 if (NULL == ln)
107 return(-1);
108 return(ln->unicode);
109 }
110
111 char
112 mchars_num2char(const char *p, size_t sz)
113 {
114 int i;
115
116 if ((i = mandoc_strntoi(p, sz, 10)) < 0)
117 return('\0');
118 return(i > 0 && i < 256 && isprint(i) ?
119 /* LINTED */ i : '\0');
120 }
121
122 int
123 mchars_num2uc(const char *p, size_t sz)
124 {
125 int i;
126
127 if ((i = mandoc_strntoi(p, sz, 16)) < 0)
128 return('\0');
129 /* FIXME: make sure we're not in a bogus range. */
130 return(i > 0x80 && i <= 0x10FFFF ? i : '\0');
131 }
132
133 const char *
134 mchars_spec2str(const struct mchars *arg,
135 const char *p, size_t sz, size_t *rsz)
136 {
137 const struct ln *ln;
138
139 ln = find(arg, p, sz);
140 if (NULL == ln) {
141 *rsz = 1;
142 return(NULL);
143 }
144
145 *rsz = strlen(ln->ascii);
146 return(ln->ascii);
147 }
148
149 static const struct ln *
150 find(const struct mchars *tab, const char *p, size_t sz)
151 {
152 const struct ln *pp;
153 int hash;
154
155 assert(p);
156
157 if (0 == sz || p[0] < PRINT_LO || p[0] > PRINT_HI)
158 return(NULL);
159
160 hash = (int)p[0] - PRINT_LO;
161
162 for (pp = tab->htab[hash]; pp; pp = pp->next)
163 if (0 == strncmp(pp->code, p, sz) &&
164 '\0' == pp->code[(int)sz])
165 return(pp);
166
167 return(NULL);
168 }