xref: /openbsd-src/lib/libcurses/tinfo/MKcaptab.sh (revision f2da64fbbbf1b03f09f390ab01267c93dfd77c4c)
1#!/bin/sh
2# $OpenBSD: MKcaptab.sh,v 1.1 2010/01/12 23:22:06 nicm Exp $
3##############################################################################
4# Copyright (c) 2007 Free Software Foundation, Inc.                          #
5#                                                                            #
6# Permission is hereby granted, free of charge, to any person obtaining a    #
7# copy of this software and associated documentation files (the "Software"), #
8# to deal in the Software without restriction, including without limitation  #
9# the rights to use, copy, modify, merge, publish, distribute, distribute    #
10# with modifications, sublicense, and/or sell copies of the Software, and to #
11# permit persons to whom the Software is furnished to do so, subject to the  #
12# following conditions:                                                      #
13#                                                                            #
14# The above copyright notice and this permission notice shall be included in #
15# all copies or substantial portions of the Software.                        #
16#                                                                            #
17# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR #
18# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,   #
19# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL    #
20# THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER      #
21# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING    #
22# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER        #
23# DEALINGS IN THE SOFTWARE.                                                  #
24#                                                                            #
25# Except as contained in this notice, the name(s) of the above copyright     #
26# holders shall not be used in advertising or otherwise to promote the sale, #
27# use or other dealings in this Software without prior written               #
28# authorization.                                                             #
29##############################################################################
30# $Id: MKcaptab.sh,v 1.1 2010/01/12 23:22:06 nicm Exp $
31AWK=${1-awk}
32OPT1=${2-0}
33OPT2=${3-tinfo/MKcaptab.awk}
34DATA=${4-../include/Caps}
35
36cat <<'EOF'
37/*
38 *	comp_captab.c -- The names of the capabilities indexed via a hash
39 *		         table for the compiler.
40 *
41 */
42
43#include <curses.priv.h>
44#include <tic.h>
45#include <hashsize.h>
46
47EOF
48
49./make_hash 1 info $OPT1 <$DATA
50./make_hash 3 cap  $OPT1 <$DATA
51
52$AWK -f $OPT2 bigstrings=$OPT1 tablename=capalias <$DATA
53
54$AWK -f $OPT2 bigstrings=$OPT1 tablename=infoalias <$DATA
55
56cat <<EOF
57
58#if $OPT1
59static void
60next_string(const char *strings, unsigned *offset)
61{
62	*offset += strlen(strings + *offset) + 1;
63}
64
65static const struct name_table_entry *
66_nc_build_names(struct name_table_entry **actual,
67		const name_table_data *source,
68		const char *strings)
69{
70	if (*actual == 0) {
71		*actual = typeCalloc(struct name_table_entry, CAPTABSIZE);
72		if (*actual != 0) {
73			unsigned n;
74			unsigned len = 0;
75			for (n = 0; n < CAPTABSIZE; ++n) {
76				(*actual)[n].nte_name = strings + len;
77				(*actual)[n].nte_type = source[n].nte_type;
78				(*actual)[n].nte_index = source[n].nte_index;
79				(*actual)[n].nte_link = source[n].nte_link;
80				next_string(strings, &len);
81			}
82		}
83	}
84	return *actual;
85}
86
87#define add_alias(field) \\
88	if (source[n].field >= 0) { \\
89		(*actual)[n].field = strings + source[n].field; \\
90	}
91
92static const struct alias *
93_nc_build_alias(struct alias **actual,
94		const alias_table_data *source,
95		const char *strings,
96		unsigned tablesize)
97{
98	if (*actual == 0) {
99		*actual = typeCalloc(struct alias, tablesize + 1);
100		if (*actual != 0) {
101			unsigned n;
102			for (n = 0; n < tablesize; ++n) {
103				add_alias(from);
104				add_alias(to);
105				add_alias(source);
106			}
107		}
108	}
109	return *actual;
110}
111
112#define build_names(root) _nc_build_names(&_nc_##root##_table, \\
113					  root##_names_data, \\
114					  root##_names_text)
115#define build_alias(root) _nc_build_alias(&_nc_##root##alias_table, \\
116					  root##alias_data, \\
117					  root##alias_text, \\
118					  SIZEOF(root##alias_data))
119#else
120#define build_names(root) _nc_ ## root ## _table
121#define build_alias(root) _nc_ ## root ## alias_table
122#endif
123
124NCURSES_EXPORT(const struct name_table_entry *) _nc_get_table (bool termcap)
125{
126	return termcap ? build_names(cap) : build_names(info) ;
127}
128
129NCURSES_EXPORT(const short *) _nc_get_hash_table (bool termcap)
130{
131	return termcap ? _nc_cap_hash_table: _nc_info_hash_table ;
132}
133
134NCURSES_EXPORT(const struct alias *) _nc_get_alias_table (bool termcap)
135{
136	return termcap ? build_alias(cap) : build_alias(info) ;
137}
138
139#if NO_LEAKS
140NCURSES_EXPORT(void) _nc_comp_captab_leaks(void)
141{
142#if $OPT1
143	FreeIfNeeded(_nc_cap_table);
144	FreeIfNeeded(_nc_info_table);
145	FreeIfNeeded(_nc_capalias_table);
146	FreeIfNeeded(_nc_infoalias_table);
147#endif
148}
149#endif /* NO_LEAKS */
150EOF
151