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.4 2016/06/04 22:39:47 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;
56 full_name(const char *name)
58 const char *result = name;
60 for (n = 0; strnames[n] != 0; ++n) {
61 if (!strcmp(name, strnames[n])) {
62 result = strfnames[n];
70 show_key(const char *name, bool show)
74 char *value = tigetstr(name);
79 if (value != 0 && value != (char *) -1) {
81 int ch = UChar(*value++);
87 strcpy(buffer, "\\E");
90 strcpy(buffer, "\\b");
93 strcpy(buffer, "\\f");
96 strcpy(buffer, "\\n");
99 strcpy(buffer, "\\r");
102 strcpy(buffer, "\\s");
105 strcpy(buffer, "\\t");
108 strcpy(buffer, "\\^");
111 strcpy(buffer, "\\072");
114 strcpy(buffer, "\\\\");
117 if (t_opt && ch == '"') {
118 strcpy(buffer, "\"\"");
119 } else if (isgraph(ch)) {
120 sprintf(buffer, "%c", ch);
121 } else if (ch < 32) {
122 sprintf(buffer, "^%c", ch + '@');
124 sprintf(buffer, "\\%03o", ch);
128 width += (int) strlen(buffer);
130 fputs(buffer, stdout);
141 valid_key(const char *name, TERMINAL ** terms, int count)
146 for (k = 0; k < count; ++k) {
147 set_curterm(terms[k]);
148 if (show_key(name, FALSE)) {
158 list_keys(TERMINAL ** terms, int count)
165 const char *name = f_opt ? "strfname" : "strname";
168 for (total = 0; strnames[total]; ++total) {
174 for (k = 0; k < count; ++k) {
175 set_curterm(terms[k]);
176 term = &(cur_term->type);
177 total += NUM_STRINGS(term) - STRCOUNT;
181 list = typeCalloc(const char *, total + 1);
182 for (j = 0; strnames[j]; ++j) {
183 list[j] = strnames[j];
189 for (k = 0; k < count; ++k) {
190 set_curterm(terms[k]);
191 term = &(cur_term->type);
192 for (n = STRCOUNT; n < NUM_STRINGS(term); ++n) {
194 const char *estr = ExtStrname(term, (int) n, strnames);
195 for (m = STRCOUNT; m < j; ++m) {
196 if (!strcmp(estr, list[m])) {
209 widths0 = (int) strlen(name);
210 for (k = 0; k < count; ++k) {
211 set_curterm(terms[k]);
212 check = (int) strlen(name);
216 for (j = 0; list[j] != 0; ++j) {
217 if (valid_key(list[j], terms, count)) {
218 const char *label = f_opt ? full_name(list[j]) : list[j];
219 check = (int) strlen(label);
222 for (k = 0; k < count; ++k) {
223 set_curterm(terms[k]);
224 check = show_key(list[j], FALSE);
232 printf("\"%s\"", name);
234 printf("%-*s", widths0, name);
236 for (k = 0; k < count; ++k) {
237 set_curterm(terms[k]);
239 printf(",\"%s\"", termname());
240 } else if (k + 1 >= count) {
241 printf(" %s", termname());
243 printf(" %-*s", widths1, termname());
248 for (j = 0; list[j] != 0; ++j) {
249 if (valid_key(list[j], terms, count)) {
250 const char *label = f_opt ? full_name(list[j]) : list[j];
252 printf("\"%s\"", label);
254 printf("%-*s", widths0, label);
256 for (k = 0; k < count; ++k) {
257 printf(t_opt ? "," : " ");
258 set_curterm(terms[k]);
259 check = show_key(list[j], TRUE);
262 printf("%*s", widths1 + 1 - check, " ");
274 static const char *msg[] =
276 "Usage: list_keys [options] [terminal [terminal2 [...]]]",
278 "Print capabilities for terminal special keys.",
281 " -f print full names",
282 " -t print result as CSV table",
283 #ifdef NCURSES_VERSION
284 " -x print extended capabilities",
288 for (n = 0; n < SIZEOF(msg); ++n) {
289 fprintf(stderr, "%s\n", msg[n]);
291 ExitProgram(EXIT_FAILURE);
295 main(int argc, char *argv[])
298 TERMINAL **terms = typeCalloc(TERMINAL *, argc);
300 while ((n = getopt(argc, argv, "ftx")) != -1) {
308 #ifdef NCURSES_VERSION
319 #if HAVE_USE_EXTENDED_NAMES
320 use_extended_names(x_opt);
323 for (n = optind; n < argc; ++n) {
324 setupterm((NCURSES_CONST char *) argv[n], 1, (int *) 0);
325 terms[n - optind] = cur_term;
327 list_keys(terms, argc - optind);
329 ExitProgram(EXIT_SUCCESS);
334 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
336 printf("This program requires the terminfo arrays\n");
337 ExitProgram(EXIT_FAILURE);
340 #else /* !HAVE_TIGETSTR */
342 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
344 printf("This program requires the terminfo functions such as tigetstr\n");
345 ExitProgram(EXIT_FAILURE);
347 #endif /* HAVE_TIGETSTR */