xref: /openbsd/lib/libcurses/base/MKkeyname.awk (revision 09467b48)
1# $OpenBSD: MKkeyname.awk,v 1.5 2010/01/12 23:22:05 nicm Exp $
2# $Id: MKkeyname.awk,v 1.5 2010/01/12 23:22:05 nicm Exp $
3##############################################################################
4# Copyright (c) 1999-2007,2008 Free Software Foundation, Inc.                #
5#                                                                            #
6# Permission is hereby granted, free of charge, to any person obtaining a    #
7# copy of this software and associated documentation files (the "Software"), #
8# to deal in the Software without restriction, including without limitation  #
9# the rights to use, copy, modify, merge, publish, distribute, distribute    #
10# with modifications, sublicense, and/or sell copies of the Software, and to #
11# permit persons to whom the Software is furnished to do so, subject to the  #
12# following conditions:                                                      #
13#                                                                            #
14# The above copyright notice and this permission notice shall be included in #
15# all copies or substantial portions of the Software.                        #
16#                                                                            #
17# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR #
18# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,   #
19# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL    #
20# THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER      #
21# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING    #
22# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER        #
23# DEALINGS IN THE SOFTWARE.                                                  #
24#                                                                            #
25# Except as contained in this notice, the name(s) of the above copyright     #
26# holders shall not be used in advertising or otherwise to promote the sale, #
27# use or other dealings in this Software without prior written               #
28# authorization.                                                             #
29##############################################################################
30BEGIN {
31	print "/* generated by MKkeyname.awk */"
32	print ""
33	print "#include <curses.priv.h>"
34	print "#include <tic.h>"
35	print "#include <term_entry.h>"
36	print ""
37	first = 1;
38}
39
40/^[^#]/ {
41		if (bigstrings) {
42			if (first)  {
43				print "struct kn { short offset; int code; };"
44				print "static const struct kn _nc_key_names[] = {"
45			}
46			printf "\t{ %d, %s },\n", offset, $1
47			offset += length($1) + 1
48			names = names"\n\t\""$1"\\0\""
49		} else {
50			if (first) {
51				print "struct kn { const char *name; int code; };"
52				print "static const struct kn _nc_key_names[] = {"
53			}
54			printf "\t{ \"%s\", %s },\n", $1, $1;
55		}
56		first = 0;
57	}
58
59END {
60	if (bigstrings) {
61		printf "\t{ -1, 0 }};\n"
62		print ""
63		print "static const char key_names[] = "names";"
64	} else {
65		printf "\t{ 0, 0 }};\n"
66	}
67	print ""
68	print "#define SIZEOF_TABLE 256"
69	print "#define MyTable _nc_globals.keyname_table"
70	print ""
71	print "NCURSES_EXPORT(NCURSES_CONST char *) _nc_keyname (SCREEN *sp, int c)"
72	print "{"
73	print "	int i;"
74	print "	char name[20];"
75	print "	char *p;"
76	print "	size_t psize;"
77	print "	NCURSES_CONST char *result = 0;"
78	print ""
79	print "	if (c == -1) {"
80	print "		result = \"-1\";"
81	print "	} else {"
82	if (bigstrings) {
83		print "		for (i = 0; _nc_key_names[i].offset != -1; i++) {"
84		print "			if (_nc_key_names[i].code == c) {"
85		print "				result = (NCURSES_CONST char *)key_names + _nc_key_names[i].offset;"
86		print "				break;"
87		print "			}"
88		print "		}"
89	} else {
90		print "		for (i = 0; _nc_key_names[i].name != 0; i++) {"
91		print "			if (_nc_key_names[i].code == c) {"
92		print "				result = (NCURSES_CONST char *)_nc_key_names[i].name;"
93		print "				break;"
94		print "			}"
95		print "		}"
96	}
97	print ""
98	print "		if (result == 0 && (c >= 0 && c < SIZEOF_TABLE)) {"
99	print "			if (MyTable == 0)"
100	print "				MyTable = typeCalloc(char *, SIZEOF_TABLE);"
101	print "			if (MyTable != 0) {"
102	print "				if (MyTable[c] == 0) {"
103	print "					int cc = c;"
104	print "					p = name;"
105	print "					psize = sizeof(name);"
106	print "					if (cc >= 128 && (sp == 0 || sp->_use_meta)) {"
107	print "						strlcpy(p, \"M-\", psize);"
108	print "						p += 2;"
109	print "						psize -= 2;"
110	print "						cc -= 128;"
111	print "					}"
112	print "					if (cc < 32)"
113	print "						snprintf(p, psize, \"^%c\", cc + '@');"
114	print "					else if (cc == 127)"
115	print "						strlcpy(p, \"^?\", psize);"
116	print "					else"
117	print "						snprintf(p, psize, \"%c\", cc);"
118	print "					MyTable[c] = strdup(name);"
119	print "				}"
120	print "				result = MyTable[c];"
121	print "			}"
122	print "#if NCURSES_EXT_FUNCS && NCURSES_XNAMES"
123	print "		} else if (result == 0 && cur_term != 0) {"
124	print "			int j, k;"
125	print "			char * bound;"
126	print "			TERMTYPE *tp = &(cur_term->type);"
127	print "			int save_trace = _nc_tracing;"
128	print ""
129	print "			_nc_tracing = 0;	/* prevent recursion via keybound() */"
130	print "			for (j = 0; (bound = keybound(c, j)) != 0; ++j) {"
131	print "				for(k = STRCOUNT; k < (int) NUM_STRINGS(tp);  k++) {"
132	print "					if (tp->Strings[k] != 0 && !strcmp(bound, tp->Strings[k])) {"
133	print "						result = ExtStrname(tp, k, strnames);"
134	print "						break;"
135	print "					}"
136	print "				}"
137	print "				free(bound);"
138	print "				if (result != 0)"
139	print "					break;"
140	print "			}"
141	print "			_nc_tracing = save_trace;"
142	print "#endif"
143	print "		}"
144	print "	}"
145	print "	return result;"
146	print "}"
147	print ""
148	print "NCURSES_EXPORT(NCURSES_CONST char *) keyname (int c)"
149	print "{"
150	print "\treturn _nc_keyname(SP, c);"
151	print "}"
152	print ""
153	print "#if NO_LEAKS"
154	print "void _nc_keyname_leaks(void)"
155	print "{"
156	print "	int j;"
157	print "	if (MyTable != 0) {"
158	print "		for (j = 0; j < SIZEOF_TABLE; ++j) {"
159	print "			FreeIfNeeded(MyTable[j]);"
160	print "		}"
161	print "		FreeAndNull(MyTable);"
162	print "	}"
163	print "}"
164	print "#endif /* NO_LEAKS */"
165}
166