1#!/bin/sh
2##############################################################################
3# Copyright (c) 2007 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.8 2007/08/12 13:13:51 tom Exp $
30AWK=${1-awk}
31OPT1=${2-0}
32OPT2=${3-tinfo/MKcaptab.awk}
33DATA=${4-../include/Caps}
34
35cat <<'EOF'
36/*
37 *	comp_captab.c -- The names of the capabilities indexed via a hash
38 *		         table for the compiler.
39 *
40 */
41
42#include <curses.priv.h>
43#include <tic.h>
44#include <hashsize.h>
45
46EOF
47
48./make_hash 1 info $OPT1 <$DATA
49./make_hash 3 cap  $OPT1 <$DATA
50
51$AWK -f $OPT2 bigstrings=$OPT1 tablename=capalias <$DATA 
52
53$AWK -f $OPT2 bigstrings=$OPT1 tablename=infoalias <$DATA
54
55cat <<EOF
56
57#if $OPT1
58static void
59next_string(const char *strings, unsigned *offset)
60{
61	*offset += strlen(strings + *offset) + 1;
62}
63
64static const struct name_table_entry *
65_nc_build_names(struct name_table_entry **actual,
66		const name_table_data *source,
67		const char *strings)
68{
69	if (*actual == 0) {
70		*actual = typeCalloc(struct name_table_entry, CAPTABSIZE);
71		if (*actual != 0) {
72			unsigned n;
73			unsigned len = 0;
74			for (n = 0; n < CAPTABSIZE; ++n) {
75				(*actual)[n].nte_name = strings + len;
76				(*actual)[n].nte_type = source[n].nte_type;
77				(*actual)[n].nte_index = source[n].nte_index;
78				(*actual)[n].nte_link = source[n].nte_link;
79				next_string(strings, &len);
80			}
81		}
82	}
83	return *actual;
84}
85
86#define add_alias(field) \\
87	if (source[n].field >= 0) { \\
88		(*actual)[n].field = strings + source[n].field; \\
89	}
90
91static const struct alias *
92_nc_build_alias(struct alias **actual,
93		const alias_table_data *source,
94		const char *strings,
95		unsigned tablesize)
96{
97	if (*actual == 0) {
98		*actual = typeCalloc(struct alias, tablesize + 1);
99		if (*actual != 0) {
100			unsigned n;
101			for (n = 0; n < tablesize; ++n) {
102				add_alias(from);
103				add_alias(to);
104				add_alias(source);
105			}
106		}
107	}
108	return *actual;
109}
110
111#define build_names(root) _nc_build_names(&_nc_##root##_table, \\
112					  root##_names_data, \\
113					  root##_names_text)
114#define build_alias(root) _nc_build_alias(&_nc_##root##alias_table, \\
115					  root##alias_data, \\
116					  root##alias_text, \\
117					  SIZEOF(root##alias_data))
118#else
119#define build_names(root) _nc_ ## root ## _table
120#define build_alias(root) _nc_ ## root ## alias_table
121#endif
122
123NCURSES_EXPORT(const struct name_table_entry *) _nc_get_table (bool termcap)
124{
125	return termcap ? build_names(cap) : build_names(info) ;
126}
127
128NCURSES_EXPORT(const short *) _nc_get_hash_table (bool termcap)
129{
130	return termcap ? _nc_cap_hash_table: _nc_info_hash_table ;
131}
132
133NCURSES_EXPORT(const struct alias *) _nc_get_alias_table (bool termcap)
134{
135	return termcap ? build_alias(cap) : build_alias(info) ;
136}
137
138#if NO_LEAKS
139NCURSES_EXPORT(void) _nc_comp_captab_leaks(void)
140{
141#if $OPT1
142	FreeIfNeeded(_nc_cap_table);
143	FreeIfNeeded(_nc_info_table);
144	FreeIfNeeded(_nc_capalias_table);
145	FreeIfNeeded(_nc_infoalias_table);
146#endif
147}
148#endif /* NO_LEAKS */
149EOF
150