1 /****************************************************************************
2 * Copyright 2018-2019,2020 Thomas E. Dickey *
3 * Copyright 2009-2013,2017 Free Software Foundation, Inc. *
5 * Permission is hereby granted, free of charge, to any person obtaining a *
6 * copy of this software and associated documentation files (the *
7 * "Software"), to deal in the Software without restriction, including *
8 * without limitation the rights to use, copy, modify, merge, publish, *
9 * distribute, distribute with modifications, sublicense, and/or sell *
10 * copies of the Software, and to permit persons to whom the Software is *
11 * furnished to do so, subject to the following conditions: *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
19 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
22 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
24 * Except as contained in this notice, the name(s) of the above copyright *
25 * holders shall not be used in advertising or otherwise to promote the *
26 * sale, use or other dealings in this Software without prior written *
28 ****************************************************************************/
30 /****************************************************************************
31 * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 *
32 * and: Eric S. Raymond <esr@snark.thyrsus.com> *
33 * and: Thomas E. Dickey 1996-on *
34 ****************************************************************************/
37 * make_hash.c --- build-time program for constructing comp_captab.c
40 #include <build.priv.h>
47 MODULE_ID("$Id: make_hash.c,v 1.33 2020/02/02 23:34:34 tom Exp $")
50 * _nc_make_hash_table()
52 * Takes the entries in table[] and hashes them into hash_table[]
53 * by name. There are CAPTABSIZE entries in the predefined table[]
54 * and HASHTABSIZE slots in hash_table[].
59 #define MODULE_ID(id) /*nothing */
60 #include <tinfo/doalloc.c>
67 static const char *typenames[] =
68 {"BOOLEAN", "NUMBER", "STRING"};
80 size_t need = strlen(s) + 1;
81 char *result = malloc(need);
84 _nc_STRCPY(result, s, need);
89 * int hash_function(string)
91 * Computes the hashing function on the given string.
93 * The current hash function is the sum of each consecutive pair
94 * of characters, taken as two-byte integers, mod HASHTABSIZE.
99 hash_function(const char *string)
104 sum += (long) (UChar(*string) + (UChar(*(string + 1)) << 8));
108 return (int) (sum % HASHTABSIZE);
114 _nc_make_hash_table(struct user_table_entry *table,
115 HashValue * hash_table,
122 for (i = 0; i < HASHTABSIZE; i++) {
123 hash_table[i] = UNUSED;
125 for (i = 0; i < tablesize; i++) {
126 hashvalue = hash_function(table[i].ute_name);
128 if (hash_table[hashvalue] >= 0)
131 if (hash_table[hashvalue] != UNUSED) {
132 table[i].ute_link = hash_table[hashvalue];
134 hash_table[hashvalue] = (HashValue) i;
137 printf("/* %d collisions out of %d entries */\n", collisions, tablesize);
141 * This filter reads from standard input a list of tab-delimited columns,
142 * (e.g., from Caps.filtered) computes the hash-value of a specified column and
143 * writes the hashed tables to standard output.
145 * By compiling the hash table at build time, we're able to make the entire
146 * set of terminfo and termcap tables readonly (and also provide some runtime
147 * performance enhancement).
150 #define MAX_COLUMNS BUFSIZ /* this _has_ to be worst-case */
153 count_columns(char **list)
165 parse_columns(char *buffer)
177 if (*buffer != '#') {
179 list = typeCalloc(char *, (MAX_COLUMNS + 1));
183 while (*buffer != '\0') {
185 for (s = buffer; (*s != '\0') && !isspace(UChar(*s)); s++)
192 && (s[-1] == '"')) { /* strip the quotes */
193 assert(s > buffer + 1);
201 while (*++s && isspace(UChar(*s)))
208 return col ? list : 0;
211 #define SetType(n,t) \
213 name_table[n].ute_type |= (int)(1 << (t)); \
215 name_table[n].ute_type = (t)
219 ? get_type(name_table[n].ute_type) \
220 : typenames[name_table[n].ute_type])
223 get_type(int type_mask)
225 static char result[80];
227 _nc_STRCPY(result, L_PAREN, sizeof(result));
228 for (n = 0; n < 3; ++n) {
229 if ((1 << n) & type_mask) {
230 size_t want = 5 + strlen(typenames[n]);
231 if (want > sizeof(result)) {
232 fprintf(stderr, "Buffer is not large enough for %s + %s\n",
233 result, typenames[n]);
237 _nc_STRCAT(result, "|", sizeof(result));
238 _nc_STRCAT(result, "1<<", sizeof(result));
239 _nc_STRCAT(result, typenames[n], sizeof(result));
242 _nc_STRCAT(result, R_PAREN, sizeof(result));
247 main(int argc, char **argv)
249 unsigned tablesize = CAPTABSIZE;
250 struct user_table_entry *name_table = typeCalloc(struct
251 user_table_entry, tablesize);
252 HashValue *hash_table = typeCalloc(HashValue, HASHTABSIZE);
253 const char *root_name = "";
258 unsigned tableused = 0;
260 const char *table_name;
267 /* The first argument is the column-number (starting with 0).
268 * The second is the root name of the tables to generate.
271 || (column = atoi(argv[1])) <= 0
272 || (column >= MAX_COLUMNS)
273 || *(root_name = argv[2]) == 0
274 || (bigstring = atoi(argv[3])) < 0
276 || hash_table == 0) {
277 fprintf(stderr, "usage: make_hash column root_name bigstring\n");
280 is_user = (*root_name == 'u');
281 table_name = (is_user ? "user" : "name");
284 * Read the table into our arrays.
286 for (n = 0; (n < tablesize) && fgets(buffer, BUFSIZ, stdin);) {
288 char *nlp = strchr(buffer, '\n');
292 buffer[sizeof(buffer) - 2] = '\0';
293 list = parse_columns(buffer);
294 if (list == 0) /* blank or comment */
297 if (strcmp(list[0], "userdef"))
299 } else if (!strcmp(list[0], "userdef")) {
302 if (column < 0 || column > count_columns(list)) {
303 fprintf(stderr, "expected %d columns, have %d:\n%s\n",
312 for (j = 0; j < tableused; ++j) {
313 if (!strcmp(list[column], name_table[j].ute_name)) {
319 if (nn == tableused) {
320 name_table[nn].ute_link = -1; /* end-of-hash */
321 name_table[nn].ute_name = strmalloc(list[column]);
325 if (!strcmp(list[2], "bool")) {
326 SetType(nn, BOOLEAN);
327 name_table[nn].ute_index = BoolCount++;
328 } else if (!strcmp(list[2], "num")) {
330 name_table[nn].ute_index = NumCount++;
331 } else if (!strcmp(list[2], "str")) {
333 name_table[nn].ute_index = StrCount++;
335 if (*list[3] != '-') {
337 name_table[nn].ute_argc = (unsigned) strlen(list[3]);
338 for (j = 0; j < name_table[nn].ute_argc; ++j) {
339 if (list[3][j] == 's') {
340 name_table[nn].ute_args |= (1U << j);
346 fprintf(stderr, "Unknown type: %s\n", list[2]);
351 if (tablesize > tableused)
352 tablesize = tableused;
353 _nc_make_hash_table(name_table, hash_table, tablesize);
356 * Write the compiled tables to standard output
362 printf("static const char %s_names_text[] = \\\n", root_name);
363 for (n = 0; n < tablesize; n++) {
364 nxt = (int) strlen(name_table[n].ute_name) + 5;
365 if (nxt + len > 72) {
369 printf("\"%s\\0\" ", name_table[n].ute_name);
375 printf("static %s_table_data const %s_names_data[] =\n",
378 printf("%s\n", L_BRACE);
379 for (n = 0; n < tablesize; n++) {
380 printf("\t%s %15d,\t%10s,", L_BRACE, len, GetType(n));
383 name_table[n].ute_argc,
384 name_table[n].ute_args);
385 printf("\t%3d, %3d %s%c\n",
386 name_table[n].ute_index,
387 name_table[n].ute_link,
389 n < tablesize - 1 ? ',' : ' ');
390 len += (int) strlen(name_table[n].ute_name) + 1;
392 printf("%s;\n\n", R_BRACE);
393 printf("static struct %s_table_entry *_nc_%s_table = 0;\n\n",
398 printf("static struct %s_table_entry const _nc_%s_table[] =\n",
401 printf("%s\n", L_BRACE);
402 for (n = 0; n < tablesize; n++) {
403 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer)) "\"%s\"",
404 name_table[n].ute_name);
405 printf("\t%s %15s,\t%10s,", L_BRACE, buffer, GetType(n));
408 name_table[n].ute_argc,
409 name_table[n].ute_args);
410 printf("\t%3d, %3d %s%c\n",
411 name_table[n].ute_index,
412 name_table[n].ute_link,
414 n < tablesize - 1 ? ',' : ' ');
416 printf("%s;\n\n", R_BRACE);
419 printf("static const HashValue _nc_%s_hash_table[%d] =\n",
422 printf("%s\n", L_BRACE);
423 for (n = 0; n < HASHTABSIZE; n++) {
424 printf("\t%3d,\n", hash_table[n]);
426 printf("\t0\t/* base-of-table */\n");
427 printf("%s;\n\n", R_BRACE);
430 printf("#if (BOOLCOUNT!=%d)||(NUMCOUNT!=%d)||(STRCOUNT!=%d)\n",
431 BoolCount, NumCount, StrCount);
432 printf("#error\t--> term.h and comp_captab.c disagree about the <--\n");
433 printf("#error\t--> numbers of booleans, numbers and/or strings <--\n");
434 printf("#endif\n\n");
438 for (n = 0; (n < tablesize); ++n) {
439 free((void *) name_table[n].ute_name);