]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/comp_hash.c
ncurses 4.2
[ncurses.git] / ncurses / comp_hash.c
1 /****************************************************************************
2  * Copyright (c) 1998 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  ****************************************************************************/
33
34
35 /*
36  *      comp_hash.c --- Routines to deal with the hashtable of capability
37  *                      names.
38  *
39  */
40
41 #include <curses.priv.h>
42
43 #include <tic.h>
44 #include <hashsize.h>
45
46 #ifdef MAIN_PROGRAM
47 #include <ctype.h>
48 #undef  DEBUG
49 #define DEBUG(level, params) /*nothing*/
50 #endif
51
52 MODULE_ID("$Id: comp_hash.c,v 1.16 1998/02/11 12:13:53 tom Exp $")
53
54 static  int hash_function(const char *);
55
56 /*
57  *      _nc_make_hash_table()
58  *
59  *      Takes the entries in table[] and hashes them into hash_table[]
60  *      by name.  There are CAPTABSIZE entries in table[] and HASHTABSIZE
61  *      slots in hash_table[].
62  *
63  */
64
65 #ifdef MAIN_PROGRAM
66 static void _nc_make_hash_table(struct name_table_entry *table,
67                      struct name_table_entry **hash_table)
68 {
69 int     i;
70 int     hashvalue;
71 int     collisions = 0;
72
73         for (i = 0; i < CAPTABSIZE; i++) {
74             hashvalue = hash_function(table[i].nte_name);
75
76             if (hash_table[hashvalue] != (struct name_table_entry *) 0)
77                 collisions++;
78
79             if (hash_table[hashvalue] != 0)
80                 table[i].nte_link = (short)(hash_table[hashvalue] - table);
81             hash_table[hashvalue] = &table[i];
82         }
83
84         DEBUG(4, ("Hash table complete: %d collisions out of %d entries", collisions, CAPTABSIZE));
85 }
86 #endif
87
88
89 /*
90  *      int hash_function(string)
91  *
92  *      Computes the hashing function on the given string.
93  *
94  *      The current hash function is the sum of each consectutive pair
95  *      of characters, taken as two-byte integers, mod Hashtabsize.
96  *
97  */
98
99 static
100 int
101 hash_function(const char *string)
102 {
103 long    sum = 0;
104
105         DEBUG(9, ("hashing %s", string));
106         while (*string) {
107             sum += (long)(*string + (*(string + 1) << 8));
108             string++;
109         }
110
111         DEBUG(9, ("sum is %ld", sum));
112         return (int)(sum % HASHTABSIZE);
113 }
114
115
116 /*
117  *      struct name_table_entry *
118  *      find_entry(string)
119  *
120  *      Finds the entry for the given string in the hash table if present.
121  *      Returns a pointer to the entry in the table or 0 if not found.
122  *
123  */
124
125 #ifndef MAIN_PROGRAM
126 struct name_table_entry const *
127 _nc_find_entry(const char *string, const struct name_table_entry *const *hash_table)
128 {
129 int     hashvalue;
130 struct name_table_entry const *ptr;
131
132         hashvalue = hash_function(string);
133
134         if ((ptr = hash_table[hashvalue]) != 0) {
135                 while (strcmp(ptr->nte_name, string) != 0) {
136                         if (ptr->nte_link < 0)
137                                 return 0;
138                         ptr = ptr->nte_link + hash_table[HASHTABSIZE];
139                 }
140         }
141
142         return (ptr);
143 }
144
145 /*
146  *      struct name_table_entry *
147  *      find_type_entry(string, type, table)
148  *
149  *      Finds the first entry for the given name with the given type in the
150  *      given table if present (as distinct from find_entry, which finds the
151  *      the last entry regardless of type).  You can use this if you detect
152  *      a name clash.  It's slower, though.  Returns a pointer to the entry
153  *      in the table or 0 if not found.
154  */
155
156 struct name_table_entry const *
157 _nc_find_type_entry(const char *string,
158                     int type,
159                     const struct name_table_entry *table)
160 {
161 struct name_table_entry const *ptr;
162
163         for (ptr = table; ptr < table + CAPTABSIZE; ptr++) {
164             if (ptr->nte_type == type && strcmp(string, ptr->nte_name) == 0)
165                 return(ptr);
166         }
167
168         return ((struct name_table_entry *)NULL);
169 }
170 #endif
171
172 #ifdef MAIN_PROGRAM
173 /*
174  * This filter reads from standard input a list of tab-delimited columns,
175  * (e.g., from Caps.filtered) computes the hash-value of a specified column and
176  * writes the hashed tables to standard output.
177  *
178  * By compiling the hash table at build time, we're able to make the entire
179  * set of terminfo and termcap tables readonly (and also provide some runtime
180  * performance enhancement).
181  */
182
183 #if !HAVE_STRDUP
184 static char *strdup (char *s)
185 {
186   char *p;
187
188   p = malloc(strlen(s)+1);
189   if (p)
190     strcpy(p,s);
191   return(p);
192 }
193 #endif /* not HAVE_STRDUP */
194
195 #define MAX_COLUMNS BUFSIZ      /* this _has_ to be worst-case */
196
197 static char **parse_columns(char *buffer)
198 {
199         static char **list;
200
201         int col = 0;
202
203         if (list == 0)
204                 list = typeCalloc(char *, MAX_COLUMNS);
205
206         if (*buffer != '#') {
207                 while (*buffer != '\0') {
208                         char *s;
209                         for (s = buffer; (*s != '\0') && !isspace(*s); s++)
210                                 /*EMPTY*/;
211                         if (s != buffer) {
212                                 char mark = *s;
213                                 *s = '\0';
214                                 if ((s - buffer) > 1
215                                  && (*buffer == '"')
216                                  && (s[-1] == '"')) {   /* strip the quotes */
217                                         buffer++;
218                                         s[-1] = '\0';
219                                 }
220                                 list[col] = buffer;
221                                 col++;
222                                 if (mark == '\0')
223                                         break;
224                                 while (*++s && isspace(*s))
225                                         /*EMPTY*/;
226                                 buffer = s;
227                         } else
228                                 break;
229                 }
230         }
231         return col ? list : 0;
232 }
233
234 int main(int argc, char **argv)
235 {
236         struct name_table_entry *name_table = typeCalloc(struct name_table_entry, CAPTABSIZE);
237         struct name_table_entry **hash_table = typeCalloc(struct name_table_entry *, HASHTABSIZE);
238         const char *root_name = "";
239         int  column = 0;
240         int  n;
241         char buffer[BUFSIZ];
242
243         static const char * typenames[] = { "BOOLEAN", "NUMBER", "STRING" };
244
245         short BoolCount = 0;
246         short NumCount  = 0;
247         short StrCount  = 0;
248
249         /* The first argument is the column-number (starting with 0).
250          * The second is the root name of the tables to generate.
251          */
252         if (argc <= 2
253          || (column = atoi(argv[1])) <= 0
254          || (column >= MAX_COLUMNS)
255          || *(root_name = argv[2]) == 0) {
256                 fprintf(stderr, "usage: make_hash column root_name\n");
257                 exit(EXIT_FAILURE);
258         }
259
260         /*
261          * Read the table into our arrays.
262          */
263         for (n = 0; (n < CAPTABSIZE) && fgets(buffer, BUFSIZ, stdin); ) {
264                 char **list, *nlp = strchr(buffer, '\n');
265                 if (nlp)
266                     *nlp = '\0';
267                 list = parse_columns(buffer);
268                 if (list == 0)  /* blank or comment */
269                     continue;
270                 name_table[n].nte_link = -1;    /* end-of-hash */
271                 name_table[n].nte_name = strdup(list[column]);
272                 if (!strcmp(list[2], "bool")) {
273                         name_table[n].nte_type  = BOOLEAN;
274                         name_table[n].nte_index = BoolCount++;
275                 } else if (!strcmp(list[2], "num")) {
276                         name_table[n].nte_type  = NUMBER;
277                         name_table[n].nte_index = NumCount++;
278                 } else if (!strcmp(list[2], "str")) {
279                         name_table[n].nte_type  = STRING;
280                         name_table[n].nte_index = StrCount++;
281                 } else {
282                         fprintf(stderr, "Unknown type: %s\n", list[2]);
283                         exit(EXIT_FAILURE);
284                 }
285                 n++;
286         }
287         _nc_make_hash_table(name_table, hash_table);
288
289         /*
290          * Write the compiled tables to standard output
291          */
292         printf("static struct name_table_entry const _nc_%s_table[] =\n",
293                 root_name);
294         printf("{\n");
295         for (n = 0; n < CAPTABSIZE; n++) {
296                 sprintf(buffer, "\"%s\"",
297                         name_table[n].nte_name);
298                 printf("\t{ %15s,\t%10s,\t%3d, %3d }%c\n",
299                         buffer,
300                         typenames[name_table[n].nte_type],
301                         name_table[n].nte_index,
302                         name_table[n].nte_link,
303                         n < CAPTABSIZE - 1 ? ',' : ' ');
304         }
305         printf("};\n\n");
306
307         printf("const struct name_table_entry * const _nc_%s_hash_table[%d] =\n",
308                 root_name,
309                 HASHTABSIZE+1);
310         printf("{\n");
311         for (n = 0; n < HASHTABSIZE; n++) {
312                 if (hash_table[n] != 0) {
313                         sprintf(buffer, "_nc_%s_table + %3d",
314                                 root_name,
315                                 hash_table[n] - name_table);
316                 } else {
317                         strcpy(buffer, "0");
318                 }
319                 printf("\t%s,\n", buffer);
320         }
321         printf("\t_nc_%s_table\t/* base-of-table */\n", root_name);
322         printf("};\n\n");
323
324         printf("#if (BOOLCOUNT!=%d)||(NUMCOUNT!=%d)||(STRCOUNT!=%d)\n",
325                 BoolCount, NumCount, StrCount);
326         printf("#error\t--> term.h and comp_captab.c disagree about the <--\n");
327         printf("#error\t--> numbers of booleans, numbers and/or strings <--\n");
328         printf("#endif\n\n");
329
330         return EXIT_SUCCESS;
331 }
332 #endif