1# $OpenBSD: MKcaptab.sh,v 1.2 2023/10/17 09:52:09 nicm Exp $ 2#!/bin/sh 3############################################################################## 4# Copyright 2019-2020,2023 Thomas E. Dickey # 5# Copyright 2007-2010,2011 Free Software Foundation, Inc. # 6# # 7# Permission is hereby granted, free of charge, to any person obtaining a # 8# copy of this software and associated documentation files (the "Software"), # 9# to deal in the Software without restriction, including without limitation # 10# the rights to use, copy, modify, merge, publish, distribute, distribute # 11# with modifications, sublicense, and/or sell copies of the Software, and to # 12# permit persons to whom the Software is furnished to do so, subject to the # 13# following conditions: # 14# # 15# The above copyright notice and this permission notice shall be included in # 16# all copies or substantial portions of the Software. # 17# # 18# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # 19# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # 20# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL # 21# THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # 22# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING # 23# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER # 24# DEALINGS IN THE SOFTWARE. # 25# # 26# Except as contained in this notice, the name(s) of the above copyright # 27# holders shall not be used in advertising or otherwise to promote the sale, # 28# use or other dealings in this Software without prior written # 29# authorization. # 30############################################################################## 31# $Id: MKcaptab.sh,v 1.2 2023/10/17 09:52:09 nicm Exp $ 32 33if test $# != 0 34then 35 AWK="$1"; shift 1 36else 37 AWK=awk 38fi 39 40if test $# != 0 41then 42 OPT1="$1"; shift 1 43else 44 OPT1="-0" 45fi 46 47if test $# != 0 48then 49 OPT2="$1"; shift 1 50else 51 OPT2="tinfo/MKcaptab.awk" 52fi 53 54cat <<EOF 55/* 56 * generated by $0 57 */ 58 59EOF 60 61cat <<'EOF' 62/* 63 * comp_captab.c -- The names of the capabilities indexed via a hash 64 * table for the compiler. 65 * 66 */ 67 68#include <curses.priv.h> 69#include <tic.h> 70#include <hashsize.h> 71 72/* *INDENT-OFF* */ 73EOF 74 75cat "$@" |./make_hash 1 info $OPT1 76cat "$@" |./make_hash 3 cap $OPT1 77 78cat "$@" |$AWK -f $OPT2 bigstrings=$OPT1 tablename=capalias 79 80cat "$@" |$AWK -f $OPT2 bigstrings=$OPT1 tablename=infoalias 81 82cat <<EOF 83/* *INDENT-ON* */ 84 85#if $OPT1 86static void 87next_string(const char *strings, unsigned *offset) 88{ 89 *offset += (unsigned) strlen(strings + *offset) + 1; 90} 91 92static const struct name_table_entry * 93_nc_build_names(struct name_table_entry **actual, 94 const name_table_data * source, 95 const char *strings) 96{ 97 if (*actual == 0) { 98 *actual = typeCalloc(struct name_table_entry, CAPTABSIZE); 99 if (*actual != 0) { 100 unsigned n; 101 unsigned len = 0; 102 for (n = 0; n < CAPTABSIZE; ++n) { 103 (*actual)[n].nte_name = strings + len; 104 (*actual)[n].nte_type = source[n].nte_type; 105 (*actual)[n].nte_index = source[n].nte_index; 106 (*actual)[n].nte_link = source[n].nte_link; 107 next_string(strings, &len); 108 } 109 } 110 } 111 return *actual; 112} 113 114#define add_alias(field) \\ 115 if (source[n].field >= 0) { \\ 116 (*actual)[n].field = strings + source[n].field; \\ 117 } 118 119static const struct alias * 120_nc_build_alias(struct alias **actual, 121 const alias_table_data * source, 122 const char *strings, 123 size_t tablesize) 124{ 125 if (*actual == 0) { 126 *actual = typeCalloc(struct alias, tablesize + 1); 127 if (*actual != 0) { 128 size_t n; 129 for (n = 0; n < tablesize; ++n) { 130 add_alias(from); 131 add_alias(to); 132 add_alias(source); 133 } 134 } 135 } 136 return *actual; 137} 138 139#define build_names(root) _nc_build_names(&_nc_##root##_table, \\ 140 root##_names_data, \\ 141 root##_names_text) 142#define build_alias(root) _nc_build_alias(&_nc_##root##alias_table, \\ 143 root##alias_data, \\ 144 root##alias_text, \\ 145 SIZEOF(root##alias_data)) 146#else 147#define build_names(root) _nc_ ## root ## _table 148#define build_alias(root) _nc_ ## root ## alias_table 149#endif 150 151NCURSES_EXPORT(const struct name_table_entry *) 152_nc_get_table(bool termcap) 153{ 154 return termcap ? build_names(cap) : build_names(info); 155} 156 157NCURSES_EXPORT(const HashValue *) 158_nc_get_hash_table(bool termcap) 159{ 160 return termcap ? _nc_cap_hash_table : _nc_info_hash_table; 161} 162 163NCURSES_EXPORT(const struct alias *) 164_nc_get_alias_table(bool termcap) 165{ 166 return termcap ? build_alias(cap) : build_alias(info); 167} 168 169static HashValue 170info_hash(const char *string) 171{ 172 long sum = 0; 173 174 DEBUG(9, ("hashing %s", string)); 175 while (*string) { 176 sum += (long) (UChar(*string) + (UChar(*(string + 1)) << 8)); 177 string++; 178 } 179 180 DEBUG(9, ("sum is %ld", sum)); 181 return (HashValue) (sum % HASHTABSIZE); 182} 183 184#define TCAP_LEN 2 /* only 1- or 2-character names are used */ 185 186static HashValue 187tcap_hash(const char *string) 188{ 189 char temp[TCAP_LEN + 1]; 190 int limit = 0; 191 192 while (*string) { 193 temp[limit++] = *string++; 194 if (limit >= TCAP_LEN) 195 break; 196 } 197 temp[limit] = '\0'; 198 return info_hash(temp); 199} 200 201static int 202compare_tcap_names(const char *a, const char *b) 203{ 204 return !strncmp(a, b, (size_t) TCAP_LEN); 205} 206 207static int 208compare_info_names(const char *a, const char *b) 209{ 210 return !strcmp(a, b); 211} 212 213static const HashData hash_data[2] = 214{ 215 {HASHTABSIZE, _nc_info_hash_table, info_hash, compare_info_names}, 216 {HASHTABSIZE, _nc_cap_hash_table, tcap_hash, compare_tcap_names} 217}; 218 219NCURSES_EXPORT(const HashData *) 220_nc_get_hash_info(bool termcap) 221{ 222 return &hash_data[(termcap != FALSE)]; 223} 224 225#if NO_LEAKS 226NCURSES_EXPORT(void) 227_nc_comp_captab_leaks(void) 228{ 229#if $OPT1 230 FreeIfNeeded(_nc_cap_table); 231 FreeIfNeeded(_nc_info_table); 232 FreeIfNeeded(_nc_capalias_table); 233 FreeIfNeeded(_nc_infoalias_table); 234#endif 235} 236#endif /* NO_LEAKS */ 237EOF 238