]> git.cameronkatri.com Git - mandoc.git/blob - chars.c
6d9cf9d36a7fce143062c47d98956418b2eaf7c5
[mandoc.git] / chars.c
1 /* $Id: chars.c,v 1.41 2011/05/14 17:54:42 kristaps 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 <ctype.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28 #include "mandoc.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 353
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 mchars {
59 struct ln **htab;
60 };
61
62 static inline int match(const struct ln *,
63 const char *, size_t, int);
64 static const struct ln *find(struct mchars *, const char *, size_t, int);
65
66 void
67 mchars_free(struct mchars *arg)
68 {
69
70 free(arg->htab);
71 free(arg);
72 }
73
74 struct mchars *
75 mchars_alloc(void)
76 {
77 struct mchars *tab;
78 struct ln **htab;
79 struct ln *pp;
80 int i, hash;
81
82 /*
83 * Constructs a very basic chaining hashtable. The hash routine
84 * is simply the integral value of the first character.
85 * Subsequent entries are chained in the order they're processed
86 * (they're in-line re-ordered during lookup).
87 */
88
89 tab = mandoc_malloc(sizeof(struct mchars));
90 htab = mandoc_calloc(PRINT_HI - PRINT_LO + 1, sizeof(struct ln **));
91
92 for (i = 0; i < LINES_MAX; i++) {
93 hash = (int)lines[i].code[0] - PRINT_LO;
94
95 if (NULL == (pp = htab[hash])) {
96 htab[hash] = &lines[i];
97 continue;
98 }
99
100 for ( ; pp->next; pp = pp->next)
101 /* Scan ahead. */ ;
102 pp->next = &lines[i];
103 }
104
105 tab->htab = htab;
106 return(tab);
107 }
108
109
110 /*
111 * Special character to Unicode codepoint.
112 */
113 int
114 mchars_spec2cp(struct mchars *arg, const char *p, size_t sz)
115 {
116 const struct ln *ln;
117
118 ln = find(arg, p, sz, CHARS_CHAR);
119 if (NULL == ln)
120 return(-1);
121 return(ln->unicode);
122 }
123
124
125 /*
126 * Reserved word to Unicode codepoint.
127 */
128 int
129 mchars_res2cp(struct mchars *arg, const char *p, size_t sz)
130 {
131 const struct ln *ln;
132
133 ln = find(arg, p, sz, CHARS_STRING);
134 if (NULL == ln)
135 return(-1);
136 return(ln->unicode);
137 }
138
139 /*
140 * Numbered character to literal character.
141 * This can only be a printable character (i.e., alnum, punct, space) so
142 * prevent the character from ruining our state (backspace, newline, and
143 * so on).
144 */
145 char
146 mchars_num2char(const char *p, size_t sz)
147 {
148 int i;
149
150 if (sz > 3)
151 return('\0');
152
153 i = atoi(p);
154 /* LINTED */
155 return(isprint(i) ? i : '\0');
156 }
157
158 /*
159 * Special character to string array.
160 */
161 const char *
162 mchars_spec2str(struct mchars *arg, const char *p, size_t sz, size_t *rsz)
163 {
164 const struct ln *ln;
165
166 ln = find(arg, p, sz, CHARS_CHAR);
167 if (NULL == ln)
168 return(NULL);
169
170 *rsz = strlen(ln->ascii);
171 return(ln->ascii);
172 }
173
174 /*
175 * Reserved word to string array.
176 */
177 const char *
178 mchars_res2str(struct mchars *arg, const char *p, size_t sz, size_t *rsz)
179 {
180 const struct ln *ln;
181
182 ln = find(arg, p, sz, CHARS_STRING);
183 if (NULL == ln)
184 return(NULL);
185
186 *rsz = strlen(ln->ascii);
187 return(ln->ascii);
188 }
189
190 static const struct ln *
191 find(struct mchars *tab, const char *p, size_t sz, int type)
192 {
193 struct ln *pp, *prev;
194 struct ln **htab;
195 int hash;
196
197 assert(p);
198 if (0 == sz)
199 return(NULL);
200
201 if (p[0] < PRINT_LO || p[0] > PRINT_HI)
202 return(NULL);
203
204 /*
205 * Lookup the symbol in the symbol hash. See ascii2htab for the
206 * hashtable specs. This dynamically re-orders the hash chain
207 * to optimise for repeat hits.
208 */
209
210 hash = (int)p[0] - PRINT_LO;
211 htab = tab->htab;
212
213 if (NULL == (pp = htab[hash]))
214 return(NULL);
215
216 for (prev = NULL; pp; pp = pp->next) {
217 if ( ! match(pp, p, sz, type)) {
218 prev = pp;
219 continue;
220 }
221
222 if (prev) {
223 prev->next = pp->next;
224 pp->next = htab[hash];
225 htab[hash] = pp;
226 }
227
228 return(pp);
229 }
230
231 return(NULL);
232 }
233
234 static inline int
235 match(const struct ln *ln, const char *p, size_t sz, int type)
236 {
237
238 if ( ! (ln->type & type))
239 return(0);
240 if (strncmp(ln->code, p, sz))
241 return(0);
242 return('\0' == ln->code[(int)sz]);
243 }