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