]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/list_keys.c
ncurses 6.1 - patch 20180922
[ncurses.git] / test / list_keys.c
1 /****************************************************************************
2  * Copyright (c) 2016-2017,2018 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.24 2018/05/20 18:56:56 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     char buffer[10];
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             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     int j;
201     if (!t_opt) {
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         TERMTYPE *term;
289         for (k = 0; k < count; ++k) {
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         TERMTYPE *term;
309         int m, n;
310         for (k = 0; k < count; ++k) {
311             set_curterm(terms[k]);
312             term = (TERMTYPE *) cur_term;
313             for (n = STRCOUNT; n < NUM_STRINGS(term); ++n) {
314                 bool found = FALSE;
315                 const char *estr = ExtStrname(term, (int) n, strnames);
316                 for (m = STRCOUNT; m < j; ++m) {
317                     if (!strcmp(estr, Name(m))) {
318                         found = TRUE;
319                         break;
320                     }
321                 }
322                 if (!found) {
323                     Type(j) = ktExtended;
324                     Name(j++) = estr;
325                 }
326             }
327         }
328     }
329 #endif
330     actual = (size_t) j;
331     qsort(list, actual, sizeof(KEYNAMES), compare_keys);
332
333     widths0 = (int) strlen(name);
334     if (m_opt)
335         widths1 = (int) strlen(modifier);
336
337     for (k = 0; k < count; ++k) {
338         set_curterm(terms[k]);
339         check = (int) strlen(termname());
340         if (widths2 < check)
341             widths2 = check;
342     }
343     for (j = 0; Name(j) != 0; ++j) {
344         if (valid_key(Name(j), terms, count)) {
345             const char *label = f_opt ? full_name(Name(j)) : Name(j);
346             check = (int) strlen(label);
347             if (widths0 < check)
348                 widths0 = check;
349             for (k = 0; k < count; ++k) {
350                 set_curterm(terms[k]);
351                 check = show_key(Name(j), FALSE) + 1;
352                 if (widths2 < check)
353                     widths2 = check;
354                 if (m_opt) {
355                     check = (int) strlen(modified_key(Name(j)));
356                     if (widths1 < check)
357                         widths1 = check;
358                 }
359             }
360         }
361     }
362
363     if (t_opt) {
364         printf("\"%s\"", name);
365         if (m_opt)
366             printf(",\"%s\"", modifier);
367     } else {
368         printf("%-*s", widths0, name);
369         if (m_opt)
370             printf(" %-*s", widths1, modifier);
371     }
372     for (k = 0; k < count; ++k) {
373         set_curterm(terms[k]);
374         if (t_opt) {
375             printf(",\"%s\"", termname());
376         } else if (k + 1 >= count) {
377             printf(" %s", termname());
378         } else {
379             printf(" %-*s", widths2, termname());
380         }
381     }
382     printf("\n");
383
384     widthsx = widths0 + ((count + 1) * widths2);
385
386     for (j = 0; Name(j) != 0; ++j) {
387         if (j == 0 || (Type(j) != Type(j - 1)))
388             draw_line(widthsx);
389         if (valid_key(Name(j), terms, count)) {
390             const char *label = f_opt ? full_name(Name(j)) : Name(j);
391             if (t_opt) {
392                 printf("\"%s\"", label);
393                 if (m_opt)
394                     printf(",\"%s\"", modified_key(Name(j)));
395             } else {
396                 printf("%-*s", widths0, label);
397                 if (m_opt)
398                     printf(" %-*s", widths1, modified_key(Name(j)));
399             }
400             for (k = 0; k < count; ++k) {
401                 printf(t_opt ? "," : " ");
402                 set_curterm(terms[k]);
403                 check = show_key(Name(j), TRUE);
404                 if (!t_opt) {
405                     if (k + 1 < count) {
406                         printf("%*s", widths2 - check, " ");
407                     }
408                 }
409             }
410             printf("\n");
411         }
412     }
413     free(list);
414 }
415
416 static void
417 usage(void)
418 {
419     static const char *msg[] =
420     {
421         "Usage: list_keys [options] [terminal [terminal2 [...]]]",
422         "",
423         "Print capabilities for terminal special keys.",
424         "",
425         "Options:",
426         " -f       print full names",
427         " -m       print modifier-column for shift/control keys",
428         " -t       print result as CSV table",
429 #ifdef NCURSES_VERSION
430         " -x       print extended capabilities",
431 #endif
432     };
433     unsigned n;
434     for (n = 0; n < SIZEOF(msg); ++n) {
435         fprintf(stderr, "%s\n", msg[n]);
436     }
437     ExitProgram(EXIT_FAILURE);
438 }
439
440 int
441 main(int argc, char *argv[])
442 {
443     int n;
444     TERMINAL **terms = typeCalloc(TERMINAL *, argc + 1);
445
446     while ((n = getopt(argc, argv, "fmtx")) != -1) {
447         switch (n) {
448         case 'f':
449             f_opt = TRUE;
450             break;
451         case 'm':
452             m_opt = TRUE;
453             break;
454         case 't':
455             t_opt = TRUE;
456             break;
457 #ifdef NCURSES_VERSION
458         case 'x':
459             x_opt = TRUE;
460             break;
461 #endif
462         default:
463             usage();
464             break;
465         }
466     }
467
468 #if HAVE_USE_EXTENDED_NAMES
469     use_extended_names(x_opt);
470 #endif
471
472     if (optind < argc) {
473         int found = 0;
474         int status;
475         for (n = optind; n < argc; ++n) {
476             setupterm((NCURSES_CONST char *) argv[n], 1, &status);
477             if (status > 0 && cur_term != 0) {
478                 terms[found++] = cur_term;
479             }
480         }
481         if (found)
482             list_keys(terms, found);
483     } else {
484         setupterm(NULL, 1, (int *) 0);
485         terms[0] = cur_term;
486         list_keys(terms, 1);
487     }
488
489     free(terms);
490
491     ExitProgram(EXIT_SUCCESS);
492 }
493
494 #else
495 int
496 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
497 {
498     printf("This program requires the terminfo arrays\n");
499     ExitProgram(EXIT_FAILURE);
500 }
501 #endif
502 #else /* !HAVE_TIGETSTR */
503 int
504 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
505 {
506     printf("This program requires the terminfo functions such as tigetstr\n");
507     ExitProgram(EXIT_FAILURE);
508 }
509 #endif /* HAVE_TIGETSTR */