]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/tinfo/comp_hash.c
ncurses 5.7 - patch 20090711
[ncurses.git] / ncurses / tinfo / comp_hash.c
1 /****************************************************************************
2  * Copyright (c) 1998-2008,2009 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.39 2009/07/11 18:27:26 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     short 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
135 #define SameName(a,b,termcap) (termcap ? !strncmp(a,b,2) : !strcmp(a,b))
136 #if 0
137 static bool
138 same_name(const char *a, const char *b, bool termcap)
139 {
140     fprintf(stderr, "compare(%s,%s)\n", a, b);
141     return SameName(a, b, termcap);
142 }
143 #else
144 #define same_name(a,b,termcap) SameName(a,b,termcap)
145 #endif
146
147 NCURSES_EXPORT(struct name_table_entry const *)
148 _nc_find_entry(const char *string,
149                const short *hash_table)
150 {
151     int hashvalue;
152     struct name_table_entry const *ptr = 0;
153     struct name_table_entry const *real_table;
154
155     hashvalue = hash_function(string);
156
157     if (hash_table[hashvalue] >= 0) {
158         bool termcap = (hash_table != _nc_get_hash_table(FALSE));
159
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)
164                 return 0;
165             ptr = real_table + (ptr->nte_link + hash_table[HASHTABSIZE]);
166         }
167     }
168
169     return (ptr);
170 }
171
172 /*
173  *      struct name_table_entry *
174  *      find_type_entry(string, type, table)
175  *
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.
181  */
182
183 NCURSES_EXPORT(struct name_table_entry const *)
184 _nc_find_type_entry(const char *string,
185                     int type,
186                     bool termcap)
187 {
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;
191
192     for (ptr = table; ptr < table + CAPTABSIZE; ptr++) {
193         if (ptr->nte_type == type) {
194             if (same_name(ptr->nte_name, string, termcap)) {
195                 result = ptr;
196             }
197         }
198     }
199
200     return result;
201 }
202 #endif
203
204 #ifdef MAIN_PROGRAM
205 /*
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.
209  *
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).
213  */
214
215 #define MAX_COLUMNS BUFSIZ      /* this _has_ to be worst-case */
216
217 static char **
218 parse_columns(char *buffer)
219 {
220     static char **list;
221
222     int col = 0;
223
224     if (list == 0 && (list = typeCalloc(char *, MAX_COLUMNS)) == 0)
225           return (0);
226
227     if (*buffer != '#') {
228         while (*buffer != '\0') {
229             char *s;
230             for (s = buffer; (*s != '\0') && !isspace(UChar(*s)); s++)
231                 /*EMPTY */ ;
232             if (s != buffer) {
233                 char mark = *s;
234                 *s = '\0';
235                 if ((s - buffer) > 1
236                     && (*buffer == '"')
237                     && (s[-1] == '"')) {        /* strip the quotes */
238                     assert(s > buffer + 1);
239                     s[-1] = '\0';
240                     buffer++;
241                 }
242                 list[col] = buffer;
243                 col++;
244                 if (mark == '\0')
245                     break;
246                 while (*++s && isspace(UChar(*s)))
247                     /*EMPTY */ ;
248                 buffer = s;
249             } else
250                 break;
251         }
252     }
253     return col ? list : 0;
254 }
255
256 int
257 main(int argc, char **argv)
258 {
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 = "";
263     int column = 0;
264     int bigstring = 0;
265     int n;
266     char buffer[BUFSIZ];
267
268     static const char *typenames[] =
269     {"BOOLEAN", "NUMBER", "STRING"};
270
271     short BoolCount = 0;
272     short NumCount = 0;
273     short StrCount = 0;
274
275     /* The first argument is the column-number (starting with 0).
276      * The second is the root name of the tables to generate.
277      */
278     if (argc <= 3
279         || (column = atoi(argv[1])) <= 0
280         || (column >= MAX_COLUMNS)
281         || *(root_name = argv[2]) == 0
282         || (bigstring = atoi(argv[3])) < 0
283         || name_table == 0
284         || hash_table == 0) {
285         fprintf(stderr, "usage: make_hash column root_name bigstring\n");
286         exit(EXIT_FAILURE);
287     }
288
289     /*
290      * Read the table into our arrays.
291      */
292     for (n = 0; (n < CAPTABSIZE) && fgets(buffer, BUFSIZ, stdin);) {
293         char **list, *nlp = strchr(buffer, '\n');
294         if (nlp)
295             *nlp = '\0';
296         list = parse_columns(buffer);
297         if (list == 0)          /* blank or comment */
298             continue;
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++;
310         } else {
311             fprintf(stderr, "Unknown type: %s\n", list[2]);
312             exit(EXIT_FAILURE);
313         }
314         n++;
315     }
316     _nc_make_hash_table(name_table, hash_table);
317
318     /*
319      * Write the compiled tables to standard output
320      */
321     if (bigstring) {
322         int len = 0;
323         int nxt;
324
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) {
329                 printf("\\\n");
330                 len = 0;
331             }
332             printf("\"%s\\0\" ", name_table[n].nte_name);
333             len += nxt;
334         }
335         printf(";\n\n");
336
337         len = 0;
338         printf("static name_table_data const %s_names_data[] =\n",
339                root_name);
340         printf("{\n");
341         for (n = 0; n < CAPTABSIZE; n++) {
342             printf("\t{ %15d,\t%10s,\t%3d, %3d }%c\n",
343                    len,
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;
349         }
350         printf("};\n\n");
351         printf("static struct name_table_entry *_nc_%s_table = 0;\n\n", root_name);
352     } else {
353
354         printf("static struct name_table_entry %s _nc_%s_table[] =\n",
355                bigstring ? "" : "const",
356                root_name);
357         printf("{\n");
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",
362                    buffer,
363                    typenames[name_table[n].nte_type],
364                    name_table[n].nte_index,
365                    name_table[n].nte_link,
366                    n < CAPTABSIZE - 1 ? ',' : ' ');
367         }
368         printf("};\n\n");
369     }
370
371     printf("static const short _nc_%s_hash_table[%d] =\n",
372            root_name,
373            HASHTABSIZE + 1);
374     printf("{\n");
375     for (n = 0; n < HASHTABSIZE; n++) {
376         printf("\t%3d,\n", hash_table[n]);
377     }
378     printf("\t0\t/* base-of-table */\n");
379     printf("};\n\n");
380
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");
386
387     free(hash_table);
388     return EXIT_SUCCESS;
389 }
390 #endif