]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/tinfo/comp_hash.c
ncurses 5.6 - patch 20070908
[ncurses.git] / ncurses / tinfo / comp_hash.c
1 /****************************************************************************
2  * Copyright (c) 1998-2005,2007 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.33 2007/08/18 21:42:25 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 bigstring = 0;
244     int n;
245     char buffer[BUFSIZ];
246
247     static const char *typenames[] =
248     {"BOOLEAN", "NUMBER", "STRING"};
249
250     short BoolCount = 0;
251     short NumCount = 0;
252     short StrCount = 0;
253
254     /* The first argument is the column-number (starting with 0).
255      * The second is the root name of the tables to generate.
256      */
257     if (argc <= 3
258         || (column = atoi(argv[1])) <= 0
259         || (column >= MAX_COLUMNS)
260         || *(root_name = argv[2]) == 0
261         || (bigstring = atoi(argv[3])) < 0) {
262         fprintf(stderr, "usage: make_hash column root_name bigstring\n");
263         exit(EXIT_FAILURE);
264     }
265
266     /*
267      * Read the table into our arrays.
268      */
269     for (n = 0; (n < CAPTABSIZE) && fgets(buffer, BUFSIZ, stdin);) {
270         char **list, *nlp = strchr(buffer, '\n');
271         if (nlp)
272             *nlp = '\0';
273         list = parse_columns(buffer);
274         if (list == 0)          /* blank or comment */
275             continue;
276         name_table[n].nte_link = -1;    /* end-of-hash */
277         name_table[n].nte_name = strdup(list[column]);
278         if (!strcmp(list[2], "bool")) {
279             name_table[n].nte_type = BOOLEAN;
280             name_table[n].nte_index = BoolCount++;
281         } else if (!strcmp(list[2], "num")) {
282             name_table[n].nte_type = NUMBER;
283             name_table[n].nte_index = NumCount++;
284         } else if (!strcmp(list[2], "str")) {
285             name_table[n].nte_type = STRING;
286             name_table[n].nte_index = StrCount++;
287         } else {
288             fprintf(stderr, "Unknown type: %s\n", list[2]);
289             exit(EXIT_FAILURE);
290         }
291         n++;
292     }
293     _nc_make_hash_table(name_table, hash_table);
294
295     /*
296      * Write the compiled tables to standard output
297      */
298     if (bigstring) {
299         int len = 0;
300         int nxt;
301
302         printf("static const char %s_names_text[] = \\\n", root_name);
303         for (n = 0; n < CAPTABSIZE; n++) {
304             nxt = strlen(name_table[n].nte_name) + 5;
305             if (nxt + len > 72) {
306                 printf("\\\n");
307                 len = 0;
308             }
309             printf("\"%s\\0\" ", name_table[n].nte_name);
310             len += nxt;
311         }
312         printf(";\n\n");
313
314         len = 0;
315         printf("static name_table_data const %s_names_data[] =\n",
316                root_name);
317         printf("{\n");
318         for (n = 0; n < CAPTABSIZE; n++) {
319             printf("\t{ %15d,\t%10s,\t%3d, %3d }%c\n",
320                    len,
321                    typenames[name_table[n].nte_type],
322                    name_table[n].nte_index,
323                    name_table[n].nte_link,
324                    n < CAPTABSIZE - 1 ? ',' : ' ');
325             len += strlen(name_table[n].nte_name) + 1;
326         }
327         printf("};\n\n");
328         printf("static struct name_table_entry *_nc_%s_table = 0;\n\n", root_name);
329     } else {
330
331         printf("static struct name_table_entry %s _nc_%s_table[] =\n",
332                bigstring ? "" : "const",
333                root_name);
334         printf("{\n");
335         for (n = 0; n < CAPTABSIZE; n++) {
336             sprintf(buffer, "\"%s\"",
337                     name_table[n].nte_name);
338             printf("\t{ %15s,\t%10s,\t%3d, %3d }%c\n",
339                    buffer,
340                    typenames[name_table[n].nte_type],
341                    name_table[n].nte_index,
342                    name_table[n].nte_link,
343                    n < CAPTABSIZE - 1 ? ',' : ' ');
344         }
345         printf("};\n\n");
346     }
347
348     printf("static const short _nc_%s_hash_table[%d] =\n",
349            root_name,
350            HASHTABSIZE + 1);
351     printf("{\n");
352     for (n = 0; n < HASHTABSIZE; n++) {
353         printf("\t%3d,\n", hash_table[n]);
354     }
355     printf("\t0\t/* base-of-table */\n");
356     printf("};\n\n");
357
358     printf("#if (BOOLCOUNT!=%d)||(NUMCOUNT!=%d)||(STRCOUNT!=%d)\n",
359            BoolCount, NumCount, StrCount);
360     printf("#error\t--> term.h and comp_captab.c disagree about the <--\n");
361     printf("#error\t--> numbers of booleans, numbers and/or strings <--\n");
362     printf("#endif\n\n");
363
364     return EXIT_SUCCESS;
365 }
366 #endif