Removing warning about OSNAME.
[minix.git] / external / bsd / mdocml / dist / man_hash.c
blob7c31e1b48491c8fb0f7ffecff4eeeb4e150ca104
1 /* $Vendor-Id: man_hash.c,v 1.23 2010/07/31 23:52:58 schwarze 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 "mandoc.h"
30 #include "libman.h"
32 #define HASH_DEPTH 6
34 #define HASH_ROW(x) do { \
35 if (isupper((u_char)(x))) \
36 (x) -= 65; \
37 else \
38 (x) -= 97; \
39 (x) *= HASH_DEPTH; \
40 } while (/* CONSTCOND */ 0)
43 * Lookup table is indexed first by lower-case first letter (plus one
44 * for the period, which is stored in the last row), then by lower or
45 * uppercase second letter. Buckets correspond to the index of the
46 * macro (the integer value of the enum stored as a char to save a bit
47 * of space).
49 static u_char table[26 * HASH_DEPTH];
52 * XXX - this hash has global scope, so if intended for use as a library
53 * with multiple callers, it will need re-invocation protection.
55 void
56 man_hash_init(void)
58 int i, j, x;
60 memset(table, UCHAR_MAX, sizeof(table));
62 assert(/* LINTED */
63 MAN_MAX < UCHAR_MAX);
65 for (i = 0; i < (int)MAN_MAX; i++) {
66 x = man_macronames[i][0];
68 assert(isalpha((u_char)x));
70 HASH_ROW(x);
72 for (j = 0; j < HASH_DEPTH; j++)
73 if (UCHAR_MAX == table[x + j]) {
74 table[x + j] = (u_char)i;
75 break;
78 assert(j < HASH_DEPTH);
83 enum mant
84 man_hash_find(const char *tmp)
86 int x, y, i;
87 enum mant tok;
89 if ('\0' == (x = tmp[0]))
90 return(MAN_MAX);
91 if ( ! (isalpha((u_char)x)))
92 return(MAN_MAX);
94 HASH_ROW(x);
96 for (i = 0; i < HASH_DEPTH; i++) {
97 if (UCHAR_MAX == (y = table[x + i]))
98 return(MAN_MAX);
100 tok = (enum mant)y;
101 if (0 == strcmp(tmp, man_macronames[tok]))
102 return(tok);
105 return(MAN_MAX);