X-Git-Url: http://ncurses.scripts.mit.edu/?a=blobdiff_plain;f=test%2Flist_keys.c;h=96b02e14f355e29fcb448dc6ce003720a936dd0a;hb=91494ab0071e71177728916199a406768f5ef963;hp=31c057a0a7f6ef29c38b2340fc37bdf7b0b11982;hpb=a924c24b2535cccdc0f5f991cd8ddcadcfa1f0d2;p=ncurses.git diff --git a/test/list_keys.c b/test/list_keys.c index 31c057a0..96b02e14 100644 --- a/test/list_keys.c +++ b/test/list_keys.c @@ -1,5 +1,6 @@ /**************************************************************************** - * Copyright (c) 2016,2017 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.19 2017/04/08 22:57:26 tom Exp $ + * $Id: list_keys.c,v 1.32 2023/06/24 13:57:11 tom Exp $ * * Author: Thomas E Dickey * @@ -70,6 +71,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) { @@ -88,14 +96,14 @@ 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': @@ -197,8 +205,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("-"); } @@ -209,8 +217,8 @@ draw_line(int width) static const char * modified_key(const char *name) { - static char result[80]; - char buffer[sizeof(result)]; + static char result[100]; + char buffer[sizeof(result) - 10]; int value; char chr; static const char *modifiers[][2] = @@ -238,8 +246,8 @@ modified_key(const char *name) map &= ~6; map |= (bit1 << 1) | (bit2 >> 1); _nc_SPRINTF(result, _nc_SLIMIT(sizeof(result)) - "%sF%d", modifiers[map][f_opt], 1 + key); - } else if (sscanf(name, "k%[A-Z]%d%c", buffer, &value, &chr) == 2 && + "%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") || @@ -253,12 +261,12 @@ modified_key(const char *name) !strcmp(buffer, "NXT") || !strcmp(buffer, "PRV"))) { _nc_SPRINTF(result, _nc_SLIMIT(sizeof(result)) - "%sk%s", modifiers[value - 1][f_opt], buffer); - } else if (sscanf(name, "k%[A-Z]%c", buffer, &chr) == 1 && + "%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][f_opt], buffer); + "%sk%s", modifiers[1][(unsigned) f_opt], buffer); } else { *result = '\0'; } @@ -285,8 +293,8 @@ 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 = (TERMTYPE *) cur_term; total += (size_t) (NUM_STRINGS(term) - STRCOUNT); @@ -298,16 +306,18 @@ 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 = (TERMTYPE *) cur_term; for (n = STRCOUNT; n < NUM_STRINGS(term); ++n) { @@ -335,8 +345,11 @@ list_keys(TERMINAL **terms, int count) widths1 = (int) strlen(modifier); for (k = 0; k < count; ++k) { + char *value; set_curterm(terms[k]); - check = (int) strlen(termname()); + if ((value = termname()) == NULL) + failed("termname"); + check = (int) strlen(value); if (widths2 < check) widths2 = check; } @@ -414,37 +427,41 @@ 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", - " -m print modifier-column for shift/control keys", - " -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, "fmtx")) != -1) { - switch (n) { + while ((ch = getopt(argc, argv, OPTS_COMMON "fmtx")) != -1) { + switch (ch) { case 'f': f_opt = TRUE; break; @@ -459,9 +476,12 @@ main(int argc, char *argv[]) x_opt = TRUE; break; #endif + case OPTS_VERSION: + show_version(argv); + ExitProgram(EXIT_SUCCESS); default: - usage(); - break; + usage(ch == OPTS_USAGE); + /* NOTREACHED */ } } @@ -472,6 +492,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) { @@ -493,7 +514,7 @@ main(int argc, char *argv[]) #else int -main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED) +main(void) { printf("This program requires the terminfo arrays\n"); ExitProgram(EXIT_FAILURE); @@ -501,7 +522,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);