1 /****************************************************************************
2 * Copyright (c) 1998 Free Software Foundation, Inc. *
4 * Permission is hereby granted, free of charge, to any person obtaining a *
5 * copy of this software and associated documentation files (the *
6 * "Software"), to deal in the Software without restriction, including *
7 * without limitation the rights to use, copy, modify, merge, publish, *
8 * distribute, distribute with modifications, sublicense, and/or sell *
9 * copies of the Software, and to permit persons to whom the Software is *
10 * furnished to do so, subject to the following conditions: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
18 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
21 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
23 * Except as contained in this notice, the name(s) of the above copyright *
24 * holders shall not be used in advertising or otherwise to promote the *
25 * sale, use or other dealings in this Software without prior written *
27 ****************************************************************************/
29 /****************************************************************************
30 * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 *
31 * and: Eric S. Raymond <esr@snark.thyrsus.com> *
32 ****************************************************************************/
36 * comp_hash.c --- Routines to deal with the hashtable of capability
41 #include <curses.priv.h>
49 #define DEBUG(level, params) /*nothing*/
52 MODULE_ID("$Id: comp_hash.c,v 1.21 1999/06/26 21:25:11 tom Exp $")
54 static int hash_function(const char *);
57 * _nc_make_hash_table()
59 * Takes the entries in table[] and hashes them into hash_table[]
60 * by name. There are CAPTABSIZE entries in table[] and HASHTABSIZE
61 * slots in hash_table[].
68 #define MODULE_ID(id) /*nothing*/
69 #include <tinfo/doalloc.c>
71 static void _nc_make_hash_table(struct name_table_entry *table,
72 struct name_table_entry **hash_table)
78 for (i = 0; i < CAPTABSIZE; i++) {
79 hashvalue = hash_function(table[i].nte_name);
81 if (hash_table[hashvalue] != (struct name_table_entry *) 0)
84 if (hash_table[hashvalue] != 0)
85 table[i].nte_link = (short)(hash_table[hashvalue] - table);
86 hash_table[hashvalue] = &table[i];
89 DEBUG(4, ("Hash table complete: %d collisions out of %d entries", collisions, CAPTABSIZE));
95 * int hash_function(string)
97 * Computes the hashing function on the given string.
99 * The current hash function is the sum of each consectutive pair
100 * of characters, taken as two-byte integers, mod Hashtabsize.
106 hash_function(const char *string)
110 DEBUG(9, ("hashing %s", string));
112 sum += (long)(*string + (*(string + 1) << 8));
116 DEBUG(9, ("sum is %ld", sum));
117 return (int)(sum % HASHTABSIZE);
122 * struct name_table_entry *
125 * Finds the entry for the given string in the hash table if present.
126 * Returns a pointer to the entry in the table or 0 if not found.
131 struct name_table_entry const *
132 _nc_find_entry(const char *string, const struct name_table_entry *const *hash_table)
135 struct name_table_entry const *ptr;
137 hashvalue = hash_function(string);
139 if ((ptr = hash_table[hashvalue]) != 0) {
140 while (strcmp(ptr->nte_name, string) != 0) {
141 if (ptr->nte_link < 0)
143 ptr = ptr->nte_link + hash_table[HASHTABSIZE];
151 * struct name_table_entry *
152 * find_type_entry(string, type, table)
154 * Finds the first entry for the given name with the given type in the
155 * given table if present (as distinct from find_entry, which finds the
156 * the last entry regardless of type). You can use this if you detect
157 * a name clash. It's slower, though. Returns a pointer to the entry
158 * in the table or 0 if not found.
161 struct name_table_entry const *
162 _nc_find_type_entry(const char *string,
164 const struct name_table_entry *table)
166 struct name_table_entry const *ptr;
168 for (ptr = table; ptr < table + CAPTABSIZE; ptr++) {
169 if (ptr->nte_type == type && strcmp(string, ptr->nte_name) == 0)
173 return ((struct name_table_entry *)NULL);
179 * This filter reads from standard input a list of tab-delimited columns,
180 * (e.g., from Caps.filtered) computes the hash-value of a specified column and
181 * writes the hashed tables to standard output.
183 * By compiling the hash table at build time, we're able to make the entire
184 * set of terminfo and termcap tables readonly (and also provide some runtime
185 * performance enhancement).
188 #define MAX_COLUMNS BUFSIZ /* this _has_ to be worst-case */
190 static char **parse_columns(char *buffer)
196 if (list == 0 && (list = typeCalloc(char *, MAX_COLUMNS)) == 0)
199 if (*buffer != '#') {
200 while (*buffer != '\0') {
202 for (s = buffer; (*s != '\0') && !isspace(*s); s++)
209 && (s[-1] == '"')) { /* strip the quotes */
217 while (*++s && isspace(*s))
224 return col ? list : 0;
227 int main(int argc, char **argv)
229 struct name_table_entry *name_table = typeCalloc(struct name_table_entry, CAPTABSIZE);
230 struct name_table_entry **hash_table = typeCalloc(struct name_table_entry *, HASHTABSIZE);
231 const char *root_name = "";
236 static const char * typenames[] = { "BOOLEAN", "NUMBER", "STRING" };
242 /* The first argument is the column-number (starting with 0).
243 * The second is the root name of the tables to generate.
246 || (column = atoi(argv[1])) <= 0
247 || (column >= MAX_COLUMNS)
248 || *(root_name = argv[2]) == 0) {
249 fprintf(stderr, "usage: make_hash column root_name\n");
254 * Read the table into our arrays.
256 for (n = 0; (n < CAPTABSIZE) && fgets(buffer, BUFSIZ, stdin); ) {
257 char **list, *nlp = strchr(buffer, '\n');
260 list = parse_columns(buffer);
261 if (list == 0) /* blank or comment */
263 name_table[n].nte_link = -1; /* end-of-hash */
264 name_table[n].nte_name = strdup(list[column]);
265 if (!strcmp(list[2], "bool")) {
266 name_table[n].nte_type = BOOLEAN;
267 name_table[n].nte_index = BoolCount++;
268 } else if (!strcmp(list[2], "num")) {
269 name_table[n].nte_type = NUMBER;
270 name_table[n].nte_index = NumCount++;
271 } else if (!strcmp(list[2], "str")) {
272 name_table[n].nte_type = STRING;
273 name_table[n].nte_index = StrCount++;
275 fprintf(stderr, "Unknown type: %s\n", list[2]);
280 _nc_make_hash_table(name_table, hash_table);
283 * Write the compiled tables to standard output
285 printf("static struct name_table_entry const _nc_%s_table[] =\n",
288 for (n = 0; n < CAPTABSIZE; n++) {
289 sprintf(buffer, "\"%s\"",
290 name_table[n].nte_name);
291 printf("\t{ %15s,\t%10s,\t%3d, %3d }%c\n",
293 typenames[name_table[n].nte_type],
294 name_table[n].nte_index,
295 name_table[n].nte_link,
296 n < CAPTABSIZE - 1 ? ',' : ' ');
300 printf("const struct name_table_entry * const _nc_%s_hash_table[%d] =\n",
304 for (n = 0; n < HASHTABSIZE; n++) {
305 if (hash_table[n] != 0) {
306 sprintf(buffer, "_nc_%s_table + %3ld",
308 (long) (hash_table[n] - name_table));
312 printf("\t%s,\n", buffer);
314 printf("\t_nc_%s_table\t/* base-of-table */\n", root_name);
317 printf("#if (BOOLCOUNT!=%d)||(NUMCOUNT!=%d)||(STRCOUNT!=%d)\n",
318 BoolCount, NumCount, StrCount);
319 printf("#error\t--> term.h and comp_captab.c disagree about the <--\n");
320 printf("#error\t--> numbers of booleans, numbers and/or strings <--\n");
321 printf("#endif\n\n");