Annotation of src/usr.bin/mandoc/mdoc_hash.c, Revision 1.15
1.15 ! schwarze 1: /* $Id: mdoc_hash.c,v 1.14 2014/03/21 22:17:01 schwarze Exp $ */
1.1 kristaps 2: /*
1.11 schwarze 3: * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@bsd.lv>
1.1 kristaps 4: *
5: * Permission to use, copy, modify, and distribute this software for any
1.2 schwarze 6: * purpose with or without fee is hereby granted, provided that the above
7: * copyright notice and this permission notice appear in all copies.
1.1 kristaps 8: *
1.2 schwarze 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.
1.1 kristaps 16: */
1.5 schwarze 17: #include <sys/types.h>
18:
1.1 kristaps 19: #include <assert.h>
20: #include <ctype.h>
1.6 schwarze 21: #include <limits.h>
1.1 kristaps 22: #include <stdlib.h>
23: #include <stdio.h>
24: #include <string.h>
25:
1.12 schwarze 26: #include "mdoc.h"
1.1 kristaps 27: #include "libmdoc.h"
28:
1.13 schwarze 29: static unsigned char table[27 * 12];
1.1 kristaps 30:
1.15 ! schwarze 31:
1.6 schwarze 32: /*
33: * XXX - this hash has global scope, so if intended for use as a library
34: * with multiple callers, it will need re-invocation protection.
35: */
1.1 kristaps 36: void
1.6 schwarze 37: mdoc_hash_init(void)
1.1 kristaps 38: {
1.6 schwarze 39: int i, j, major;
40: const char *p;
1.1 kristaps 41:
1.6 schwarze 42: memset(table, UCHAR_MAX, sizeof(table));
1.1 kristaps 43:
1.9 schwarze 44: for (i = 0; i < (int)MDOC_MAX; i++) {
1.6 schwarze 45: p = mdoc_macronames[i];
1.1 kristaps 46:
1.13 schwarze 47: if (isalpha((unsigned char)p[1]))
48: major = 12 * (tolower((unsigned char)p[1]) - 97);
1.6 schwarze 49: else
1.13 schwarze 50: major = 12 * 26;
1.6 schwarze 51:
1.13 schwarze 52: for (j = 0; j < 12; j++)
1.6 schwarze 53: if (UCHAR_MAX == table[major + j]) {
1.13 schwarze 54: table[major + j] = (unsigned char)i;
1.6 schwarze 55: break;
56: }
1.1 kristaps 57:
1.13 schwarze 58: assert(j < 12);
1.1 kristaps 59: }
60: }
61:
1.8 schwarze 62: enum mdoct
1.6 schwarze 63: mdoc_hash_find(const char *p)
1.1 kristaps 64: {
1.6 schwarze 65: int major, i, j;
1.1 kristaps 66:
1.6 schwarze 67: if (0 == p[0])
1.1 kristaps 68: return(MDOC_MAX);
1.13 schwarze 69: if ( ! isalpha((unsigned char)p[0]) && '%' != p[0])
1.4 schwarze 70: return(MDOC_MAX);
71:
1.13 schwarze 72: if (isalpha((unsigned char)p[1]))
73: major = 12 * (tolower((unsigned char)p[1]) - 97);
1.6 schwarze 74: else if ('1' == p[1])
1.13 schwarze 75: major = 12 * 26;
1.15 ! schwarze 76: else
1.1 kristaps 77: return(MDOC_MAX);
78:
1.6 schwarze 79: if (p[2] && p[3])
1.1 kristaps 80: return(MDOC_MAX);
81:
1.13 schwarze 82: for (j = 0; j < 12; j++) {
1.6 schwarze 83: if (UCHAR_MAX == (i = table[major + j]))
84: break;
85: if (0 == strcmp(p, mdoc_macronames[i]))
1.8 schwarze 86: return((enum mdoct)i);
1.1 kristaps 87: }
88:
89: return(MDOC_MAX);
90: }