]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/tinfo/make_hash.c
ncurses 5.9 - patch 20130504
[ncurses.git] / ncurses / tinfo / make_hash.c
1 /****************************************************************************
2  * Copyright (c) 1998-2012,2013 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
40 #include <build.priv.h>
41
42 #include <tic.h>
43 #include <hashsize.h>
44
45 #include <ctype.h>
46
47 MODULE_ID("$Id: make_hash.c,v 1.12 2013/02/16 21:27:50 tom Exp $")
48
49 /*
50  *      _nc_make_hash_table()
51  *
52  *      Takes the entries in table[] and hashes them into hash_table[]
53  *      by name.  There are CAPTABSIZE entries in table[] and HASHTABSIZE
54  *      slots in hash_table[].
55  *
56  */
57
58 #undef MODULE_ID
59 #define MODULE_ID(id)           /*nothing */
60 #include <tinfo/doalloc.c>
61
62 static void
63 failed(const char *s)
64 {
65     perror(s);
66     exit(EXIT_FAILURE);
67 }
68
69 static char *
70 strmalloc(char *s)
71 {
72     char *result = malloc(strlen(s) + 1);
73     if (result == 0)
74         failed("strmalloc");
75     strcpy(result, s);
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 (list == 0 && (list = typeCalloc(char *, (MAX_COLUMNS + 1))) == 0)
159           return (0);
160
161     if (*buffer != '#') {
162         while (*buffer != '\0') {
163             char *s;
164             for (s = buffer; (*s != '\0') && !isspace(UChar(*s)); s++)
165                 /*EMPTY */ ;
166             if (s != buffer) {
167                 char mark = *s;
168                 *s = '\0';
169                 if ((s - buffer) > 1
170                     && (*buffer == '"')
171                     && (s[-1] == '"')) {        /* strip the quotes */
172                     assert(s > buffer + 1);
173                     s[-1] = '\0';
174                     buffer++;
175                 }
176                 list[col] = buffer;
177                 col++;
178                 if (mark == '\0')
179                     break;
180                 while (*++s && isspace(UChar(*s)))
181                     /*EMPTY */ ;
182                 buffer = s;
183             } else
184                 break;
185         }
186     }
187     return col ? list : 0;
188 }
189
190 int
191 main(int argc, char **argv)
192 {
193     struct name_table_entry *name_table = typeCalloc(struct
194                                                      name_table_entry, CAPTABSIZE);
195     HashValue *hash_table = typeCalloc(HashValue, HASHTABSIZE);
196     const char *root_name = "";
197     int column = 0;
198     int bigstring = 0;
199     int n;
200     char buffer[BUFSIZ];
201
202     static const char *typenames[] =
203     {"BOOLEAN", "NUMBER", "STRING"};
204
205     short BoolCount = 0;
206     short NumCount = 0;
207     short StrCount = 0;
208
209     /* The first argument is the column-number (starting with 0).
210      * The second is the root name of the tables to generate.
211      */
212     if (argc <= 3
213         || (column = atoi(argv[1])) <= 0
214         || (column >= MAX_COLUMNS)
215         || *(root_name = argv[2]) == 0
216         || (bigstring = atoi(argv[3])) < 0
217         || name_table == 0
218         || hash_table == 0) {
219         fprintf(stderr, "usage: make_hash column root_name bigstring\n");
220         exit(EXIT_FAILURE);
221     }
222
223     /*
224      * Read the table into our arrays.
225      */
226     for (n = 0; (n < CAPTABSIZE) && fgets(buffer, BUFSIZ, stdin);) {
227         char **list, *nlp = strchr(buffer, '\n');
228         if (nlp)
229             *nlp = '\0';
230         list = parse_columns(buffer);
231         if (list == 0)          /* blank or comment */
232             continue;
233         if (column > count_columns(list)) {
234             fprintf(stderr, "expected %d columns, have %d:\n%s\n",
235                     column,
236                     count_columns(list),
237                     buffer);
238             exit(EXIT_FAILURE);
239         }
240         name_table[n].nte_link = -1;    /* end-of-hash */
241         name_table[n].nte_name = strmalloc(list[column]);
242         if (!strcmp(list[2], "bool")) {
243             name_table[n].nte_type = BOOLEAN;
244             name_table[n].nte_index = BoolCount++;
245         } else if (!strcmp(list[2], "num")) {
246             name_table[n].nte_type = NUMBER;
247             name_table[n].nte_index = NumCount++;
248         } else if (!strcmp(list[2], "str")) {
249             name_table[n].nte_type = STRING;
250             name_table[n].nte_index = StrCount++;
251         } else {
252             fprintf(stderr, "Unknown type: %s\n", list[2]);
253             exit(EXIT_FAILURE);
254         }
255         n++;
256     }
257     _nc_make_hash_table(name_table, hash_table);
258
259     /*
260      * Write the compiled tables to standard output
261      */
262     if (bigstring) {
263         int len = 0;
264         int nxt;
265
266         printf("static const char %s_names_text[] = \\\n", root_name);
267         for (n = 0; n < CAPTABSIZE; n++) {
268             nxt = (int) strlen(name_table[n].nte_name) + 5;
269             if (nxt + len > 72) {
270                 printf("\\\n");
271                 len = 0;
272             }
273             printf("\"%s\\0\" ", name_table[n].nte_name);
274             len += nxt;
275         }
276         printf(";\n\n");
277
278         len = 0;
279         printf("static name_table_data const %s_names_data[] =\n",
280                root_name);
281         printf("{\n");
282         for (n = 0; n < CAPTABSIZE; n++) {
283             printf("\t{ %15d,\t%10s,\t%3d, %3d }%c\n",
284                    len,
285                    typenames[name_table[n].nte_type],
286                    name_table[n].nte_index,
287                    name_table[n].nte_link,
288                    n < CAPTABSIZE - 1 ? ',' : ' ');
289             len += (int) strlen(name_table[n].nte_name) + 1;
290         }
291         printf("};\n\n");
292         printf("static struct name_table_entry *_nc_%s_table = 0;\n\n", root_name);
293     } else {
294
295         printf("static struct name_table_entry const _nc_%s_table[] =\n",
296                root_name);
297         printf("{\n");
298         for (n = 0; n < CAPTABSIZE; n++) {
299             _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer)) "\"%s\"",
300                         name_table[n].nte_name);
301             printf("\t{ %15s,\t%10s,\t%3d, %3d }%c\n",
302                    buffer,
303                    typenames[name_table[n].nte_type],
304                    name_table[n].nte_index,
305                    name_table[n].nte_link,
306                    n < CAPTABSIZE - 1 ? ',' : ' ');
307         }
308         printf("};\n\n");
309     }
310
311     printf("static const HashValue _nc_%s_hash_table[%d] =\n",
312            root_name,
313            HASHTABSIZE + 1);
314     printf("{\n");
315     for (n = 0; n < HASHTABSIZE; n++) {
316         printf("\t%3d,\n", hash_table[n]);
317     }
318     printf("\t0\t/* base-of-table */\n");
319     printf("};\n\n");
320
321     printf("#if (BOOLCOUNT!=%d)||(NUMCOUNT!=%d)||(STRCOUNT!=%d)\n",
322            BoolCount, NumCount, StrCount);
323     printf("#error\t--> term.h and comp_captab.c disagree about the <--\n");
324     printf("#error\t--> numbers of booleans, numbers and/or strings <--\n");
325     printf("#endif\n\n");
326
327     free(hash_table);
328     return EXIT_SUCCESS;
329 }