1 /* $Id: chars.c,v 1.31 2011/01/02 10:10:57 kristaps Exp $ */
3 * Copyright (c) 2009, 2010 Kristaps Dzonsons <kristaps@bsd.lv>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
38 #define CHARS_CHAR (1 << 0)
39 #define CHARS_STRING (1 << 1)
40 #define CHARS_BOTH (CHARS_CHAR | CHARS_STRING)
45 #define CHAR(in, ch, code) \
46 { NULL, (in), (ch), (code), CHARS_CHAR },
47 #define STRING(in, ch, code) \
48 { NULL, (in), (ch), (code), CHARS_STRING },
49 #define BOTH(in, ch, code) \
50 { NULL, (in), (ch), (code), CHARS_BOTH },
52 #define CHAR_TBL_START static struct ln lines[LINES_MAX] = {
53 #define CHAR_TBL_END };
62 static inline int match(const struct ln *,
63 const char *, size_t, int);
64 static const struct ln *find(struct ctab *, const char *, size_t, int);
72 tab = (struct ctab *)arg;
80 chars_init(enum chars type)
88 * Constructs a very basic chaining hashtable. The hash routine
89 * is simply the integral value of the first character.
90 * Subsequent entries are chained in the order they're processed
91 * (they're in-line re-ordered during lookup).
94 tab = malloc(sizeof(struct ctab));
97 exit((int)MANDOCLEVEL_SYSERR);
100 htab = calloc(PRINT_HI - PRINT_LO + 1, sizeof(struct ln **));
103 exit((int)MANDOCLEVEL_SYSERR);
106 for (i = 0; i < LINES_MAX; i++) {
107 hash = (int)lines[i].code[0] - PRINT_LO;
109 if (NULL == (pp = htab[hash])) {
110 htab[hash] = &lines[i];
114 for ( ; pp->next; pp = pp->next)
116 pp->next = &lines[i];
126 * Special character to Unicode codepoint.
129 chars_spec2cp(void *arg, const char *p, size_t sz)
133 ln = find((struct ctab *)arg, p, sz, CHARS_CHAR);
141 * Reserved word to Unicode codepoint.
144 chars_res2cp(void *arg, const char *p, size_t sz)
148 ln = find((struct ctab *)arg, p, sz, CHARS_STRING);
156 * Special character to string array.
159 chars_spec2str(void *arg, const char *p, size_t sz, size_t *rsz)
163 ln = find((struct ctab *)arg, p, sz, CHARS_CHAR);
167 *rsz = strlen(ln->ascii);
173 * Reserved word to string array.
176 chars_res2str(void *arg, const char *p, size_t sz, size_t *rsz)
180 ln = find((struct ctab *)arg, p, sz, CHARS_STRING);
184 *rsz = strlen(ln->ascii);
189 static const struct ln *
190 find(struct ctab *tab, const char *p, size_t sz, int type)
192 struct ln *pp, *prev;
200 if (p[0] < PRINT_LO || p[0] > PRINT_HI)
204 * Lookup the symbol in the symbol hash. See ascii2htab for the
205 * hashtable specs. This dynamically re-orders the hash chain
206 * to optimise for repeat hits.
209 hash = (int)p[0] - PRINT_LO;
212 if (NULL == (pp = htab[hash]))
215 for (prev = NULL; pp; pp = pp->next) {
216 if ( ! match(pp, p, sz, type)) {
222 prev->next = pp->next;
223 pp->next = htab[hash];
235 match(const struct ln *ln, const char *p, size_t sz, int type)
238 if ( ! (ln->type & type))
240 if (strncmp(ln->code, p, sz))
242 return('\0' == ln->code[(int)sz]);