]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/list_keys.c
ncurses 6.1 - patch 20191019
[ncurses.git] / test / list_keys.c
1 /****************************************************************************
2  * Copyright (c) 2016-2018,2019 Free Software Foundation, Inc.              *
3  *                                                                          *
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:                 *
11  *                                                                          *
12  * The above copyright notice and this permission notice shall be included  *
13  * in all copies or substantial portions of the Software.                   *
14  *                                                                          *
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.                               *
22  *                                                                          *
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       *
26  * authorization.                                                           *
27  ****************************************************************************/
28 /*
29  * $Id: list_keys.c,v 1.25 2019/08/24 23:11:01 tom Exp $
30  *
31  * Author: Thomas E Dickey
32  *
33  * List function keys for one or more terminals.
34  */
35
36 #define USE_TINFO
37 #include <test.priv.h>
38
39 #if NCURSES_XNAMES
40 #if HAVE_TERM_ENTRY_H
41 #include <term_entry.h>
42 #else
43 #undef NCURSES_XNAMES
44 #define NCURSES_XNAMES 0
45 #endif
46 #endif
47
48 #if HAVE_TIGETSTR
49 #if defined(HAVE_CURSES_DATA_BOOLNAMES) || defined(DECL_CURSES_DATA_BOOLNAMES)
50
51 static bool f_opt = FALSE;
52 static bool m_opt = FALSE;
53 static bool t_opt = FALSE;
54 static bool x_opt = FALSE;
55
56 typedef enum {
57     ktCursor
58     ,ktFunction
59     ,ktOther
60 #if HAVE_USE_EXTENDED_NAMES
61     ,ktExtended
62 #endif
63 } KEYTYPE;
64
65 typedef struct {
66     KEYTYPE type;
67     const char *name;
68 } KEYNAMES;
69
70 #define Type(n) list[n].type
71 #define Name(n) list[n].name
72
73 static const char *
74 full_name(const char *name)
75 {
76     const char *result = name;
77     int n;
78     for (n = 0; strnames[n] != 0; ++n) {
79         if (!strcmp(name, strnames[n])) {
80             result = strfnames[n];
81             break;
82         }
83     }
84     return result;
85 }
86
87 static int
88 show_key(const char *name, bool show)
89 {
90     int width = 0;
91     NCURSES_CONST char *value = tigetstr((NCURSES_CONST char *) name);
92
93     if (show && t_opt)
94         fputc('"', stdout);
95
96     if (value != 0 && value != (char *) -1) {
97         while (*value != 0) {
98             char buffer[10];
99             int ch = UChar(*value++);
100             switch (ch) {
101             case '\177':
102                 _nc_STRCPY(buffer, "^?", sizeof(buffer));
103                 break;
104             case '\033':
105                 _nc_STRCPY(buffer, "\\E", sizeof(buffer));
106                 break;
107             case '\b':
108                 _nc_STRCPY(buffer, "\\b", sizeof(buffer));
109                 break;
110             case '\f':
111                 _nc_STRCPY(buffer, "\\f", sizeof(buffer));
112                 break;
113             case '\n':
114                 _nc_STRCPY(buffer, "\\n", sizeof(buffer));
115                 break;
116             case '\r':
117                 _nc_STRCPY(buffer, "\\r", sizeof(buffer));
118                 break;
119             case ' ':
120                 _nc_STRCPY(buffer, "\\s", sizeof(buffer));
121                 break;
122             case '\t':
123                 _nc_STRCPY(buffer, "\\t", sizeof(buffer));
124                 break;
125             case '^':
126                 _nc_STRCPY(buffer, "\\^", sizeof(buffer));
127                 break;
128             case ':':
129                 _nc_STRCPY(buffer, "\\072", sizeof(buffer));
130                 break;
131             case '\\':
132                 _nc_STRCPY(buffer, "\\\\", sizeof(buffer));
133                 break;
134             default:
135                 if (t_opt && ch == '"') {
136                     _nc_STRCPY(buffer, "\"\"", sizeof(buffer));
137                 } else if (isgraph(ch)) {
138                     _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
139                                 "%c", ch);
140                 } else if (ch < 32) {
141                     _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
142                                 "^%c", ch + '@');
143                 } else {
144                     _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer))
145                                 "\\%03o", ch);
146                 }
147                 break;
148             }
149             width += (int) strlen(buffer);
150             if (show)
151                 fputs(buffer, stdout);
152         }
153     }
154
155     if (show && t_opt)
156         fputc('"', stdout);
157
158     return width;
159 }
160
161 static bool
162 valid_key(const char *name, TERMINAL **terms, int count)
163 {
164     bool result = FALSE;
165     if (*name == 'k') {
166         int k;
167         for (k = 0; k < count; ++k) {
168             set_curterm(terms[k]);
169             if (show_key(name, FALSE)) {
170                 result = TRUE;
171                 break;
172             }
173         }
174     }
175     return result;
176 }
177
178 static int
179 compare_keys(const void *a, const void *b)
180 {
181     const KEYNAMES *p = (const KEYNAMES *) a;
182     const KEYNAMES *q = (const KEYNAMES *) b;
183     int result = (int) (p->type - q->type);
184     int pn, qn;
185     if (result == 0) {
186         if (p->type == ktFunction &&
187             sscanf(p->name, "kf%d", &pn) == 1 &&
188             sscanf(q->name, "kf%d", &qn) == 1) {
189             result = (pn - qn);
190         } else {
191             result = strcmp(p->name, q->name);
192         }
193     }
194     return result;
195 }
196
197 static void
198 draw_line(int width)
199 {
200     if (!t_opt) {
201         int j;
202         for (j = 0; j < width; ++j) {
203             printf("-");
204         }
205         printf("\n");
206     }
207 }
208
209 static const char *
210 modified_key(const char *name)
211 {
212     static char result[100];
213     char buffer[sizeof(result) - 10];
214     int value;
215     char chr;
216     static const char *modifiers[][2] =
217     {
218         {"", ""},
219         {"s-", "shift-"},
220         {"a-", "alt-"},
221         {"as-", "alt-shift-"},
222         {"c-", "ctrl-"},
223         {"sc-", "ctrl-shift-"},
224         {"ac-", "alt-ctrl-"},
225         {"acs-" "alt-ctrl-shift-"},
226     };
227
228     if (strlen(name) > (sizeof(result) - 3)) {
229         *result = '\0';
230     } else if (sscanf(name, "kf%d%c", &value, &chr) == 1 &&
231                value >= 1 &&
232                value <= 63) {
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);
238         map &= ~6;
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 &&
243                (value > 1 &&
244                 value <= 8) &&
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);
262     } else {
263         *result = '\0';
264     }
265     return result;
266 }
267
268 static void
269 list_keys(TERMINAL **terms, int count)
270 {
271     int j, k;
272     int widths0 = 0;
273     int widths1 = 0;
274     int widths2 = 0;
275     int widthsx;
276     int check;
277     size_t total = 0;
278     size_t actual = 0;
279     const char *name = f_opt ? "strfname" : "strname";
280     const char *modifier = "extended";
281     KEYNAMES *list;
282
283     for (total = 0; strnames[total]; ++total) {
284         ;
285     }
286 #if NCURSES_XNAMES
287     if (x_opt) {
288         for (k = 0; k < count; ++k) {
289             TERMTYPE *term;
290             set_curterm(terms[k]);
291             term = (TERMTYPE *) cur_term;
292             total += (size_t) (NUM_STRINGS(term) - STRCOUNT);
293         }
294     }
295 #endif
296     list = typeCalloc(KEYNAMES, total + 1);
297     for (j = 0; strnames[j]; ++j) {
298         Type(j) = ktOther;
299         if (sscanf(strnames[j], "kf%d", &k) == 1) {
300             Type(j) = ktFunction;
301         } else if (!(strncmp) (strnames[j], "kcu", 3)) {
302             Type(j) = ktCursor;
303         }
304         Name(j) = strnames[j];
305     }
306 #if NCURSES_XNAMES
307     if (x_opt) {
308         int m, n;
309
310         for (k = 0; k < count; ++k) {
311             TERMTYPE *term;
312
313             set_curterm(terms[k]);
314             term = (TERMTYPE *) cur_term;
315             for (n = STRCOUNT; n < NUM_STRINGS(term); ++n) {
316                 bool found = FALSE;
317                 const char *estr = ExtStrname(term, (int) n, strnames);
318                 for (m = STRCOUNT; m < j; ++m) {
319                     if (!strcmp(estr, Name(m))) {
320                         found = TRUE;
321                         break;
322                     }
323                 }
324                 if (!found) {
325                     Type(j) = ktExtended;
326                     Name(j++) = estr;
327                 }
328             }
329         }
330     }
331 #endif
332     actual = (size_t) j;
333     qsort(list, actual, sizeof(KEYNAMES), compare_keys);
334
335     widths0 = (int) strlen(name);
336     if (m_opt)
337         widths1 = (int) strlen(modifier);
338
339     for (k = 0; k < count; ++k) {
340         set_curterm(terms[k]);
341         check = (int) strlen(termname());
342         if (widths2 < check)
343             widths2 = check;
344     }
345     for (j = 0; Name(j) != 0; ++j) {
346         if (valid_key(Name(j), terms, count)) {
347             const char *label = f_opt ? full_name(Name(j)) : Name(j);
348             check = (int) strlen(label);
349             if (widths0 < check)
350                 widths0 = check;
351             for (k = 0; k < count; ++k) {
352                 set_curterm(terms[k]);
353                 check = show_key(Name(j), FALSE) + 1;
354                 if (widths2 < check)
355                     widths2 = check;
356                 if (m_opt) {
357                     check = (int) strlen(modified_key(Name(j)));
358                     if (widths1 < check)
359                         widths1 = check;
360                 }
361             }
362         }
363     }
364
365     if (t_opt) {
366         printf("\"%s\"", name);
367         if (m_opt)
368             printf(",\"%s\"", modifier);
369     } else {
370         printf("%-*s", widths0, name);
371         if (m_opt)
372             printf(" %-*s", widths1, modifier);
373     }
374     for (k = 0; k < count; ++k) {
375         set_curterm(terms[k]);
376         if (t_opt) {
377             printf(",\"%s\"", termname());
378         } else if (k + 1 >= count) {
379             printf(" %s", termname());
380         } else {
381             printf(" %-*s", widths2, termname());
382         }
383     }
384     printf("\n");
385
386     widthsx = widths0 + ((count + 1) * widths2);
387
388     for (j = 0; Name(j) != 0; ++j) {
389         if (j == 0 || (Type(j) != Type(j - 1)))
390             draw_line(widthsx);
391         if (valid_key(Name(j), terms, count)) {
392             const char *label = f_opt ? full_name(Name(j)) : Name(j);
393             if (t_opt) {
394                 printf("\"%s\"", label);
395                 if (m_opt)
396                     printf(",\"%s\"", modified_key(Name(j)));
397             } else {
398                 printf("%-*s", widths0, label);
399                 if (m_opt)
400                     printf(" %-*s", widths1, modified_key(Name(j)));
401             }
402             for (k = 0; k < count; ++k) {
403                 printf(t_opt ? "," : " ");
404                 set_curterm(terms[k]);
405                 check = show_key(Name(j), TRUE);
406                 if (!t_opt) {
407                     if (k + 1 < count) {
408                         printf("%*s", widths2 - check, " ");
409                     }
410                 }
411             }
412             printf("\n");
413         }
414     }
415     free(list);
416 }
417
418 static void
419 usage(void)
420 {
421     static const char *msg[] =
422     {
423         "Usage: list_keys [options] [terminal [terminal2 [...]]]",
424         "",
425         "Print capabilities for terminal special keys.",
426         "",
427         "Options:",
428         " -f       print full names",
429         " -m       print modifier-column for shift/control keys",
430         " -t       print result as CSV table",
431 #ifdef NCURSES_VERSION
432         " -x       print extended capabilities",
433 #endif
434     };
435     unsigned n;
436     for (n = 0; n < SIZEOF(msg); ++n) {
437         fprintf(stderr, "%s\n", msg[n]);
438     }
439     ExitProgram(EXIT_FAILURE);
440 }
441
442 int
443 main(int argc, char *argv[])
444 {
445     int n;
446     TERMINAL **terms = typeCalloc(TERMINAL *, argc + 1);
447
448     while ((n = getopt(argc, argv, "fmtx")) != -1) {
449         switch (n) {
450         case 'f':
451             f_opt = TRUE;
452             break;
453         case 'm':
454             m_opt = TRUE;
455             break;
456         case 't':
457             t_opt = TRUE;
458             break;
459 #ifdef NCURSES_VERSION
460         case 'x':
461             x_opt = TRUE;
462             break;
463 #endif
464         default:
465             usage();
466             break;
467         }
468     }
469
470 #if HAVE_USE_EXTENDED_NAMES
471     use_extended_names(x_opt);
472 #endif
473
474     if (optind < argc) {
475         int found = 0;
476         int status;
477         for (n = optind; n < argc; ++n) {
478             setupterm((NCURSES_CONST char *) argv[n], 1, &status);
479             if (status > 0 && cur_term != 0) {
480                 terms[found++] = cur_term;
481             }
482         }
483         if (found)
484             list_keys(terms, found);
485     } else {
486         setupterm(NULL, 1, (int *) 0);
487         terms[0] = cur_term;
488         list_keys(terms, 1);
489     }
490
491     free(terms);
492
493     ExitProgram(EXIT_SUCCESS);
494 }
495
496 #else
497 int
498 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
499 {
500     printf("This program requires the terminfo arrays\n");
501     ExitProgram(EXIT_FAILURE);
502 }
503 #endif
504 #else /* !HAVE_TIGETSTR */
505 int
506 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
507 {
508     printf("This program requires the terminfo functions such as tigetstr\n");
509     ExitProgram(EXIT_FAILURE);
510 }
511 #endif /* HAVE_TIGETSTR */