1 /****************************************************************************
2 * Copyright (c) 2016-2017,2018 Free Software Foundation, Inc. *
4 * Permission is hereby granted, free of charge, to any person obtaining a *
5 * copy of this software and associated documentation files (the *
6 * "Software"), to deal in the Software without restriction, including *
7 * without limitation the rights to use, copy, modify, merge, publish, *
8 * distribute, distribute with modifications, sublicense, and/or sell *
9 * copies of the Software, and to permit persons to whom the Software is *
10 * furnished to do so, subject to the following conditions: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
18 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
21 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
23 * Except as contained in this notice, the name(s) of the above copyright *
24 * holders shall not be used in advertising or otherwise to promote the *
25 * sale, use or other dealings in this Software without prior written *
27 ****************************************************************************/
29 * $Id: list_keys.c,v 1.24 2018/05/20 18:56:56 tom Exp $
31 * Author: Thomas E Dickey
33 * List function keys for one or more terminals.
37 #include <test.priv.h>
41 #include <term_entry.h>
44 #define NCURSES_XNAMES 0
49 #if defined(HAVE_CURSES_DATA_BOOLNAMES) || defined(DECL_CURSES_DATA_BOOLNAMES)
51 static bool f_opt = FALSE;
52 static bool m_opt = FALSE;
53 static bool t_opt = FALSE;
54 static bool x_opt = FALSE;
60 #if HAVE_USE_EXTENDED_NAMES
70 #define Type(n) list[n].type
71 #define Name(n) list[n].name
74 full_name(const char *name)
76 const char *result = name;
78 for (n = 0; strnames[n] != 0; ++n) {
79 if (!strcmp(name, strnames[n])) {
80 result = strfnames[n];
88 show_key(const char *name, bool show)
92 NCURSES_CONST char *value = tigetstr((NCURSES_CONST char *) name);
97 if (value != 0 && value != (char *) -1) {
99 int ch = UChar(*value++);
102 _nc_STRCPY(buffer, "^?", sizeof(buffer));
105 _nc_STRCPY(buffer, "\\E", sizeof(buffer));
108 _nc_STRCPY(buffer, "\\b", sizeof(buffer));
111 _nc_STRCPY(buffer, "\\f", sizeof(buffer));
114 _nc_STRCPY(buffer, "\\n", sizeof(buffer));
117 _nc_STRCPY(buffer, "\\r", sizeof(buffer));
120 _nc_STRCPY(buffer, "\\s", sizeof(buffer));
123 _nc_STRCPY(buffer, "\\t", sizeof(buffer));
126 _nc_STRCPY(buffer, "\\^", sizeof(buffer));
129 _nc_STRCPY(buffer, "\\072", sizeof(buffer));
132 _nc_STRCPY(buffer, "\\\\", sizeof(buffer));
135 if (t_opt && ch == '"') {
136 _nc_STRCPY(buffer, "\"\"", sizeof(buffer));
137 } else if (isgraph(ch)) {
138 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
140 } else if (ch < 32) {
141 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
144 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
149 width += (int) strlen(buffer);
151 fputs(buffer, stdout);
162 valid_key(const char *name, TERMINAL **terms, int count)
167 for (k = 0; k < count; ++k) {
168 set_curterm(terms[k]);
169 if (show_key(name, FALSE)) {
179 compare_keys(const void *a, const void *b)
181 const KEYNAMES *p = (const KEYNAMES *) a;
182 const KEYNAMES *q = (const KEYNAMES *) b;
183 int result = (int) (p->type - q->type);
186 if (p->type == ktFunction &&
187 sscanf(p->name, "kf%d", &pn) == 1 &&
188 sscanf(q->name, "kf%d", &qn) == 1) {
191 result = strcmp(p->name, q->name);
202 for (j = 0; j < width; ++j) {
210 modified_key(const char *name)
212 static char result[100];
213 char buffer[sizeof(result) - 10];
216 static const char *modifiers[][2] =
221 {"as-", "alt-shift-"},
223 {"sc-", "ctrl-shift-"},
224 {"ac-", "alt-ctrl-"},
225 {"acs-" "alt-ctrl-shift-"},
228 if (strlen(name) > (sizeof(result) - 3)) {
230 } else if (sscanf(name, "kf%d%c", &value, &chr) == 1 &&
233 /* map 1,2,3,4,5,6,7 to 1,2,5,... */
234 int map = ((value - 1) / 12);
235 int key = ((value - 1) % 12);
236 int bit1 = (map & 2);
237 int bit2 = (map & 4);
239 map |= (bit1 << 1) | (bit2 >> 1);
240 _nc_SPRINTF(result, _nc_SLIMIT(sizeof(result))
241 "%sF%d", modifiers[map][(unsigned) f_opt], 1 + key);
242 } else if (sscanf(name, "k%[A-Z]%d%c", buffer, &value, &chr) == 2 &&
245 (!strcmp(buffer, "UP") ||
246 !strcmp(buffer, "DN") ||
247 !strcmp(buffer, "LFT") ||
248 !strcmp(buffer, "RIT") ||
249 !strcmp(buffer, "IC") ||
250 !strcmp(buffer, "DC") ||
251 !strcmp(buffer, "HOM") ||
252 !strcmp(buffer, "END") ||
253 !strcmp(buffer, "NXT") ||
254 !strcmp(buffer, "PRV"))) {
255 _nc_SPRINTF(result, _nc_SLIMIT(sizeof(result))
256 "%sk%s", modifiers[value - 1][(unsigned) f_opt], buffer);
257 } else if (sscanf(name, "k%[A-Z]%c", buffer, &chr) == 1 &&
258 (!strcmp(buffer, "UP") ||
259 !strcmp(buffer, "DN"))) {
260 _nc_SPRINTF(result, _nc_SLIMIT(sizeof(result))
261 "%sk%s", modifiers[1][(unsigned) f_opt], buffer);
269 list_keys(TERMINAL **terms, int count)
279 const char *name = f_opt ? "strfname" : "strname";
280 const char *modifier = "extended";
283 for (total = 0; strnames[total]; ++total) {
289 for (k = 0; k < count; ++k) {
290 set_curterm(terms[k]);
291 term = (TERMTYPE *) cur_term;
292 total += (size_t) (NUM_STRINGS(term) - STRCOUNT);
296 list = typeCalloc(KEYNAMES, total + 1);
297 for (j = 0; strnames[j]; ++j) {
299 if (sscanf(strnames[j], "kf%d", &k) == 1) {
300 Type(j) = ktFunction;
301 } else if (!(strncmp) (strnames[j], "kcu", 3)) {
304 Name(j) = strnames[j];
310 for (k = 0; k < count; ++k) {
311 set_curterm(terms[k]);
312 term = (TERMTYPE *) cur_term;
313 for (n = STRCOUNT; n < NUM_STRINGS(term); ++n) {
315 const char *estr = ExtStrname(term, (int) n, strnames);
316 for (m = STRCOUNT; m < j; ++m) {
317 if (!strcmp(estr, Name(m))) {
323 Type(j) = ktExtended;
331 qsort(list, actual, sizeof(KEYNAMES), compare_keys);
333 widths0 = (int) strlen(name);
335 widths1 = (int) strlen(modifier);
337 for (k = 0; k < count; ++k) {
338 set_curterm(terms[k]);
339 check = (int) strlen(termname());
343 for (j = 0; Name(j) != 0; ++j) {
344 if (valid_key(Name(j), terms, count)) {
345 const char *label = f_opt ? full_name(Name(j)) : Name(j);
346 check = (int) strlen(label);
349 for (k = 0; k < count; ++k) {
350 set_curterm(terms[k]);
351 check = show_key(Name(j), FALSE) + 1;
355 check = (int) strlen(modified_key(Name(j)));
364 printf("\"%s\"", name);
366 printf(",\"%s\"", modifier);
368 printf("%-*s", widths0, name);
370 printf(" %-*s", widths1, modifier);
372 for (k = 0; k < count; ++k) {
373 set_curterm(terms[k]);
375 printf(",\"%s\"", termname());
376 } else if (k + 1 >= count) {
377 printf(" %s", termname());
379 printf(" %-*s", widths2, termname());
384 widthsx = widths0 + ((count + 1) * widths2);
386 for (j = 0; Name(j) != 0; ++j) {
387 if (j == 0 || (Type(j) != Type(j - 1)))
389 if (valid_key(Name(j), terms, count)) {
390 const char *label = f_opt ? full_name(Name(j)) : Name(j);
392 printf("\"%s\"", label);
394 printf(",\"%s\"", modified_key(Name(j)));
396 printf("%-*s", widths0, label);
398 printf(" %-*s", widths1, modified_key(Name(j)));
400 for (k = 0; k < count; ++k) {
401 printf(t_opt ? "," : " ");
402 set_curterm(terms[k]);
403 check = show_key(Name(j), TRUE);
406 printf("%*s", widths2 - check, " ");
419 static const char *msg[] =
421 "Usage: list_keys [options] [terminal [terminal2 [...]]]",
423 "Print capabilities for terminal special keys.",
426 " -f print full names",
427 " -m print modifier-column for shift/control keys",
428 " -t print result as CSV table",
429 #ifdef NCURSES_VERSION
430 " -x print extended capabilities",
434 for (n = 0; n < SIZEOF(msg); ++n) {
435 fprintf(stderr, "%s\n", msg[n]);
437 ExitProgram(EXIT_FAILURE);
441 main(int argc, char *argv[])
444 TERMINAL **terms = typeCalloc(TERMINAL *, argc + 1);
446 while ((n = getopt(argc, argv, "fmtx")) != -1) {
457 #ifdef NCURSES_VERSION
468 #if HAVE_USE_EXTENDED_NAMES
469 use_extended_names(x_opt);
475 for (n = optind; n < argc; ++n) {
476 setupterm((NCURSES_CONST char *) argv[n], 1, &status);
477 if (status > 0 && cur_term != 0) {
478 terms[found++] = cur_term;
482 list_keys(terms, found);
484 setupterm(NULL, 1, (int *) 0);
491 ExitProgram(EXIT_SUCCESS);
496 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
498 printf("This program requires the terminfo arrays\n");
499 ExitProgram(EXIT_FAILURE);
502 #else /* !HAVE_TIGETSTR */
504 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
506 printf("This program requires the terminfo functions such as tigetstr\n");
507 ExitProgram(EXIT_FAILURE);
509 #endif /* HAVE_TIGETSTR */