1 /****************************************************************************
2 * Copyright (c) 1998-2005,2007 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 * and: Thomas E. Dickey 1996-on *
33 ****************************************************************************/
36 * comp_hash.c --- Routines to deal with the hashtable of capability
42 #include <curses.priv.h>
50 #define DEBUG(level, params) /*nothing */
53 MODULE_ID("$Id: comp_hash.c,v 1.33 2007/08/18 21:42:25 tom Exp $")
55 static int hash_function(const char *);
58 * _nc_make_hash_table()
60 * Takes the entries in table[] and hashes them into hash_table[]
61 * by name. There are CAPTABSIZE entries in table[] and HASHTABSIZE
62 * slots in hash_table[].
69 #define MODULE_ID(id) /*nothing */
70 #include <tinfo/doalloc.c>
73 _nc_make_hash_table(struct name_table_entry *table,
80 for (i = 0; i < HASHTABSIZE; i++) {
83 for (i = 0; i < CAPTABSIZE; i++) {
84 hashvalue = hash_function(table[i].nte_name);
86 if (hash_table[hashvalue] >= 0)
89 if (hash_table[hashvalue] != 0)
90 table[i].nte_link = hash_table[hashvalue];
91 hash_table[hashvalue] = i;
94 DEBUG(4, ("Hash table complete: %d collisions out of %d entries",
95 collisions, CAPTABSIZE));
100 * int hash_function(string)
102 * Computes the hashing function on the given string.
104 * The current hash function is the sum of each consectutive pair
105 * of characters, taken as two-byte integers, mod HASHTABSIZE.
110 hash_function(const char *string)
114 DEBUG(9, ("hashing %s", string));
116 sum += (long) (*string + (*(string + 1) << 8));
120 DEBUG(9, ("sum is %ld", sum));
121 return (int) (sum % HASHTABSIZE);
125 * struct name_table_entry *
128 * Finds the entry for the given string in the hash table if present.
129 * Returns a pointer to the entry in the table or 0 if not found.
134 NCURSES_EXPORT(struct name_table_entry const *)
135 _nc_find_entry(const char *string,
136 const short *hash_table)
139 struct name_table_entry const *ptr = 0;
140 struct name_table_entry const *real_table;
142 hashvalue = hash_function(string);
144 if (hash_table[hashvalue] >= 0) {
145 real_table = _nc_get_table(hash_table != _nc_get_hash_table(FALSE));
146 ptr = real_table + hash_table[hashvalue];
147 while (strcmp(ptr->nte_name, string) != 0) {
148 if (ptr->nte_link < 0)
150 ptr = real_table + (ptr->nte_link + hash_table[HASHTABSIZE]);
158 * struct name_table_entry *
159 * find_type_entry(string, type, table)
161 * Finds the first entry for the given name with the given type in the
162 * given table if present (as distinct from find_entry, which finds the
163 * the last entry regardless of type). You can use this if you detect
164 * a name clash. It's slower, though. Returns a pointer to the entry
165 * in the table or 0 if not found.
168 NCURSES_EXPORT(struct name_table_entry const *)
169 _nc_find_type_entry(const char *string,
171 const struct name_table_entry *table)
173 struct name_table_entry const *ptr;
175 for (ptr = table; ptr < table + CAPTABSIZE; ptr++) {
176 if (ptr->nte_type == type && strcmp(string, ptr->nte_name) == 0)
180 return ((struct name_table_entry *) NULL);
186 * This filter reads from standard input a list of tab-delimited columns,
187 * (e.g., from Caps.filtered) computes the hash-value of a specified column and
188 * writes the hashed tables to standard output.
190 * By compiling the hash table at build time, we're able to make the entire
191 * set of terminfo and termcap tables readonly (and also provide some runtime
192 * performance enhancement).
195 #define MAX_COLUMNS BUFSIZ /* this _has_ to be worst-case */
198 parse_columns(char *buffer)
204 if (list == 0 && (list = typeCalloc(char *, MAX_COLUMNS)) == 0)
207 if (*buffer != '#') {
208 while (*buffer != '\0') {
210 for (s = buffer; (*s != '\0') && !isspace(UChar(*s)); s++)
217 && (s[-1] == '"')) { /* strip the quotes */
225 while (*++s && isspace(UChar(*s)))
232 return col ? list : 0;
236 main(int argc, char **argv)
238 struct name_table_entry *name_table = typeCalloc(struct
239 name_table_entry, CAPTABSIZE);
240 short *hash_table = typeCalloc(short, HASHTABSIZE);
241 const char *root_name = "";
247 static const char *typenames[] =
248 {"BOOLEAN", "NUMBER", "STRING"};
254 /* The first argument is the column-number (starting with 0).
255 * The second is the root name of the tables to generate.
258 || (column = atoi(argv[1])) <= 0
259 || (column >= MAX_COLUMNS)
260 || *(root_name = argv[2]) == 0
261 || (bigstring = atoi(argv[3])) < 0) {
262 fprintf(stderr, "usage: make_hash column root_name bigstring\n");
267 * Read the table into our arrays.
269 for (n = 0; (n < CAPTABSIZE) && fgets(buffer, BUFSIZ, stdin);) {
270 char **list, *nlp = strchr(buffer, '\n');
273 list = parse_columns(buffer);
274 if (list == 0) /* blank or comment */
276 name_table[n].nte_link = -1; /* end-of-hash */
277 name_table[n].nte_name = strdup(list[column]);
278 if (!strcmp(list[2], "bool")) {
279 name_table[n].nte_type = BOOLEAN;
280 name_table[n].nte_index = BoolCount++;
281 } else if (!strcmp(list[2], "num")) {
282 name_table[n].nte_type = NUMBER;
283 name_table[n].nte_index = NumCount++;
284 } else if (!strcmp(list[2], "str")) {
285 name_table[n].nte_type = STRING;
286 name_table[n].nte_index = StrCount++;
288 fprintf(stderr, "Unknown type: %s\n", list[2]);
293 _nc_make_hash_table(name_table, hash_table);
296 * Write the compiled tables to standard output
302 printf("static const char %s_names_text[] = \\\n", root_name);
303 for (n = 0; n < CAPTABSIZE; n++) {
304 nxt = strlen(name_table[n].nte_name) + 5;
305 if (nxt + len > 72) {
309 printf("\"%s\\0\" ", name_table[n].nte_name);
315 printf("static name_table_data const %s_names_data[] =\n",
318 for (n = 0; n < CAPTABSIZE; n++) {
319 printf("\t{ %15d,\t%10s,\t%3d, %3d }%c\n",
321 typenames[name_table[n].nte_type],
322 name_table[n].nte_index,
323 name_table[n].nte_link,
324 n < CAPTABSIZE - 1 ? ',' : ' ');
325 len += strlen(name_table[n].nte_name) + 1;
328 printf("static struct name_table_entry *_nc_%s_table = 0;\n\n", root_name);
331 printf("static struct name_table_entry %s _nc_%s_table[] =\n",
332 bigstring ? "" : "const",
335 for (n = 0; n < CAPTABSIZE; n++) {
336 sprintf(buffer, "\"%s\"",
337 name_table[n].nte_name);
338 printf("\t{ %15s,\t%10s,\t%3d, %3d }%c\n",
340 typenames[name_table[n].nte_type],
341 name_table[n].nte_index,
342 name_table[n].nte_link,
343 n < CAPTABSIZE - 1 ? ',' : ' ');
348 printf("static const short _nc_%s_hash_table[%d] =\n",
352 for (n = 0; n < HASHTABSIZE; n++) {
353 printf("\t%3d,\n", hash_table[n]);
355 printf("\t0\t/* base-of-table */\n");
358 printf("#if (BOOLCOUNT!=%d)||(NUMCOUNT!=%d)||(STRCOUNT!=%d)\n",
359 BoolCount, NumCount, StrCount);
360 printf("#error\t--> term.h and comp_captab.c disagree about the <--\n");
361 printf("#error\t--> numbers of booleans, numbers and/or strings <--\n");
362 printf("#endif\n\n");