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