e487038ddafdf27f2c4e44cd3423c084d5def3ab
[ncurses.git] / ncurses / base / MKkeyname.awk
1 # $Id: MKkeyname.awk,v 1.43 2009/05/09 19:01:49 tom Exp $
2 ##############################################################################
3 # Copyright (c) 1999-2008,2009 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 "#include <term_entry.h>"
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"
69         print ""
70         print "NCURSES_EXPORT(NCURSES_CONST char *)"
71         print "safe_keyname (SCREEN *sp, int c)"
72         print "{"
73         print " int i;"
74         print " char name[20];"
75         print " char *p;"
76         print " NCURSES_CONST char *result = 0;"
77         print ""
78         print " if (c == -1) {"
79         print "         result = \"-1\";"
80         print " } else {"
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);"
100         print "                 if (MyTable != 0) {"
101         print "                         if (MyTable[c] == 0) {"
102         print "                                 int cc = c;"
103         print "                                 p = name;"
104         print "                                 if (cc >= 128 && (sp == 0 || sp->_use_meta)) {"
105         print "                                         strcpy(p, \"M-\");"
106         print "                                         p += 2;"
107         print "                                         cc -= 128;"
108         print "                                 }"
109         print "                                 if (cc < 32)"
110         print "                                         sprintf(p, \"^%c\", cc + '@');"
111         print "                                 else if (cc == 127)"
112         print "                                         strcpy(p, \"^?\");"
113         print "                                 else"
114         print "                                         sprintf(p, \"%c\", cc);"
115         print "                                 MyTable[c] = strdup(name);"
116         print "                         }"
117         print "                         result = MyTable[c];"
118         print "                 }"
119         print "#if NCURSES_EXT_FUNCS && NCURSES_XNAMES"
120         print "         } else if (result == 0 && HasTerminal(sp)) {"
121         print "                 int j, k;"
122         print "                 char * bound;"
123         print "                 TERMTYPE *tp = &(TerminalOf(sp)->type);"
124         print "                 int save_trace = _nc_tracing;"
125         print ""
126         print "                 _nc_tracing = 0;        /* prevent recursion via keybound() */"
127         print "                 for (j = 0; (bound = NCURSES_SP_NAME(keybound)(NCURSES_SP_ARGx c, j)) != 0; ++j) {"
128         print "                         for(k = STRCOUNT; k < (int) NUM_STRINGS(tp);  k++) {"
129         print "                                 if (tp->Strings[k] != 0 && !strcmp(bound, tp->Strings[k])) {"
130         print "                                         result = ExtStrname(tp, k, strnames);"
131         print "                                         break;"
132         print "                                 }"
133         print "                         }"
134         print "                         free(bound);"
135         print "                         if (result != 0)"
136         print "                                 break;"
137         print "                 }"
138         print "                 _nc_tracing = save_trace;"
139         print "#endif"
140         print "         }"
141         print " }"
142         print " return result;"
143         print "}"
144         print ""
145         print "NCURSES_EXPORT(NCURSES_CONST char *)"
146         print "keyname (int c)"
147         print "{"
148         print " return safe_keyname (CURRENT_SCREEN, c);"
149         print "}"
150         print ""
151         print "#if NO_LEAKS"
152         print "void _nc_keyname_leaks(void)"
153         print "{"
154         print " int j;"
155         print " if (MyTable != 0) {"
156         print "         for (j = 0; j < SIZEOF_TABLE; ++j) {"
157         print "                 FreeIfNeeded(MyTable[j]);"
158         print "         }"
159         print "         FreeAndNull(MyTable);"
160         print " }"
161         print "}"
162         print "#endif /* NO_LEAKS */"
163 }