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