1 /****************************************************************************
2 * Copyright (c) 1998-2018,2019 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.26 2019/03/10 01:10:15 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 the predefined table[]
53 * and HASHTABSIZE slots in hash_table[].
58 #define MODULE_ID(id) /*nothing */
59 #include <tinfo/doalloc.c>
66 static const char *typenames[] =
67 {"BOOLEAN", "NUMBER", "STRING"};
79 size_t need = strlen(s) + 1;
80 char *result = malloc(need);
83 _nc_STRCPY(result, s, need);
88 * int hash_function(string)
90 * Computes the hashing function on the given string.
92 * The current hash function is the sum of each consectutive pair
93 * of characters, taken as two-byte integers, mod HASHTABSIZE.
98 hash_function(const char *string)
103 sum += (long) (*string + (*(string + 1) << 8));
107 return (int) (sum % HASHTABSIZE);
111 _nc_make_hash_table(struct user_table_entry *table,
112 HashValue * hash_table,
119 for (i = 0; i < HASHTABSIZE; i++) {
122 for (i = 0; i < tablesize; i++) {
123 hashvalue = hash_function(table[i].ute_name);
125 if (hash_table[hashvalue] >= 0)
128 if (hash_table[hashvalue] != 0)
129 table[i].ute_link = hash_table[hashvalue];
130 hash_table[hashvalue] = (HashValue) i;
133 printf("/* %d collisions out of %d entries */\n", collisions, tablesize);
137 * This filter reads from standard input a list of tab-delimited columns,
138 * (e.g., from Caps.filtered) computes the hash-value of a specified column and
139 * writes the hashed tables to standard output.
141 * By compiling the hash table at build time, we're able to make the entire
142 * set of terminfo and termcap tables readonly (and also provide some runtime
143 * performance enhancement).
146 #define MAX_COLUMNS BUFSIZ /* this _has_ to be worst-case */
149 count_columns(char **list)
161 parse_columns(char *buffer)
175 if (*buffer != '#') {
177 list = typeCalloc(char *, (MAX_COLUMNS + 1));
181 while (*buffer != '\0') {
183 for (s = buffer; (*s != '\0') && !isspace(UChar(*s)); s++)
190 && (s[-1] == '"')) { /* strip the quotes */
191 assert(s > buffer + 1);
199 while (*++s && isspace(UChar(*s)))
206 return col ? list : 0;
209 #define SetType(n,t) \
211 name_table[n].ute_type |= (int)(1 << (t)); \
213 name_table[n].ute_type = (t)
217 ? get_type(name_table[n].ute_type) \
218 : typenames[name_table[n].ute_type])
221 get_type(int type_mask)
223 static char result[40];
225 strcpy(result, L_PAREN);
226 for (n = 0; n < 3; ++n) {
227 if ((1 << n) & type_mask) {
230 strcat(result, "1<<");
231 strcat(result, typenames[n]);
234 strcat(result, R_PAREN);
239 main(int argc, char **argv)
241 unsigned tablesize = CAPTABSIZE;
242 struct user_table_entry *name_table = typeCalloc(struct
243 user_table_entry, tablesize);
244 HashValue *hash_table = typeCalloc(HashValue, HASHTABSIZE);
245 const char *root_name = "";
250 unsigned tableused = 0;
252 const char *table_name;
259 /* The first argument is the column-number (starting with 0).
260 * The second is the root name of the tables to generate.
263 || (column = atoi(argv[1])) <= 0
264 || (column >= MAX_COLUMNS)
265 || *(root_name = argv[2]) == 0
266 || (bigstring = atoi(argv[3])) < 0
268 || hash_table == 0) {
269 fprintf(stderr, "usage: make_hash column root_name bigstring\n");
272 is_user = (*root_name == 'u');
273 table_name = (is_user ? "user" : "name");
276 * Read the table into our arrays.
278 for (n = 0; (n < tablesize) && fgets(buffer, BUFSIZ, stdin);) {
280 char *nlp = strchr(buffer, '\n');
284 buffer[sizeof(buffer) - 2] = '\0';
285 list = parse_columns(buffer);
286 if (list == 0) /* blank or comment */
289 if (strcmp(list[0], "userdef"))
291 } else if (!strcmp(list[0], "userdef")) {
294 if (column < 0 || column > count_columns(list)) {
295 fprintf(stderr, "expected %d columns, have %d:\n%s\n",
304 for (j = 0; j < tableused; ++j) {
305 if (!strcmp(list[column], name_table[j].ute_name)) {
311 if (nn == tableused) {
312 name_table[nn].ute_link = -1; /* end-of-hash */
313 name_table[nn].ute_name = strmalloc(list[column]);
317 if (!strcmp(list[2], "bool")) {
318 SetType(nn, BOOLEAN);
319 name_table[nn].ute_index = BoolCount++;
320 } else if (!strcmp(list[2], "num")) {
322 name_table[nn].ute_index = NumCount++;
323 } else if (!strcmp(list[2], "str")) {
325 name_table[nn].ute_index = StrCount++;
327 if (*list[3] != '-') {
329 name_table[nn].ute_argc = (unsigned) strlen(list[3]);
330 for (j = 0; j < name_table[nn].ute_argc; ++j) {
331 if (list[3][j] == 's') {
332 name_table[nn].ute_args |= (1U << j);
338 fprintf(stderr, "Unknown type: %s\n", list[2]);
343 if (tablesize > tableused)
344 tablesize = tableused;
345 _nc_make_hash_table(name_table, hash_table, tablesize);
348 * Write the compiled tables to standard output
354 printf("static const char %s_names_text[] = \\\n", root_name);
355 for (n = 0; n < tablesize; n++) {
356 nxt = (int) strlen(name_table[n].ute_name) + 5;
357 if (nxt + len > 72) {
361 printf("\"%s\\0\" ", name_table[n].ute_name);
367 printf("static %s_table_data const %s_names_data[] =\n",
370 printf("%s\n", L_BRACE);
371 for (n = 0; n < tablesize; n++) {
372 printf("\t%s %15d,\t%10s,", L_BRACE, len, GetType(n));
375 name_table[n].ute_argc,
376 name_table[n].ute_args);
377 printf("\t%3d, %3d %s%c\n",
378 name_table[n].ute_index,
379 name_table[n].ute_link,
381 n < tablesize - 1 ? ',' : ' ');
382 len += (int) strlen(name_table[n].ute_name) + 1;
384 printf("%s;\n\n", R_BRACE);
385 printf("static struct %s_table_entry *_nc_%s_table = 0;\n\n",
390 printf("static struct %s_table_entry const _nc_%s_table[] =\n",
393 printf("%s\n", L_BRACE);
394 for (n = 0; n < tablesize; n++) {
395 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer)) "\"%s\"",
396 name_table[n].ute_name);
397 printf("\t%s %15s,\t%10s,", L_BRACE, buffer, GetType(n));
400 name_table[n].ute_argc,
401 name_table[n].ute_args);
402 printf("\t%3d, %3d %s%c\n",
403 name_table[n].ute_index,
404 name_table[n].ute_link,
406 n < tablesize - 1 ? ',' : ' ');
408 printf("%s;\n\n", R_BRACE);
411 printf("static const HashValue _nc_%s_hash_table[%d] =\n",
414 printf("%s\n", L_BRACE);
415 for (n = 0; n < HASHTABSIZE; n++) {
416 printf("\t%3d,\n", hash_table[n]);
418 printf("\t0\t/* base-of-table */\n");
419 printf("%s;\n\n", R_BRACE);
422 printf("#if (BOOLCOUNT!=%d)||(NUMCOUNT!=%d)||(STRCOUNT!=%d)\n",
423 BoolCount, NumCount, StrCount);
424 printf("#error\t--> term.h and comp_captab.c disagree about the <--\n");
425 printf("#error\t--> numbers of booleans, numbers and/or strings <--\n");
426 printf("#endif\n\n");
431 for (n = 0; (n < tablesize); ++n) {
432 free((void *) name_table[n].ute_name);