1 /****************************************************************************
2 * Copyright (c) 1998-2017,2018 Free Software Foundation, Inc. *
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: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
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. *
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 *
27 ****************************************************************************/
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 ****************************************************************************/
36 * make_hash.c --- build-time program for constructing comp_captab.c
39 #include <build.priv.h>
46 MODULE_ID("$Id: make_hash.c,v 1.17 2018/05/12 15:58:31 tom Exp $")
49 * _nc_make_hash_table()
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[].
58 #define MODULE_ID(id) /*nothing */
59 #include <tinfo/doalloc.c>
71 size_t need = strlen(s) + 1;
72 char *result = malloc(need);
75 _nc_STRCPY(result, s, need);
80 * int hash_function(string)
82 * Computes the hashing function on the given string.
84 * The current hash function is the sum of each consectutive pair
85 * of characters, taken as two-byte integers, mod HASHTABSIZE.
90 hash_function(const char *string)
95 sum += (long) (*string + (*(string + 1) << 8));
99 return (int) (sum % HASHTABSIZE);
103 _nc_make_hash_table(struct name_table_entry *table,
104 HashValue * hash_table)
110 for (i = 0; i < HASHTABSIZE; i++) {
113 for (i = 0; i < CAPTABSIZE; i++) {
114 hashvalue = hash_function(table[i].nte_name);
116 if (hash_table[hashvalue] >= 0)
119 if (hash_table[hashvalue] != 0)
120 table[i].nte_link = hash_table[hashvalue];
121 hash_table[hashvalue] = i;
124 printf("/* %d collisions out of %d entries */\n", collisions, CAPTABSIZE);
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.
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).
137 #define MAX_COLUMNS BUFSIZ /* this _has_ to be worst-case */
140 count_columns(char **list)
152 parse_columns(char *buffer)
158 if (*buffer != '#') {
160 list = typeCalloc(char *, (MAX_COLUMNS + 1));
164 while (*buffer != '\0') {
166 for (s = buffer; (*s != '\0') && !isspace(UChar(*s)); s++)
173 && (s[-1] == '"')) { /* strip the quotes */
174 assert(s > buffer + 1);
182 while (*++s && isspace(UChar(*s)))
189 return col ? list : 0;
193 main(int argc, char **argv)
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 = "";
204 static const char *typenames[] =
205 {"BOOLEAN", "NUMBER", "STRING"};
211 /* The first argument is the column-number (starting with 0).
212 * The second is the root name of the tables to generate.
215 || (column = atoi(argv[1])) <= 0
216 || (column >= MAX_COLUMNS)
217 || *(root_name = argv[2]) == 0
218 || (bigstring = atoi(argv[3])) < 0
220 || hash_table == 0) {
221 fprintf(stderr, "usage: make_hash column root_name bigstring\n");
226 * Read the table into our arrays.
228 for (n = 0; (n < CAPTABSIZE) && fgets(buffer, BUFSIZ, stdin);) {
230 char *nlp = strchr(buffer, '\n');
234 buffer[sizeof(buffer) - 2] = '\0';
235 list = parse_columns(buffer);
236 if (list == 0) /* blank or comment */
238 if (column < 0 || column > count_columns(list)) {
239 fprintf(stderr, "expected %d columns, have %d:\n%s\n",
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++;
257 fprintf(stderr, "Unknown type: %s\n", list[2]);
262 _nc_make_hash_table(name_table, hash_table);
265 * Write the compiled tables to standard output
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) {
278 printf("\"%s\\0\" ", name_table[n].nte_name);
284 printf("static name_table_data const %s_names_data[] =\n",
287 for (n = 0; n < CAPTABSIZE; n++) {
288 printf("\t{ %15d,\t%10s,\t%3d, %3d }%c\n",
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;
297 printf("static struct name_table_entry *_nc_%s_table = 0;\n\n", root_name);
300 printf("static struct name_table_entry const _nc_%s_table[] =\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",
308 typenames[name_table[n].nte_type],
309 name_table[n].nte_index,
310 name_table[n].nte_link,
311 n < CAPTABSIZE - 1 ? ',' : ' ');
316 printf("static const HashValue _nc_%s_hash_table[%d] =\n",
320 for (n = 0; n < HASHTABSIZE; n++) {
321 printf("\t%3d,\n", hash_table[n]);
323 printf("\t0\t/* base-of-table */\n");
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");
334 for (n = 0; (n < CAPTABSIZE); ++n) {
335 free((void *) name_table[n].nte_name);