MKkeyname.awk revision 184989
12088Ssos# $Id: MKkeyname.awk,v 1.40 2008/07/12 18:40:00 tom Exp $
2228976Suqs##############################################################################
32088Ssos# Copyright (c) 1999-2007,2008 Free Software Foundation, Inc.                #
42088Ssos#                                                                            #
52088Ssos# Permission is hereby granted, free of charge, to any person obtaining a    #
62088Ssos# copy of this software and associated documentation files (the "Software"), #
72088Ssos# to deal in the Software without restriction, including without limitation  #
82088Ssos# the rights to use, copy, modify, merge, publish, distribute, distribute    #
95994Ssos# with modifications, sublicense, and/or sell copies of the Software, and to #
105994Ssos# permit persons to whom the Software is furnished to do so, subject to the  #
112088Ssos# following conditions:                                                      #
122088Ssos#                                                                            #
132088Ssos# The above copyright notice and this permission notice shall be included in #
142088Ssos# all copies or substantial portions of the Software.                        #
1597748Sschweikh#                                                                            #
162088Ssos# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR #
172088Ssos# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,   #
182088Ssos# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL    #
192088Ssos# THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER      #
202088Ssos# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING    #
212088Ssos# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER        #
222088Ssos# DEALINGS IN THE SOFTWARE.                                                  #
232088Ssos#                                                                            #
242088Ssos# Except as contained in this notice, the name(s) of the above copyright     #
252088Ssos# holders shall not be used in advertising or otherwise to promote the sale, #
262088Ssos# use or other dealings in this Software without prior written               #
272088Ssos# authorization.                                                             #
282088Ssos##############################################################################
29114601SobrienBEGIN {
30114601Sobrien	print "/* generated by MKkeyname.awk */"
3129603Scharnier	print ""
322088Ssos	print "#include <curses.priv.h>"
3329603Scharnier	print "#include <tic.h>"
342088Ssos	print "#include <term_entry.h>"
3529603Scharnier	print ""
363864Sswallace	first = 1;
3729603Scharnier}
3842505Syokota
3966834Sphk/^[^#]/ {
4066834Sphk		if (bigstrings) {
41266839Sray			if (first)  {
422088Ssos				print "struct kn { short offset; int code; };"
432088Ssos				print "static const struct kn _nc_key_names[] = {"
442088Ssos			}
4576643Simp			printf "\t{ %d, %s },\n", offset, $1
4690394Sru			offset += length($1) + 1
4790394Sru			names = names"\n\t\""$1"\\0\""
4876643Simp		} else {
4990394Sru			if (first) {
5090394Sru				print "struct kn { const char *name; int code; };"
5190394Sru				print "static const struct kn _nc_key_names[] = {"
5290394Sru			}
5390394Sru			printf "\t{ \"%s\", %s },\n", $1, $1;
5490394Sru		}
5576643Simp		first = 0;
5676643Simp	}
5776643Simp
5876643SimpEND {
59196500Sed	if (bigstrings) {
60196500Sed		printf "\t{ -1, 0 }};\n"
61228437Sed		print ""
628857Srgrimes		print "static const char key_names[] = "names";"
632088Ssos	} else {
642088Ssos		printf "\t{ 0, 0 }};\n"
6538139Syokota	}
662088Ssos	print ""
672088Ssos	print "#define SIZEOF_TABLE 256"
68228437Sed	print "#define MyTable _nc_globals.keyname_table"
6932316Syokota	print ""
7032316Syokota	print "NCURSES_EXPORT(NCURSES_CONST char *) _nc_keyname (SCREEN *sp, int c)"
7132316Syokota	print "{"
7232316Syokota	print "	int i;"
7332316Syokota	print "	char name[20];"
74228437Sed	print "	char *p;"
7532316Syokota	print "	NCURSES_CONST char *result = 0;"
7632316Syokota	print ""
7732316Syokota	print "	if (c == -1) {"
7832316Syokota	print "		result = \"-1\";"
7932316Syokota	print "	} else {"
80228437Sed	if (bigstrings) {
815994Ssos		print "		for (i = 0; _nc_key_names[i].offset != -1; i++) {"
825994Ssos		print "			if (_nc_key_names[i].code == c) {"
835994Ssos		print "				result = (NCURSES_CONST char *)key_names + _nc_key_names[i].offset;"
845994Ssos		print "				break;"
855994Ssos		print "			}"
865994Ssos		print "		}"
875994Ssos	} else {
885994Ssos		print "		for (i = 0; _nc_key_names[i].name != 0; i++) {"
895994Ssos		print "			if (_nc_key_names[i].code == c) {"
905994Ssos		print "				result = (NCURSES_CONST char *)_nc_key_names[i].name;"
915994Ssos		print "				break;"
929202Srgrimes		print "			}"
935994Ssos		print "		}"
945994Ssos	}
955994Ssos	print ""
969202Srgrimes	print "		if (result == 0 && (c >= 0 && c < SIZEOF_TABLE)) {"
975994Ssos	print "			if (MyTable == 0)"
985994Ssos	print "				MyTable = typeCalloc(char *, SIZEOF_TABLE);"
995994Ssos	print "			if (MyTable != 0) {"
1005994Ssos	print "				if (MyTable[c] == 0) {"
1015994Ssos	print "					int cc = c;"
1025994Ssos	print "					p = name;"
1035994Ssos	print "					if (cc >= 128 && (sp == 0 || sp->_use_meta)) {"
1045994Ssos	print "						strcpy(p, \"M-\");"
1052088Ssos	print "						p += 2;"
1062088Ssos	print "						cc -= 128;"
107228437Sed	print "					}"
108228437Sed	print "					if (cc < 32)"
109228437Sed	print "						sprintf(p, \"^%c\", cc + '@');"
110228437Sed	print "					else if (cc == 127)"
111228437Sed	print "						strcpy(p, \"^?\");"
112228437Sed	print "					else"
113228437Sed	print "						sprintf(p, \"%c\", cc);"
114228437Sed	print "					MyTable[c] = strdup(name);"
115228437Sed	print "				}"
1162088Ssos	print "				result = MyTable[c];"
117228437Sed	print "			}"
118228437Sed	print "#if NCURSES_EXT_FUNCS && NCURSES_XNAMES"
1192088Ssos	print "		} else if (result == 0 && cur_term != 0) {"
120228437Sed	print "			int j, k;"
121228437Sed	print "			char * bound;"
122228437Sed	print "			TERMTYPE *tp = &(cur_term->type);"
123228437Sed	print "			int save_trace = _nc_tracing;"
124228437Sed	print ""
125228437Sed	print "			_nc_tracing = 0;	/* prevent recursion via keybound() */"
126228437Sed	print "			for (j = 0; (bound = keybound(c, j)) != 0; ++j) {"
127228437Sed	print "				for(k = STRCOUNT; k < (int) NUM_STRINGS(tp);  k++) {"
128228437Sed	print "					if (tp->Strings[k] != 0 && !strcmp(bound, tp->Strings[k])) {"
129228437Sed	print "						result = ExtStrname(tp, k, strnames);"
130228437Sed	print "						break;"
131228437Sed	print "					}"
132228437Sed	print "				}"
133228437Sed	print "				free(bound);"
134228437Sed	print "				if (result != 0)"
135228437Sed	print "					break;"
136228437Sed	print "			}"
137228437Sed	print "			_nc_tracing = save_trace;"
138228437Sed	print "#endif"
139228437Sed	print "		}"
140228437Sed	print "	}"
141228437Sed	print "	return result;"
142228437Sed	print "}"
143228437Sed	print ""
144228437Sed	print "NCURSES_EXPORT(NCURSES_CONST char *) keyname (int c)"
145266839Sray	print "{"
146266839Sray	print "\treturn _nc_keyname(SP, c);"
147266839Sray	print "}"
148266839Sray	print ""
149268175Semaste	print "#if NO_LEAKS"
150268175Semaste	print "void _nc_keyname_leaks(void)"
151266839Sray	print "{"
152268175Semaste	print "	int j;"
153268175Semaste	print "	if (MyTable != 0) {"
154268175Semaste	print "		for (j = 0; j < SIZEOF_TABLE; ++j) {"
155266839Sray	print "			FreeIfNeeded(MyTable[j]);"
156266839Sray	print "		}"
157228437Sed	print "		FreeAndNull(MyTable);"
1582088Ssos	print "	}"
1592088Ssos	print "}"
1602088Ssos	print "#endif /* NO_LEAKS */"
1612088Ssos}
16229603Scharnier