]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/tinfo/comp_hash.c
94d3d5c635515b39672ff5fd1926a2064e2bac2b
[ncurses.git] / ncurses / tinfo / comp_hash.c
1 /****************************************************************************
2  * Copyright (c) 1998-2003,2005 Free Software Foundation, Inc.              *
3  *                                                                          *
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:                 *
11  *                                                                          *
12  * The above copyright notice and this permission notice shall be included  *
13  * in all copies or substantial portions of the Software.                   *
14  *                                                                          *
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.                               *
22  *                                                                          *
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       *
26  * authorization.                                                           *
27  ****************************************************************************/
28
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  ****************************************************************************/
34
35 /*
36  *      comp_hash.c --- Routines to deal with the hashtable of capability
37  *                      names.
38  *
39  */
40
41 #define USE_TERMLIB 1
42 #include <curses.priv.h>
43
44 #include <tic.h>
45 #include <hashsize.h>
46
47 #ifdef MAIN_PROGRAM
48 #include <ctype.h>
49 #undef  DEBUG
50 #define DEBUG(level, params)    /*nothing */
51 #endif
52
53 MODULE_ID("$Id: comp_hash.c,v 1.29 2007/07/28 19:44:40 tom Exp $")
54
55 static int hash_function(const char *);
56
57 /*
58  *      _nc_make_hash_table()
59  *
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[].
63  *
64  */
65
66 #ifdef MAIN_PROGRAM
67
68 #undef MODULE_ID
69 #define MODULE_ID(id)           /*nothing */
70 #include <tinfo/doalloc.c>
71
72 static void
73 _nc_make_hash_table(struct name_table_entry *table,
74                     short *hash_table)
75 {
76     int i;
77     int hashvalue;
78     int collisions = 0;
79
80     for (i = 0; i < HASHTABSIZE; i++) {
81         hash_table[i] = -1;
82     }
83     for (i = 0; i < CAPTABSIZE; i++) {
84         hashvalue = hash_function(table[i].nte_name);
85
86         if (hash_table[hashvalue] >= 0)
87             collisions++;
88
89         if (hash_table[hashvalue] != 0)
90             table[i].nte_link = hash_table[hashvalue];
91         hash_table[hashvalue] = i;
92     }
93
94     DEBUG(4, ("Hash table complete: %d collisions out of %d entries",
95               collisions, CAPTABSIZE));
96 }
97 #endif
98
99 /*
100  *      int hash_function(string)
101  *
102  *      Computes the hashing function on the given string.
103  *
104  *      The current hash function is the sum of each consectutive pair
105  *      of characters, taken as two-byte integers, mod HASHTABSIZE.
106  *
107  */
108
109 static int
110 hash_function(const char *string)
111 {
112     long sum = 0;
113
114     DEBUG(9, ("hashing %s", string));
115     while (*string) {
116         sum += (long) (*string + (*(string + 1) << 8));
117         string++;
118     }
119
120     DEBUG(9, ("sum is %ld", sum));
121     return (int) (sum % HASHTABSIZE);
122 }
123
124 /*
125  *      struct name_table_entry *
126  *      find_entry(string)
127  *
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.
130  *
131  */
132
133 #ifndef MAIN_PROGRAM
134 NCURSES_EXPORT(struct name_table_entry const *)
135 _nc_find_entry(const char *string,
136                const short *hash_table)
137 {
138     int hashvalue;
139     struct name_table_entry const *ptr = 0;
140     struct name_table_entry const *real_table;
141
142     hashvalue = hash_function(string);
143
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)
149                 return 0;
150             ptr = real_table + (ptr->nte_link + hash_table[HASHTABSIZE]);
151         }
152     }
153
154     return (ptr);
155 }
156
157 /*
158  *      struct name_table_entry *
159  *      find_type_entry(string, type, table)
160  *
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.
166  */
167
168 NCURSES_EXPORT(struct name_table_entry const *)
169 _nc_find_type_entry(const char *string,
170                     int type,
171                     const struct name_table_entry *table)
172 {
173     struct name_table_entry const *ptr;
174
175     for (ptr = table; ptr < table + CAPTABSIZE; ptr++) {
176         if (ptr->nte_type == type && strcmp(string, ptr->nte_name) == 0)
177             return (ptr);
178     }
179
180     return ((struct name_table_entry *) NULL);
181 }
182 #endif
183
184 #ifdef MAIN_PROGRAM
185 /*
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.
189  *
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).
193  */
194
195 #define MAX_COLUMNS BUFSIZ      /* this _has_ to be worst-case */
196
197 static char **
198 parse_columns(char *buffer)
199 {
200     static char **list;
201
202     int col = 0;
203
204     if (list == 0 && (list = typeCalloc(char *, MAX_COLUMNS)) == 0)
205           return (0);
206
207     if (*buffer != '#') {
208         while (*buffer != '\0') {
209             char *s;
210             for (s = buffer; (*s != '\0') && !isspace(UChar(*s)); s++)
211                 /*EMPTY */ ;
212             if (s != buffer) {
213                 char mark = *s;
214                 *s = '\0';
215                 if ((s - buffer) > 1
216                     && (*buffer == '"')
217                     && (s[-1] == '"')) {        /* strip the quotes */
218                     buffer++;
219                     s[-1] = '\0';
220                 }
221                 list[col] = buffer;
222                 col++;
223                 if (mark == '\0')
224                     break;
225                 while (*++s && isspace(UChar(*s)))
226                     /*EMPTY */ ;
227                 buffer = s;
228             } else
229                 break;
230         }
231     }
232     return col ? list : 0;
233 }
234
235 int
236 main(int argc, char **argv)
237 {
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 = "";
242     int column = 0;
243     int n;
244     char buffer[BUFSIZ];
245
246     static const char *typenames[] =
247     {"BOOLEAN", "NUMBER", "STRING"};
248
249     short BoolCount = 0;
250     short NumCount = 0;
251     short StrCount = 0;
252
253     /* The first argument is the column-number (starting with 0).
254      * The second is the root name of the tables to generate.
255      */
256     if (argc <= 2
257         || (column = atoi(argv[1])) <= 0
258         || (column >= MAX_COLUMNS)
259         || *(root_name = argv[2]) == 0) {
260         fprintf(stderr, "usage: make_hash column root_name\n");
261         exit(EXIT_FAILURE);
262     }
263
264     /*
265      * Read the table into our arrays.
266      */
267     for (n = 0; (n < CAPTABSIZE) && fgets(buffer, BUFSIZ, stdin);) {
268         char **list, *nlp = strchr(buffer, '\n');
269         if (nlp)
270             *nlp = '\0';
271         list = parse_columns(buffer);
272         if (list == 0)          /* blank or comment */
273             continue;
274         name_table[n].nte_link = -1;    /* end-of-hash */
275         name_table[n].nte_name = strdup(list[column]);
276         if (!strcmp(list[2], "bool")) {
277             name_table[n].nte_type = BOOLEAN;
278             name_table[n].nte_index = BoolCount++;
279         } else if (!strcmp(list[2], "num")) {
280             name_table[n].nte_type = NUMBER;
281             name_table[n].nte_index = NumCount++;
282         } else if (!strcmp(list[2], "str")) {
283             name_table[n].nte_type = STRING;
284             name_table[n].nte_index = StrCount++;
285         } else {
286             fprintf(stderr, "Unknown type: %s\n", list[2]);
287             exit(EXIT_FAILURE);
288         }
289         n++;
290     }
291     _nc_make_hash_table(name_table, hash_table);
292
293     /*
294      * Write the compiled tables to standard output
295      */
296     printf("static struct name_table_entry const _nc_%s_table[] =\n",
297            root_name);
298     printf("{\n");
299     for (n = 0; n < CAPTABSIZE; n++) {
300         sprintf(buffer, "\"%s\"",
301                 name_table[n].nte_name);
302         printf("\t{ %15s,\t%10s,\t%3d, %3d }%c\n",
303                buffer,
304                typenames[name_table[n].nte_type],
305                name_table[n].nte_index,
306                name_table[n].nte_link,
307                n < CAPTABSIZE - 1 ? ',' : ' ');
308     }
309     printf("};\n\n");
310
311     printf("const short _nc_%s_hash_table[%d] =\n",
312            root_name,
313            HASHTABSIZE + 1);
314     printf("{\n");
315     for (n = 0; n < HASHTABSIZE; n++) {
316         printf("\t%3d,\n", hash_table[n]);
317     }
318     printf("\t0\t/* base-of-table */\n");
319     printf("};\n\n");
320
321     printf("#if (BOOLCOUNT!=%d)||(NUMCOUNT!=%d)||(STRCOUNT!=%d)\n",
322            BoolCount, NumCount, StrCount);
323     printf("#error\t--> term.h and comp_captab.c disagree about the <--\n");
324     printf("#error\t--> numbers of booleans, numbers and/or strings <--\n");
325     printf("#endif\n\n");
326
327     return EXIT_SUCCESS;
328 }
329 #endif