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