1*260e9a87SYuri Pankov /* $Id: man_hash.c,v 1.29 2014/12/01 08:05:52 schwarze Exp $ */
295c635efSGarrett D'Amore /*
395c635efSGarrett D'Amore * Copyright (c) 2008, 2009, 2010 Kristaps Dzonsons <kristaps@bsd.lv>
495c635efSGarrett D'Amore *
595c635efSGarrett D'Amore * Permission to use, copy, modify, and distribute this software for any
695c635efSGarrett D'Amore * purpose with or without fee is hereby granted, provided that the above
795c635efSGarrett D'Amore * copyright notice and this permission notice appear in all copies.
895c635efSGarrett D'Amore *
995c635efSGarrett D'Amore * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1095c635efSGarrett D'Amore * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1195c635efSGarrett D'Amore * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1295c635efSGarrett D'Amore * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1395c635efSGarrett D'Amore * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1495c635efSGarrett D'Amore * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1595c635efSGarrett D'Amore * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1695c635efSGarrett D'Amore */
1795c635efSGarrett D'Amore #include "config.h"
1895c635efSGarrett D'Amore
1995c635efSGarrett D'Amore #include <sys/types.h>
2095c635efSGarrett D'Amore
2195c635efSGarrett D'Amore #include <assert.h>
2295c635efSGarrett D'Amore #include <ctype.h>
2395c635efSGarrett D'Amore #include <limits.h>
2495c635efSGarrett D'Amore #include <string.h>
2595c635efSGarrett D'Amore
2695c635efSGarrett D'Amore #include "man.h"
2795c635efSGarrett D'Amore #include "libman.h"
2895c635efSGarrett D'Amore
2995c635efSGarrett D'Amore #define HASH_DEPTH 6
3095c635efSGarrett D'Amore
3195c635efSGarrett D'Amore #define HASH_ROW(x) do { \
3295c635efSGarrett D'Amore if (isupper((unsigned char)(x))) \
3395c635efSGarrett D'Amore (x) -= 65; \
3495c635efSGarrett D'Amore else \
3595c635efSGarrett D'Amore (x) -= 97; \
3695c635efSGarrett D'Amore (x) *= HASH_DEPTH; \
3795c635efSGarrett D'Amore } while (/* CONSTCOND */ 0)
3895c635efSGarrett D'Amore
3995c635efSGarrett D'Amore /*
4095c635efSGarrett D'Amore * Lookup table is indexed first by lower-case first letter (plus one
4195c635efSGarrett D'Amore * for the period, which is stored in the last row), then by lower or
4295c635efSGarrett D'Amore * uppercase second letter. Buckets correspond to the index of the
4395c635efSGarrett D'Amore * macro (the integer value of the enum stored as a char to save a bit
4495c635efSGarrett D'Amore * of space).
4595c635efSGarrett D'Amore */
4695c635efSGarrett D'Amore static unsigned char table[26 * HASH_DEPTH];
4795c635efSGarrett D'Amore
48*260e9a87SYuri Pankov
4995c635efSGarrett D'Amore /*
5095c635efSGarrett D'Amore * XXX - this hash has global scope, so if intended for use as a library
5195c635efSGarrett D'Amore * with multiple callers, it will need re-invocation protection.
5295c635efSGarrett D'Amore */
5395c635efSGarrett D'Amore void
man_hash_init(void)5495c635efSGarrett D'Amore man_hash_init(void)
5595c635efSGarrett D'Amore {
5695c635efSGarrett D'Amore int i, j, x;
5795c635efSGarrett D'Amore
5895c635efSGarrett D'Amore memset(table, UCHAR_MAX, sizeof(table));
5995c635efSGarrett D'Amore
60*260e9a87SYuri Pankov assert(MAN_MAX < UCHAR_MAX);
6195c635efSGarrett D'Amore
6295c635efSGarrett D'Amore for (i = 0; i < (int)MAN_MAX; i++) {
6395c635efSGarrett D'Amore x = man_macronames[i][0];
6495c635efSGarrett D'Amore
6595c635efSGarrett D'Amore assert(isalpha((unsigned char)x));
6695c635efSGarrett D'Amore
6795c635efSGarrett D'Amore HASH_ROW(x);
6895c635efSGarrett D'Amore
6995c635efSGarrett D'Amore for (j = 0; j < HASH_DEPTH; j++)
7095c635efSGarrett D'Amore if (UCHAR_MAX == table[x + j]) {
7195c635efSGarrett D'Amore table[x + j] = (unsigned char)i;
7295c635efSGarrett D'Amore break;
7395c635efSGarrett D'Amore }
7495c635efSGarrett D'Amore
7595c635efSGarrett D'Amore assert(j < HASH_DEPTH);
7695c635efSGarrett D'Amore }
7795c635efSGarrett D'Amore }
7895c635efSGarrett D'Amore
7995c635efSGarrett D'Amore enum mant
man_hash_find(const char * tmp)8095c635efSGarrett D'Amore man_hash_find(const char *tmp)
8195c635efSGarrett D'Amore {
8295c635efSGarrett D'Amore int x, y, i;
8395c635efSGarrett D'Amore enum mant tok;
8495c635efSGarrett D'Amore
8595c635efSGarrett D'Amore if ('\0' == (x = tmp[0]))
8695c635efSGarrett D'Amore return(MAN_MAX);
8795c635efSGarrett D'Amore if ( ! (isalpha((unsigned char)x)))
8895c635efSGarrett D'Amore return(MAN_MAX);
8995c635efSGarrett D'Amore
9095c635efSGarrett D'Amore HASH_ROW(x);
9195c635efSGarrett D'Amore
9295c635efSGarrett D'Amore for (i = 0; i < HASH_DEPTH; i++) {
9395c635efSGarrett D'Amore if (UCHAR_MAX == (y = table[x + i]))
9495c635efSGarrett D'Amore return(MAN_MAX);
9595c635efSGarrett D'Amore
9695c635efSGarrett D'Amore tok = (enum mant)y;
9795c635efSGarrett D'Amore if (0 == strcmp(tmp, man_macronames[tok]))
9895c635efSGarrett D'Amore return(tok);
9995c635efSGarrett D'Amore }
10095c635efSGarrett D'Amore
10195c635efSGarrett D'Amore return(MAN_MAX);
10295c635efSGarrett D'Amore }
103