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