micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 1 | # $Id: MKkeyname.awk,v 1.51 2020/02/02 23:34:34 tom Exp $ |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 2 | ############################################################################## |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 3 | # Copyright 2020 Thomas E. Dickey # |
| 4 | # Copyright 1998-2016,2017 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 | ############################################################################## |
| 30 | BEGIN { |
| 31 | print "/* generated by MKkeyname.awk */" |
| 32 | print "" |
| 33 | print "#include <curses.priv.h>" |
| 34 | print "#include <tic.h>" |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 35 | print "" |
| 36 | first = 1; |
| 37 | } |
| 38 | |
| 39 | /^[^#]/ { |
| 40 | if (bigstrings) { |
| 41 | if (first) { |
| 42 | print "struct kn { short offset; int code; };" |
| 43 | print "static const struct kn _nc_key_names[] = {" |
| 44 | } |
| 45 | printf "\t{ %d, %s },\n", offset, $1 |
| 46 | offset += length($1) + 1 |
| 47 | names = names"\n\t\""$1"\\0\"" |
| 48 | } else { |
| 49 | if (first) { |
| 50 | print "struct kn { const char *name; int code; };" |
| 51 | print "static const struct kn _nc_key_names[] = {" |
| 52 | } |
| 53 | printf "\t{ \"%s\", %s },\n", $1, $1; |
| 54 | } |
| 55 | first = 0; |
| 56 | } |
| 57 | |
| 58 | END { |
| 59 | if (bigstrings) { |
| 60 | printf "\t{ -1, 0 }};\n" |
| 61 | print "" |
| 62 | print "static const char key_names[] = "names";" |
| 63 | } else { |
| 64 | printf "\t{ 0, 0 }};\n" |
| 65 | } |
| 66 | print "" |
| 67 | print "#define SIZEOF_TABLE 256" |
| 68 | print "#define MyTable _nc_globals.keyname_table" |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 69 | print "#define MyInit _nc_globals.init_keyname" |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 70 | print "" |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 71 | print "NCURSES_EXPORT(NCURSES_CONST char *)" |
| 72 | print "safe_keyname (SCREEN *sp, int c)" |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 73 | print "{" |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 74 | print " char name[20];" |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 75 | print " NCURSES_CONST char *result = 0;" |
| 76 | print "" |
| 77 | print " if (c == -1) {" |
| 78 | print " result = \"-1\";" |
| 79 | print " } else {" |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 80 | print " int i;" |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 81 | if (bigstrings) { |
| 82 | print " for (i = 0; _nc_key_names[i].offset != -1; i++) {" |
| 83 | print " if (_nc_key_names[i].code == c) {" |
| 84 | print " result = (NCURSES_CONST char *)key_names + _nc_key_names[i].offset;" |
| 85 | print " break;" |
| 86 | print " }" |
| 87 | print " }" |
| 88 | } else { |
| 89 | print " for (i = 0; _nc_key_names[i].name != 0; i++) {" |
| 90 | print " if (_nc_key_names[i].code == c) {" |
| 91 | print " result = (NCURSES_CONST char *)_nc_key_names[i].name;" |
| 92 | print " break;" |
| 93 | print " }" |
| 94 | print " }" |
| 95 | } |
| 96 | print "" |
| 97 | print " if (result == 0 && (c >= 0 && c < SIZEOF_TABLE)) {" |
| 98 | print " if (MyTable == 0)" |
| 99 | print " MyTable = typeCalloc(char *, SIZEOF_TABLE);" |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 100 | print "" |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 101 | print " if (MyTable != 0) {" |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 102 | print " int m_prefix = (sp == 0 || sp->_use_meta);" |
| 103 | print "" |
| 104 | print " /* if sense of meta() changed, discard cached data */" |
| 105 | print " if (MyInit != (m_prefix + 1)) {" |
| 106 | print " MyInit = m_prefix + 1;" |
| 107 | print " for (i = 0; i < SIZEOF_TABLE; ++i) {" |
| 108 | print " if (MyTable[i]) {" |
| 109 | print " FreeAndNull(MyTable[i]);" |
| 110 | print " }" |
| 111 | print " }" |
| 112 | print " }" |
| 113 | print "" |
| 114 | print " /* create and cache result as needed */" |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 115 | print " if (MyTable[c] == 0) {" |
| 116 | print " int cc = c;" |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 117 | print " char *p = name;" |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 118 | print "#define P_LIMIT (sizeof(name) - (size_t) (p - name))" |
| 119 | print " if (cc >= 128 && m_prefix) {" |
| 120 | print " _nc_STRCPY(p, \"M-\", P_LIMIT);" |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 121 | print " p += 2;" |
| 122 | print " cc -= 128;" |
| 123 | print " }" |
| 124 | print " if (cc < 32)" |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 125 | print " _nc_SPRINTF(p, _nc_SLIMIT(P_LIMIT) \"^%c\", cc + '@');" |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 126 | print " else if (cc == 127)" |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 127 | print " _nc_STRCPY(p, \"^?\", P_LIMIT);" |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 128 | print " else" |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 129 | print " _nc_SPRINTF(p, _nc_SLIMIT(P_LIMIT) \"%c\", cc);" |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 130 | print " MyTable[c] = strdup(name);" |
| 131 | print " }" |
| 132 | print " result = MyTable[c];" |
| 133 | print " }" |
| 134 | print "#if NCURSES_EXT_FUNCS && NCURSES_XNAMES" |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 135 | print " } else if (result == 0 && HasTerminal(sp)) {" |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 136 | print " int j, k;" |
| 137 | print " char * bound;" |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 138 | print " TERMTYPE2 *tp = &TerminalType(TerminalOf(sp));" |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 139 | print " unsigned save_trace = _nc_tracing;" |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 140 | print "" |
| 141 | print " _nc_tracing = 0; /* prevent recursion via keybound() */" |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 142 | print " for (j = 0; (bound = NCURSES_SP_NAME(keybound)(NCURSES_SP_ARGx c, j)) != 0; ++j) {" |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 143 | print " for(k = STRCOUNT; k < (int) NUM_STRINGS(tp); k++) {" |
| 144 | print " if (tp->Strings[k] != 0 && !strcmp(bound, tp->Strings[k])) {" |
| 145 | print " result = ExtStrname(tp, k, strnames);" |
| 146 | print " break;" |
| 147 | print " }" |
| 148 | print " }" |
| 149 | print " free(bound);" |
| 150 | print " if (result != 0)" |
| 151 | print " break;" |
| 152 | print " }" |
| 153 | print " _nc_tracing = save_trace;" |
| 154 | print "#endif" |
| 155 | print " }" |
| 156 | print " }" |
| 157 | print " return result;" |
| 158 | print "}" |
| 159 | print "" |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 160 | print "NCURSES_EXPORT(NCURSES_CONST char *)" |
| 161 | print "keyname (int c)" |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 162 | print "{" |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 163 | print " return safe_keyname (CURRENT_SCREEN, c);" |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 164 | print "}" |
| 165 | print "" |
| 166 | print "#if NO_LEAKS" |
| 167 | print "void _nc_keyname_leaks(void)" |
| 168 | print "{" |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 169 | print " if (MyTable != 0) {" |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 170 | print " int j;" |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 171 | print " for (j = 0; j < SIZEOF_TABLE; ++j) {" |
| 172 | print " FreeIfNeeded(MyTable[j]);" |
| 173 | print " }" |
| 174 | print " FreeAndNull(MyTable);" |
| 175 | print " }" |
| 176 | print "}" |
| 177 | print "#endif /* NO_LEAKS */" |
| 178 | } |