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