]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/mdocml/mdoc_hash.c
Merge ACPICA 20141107 and 20150204.
[FreeBSD/FreeBSD.git] / contrib / mdocml / mdoc_hash.c
1 /*      $Id: mdoc_hash.c,v 1.21 2014/08/10 23:54:41 schwarze Exp $ */
2 /*
3  * Copyright (c) 2008, 2009 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 <stdlib.h>
25 #include <stdio.h>
26 #include <string.h>
27
28 #include "mdoc.h"
29 #include "libmdoc.h"
30
31 static  unsigned char    table[27 * 12];
32
33
34 /*
35  * XXX - this hash has global scope, so if intended for use as a library
36  * with multiple callers, it will need re-invocation protection.
37  */
38 void
39 mdoc_hash_init(void)
40 {
41         int              i, j, major;
42         const char      *p;
43
44         memset(table, UCHAR_MAX, sizeof(table));
45
46         for (i = 0; i < (int)MDOC_MAX; i++) {
47                 p = mdoc_macronames[i];
48
49                 if (isalpha((unsigned char)p[1]))
50                         major = 12 * (tolower((unsigned char)p[1]) - 97);
51                 else
52                         major = 12 * 26;
53
54                 for (j = 0; j < 12; j++)
55                         if (UCHAR_MAX == table[major + j]) {
56                                 table[major + j] = (unsigned char)i;
57                                 break;
58                         }
59
60                 assert(j < 12);
61         }
62 }
63
64 enum mdoct
65 mdoc_hash_find(const char *p)
66 {
67         int               major, i, j;
68
69         if (0 == p[0])
70                 return(MDOC_MAX);
71         if ( ! isalpha((unsigned char)p[0]) && '%' != p[0])
72                 return(MDOC_MAX);
73
74         if (isalpha((unsigned char)p[1]))
75                 major = 12 * (tolower((unsigned char)p[1]) - 97);
76         else if ('1' == p[1])
77                 major = 12 * 26;
78         else
79                 return(MDOC_MAX);
80
81         if (p[2] && p[3])
82                 return(MDOC_MAX);
83
84         for (j = 0; j < 12; j++) {
85                 if (UCHAR_MAX == (i = table[major + j]))
86                         break;
87                 if (0 == strcmp(p, mdoc_macronames[i]))
88                         return((enum mdoct)i);
89         }
90
91         return(MDOC_MAX);
92 }