11dcfe5c23755b2080e2e670d86913269b1f1418
[ncurses.git] / ncurses / base / MKkeyname.awk
1 # $Id: MKkeyname.awk,v 1.30 2006/05/20 17:35:30 tom Exp $
2 ##############################################################################
3 # Copyright (c) 1999-2005,2006 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         print "const struct kn _nc_key_names[] = {"
37 }
38
39 /^[^#]/ {
40         printf "\t{ \"%s\", %s },\n", $1, $1;
41         }
42
43 END {
44         printf "\t{ 0, 0 }};\n"
45         print ""
46         print "#define SIZEOF_TABLE 256"
47         print "static char **keyname_table;"
48         print ""
49         print "NCURSES_EXPORT(NCURSES_CONST char *) keyname (int c)"
50         print "{"
51         print " int i;"
52         print " char name[20];"
53         print " char *p;"
54         print " NCURSES_CONST char *result = 0;"
55         print ""
56         print " if (c == -1) {"
57         print "         result = \"-1\";"
58         print " } else {"
59         print "         for (i = 0; _nc_key_names[i].name != 0; i++) {"
60         print "                 if (_nc_key_names[i].code == c) {"
61         print "                         result = (NCURSES_CONST char *)_nc_key_names[i].name;"
62         print "                         break;"
63         print "                 }"
64         print "         }"
65         print ""
66         print "         if (result == 0 && (c >= 0 && c < SIZEOF_TABLE)) {"
67         print "                 if (keyname_table == 0)"
68         print "                         keyname_table = typeCalloc(char *, SIZEOF_TABLE);"
69         print "                 if (keyname_table != 0) {"
70         print "                         if (keyname_table[c] == 0) {"
71         print "                                 int cc = c;"
72         print "                                 p = name;"
73         print "                                 if (cc >= 128) {"
74         print "                                         strcpy(p, \"M-\");"
75         print "                                         p += 2;"
76         print "                                         cc -= 128;"
77         print "                                 }"
78         print "                                 if (cc < 32)"
79         print "                                         sprintf(p, \"^%c\", cc + '@');"
80         print "                                 else if (cc == 127)"
81         print "                                         strcpy(p, \"^?\");"
82         print "                                 else"
83         print "                                         sprintf(p, \"%c\", cc);"
84         print "                                 keyname_table[c] = strdup(name);"
85         print "                         }"
86         print "                         result = keyname_table[c];"
87         print "                 }"
88         print "#if NCURSES_EXT_FUNCS && NCURSES_XNAMES"
89         print "         } else if (result == 0 && cur_term != 0) {"
90         print "                 int j, k;"
91         print "                 char * bound;"
92         print "                 TERMTYPE *tp = &(cur_term->type);"
93         print "                 int save_trace = _nc_tracing;"
94         print ""
95         print "                 _nc_tracing = 0;        /* prevent recursion via keybound() */"
96         print "                 for (j = 0; (bound = keybound(c, j)) != 0; ++j) {"
97         print "                         for(k = STRCOUNT; k < NUM_STRINGS(tp);  k++) {"
98         print "                                 if (tp->Strings[k] != 0 && !strcmp(bound, tp->Strings[k])) {"
99         print "                                         result = ExtStrname(tp, k, strnames);"
100         print "                                         break;"
101         print "                                 }"
102         print "                         }"
103         print "                         free(bound);"
104         print "                         if (result != 0)"
105         print "                                 break;"
106         print "                 }"
107         print "                 _nc_tracing = save_trace;"
108         print "#endif"
109         print "         }"
110         print " }"
111         print " return result;"
112         print "}"
113         print ""
114         print "#if USE_WIDEC_SUPPORT"
115         print "NCURSES_EXPORT(NCURSES_CONST char *) key_name (wchar_t c)"
116         print "{"
117         print " NCURSES_CONST char *result = keyname((int)c);"
118         print " if (!strncmp(result, \"M-\", 2)) result = 0;"
119         print " return result;"
120         print "}"
121         print "#endif"
122         print ""
123         print "#if NO_LEAKS"
124         print "void _nc_keyname_leaks(void)"
125         print "{"
126         print " int j;"
127         print " if (keyname_table != 0) {"
128         print "         for (j = 0; j < SIZEOF_TABLE; ++j) {"
129         print "                 FreeIfNeeded(keyname_table[j]);"
130         print "         }"
131         print "         FreeAndNull(keyname_table);"
132         print " }"
133         print "}"
134         print "#endif /* NO_LEAKS */"
135 }