2 /***************************************************************************
4 ****************************************************************************
5 * ncurses is copyright (C) 1992-1995 *
7 * zmbenhal@netcom.com *
9 * esr@snark.thyrsus.com *
11 * Permission is hereby granted to reproduce and distribute ncurses *
12 * by any means and for any fee, whether alone or as part of a *
13 * larger distribution, in source or in binary form, PROVIDED *
14 * this notice is included with any such distribution, and is not *
15 * removed from any of its header files. Mention of ncurses in any *
16 * applications linked with it is highly appreciated. *
18 * ncurses comes AS IS with no warranty, implied or expressed. *
20 ***************************************************************************/
24 * comp_hash.c --- Routines to deal with the hashtable of capability
29 #include <curses.priv.h>
37 #define DEBUG(level, params) /*nothing*/
40 MODULE_ID("$Id: comp_hash.c,v 1.15 1997/04/26 18:16:19 tom Exp $")
42 static int hash_function(const char *);
45 * _nc_make_hash_table()
47 * Takes the entries in table[] and hashes them into hash_table[]
48 * by name. There are CAPTABSIZE entries in table[] and HASHTABSIZE
49 * slots in hash_table[].
54 static void _nc_make_hash_table(struct name_table_entry *table,
55 struct name_table_entry **hash_table)
61 for (i = 0; i < CAPTABSIZE; i++) {
62 hashvalue = hash_function(table[i].nte_name);
64 if (hash_table[hashvalue] != (struct name_table_entry *) 0)
67 if (hash_table[hashvalue] != 0)
68 table[i].nte_link = (short)(hash_table[hashvalue] - table);
69 hash_table[hashvalue] = &table[i];
72 DEBUG(4, ("Hash table complete: %d collisions out of %d entries", collisions, CAPTABSIZE));
78 * int hash_function(string)
80 * Computes the hashing function on the given string.
82 * The current hash function is the sum of each consectutive pair
83 * of characters, taken as two-byte integers, mod Hashtabsize.
89 hash_function(const char *string)
93 DEBUG(9, ("hashing %s", string));
95 sum += (long)(*string + (*(string + 1) << 8));
99 DEBUG(9, ("sum is %ld", sum));
100 return (int)(sum % HASHTABSIZE);
105 * struct name_table_entry *
108 * Finds the entry for the given string in the hash table if present.
109 * Returns a pointer to the entry in the table or 0 if not found.
114 struct name_table_entry const *
115 _nc_find_entry(const char *string, const struct name_table_entry *const *hash_table)
118 struct name_table_entry const *ptr;
120 hashvalue = hash_function(string);
122 if ((ptr = hash_table[hashvalue]) != 0) {
123 while (strcmp(ptr->nte_name, string) != 0) {
124 if (ptr->nte_link < 0)
126 ptr = ptr->nte_link + hash_table[HASHTABSIZE];
134 * struct name_table_entry *
135 * find_type_entry(string, type, table)
137 * Finds the first entry for the given name with the given type in the
138 * given table if present (as distinct from find_entry, which finds the
139 * the last entry regardless of type). You can use this if you detect
140 * a name clash. It's slower, though. Returns a pointer to the entry
141 * in the table or 0 if not found.
144 struct name_table_entry const *
145 _nc_find_type_entry(const char *string,
147 const struct name_table_entry *table)
149 struct name_table_entry const *ptr;
151 for (ptr = table; ptr < table + CAPTABSIZE; ptr++) {
152 if (ptr->nte_type == type && strcmp(string, ptr->nte_name) == 0)
156 return ((struct name_table_entry *)NULL);
162 * This filter reads from standard input a list of tab-delimited columns,
163 * (e.g., from Caps.filtered) computes the hash-value of a specified column and
164 * writes the hashed tables to standard output.
166 * By compiling the hash table at build time, we're able to make the entire
167 * set of terminfo and termcap tables readonly (and also provide some runtime
168 * performance enhancement).
172 static char *strdup (char *s)
176 p = malloc(strlen(s)+1);
181 #endif /* not HAVE_STRDUP */
183 #define MAX_COLUMNS BUFSIZ /* this _has_ to be worst-case */
185 static char **parse_columns(char *buffer)
192 list = typeCalloc(char *, MAX_COLUMNS);
194 if (*buffer != '#') {
195 while (*buffer != '\0') {
197 for (s = buffer; (*s != '\0') && !isspace(*s); s++)
204 && (s[-1] == '"')) { /* strip the quotes */
212 while (*++s && isspace(*s))
219 return col ? list : 0;
222 int main(int argc, char **argv)
224 struct name_table_entry *name_table = typeCalloc(struct name_table_entry, CAPTABSIZE);
225 struct name_table_entry **hash_table = typeCalloc(struct name_table_entry *, HASHTABSIZE);
226 const char *root_name = "";
231 static const char * typenames[] = { "BOOLEAN", "NUMBER", "STRING" };
237 /* The first argument is the column-number (starting with 0).
238 * The second is the root name of the tables to generate.
241 || (column = atoi(argv[1])) <= 0
242 || (column >= MAX_COLUMNS)
243 || *(root_name = argv[2]) == 0) {
244 fprintf(stderr, "usage: make_hash column root_name\n");
249 * Read the table into our arrays.
251 for (n = 0; (n < CAPTABSIZE) && fgets(buffer, BUFSIZ, stdin); ) {
252 char **list, *nlp = strchr(buffer, '\n');
255 list = parse_columns(buffer);
256 if (list == 0) /* blank or comment */
258 name_table[n].nte_link = -1; /* end-of-hash */
259 name_table[n].nte_name = strdup(list[column]);
260 if (!strcmp(list[2], "bool")) {
261 name_table[n].nte_type = BOOLEAN;
262 name_table[n].nte_index = BoolCount++;
263 } else if (!strcmp(list[2], "num")) {
264 name_table[n].nte_type = NUMBER;
265 name_table[n].nte_index = NumCount++;
266 } else if (!strcmp(list[2], "str")) {
267 name_table[n].nte_type = STRING;
268 name_table[n].nte_index = StrCount++;
270 fprintf(stderr, "Unknown type: %s\n", list[2]);
275 _nc_make_hash_table(name_table, hash_table);
278 * Write the compiled tables to standard output
280 printf("static struct name_table_entry const _nc_%s_table[] =\n",
283 for (n = 0; n < CAPTABSIZE; n++) {
284 sprintf(buffer, "\"%s\"",
285 name_table[n].nte_name);
286 printf("\t{ %15s,\t%10s,\t%3d, %3d }%c\n",
288 typenames[name_table[n].nte_type],
289 name_table[n].nte_index,
290 name_table[n].nte_link,
291 n < CAPTABSIZE - 1 ? ',' : ' ');
295 printf("const struct name_table_entry * const _nc_%s_hash_table[%d] =\n",
299 for (n = 0; n < HASHTABSIZE; n++) {
300 if (hash_table[n] != 0) {
301 sprintf(buffer, "_nc_%s_table + %3d",
303 hash_table[n] - name_table);
307 printf("\t%s,\n", buffer);
309 printf("\t_nc_%s_table\t/* base-of-table */\n", root_name);
312 printf("#if (BOOLCOUNT!=%d)||(NUMCOUNT!=%d)||(STRCOUNT!=%d)\n",
313 BoolCount, NumCount, StrCount);
314 printf("#error\t--> term.h and comp_captab.c disagree about the <--\n");
315 printf("#error\t--> numbers of booleans, numbers and/or strings <--\n");
316 printf("#endif\n\n");