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.9 2016/06/18 22:18:30 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 t_opt = FALSE;
53 static bool x_opt = FALSE;
59 #if HAVE_USE_EXTENDED_NAMES
69 #define Type(n) list[n].type
70 #define Name(n) list[n].name
73 full_name(const char *name)
75 const char *result = name;
77 for (n = 0; strnames[n] != 0; ++n) {
78 if (!strcmp(name, strnames[n])) {
79 result = strfnames[n];
87 show_key(const char *name, bool show)
91 char *value = tigetstr(name);
96 if (value != 0 && value != (char *) -1) {
98 int ch = UChar(*value++);
101 strcpy(buffer, "^?");
104 strcpy(buffer, "\\E");
107 strcpy(buffer, "\\b");
110 strcpy(buffer, "\\f");
113 strcpy(buffer, "\\n");
116 strcpy(buffer, "\\r");
119 strcpy(buffer, "\\s");
122 strcpy(buffer, "\\t");
125 strcpy(buffer, "\\^");
128 strcpy(buffer, "\\072");
131 strcpy(buffer, "\\\\");
134 if (t_opt && ch == '"') {
135 strcpy(buffer, "\"\"");
136 } else if (isgraph(ch)) {
137 sprintf(buffer, "%c", ch);
138 } else if (ch < 32) {
139 sprintf(buffer, "^%c", ch + '@');
141 sprintf(buffer, "\\%03o", ch);
145 width += (int) strlen(buffer);
147 fputs(buffer, stdout);
158 valid_key(const char *name, TERMINAL ** terms, int count)
163 for (k = 0; k < count; ++k) {
164 set_curterm(terms[k]);
165 if (show_key(name, FALSE)) {
175 compare_keys(const void *a, const void *b)
177 const KEYNAMES *p = (const KEYNAMES *) a;
178 const KEYNAMES *q = (const KEYNAMES *) b;
179 int result = (int) (p->type - q->type);
182 if (p->type == ktFunction &&
183 sscanf(p->name, "kf%d", &pn) == 1 &&
184 sscanf(q->name, "kf%d", &qn) == 1) {
187 result = strcmp(p->name, q->name);
198 for (j = 0; j < width; ++j) {
206 list_keys(TERMINAL ** terms, int count)
215 const char *name = f_opt ? "strfname" : "strname";
218 for (total = 0; strnames[total]; ++total) {
224 for (k = 0; k < count; ++k) {
225 set_curterm(terms[k]);
226 term = &(cur_term->type);
227 total += (size_t) (NUM_STRINGS(term) - STRCOUNT);
231 list = typeCalloc(KEYNAMES, total + 1);
232 for (j = 0; strnames[j]; ++j) {
234 if (sscanf(strnames[j], "kf%d", &k) == 1) {
235 Type(j) = ktFunction;
236 } else if (!strncmp(strnames[j], "kcu", 3)) {
239 Name(j) = strnames[j];
245 for (k = 0; k < count; ++k) {
246 set_curterm(terms[k]);
247 term = &(cur_term->type);
248 for (n = STRCOUNT; n < NUM_STRINGS(term); ++n) {
250 const char *estr = ExtStrname(term, (int) n, strnames);
251 for (m = STRCOUNT; m < j; ++m) {
252 if (!strcmp(estr, Name(m))) {
258 Type(j) = ktExtended;
266 qsort(list, actual, sizeof(KEYNAMES), compare_keys);
268 widths0 = (int) strlen(name);
269 for (k = 0; k < count; ++k) {
270 set_curterm(terms[k]);
271 check = (int) strlen(termname());
275 for (j = 0; Name(j) != 0; ++j) {
276 if (valid_key(Name(j), terms, count)) {
277 const char *label = f_opt ? full_name(Name(j)) : Name(j);
278 check = (int) strlen(label);
281 for (k = 0; k < count; ++k) {
282 set_curterm(terms[k]);
283 check = show_key(Name(j), FALSE);
291 printf("\"%s\"", name);
293 printf("%-*s", widths0, name);
295 for (k = 0; k < count; ++k) {
296 set_curterm(terms[k]);
298 printf(",\"%s\"", termname());
299 } else if (k + 1 >= count) {
300 printf(" %s", termname());
302 printf(" %-*s", widths1, termname());
307 widthsx = widths0 + ((count + 1) * widths1);
309 for (j = 0; Name(j) != 0; ++j) {
310 if (j == 0 || (Type(j) != Type(j - 1)))
312 if (valid_key(Name(j), terms, count)) {
313 const char *label = f_opt ? full_name(Name(j)) : Name(j);
315 printf("\"%s\"", label);
317 printf("%-*s", widths0, label);
319 for (k = 0; k < count; ++k) {
320 printf(t_opt ? "," : " ");
321 set_curterm(terms[k]);
322 check = show_key(Name(j), TRUE);
325 printf("%*s", widths1 - check, " ");
337 static const char *msg[] =
339 "Usage: list_keys [options] [terminal [terminal2 [...]]]",
341 "Print capabilities for terminal special keys.",
344 " -f print full names",
345 " -t print result as CSV table",
346 #ifdef NCURSES_VERSION
347 " -x print extended capabilities",
351 for (n = 0; n < SIZEOF(msg); ++n) {
352 fprintf(stderr, "%s\n", msg[n]);
354 ExitProgram(EXIT_FAILURE);
358 main(int argc, char *argv[])
361 TERMINAL **terms = typeCalloc(TERMINAL *, argc);
363 while ((n = getopt(argc, argv, "ftx")) != -1) {
371 #ifdef NCURSES_VERSION
382 #if HAVE_USE_EXTENDED_NAMES
383 use_extended_names(x_opt);
386 for (n = optind; n < argc; ++n) {
387 setupterm((NCURSES_CONST char *) argv[n], 1, (int *) 0);
388 terms[n - optind] = cur_term;
390 list_keys(terms, argc - optind);
392 ExitProgram(EXIT_SUCCESS);
397 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
399 printf("This program requires the terminfo arrays\n");
400 ExitProgram(EXIT_FAILURE);
403 #else /* !HAVE_TIGETSTR */
405 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
407 printf("This program requires the terminfo functions such as tigetstr\n");
408 ExitProgram(EXIT_FAILURE);
410 #endif /* HAVE_TIGETSTR */