1 /****************************************************************************
2 * Copyright 2018-2019,2020 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.26 2020/02/02 23:34:34 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 full_name(const char *name)
77 const char *result = name;
79 for (n = 0; strnames[n] != 0; ++n) {
80 if (!strcmp(name, strnames[n])) {
81 result = strfnames[n];
89 show_key(const char *name, bool show)
92 NCURSES_CONST char *value = tigetstr((NCURSES_CONST char *) name);
97 if (value != 0 && value != (char *) -1) {
100 int ch = UChar(*value++);
103 _nc_STRCPY(buffer, "^?", sizeof(buffer));
106 _nc_STRCPY(buffer, "\\E", sizeof(buffer));
109 _nc_STRCPY(buffer, "\\b", sizeof(buffer));
112 _nc_STRCPY(buffer, "\\f", sizeof(buffer));
115 _nc_STRCPY(buffer, "\\n", sizeof(buffer));
118 _nc_STRCPY(buffer, "\\r", sizeof(buffer));
121 _nc_STRCPY(buffer, "\\s", sizeof(buffer));
124 _nc_STRCPY(buffer, "\\t", sizeof(buffer));
127 _nc_STRCPY(buffer, "\\^", sizeof(buffer));
130 _nc_STRCPY(buffer, "\\072", sizeof(buffer));
133 _nc_STRCPY(buffer, "\\\\", sizeof(buffer));
136 if (t_opt && ch == '"') {
137 _nc_STRCPY(buffer, "\"\"", sizeof(buffer));
138 } else if (isgraph(ch)) {
139 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
141 } else if (ch < 32) {
142 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
145 _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
150 width += (int) strlen(buffer);
152 fputs(buffer, stdout);
163 valid_key(const char *name, TERMINAL **terms, int count)
168 for (k = 0; k < count; ++k) {
169 set_curterm(terms[k]);
170 if (show_key(name, FALSE)) {
180 compare_keys(const void *a, const void *b)
182 const KEYNAMES *p = (const KEYNAMES *) a;
183 const KEYNAMES *q = (const KEYNAMES *) b;
184 int result = (int) (p->type - q->type);
187 if (p->type == ktFunction &&
188 sscanf(p->name, "kf%d", &pn) == 1 &&
189 sscanf(q->name, "kf%d", &qn) == 1) {
192 result = strcmp(p->name, q->name);
203 for (j = 0; j < width; ++j) {
211 modified_key(const char *name)
213 static char result[100];
214 char buffer[sizeof(result) - 10];
217 static const char *modifiers[][2] =
222 {"as-", "alt-shift-"},
224 {"sc-", "ctrl-shift-"},
225 {"ac-", "alt-ctrl-"},
226 {"acs-" "alt-ctrl-shift-"},
229 if (strlen(name) > (sizeof(result) - 3)) {
231 } else if (sscanf(name, "kf%d%c", &value, &chr) == 1 &&
234 /* map 1,2,3,4,5,6,7 to 1,2,5,... */
235 int map = ((value - 1) / 12);
236 int key = ((value - 1) % 12);
237 int bit1 = (map & 2);
238 int bit2 = (map & 4);
240 map |= (bit1 << 1) | (bit2 >> 1);
241 _nc_SPRINTF(result, _nc_SLIMIT(sizeof(result))
242 "%sF%d", modifiers[map][(unsigned) f_opt], 1 + key);
243 } else if (sscanf(name, "k%[A-Z]%d%c", buffer, &value, &chr) == 2 &&
246 (!strcmp(buffer, "UP") ||
247 !strcmp(buffer, "DN") ||
248 !strcmp(buffer, "LFT") ||
249 !strcmp(buffer, "RIT") ||
250 !strcmp(buffer, "IC") ||
251 !strcmp(buffer, "DC") ||
252 !strcmp(buffer, "HOM") ||
253 !strcmp(buffer, "END") ||
254 !strcmp(buffer, "NXT") ||
255 !strcmp(buffer, "PRV"))) {
256 _nc_SPRINTF(result, _nc_SLIMIT(sizeof(result))
257 "%sk%s", modifiers[value - 1][(unsigned) f_opt], buffer);
258 } else if (sscanf(name, "k%[A-Z]%c", buffer, &chr) == 1 &&
259 (!strcmp(buffer, "UP") ||
260 !strcmp(buffer, "DN"))) {
261 _nc_SPRINTF(result, _nc_SLIMIT(sizeof(result))
262 "%sk%s", modifiers[1][(unsigned) f_opt], buffer);
270 list_keys(TERMINAL **terms, int count)
280 const char *name = f_opt ? "strfname" : "strname";
281 const char *modifier = "extended";
284 for (total = 0; strnames[total]; ++total) {
289 for (k = 0; k < count; ++k) {
291 set_curterm(terms[k]);
292 term = (TERMTYPE *) cur_term;
293 total += (size_t) (NUM_STRINGS(term) - STRCOUNT);
297 list = typeCalloc(KEYNAMES, total + 1);
298 for (j = 0; strnames[j]; ++j) {
300 if (sscanf(strnames[j], "kf%d", &k) == 1) {
301 Type(j) = ktFunction;
302 } else if (!(strncmp) (strnames[j], "kcu", 3)) {
305 Name(j) = strnames[j];
311 for (k = 0; k < count; ++k) {
314 set_curterm(terms[k]);
315 term = (TERMTYPE *) cur_term;
316 for (n = STRCOUNT; n < NUM_STRINGS(term); ++n) {
318 const char *estr = ExtStrname(term, (int) n, strnames);
319 for (m = STRCOUNT; m < j; ++m) {
320 if (!strcmp(estr, Name(m))) {
326 Type(j) = ktExtended;
334 qsort(list, actual, sizeof(KEYNAMES), compare_keys);
336 widths0 = (int) strlen(name);
338 widths1 = (int) strlen(modifier);
340 for (k = 0; k < count; ++k) {
341 set_curterm(terms[k]);
342 check = (int) strlen(termname());
346 for (j = 0; Name(j) != 0; ++j) {
347 if (valid_key(Name(j), terms, count)) {
348 const char *label = f_opt ? full_name(Name(j)) : Name(j);
349 check = (int) strlen(label);
352 for (k = 0; k < count; ++k) {
353 set_curterm(terms[k]);
354 check = show_key(Name(j), FALSE) + 1;
358 check = (int) strlen(modified_key(Name(j)));
367 printf("\"%s\"", name);
369 printf(",\"%s\"", modifier);
371 printf("%-*s", widths0, name);
373 printf(" %-*s", widths1, modifier);
375 for (k = 0; k < count; ++k) {
376 set_curterm(terms[k]);
378 printf(",\"%s\"", termname());
379 } else if (k + 1 >= count) {
380 printf(" %s", termname());
382 printf(" %-*s", widths2, termname());
387 widthsx = widths0 + ((count + 1) * widths2);
389 for (j = 0; Name(j) != 0; ++j) {
390 if (j == 0 || (Type(j) != Type(j - 1)))
392 if (valid_key(Name(j), terms, count)) {
393 const char *label = f_opt ? full_name(Name(j)) : Name(j);
395 printf("\"%s\"", label);
397 printf(",\"%s\"", modified_key(Name(j)));
399 printf("%-*s", widths0, label);
401 printf(" %-*s", widths1, modified_key(Name(j)));
403 for (k = 0; k < count; ++k) {
404 printf(t_opt ? "," : " ");
405 set_curterm(terms[k]);
406 check = show_key(Name(j), TRUE);
409 printf("%*s", widths2 - check, " ");
422 static const char *msg[] =
424 "Usage: list_keys [options] [terminal [terminal2 [...]]]",
426 "Print capabilities for terminal special keys.",
429 " -f print full names",
430 " -m print modifier-column for shift/control keys",
431 " -t print result as CSV table",
432 #ifdef NCURSES_VERSION
433 " -x print extended capabilities",
437 for (n = 0; n < SIZEOF(msg); ++n) {
438 fprintf(stderr, "%s\n", msg[n]);
440 ExitProgram(EXIT_FAILURE);
444 main(int argc, char *argv[])
447 TERMINAL **terms = typeCalloc(TERMINAL *, argc + 1);
449 while ((n = getopt(argc, argv, "fmtx")) != -1) {
460 #ifdef NCURSES_VERSION
471 #if HAVE_USE_EXTENDED_NAMES
472 use_extended_names(x_opt);
478 for (n = optind; n < argc; ++n) {
479 setupterm((NCURSES_CONST char *) argv[n], 1, &status);
480 if (status > 0 && cur_term != 0) {
481 terms[found++] = cur_term;
485 list_keys(terms, found);
487 setupterm(NULL, 1, (int *) 0);
494 ExitProgram(EXIT_SUCCESS);
499 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
501 printf("This program requires the terminfo arrays\n");
502 ExitProgram(EXIT_FAILURE);
505 #else /* !HAVE_TIGETSTR */
507 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
509 printf("This program requires the terminfo functions such as tigetstr\n");
510 ExitProgram(EXIT_FAILURE);
512 #endif /* HAVE_TIGETSTR */