ncurses 5.6 - patch 20070224
[ncurses.git] / ncurses / base / MKkeyname.awk
index aaeb4743cdfe81b86f70a8f3bfeba08819ba6343..2d449271fd194ed9bd8df085e5581ee6f06d4615 100644 (file)
@@ -1,6 +1,6 @@
-# $Id: MKkeyname.awk,v 1.17 1999/02/18 11:18:06 tom Exp $
+# $Id: MKkeyname.awk,v 1.31 2007/01/06 21:19:44 Miroslav.Lichvar Exp $
 ##############################################################################
-# Copyright (c) 1999 Free Software Foundation, Inc.                          #
+# Copyright (c) 1999-2006,2007 Free Software Foundation, Inc.                #
 #                                                                            #
 # Permission is hereby granted, free of charge, to any person obtaining a    #
 # copy of this software and associated documentation files (the "Software"), #
 BEGIN {
        print "/* generated by MKkeyname.awk */"
        print ""
-       print "#include <ncurses_cfg.h>"
-       print "#include <stdlib.h>"
-       print "#include <string.h>"
-       print "#include <curses.h>"
+       print "#include <curses.priv.h>"
        print "#include <tic.h>"
+       print "#include <term_entry.h>"
        print ""
        print "const struct kn _nc_key_names[] = {"
 }
 
 /^[^#]/ {
-       printf "\t{ \"%s\", %s },\n", $1, $1;
+       printf "\t{ %d, %s },\n", offset, $1
+       offset += length($1) + 1
+       names = names"\n\t\""$1"\\0\""
        }
 
 END {
-       printf "\t{ 0, 0 }};\n"
+       printf "\t{ -1, 0 }};\n"
        print ""
-       print "NCURSES_CONST char *keyname(int c)"
+       print "static const char key_names[] = "names";"
+       print ""
+       print "#define SIZEOF_TABLE 256"
+       print "static char **keyname_table;"
+       print ""
+       print "NCURSES_EXPORT(NCURSES_CONST char *) keyname (int c)"
+       print "{"
+       print " int i;"
+       print " char name[20];"
+       print " char *p;"
+       print " NCURSES_CONST char *result = 0;"
+       print ""
+       print " if (c == -1) {"
+       print "         result = \"-1\";"
+       print " } else {"
+       print "         for (i = 0; _nc_key_names[i].offset != -1; i++) {"
+       print "                 if (_nc_key_names[i].code == c) {"
+       print "                         result = (NCURSES_CONST char *)key_names + _nc_key_names[i].offset;"
+       print "                         break;"
+       print "                 }"
+       print "         }"
+       print ""
+       print "         if (result == 0 && (c >= 0 && c < SIZEOF_TABLE)) {"
+       print "                 if (keyname_table == 0)"
+       print "                         keyname_table = typeCalloc(char *, SIZEOF_TABLE);"
+       print "                 if (keyname_table != 0) {"
+       print "                         if (keyname_table[c] == 0) {"
+       print "                                 int cc = c;"
+       print "                                 p = name;"
+       print "                                 if (cc >= 128) {"
+       print "                                         strcpy(p, \"M-\");"
+       print "                                         p += 2;"
+       print "                                         cc -= 128;"
+       print "                                 }"
+       print "                                 if (cc < 32)"
+       print "                                         sprintf(p, \"^%c\", cc + '@');"
+       print "                                 else if (cc == 127)"
+       print "                                         strcpy(p, \"^?\");"
+       print "                                 else"
+       print "                                         sprintf(p, \"%c\", cc);"
+       print "                                 keyname_table[c] = strdup(name);"
+       print "                         }"
+       print "                         result = keyname_table[c];"
+       print "                 }"
+       print "#if NCURSES_EXT_FUNCS && NCURSES_XNAMES"
+       print "         } else if (result == 0 && cur_term != 0) {"
+       print "                 int j, k;"
+       print "                 char * bound;"
+       print "                 TERMTYPE *tp = &(cur_term->type);"
+       print "                 int save_trace = _nc_tracing;"
+       print ""
+       print "                 _nc_tracing = 0;        /* prevent recursion via keybound() */"
+       print "                 for (j = 0; (bound = keybound(c, j)) != 0; ++j) {"
+       print "                         for(k = STRCOUNT; k < NUM_STRINGS(tp);  k++) {"
+       print "                                 if (tp->Strings[k] != 0 && !strcmp(bound, tp->Strings[k])) {"
+       print "                                         result = ExtStrname(tp, k, strnames);"
+       print "                                         break;"
+       print "                                 }"
+       print "                         }"
+       print "                         free(bound);"
+       print "                         if (result != 0)"
+       print "                                 break;"
+       print "                 }"
+       print "                 _nc_tracing = save_trace;"
+       print "#endif"
+       print "         }"
+       print " }"
+       print " return result;"
+       print "}"
+       print ""
+       print "#if USE_WIDEC_SUPPORT"
+       print "NCURSES_EXPORT(NCURSES_CONST char *) key_name (wchar_t c)"
        print "{"
-       print "int i;"
-       print "static char name[20];"
-       print "char *p;"
+       print " NCURSES_CONST char *result = keyname((int)c);"
+       print " if (!strncmp(result, \"M-\", 2)) result = 0;"
+       print " return result;"
+       print "}"
+       print "#endif"
        print ""
-       print "\tfor (i = 0; _nc_key_names[i].name != 0; i++)"
-       print "\t\tif (_nc_key_names[i].code == c)"
-       print "\t\t\treturn (NCURSES_CONST char *)_nc_key_names[i].name;"
-       print "\tif (c >= 256) return \"UNKNOWN KEY\";"
-       print "\tp = name;"
-       print "\tif (c >= 128) {"
-       print "\t\tstrcpy(p, \"M-\");"
-       print "\t\tp += 2;"
-       print "\t\tc -= 128;"
-       print "\t}"
-       print "\tif (c < 0)"
-       print "\t\tsprintf(p, \"%d\", c);"
-       print "\telse if (c < 32)"
-       print "\t\tsprintf(p, \"^%c\", c + '@');"
-       print "\telse if (c == 127)"
-       print "\t\tstrcpy(p, \"^?\");"
-       print "\telse"
-       print "\t\tsprintf(p, \"%c\", c);"
-       print "\treturn (NCURSES_CONST char *)name;"
+       print "#if NO_LEAKS"
+       print "void _nc_keyname_leaks(void)"
+       print "{"
+       print " int j;"
+       print " if (keyname_table != 0) {"
+       print "         for (j = 0; j < SIZEOF_TABLE; ++j) {"
+       print "                 FreeIfNeeded(keyname_table[j]);"
+       print "         }"
+       print "         FreeAndNull(keyname_table);"
+       print " }"
        print "}"
+       print "#endif /* NO_LEAKS */"
 }