1 /****************************************************************************
2 * Copyright (c) 2016 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.15 2016/07/09 18:21:24 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(name);
97 if (value != 0 && value != (char *) -1) {
99 int ch = UChar(*value++);
102 strcpy(buffer, "^?");
105 strcpy(buffer, "\\E");
108 strcpy(buffer, "\\b");
111 strcpy(buffer, "\\f");
114 strcpy(buffer, "\\n");
117 strcpy(buffer, "\\r");
120 strcpy(buffer, "\\s");
123 strcpy(buffer, "\\t");
126 strcpy(buffer, "\\^");
129 strcpy(buffer, "\\072");
132 strcpy(buffer, "\\\\");
135 if (t_opt && ch == '"') {
136 strcpy(buffer, "\"\"");
137 } else if (isgraph(ch)) {
138 sprintf(buffer, "%c", ch);
139 } else if (ch < 32) {
140 sprintf(buffer, "^%c", ch + '@');
142 sprintf(buffer, "\\%03o", ch);
146 width += (int) strlen(buffer);
148 fputs(buffer, stdout);
159 valid_key(const char *name, TERMINAL ** terms, int count)
164 for (k = 0; k < count; ++k) {
165 set_curterm(terms[k]);
166 if (show_key(name, FALSE)) {
176 compare_keys(const void *a, const void *b)
178 const KEYNAMES *p = (const KEYNAMES *) a;
179 const KEYNAMES *q = (const KEYNAMES *) b;
180 int result = (int) (p->type - q->type);
183 if (p->type == ktFunction &&
184 sscanf(p->name, "kf%d", &pn) == 1 &&
185 sscanf(q->name, "kf%d", &qn) == 1) {
188 result = strcmp(p->name, q->name);
199 for (j = 0; j < width; ++j) {
207 modified_key(const char *name)
209 static char result[80];
210 char buffer[sizeof(result)];
213 static const char *modifiers[][2] =
218 {"as-", "alt-shift-"},
220 {"sc-", "ctrl-shift-"},
221 {"ac-", "alt-ctrl-"},
222 {"acs-" "alt-ctrl-shift-"},
225 if (strlen(name) > (sizeof(result) - 3)) {
227 } else if (sscanf(name, "kf%d%c", &value, &chr) == 1 &&
230 /* map 1,2,3,4,5,6,7 to 1,2,5,... */
231 int map = ((value - 1) / 12);
232 int key = ((value - 1) % 12);
233 int bit1 = (map & 2);
234 int bit2 = (map & 4);
236 map |= (bit1 << 1) | (bit2 >> 1);
237 sprintf(result, "%sF%d", modifiers[map][f_opt], 1 + key);
238 } else if (sscanf(name, "k%[A-Z]%d%c", buffer, &value, &chr) == 2 &&
241 (!strcmp(buffer, "UP") ||
242 !strcmp(buffer, "DN") ||
243 !strcmp(buffer, "LFT") ||
244 !strcmp(buffer, "RIT") ||
245 !strcmp(buffer, "IC") ||
246 !strcmp(buffer, "DC") ||
247 !strcmp(buffer, "HOM") ||
248 !strcmp(buffer, "END") ||
249 !strcmp(buffer, "NXT") ||
250 !strcmp(buffer, "PRV"))) {
251 sprintf(result, "%sk%s", modifiers[value - 1][f_opt], buffer);
252 } else if (sscanf(name, "k%[A-Z]%c", buffer, &chr) == 1 &&
253 (!strcmp(buffer, "UP") ||
254 !strcmp(buffer, "DN"))) {
255 sprintf(result, "%sk%s", modifiers[1][f_opt], buffer);
263 list_keys(TERMINAL ** terms, int count)
273 const char *name = f_opt ? "strfname" : "strname";
274 const char *modifier = "extended";
277 for (total = 0; strnames[total]; ++total) {
283 for (k = 0; k < count; ++k) {
284 set_curterm(terms[k]);
285 term = &(cur_term->type);
286 total += (size_t) (NUM_STRINGS(term) - STRCOUNT);
290 list = typeCalloc(KEYNAMES, total + 1);
291 for (j = 0; strnames[j]; ++j) {
293 if (sscanf(strnames[j], "kf%d", &k) == 1) {
294 Type(j) = ktFunction;
295 } else if (!strncmp(strnames[j], "kcu", 3)) {
298 Name(j) = strnames[j];
304 for (k = 0; k < count; ++k) {
305 set_curterm(terms[k]);
306 term = &(cur_term->type);
307 for (n = STRCOUNT; n < NUM_STRINGS(term); ++n) {
309 const char *estr = ExtStrname(term, (int) n, strnames);
310 for (m = STRCOUNT; m < j; ++m) {
311 if (!strcmp(estr, Name(m))) {
317 Type(j) = ktExtended;
325 qsort(list, actual, sizeof(KEYNAMES), compare_keys);
327 widths0 = (int) strlen(name);
329 widths1 = (int) strlen(modifier);
331 for (k = 0; k < count; ++k) {
332 set_curterm(terms[k]);
333 check = (int) strlen(termname());
337 for (j = 0; Name(j) != 0; ++j) {
338 if (valid_key(Name(j), terms, count)) {
339 const char *label = f_opt ? full_name(Name(j)) : Name(j);
340 check = (int) strlen(label);
343 for (k = 0; k < count; ++k) {
344 set_curterm(terms[k]);
345 check = show_key(Name(j), FALSE) + 1;
349 check = (int) strlen(modified_key(Name(j)));
358 printf("\"%s\"", name);
360 printf(",\"%s\"", modifier);
362 printf("%-*s", widths0, name);
364 printf(" %-*s", widths1, modifier);
366 for (k = 0; k < count; ++k) {
367 set_curterm(terms[k]);
369 printf(",\"%s\"", termname());
370 } else if (k + 1 >= count) {
371 printf(" %s", termname());
373 printf(" %-*s", widths2, termname());
378 widthsx = widths0 + ((count + 1) * widths2);
380 for (j = 0; Name(j) != 0; ++j) {
381 if (j == 0 || (Type(j) != Type(j - 1)))
383 if (valid_key(Name(j), terms, count)) {
384 const char *label = f_opt ? full_name(Name(j)) : Name(j);
386 printf("\"%s\"", label);
388 printf(",\"%s\"", modified_key(Name(j)));
390 printf("%-*s", widths0, label);
392 printf(" %-*s", widths1, modified_key(Name(j)));
394 for (k = 0; k < count; ++k) {
395 printf(t_opt ? "," : " ");
396 set_curterm(terms[k]);
397 check = show_key(Name(j), TRUE);
400 printf("%*s", widths2 - check, " ");
413 static const char *msg[] =
415 "Usage: list_keys [options] [terminal [terminal2 [...]]]",
417 "Print capabilities for terminal special keys.",
420 " -f print full names",
421 " -m print modifier-column for shift/control keys",
422 " -t print result as CSV table",
423 #ifdef NCURSES_VERSION
424 " -x print extended capabilities",
428 for (n = 0; n < SIZEOF(msg); ++n) {
429 fprintf(stderr, "%s\n", msg[n]);
431 ExitProgram(EXIT_FAILURE);
435 main(int argc, char *argv[])
438 TERMINAL **terms = typeCalloc(TERMINAL *, argc + 1);
440 while ((n = getopt(argc, argv, "fmtx")) != -1) {
451 #ifdef NCURSES_VERSION
462 #if HAVE_USE_EXTENDED_NAMES
463 use_extended_names(x_opt);
469 for (n = optind; n < argc; ++n) {
470 setupterm((NCURSES_CONST char *) argv[n], 1, &status);
471 if (status > 0 && cur_term != 0) {
472 terms[found++] = cur_term;
476 list_keys(terms, found);
478 setupterm(NULL, 1, (int *) 0);
483 ExitProgram(EXIT_SUCCESS);
488 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
490 printf("This program requires the terminfo arrays\n");
491 ExitProgram(EXIT_FAILURE);
494 #else /* !HAVE_TIGETSTR */
496 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
498 printf("This program requires the terminfo functions such as tigetstr\n");
499 ExitProgram(EXIT_FAILURE);
501 #endif /* HAVE_TIGETSTR */