]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/tinfo/comp_hash.c
ncurses 5.7 - patch 20090801
[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.45 2009/07/18 20:30:21 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     printf("/* %d collisions out of %d entries */\n", collisions, CAPTABSIZE);
95 }
96 #endif
97
98 /*
99  *      int hash_function(string)
100  *
101  *      Computes the hashing function on the given string.
102  *
103  *      The current hash function is the sum of each consectutive pair
104  *      of characters, taken as two-byte integers, mod HASHTABSIZE.
105  *
106  */
107
108 static int
109 hash_function(const char *string)
110 {
111     long sum = 0;
112
113     DEBUG(9, ("hashing %s", string));
114     while (*string) {
115         sum += (long) (*string + (*(string + 1) << 8));
116         string++;
117     }
118
119     DEBUG(9, ("sum is %ld", sum));
120     return (int) (sum % HASHTABSIZE);
121 }
122
123 #ifndef MAIN_PROGRAM
124
125 #define SameName(a,b,termcap) (termcap ? !strncmp(a,b,2) : !strcmp(a,b))
126 #if 0
127 static bool
128 same_name(const char *a, const char *b, bool termcap)
129 {
130     fprintf(stderr, "compare(%s,%s)\n", a, b);
131     return SameName(a, b, termcap);
132 }
133 #else
134 #define same_name(a,b,termcap) SameName(a,b,termcap)
135 #endif
136
137 /*
138  * Finds the entry for the given string in the hash table if present.
139  * Returns a pointer to the entry in the table or 0 if not found.
140  */
141 NCURSES_EXPORT(struct name_table_entry const *)
142 _nc_find_entry(const char *string,
143                const short *hash_table)
144 {
145     int hashvalue;
146     struct name_table_entry const *ptr = 0;
147     struct name_table_entry const *real_table;
148
149     hashvalue = hash_function(string);
150
151     if (hash_table[hashvalue] >= 0) {
152         bool termcap = (hash_table != _nc_get_hash_table(FALSE));
153
154         real_table = _nc_get_table(termcap);
155         ptr = real_table + hash_table[hashvalue];
156         while (!same_name(ptr->nte_name, string, termcap)) {
157             if (ptr->nte_link < 0) {
158                 ptr = 0;
159                 break;
160             }
161             ptr = real_table + (ptr->nte_link + hash_table[HASHTABSIZE]);
162         }
163     }
164
165     return (ptr);
166 }
167
168 /*
169  * Finds the entry for the given name with the given type in the given table if
170  * present (as distinct from _nc_find_entry, which finds the last entry
171  * regardless of type).
172  *
173  * Returns a pointer to the entry in the table or 0 if not found.
174  */
175 NCURSES_EXPORT(struct name_table_entry const *)
176 _nc_find_type_entry(const char *string,
177                     int type,
178                     bool termcap)
179 {
180     struct name_table_entry const *ptr = NULL;
181     const short *hash_table = _nc_get_hash_table(termcap);
182     int hashvalue = hash_function(string);
183
184     if (hash_table[hashvalue] >= 0) {
185         const struct name_table_entry *const table = _nc_get_table(termcap);
186
187         ptr = table + hash_table[hashvalue];
188         while (ptr->nte_type != type
189                || !same_name(ptr->nte_name, string, termcap)) {
190             if (ptr->nte_link < 0) {
191                 ptr = 0;
192                 break;
193             }
194             ptr = table + (ptr->nte_link + hash_table[HASHTABSIZE]);
195         }
196     }
197
198     return ptr;
199 }
200 #endif
201
202 #ifdef MAIN_PROGRAM
203 /*
204  * This filter reads from standard input a list of tab-delimited columns,
205  * (e.g., from Caps.filtered) computes the hash-value of a specified column and
206  * writes the hashed tables to standard output.
207  *
208  * By compiling the hash table at build time, we're able to make the entire
209  * set of terminfo and termcap tables readonly (and also provide some runtime
210  * performance enhancement).
211  */
212
213 #define MAX_COLUMNS BUFSIZ      /* this _has_ to be worst-case */
214
215 static char **
216 parse_columns(char *buffer)
217 {
218     static char **list;
219
220     int col = 0;
221
222     if (list == 0 && (list = typeCalloc(char *, MAX_COLUMNS)) == 0)
223           return (0);
224
225     if (*buffer != '#') {
226         while (*buffer != '\0') {
227             char *s;
228             for (s = buffer; (*s != '\0') && !isspace(UChar(*s)); s++)
229                 /*EMPTY */ ;
230             if (s != buffer) {
231                 char mark = *s;
232                 *s = '\0';
233                 if ((s - buffer) > 1
234                     && (*buffer == '"')
235                     && (s[-1] == '"')) {        /* strip the quotes */
236                     assert(s > buffer + 1);
237                     s[-1] = '\0';
238                     buffer++;
239                 }
240                 list[col] = buffer;
241                 col++;
242                 if (mark == '\0')
243                     break;
244                 while (*++s && isspace(UChar(*s)))
245                     /*EMPTY */ ;
246                 buffer = s;
247             } else
248                 break;
249         }
250     }
251     return col ? list : 0;
252 }
253
254 int
255 main(int argc, char **argv)
256 {
257     struct name_table_entry *name_table = typeCalloc(struct
258                                                      name_table_entry, CAPTABSIZE);
259     short *hash_table = typeCalloc(short, HASHTABSIZE);
260     const char *root_name = "";
261     int column = 0;
262     int bigstring = 0;
263     int n;
264     char buffer[BUFSIZ];
265
266     static const char *typenames[] =
267     {"BOOLEAN", "NUMBER", "STRING"};
268
269     short BoolCount = 0;
270     short NumCount = 0;
271     short StrCount = 0;
272
273     /* The first argument is the column-number (starting with 0).
274      * The second is the root name of the tables to generate.
275      */
276     if (argc <= 3
277         || (column = atoi(argv[1])) <= 0
278         || (column >= MAX_COLUMNS)
279         || *(root_name = argv[2]) == 0
280         || (bigstring = atoi(argv[3])) < 0
281         || name_table == 0
282         || hash_table == 0) {
283         fprintf(stderr, "usage: make_hash column root_name bigstring\n");
284         exit(EXIT_FAILURE);
285     }
286
287     /*
288      * Read the table into our arrays.
289      */
290     for (n = 0; (n < CAPTABSIZE) && fgets(buffer, BUFSIZ, stdin);) {
291         char **list, *nlp = strchr(buffer, '\n');
292         if (nlp)
293             *nlp = '\0';
294         list = parse_columns(buffer);
295         if (list == 0)          /* blank or comment */
296             continue;
297         name_table[n].nte_link = -1;    /* end-of-hash */
298         name_table[n].nte_name = strdup(list[column]);
299         if (!strcmp(list[2], "bool")) {
300             name_table[n].nte_type = BOOLEAN;
301             name_table[n].nte_index = BoolCount++;
302         } else if (!strcmp(list[2], "num")) {
303             name_table[n].nte_type = NUMBER;
304             name_table[n].nte_index = NumCount++;
305         } else if (!strcmp(list[2], "str")) {
306             name_table[n].nte_type = STRING;
307             name_table[n].nte_index = StrCount++;
308         } else {
309             fprintf(stderr, "Unknown type: %s\n", list[2]);
310             exit(EXIT_FAILURE);
311         }
312         n++;
313     }
314     _nc_make_hash_table(name_table, hash_table);
315
316     /*
317      * Write the compiled tables to standard output
318      */
319     if (bigstring) {
320         int len = 0;
321         int nxt;
322
323         printf("static const char %s_names_text[] = \\\n", root_name);
324         for (n = 0; n < CAPTABSIZE; n++) {
325             nxt = (int) strlen(name_table[n].nte_name) + 5;
326             if (nxt + len > 72) {
327                 printf("\\\n");
328                 len = 0;
329             }
330             printf("\"%s\\0\" ", name_table[n].nte_name);
331             len += nxt;
332         }
333         printf(";\n\n");
334
335         len = 0;
336         printf("static name_table_data const %s_names_data[] =\n",
337                root_name);
338         printf("{\n");
339         for (n = 0; n < CAPTABSIZE; n++) {
340             printf("\t{ %15d,\t%10s,\t%3d, %3d }%c\n",
341                    len,
342                    typenames[name_table[n].nte_type],
343                    name_table[n].nte_index,
344                    name_table[n].nte_link,
345                    n < CAPTABSIZE - 1 ? ',' : ' ');
346             len += (int) strlen(name_table[n].nte_name) + 1;
347         }
348         printf("};\n\n");
349         printf("static struct name_table_entry *_nc_%s_table = 0;\n\n", root_name);
350     } else {
351
352         printf("static struct name_table_entry %s _nc_%s_table[] =\n",
353                bigstring ? "" : "const",
354                root_name);
355         printf("{\n");
356         for (n = 0; n < CAPTABSIZE; n++) {
357             sprintf(buffer, "\"%s\"",
358                     name_table[n].nte_name);
359             printf("\t{ %15s,\t%10s,\t%3d, %3d }%c\n",
360                    buffer,
361                    typenames[name_table[n].nte_type],
362                    name_table[n].nte_index,
363                    name_table[n].nte_link,
364                    n < CAPTABSIZE - 1 ? ',' : ' ');
365         }
366         printf("};\n\n");
367     }
368
369     printf("static const short _nc_%s_hash_table[%d] =\n",
370            root_name,
371            HASHTABSIZE + 1);
372     printf("{\n");
373     for (n = 0; n < HASHTABSIZE; n++) {
374         printf("\t%3d,\n", hash_table[n]);
375     }
376     printf("\t0\t/* base-of-table */\n");
377     printf("};\n\n");
378
379     printf("#if (BOOLCOUNT!=%d)||(NUMCOUNT!=%d)||(STRCOUNT!=%d)\n",
380            BoolCount, NumCount, StrCount);
381     printf("#error\t--> term.h and comp_captab.c disagree about the <--\n");
382     printf("#error\t--> numbers of booleans, numbers and/or strings <--\n");
383     printf("#endif\n\n");
384
385     free(hash_table);
386     return EXIT_SUCCESS;
387 }
388 #endif