1 /****************************************************************************
2 * Copyright 2018-2022,2023 Thomas E. Dickey *
3 * Copyright 2016,2017 Free Software Foundation, Inc. *
5 * Permission is hereby granted, free of charge, to any person obtaining a *
6 * copy of this software and associated documentation files (the *
7 * "Software"), to deal in the Software without restriction, including *
8 * without limitation the rights to use, copy, modify, merge, publish, *
9 * distribute, distribute with modifications, sublicense, and/or sell *
10 * copies of the Software, and to permit persons to whom the Software is *
11 * furnished to do so, subject to the following conditions: *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
19 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
22 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
24 * Except as contained in this notice, the name(s) of the above copyright *
25 * holders shall not be used in advertising or otherwise to promote the *
26 * sale, use or other dealings in this Software without prior written *
28 ****************************************************************************/
30 * $Id: list_keys.c,v 1.32 2023/06/24 13:57:11 tom Exp $
32 * Author: Thomas E Dickey
34 * List function keys for one or more terminals.
38 #include <test.priv.h>
42 #include <term_entry.h>
45 #define NCURSES_XNAMES 0
50 #if defined(HAVE_CURSES_DATA_BOOLNAMES) || defined(DECL_CURSES_DATA_BOOLNAMES)
52 static bool f_opt = FALSE;
53 static bool m_opt = FALSE;
54 static bool t_opt = FALSE;
55 static bool x_opt = FALSE;
61 #if HAVE_USE_EXTENDED_NAMES
71 #define Type(n) list[n].type
72 #define Name(n) list[n].name
75 failed(const char *msg)
78 ExitProgram(EXIT_FAILURE);
82 full_name(const char *name)
84 const char *result = name;
86 for (n = 0; strnames[n] != 0; ++n) {
87 if (!strcmp(name, strnames[n])) {
88 result = strfnames[n];
96 show_key(const char *name, bool show)
99 NCURSES_CONST char *value = tigetstr((NCURSES_CONST char *) name);
104 if (value != 0 && value != (char *) -1) {
105 while (*value != 0) {
107 int ch = UChar(*value++);
110 _nc_STRCPY(buffer, "^?", sizeof(buffer));
113 _nc_STRCPY(buffer, "\\E", sizeof(buffer));
116 _nc_STRCPY(buffer, "\\b", sizeof(buffer));
119 _nc_STRCPY(buffer, "\\f", sizeof(buffer));
122 _nc_STRCPY(buffer, "\\n", sizeof(buffer));
125 _nc_STRCPY(buffer, "\\r", sizeof(buffer));
128 _nc_STRCPY(buffer, "\\s", sizeof(buffer));
131 _nc_STRCPY(buffer, "\\t", sizeof(buffer));
134 _nc_STRCPY(buffer, "\\^", sizeof(buffer));
137 _nc_STRCPY(buffer, "\\072", sizeof(buffer));
140 _nc_STRCPY(buffer, "\\\\", sizeof(buffer));
143 if (t_opt && ch == '"') {
144 _nc_STRCPY(buffer, "\"\"", sizeof(buffer));
145 } else if (isgraph(ch)) {
146 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
148 } else if (ch < 32) {
149 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
152 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
157 width += (int) strlen(buffer);
159 fputs(buffer, stdout);
170 valid_key(const char *name, TERMINAL **terms, int count)
175 for (k = 0; k < count; ++k) {
176 set_curterm(terms[k]);
177 if (show_key(name, FALSE)) {
187 compare_keys(const void *a, const void *b)
189 const KEYNAMES *p = (const KEYNAMES *) a;
190 const KEYNAMES *q = (const KEYNAMES *) b;
191 int result = (int) (p->type - q->type);
194 if (p->type == ktFunction &&
195 sscanf(p->name, "kf%d", &pn) == 1 &&
196 sscanf(q->name, "kf%d", &qn) == 1) {
199 result = strcmp(p->name, q->name);
210 for (j = 0; j < width; ++j) {
218 modified_key(const char *name)
220 static char result[100];
221 char buffer[sizeof(result) - 10];
224 static const char *modifiers[][2] =
229 {"as-", "alt-shift-"},
231 {"sc-", "ctrl-shift-"},
232 {"ac-", "alt-ctrl-"},
233 {"acs-" "alt-ctrl-shift-"},
236 if (strlen(name) > (sizeof(result) - 3)) {
238 } else if (sscanf(name, "kf%d%c", &value, &chr) == 1 &&
241 /* map 1,2,3,4,5,6,7 to 1,2,5,... */
242 int map = ((value - 1) / 12);
243 int key = ((value - 1) % 12);
244 int bit1 = (map & 2);
245 int bit2 = (map & 4);
247 map |= (bit1 << 1) | (bit2 >> 1);
248 _nc_SPRINTF(result, _nc_SLIMIT(sizeof(result))
249 "%sF%d", modifiers[map][(unsigned) f_opt], 1 + key);
250 } else if (sscanf(name, "k%80[A-Z]%d%c", buffer, &value, &chr) == 2 &&
253 (!strcmp(buffer, "UP") ||
254 !strcmp(buffer, "DN") ||
255 !strcmp(buffer, "LFT") ||
256 !strcmp(buffer, "RIT") ||
257 !strcmp(buffer, "IC") ||
258 !strcmp(buffer, "DC") ||
259 !strcmp(buffer, "HOM") ||
260 !strcmp(buffer, "END") ||
261 !strcmp(buffer, "NXT") ||
262 !strcmp(buffer, "PRV"))) {
263 _nc_SPRINTF(result, _nc_SLIMIT(sizeof(result))
264 "%sk%s", modifiers[value - 1][(unsigned) f_opt], buffer);
265 } else if (sscanf(name, "k%80[A-Z]%c", buffer, &chr) == 1 &&
266 (!strcmp(buffer, "UP") ||
267 !strcmp(buffer, "DN"))) {
268 _nc_SPRINTF(result, _nc_SLIMIT(sizeof(result))
269 "%sk%s", modifiers[1][(unsigned) f_opt], buffer);
277 list_keys(TERMINAL **terms, int count)
287 const char *name = f_opt ? "strfname" : "strname";
288 const char *modifier = "extended";
291 for (total = 0; strnames[total]; ++total) {
296 for (k = 0; k < count; ++k) {
298 set_curterm(terms[k]);
299 term = (TERMTYPE *) cur_term;
300 total += (size_t) (NUM_STRINGS(term) - STRCOUNT);
304 list = typeCalloc(KEYNAMES, total + 1);
305 for (j = 0; strnames[j]; ++j) {
307 if (sscanf(strnames[j], "kf%d", &k) == 1) {
308 Type(j) = ktFunction;
309 } else if (!(strncmp) (strnames[j], "kcu", 3)) {
312 Name(j) = strnames[j];
318 for (k = 0; k < count; ++k) {
321 set_curterm(terms[k]);
322 term = (TERMTYPE *) cur_term;
323 for (n = STRCOUNT; n < NUM_STRINGS(term); ++n) {
325 const char *estr = ExtStrname(term, (int) n, strnames);
326 for (m = STRCOUNT; m < j; ++m) {
327 if (!strcmp(estr, Name(m))) {
333 Type(j) = ktExtended;
341 qsort(list, actual, sizeof(KEYNAMES), compare_keys);
343 widths0 = (int) strlen(name);
345 widths1 = (int) strlen(modifier);
347 for (k = 0; k < count; ++k) {
349 set_curterm(terms[k]);
350 if ((value = termname()) == NULL)
352 check = (int) strlen(value);
356 for (j = 0; Name(j) != 0; ++j) {
357 if (valid_key(Name(j), terms, count)) {
358 const char *label = f_opt ? full_name(Name(j)) : Name(j);
359 check = (int) strlen(label);
362 for (k = 0; k < count; ++k) {
363 set_curterm(terms[k]);
364 check = show_key(Name(j), FALSE) + 1;
368 check = (int) strlen(modified_key(Name(j)));
377 printf("\"%s\"", name);
379 printf(",\"%s\"", modifier);
381 printf("%-*s", widths0, name);
383 printf(" %-*s", widths1, modifier);
385 for (k = 0; k < count; ++k) {
386 set_curterm(terms[k]);
388 printf(",\"%s\"", termname());
389 } else if (k + 1 >= count) {
390 printf(" %s", termname());
392 printf(" %-*s", widths2, termname());
397 widthsx = widths0 + ((count + 1) * widths2);
399 for (j = 0; Name(j) != 0; ++j) {
400 if (j == 0 || (Type(j) != Type(j - 1)))
402 if (valid_key(Name(j), terms, count)) {
403 const char *label = f_opt ? full_name(Name(j)) : Name(j);
405 printf("\"%s\"", label);
407 printf(",\"%s\"", modified_key(Name(j)));
409 printf("%-*s", widths0, label);
411 printf(" %-*s", widths1, modified_key(Name(j)));
413 for (k = 0; k < count; ++k) {
414 printf(t_opt ? "," : " ");
415 set_curterm(terms[k]);
416 check = show_key(Name(j), TRUE);
419 printf("%*s", widths2 - check, " ");
432 static const char *msg[] =
434 "Usage: list_keys [options] [terminal [terminal2 [...]]]"
436 ,"Print capabilities for terminal special keys."
440 ," -f print full names"
441 ," -m print modifier-column for shift/control keys"
442 ," -t print result as CSV table"
443 #ifdef NCURSES_VERSION
444 ," -x print extended capabilities"
448 for (n = 0; n < SIZEOF(msg); ++n) {
449 fprintf(stderr, "%s\n", msg[n]);
451 ExitProgram(ok ? EXIT_SUCCESS : EXIT_FAILURE);
458 main(int argc, char *argv[])
461 TERMINAL **terms = typeCalloc(TERMINAL *, argc + 1);
463 while ((ch = getopt(argc, argv, OPTS_COMMON "fmtx")) != -1) {
474 #ifdef NCURSES_VERSION
481 ExitProgram(EXIT_SUCCESS);
483 usage(ch == OPTS_USAGE);
488 #if HAVE_USE_EXTENDED_NAMES
489 use_extended_names(x_opt);
496 for (n = optind; n < argc; ++n) {
497 setupterm((NCURSES_CONST char *) argv[n], 1, &status);
498 if (status > 0 && cur_term != 0) {
499 terms[found++] = cur_term;
503 list_keys(terms, found);
505 setupterm(NULL, 1, (int *) 0);
512 ExitProgram(EXIT_SUCCESS);
519 printf("This program requires the terminfo arrays\n");
520 ExitProgram(EXIT_FAILURE);
523 #else /* !HAVE_TIGETSTR */
527 printf("This program requires the terminfo functions such as tigetstr\n");
528 ExitProgram(EXIT_FAILURE);
530 #endif /* HAVE_TIGETSTR */