1 /****************************************************************************
2 * Copyright (c) 1998-2008,2009 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.39 2009/07/11 18:27:26 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.
135 #define SameName(a,b,termcap) (termcap ? !strncmp(a,b,2) : !strcmp(a,b))
138 same_name(const char *a, const char *b, bool termcap)
140 fprintf(stderr, "compare(%s,%s)\n", a, b);
141 return SameName(a, b, termcap);
144 #define same_name(a,b,termcap) SameName(a,b,termcap)
147 NCURSES_EXPORT(struct name_table_entry const *)
148 _nc_find_entry(const char *string,
149 const short *hash_table)
152 struct name_table_entry const *ptr = 0;
153 struct name_table_entry const *real_table;
155 hashvalue = hash_function(string);
157 if (hash_table[hashvalue] >= 0) {
158 bool termcap = (hash_table != _nc_get_hash_table(FALSE));
160 real_table = _nc_get_table(termcap);
161 ptr = real_table + hash_table[hashvalue];
162 while (!same_name(ptr->nte_name, string, termcap)) {
163 if (ptr->nte_link < 0)
165 ptr = real_table + (ptr->nte_link + hash_table[HASHTABSIZE]);
173 * struct name_table_entry *
174 * find_type_entry(string, type, table)
176 * Finds the first entry for the given name with the given type in the
177 * given table if present (as distinct from find_entry, which finds the
178 * the last entry regardless of type). You can use this if you detect
179 * a name clash. It's slower, though. Returns a pointer to the entry
180 * in the table or 0 if not found.
183 NCURSES_EXPORT(struct name_table_entry const *)
184 _nc_find_type_entry(const char *string,
188 struct name_table_entry const *result = NULL;
189 const struct name_table_entry *const table = _nc_get_table(termcap);
190 struct name_table_entry const *ptr;
192 for (ptr = table; ptr < table + CAPTABSIZE; ptr++) {
193 if (ptr->nte_type == type) {
194 if (same_name(ptr->nte_name, string, termcap)) {
206 * This filter reads from standard input a list of tab-delimited columns,
207 * (e.g., from Caps.filtered) computes the hash-value of a specified column and
208 * writes the hashed tables to standard output.
210 * By compiling the hash table at build time, we're able to make the entire
211 * set of terminfo and termcap tables readonly (and also provide some runtime
212 * performance enhancement).
215 #define MAX_COLUMNS BUFSIZ /* this _has_ to be worst-case */
218 parse_columns(char *buffer)
224 if (list == 0 && (list = typeCalloc(char *, MAX_COLUMNS)) == 0)
227 if (*buffer != '#') {
228 while (*buffer != '\0') {
230 for (s = buffer; (*s != '\0') && !isspace(UChar(*s)); s++)
237 && (s[-1] == '"')) { /* strip the quotes */
238 assert(s > buffer + 1);
246 while (*++s && isspace(UChar(*s)))
253 return col ? list : 0;
257 main(int argc, char **argv)
259 struct name_table_entry *name_table = typeCalloc(struct
260 name_table_entry, CAPTABSIZE);
261 short *hash_table = typeCalloc(short, HASHTABSIZE);
262 const char *root_name = "";
268 static const char *typenames[] =
269 {"BOOLEAN", "NUMBER", "STRING"};
275 /* The first argument is the column-number (starting with 0).
276 * The second is the root name of the tables to generate.
279 || (column = atoi(argv[1])) <= 0
280 || (column >= MAX_COLUMNS)
281 || *(root_name = argv[2]) == 0
282 || (bigstring = atoi(argv[3])) < 0
284 || hash_table == 0) {
285 fprintf(stderr, "usage: make_hash column root_name bigstring\n");
290 * Read the table into our arrays.
292 for (n = 0; (n < CAPTABSIZE) && fgets(buffer, BUFSIZ, stdin);) {
293 char **list, *nlp = strchr(buffer, '\n');
296 list = parse_columns(buffer);
297 if (list == 0) /* blank or comment */
299 name_table[n].nte_link = -1; /* end-of-hash */
300 name_table[n].nte_name = strdup(list[column]);
301 if (!strcmp(list[2], "bool")) {
302 name_table[n].nte_type = BOOLEAN;
303 name_table[n].nte_index = BoolCount++;
304 } else if (!strcmp(list[2], "num")) {
305 name_table[n].nte_type = NUMBER;
306 name_table[n].nte_index = NumCount++;
307 } else if (!strcmp(list[2], "str")) {
308 name_table[n].nte_type = STRING;
309 name_table[n].nte_index = StrCount++;
311 fprintf(stderr, "Unknown type: %s\n", list[2]);
316 _nc_make_hash_table(name_table, hash_table);
319 * Write the compiled tables to standard output
325 printf("static const char %s_names_text[] = \\\n", root_name);
326 for (n = 0; n < CAPTABSIZE; n++) {
327 nxt = (int) strlen(name_table[n].nte_name) + 5;
328 if (nxt + len > 72) {
332 printf("\"%s\\0\" ", name_table[n].nte_name);
338 printf("static name_table_data const %s_names_data[] =\n",
341 for (n = 0; n < CAPTABSIZE; n++) {
342 printf("\t{ %15d,\t%10s,\t%3d, %3d }%c\n",
344 typenames[name_table[n].nte_type],
345 name_table[n].nte_index,
346 name_table[n].nte_link,
347 n < CAPTABSIZE - 1 ? ',' : ' ');
348 len += (int) strlen(name_table[n].nte_name) + 1;
351 printf("static struct name_table_entry *_nc_%s_table = 0;\n\n", root_name);
354 printf("static struct name_table_entry %s _nc_%s_table[] =\n",
355 bigstring ? "" : "const",
358 for (n = 0; n < CAPTABSIZE; n++) {
359 sprintf(buffer, "\"%s\"",
360 name_table[n].nte_name);
361 printf("\t{ %15s,\t%10s,\t%3d, %3d }%c\n",
363 typenames[name_table[n].nte_type],
364 name_table[n].nte_index,
365 name_table[n].nte_link,
366 n < CAPTABSIZE - 1 ? ',' : ' ');
371 printf("static const short _nc_%s_hash_table[%d] =\n",
375 for (n = 0; n < HASHTABSIZE; n++) {
376 printf("\t%3d,\n", hash_table[n]);
378 printf("\t0\t/* base-of-table */\n");
381 printf("#if (BOOLCOUNT!=%d)||(NUMCOUNT!=%d)||(STRCOUNT!=%d)\n",
382 BoolCount, NumCount, StrCount);
383 printf("#error\t--> term.h and comp_captab.c disagree about the <--\n");
384 printf("#error\t--> numbers of booleans, numbers and/or strings <--\n");
385 printf("#endif\n\n");