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.28 2019/07/27 23:06:54 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[80];
225 _nc_STRCPY(result, L_PAREN, sizeof(result));
226 for (n = 0; n < 3; ++n) {
227 if ((1 << n) & type_mask) {
228 size_t want = 5 + strlen(typenames[n]);
229 if (want > sizeof(result)) {
230 fprintf(stderr, "Buffer is not large enough for %s + %s\n",
231 result, typenames[n]);
235 _nc_STRCAT(result, "|", sizeof(result));
236 _nc_STRCAT(result, "1<<", sizeof(result));
237 _nc_STRCAT(result, typenames[n], sizeof(result));
240 _nc_STRCAT(result, R_PAREN, sizeof(result));
245 main(int argc, char **argv)
247 unsigned tablesize = CAPTABSIZE;
248 struct user_table_entry *name_table = typeCalloc(struct
249 user_table_entry, tablesize);
250 HashValue *hash_table = typeCalloc(HashValue, HASHTABSIZE);
251 const char *root_name = "";
256 unsigned tableused = 0;
258 const char *table_name;
265 /* The first argument is the column-number (starting with 0).
266 * The second is the root name of the tables to generate.
269 || (column = atoi(argv[1])) <= 0
270 || (column >= MAX_COLUMNS)
271 || *(root_name = argv[2]) == 0
272 || (bigstring = atoi(argv[3])) < 0
274 || hash_table == 0) {
275 fprintf(stderr, "usage: make_hash column root_name bigstring\n");
278 is_user = (*root_name == 'u');
279 table_name = (is_user ? "user" : "name");
282 * Read the table into our arrays.
284 for (n = 0; (n < tablesize) && fgets(buffer, BUFSIZ, stdin);) {
286 char *nlp = strchr(buffer, '\n');
290 buffer[sizeof(buffer) - 2] = '\0';
291 list = parse_columns(buffer);
292 if (list == 0) /* blank or comment */
295 if (strcmp(list[0], "userdef"))
297 } else if (!strcmp(list[0], "userdef")) {
300 if (column < 0 || column > count_columns(list)) {
301 fprintf(stderr, "expected %d columns, have %d:\n%s\n",
310 for (j = 0; j < tableused; ++j) {
311 if (!strcmp(list[column], name_table[j].ute_name)) {
317 if (nn == tableused) {
318 name_table[nn].ute_link = -1; /* end-of-hash */
319 name_table[nn].ute_name = strmalloc(list[column]);
323 if (!strcmp(list[2], "bool")) {
324 SetType(nn, BOOLEAN);
325 name_table[nn].ute_index = BoolCount++;
326 } else if (!strcmp(list[2], "num")) {
328 name_table[nn].ute_index = NumCount++;
329 } else if (!strcmp(list[2], "str")) {
331 name_table[nn].ute_index = StrCount++;
333 if (*list[3] != '-') {
335 name_table[nn].ute_argc = (unsigned) strlen(list[3]);
336 for (j = 0; j < name_table[nn].ute_argc; ++j) {
337 if (list[3][j] == 's') {
338 name_table[nn].ute_args |= (1U << j);
344 fprintf(stderr, "Unknown type: %s\n", list[2]);
349 if (tablesize > tableused)
350 tablesize = tableused;
351 _nc_make_hash_table(name_table, hash_table, tablesize);
354 * Write the compiled tables to standard output
360 printf("static const char %s_names_text[] = \\\n", root_name);
361 for (n = 0; n < tablesize; n++) {
362 nxt = (int) strlen(name_table[n].ute_name) + 5;
363 if (nxt + len > 72) {
367 printf("\"%s\\0\" ", name_table[n].ute_name);
373 printf("static %s_table_data const %s_names_data[] =\n",
376 printf("%s\n", L_BRACE);
377 for (n = 0; n < tablesize; n++) {
378 printf("\t%s %15d,\t%10s,", L_BRACE, len, GetType(n));
381 name_table[n].ute_argc,
382 name_table[n].ute_args);
383 printf("\t%3d, %3d %s%c\n",
384 name_table[n].ute_index,
385 name_table[n].ute_link,
387 n < tablesize - 1 ? ',' : ' ');
388 len += (int) strlen(name_table[n].ute_name) + 1;
390 printf("%s;\n\n", R_BRACE);
391 printf("static struct %s_table_entry *_nc_%s_table = 0;\n\n",
396 printf("static struct %s_table_entry const _nc_%s_table[] =\n",
399 printf("%s\n", L_BRACE);
400 for (n = 0; n < tablesize; n++) {
401 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer)) "\"%s\"",
402 name_table[n].ute_name);
403 printf("\t%s %15s,\t%10s,", L_BRACE, buffer, GetType(n));
406 name_table[n].ute_argc,
407 name_table[n].ute_args);
408 printf("\t%3d, %3d %s%c\n",
409 name_table[n].ute_index,
410 name_table[n].ute_link,
412 n < tablesize - 1 ? ',' : ' ');
414 printf("%s;\n\n", R_BRACE);
417 printf("static const HashValue _nc_%s_hash_table[%d] =\n",
420 printf("%s\n", L_BRACE);
421 for (n = 0; n < HASHTABSIZE; n++) {
422 printf("\t%3d,\n", hash_table[n]);
424 printf("\t0\t/* base-of-table */\n");
425 printf("%s;\n\n", R_BRACE);
428 printf("#if (BOOLCOUNT!=%d)||(NUMCOUNT!=%d)||(STRCOUNT!=%d)\n",
429 BoolCount, NumCount, StrCount);
430 printf("#error\t--> term.h and comp_captab.c disagree about the <--\n");
431 printf("#error\t--> numbers of booleans, numbers and/or strings <--\n");
432 printf("#endif\n\n");
437 for (n = 0; (n < tablesize); ++n) {
438 free((void *) name_table[n].ute_name);