xref: /freebsd/contrib/ncurses/ncurses/tinfo/make_hash.c (revision aae38d10b4eebf81c0942947e8b83a9bb8651d88)
106bfebdeSXin LI /****************************************************************************
2*aae38d10SBaptiste Daroussin  * Copyright (c) 1998-2019,2020 Free Software Foundation, Inc.              *
306bfebdeSXin LI  *                                                                          *
406bfebdeSXin LI  * Permission is hereby granted, free of charge, to any person obtaining a  *
506bfebdeSXin LI  * copy of this software and associated documentation files (the            *
606bfebdeSXin LI  * "Software"), to deal in the Software without restriction, including      *
706bfebdeSXin LI  * without limitation the rights to use, copy, modify, merge, publish,      *
806bfebdeSXin LI  * distribute, distribute with modifications, sublicense, and/or sell       *
906bfebdeSXin LI  * copies of the Software, and to permit persons to whom the Software is    *
1006bfebdeSXin LI  * furnished to do so, subject to the following conditions:                 *
1106bfebdeSXin LI  *                                                                          *
1206bfebdeSXin LI  * The above copyright notice and this permission notice shall be included  *
1306bfebdeSXin LI  * in all copies or substantial portions of the Software.                   *
1406bfebdeSXin LI  *                                                                          *
1506bfebdeSXin LI  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
1606bfebdeSXin LI  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
1706bfebdeSXin LI  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
1806bfebdeSXin LI  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
1906bfebdeSXin LI  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
2006bfebdeSXin LI  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
2106bfebdeSXin LI  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
2206bfebdeSXin LI  *                                                                          *
2306bfebdeSXin LI  * Except as contained in this notice, the name(s) of the above copyright   *
2406bfebdeSXin LI  * holders shall not be used in advertising or otherwise to promote the     *
2506bfebdeSXin LI  * sale, use or other dealings in this Software without prior written       *
2606bfebdeSXin LI  * authorization.                                                           *
2706bfebdeSXin LI  ****************************************************************************/
2806bfebdeSXin LI 
2906bfebdeSXin LI /****************************************************************************
3006bfebdeSXin LI  *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
3106bfebdeSXin LI  *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
3206bfebdeSXin LI  *     and: Thomas E. Dickey                        1996-on                 *
3306bfebdeSXin LI  ****************************************************************************/
3406bfebdeSXin LI 
3506bfebdeSXin LI /*
3606bfebdeSXin LI  *	make_hash.c --- build-time program for constructing comp_captab.c
3706bfebdeSXin LI  */
3806bfebdeSXin LI 
3906bfebdeSXin LI #include <build.priv.h>
4006bfebdeSXin LI 
4106bfebdeSXin LI #include <tic.h>
4206bfebdeSXin LI #include <hashsize.h>
4306bfebdeSXin LI 
4406bfebdeSXin LI #include <ctype.h>
4506bfebdeSXin LI 
46*aae38d10SBaptiste Daroussin MODULE_ID("$Id: make_hash.c,v 1.30 2020/01/18 17:02:38 tom Exp $")
4706bfebdeSXin LI 
4806bfebdeSXin LI /*
4906bfebdeSXin LI  *	_nc_make_hash_table()
5006bfebdeSXin LI  *
5106bfebdeSXin LI  *	Takes the entries in table[] and hashes them into hash_table[]
52*aae38d10SBaptiste Daroussin  *	by name.  There are CAPTABSIZE entries in the predefined table[]
53*aae38d10SBaptiste Daroussin  *	and HASHTABSIZE slots in hash_table[].
5406bfebdeSXin LI  *
5506bfebdeSXin LI  */
5606bfebdeSXin LI 
5706bfebdeSXin LI #undef MODULE_ID
5806bfebdeSXin LI #define MODULE_ID(id)		/*nothing */
5906bfebdeSXin LI #include <tinfo/doalloc.c>
6006bfebdeSXin LI 
61*aae38d10SBaptiste Daroussin #define L_PAREN "("
62*aae38d10SBaptiste Daroussin #define R_PAREN ")"
63*aae38d10SBaptiste Daroussin #define L_BRACE "{"
64*aae38d10SBaptiste Daroussin #define R_BRACE "}"
65*aae38d10SBaptiste Daroussin 
66*aae38d10SBaptiste Daroussin static const char *typenames[] =
67*aae38d10SBaptiste Daroussin {"BOOLEAN", "NUMBER", "STRING"};
68*aae38d10SBaptiste Daroussin 
6973f0a83dSXin LI static void
7073f0a83dSXin LI failed(const char *s)
7173f0a83dSXin LI {
7273f0a83dSXin LI     perror(s);
7373f0a83dSXin LI     exit(EXIT_FAILURE);
7473f0a83dSXin LI }
7573f0a83dSXin LI 
7673f0a83dSXin LI static char *
7773f0a83dSXin LI strmalloc(char *s)
7873f0a83dSXin LI {
7973f0a83dSXin LI     size_t need = strlen(s) + 1;
8073f0a83dSXin LI     char *result = malloc(need);
8173f0a83dSXin LI     if (result == 0)
8273f0a83dSXin LI 	failed("strmalloc");
8373f0a83dSXin LI     _nc_STRCPY(result, s, need);
8473f0a83dSXin LI     return result;
8573f0a83dSXin LI }
8673f0a83dSXin LI 
8706bfebdeSXin LI /*
8806bfebdeSXin LI  *	int hash_function(string)
8906bfebdeSXin LI  *
9006bfebdeSXin LI  *	Computes the hashing function on the given string.
9106bfebdeSXin LI  *
92*aae38d10SBaptiste Daroussin  *	The current hash function is the sum of each consecutive pair
9306bfebdeSXin LI  *	of characters, taken as two-byte integers, mod HASHTABSIZE.
9406bfebdeSXin LI  *
9506bfebdeSXin LI  */
9606bfebdeSXin LI 
9706bfebdeSXin LI static int
9806bfebdeSXin LI hash_function(const char *string)
9906bfebdeSXin LI {
10006bfebdeSXin LI     long sum = 0;
10106bfebdeSXin LI 
10206bfebdeSXin LI     while (*string) {
10306bfebdeSXin LI 	sum += (long) (*string + (*(string + 1) << 8));
10406bfebdeSXin LI 	string++;
10506bfebdeSXin LI     }
10606bfebdeSXin LI 
10706bfebdeSXin LI     return (int) (sum % HASHTABSIZE);
10806bfebdeSXin LI }
10906bfebdeSXin LI 
11006bfebdeSXin LI static void
111*aae38d10SBaptiste Daroussin _nc_make_hash_table(struct user_table_entry *table,
112*aae38d10SBaptiste Daroussin 		    HashValue * hash_table,
113*aae38d10SBaptiste Daroussin 		    unsigned tablesize)
11406bfebdeSXin LI {
115*aae38d10SBaptiste Daroussin     unsigned i;
11606bfebdeSXin LI     int hashvalue;
11706bfebdeSXin LI     int collisions = 0;
11806bfebdeSXin LI 
11906bfebdeSXin LI     for (i = 0; i < HASHTABSIZE; i++) {
12006bfebdeSXin LI 	hash_table[i] = -1;
12106bfebdeSXin LI     }
122*aae38d10SBaptiste Daroussin     for (i = 0; i < tablesize; i++) {
123*aae38d10SBaptiste Daroussin 	hashvalue = hash_function(table[i].ute_name);
12406bfebdeSXin LI 
12506bfebdeSXin LI 	if (hash_table[hashvalue] >= 0)
12606bfebdeSXin LI 	    collisions++;
12706bfebdeSXin LI 
12806bfebdeSXin LI 	if (hash_table[hashvalue] != 0)
129*aae38d10SBaptiste Daroussin 	    table[i].ute_link = hash_table[hashvalue];
130*aae38d10SBaptiste Daroussin 	hash_table[hashvalue] = (HashValue) i;
13106bfebdeSXin LI     }
13206bfebdeSXin LI 
133*aae38d10SBaptiste Daroussin     printf("/* %d collisions out of %d entries */\n", collisions, tablesize);
13406bfebdeSXin LI }
13506bfebdeSXin LI 
13606bfebdeSXin LI /*
13706bfebdeSXin LI  * This filter reads from standard input a list of tab-delimited columns,
13806bfebdeSXin LI  * (e.g., from Caps.filtered) computes the hash-value of a specified column and
13906bfebdeSXin LI  * writes the hashed tables to standard output.
14006bfebdeSXin LI  *
14106bfebdeSXin LI  * By compiling the hash table at build time, we're able to make the entire
14206bfebdeSXin LI  * set of terminfo and termcap tables readonly (and also provide some runtime
14306bfebdeSXin LI  * performance enhancement).
14406bfebdeSXin LI  */
14506bfebdeSXin LI 
14606bfebdeSXin LI #define MAX_COLUMNS BUFSIZ	/* this _has_ to be worst-case */
14706bfebdeSXin LI 
14873f0a83dSXin LI static int
14973f0a83dSXin LI count_columns(char **list)
15073f0a83dSXin LI {
15173f0a83dSXin LI     int result = 0;
15273f0a83dSXin LI     if (list != 0) {
15373f0a83dSXin LI 	while (*list++) {
15473f0a83dSXin LI 	    ++result;
15573f0a83dSXin LI 	}
15673f0a83dSXin LI     }
15773f0a83dSXin LI     return result;
15873f0a83dSXin LI }
15973f0a83dSXin LI 
16006bfebdeSXin LI static char **
16106bfebdeSXin LI parse_columns(char *buffer)
16206bfebdeSXin LI {
16306bfebdeSXin LI     static char **list;
16406bfebdeSXin LI 
16506bfebdeSXin LI     int col = 0;
16606bfebdeSXin LI 
167*aae38d10SBaptiste Daroussin     if (buffer == 0) {
168*aae38d10SBaptiste Daroussin 	free(list);
169*aae38d10SBaptiste Daroussin 	list = 0;
170*aae38d10SBaptiste Daroussin 	return 0;
171*aae38d10SBaptiste Daroussin     }
17206bfebdeSXin LI 
17306bfebdeSXin LI     if (*buffer != '#') {
174*aae38d10SBaptiste Daroussin 	if (list == 0) {
175*aae38d10SBaptiste Daroussin 	    list = typeCalloc(char *, (MAX_COLUMNS + 1));
176*aae38d10SBaptiste Daroussin 	    if (list == 0)
177*aae38d10SBaptiste Daroussin 		return (0);
178*aae38d10SBaptiste Daroussin 	}
17906bfebdeSXin LI 	while (*buffer != '\0') {
18006bfebdeSXin LI 	    char *s;
18106bfebdeSXin LI 	    for (s = buffer; (*s != '\0') && !isspace(UChar(*s)); s++)
18206bfebdeSXin LI 		/*EMPTY */ ;
18306bfebdeSXin LI 	    if (s != buffer) {
18406bfebdeSXin LI 		char mark = *s;
18506bfebdeSXin LI 		*s = '\0';
18606bfebdeSXin LI 		if ((s - buffer) > 1
18706bfebdeSXin LI 		    && (*buffer == '"')
18806bfebdeSXin LI 		    && (s[-1] == '"')) {	/* strip the quotes */
18906bfebdeSXin LI 		    assert(s > buffer + 1);
19006bfebdeSXin LI 		    s[-1] = '\0';
19106bfebdeSXin LI 		    buffer++;
19206bfebdeSXin LI 		}
19306bfebdeSXin LI 		list[col] = buffer;
19406bfebdeSXin LI 		col++;
19506bfebdeSXin LI 		if (mark == '\0')
19606bfebdeSXin LI 		    break;
19706bfebdeSXin LI 		while (*++s && isspace(UChar(*s)))
19806bfebdeSXin LI 		    /*EMPTY */ ;
19906bfebdeSXin LI 		buffer = s;
20006bfebdeSXin LI 	    } else
20106bfebdeSXin LI 		break;
20206bfebdeSXin LI 	}
20306bfebdeSXin LI     }
20406bfebdeSXin LI     return col ? list : 0;
20506bfebdeSXin LI }
20606bfebdeSXin LI 
207*aae38d10SBaptiste Daroussin #define SetType(n,t) \
208*aae38d10SBaptiste Daroussin 	if (is_user) \
209*aae38d10SBaptiste Daroussin 	    name_table[n].ute_type |= (int)(1 << (t)); \
210*aae38d10SBaptiste Daroussin 	else \
211*aae38d10SBaptiste Daroussin 	    name_table[n].ute_type = (t)
212*aae38d10SBaptiste Daroussin 
213*aae38d10SBaptiste Daroussin #define GetType(n) \
214*aae38d10SBaptiste Daroussin 	(is_user \
215*aae38d10SBaptiste Daroussin 	 ? get_type(name_table[n].ute_type) \
216*aae38d10SBaptiste Daroussin 	 : typenames[name_table[n].ute_type])
217*aae38d10SBaptiste Daroussin 
218*aae38d10SBaptiste Daroussin static char *
219*aae38d10SBaptiste Daroussin get_type(int type_mask)
220*aae38d10SBaptiste Daroussin {
221*aae38d10SBaptiste Daroussin     static char result[80];
222*aae38d10SBaptiste Daroussin     unsigned n;
223*aae38d10SBaptiste Daroussin     _nc_STRCPY(result, L_PAREN, sizeof(result));
224*aae38d10SBaptiste Daroussin     for (n = 0; n < 3; ++n) {
225*aae38d10SBaptiste Daroussin 	if ((1 << n) & type_mask) {
226*aae38d10SBaptiste Daroussin 	    size_t want = 5 + strlen(typenames[n]);
227*aae38d10SBaptiste Daroussin 	    if (want > sizeof(result)) {
228*aae38d10SBaptiste Daroussin 		fprintf(stderr, "Buffer is not large enough for %s + %s\n",
229*aae38d10SBaptiste Daroussin 			result, typenames[n]);
230*aae38d10SBaptiste Daroussin 		exit(EXIT_FAILURE);
231*aae38d10SBaptiste Daroussin 	    }
232*aae38d10SBaptiste Daroussin 	    if (result[1])
233*aae38d10SBaptiste Daroussin 		_nc_STRCAT(result, "|", sizeof(result));
234*aae38d10SBaptiste Daroussin 	    _nc_STRCAT(result, "1<<", sizeof(result));
235*aae38d10SBaptiste Daroussin 	    _nc_STRCAT(result, typenames[n], sizeof(result));
236*aae38d10SBaptiste Daroussin 	}
237*aae38d10SBaptiste Daroussin     }
238*aae38d10SBaptiste Daroussin     _nc_STRCAT(result, R_PAREN, sizeof(result));
239*aae38d10SBaptiste Daroussin     return result;
240*aae38d10SBaptiste Daroussin }
241*aae38d10SBaptiste Daroussin 
24206bfebdeSXin LI int
24306bfebdeSXin LI main(int argc, char **argv)
24406bfebdeSXin LI {
245*aae38d10SBaptiste Daroussin     unsigned tablesize = CAPTABSIZE;
246*aae38d10SBaptiste Daroussin     struct user_table_entry *name_table = typeCalloc(struct
247*aae38d10SBaptiste Daroussin 						     user_table_entry, tablesize);
24806bfebdeSXin LI     HashValue *hash_table = typeCalloc(HashValue, HASHTABSIZE);
24906bfebdeSXin LI     const char *root_name = "";
25006bfebdeSXin LI     int column = 0;
25106bfebdeSXin LI     int bigstring = 0;
252*aae38d10SBaptiste Daroussin     unsigned n;
253*aae38d10SBaptiste Daroussin     unsigned nn;
254*aae38d10SBaptiste Daroussin     unsigned tableused = 0;
255*aae38d10SBaptiste Daroussin     bool is_user;
256*aae38d10SBaptiste Daroussin     const char *table_name;
25706bfebdeSXin LI     char buffer[BUFSIZ];
25806bfebdeSXin LI 
25906bfebdeSXin LI     short BoolCount = 0;
26006bfebdeSXin LI     short NumCount = 0;
26106bfebdeSXin LI     short StrCount = 0;
26206bfebdeSXin LI 
26306bfebdeSXin LI     /* The first argument is the column-number (starting with 0).
26406bfebdeSXin LI      * The second is the root name of the tables to generate.
26506bfebdeSXin LI      */
26606bfebdeSXin LI     if (argc <= 3
26706bfebdeSXin LI 	|| (column = atoi(argv[1])) <= 0
26806bfebdeSXin LI 	|| (column >= MAX_COLUMNS)
26906bfebdeSXin LI 	|| *(root_name = argv[2]) == 0
27006bfebdeSXin LI 	|| (bigstring = atoi(argv[3])) < 0
27106bfebdeSXin LI 	|| name_table == 0
27206bfebdeSXin LI 	|| hash_table == 0) {
27306bfebdeSXin LI 	fprintf(stderr, "usage: make_hash column root_name bigstring\n");
27406bfebdeSXin LI 	exit(EXIT_FAILURE);
27506bfebdeSXin LI     }
276*aae38d10SBaptiste Daroussin     is_user = (*root_name == 'u');
277*aae38d10SBaptiste Daroussin     table_name = (is_user ? "user" : "name");
27806bfebdeSXin LI 
27906bfebdeSXin LI     /*
28006bfebdeSXin LI      * Read the table into our arrays.
28106bfebdeSXin LI      */
282*aae38d10SBaptiste Daroussin     for (n = 0; (n < tablesize) && fgets(buffer, BUFSIZ, stdin);) {
283*aae38d10SBaptiste Daroussin 	char **list;
284*aae38d10SBaptiste Daroussin 	char *nlp = strchr(buffer, '\n');
28506bfebdeSXin LI 	if (nlp)
28606bfebdeSXin LI 	    *nlp = '\0';
287*aae38d10SBaptiste Daroussin 	else
288*aae38d10SBaptiste Daroussin 	    buffer[sizeof(buffer) - 2] = '\0';
28906bfebdeSXin LI 	list = parse_columns(buffer);
29006bfebdeSXin LI 	if (list == 0)		/* blank or comment */
29106bfebdeSXin LI 	    continue;
292*aae38d10SBaptiste Daroussin 	if (is_user) {
293*aae38d10SBaptiste Daroussin 	    if (strcmp(list[0], "userdef"))
294*aae38d10SBaptiste Daroussin 		continue;
295*aae38d10SBaptiste Daroussin 	} else if (!strcmp(list[0], "userdef")) {
296*aae38d10SBaptiste Daroussin 	    continue;
297*aae38d10SBaptiste Daroussin 	}
298*aae38d10SBaptiste Daroussin 	if (column < 0 || column > count_columns(list)) {
29973f0a83dSXin LI 	    fprintf(stderr, "expected %d columns, have %d:\n%s\n",
30073f0a83dSXin LI 		    column,
30173f0a83dSXin LI 		    count_columns(list),
30273f0a83dSXin LI 		    buffer);
30373f0a83dSXin LI 	    exit(EXIT_FAILURE);
30473f0a83dSXin LI 	}
305*aae38d10SBaptiste Daroussin 	nn = tableused;
306*aae38d10SBaptiste Daroussin 	if (is_user) {
307*aae38d10SBaptiste Daroussin 	    unsigned j;
308*aae38d10SBaptiste Daroussin 	    for (j = 0; j < tableused; ++j) {
309*aae38d10SBaptiste Daroussin 		if (!strcmp(list[column], name_table[j].ute_name)) {
310*aae38d10SBaptiste Daroussin 		    nn = j;
311*aae38d10SBaptiste Daroussin 		    break;
312*aae38d10SBaptiste Daroussin 		}
313*aae38d10SBaptiste Daroussin 	    }
314*aae38d10SBaptiste Daroussin 	}
315*aae38d10SBaptiste Daroussin 	if (nn == tableused) {
316*aae38d10SBaptiste Daroussin 	    name_table[nn].ute_link = -1;	/* end-of-hash */
317*aae38d10SBaptiste Daroussin 	    name_table[nn].ute_name = strmalloc(list[column]);
318*aae38d10SBaptiste Daroussin 	    ++tableused;
319*aae38d10SBaptiste Daroussin 	}
320*aae38d10SBaptiste Daroussin 
32106bfebdeSXin LI 	if (!strcmp(list[2], "bool")) {
322*aae38d10SBaptiste Daroussin 	    SetType(nn, BOOLEAN);
323*aae38d10SBaptiste Daroussin 	    name_table[nn].ute_index = BoolCount++;
32406bfebdeSXin LI 	} else if (!strcmp(list[2], "num")) {
325*aae38d10SBaptiste Daroussin 	    SetType(nn, NUMBER);
326*aae38d10SBaptiste Daroussin 	    name_table[nn].ute_index = NumCount++;
32706bfebdeSXin LI 	} else if (!strcmp(list[2], "str")) {
328*aae38d10SBaptiste Daroussin 	    SetType(nn, STRING);
329*aae38d10SBaptiste Daroussin 	    name_table[nn].ute_index = StrCount++;
330*aae38d10SBaptiste Daroussin 	    if (is_user) {
331*aae38d10SBaptiste Daroussin 		if (*list[3] != '-') {
332*aae38d10SBaptiste Daroussin 		    unsigned j;
333*aae38d10SBaptiste Daroussin 		    name_table[nn].ute_argc = (unsigned) strlen(list[3]);
334*aae38d10SBaptiste Daroussin 		    for (j = 0; j < name_table[nn].ute_argc; ++j) {
335*aae38d10SBaptiste Daroussin 			if (list[3][j] == 's') {
336*aae38d10SBaptiste Daroussin 			    name_table[nn].ute_args |= (1U << j);
337*aae38d10SBaptiste Daroussin 			}
338*aae38d10SBaptiste Daroussin 		    }
339*aae38d10SBaptiste Daroussin 		}
340*aae38d10SBaptiste Daroussin 	    }
34106bfebdeSXin LI 	} else {
34206bfebdeSXin LI 	    fprintf(stderr, "Unknown type: %s\n", list[2]);
34306bfebdeSXin LI 	    exit(EXIT_FAILURE);
34406bfebdeSXin LI 	}
34506bfebdeSXin LI 	n++;
34606bfebdeSXin LI     }
347*aae38d10SBaptiste Daroussin     if (tablesize > tableused)
348*aae38d10SBaptiste Daroussin 	tablesize = tableused;
349*aae38d10SBaptiste Daroussin     _nc_make_hash_table(name_table, hash_table, tablesize);
35006bfebdeSXin LI 
35106bfebdeSXin LI     /*
35206bfebdeSXin LI      * Write the compiled tables to standard output
35306bfebdeSXin LI      */
35406bfebdeSXin LI     if (bigstring) {
35506bfebdeSXin LI 	int len = 0;
35606bfebdeSXin LI 	int nxt;
35706bfebdeSXin LI 
35806bfebdeSXin LI 	printf("static const char %s_names_text[] = \\\n", root_name);
359*aae38d10SBaptiste Daroussin 	for (n = 0; n < tablesize; n++) {
360*aae38d10SBaptiste Daroussin 	    nxt = (int) strlen(name_table[n].ute_name) + 5;
36106bfebdeSXin LI 	    if (nxt + len > 72) {
36206bfebdeSXin LI 		printf("\\\n");
36306bfebdeSXin LI 		len = 0;
36406bfebdeSXin LI 	    }
365*aae38d10SBaptiste Daroussin 	    printf("\"%s\\0\" ", name_table[n].ute_name);
36606bfebdeSXin LI 	    len += nxt;
36706bfebdeSXin LI 	}
36806bfebdeSXin LI 	printf(";\n\n");
36906bfebdeSXin LI 
37006bfebdeSXin LI 	len = 0;
371*aae38d10SBaptiste Daroussin 	printf("static %s_table_data const %s_names_data[] =\n",
372*aae38d10SBaptiste Daroussin 	       table_name,
37306bfebdeSXin LI 	       root_name);
374*aae38d10SBaptiste Daroussin 	printf("%s\n", L_BRACE);
375*aae38d10SBaptiste Daroussin 	for (n = 0; n < tablesize; n++) {
376*aae38d10SBaptiste Daroussin 	    printf("\t%s %15d,\t%10s,", L_BRACE, len, GetType(n));
377*aae38d10SBaptiste Daroussin 	    if (is_user)
378*aae38d10SBaptiste Daroussin 		printf("\t%d,%d,",
379*aae38d10SBaptiste Daroussin 		       name_table[n].ute_argc,
380*aae38d10SBaptiste Daroussin 		       name_table[n].ute_args);
381*aae38d10SBaptiste Daroussin 	    printf("\t%3d, %3d %s%c\n",
382*aae38d10SBaptiste Daroussin 		   name_table[n].ute_index,
383*aae38d10SBaptiste Daroussin 		   name_table[n].ute_link,
384*aae38d10SBaptiste Daroussin 		   R_BRACE,
385*aae38d10SBaptiste Daroussin 		   n < tablesize - 1 ? ',' : ' ');
386*aae38d10SBaptiste Daroussin 	    len += (int) strlen(name_table[n].ute_name) + 1;
38706bfebdeSXin LI 	}
388*aae38d10SBaptiste Daroussin 	printf("%s;\n\n", R_BRACE);
389*aae38d10SBaptiste Daroussin 	printf("static struct %s_table_entry *_nc_%s_table = 0;\n\n",
390*aae38d10SBaptiste Daroussin 	       table_name,
391*aae38d10SBaptiste Daroussin 	       root_name);
39206bfebdeSXin LI     } else {
39306bfebdeSXin LI 
394*aae38d10SBaptiste Daroussin 	printf("static struct %s_table_entry const _nc_%s_table[] =\n",
395*aae38d10SBaptiste Daroussin 	       table_name,
39606bfebdeSXin LI 	       root_name);
397*aae38d10SBaptiste Daroussin 	printf("%s\n", L_BRACE);
398*aae38d10SBaptiste Daroussin 	for (n = 0; n < tablesize; n++) {
39973f0a83dSXin LI 	    _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer)) "\"%s\"",
400*aae38d10SBaptiste Daroussin 			name_table[n].ute_name);
401*aae38d10SBaptiste Daroussin 	    printf("\t%s %15s,\t%10s,", L_BRACE, buffer, GetType(n));
402*aae38d10SBaptiste Daroussin 	    if (is_user)
403*aae38d10SBaptiste Daroussin 		printf("\t%d,%d,",
404*aae38d10SBaptiste Daroussin 		       name_table[n].ute_argc,
405*aae38d10SBaptiste Daroussin 		       name_table[n].ute_args);
406*aae38d10SBaptiste Daroussin 	    printf("\t%3d, %3d %s%c\n",
407*aae38d10SBaptiste Daroussin 		   name_table[n].ute_index,
408*aae38d10SBaptiste Daroussin 		   name_table[n].ute_link,
409*aae38d10SBaptiste Daroussin 		   R_BRACE,
410*aae38d10SBaptiste Daroussin 		   n < tablesize - 1 ? ',' : ' ');
41106bfebdeSXin LI 	}
412*aae38d10SBaptiste Daroussin 	printf("%s;\n\n", R_BRACE);
41306bfebdeSXin LI     }
41406bfebdeSXin LI 
41506bfebdeSXin LI     printf("static const HashValue _nc_%s_hash_table[%d] =\n",
41606bfebdeSXin LI 	   root_name,
41706bfebdeSXin LI 	   HASHTABSIZE + 1);
418*aae38d10SBaptiste Daroussin     printf("%s\n", L_BRACE);
41906bfebdeSXin LI     for (n = 0; n < HASHTABSIZE; n++) {
42006bfebdeSXin LI 	printf("\t%3d,\n", hash_table[n]);
42106bfebdeSXin LI     }
42206bfebdeSXin LI     printf("\t0\t/* base-of-table */\n");
423*aae38d10SBaptiste Daroussin     printf("%s;\n\n", R_BRACE);
42406bfebdeSXin LI 
425*aae38d10SBaptiste Daroussin     if (!is_user) {
42606bfebdeSXin LI 	printf("#if (BOOLCOUNT!=%d)||(NUMCOUNT!=%d)||(STRCOUNT!=%d)\n",
42706bfebdeSXin LI 	       BoolCount, NumCount, StrCount);
42806bfebdeSXin LI 	printf("#error\t--> term.h and comp_captab.c disagree about the <--\n");
42906bfebdeSXin LI 	printf("#error\t--> numbers of booleans, numbers and/or strings <--\n");
43006bfebdeSXin LI 	printf("#endif\n\n");
431*aae38d10SBaptiste Daroussin     }
43206bfebdeSXin LI 
43306bfebdeSXin LI     free(hash_table);
434*aae38d10SBaptiste Daroussin     for (n = 0; (n < tablesize); ++n) {
435*aae38d10SBaptiste Daroussin 	free((void *) name_table[n].ute_name);
436*aae38d10SBaptiste Daroussin     }
437*aae38d10SBaptiste Daroussin     free(name_table);
438*aae38d10SBaptiste Daroussin     parse_columns(0);
439*aae38d10SBaptiste Daroussin 
44006bfebdeSXin LI     return EXIT_SUCCESS;
44106bfebdeSXin LI }
442