]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc_hash.c
1 /* $Id: mdoc_hash.c,v 1.8 2009/07/20 20:49:22 kristaps Exp $ */
3 * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se>
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.
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.
17 #include <sys/types.h>
27 #define ADJUST_MAJOR(x) \
29 (x) = 0; /* % -> 00 */ \
31 (x) -= 64; /* A-Z -> 01 - 26 */ \
33 (x) -= 70; /* a-z -> 27 - 52 */ \
34 while (/*CONSTCOND*/0)
36 #define ADJUST_MINOR(y) \
38 (y) = 0; /* 1 -> 00 */ \
40 (y) -= 65; /* A-Z -> 00 - 25 */ \
42 (y) -= 97; /* a-z -> 00 - 25 */ \
43 while (/*CONSTCOND*/0)
45 #define INDEX(maj, min) \
46 ((maj) * 26 * 3) + ((min) * 3)
48 #define SLOTCMP(slot, val) \
49 (mdoc_macronames[(slot)][0] == (val)[0] && \
50 mdoc_macronames[(slot)][1] == (val)[1] && \
52 mdoc_macronames[(slot)][2] == (val)[2]))
56 mdoc_hash_free(void *htab
)
67 int i
, major
, minor
, ind
;
70 htab
= calloc(26 * 3 * 52, sizeof(struct mdoc_macro
*));
74 for (i
= 0; i
< MDOC_MAX
; i
++) {
75 major
= mdoc_macronames
[i
][0];
76 assert(isalpha((u_char
)major
) || 37 == major
);
80 minor
= mdoc_macronames
[i
][1];
81 assert(isalpha((u_char
)minor
) || 49 == minor
);
85 ind
= INDEX(major
, minor
);
87 if (NULL
== htab
[ind
]) {
88 htab
[ind
] = &mdoc_macros
[i
];
92 if (NULL
== htab
[++ind
]) {
93 htab
[ind
] = &mdoc_macros
[i
];
97 assert(NULL
== htab
[++ind
]);
98 htab
[ind
] = &mdoc_macros
[i
];
101 return((void *)htab
);
106 mdoc_hash_find(const void *arg
, const char *tmp
)
108 int major
, minor
, ind
, slot
;
114 if (0 == (major
= tmp
[0]))
116 if (0 == (minor
= tmp
[1]))
119 if (tmp
[2] && tmp
[3])
122 if (37 != major
&& ! isalpha((u_char
)major
))
124 if (49 != minor
&& ! isalpha((u_char
)minor
))
130 ind
= INDEX(major
, minor
);
132 if (ind
< 0 || ind
>= 26 * 3 * 52)
136 slot
= htab
[ind
] - /* LINTED */
138 assert(0 == (size_t)slot
% sizeof(struct mdoc_macro
));
139 slot
/= sizeof(struct mdoc_macro
);
140 if (SLOTCMP(slot
, tmp
))
146 slot
= htab
[ind
] - /* LINTED */
148 assert(0 == (size_t)slot
% sizeof(struct mdoc_macro
));
149 slot
/= sizeof(struct mdoc_macro
);
150 if (SLOTCMP(slot
, tmp
))
155 if (NULL
== htab
[ind
])
157 slot
= htab
[ind
] - /* LINTED */
159 assert(0 == (size_t)slot
% sizeof(struct mdoc_macro
));
160 slot
/= sizeof(struct mdoc_macro
);
161 if (SLOTCMP(slot
, tmp
))