1#!/bin/sh
2##############################################################################
3# Copyright 2019,2020 Thomas E. Dickey                                       #
4#                                                                            #
5# Permission is hereby granted, free of charge, to any person obtaining a    #
6# copy of this software and associated documentation files (the "Software"), #
7# to deal in the Software without restriction, including without limitation  #
8# the rights to use, copy, modify, merge, publish, distribute, distribute    #
9# with modifications, sublicense, and/or sell copies of the Software, and to #
10# permit persons to whom the Software is furnished to do so, subject to the  #
11# following conditions:                                                      #
12#                                                                            #
13# The above copyright notice and this permission notice shall be included in #
14# all copies or substantial portions of the Software.                        #
15#                                                                            #
16# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR #
17# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,   #
18# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL    #
19# THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER      #
20# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING    #
21# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER        #
22# DEALINGS IN THE SOFTWARE.                                                  #
23#                                                                            #
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 sale, #
26# use or other dealings in this Software without prior written               #
27# authorization.                                                             #
28##############################################################################
29# $Id: MKuserdefs.sh,v 1.10 2020/02/02 23:34:34 tom Exp $
30AWK=${1-awk}; shift 1
31OPT1=${1-0}; shift 1
32
33cat <<EOF
34/*
35 * generated by $0
36 */
37
38EOF
39
40cat <<'EOF'
41/*
42 *    comp_userdefs.c -- The names of widely used user-defined capabilities
43 *                       indexed via a hash table for the compiler.
44 *
45 */
46
47#include <curses.priv.h>
48#include <tic.h>
49#include <hashsize.h>
50
51#if NCURSES_XNAMES
52EOF
53
54cat "$@" | ./make_hash 1 user $OPT1
55
56cat <<EOF
57
58#define USERTABSIZE SIZEOF(user_names_data)
59
60#if $OPT1
61static void
62next_string(const char *strings, unsigned *offset)
63{
64    *offset += (unsigned) strlen(strings + *offset) + 1;
65}
66
67static const struct user_table_entry *
68_nc_build_names(struct user_table_entry **actual,
69		const user_table_data *source,
70		const char *strings)
71{
72    if (*actual == 0) {
73	*actual = typeCalloc(struct user_table_entry, USERTABSIZE);
74	if (*actual != 0) {
75	    unsigned n;
76	    unsigned len = 0;
77	    for (n = 0; n < USERTABSIZE; ++n) {
78		(*actual)[n].ute_name = strings + len;
79		(*actual)[n].ute_type = (int) source[n].ute_type;
80		(*actual)[n].ute_argc = source[n].ute_argc;
81		(*actual)[n].ute_args = source[n].ute_args;
82		(*actual)[n].ute_index = source[n].ute_index;
83		(*actual)[n].ute_link = source[n].ute_link;
84		next_string(strings, &len);
85	    }
86	}
87    }
88    return *actual;
89}
90
91#define build_names(root) _nc_build_names(&_nc_##root##_table, \\
92					  root##_names_data, \\
93					  root##_names_text)
94#else
95#define build_names(root) _nc_ ## root ## _table
96#endif
97
98NCURSES_EXPORT(const struct user_table_entry *) _nc_get_userdefs_table (void)
99{
100    return build_names(user) ;
101}
102
103static HashValue
104info_hash(const char *string)
105{
106    long sum = 0;
107
108    DEBUG(9, ("hashing %s", string));
109    while (*string) {
110	sum += (long) (*string + (*(string + 1) << 8));
111	string++;
112    }
113
114    DEBUG(9, ("sum is %ld", sum));
115    return (HashValue) (sum % HASHTABSIZE);
116}
117
118static int
119compare_info_names(const char *a, const char *b)
120{
121    return !strcmp(a, b);
122}
123
124static const HashData hash_data[] = {
125    { HASHTABSIZE, _nc_user_hash_table, info_hash, compare_info_names }
126};
127
128NCURSES_EXPORT(const HashData *) _nc_get_hash_user (void)
129{
130    return hash_data;
131}
132
133#if NO_LEAKS
134NCURSES_EXPORT(void) _nc_comp_userdefs_leaks(void)
135{
136#if $OPT1
137    FreeIfNeeded(_nc_user_table);
138#endif
139}
140#endif /* NO_LEAKS */
141
142#else /*! NCURSES_XNAMES */
143NCURSES_EXPORT(void) _nc_comp_userdefs(void);
144NCURSES_EXPORT(void) _nc_comp_userdefs(void) { }
145#endif /* NCURSES_XNAMES */
146EOF
147