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