ncurses 5.5
[ncurses.git] / ncurses / base / MKkeyname.awk
1 # $Id: MKkeyname.awk,v 1.28 2005/04/30 19:26:25 tom Exp $
2 ##############################################################################
3 # Copyright (c) 1999-2002,2005 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 "NCURSES_EXPORT(NCURSES_CONST char *) keyname (int c)"
47         print "{"
48         print " static char **table;"
49         print " int i;"
50         print " char name[20];"
51         print " char *p;"
52         print " NCURSES_CONST char *result = 0;"
53         print ""
54         print " if (c == -1) {"
55         print "         result = \"-1\";"
56         print " } else {"
57         print "         for (i = 0; _nc_key_names[i].name != 0; i++) {"
58         print "                 if (_nc_key_names[i].code == c) {"
59         print "                         result = (NCURSES_CONST char *)_nc_key_names[i].name;"
60         print "                         break;"
61         print "                 }"
62         print "         }"
63         print ""
64         print "         if (result == 0 && (c >= 0 && c < 256)) {"
65         print "                 if (table == 0)"
66         print "                         table = typeCalloc(char *, 256);"
67         print "                 if (table != 0) {"
68         print "                         if (table[c] == 0) {"
69         print "                                 p = name;"
70         print "                                 if (c >= 128) {"
71         print "                                         strcpy(p, \"M-\");"
72         print "                                         p += 2;"
73         print "                                         c -= 128;"
74         print "                                 }"
75         print "                                 if (c < 32)"
76         print "                                         sprintf(p, \"^%c\", c + '@');"
77         print "                                 else if (c == 127)"
78         print "                                         strcpy(p, \"^?\");"
79         print "                                 else"
80         print "                                         sprintf(p, \"%c\", c);"
81         print "                                 table[c] = strdup(name);"
82         print "                         }"
83         print "                         result = table[c];"
84         print "                 }"
85         print "#if NCURSES_EXT_FUNCS && NCURSES_XNAMES"
86         print "         } else if (result == 0 && cur_term != 0) {"
87         print "                 int j, k;"
88         print "                 char * bound;"
89         print "                 TERMTYPE *tp = &(cur_term->type);"
90         print "                 int save_trace = _nc_tracing;"
91         print ""
92         print "                 _nc_tracing = 0;        /* prevent recursion via keybound() */"
93         print "                 for (j = 0; (bound = keybound(c, j)) != 0; ++j) {"
94         print "                         for(k = STRCOUNT; k < NUM_STRINGS(tp);  k++) {"
95         print "                                 if (tp->Strings[k] != 0 && !strcmp(bound, tp->Strings[k])) {"
96         print "                                         result = ExtStrname(tp, k, strnames);"
97         print "                                         break;"
98         print "                                 }"
99         print "                         }"
100         print "                         if (result != 0)"
101         print "                                 break;"
102         print "                 }"
103         print "                 _nc_tracing = save_trace;"
104         print "#endif"
105         print "         }"
106         print " }"
107         print " return result;"
108         print "}"
109         print ""
110         print "#if USE_WIDEC_SUPPORT"
111         print "NCURSES_EXPORT(NCURSES_CONST char *) key_name (wchar_t c)"
112         print "{"
113         print " NCURSES_CONST char *result = keyname((int)c);"
114         print " if (!strncmp(result, \"M-\", 2)) result = 0;"
115         print " return result;"
116         print "}"
117         print "#endif"
118 }