Remove building with NOCRYPTO option
[minix3.git] / external / bsd / mdocml / dist / man_hash.c
blob86577ddbd6660a2549fdbeb1e214c139dcd050c5
1 /* Id: man_hash.c,v 1.25 2011/07/24 18:15:14 kristaps Exp */
2 /*
3 * Copyright (c) 2008, 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.
17 #ifdef HAVE_CONFIG_H
18 #include "config.h"
19 #endif
21 #include <sys/types.h>
23 #include <assert.h>
24 #include <ctype.h>
25 #include <limits.h>
26 #include <stdlib.h>
27 #include <string.h>
29 #include "man.h"
30 #include "mandoc.h"
31 #include "libman.h"
33 #define HASH_DEPTH 6
35 #define HASH_ROW(x) do { \
36 if (isupper((unsigned char)(x))) \
37 (x) -= 65; \
38 else \
39 (x) -= 97; \
40 (x) *= HASH_DEPTH; \
41 } while (/* CONSTCOND */ 0)
44 * Lookup table is indexed first by lower-case first letter (plus one
45 * for the period, which is stored in the last row), then by lower or
46 * uppercase second letter. Buckets correspond to the index of the
47 * macro (the integer value of the enum stored as a char to save a bit
48 * of space).
50 static unsigned char table[26 * HASH_DEPTH];
53 * XXX - this hash has global scope, so if intended for use as a library
54 * with multiple callers, it will need re-invocation protection.
56 void
57 man_hash_init(void)
59 int i, j, x;
61 memset(table, UCHAR_MAX, sizeof(table));
63 assert(/* LINTED */
64 MAN_MAX < UCHAR_MAX);
66 for (i = 0; i < (int)MAN_MAX; i++) {
67 x = man_macronames[i][0];
69 assert(isalpha((unsigned char)x));
71 HASH_ROW(x);
73 for (j = 0; j < HASH_DEPTH; j++)
74 if (UCHAR_MAX == table[x + j]) {
75 table[x + j] = (unsigned char)i;
76 break;
79 assert(j < HASH_DEPTH);
84 enum mant
85 man_hash_find(const char *tmp)
87 int x, y, i;
88 enum mant tok;
90 if ('\0' == (x = tmp[0]))
91 return(MAN_MAX);
92 if ( ! (isalpha((unsigned char)x)))
93 return(MAN_MAX);
95 HASH_ROW(x);
97 for (i = 0; i < HASH_DEPTH; i++) {
98 if (UCHAR_MAX == (y = table[x + i]))
99 return(MAN_MAX);
101 tok = (enum mant)y;
102 if (0 == strcmp(tmp, man_macronames[tok]))
103 return(tok);
106 return(MAN_MAX);