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