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