X-Git-Url: http://ncurses.scripts.mit.edu/?p=ncurses.git;a=blobdiff_plain;f=test%2Flist_keys.c;h=cdad60e928b6a92a9a77ce4cce74e9d3ff53234f;hp=87c04c970cd20e3a94c5b03489cca9009b19039f;hb=HEAD;hpb=3353ecc7ed0f1d3b4e6c7e8e98d92f469d593e35 diff --git a/test/list_keys.c b/test/list_keys.c index 87c04c97..b616919f 100644 --- a/test/list_keys.c +++ b/test/list_keys.c @@ -1,5 +1,6 @@ /**************************************************************************** - * Copyright (c) 2016 Free Software Foundation, Inc. * + * Copyright 2018-2022,2023 Thomas E. Dickey * + * Copyright 2016,2017 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 * @@ -26,7 +27,7 @@ * authorization. * ****************************************************************************/ /* - * $Id: list_keys.c,v 1.12 2016/07/02 23:45:53 tom Exp $ + * $Id: list_keys.c,v 1.33 2023/11/11 00:35:05 tom Exp $ * * Author: Thomas E Dickey * @@ -49,8 +50,12 @@ #if defined(HAVE_CURSES_DATA_BOOLNAMES) || defined(DECL_CURSES_DATA_BOOLNAMES) static bool f_opt = FALSE; +static bool m_opt = FALSE; static bool t_opt = FALSE; + +#if NCURSES_XNAMES || HAVE_USE_EXTENDED_NAMES static bool x_opt = FALSE; +#endif typedef enum { ktCursor @@ -69,6 +74,13 @@ typedef struct { #define Type(n) list[n].type #define Name(n) list[n].name +static void +failed(const char *msg) +{ + perror(msg); + ExitProgram(EXIT_FAILURE); +} + static const char * full_name(const char *name) { @@ -87,58 +99,61 @@ static int show_key(const char *name, bool show) { int width = 0; - char buffer[10]; - NCURSES_CONST char *value = tigetstr(name); + NCURSES_CONST char *value = tigetstr((NCURSES_CONST char *) name); if (show && t_opt) fputc('"', stdout); if (value != 0 && value != (char *) -1) { while (*value != 0) { + char buffer[10]; int ch = UChar(*value++); switch (ch) { case '\177': - strcpy(buffer, "^?"); + _nc_STRCPY(buffer, "^?", sizeof(buffer)); break; case '\033': - strcpy(buffer, "\\E"); + _nc_STRCPY(buffer, "\\E", sizeof(buffer)); break; case '\b': - strcpy(buffer, "\\b"); + _nc_STRCPY(buffer, "\\b", sizeof(buffer)); break; case '\f': - strcpy(buffer, "\\f"); + _nc_STRCPY(buffer, "\\f", sizeof(buffer)); break; case '\n': - strcpy(buffer, "\\n"); + _nc_STRCPY(buffer, "\\n", sizeof(buffer)); break; case '\r': - strcpy(buffer, "\\r"); + _nc_STRCPY(buffer, "\\r", sizeof(buffer)); break; case ' ': - strcpy(buffer, "\\s"); + _nc_STRCPY(buffer, "\\s", sizeof(buffer)); break; case '\t': - strcpy(buffer, "\\t"); + _nc_STRCPY(buffer, "\\t", sizeof(buffer)); break; case '^': - strcpy(buffer, "\\^"); + _nc_STRCPY(buffer, "\\^", sizeof(buffer)); break; case ':': - strcpy(buffer, "\\072"); + _nc_STRCPY(buffer, "\\072", sizeof(buffer)); break; case '\\': - strcpy(buffer, "\\\\"); + _nc_STRCPY(buffer, "\\\\", sizeof(buffer)); break; default: if (t_opt && ch == '"') { - strcpy(buffer, "\"\""); + _nc_STRCPY(buffer, "\"\"", sizeof(buffer)); } else if (isgraph(ch)) { - sprintf(buffer, "%c", ch); + _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer)) + "%c", ch); } else if (ch < 32) { - sprintf(buffer, "^%c", ch + '@'); + _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer)) + "^%c", ch + '@'); } else { - sprintf(buffer, "\\%03o", ch); + _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer)) + "\\%03o", ch); } break; } @@ -155,7 +170,7 @@ show_key(const char *name, bool show) } static bool -valid_key(const char *name, TERMINAL ** terms, int count) +valid_key(const char *name, TERMINAL **terms, int count) { bool result = FALSE; if (*name == 'k') { @@ -193,8 +208,8 @@ compare_keys(const void *a, const void *b) static void draw_line(int width) { - int j; if (!t_opt) { + int j; for (j = 0; j < width; ++j) { printf("-"); } @@ -202,17 +217,78 @@ draw_line(int width) } } +static const char * +modified_key(const char *name) +{ + static char result[100]; + char buffer[sizeof(result) - 10]; + int value; + char chr; + static const char *modifiers[][2] = + { + {"", ""}, + {"s-", "shift-"}, + {"a-", "alt-"}, + {"as-", "alt-shift-"}, + {"c-", "ctrl-"}, + {"sc-", "ctrl-shift-"}, + {"ac-", "alt-ctrl-"}, + {"acs-" "alt-ctrl-shift-"}, + }; + + if (strlen(name) > (sizeof(result) - 3)) { + *result = '\0'; + } else if (sscanf(name, "kf%d%c", &value, &chr) == 1 && + value >= 1 && + value <= 63) { + /* map 1,2,3,4,5,6,7 to 1,2,5,... */ + int map = ((value - 1) / 12); + int key = ((value - 1) % 12); + int bit1 = (map & 2); + int bit2 = (map & 4); + map &= ~6; + map |= (bit1 << 1) | (bit2 >> 1); + _nc_SPRINTF(result, _nc_SLIMIT(sizeof(result)) + "%sF%d", modifiers[map][(unsigned) f_opt], 1 + key); + } else if (sscanf(name, "k%80[A-Z]%d%c", buffer, &value, &chr) == 2 && + (value > 1 && + value <= 8) && + (!strcmp(buffer, "UP") || + !strcmp(buffer, "DN") || + !strcmp(buffer, "LFT") || + !strcmp(buffer, "RIT") || + !strcmp(buffer, "IC") || + !strcmp(buffer, "DC") || + !strcmp(buffer, "HOM") || + !strcmp(buffer, "END") || + !strcmp(buffer, "NXT") || + !strcmp(buffer, "PRV"))) { + _nc_SPRINTF(result, _nc_SLIMIT(sizeof(result)) + "%sk%s", modifiers[value - 1][(unsigned) f_opt], buffer); + } else if (sscanf(name, "k%80[A-Z]%c", buffer, &chr) == 1 && + (!strcmp(buffer, "UP") || + !strcmp(buffer, "DN"))) { + _nc_SPRINTF(result, _nc_SLIMIT(sizeof(result)) + "%sk%s", modifiers[1][(unsigned) f_opt], buffer); + } else { + *result = '\0'; + } + return result; +} + static void -list_keys(TERMINAL ** terms, int count) +list_keys(TERMINAL **terms, int count) { int j, k; int widths0 = 0; int widths1 = 0; + int widths2 = 0; int widthsx; int check; size_t total = 0; size_t actual = 0; const char *name = f_opt ? "strfname" : "strname"; + const char *modifier = "extended"; KEYNAMES *list; for (total = 0; strnames[total]; ++total) { @@ -220,10 +296,10 @@ list_keys(TERMINAL ** terms, int count) } #if NCURSES_XNAMES if (x_opt) { - TERMTYPE *term; for (k = 0; k < count; ++k) { + TERMTYPE *term; set_curterm(terms[k]); - term = &(cur_term->type); + term = (TERMTYPE *) cur_term; total += (size_t) (NUM_STRINGS(term) - STRCOUNT); } } @@ -233,18 +309,20 @@ list_keys(TERMINAL ** terms, int count) Type(j) = ktOther; if (sscanf(strnames[j], "kf%d", &k) == 1) { Type(j) = ktFunction; - } else if (!strncmp(strnames[j], "kcu", 3)) { + } else if (!(strncmp) (strnames[j], "kcu", 3)) { Type(j) = ktCursor; } Name(j) = strnames[j]; } #if NCURSES_XNAMES if (x_opt) { - TERMTYPE *term; int m, n; + for (k = 0; k < count; ++k) { + TERMTYPE *term; + set_curterm(terms[k]); - term = &(cur_term->type); + term = (TERMTYPE *) cur_term; for (n = STRCOUNT; n < NUM_STRINGS(term); ++n) { bool found = FALSE; const char *estr = ExtStrname(term, (int) n, strnames); @@ -266,11 +344,17 @@ list_keys(TERMINAL ** terms, int count) qsort(list, actual, sizeof(KEYNAMES), compare_keys); widths0 = (int) strlen(name); + if (m_opt) + widths1 = (int) strlen(modifier); + for (k = 0; k < count; ++k) { + char *value; set_curterm(terms[k]); - check = (int) strlen(termname()); - if (widths1 < check) - widths1 = check; + if ((value = termname()) == NULL) + failed("termname"); + check = (int) strlen(value); + if (widths2 < check) + widths2 = check; } for (j = 0; Name(j) != 0; ++j) { if (valid_key(Name(j), terms, count)) { @@ -280,17 +364,26 @@ list_keys(TERMINAL ** terms, int count) widths0 = check; for (k = 0; k < count; ++k) { set_curterm(terms[k]); - check = show_key(Name(j), FALSE); - if (widths1 < check) - widths1 = check; + check = show_key(Name(j), FALSE) + 1; + if (widths2 < check) + widths2 = check; + if (m_opt) { + check = (int) strlen(modified_key(Name(j))); + if (widths1 < check) + widths1 = check; + } } } } if (t_opt) { printf("\"%s\"", name); + if (m_opt) + printf(",\"%s\"", modifier); } else { printf("%-*s", widths0, name); + if (m_opt) + printf(" %-*s", widths1, modifier); } for (k = 0; k < count; ++k) { set_curterm(terms[k]); @@ -299,12 +392,12 @@ list_keys(TERMINAL ** terms, int count) } else if (k + 1 >= count) { printf(" %s", termname()); } else { - printf(" %-*s", widths1, termname()); + printf(" %-*s", widths2, termname()); } } printf("\n"); - widthsx = widths0 + ((count + 1) * widths1); + widthsx = widths0 + ((count + 1) * widths2); for (j = 0; Name(j) != 0; ++j) { if (j == 0 || (Type(j) != Type(j - 1))) @@ -313,8 +406,12 @@ list_keys(TERMINAL ** terms, int count) const char *label = f_opt ? full_name(Name(j)) : Name(j); if (t_opt) { printf("\"%s\"", label); + if (m_opt) + printf(",\"%s\"", modified_key(Name(j))); } else { printf("%-*s", widths0, label); + if (m_opt) + printf(" %-*s", widths1, modified_key(Name(j))); } for (k = 0; k < count; ++k) { printf(t_opt ? "," : " "); @@ -322,7 +419,7 @@ list_keys(TERMINAL ** terms, int count) check = show_key(Name(j), TRUE); if (!t_opt) { if (k + 1 < count) { - printf("%*s", widths1 - check, " "); + printf("%*s", widths2 - check, " "); } } } @@ -333,50 +430,61 @@ list_keys(TERMINAL ** terms, int count) } static void -usage(void) +usage(int ok) { static const char *msg[] = { - "Usage: list_keys [options] [terminal [terminal2 [...]]]", - "", - "Print capabilities for terminal special keys.", - "", - "Options:", - " -f print full names", - " -t print result as CSV table", + "Usage: list_keys [options] [terminal [terminal2 [...]]]" + ,"" + ,"Print capabilities for terminal special keys." + ,"" + ,USAGE_COMMON + ,"Options:" + ," -f print full names" + ," -m print modifier-column for shift/control keys" + ," -t print result as CSV table" #ifdef NCURSES_VERSION - " -x print extended capabilities", + ," -x print extended capabilities" #endif }; unsigned n; for (n = 0; n < SIZEOF(msg); ++n) { fprintf(stderr, "%s\n", msg[n]); } - ExitProgram(EXIT_FAILURE); + ExitProgram(ok ? EXIT_SUCCESS : EXIT_FAILURE); } +/* *INDENT-OFF* */ +VERSION_COMMON() +/* *INDENT-ON* */ int main(int argc, char *argv[]) { - int n; + int ch; TERMINAL **terms = typeCalloc(TERMINAL *, argc + 1); - while ((n = getopt(argc, argv, "ftx")) != -1) { - switch (n) { + while ((ch = getopt(argc, argv, OPTS_COMMON "fmtx")) != -1) { + switch (ch) { case 'f': f_opt = TRUE; break; + case 'm': + m_opt = TRUE; + break; case 't': t_opt = TRUE; break; -#ifdef NCURSES_VERSION +#if NCURSES_XNAMES || HAVE_USE_EXTENDED_NAMES case 'x': x_opt = TRUE; break; #endif + case OPTS_VERSION: + show_version(argv); + ExitProgram(EXIT_SUCCESS); default: - usage(); - break; + usage(ch == OPTS_USAGE); + /* NOTREACHED */ } } @@ -387,6 +495,7 @@ main(int argc, char *argv[]) if (optind < argc) { int found = 0; int status; + int n; for (n = optind; n < argc; ++n) { setupterm((NCURSES_CONST char *) argv[n], 1, &status); if (status > 0 && cur_term != 0) { @@ -401,12 +510,14 @@ main(int argc, char *argv[]) list_keys(terms, 1); } + free(terms); + ExitProgram(EXIT_SUCCESS); } #else int -main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED) +main(void) { printf("This program requires the terminfo arrays\n"); ExitProgram(EXIT_FAILURE); @@ -414,7 +525,7 @@ main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED) #endif #else /* !HAVE_TIGETSTR */ int -main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED) +main(void) { printf("This program requires the terminfo functions such as tigetstr\n"); ExitProgram(EXIT_FAILURE);