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