1174993Srafan#!/bin/sh
2174993Srafan##############################################################################
3262685Sdelphij# Copyright (c) 2007-2010,2011 Free Software Foundation, Inc.                #
4174993Srafan#                                                                            #
5174993Srafan# Permission is hereby granted, free of charge, to any person obtaining a    #
6174993Srafan# copy of this software and associated documentation files (the "Software"), #
7174993Srafan# to deal in the Software without restriction, including without limitation  #
8174993Srafan# the rights to use, copy, modify, merge, publish, distribute, distribute    #
9174993Srafan# with modifications, sublicense, and/or sell copies of the Software, and to #
10174993Srafan# permit persons to whom the Software is furnished to do so, subject to the  #
11174993Srafan# following conditions:                                                      #
12174993Srafan#                                                                            #
13174993Srafan# The above copyright notice and this permission notice shall be included in #
14174993Srafan# all copies or substantial portions of the Software.                        #
15174993Srafan#                                                                            #
16174993Srafan# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR #
17174993Srafan# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,   #
18174993Srafan# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL    #
19174993Srafan# THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER      #
20174993Srafan# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING    #
21174993Srafan# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER        #
22174993Srafan# DEALINGS IN THE SOFTWARE.                                                  #
23174993Srafan#                                                                            #
24174993Srafan# Except as contained in this notice, the name(s) of the above copyright     #
25174993Srafan# holders shall not be used in advertising or otherwise to promote the sale, #
26174993Srafan# use or other dealings in this Software without prior written               #
27174993Srafan# authorization.                                                             #
28174993Srafan##############################################################################
29262685Sdelphij# $Id: MKcaptab.sh,v 1.14 2011/10/22 16:34:50 tom Exp $
30174993SrafanAWK=${1-awk}
31174993SrafanOPT1=${2-0}
32174993SrafanOPT2=${3-tinfo/MKcaptab.awk}
33174993SrafanDATA=${4-../include/Caps}
34174993Srafan
35262629Sdelphijcat <<EOF
36262629Sdelphij/*
37262629Sdelphij * generated by $0
38262629Sdelphij */
39262629Sdelphij
40262629SdelphijEOF
41262629Sdelphij
42174993Srafancat <<'EOF'
43174993Srafan/*
44174993Srafan *	comp_captab.c -- The names of the capabilities indexed via a hash
45174993Srafan *		         table for the compiler.
46174993Srafan *
47174993Srafan */
48174993Srafan
49174993Srafan#include <curses.priv.h>
50174993Srafan#include <tic.h>
51174993Srafan#include <hashsize.h>
52174993Srafan
53174993SrafanEOF
54174993Srafan
55299289Sbdrewerymake_hash 1 info $OPT1 <$DATA
56299289Sbdrewerymake_hash 3 cap  $OPT1 <$DATA
57174993Srafan
58262629Sdelphij$AWK -f $OPT2 bigstrings=$OPT1 tablename=capalias <$DATA
59174993Srafan
60174993Srafan$AWK -f $OPT2 bigstrings=$OPT1 tablename=infoalias <$DATA
61174993Srafan
62174993Srafancat <<EOF
63174993Srafan
64174993Srafan#if $OPT1
65174993Srafanstatic void
66174993Srafannext_string(const char *strings, unsigned *offset)
67174993Srafan{
68262629Sdelphij    *offset += (unsigned) strlen(strings + *offset) + 1;
69174993Srafan}
70174993Srafan
71174993Srafanstatic const struct name_table_entry *
72174993Srafan_nc_build_names(struct name_table_entry **actual,
73174993Srafan		const name_table_data *source,
74174993Srafan		const char *strings)
75174993Srafan{
76262629Sdelphij    if (*actual == 0) {
77262629Sdelphij	*actual = typeCalloc(struct name_table_entry, CAPTABSIZE);
78262629Sdelphij	if (*actual != 0) {
79262629Sdelphij	    unsigned n;
80262629Sdelphij	    unsigned len = 0;
81262629Sdelphij	    for (n = 0; n < CAPTABSIZE; ++n) {
82262629Sdelphij		(*actual)[n].nte_name = strings + len;
83262629Sdelphij		(*actual)[n].nte_type = source[n].nte_type;
84262629Sdelphij		(*actual)[n].nte_index = source[n].nte_index;
85262629Sdelphij		(*actual)[n].nte_link = source[n].nte_link;
86262629Sdelphij		next_string(strings, &len);
87262629Sdelphij	    }
88174993Srafan	}
89262629Sdelphij    }
90262629Sdelphij    return *actual;
91174993Srafan}
92174993Srafan
93174993Srafan#define add_alias(field) \\
94174993Srafan	if (source[n].field >= 0) { \\
95174993Srafan		(*actual)[n].field = strings + source[n].field; \\
96174993Srafan	}
97174993Srafan
98174993Srafanstatic const struct alias *
99174993Srafan_nc_build_alias(struct alias **actual,
100174993Srafan		const alias_table_data *source,
101174993Srafan		const char *strings,
102262685Sdelphij		size_t tablesize)
103174993Srafan{
104262629Sdelphij    if (*actual == 0) {
105262629Sdelphij	*actual = typeCalloc(struct alias, tablesize + 1);
106262629Sdelphij	if (*actual != 0) {
107262685Sdelphij	    size_t n;
108262629Sdelphij	    for (n = 0; n < tablesize; ++n) {
109262629Sdelphij		add_alias(from);
110262629Sdelphij		add_alias(to);
111262629Sdelphij		add_alias(source);
112262629Sdelphij	    }
113174993Srafan	}
114262629Sdelphij    }
115262629Sdelphij    return *actual;
116174993Srafan}
117174993Srafan
118174993Srafan#define build_names(root) _nc_build_names(&_nc_##root##_table, \\
119174993Srafan					  root##_names_data, \\
120174993Srafan					  root##_names_text)
121174993Srafan#define build_alias(root) _nc_build_alias(&_nc_##root##alias_table, \\
122174993Srafan					  root##alias_data, \\
123174993Srafan					  root##alias_text, \\
124174993Srafan					  SIZEOF(root##alias_data))
125174993Srafan#else
126174993Srafan#define build_names(root) _nc_ ## root ## _table
127174993Srafan#define build_alias(root) _nc_ ## root ## alias_table
128174993Srafan#endif
129174993Srafan
130174993SrafanNCURSES_EXPORT(const struct name_table_entry *) _nc_get_table (bool termcap)
131174993Srafan{
132262629Sdelphij    return termcap ? build_names(cap) : build_names(info) ;
133174993Srafan}
134174993Srafan
135262629Sdelphij/* entrypoint used by tack (do not alter) */
136262629SdelphijNCURSES_EXPORT(const HashValue *) _nc_get_hash_table (bool termcap)
137174993Srafan{
138262629Sdelphij    return termcap ? _nc_cap_hash_table: _nc_info_hash_table ;
139174993Srafan}
140174993Srafan
141174993SrafanNCURSES_EXPORT(const struct alias *) _nc_get_alias_table (bool termcap)
142174993Srafan{
143262629Sdelphij    return termcap ? build_alias(cap) : build_alias(info) ;
144174993Srafan}
145174993Srafan
146262629Sdelphijstatic HashValue
147262629Sdelphijinfo_hash(const char *string)
148262629Sdelphij{
149262629Sdelphij    long sum = 0;
150262629Sdelphij
151262629Sdelphij    DEBUG(9, ("hashing %s", string));
152262629Sdelphij    while (*string) {
153262629Sdelphij	sum += (long) (*string + (*(string + 1) << 8));
154262629Sdelphij	string++;
155262629Sdelphij    }
156262629Sdelphij
157262629Sdelphij    DEBUG(9, ("sum is %ld", sum));
158262629Sdelphij    return (HashValue) (sum % HASHTABSIZE);
159262629Sdelphij}
160262629Sdelphij
161262629Sdelphij#define TCAP_LEN 2		/* only 1- or 2-character names are used */
162262629Sdelphij
163262629Sdelphijstatic HashValue
164262629Sdelphijtcap_hash(const char *string)
165262629Sdelphij{
166262629Sdelphij    char temp[TCAP_LEN + 1];
167262629Sdelphij    int limit = 0;
168262629Sdelphij
169262629Sdelphij    while (*string) {
170262629Sdelphij	temp[limit++] = *string++;
171262629Sdelphij	if (limit >= TCAP_LEN)
172262629Sdelphij	    break;
173262629Sdelphij    }
174262629Sdelphij    temp[limit] = '\0';
175262629Sdelphij    return info_hash(temp);
176262629Sdelphij}
177262629Sdelphij
178262629Sdelphijstatic int
179262629Sdelphijcompare_tcap_names(const char *a, const char *b)
180262629Sdelphij{
181262685Sdelphij    return !strncmp(a, b, (size_t) TCAP_LEN);
182262629Sdelphij}
183262629Sdelphij
184262629Sdelphijstatic int
185262629Sdelphijcompare_info_names(const char *a, const char *b)
186262629Sdelphij{
187262629Sdelphij    return !strcmp(a, b);
188262629Sdelphij}
189262629Sdelphij
190262629Sdelphijstatic const HashData hash_data[2] = {
191262629Sdelphij    { HASHTABSIZE, _nc_info_hash_table, info_hash, compare_info_names },
192262629Sdelphij    { HASHTABSIZE, _nc_cap_hash_table, tcap_hash, compare_tcap_names }
193262629Sdelphij};
194262629Sdelphij
195262629SdelphijNCURSES_EXPORT(const HashData *) _nc_get_hash_info (bool termcap)
196262629Sdelphij{
197262629Sdelphij    return &hash_data[(termcap != FALSE)];
198262629Sdelphij}
199262629Sdelphij
200174993Srafan#if NO_LEAKS
201174993SrafanNCURSES_EXPORT(void) _nc_comp_captab_leaks(void)
202174993Srafan{
203174993Srafan#if $OPT1
204262629Sdelphij    FreeIfNeeded(_nc_cap_table);
205262629Sdelphij    FreeIfNeeded(_nc_info_table);
206262629Sdelphij    FreeIfNeeded(_nc_capalias_table);
207262629Sdelphij    FreeIfNeeded(_nc_infoalias_table);
208174993Srafan#endif
209174993Srafan}
210174993Srafan#endif /* NO_LEAKS */
211174993SrafanEOF
212