]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/mdocml/man_hash.c
Update tcpdump to 4.9.0.
[FreeBSD/FreeBSD.git] / contrib / mdocml / man_hash.c
1 /*      $Id: man_hash.c,v 1.35 2016/07/15 18:03:45 schwarze Exp $ */
2 /*
3  * Copyright (c) 2008, 2009, 2010 Kristaps Dzonsons <kristaps@bsd.lv>
4  * Copyright (c) 2015 Ingo Schwarze <schwarze@openbsd.org>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 #include "config.h"
19
20 #include <sys/types.h>
21
22 #include <assert.h>
23 #include <ctype.h>
24 #include <limits.h>
25 #include <string.h>
26
27 #include "mandoc.h"
28 #include "roff.h"
29 #include "man.h"
30 #include "libmandoc.h"
31 #include "libman.h"
32
33 #define HASH_DEPTH       6
34
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)
42
43 /*
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).
49  */
50 static  unsigned char    table[26 * HASH_DEPTH];
51
52
53 void
54 man_hash_init(void)
55 {
56         int              i, j, x;
57
58         if (*table != '\0')
59                 return;
60
61         memset(table, UCHAR_MAX, sizeof(table));
62
63         for (i = 0; i < (int)MAN_MAX; i++) {
64                 x = man_macronames[i][0];
65
66                 assert(isalpha((unsigned char)x));
67
68                 HASH_ROW(x);
69
70                 for (j = 0; j < HASH_DEPTH; j++)
71                         if (UCHAR_MAX == table[x + j]) {
72                                 table[x + j] = (unsigned char)i;
73                                 break;
74                         }
75
76                 assert(j < HASH_DEPTH);
77         }
78 }
79
80 int
81 man_hash_find(const char *tmp)
82 {
83         int              x, y, i;
84         int              tok;
85
86         if ('\0' == (x = tmp[0]))
87                 return TOKEN_NONE;
88         if ( ! (isalpha((unsigned char)x)))
89                 return TOKEN_NONE;
90
91         HASH_ROW(x);
92
93         for (i = 0; i < HASH_DEPTH; i++) {
94                 if (UCHAR_MAX == (y = table[x + i]))
95                         return TOKEN_NONE;
96
97                 tok = y;
98                 if (0 == strcmp(tmp, man_macronames[tok]))
99                         return tok;
100         }
101
102         return TOKEN_NONE;
103 }