1#!/bin/sh
2##############################################################################
3# Copyright (c) 2007-2010,2011 Free Software Foundation, Inc.                #
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: MKcaptab.sh,v 1.14 2011/10/22 16:34:50 tom Exp $
30AWK=${1-awk}
31OPT1=${2-0}
32OPT2=${3-tinfo/MKcaptab.awk}
33DATA=${4-../include/Caps}
34
35cat <<EOF
36/*
37 * generated by $0
38 */
39
40EOF
41
42cat <<'EOF'
43/*
44 *	comp_captab.c -- The names of the capabilities indexed via a hash
45 *		         table for the compiler.
46 *
47 */
48
49#include <curses.priv.h>
50#include <tic.h>
51#include <hashsize.h>
52
53EOF
54
55./make_hash 1 info $OPT1 <$DATA
56./make_hash 3 cap  $OPT1 <$DATA
57
58$AWK -f $OPT2 bigstrings=$OPT1 tablename=capalias <$DATA
59
60$AWK -f $OPT2 bigstrings=$OPT1 tablename=infoalias <$DATA
61
62cat <<EOF
63
64#if $OPT1
65static void
66next_string(const char *strings, unsigned *offset)
67{
68    *offset += (unsigned) strlen(strings + *offset) + 1;
69}
70
71static const struct name_table_entry *
72_nc_build_names(struct name_table_entry **actual,
73		const name_table_data *source,
74		const char *strings)
75{
76    if (*actual == 0) {
77	*actual = typeCalloc(struct name_table_entry, CAPTABSIZE);
78	if (*actual != 0) {
79	    unsigned n;
80	    unsigned len = 0;
81	    for (n = 0; n < CAPTABSIZE; ++n) {
82		(*actual)[n].nte_name = strings + len;
83		(*actual)[n].nte_type = source[n].nte_type;
84		(*actual)[n].nte_index = source[n].nte_index;
85		(*actual)[n].nte_link = source[n].nte_link;
86		next_string(strings, &len);
87	    }
88	}
89    }
90    return *actual;
91}
92
93#define add_alias(field) \\
94	if (source[n].field >= 0) { \\
95		(*actual)[n].field = strings + source[n].field; \\
96	}
97
98static const struct alias *
99_nc_build_alias(struct alias **actual,
100		const alias_table_data *source,
101		const char *strings,
102		size_t tablesize)
103{
104    if (*actual == 0) {
105	*actual = typeCalloc(struct alias, tablesize + 1);
106	if (*actual != 0) {
107	    size_t n;
108	    for (n = 0; n < tablesize; ++n) {
109		add_alias(from);
110		add_alias(to);
111		add_alias(source);
112	    }
113	}
114    }
115    return *actual;
116}
117
118#define build_names(root) _nc_build_names(&_nc_##root##_table, \\
119					  root##_names_data, \\
120					  root##_names_text)
121#define build_alias(root) _nc_build_alias(&_nc_##root##alias_table, \\
122					  root##alias_data, \\
123					  root##alias_text, \\
124					  SIZEOF(root##alias_data))
125#else
126#define build_names(root) _nc_ ## root ## _table
127#define build_alias(root) _nc_ ## root ## alias_table
128#endif
129
130NCURSES_EXPORT(const struct name_table_entry *) _nc_get_table (bool termcap)
131{
132    return termcap ? build_names(cap) : build_names(info) ;
133}
134
135/* entrypoint used by tack (do not alter) */
136NCURSES_EXPORT(const HashValue *) _nc_get_hash_table (bool termcap)
137{
138    return termcap ? _nc_cap_hash_table: _nc_info_hash_table ;
139}
140
141NCURSES_EXPORT(const struct alias *) _nc_get_alias_table (bool termcap)
142{
143    return termcap ? build_alias(cap) : build_alias(info) ;
144}
145
146static HashValue
147info_hash(const char *string)
148{
149    long sum = 0;
150
151    DEBUG(9, ("hashing %s", string));
152    while (*string) {
153	sum += (long) (*string + (*(string + 1) << 8));
154	string++;
155    }
156
157    DEBUG(9, ("sum is %ld", sum));
158    return (HashValue) (sum % HASHTABSIZE);
159}
160
161#define TCAP_LEN 2		/* only 1- or 2-character names are used */
162
163static HashValue
164tcap_hash(const char *string)
165{
166    char temp[TCAP_LEN + 1];
167    int limit = 0;
168
169    while (*string) {
170	temp[limit++] = *string++;
171	if (limit >= TCAP_LEN)
172	    break;
173    }
174    temp[limit] = '\0';
175    return info_hash(temp);
176}
177
178static int
179compare_tcap_names(const char *a, const char *b)
180{
181    return !strncmp(a, b, (size_t) TCAP_LEN);
182}
183
184static int
185compare_info_names(const char *a, const char *b)
186{
187    return !strcmp(a, b);
188}
189
190static const HashData hash_data[2] = {
191    { HASHTABSIZE, _nc_info_hash_table, info_hash, compare_info_names },
192    { HASHTABSIZE, _nc_cap_hash_table, tcap_hash, compare_tcap_names }
193};
194
195NCURSES_EXPORT(const HashData *) _nc_get_hash_info (bool termcap)
196{
197    return &hash_data[(termcap != FALSE)];
198}
199
200#if NO_LEAKS
201NCURSES_EXPORT(void) _nc_comp_captab_leaks(void)
202{
203#if $OPT1
204    FreeIfNeeded(_nc_cap_table);
205    FreeIfNeeded(_nc_info_table);
206    FreeIfNeeded(_nc_capalias_table);
207    FreeIfNeeded(_nc_infoalias_table);
208#endif
209}
210#endif /* NO_LEAKS */
211EOF
212