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