]> git.cameronkatri.com Git - mandoc.git/blob - chars.c
Put tbl_alloc function right into the addspan() one, as this is the only
[mandoc.git] / chars.c
1 /* $Id: chars.c,v 1.32 2011/01/30 16:05:37 schwarze Exp $ */
2 /*
3 * Copyright (c) 2009, 2010 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 <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26
27 #include "mandoc.h"
28 #include "chars.h"
29
30 #define PRINT_HI 126
31 #define PRINT_LO 32
32
33 struct ln {
34 struct ln *next;
35 const char *code;
36 const char *ascii;
37 int unicode;
38 int type;
39 #define CHARS_CHAR (1 << 0)
40 #define CHARS_STRING (1 << 1)
41 #define CHARS_BOTH (CHARS_CHAR | CHARS_STRING)
42 };
43
44 #define LINES_MAX 351
45
46 #define CHAR(in, ch, code) \
47 { NULL, (in), (ch), (code), CHARS_CHAR },
48 #define STRING(in, ch, code) \
49 { NULL, (in), (ch), (code), CHARS_STRING },
50 #define BOTH(in, ch, code) \
51 { NULL, (in), (ch), (code), CHARS_BOTH },
52
53 #define CHAR_TBL_START static struct ln lines[LINES_MAX] = {
54 #define CHAR_TBL_END };
55
56 #include "chars.in"
57
58 struct ctab {
59 enum chars type;
60 struct ln **htab;
61 };
62
63 static inline int match(const struct ln *,
64 const char *, size_t, int);
65 static const struct ln *find(struct ctab *, const char *, size_t, int);
66
67
68 void
69 chars_free(void *arg)
70 {
71 struct ctab *tab;
72
73 tab = (struct ctab *)arg;
74
75 free(tab->htab);
76 free(tab);
77 }
78
79
80 void *
81 chars_init(enum chars type)
82 {
83 struct ctab *tab;
84 struct ln **htab;
85 struct ln *pp;
86 int i, hash;
87
88 /*
89 * Constructs a very basic chaining hashtable. The hash routine
90 * is simply the integral value of the first character.
91 * Subsequent entries are chained in the order they're processed
92 * (they're in-line re-ordered during lookup).
93 */
94
95 tab = malloc(sizeof(struct ctab));
96 if (NULL == tab) {
97 perror(NULL);
98 exit((int)MANDOCLEVEL_SYSERR);
99 }
100
101 htab = calloc(PRINT_HI - PRINT_LO + 1, sizeof(struct ln **));
102 if (NULL == htab) {
103 perror(NULL);
104 exit((int)MANDOCLEVEL_SYSERR);
105 }
106
107 for (i = 0; i < LINES_MAX; i++) {
108 hash = (int)lines[i].code[0] - PRINT_LO;
109
110 if (NULL == (pp = htab[hash])) {
111 htab[hash] = &lines[i];
112 continue;
113 }
114
115 for ( ; pp->next; pp = pp->next)
116 /* Scan ahead. */ ;
117 pp->next = &lines[i];
118 }
119
120 tab->htab = htab;
121 tab->type = type;
122 return(tab);
123 }
124
125
126 /*
127 * Special character to Unicode codepoint.
128 */
129 int
130 chars_spec2cp(void *arg, const char *p, size_t sz)
131 {
132 const struct ln *ln;
133
134 ln = find((struct ctab *)arg, p, sz, CHARS_CHAR);
135 if (NULL == ln)
136 return(-1);
137 return(ln->unicode);
138 }
139
140
141 /*
142 * Reserved word to Unicode codepoint.
143 */
144 int
145 chars_res2cp(void *arg, const char *p, size_t sz)
146 {
147 const struct ln *ln;
148
149 ln = find((struct ctab *)arg, p, sz, CHARS_STRING);
150 if (NULL == ln)
151 return(-1);
152 return(ln->unicode);
153 }
154
155
156 /*
157 * Numbered character to literal character,
158 * represented as a null-terminated string for additional safety.
159 */
160 const char *
161 chars_num2char(const char *p, size_t sz)
162 {
163 int i;
164 static char c[2];
165
166 if (sz > 3)
167 return(NULL);
168 i = atoi(p);
169 if (i < 0 || i > 255)
170 return(NULL);
171 c[0] = (char)i;
172 c[1] = '\0';
173 return(c);
174 }
175
176
177 /*
178 * Special character to string array.
179 */
180 const char *
181 chars_spec2str(void *arg, const char *p, size_t sz, size_t *rsz)
182 {
183 const struct ln *ln;
184
185 ln = find((struct ctab *)arg, p, sz, CHARS_CHAR);
186 if (NULL == ln)
187 return(NULL);
188
189 *rsz = strlen(ln->ascii);
190 return(ln->ascii);
191 }
192
193
194 /*
195 * Reserved word to string array.
196 */
197 const char *
198 chars_res2str(void *arg, const char *p, size_t sz, size_t *rsz)
199 {
200 const struct ln *ln;
201
202 ln = find((struct ctab *)arg, p, sz, CHARS_STRING);
203 if (NULL == ln)
204 return(NULL);
205
206 *rsz = strlen(ln->ascii);
207 return(ln->ascii);
208 }
209
210
211 static const struct ln *
212 find(struct ctab *tab, const char *p, size_t sz, int type)
213 {
214 struct ln *pp, *prev;
215 struct ln **htab;
216 int hash;
217
218 assert(p);
219 if (0 == sz)
220 return(NULL);
221
222 if (p[0] < PRINT_LO || p[0] > PRINT_HI)
223 return(NULL);
224
225 /*
226 * Lookup the symbol in the symbol hash. See ascii2htab for the
227 * hashtable specs. This dynamically re-orders the hash chain
228 * to optimise for repeat hits.
229 */
230
231 hash = (int)p[0] - PRINT_LO;
232 htab = tab->htab;
233
234 if (NULL == (pp = htab[hash]))
235 return(NULL);
236
237 for (prev = NULL; pp; pp = pp->next) {
238 if ( ! match(pp, p, sz, type)) {
239 prev = pp;
240 continue;
241 }
242
243 if (prev) {
244 prev->next = pp->next;
245 pp->next = htab[hash];
246 htab[hash] = pp;
247 }
248
249 return(pp);
250 }
251
252 return(NULL);
253 }
254
255
256 static inline int
257 match(const struct ln *ln, const char *p, size_t sz, int type)
258 {
259
260 if ( ! (ln->type & type))
261 return(0);
262 if (strncmp(ln->code, p, sz))
263 return(0);
264 return('\0' == ln->code[(int)sz]);
265 }