]> git.cameronkatri.com Git - mandoc.git/blob - man_hash.c
Do not mark a block with the MDOC_BROKEN flag if it merely contains
[mandoc.git] / man_hash.c
1 /* $Id: man_hash.c,v 1.33 2015/04/19 14:00:19 schwarze Exp $ */
2 /*
3 * Copyright (c) 2008, 2009, 2010 Kristaps Dzonsons <kristaps@bsd.lv>
4 *
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.
8 *
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.
16 */
17 #include "config.h"
18
19 #include <sys/types.h>
20
21 #include <assert.h>
22 #include <ctype.h>
23 #include <limits.h>
24 #include <string.h>
25
26 #include "roff.h"
27 #include "man.h"
28 #include "libman.h"
29
30 #define HASH_DEPTH 6
31
32 #define HASH_ROW(x) do { \
33 if (isupper((unsigned char)(x))) \
34 (x) -= 65; \
35 else \
36 (x) -= 97; \
37 (x) *= HASH_DEPTH; \
38 } while (/* CONSTCOND */ 0)
39
40 /*
41 * Lookup table is indexed first by lower-case first letter (plus one
42 * for the period, which is stored in the last row), then by lower or
43 * uppercase second letter. Buckets correspond to the index of the
44 * macro (the integer value of the enum stored as a char to save a bit
45 * of space).
46 */
47 static unsigned char table[26 * HASH_DEPTH];
48
49
50 void
51 man_hash_init(void)
52 {
53 int i, j, x;
54
55 if (*table != '\0')
56 return;
57
58 memset(table, UCHAR_MAX, sizeof(table));
59
60 for (i = 0; i < (int)MAN_MAX; i++) {
61 x = man_macronames[i][0];
62
63 assert(isalpha((unsigned char)x));
64
65 HASH_ROW(x);
66
67 for (j = 0; j < HASH_DEPTH; j++)
68 if (UCHAR_MAX == table[x + j]) {
69 table[x + j] = (unsigned char)i;
70 break;
71 }
72
73 assert(j < HASH_DEPTH);
74 }
75 }
76
77 int
78 man_hash_find(const char *tmp)
79 {
80 int x, y, i;
81 int tok;
82
83 if ('\0' == (x = tmp[0]))
84 return(TOKEN_NONE);
85 if ( ! (isalpha((unsigned char)x)))
86 return(TOKEN_NONE);
87
88 HASH_ROW(x);
89
90 for (i = 0; i < HASH_DEPTH; i++) {
91 if (UCHAR_MAX == (y = table[x + i]))
92 return(TOKEN_NONE);
93
94 tok = y;
95 if (0 == strcmp(tmp, man_macronames[tok]))
96 return(tok);
97 }
98
99 return(TOKEN_NONE);
100 }