xref: /minix3/external/bsd/mdocml/dist/man_hash.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	Id: man_hash.c,v 1.25 2011/07/24 18:15:14 kristaps Exp  */
2d65f6f70SBen Gras /*
3d65f6f70SBen Gras  * Copyright (c) 2008, 2009, 2010 Kristaps Dzonsons <kristaps@bsd.lv>
4d65f6f70SBen Gras  *
5d65f6f70SBen Gras  * Permission to use, copy, modify, and distribute this software for any
6d65f6f70SBen Gras  * purpose with or without fee is hereby granted, provided that the above
7d65f6f70SBen Gras  * copyright notice and this permission notice appear in all copies.
8d65f6f70SBen Gras  *
9d65f6f70SBen Gras  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10d65f6f70SBen Gras  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11d65f6f70SBen Gras  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12d65f6f70SBen Gras  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13d65f6f70SBen Gras  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14d65f6f70SBen Gras  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15d65f6f70SBen Gras  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16d65f6f70SBen Gras  */
17d65f6f70SBen Gras #ifdef HAVE_CONFIG_H
18d65f6f70SBen Gras #include "config.h"
19d65f6f70SBen Gras #endif
20d65f6f70SBen Gras 
21d65f6f70SBen Gras #include <sys/types.h>
22d65f6f70SBen Gras 
23d65f6f70SBen Gras #include <assert.h>
24d65f6f70SBen Gras #include <ctype.h>
25d65f6f70SBen Gras #include <limits.h>
26d65f6f70SBen Gras #include <stdlib.h>
27d65f6f70SBen Gras #include <string.h>
28d65f6f70SBen Gras 
2992395e9cSLionel Sambuc #include "man.h"
30d65f6f70SBen Gras #include "mandoc.h"
31d65f6f70SBen Gras #include "libman.h"
32d65f6f70SBen Gras 
33d65f6f70SBen Gras #define	HASH_DEPTH	 6
34d65f6f70SBen Gras 
35d65f6f70SBen Gras #define	HASH_ROW(x) do { \
3692395e9cSLionel Sambuc 		if (isupper((unsigned char)(x))) \
37d65f6f70SBen Gras 			(x) -= 65; \
38d65f6f70SBen Gras 		else \
39d65f6f70SBen Gras 			(x) -= 97; \
40d65f6f70SBen Gras 		(x) *= HASH_DEPTH; \
41d65f6f70SBen Gras 	} while (/* CONSTCOND */ 0)
42d65f6f70SBen Gras 
43d65f6f70SBen Gras /*
44d65f6f70SBen Gras  * Lookup table is indexed first by lower-case first letter (plus one
45d65f6f70SBen Gras  * for the period, which is stored in the last row), then by lower or
46d65f6f70SBen Gras  * uppercase second letter.  Buckets correspond to the index of the
47d65f6f70SBen Gras  * macro (the integer value of the enum stored as a char to save a bit
48d65f6f70SBen Gras  * of space).
49d65f6f70SBen Gras  */
5092395e9cSLionel Sambuc static	unsigned char	 table[26 * HASH_DEPTH];
51d65f6f70SBen Gras 
52d65f6f70SBen Gras /*
53d65f6f70SBen Gras  * XXX - this hash has global scope, so if intended for use as a library
54d65f6f70SBen Gras  * with multiple callers, it will need re-invocation protection.
55d65f6f70SBen Gras  */
56d65f6f70SBen Gras void
man_hash_init(void)57d65f6f70SBen Gras man_hash_init(void)
58d65f6f70SBen Gras {
59d65f6f70SBen Gras 	int		 i, j, x;
60d65f6f70SBen Gras 
61d65f6f70SBen Gras 	memset(table, UCHAR_MAX, sizeof(table));
62d65f6f70SBen Gras 
63d65f6f70SBen Gras 	assert(/* LINTED */
64d65f6f70SBen Gras 			MAN_MAX < UCHAR_MAX);
65d65f6f70SBen Gras 
66d65f6f70SBen Gras 	for (i = 0; i < (int)MAN_MAX; i++) {
67d65f6f70SBen Gras 		x = man_macronames[i][0];
68d65f6f70SBen Gras 
6992395e9cSLionel Sambuc 		assert(isalpha((unsigned char)x));
70d65f6f70SBen Gras 
71d65f6f70SBen Gras 		HASH_ROW(x);
72d65f6f70SBen Gras 
73d65f6f70SBen Gras 		for (j = 0; j < HASH_DEPTH; j++)
74d65f6f70SBen Gras 			if (UCHAR_MAX == table[x + j]) {
7592395e9cSLionel Sambuc 				table[x + j] = (unsigned char)i;
76d65f6f70SBen Gras 				break;
77d65f6f70SBen Gras 			}
78d65f6f70SBen Gras 
79d65f6f70SBen Gras 		assert(j < HASH_DEPTH);
80d65f6f70SBen Gras 	}
81d65f6f70SBen Gras }
82d65f6f70SBen Gras 
83d65f6f70SBen Gras 
84d65f6f70SBen Gras enum mant
man_hash_find(const char * tmp)85d65f6f70SBen Gras man_hash_find(const char *tmp)
86d65f6f70SBen Gras {
87d65f6f70SBen Gras 	int		 x, y, i;
88d65f6f70SBen Gras 	enum mant	 tok;
89d65f6f70SBen Gras 
90d65f6f70SBen Gras 	if ('\0' == (x = tmp[0]))
91d65f6f70SBen Gras 		return(MAN_MAX);
9292395e9cSLionel Sambuc 	if ( ! (isalpha((unsigned char)x)))
93d65f6f70SBen Gras 		return(MAN_MAX);
94d65f6f70SBen Gras 
95d65f6f70SBen Gras 	HASH_ROW(x);
96d65f6f70SBen Gras 
97d65f6f70SBen Gras 	for (i = 0; i < HASH_DEPTH; i++) {
98d65f6f70SBen Gras 		if (UCHAR_MAX == (y = table[x + i]))
99d65f6f70SBen Gras 			return(MAN_MAX);
100d65f6f70SBen Gras 
101d65f6f70SBen Gras 		tok = (enum mant)y;
102d65f6f70SBen Gras 		if (0 == strcmp(tmp, man_macronames[tok]))
103d65f6f70SBen Gras 			return(tok);
104d65f6f70SBen Gras 	}
105d65f6f70SBen Gras 
106d65f6f70SBen Gras 	return(MAN_MAX);
107d65f6f70SBen Gras }
108