]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/list_keys.c
ncurses 6.0 - patch 20160702
[ncurses.git] / test / list_keys.c
1 /****************************************************************************
2  * Copyright (c) 2016 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.12 2016/07/02 23:45:53 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 t_opt = FALSE;
53 static bool x_opt = FALSE;
54
55 typedef enum {
56     ktCursor
57     ,ktFunction
58     ,ktOther
59 #if HAVE_USE_EXTENDED_NAMES
60     ,ktExtended
61 #endif
62 } KEYTYPE;
63
64 typedef struct {
65     KEYTYPE type;
66     const char *name;
67 } KEYNAMES;
68
69 #define Type(n) list[n].type
70 #define Name(n) list[n].name
71
72 static const char *
73 full_name(const char *name)
74 {
75     const char *result = name;
76     int n;
77     for (n = 0; strnames[n] != 0; ++n) {
78         if (!strcmp(name, strnames[n])) {
79             result = strfnames[n];
80             break;
81         }
82     }
83     return result;
84 }
85
86 static int
87 show_key(const char *name, bool show)
88 {
89     int width = 0;
90     char buffer[10];
91     NCURSES_CONST char *value = tigetstr(name);
92
93     if (show && t_opt)
94         fputc('"', stdout);
95
96     if (value != 0 && value != (char *) -1) {
97         while (*value != 0) {
98             int ch = UChar(*value++);
99             switch (ch) {
100             case '\177':
101                 strcpy(buffer, "^?");
102                 break;
103             case '\033':
104                 strcpy(buffer, "\\E");
105                 break;
106             case '\b':
107                 strcpy(buffer, "\\b");
108                 break;
109             case '\f':
110                 strcpy(buffer, "\\f");
111                 break;
112             case '\n':
113                 strcpy(buffer, "\\n");
114                 break;
115             case '\r':
116                 strcpy(buffer, "\\r");
117                 break;
118             case ' ':
119                 strcpy(buffer, "\\s");
120                 break;
121             case '\t':
122                 strcpy(buffer, "\\t");
123                 break;
124             case '^':
125                 strcpy(buffer, "\\^");
126                 break;
127             case ':':
128                 strcpy(buffer, "\\072");
129                 break;
130             case '\\':
131                 strcpy(buffer, "\\\\");
132                 break;
133             default:
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 + '@');
140                 } else {
141                     sprintf(buffer, "\\%03o", ch);
142                 }
143                 break;
144             }
145             width += (int) strlen(buffer);
146             if (show)
147                 fputs(buffer, stdout);
148         }
149     }
150
151     if (show && t_opt)
152         fputc('"', stdout);
153
154     return width;
155 }
156
157 static bool
158 valid_key(const char *name, TERMINAL ** terms, int count)
159 {
160     bool result = FALSE;
161     if (*name == 'k') {
162         int k;
163         for (k = 0; k < count; ++k) {
164             set_curterm(terms[k]);
165             if (show_key(name, FALSE)) {
166                 result = TRUE;
167                 break;
168             }
169         }
170     }
171     return result;
172 }
173
174 static int
175 compare_keys(const void *a, const void *b)
176 {
177     const KEYNAMES *p = (const KEYNAMES *) a;
178     const KEYNAMES *q = (const KEYNAMES *) b;
179     int result = (int) (p->type - q->type);
180     int pn, qn;
181     if (result == 0) {
182         if (p->type == ktFunction &&
183             sscanf(p->name, "kf%d", &pn) == 1 &&
184             sscanf(q->name, "kf%d", &qn) == 1) {
185             result = (pn - qn);
186         } else {
187             result = strcmp(p->name, q->name);
188         }
189     }
190     return result;
191 }
192
193 static void
194 draw_line(int width)
195 {
196     int j;
197     if (!t_opt) {
198         for (j = 0; j < width; ++j) {
199             printf("-");
200         }
201         printf("\n");
202     }
203 }
204
205 static void
206 list_keys(TERMINAL ** terms, int count)
207 {
208     int j, k;
209     int widths0 = 0;
210     int widths1 = 0;
211     int widthsx;
212     int check;
213     size_t total = 0;
214     size_t actual = 0;
215     const char *name = f_opt ? "strfname" : "strname";
216     KEYNAMES *list;
217
218     for (total = 0; strnames[total]; ++total) {
219         ;
220     }
221 #if NCURSES_XNAMES
222     if (x_opt) {
223         TERMTYPE *term;
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);
228         }
229     }
230 #endif
231     list = typeCalloc(KEYNAMES, total + 1);
232     for (j = 0; strnames[j]; ++j) {
233         Type(j) = ktOther;
234         if (sscanf(strnames[j], "kf%d", &k) == 1) {
235             Type(j) = ktFunction;
236         } else if (!strncmp(strnames[j], "kcu", 3)) {
237             Type(j) = ktCursor;
238         }
239         Name(j) = strnames[j];
240     }
241 #if NCURSES_XNAMES
242     if (x_opt) {
243         TERMTYPE *term;
244         int m, n;
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) {
249                 bool found = FALSE;
250                 const char *estr = ExtStrname(term, (int) n, strnames);
251                 for (m = STRCOUNT; m < j; ++m) {
252                     if (!strcmp(estr, Name(m))) {
253                         found = TRUE;
254                         break;
255                     }
256                 }
257                 if (!found) {
258                     Type(j) = ktExtended;
259                     Name(j++) = estr;
260                 }
261             }
262         }
263     }
264 #endif
265     actual = (size_t) j;
266     qsort(list, actual, sizeof(KEYNAMES), compare_keys);
267
268     widths0 = (int) strlen(name);
269     for (k = 0; k < count; ++k) {
270         set_curterm(terms[k]);
271         check = (int) strlen(termname());
272         if (widths1 < check)
273             widths1 = check;
274     }
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);
279             if (widths0 < check)
280                 widths0 = check;
281             for (k = 0; k < count; ++k) {
282                 set_curterm(terms[k]);
283                 check = show_key(Name(j), FALSE);
284                 if (widths1 < check)
285                     widths1 = check;
286             }
287         }
288     }
289
290     if (t_opt) {
291         printf("\"%s\"", name);
292     } else {
293         printf("%-*s", widths0, name);
294     }
295     for (k = 0; k < count; ++k) {
296         set_curterm(terms[k]);
297         if (t_opt) {
298             printf(",\"%s\"", termname());
299         } else if (k + 1 >= count) {
300             printf(" %s", termname());
301         } else {
302             printf(" %-*s", widths1, termname());
303         }
304     }
305     printf("\n");
306
307     widthsx = widths0 + ((count + 1) * widths1);
308
309     for (j = 0; Name(j) != 0; ++j) {
310         if (j == 0 || (Type(j) != Type(j - 1)))
311             draw_line(widthsx);
312         if (valid_key(Name(j), terms, count)) {
313             const char *label = f_opt ? full_name(Name(j)) : Name(j);
314             if (t_opt) {
315                 printf("\"%s\"", label);
316             } else {
317                 printf("%-*s", widths0, label);
318             }
319             for (k = 0; k < count; ++k) {
320                 printf(t_opt ? "," : " ");
321                 set_curterm(terms[k]);
322                 check = show_key(Name(j), TRUE);
323                 if (!t_opt) {
324                     if (k + 1 < count) {
325                         printf("%*s", widths1 - check, " ");
326                     }
327                 }
328             }
329             printf("\n");
330         }
331     }
332     free(list);
333 }
334
335 static void
336 usage(void)
337 {
338     static const char *msg[] =
339     {
340         "Usage: list_keys [options] [terminal [terminal2 [...]]]",
341         "",
342         "Print capabilities for terminal special keys.",
343         "",
344         "Options:",
345         " -f       print full names",
346         " -t       print result as CSV table",
347 #ifdef NCURSES_VERSION
348         " -x       print extended capabilities",
349 #endif
350     };
351     unsigned n;
352     for (n = 0; n < SIZEOF(msg); ++n) {
353         fprintf(stderr, "%s\n", msg[n]);
354     }
355     ExitProgram(EXIT_FAILURE);
356 }
357
358 int
359 main(int argc, char *argv[])
360 {
361     int n;
362     TERMINAL **terms = typeCalloc(TERMINAL *, argc + 1);
363
364     while ((n = getopt(argc, argv, "ftx")) != -1) {
365         switch (n) {
366         case 'f':
367             f_opt = TRUE;
368             break;
369         case 't':
370             t_opt = TRUE;
371             break;
372 #ifdef NCURSES_VERSION
373         case 'x':
374             x_opt = TRUE;
375             break;
376 #endif
377         default:
378             usage();
379             break;
380         }
381     }
382
383 #if HAVE_USE_EXTENDED_NAMES
384     use_extended_names(x_opt);
385 #endif
386
387     if (optind < argc) {
388         int found = 0;
389         int status;
390         for (n = optind; n < argc; ++n) {
391             setupterm((NCURSES_CONST char *) argv[n], 1, &status);
392             if (status > 0 && cur_term != 0) {
393                 terms[found++] = cur_term;
394             }
395         }
396         if (found)
397             list_keys(terms, found);
398     } else {
399         setupterm(NULL, 1, (int *) 0);
400         terms[0] = cur_term;
401         list_keys(terms, 1);
402     }
403
404     ExitProgram(EXIT_SUCCESS);
405 }
406
407 #else
408 int
409 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
410 {
411     printf("This program requires the terminfo arrays\n");
412     ExitProgram(EXIT_FAILURE);
413 }
414 #endif
415 #else /* !HAVE_TIGETSTR */
416 int
417 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
418 {
419     printf("This program requires the terminfo functions such as tigetstr\n");
420     ExitProgram(EXIT_FAILURE);
421 }
422 #endif /* HAVE_TIGETSTR */