Annotation of mandoc/chars.c, Revision 1.45
1.45 ! kristaps 1: /* $Id: chars.c,v 1.44 2011/05/17 11:50:20 kristaps Exp $ */
1.1 kristaps 2: /*
1.25 schwarze 3: * Copyright (c) 2009, 2010 Kristaps Dzonsons <kristaps@bsd.lv>
1.32 schwarze 4: * Copyright (c) 2011 Ingo Schwarze <schwarze@openbsd.org>
1.1 kristaps 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: */
1.14 kristaps 18: #ifdef HAVE_CONFIG_H
19: #include "config.h"
20: #endif
21:
1.1 kristaps 22: #include <assert.h>
1.40 kristaps 23: #include <ctype.h>
1.10 kristaps 24: #include <stdio.h>
1.1 kristaps 25: #include <stdlib.h>
26: #include <string.h>
27:
1.18 kristaps 28: #include "mandoc.h"
1.43 kristaps 29: #include "libmandoc.h"
1.1 kristaps 30:
1.2 kristaps 31: #define PRINT_HI 126
32: #define PRINT_LO 32
1.1 kristaps 33:
34: struct ln {
35: struct ln *next;
36: const char *code;
1.2 kristaps 37: const char *ascii;
1.21 kristaps 38: int unicode;
1.1 kristaps 39: };
40:
1.45 ! kristaps 41: #define LINES_MAX 325
1.1 kristaps 42:
1.24 kristaps 43: #define CHAR(in, ch, code) \
1.45 ! kristaps 44: { NULL, (in), (ch), (code) },
1.1 kristaps 45:
1.13 kristaps 46: #define CHAR_TBL_START static struct ln lines[LINES_MAX] = {
47: #define CHAR_TBL_END };
48:
1.1 kristaps 49: #include "chars.in"
50:
1.36 kristaps 51: struct mchars {
1.1 kristaps 52: struct ln **htab;
53: };
54:
1.45 ! kristaps 55: static inline int match(const struct ln *, const char *, size_t);
! 56: static const struct ln *find(struct mchars *, const char *, size_t);
1.1 kristaps 57:
58: void
1.36 kristaps 59: mchars_free(struct mchars *arg)
1.1 kristaps 60: {
61:
1.36 kristaps 62: free(arg->htab);
63: free(arg);
1.1 kristaps 64: }
65:
1.36 kristaps 66: struct mchars *
1.38 kristaps 67: mchars_alloc(void)
1.1 kristaps 68: {
1.36 kristaps 69: struct mchars *tab;
1.1 kristaps 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: * (they're in-line re-ordered during lookup).
79: */
80:
1.36 kristaps 81: tab = mandoc_malloc(sizeof(struct mchars));
1.33 kristaps 82: htab = mandoc_calloc(PRINT_HI - PRINT_LO + 1, sizeof(struct ln **));
1.1 kristaps 83:
84: for (i = 0; i < LINES_MAX; i++) {
1.2 kristaps 85: hash = (int)lines[i].code[0] - PRINT_LO;
1.1 kristaps 86:
87: if (NULL == (pp = htab[hash])) {
88: htab[hash] = &lines[i];
89: continue;
90: }
91:
92: for ( ; pp->next; pp = pp->next)
93: /* Scan ahead. */ ;
94: pp->next = &lines[i];
95: }
96:
97: tab->htab = htab;
98: return(tab);
99: }
100:
101:
1.21 kristaps 102: /*
103: * Special character to Unicode codepoint.
104: */
105: int
1.36 kristaps 106: mchars_spec2cp(struct mchars *arg, const char *p, size_t sz)
1.21 kristaps 107: {
108: const struct ln *ln;
109:
1.45 ! kristaps 110: ln = find(arg, p, sz);
1.21 kristaps 111: if (NULL == ln)
112: return(-1);
113: return(ln->unicode);
114: }
115:
116:
117: /*
118: * Reserved word to Unicode codepoint.
119: */
120: int
1.36 kristaps 121: mchars_res2cp(struct mchars *arg, const char *p, size_t sz)
1.21 kristaps 122: {
123: const struct ln *ln;
124:
1.45 ! kristaps 125: ln = find(arg, p, sz);
1.21 kristaps 126: if (NULL == ln)
127: return(-1);
128: return(ln->unicode);
1.32 schwarze 129: }
130:
131: /*
1.44 kristaps 132: * Numbered character string to ASCII codepoint.
1.40 kristaps 133: * This can only be a printable character (i.e., alnum, punct, space) so
134: * prevent the character from ruining our state (backspace, newline, and
135: * so on).
1.42 kristaps 136: * If the character is illegal, returns '\0'.
1.32 schwarze 137: */
1.39 kristaps 138: char
1.36 kristaps 139: mchars_num2char(const char *p, size_t sz)
1.32 schwarze 140: {
141: int i;
142:
1.43 kristaps 143: if ((i = mandoc_strntou(p, sz, 10)) < 0)
1.39 kristaps 144: return('\0');
1.44 kristaps 145: return(isprint(i) ? i : '\0');
146: }
147:
148: /*
149: * Hex character string to Unicode codepoint.
150: * If the character is illegal, returns '\0'.
151: */
152: int
153: mchars_num2uc(const char *p, size_t sz)
154: {
155: int i;
1.39 kristaps 156:
1.44 kristaps 157: if ((i = mandoc_strntou(p, sz, 16)) < 0)
158: return('\0');
159: /* FIXME: make sure we're not in a bogus range. */
160: return(i > 0x80 && i <= 0x10FFFF ? i : '\0');
1.21 kristaps 161: }
162:
163: /*
164: * Special character to string array.
165: */
1.1 kristaps 166: const char *
1.36 kristaps 167: mchars_spec2str(struct mchars *arg, const char *p, size_t sz, size_t *rsz)
1.1 kristaps 168: {
1.21 kristaps 169: const struct ln *ln;
170:
1.45 ! kristaps 171: ln = find(arg, p, sz);
1.21 kristaps 172: if (NULL == ln)
173: return(NULL);
1.1 kristaps 174:
1.24 kristaps 175: *rsz = strlen(ln->ascii);
1.21 kristaps 176: return(ln->ascii);
1.1 kristaps 177: }
178:
1.21 kristaps 179: /*
180: * Reserved word to string array.
181: */
1.1 kristaps 182: const char *
1.36 kristaps 183: mchars_res2str(struct mchars *arg, const char *p, size_t sz, size_t *rsz)
1.1 kristaps 184: {
1.21 kristaps 185: const struct ln *ln;
1.1 kristaps 186:
1.45 ! kristaps 187: ln = find(arg, p, sz);
1.21 kristaps 188: if (NULL == ln)
189: return(NULL);
190:
1.24 kristaps 191: *rsz = strlen(ln->ascii);
1.21 kristaps 192: return(ln->ascii);
1.1 kristaps 193: }
194:
1.21 kristaps 195: static const struct ln *
1.45 ! kristaps 196: find(struct mchars *tab, const char *p, size_t sz)
1.1 kristaps 197: {
198: struct ln *pp, *prev;
199: struct ln **htab;
200: int hash;
201:
202: assert(p);
1.23 kristaps 203: if (0 == sz)
204: return(NULL);
1.1 kristaps 205:
1.2 kristaps 206: if (p[0] < PRINT_LO || p[0] > PRINT_HI)
1.1 kristaps 207: return(NULL);
208:
209: /*
210: * Lookup the symbol in the symbol hash. See ascii2htab for the
211: * hashtable specs. This dynamically re-orders the hash chain
212: * to optimise for repeat hits.
213: */
214:
1.2 kristaps 215: hash = (int)p[0] - PRINT_LO;
1.1 kristaps 216: htab = tab->htab;
217:
218: if (NULL == (pp = htab[hash]))
219: return(NULL);
220:
221: for (prev = NULL; pp; pp = pp->next) {
1.45 ! kristaps 222: if ( ! match(pp, p, sz)) {
1.1 kristaps 223: prev = pp;
224: continue;
225: }
226:
227: if (prev) {
228: prev->next = pp->next;
229: pp->next = htab[hash];
230: htab[hash] = pp;
231: }
232:
1.21 kristaps 233: return(pp);
1.1 kristaps 234: }
235:
236: return(NULL);
237: }
238:
239: static inline int
1.45 ! kristaps 240: match(const struct ln *ln, const char *p, size_t sz)
1.1 kristaps 241: {
242:
1.22 kristaps 243: if (strncmp(ln->code, p, sz))
1.1 kristaps 244: return(0);
1.22 kristaps 245: return('\0' == ln->code[(int)sz]);
1.1 kristaps 246: }
CVSweb