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.33 2023/11/11 00:35:05 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;
56 #if NCURSES_XNAMES || HAVE_USE_EXTENDED_NAMES
57 static bool x_opt = FALSE;
64 #if HAVE_USE_EXTENDED_NAMES
74 #define Type(n) list[n].type
75 #define Name(n) list[n].name
78 failed(const char *msg)
81 ExitProgram(EXIT_FAILURE);
85 full_name(const char *name)
87 const char *result = name;
89 for (n = 0; strnames[n] != 0; ++n) {
90 if (!strcmp(name, strnames[n])) {
91 result = strfnames[n];
99 show_key(const char *name, bool show)
102 NCURSES_CONST char *value = tigetstr((NCURSES_CONST char *) name);
107 if (value != 0 && value != (char *) -1) {
108 while (*value != 0) {
110 int ch = UChar(*value++);
113 _nc_STRCPY(buffer, "^?", sizeof(buffer));
116 _nc_STRCPY(buffer, "\\E", sizeof(buffer));
119 _nc_STRCPY(buffer, "\\b", sizeof(buffer));
122 _nc_STRCPY(buffer, "\\f", sizeof(buffer));
125 _nc_STRCPY(buffer, "\\n", sizeof(buffer));
128 _nc_STRCPY(buffer, "\\r", sizeof(buffer));
131 _nc_STRCPY(buffer, "\\s", sizeof(buffer));
134 _nc_STRCPY(buffer, "\\t", sizeof(buffer));
137 _nc_STRCPY(buffer, "\\^", sizeof(buffer));
140 _nc_STRCPY(buffer, "\\072", sizeof(buffer));
143 _nc_STRCPY(buffer, "\\\\", sizeof(buffer));
146 if (t_opt && ch == '"') {
147 _nc_STRCPY(buffer, "\"\"", sizeof(buffer));
148 } else if (isgraph(ch)) {
149 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
151 } else if (ch < 32) {
152 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
155 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
160 width += (int) strlen(buffer);
162 fputs(buffer, stdout);
173 valid_key(const char *name, TERMINAL **terms, int count)
178 for (k = 0; k < count; ++k) {
179 set_curterm(terms[k]);
180 if (show_key(name, FALSE)) {
190 compare_keys(const void *a, const void *b)
192 const KEYNAMES *p = (const KEYNAMES *) a;
193 const KEYNAMES *q = (const KEYNAMES *) b;
194 int result = (int) (p->type - q->type);
197 if (p->type == ktFunction &&
198 sscanf(p->name, "kf%d", &pn) == 1 &&
199 sscanf(q->name, "kf%d", &qn) == 1) {
202 result = strcmp(p->name, q->name);
213 for (j = 0; j < width; ++j) {
221 modified_key(const char *name)
223 static char result[100];
224 char buffer[sizeof(result) - 10];
227 static const char *modifiers[][2] =
232 {"as-", "alt-shift-"},
234 {"sc-", "ctrl-shift-"},
235 {"ac-", "alt-ctrl-"},
236 {"acs-" "alt-ctrl-shift-"},
239 if (strlen(name) > (sizeof(result) - 3)) {
241 } else if (sscanf(name, "kf%d%c", &value, &chr) == 1 &&
244 /* map 1,2,3,4,5,6,7 to 1,2,5,... */
245 int map = ((value - 1) / 12);
246 int key = ((value - 1) % 12);
247 int bit1 = (map & 2);
248 int bit2 = (map & 4);
250 map |= (bit1 << 1) | (bit2 >> 1);
251 _nc_SPRINTF(result, _nc_SLIMIT(sizeof(result))
252 "%sF%d", modifiers[map][(unsigned) f_opt], 1 + key);
253 } else if (sscanf(name, "k%80[A-Z]%d%c", buffer, &value, &chr) == 2 &&
256 (!strcmp(buffer, "UP") ||
257 !strcmp(buffer, "DN") ||
258 !strcmp(buffer, "LFT") ||
259 !strcmp(buffer, "RIT") ||
260 !strcmp(buffer, "IC") ||
261 !strcmp(buffer, "DC") ||
262 !strcmp(buffer, "HOM") ||
263 !strcmp(buffer, "END") ||
264 !strcmp(buffer, "NXT") ||
265 !strcmp(buffer, "PRV"))) {
266 _nc_SPRINTF(result, _nc_SLIMIT(sizeof(result))
267 "%sk%s", modifiers[value - 1][(unsigned) f_opt], buffer);
268 } else if (sscanf(name, "k%80[A-Z]%c", buffer, &chr) == 1 &&
269 (!strcmp(buffer, "UP") ||
270 !strcmp(buffer, "DN"))) {
271 _nc_SPRINTF(result, _nc_SLIMIT(sizeof(result))
272 "%sk%s", modifiers[1][(unsigned) f_opt], buffer);
280 list_keys(TERMINAL **terms, int count)
290 const char *name = f_opt ? "strfname" : "strname";
291 const char *modifier = "extended";
294 for (total = 0; strnames[total]; ++total) {
299 for (k = 0; k < count; ++k) {
301 set_curterm(terms[k]);
302 term = (TERMTYPE *) cur_term;
303 total += (size_t) (NUM_STRINGS(term) - STRCOUNT);
307 list = typeCalloc(KEYNAMES, total + 1);
308 for (j = 0; strnames[j]; ++j) {
310 if (sscanf(strnames[j], "kf%d", &k) == 1) {
311 Type(j) = ktFunction;
312 } else if (!(strncmp) (strnames[j], "kcu", 3)) {
315 Name(j) = strnames[j];
321 for (k = 0; k < count; ++k) {
324 set_curterm(terms[k]);
325 term = (TERMTYPE *) cur_term;
326 for (n = STRCOUNT; n < NUM_STRINGS(term); ++n) {
328 const char *estr = ExtStrname(term, (int) n, strnames);
329 for (m = STRCOUNT; m < j; ++m) {
330 if (!strcmp(estr, Name(m))) {
336 Type(j) = ktExtended;
344 qsort(list, actual, sizeof(KEYNAMES), compare_keys);
346 widths0 = (int) strlen(name);
348 widths1 = (int) strlen(modifier);
350 for (k = 0; k < count; ++k) {
352 set_curterm(terms[k]);
353 if ((value = termname()) == NULL)
355 check = (int) strlen(value);
359 for (j = 0; Name(j) != 0; ++j) {
360 if (valid_key(Name(j), terms, count)) {
361 const char *label = f_opt ? full_name(Name(j)) : Name(j);
362 check = (int) strlen(label);
365 for (k = 0; k < count; ++k) {
366 set_curterm(terms[k]);
367 check = show_key(Name(j), FALSE) + 1;
371 check = (int) strlen(modified_key(Name(j)));
380 printf("\"%s\"", name);
382 printf(",\"%s\"", modifier);
384 printf("%-*s", widths0, name);
386 printf(" %-*s", widths1, modifier);
388 for (k = 0; k < count; ++k) {
389 set_curterm(terms[k]);
391 printf(",\"%s\"", termname());
392 } else if (k + 1 >= count) {
393 printf(" %s", termname());
395 printf(" %-*s", widths2, termname());
400 widthsx = widths0 + ((count + 1) * widths2);
402 for (j = 0; Name(j) != 0; ++j) {
403 if (j == 0 || (Type(j) != Type(j - 1)))
405 if (valid_key(Name(j), terms, count)) {
406 const char *label = f_opt ? full_name(Name(j)) : Name(j);
408 printf("\"%s\"", label);
410 printf(",\"%s\"", modified_key(Name(j)));
412 printf("%-*s", widths0, label);
414 printf(" %-*s", widths1, modified_key(Name(j)));
416 for (k = 0; k < count; ++k) {
417 printf(t_opt ? "," : " ");
418 set_curterm(terms[k]);
419 check = show_key(Name(j), TRUE);
422 printf("%*s", widths2 - check, " ");
435 static const char *msg[] =
437 "Usage: list_keys [options] [terminal [terminal2 [...]]]"
439 ,"Print capabilities for terminal special keys."
443 ," -f print full names"
444 ," -m print modifier-column for shift/control keys"
445 ," -t print result as CSV table"
446 #ifdef NCURSES_VERSION
447 ," -x print extended capabilities"
451 for (n = 0; n < SIZEOF(msg); ++n) {
452 fprintf(stderr, "%s\n", msg[n]);
454 ExitProgram(ok ? EXIT_SUCCESS : EXIT_FAILURE);
461 main(int argc, char *argv[])
464 TERMINAL **terms = typeCalloc(TERMINAL *, argc + 1);
466 while ((ch = getopt(argc, argv, OPTS_COMMON "fmtx")) != -1) {
477 #if NCURSES_XNAMES || HAVE_USE_EXTENDED_NAMES
484 ExitProgram(EXIT_SUCCESS);
486 usage(ch == OPTS_USAGE);
491 #if HAVE_USE_EXTENDED_NAMES
492 use_extended_names(x_opt);
499 for (n = optind; n < argc; ++n) {
500 setupterm((NCURSES_CONST char *) argv[n], 1, &status);
501 if (status > 0 && cur_term != 0) {
502 terms[found++] = cur_term;
506 list_keys(terms, found);
508 setupterm(NULL, 1, (int *) 0);
515 ExitProgram(EXIT_SUCCESS);
522 printf("This program requires the terminfo arrays\n");
523 ExitProgram(EXIT_FAILURE);
526 #else /* !HAVE_TIGETSTR */
530 printf("This program requires the terminfo functions such as tigetstr\n");
531 ExitProgram(EXIT_FAILURE);
533 #endif /* HAVE_TIGETSTR */