]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/demo_terminfo.c
ncurses 6.0 - patch 20170107
[ncurses.git] / test / demo_terminfo.c
1 /****************************************************************************
2  * Copyright (c) 2009-2015,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 /*
30  * Author: Thomas E. Dickey
31  *
32  * $Id: demo_terminfo.c,v 1.44 2016/09/10 21:22:49 tom Exp $
33  *
34  * A simple demo of the terminfo interface.
35  */
36 #define USE_TINFO
37 #include <test.priv.h>
38 #include <sys/stat.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 static void failed(const char *) GCC_NORETURN;
50
51 static void
52 failed(const char *msg)
53 {
54     fprintf(stderr, "%s\n", msg);
55     ExitProgram(EXIT_FAILURE);
56 }
57
58 #if HAVE_TIGETSTR
59
60 #if defined(HAVE_CURSES_DATA_BOOLNAMES) || defined(DECL_CURSES_DATA_BOOLNAMES)
61 #define USE_CODE_LISTS 1
62 #else
63 #define USE_CODE_LISTS 0
64 #endif
65
66 static bool a_opt = FALSE;
67 static bool b_opt = FALSE;
68 static bool f_opt = FALSE;
69 static bool n_opt = FALSE;
70 static bool q_opt = FALSE;
71 static bool s_opt = FALSE;
72 static bool x_opt = FALSE;
73 static bool y_opt = FALSE;
74
75 static char *d_opt;
76 static char *e_opt;
77 static char **db_list;
78 static int db_item;
79
80 static char *my_blob;
81 static char **my_boolcodes;
82 static char **my_numcodes;
83 static char **my_numvalues;
84 static char **my_strcodes;
85 static char **my_strvalues;
86
87 static long total_values;
88 static long total_b_values;
89 static long total_n_values;
90 static long total_s_values;
91
92 #define FCOLS 8
93 #define FNAME(type) "%s %-*s = ", #type, f_opt ? 24 : FCOLS
94
95 static char *
96 make_dbitem(char *p, char *q)
97 {
98     size_t need = strlen(e_opt) + 2 + (size_t) (p - q);
99     char *result = malloc(need);
100     _nc_SPRINTF(result, _nc_SLIMIT(need) "%s=%.*s", e_opt, (int) (p - q), q);
101     return result;
102 }
103
104 static void
105 make_dblist(void)
106 {
107     if (d_opt && e_opt) {
108         int pass;
109
110         for (pass = 0; pass < 2; ++pass) {
111             char *p, *q;
112             size_t count = 0;
113
114             for (p = q = d_opt; *p != '\0'; ++p) {
115                 if (*p == ':') {
116                     if (p != q + 1) {
117                         if (pass) {
118                             db_list[count] = make_dbitem(p, q);
119                         }
120                         count++;
121                     }
122                     q = p + 1;
123                 }
124             }
125             if (p != q + 1) {
126                 if (pass) {
127                     db_list[count] = make_dbitem(p, q);
128                 }
129                 count++;
130             }
131             if (!pass) {
132                 db_list = typeCalloc(char *, count + 1);
133             }
134         }
135     }
136 }
137
138 static char *
139 next_dbitem(void)
140 {
141     char *result = 0;
142
143     if (db_list) {
144         if ((result = db_list[db_item]) == 0) {
145             db_item = 0;
146             result = db_list[0];
147         } else {
148             db_item++;
149         }
150     }
151     printf("** %s\n", result);
152     return result;
153 }
154
155 #ifdef NO_LEAKS
156 static void
157 free_dblist(void)
158 {
159     if (db_list) {
160         int n;
161         for (n = 0; db_list[n]; ++n)
162             free(db_list[n]);
163         free(db_list);
164         db_list = 0;
165     }
166 }
167 #endif
168
169 static void
170 dumpit(NCURSES_CONST char *cap, const char *show)
171 {
172     const char *str;
173     int num;
174
175     if ((str = tigetstr(cap)) != 0 && (str != (char *) -1)) {
176         total_values++;
177         total_s_values++;
178         if (!q_opt) {
179             printf(FNAME(str), show ? show : cap);
180             while (*str != 0) {
181                 int ch = UChar(*str++);
182                 switch (ch) {
183                 case '\177':
184                     fputs("^?", stdout);
185                     break;
186                 case '\033':
187                     fputs("\\E", stdout);
188                     break;
189                 case '\b':
190                     fputs("\\b", stdout);
191                     break;
192                 case '\f':
193                     fputs("\\f", stdout);
194                     break;
195                 case '\n':
196                     fputs("\\n", stdout);
197                     break;
198                 case '\r':
199                     fputs("\\r", stdout);
200                     break;
201                 case ' ':
202                     fputs("\\s", stdout);
203                     break;
204                 case '\t':
205                     fputs("\\t", stdout);
206                     break;
207                 case '^':
208                     fputs("\\^", stdout);
209                     break;
210                 case ':':
211                     fputs("\\072", stdout);
212                     break;
213                 case '\\':
214                     fputs("\\\\", stdout);
215                     break;
216                 default:
217                     if (isgraph(ch))
218                         fputc(ch, stdout);
219                     else if (ch < 32)
220                         printf("^%c", ch + '@');
221                     else
222                         printf("\\%03o", ch);
223                     break;
224                 }
225             }
226             printf("\n");
227         }
228     } else if ((num = tigetnum(cap)) >= 0) {
229         total_values++;
230         total_n_values++;
231         if (!q_opt) {
232             printf(FNAME(num), show ? show : cap);
233             printf(" %d\n", num);
234         }
235     } else if ((num = tigetflag(cap)) >= 0) {
236         total_values++;
237         total_b_values++;
238         if (!q_opt) {
239             printf(FNAME(flg), show ? show : cap);
240             printf("%s\n", num ? "true" : "false");
241         }
242     }
243
244     if (!q_opt)
245         fflush(stdout);
246 }
247
248 #define isCapName(c) (isalnum(UChar(c)) || ((c) == '_'))
249 #define LegalItem(c,n) (n)
250
251 static void
252 brute_force(const char *name)
253 {
254 #define MAX_FORCE 5             /* omit "colors", since CPU-time is a problem */
255     static const char legal[] = "\
256 0123456789\
257 ABCDEFGHIJKLMNOPQRSTUVWXYZ\
258 abcdefghijklmnopqrstuvwxyz_";
259     int length;
260     int j, k;
261     bool carry;
262     bool changed;
263     char cap[MAX_FORCE + 1];
264     int item[MAX_FORCE + 1];
265
266     if (db_list) {
267         putenv(next_dbitem());
268     }
269     if (!q_opt)
270         printf("Terminal type \"%s\"\n", name);
271     setupterm((NCURSES_CONST char *) name, 1, (int *) 0);
272     if (!q_opt) {
273         if (strcmp(name, ttytype))
274             printf("... actual \"%s\"\n", ttytype);
275     }
276
277     for (length = 1; length <= MAX_FORCE; ++length) {
278         /* set all digits to zeros */
279         for (j = 0; j < length; ++j) {
280             item[j] = LegalItem(j, 0);
281         }
282
283         do {
284             changed = FALSE;
285             /* copy digits to cap-name */
286             for (j = 0; j < length; ++j) {
287                 cap[j] = legal[item[j]];
288             }
289             cap[length] = '\0';
290             dumpit(cap, NULL);
291
292             k = length - 1;
293             do {
294                 carry = FALSE;
295                 for (; k >= 0; --k) {
296                     item[k] += 1;
297                     if (legal[item[k]]) {
298                         changed = TRUE;
299                         break;
300                     }
301                     if (k > 0 &&
302                         legal[item[k - 1] + 1]) {
303                         for (j = k; j < length; ++j) {
304                             item[j] = LegalItem(j, 0);
305                         }
306                         carry = TRUE;
307                         changed = TRUE;
308                     }
309                 }
310             } while (carry);
311         } while (changed);
312     }
313     del_curterm(cur_term);
314 }
315
316 #if USE_CODE_LISTS
317 #define fullname(type,n) f_opt ? type##fnames[n] : cap
318 #else
319 #define fullname(type,n) cap
320 #endif
321
322 static void
323 demo_terminfo(char *name)
324 {
325     unsigned n;
326     NCURSES_CONST char *cap;
327
328     if (db_list) {
329         putenv(next_dbitem());
330     }
331     if (!q_opt)
332         printf("Terminal type \"%s\"\n", name);
333     setupterm(name, 1, (int *) 0);
334
335     if (b_opt) {
336         for (n = 0;; ++n) {
337             cap = my_boolcodes[n];
338             if (cap == 0)
339                 break;
340             dumpit(cap, fullname(bool, n));
341         }
342     }
343
344     if (n_opt) {
345         for (n = 0;; ++n) {
346             cap = my_numcodes[n];
347             if (cap == 0)
348                 break;
349             dumpit(cap, fullname(num, n));
350         }
351     }
352
353     if (s_opt) {
354         for (n = 0;; ++n) {
355             cap = my_strcodes[n];
356             if (cap == 0)
357                 break;
358             dumpit(cap, fullname(str, n));
359         }
360     }
361 #ifdef NCURSES_VERSION
362     if (x_opt && (my_blob == 0)) {
363         int mod;
364         if (y_opt) {
365 #if NCURSES_XNAMES
366             TERMTYPE *term = &(cur_term->type);
367             if (term != 0
368                 && ((NUM_BOOLEANS(term) != BOOLCOUNT)
369                     || (NUM_NUMBERS(term) != NUMCOUNT)
370                     || (NUM_STRINGS(term) != STRCOUNT))) {
371                 for (n = BOOLCOUNT; n < NUM_BOOLEANS(term); ++n) {
372                     dumpit(ExtBoolname(term, (int) n, boolnames), NULL);
373                 }
374                 for (n = NUMCOUNT; n < NUM_NUMBERS(term); ++n) {
375                     dumpit(ExtNumname(term, (int) n, numnames), NULL);
376                 }
377                 for (n = STRCOUNT; n < NUM_STRINGS(term); ++n) {
378                     dumpit(ExtStrname(term, (int) n, strnames), NULL);
379                 }
380             }
381 #endif
382         } else {
383             char temp[80];
384             static const char *xterm_keys[] =
385             {
386                 "kDC", "kDN", "kEND", "kHOM", "kIC",
387                 "kLFT", "kNXT", "kPRV", "kRIT", "kUP",
388             };
389             for (n = 0; n < SIZEOF(xterm_keys); ++n) {
390                 for (mod = 0; mod < 8; ++mod) {
391                     if (mod == 0) {
392                         /* these happen to be standard - avoid duplicates */
393                         if (!strcmp(xterm_keys[n], "kDC") ||
394                             !strcmp(xterm_keys[n], "kEND") ||
395                             !strcmp(xterm_keys[n], "kHOM") ||
396                             !strcmp(xterm_keys[n], "kLFT") ||
397                             !strcmp(xterm_keys[n], "kRIT")) {
398                             continue;
399                         }
400                         _nc_SPRINTF(temp, _nc_SLIMIT(sizeof(temp))
401                                     "%.*s", 8, xterm_keys[n]);
402                     } else {
403                         _nc_SPRINTF(temp, _nc_SLIMIT(sizeof(temp))
404                                     "%.*s%d", 8, xterm_keys[n], mod);
405                     }
406                     dumpit(temp, NULL);
407                 }
408             }
409         }
410     }
411 #endif
412     del_curterm(cur_term);
413 }
414
415 typedef enum {
416     pDefault = 0
417     ,pComment
418     ,pDescription
419     ,pEscaped
420     ,pNewline
421     ,pName
422     ,pNumber
423     ,pString
424 } STATE;
425
426 static void
427 parse_description(const char *input_name)
428 {
429     static char empty[1];
430
431     FILE *fp;
432     struct stat sb;
433     size_t count_bools = 0;
434     size_t count_nums = 0;
435     size_t count_strs = 0;
436     size_t len;
437     size_t j, k, jl;
438     STATE state;
439
440     if (stat(input_name, &sb) != 0
441         || (sb.st_mode & S_IFMT) != S_IFREG) {
442         failed("input is not a file");
443     }
444
445     if (sb.st_size == 0) {
446         failed("input is empty");
447     }
448
449     /*
450      * None of the arrays could be larger than the input-file, and since it
451      * is small, just allocate the maximum for simplicity.
452      */
453     if ((my_blob = malloc((size_t) sb.st_size + 1)) == 0 ||
454         (my_boolcodes = typeCalloc(char *, sb.st_size)) == 0 ||
455           (my_numcodes = typeCalloc(char *, sb.st_size)) == 0 ||
456           (my_numvalues = typeCalloc(char *, sb.st_size)) == 0 ||
457           (my_strcodes = typeCalloc(char *, sb.st_size)) == 0 ||
458           (my_strvalues = typeCalloc(char *, sb.st_size)) == 0) {
459         failed("cannot allocate memory for input-file");
460     }
461
462     if ((fp = fopen(input_name, "r")) == 0)
463         failed("cannot open input-file");
464     len = fread(my_blob, sizeof(char), (size_t) sb.st_size, fp);
465     my_blob[sb.st_size] = '\0';
466     fclose(fp);
467
468     /*
469      * First, get rid of comments and escaped newlines, as well as repeated
470      * colons to construct a canonical entry.
471      */
472     state = pNewline;
473     for (j = k = 0; j < len; ++j) {
474         int ch = my_blob[j];
475         if (ch == '\t') {
476             ch = ' ';
477         }
478         switch (state) {
479         case pNewline:
480             if (ch == ' ') {
481                 continue;
482             }
483             if (ch == '#') {
484                 state = pComment;
485                 continue;
486             }
487             state = pDefault;
488             /* FALLTHRU */
489         case pDefault:
490             switch (ch) {
491             case '|':
492                 state = pDescription;
493                 continue;
494             case '\\':
495                 state = pEscaped;
496                 continue;
497             case '\n':
498                 state = pNewline;
499                 continue;
500             case ' ':
501                 break;
502             case ',':
503                 my_blob[k++] = (char) ch;
504                 break;
505             default:
506                 if (isalpha(UChar(ch)))
507                     state = pName;
508                 else
509                     fprintf(stderr, "OOPS @%d:%.20s\n", __LINE__, my_blob + j);
510                 my_blob[k++] = (char) ch;
511                 break;
512             }
513             break;
514         case pComment:
515             if (ch == '\n')
516                 state = pNewline;
517             break;
518         case pDescription:
519             switch (ch) {
520             case ',':
521                 state = pDefault;
522                 break;
523             case '\n':
524                 state = pNewline;
525                 break;
526             }
527             break;
528         case pEscaped:
529             if (ch != '\n') {
530                 my_blob[k++] = (char) ch;
531                 state = pDefault;
532             } else {
533                 state = pNewline;
534             }
535             break;
536         case pName:
537             switch (ch) {
538             case '\n':
539                 state = pNewline;
540                 continue;
541             case ' ':
542             case ',':
543                 state = pDefault;
544                 break;
545             case '#':
546                 state = pNumber;
547                 break;
548             case '=':
549                 state = pString;
550                 break;
551             case '|':
552                 state = pDescription;
553                 continue;
554             }
555             my_blob[k++] = (char) ch;
556             break;
557         case pNumber:
558             switch (ch) {
559             case '\n':
560                 state = pNewline;
561                 continue;
562             case ',':
563                 state = pDefault;
564                 break;
565             case ' ':
566                 state = pDefault;
567                 continue;
568             }
569             my_blob[k++] = (char) ch;
570             break;
571         case pString:
572             switch (ch) {
573             case '\n':
574                 state = pNewline;
575                 break;
576             case ',':
577                 state = pDefault;
578                 my_blob[k++] = (char) ch;
579                 break;
580             default:
581                 my_blob[k++] = (char) ch;
582                 break;
583             }
584             break;
585         default:
586             /* not used */
587             break;
588         }
589     }
590     my_blob[k] = '\0';
591
592     /*
593      * Then, parse what's left, making indexes of the names and values.
594      */
595     state = pDefault;
596     for (j = 0; my_blob[j] != '\0'; ++j) {
597         switch (state) {
598         case pDefault:
599             switch (my_blob[j]) {
600             case '\\':
601                 state = pEscaped;
602                 break;
603             case ',':
604                 my_blob[j] = '\0';
605                 if (my_blob[j + 1] != '\0' && my_blob[j + 1] != ',')
606                     state = pName;
607                 break;
608             case ' ':
609                 break;
610             default:
611                 break;
612             }
613         case pEscaped:
614             break;
615         case pName:
616             state = pDefault;
617             if (isalpha(UChar(my_blob[j]))) {
618                 for (jl = 1; isalnum(UChar(my_blob[j + jl])); ++jl) {
619                     ;
620                 }
621             } else {
622                 jl = 0;
623             }
624             if (jl != 0) {
625                 switch (my_blob[j + jl]) {
626                 case '#':
627                     my_numvalues[count_nums] = &my_blob[j + jl + 1];
628                     my_numcodes[count_nums++] = &my_blob[j];
629                     my_blob[j + jl] = '\0';
630                     state = pNumber;
631                     j += jl;
632                     break;
633                 case '=':
634                     my_strvalues[count_strs] = &my_blob[j + jl + 1];
635                     my_strcodes[count_strs++] = &my_blob[j];
636                     my_blob[j + jl] = '\0';
637                     state = pString;
638                     j += jl;
639                     break;
640                 default:
641                     if (my_blob[j + jl] == '@') {
642                         /*
643                          * We cannot get the type for a cancelled item
644                          * directly, but can infer it assuming the input
645                          * came from infocmp, which puts the data in a
646                          * known order.
647                          */
648                         if (count_strs) {
649                             my_strvalues[count_strs] = empty;
650                             my_strcodes[count_strs++] = &my_blob[j];
651                         } else if (count_nums) {
652                             my_numvalues[count_nums] = empty;
653                             my_numcodes[count_nums++] = &my_blob[j];
654                         } else {
655                             my_boolcodes[count_bools++] = &my_blob[j];
656                         }
657                         my_blob[j + jl] = '\0';
658                         j += jl + 1;
659                     } else {
660                         my_boolcodes[count_bools++] = &my_blob[j];
661                         my_blob[j + jl] = '\0';
662                         j += jl;
663                     }
664                     state = (isCapName(my_blob[j + 1])
665                              ? pName
666                              : pDefault);
667                     break;
668                 }
669             }
670             break;
671         case pNumber:
672             if (!isdigit(UChar(my_blob[j]))) {
673                 --j;
674                 state = pDefault;
675             }
676             break;
677         case pString:
678             switch (my_blob[j]) {
679             case '\\':
680                 if (my_blob[j + 1] != '\0') {
681                     ++j;
682                 } else {
683                     --j;
684                     state = pDefault;
685                 }
686                 break;
687             case ',':
688                 --j;
689                 state = pDefault;
690                 break;
691             }
692             break;
693         case pNewline:
694         case pComment:
695         case pDescription:
696         default:
697             break;
698         }
699     }
700     my_boolcodes[count_bools] = 0;
701     my_numcodes[count_nums] = 0;
702     my_numvalues[count_nums] = 0;
703     my_strcodes[count_strs] = 0;
704     my_strvalues[count_strs] = 0;
705
706 #if 0
707     printf("# bools:%d\n", (int) count_bools);
708     for (j = 0; my_boolcodes[j]; ++j)
709         printf("\t%s,\n", my_boolcodes[j]);
710
711     printf("# numbers:%d\n", (int) count_nums);
712     for (j = 0; my_numcodes[j]; ++j)
713         printf("\t%s#%s,\n", my_numcodes[j], my_numvalues[j]);
714
715     printf("# strings:%d\n", (int) count_strs);
716     for (j = 0; my_strcodes[j]; ++j)
717         printf("\t%s=%s,\n", my_strcodes[j], my_strvalues[j]);
718 #endif
719 }
720
721 #if USE_CODE_LISTS
722 static char **
723 copy_code_list(NCURSES_CONST char *const *list)
724 {
725     int pass;
726     size_t count;
727     size_t length = 1;
728     char **result = 0;
729     char *blob = 0;
730     char *unused = 0;
731
732     for (pass = 0; pass < 2; ++pass) {
733         for (count = 0; list[count] != 0; ++count) {
734             size_t chunk = strlen(list[count]) + 1;
735             if (pass == 0) {
736                 length += chunk;
737             } else {
738                 result[count] = unused;
739                 _nc_STRCPY(unused, list[count], length);
740                 unused += chunk;
741             }
742         }
743         if (pass == 0) {
744             blob = malloc(length);
745             result = typeCalloc(char *, count + 1);
746             unused = blob;
747             if (blob == 0 || result == 0)
748                 failed("copy_code_list failed");
749         }
750     }
751
752     return result;
753 }
754 #endif
755
756 static void
757 usage(void)
758 {
759     static const char *msg[] =
760     {
761         "Usage: demo_terminfo [options] [terminal]",
762         "",
763         "If no options are given, print all (boolean, numeric, string)",
764         "capabilities for the given terminal, using short names.",
765         "",
766         "Options:",
767         " -a       try all names, print capabilities found",
768         " -b       print boolean-capabilities",
769         " -d LIST  colon-separated list of databases to use",
770         " -e NAME  environment variable to set with -d option",
771         " -f       print full names",
772         " -i NAME  terminal description to use as names for \"-a\" option",
773         " -n       print numeric-capabilities",
774         " -q       quiet (prints only counts)",
775         " -r COUNT repeat for given count",
776         " -s       print string-capabilities",
777 #ifdef NCURSES_VERSION
778         " -x       print extended capabilities",
779         " -y       direct-lookup names of extended capabilities",
780 #endif
781     };
782     unsigned n;
783     for (n = 0; n < SIZEOF(msg); ++n) {
784         fprintf(stderr, "%s\n", msg[n]);
785     }
786     ExitProgram(EXIT_FAILURE);
787 }
788
789 int
790 main(int argc, char *argv[])
791 {
792     int n;
793     int repeat;
794     char *name;
795     int r_opt = 1;
796     char *input_name = 0;
797
798     while ((n = getopt(argc, argv, "abd:e:fi:nqr:sxy")) != -1) {
799         switch (n) {
800         case 'a':
801             a_opt = TRUE;
802             break;
803         case 'b':
804             b_opt = TRUE;
805             break;
806         case 'd':
807             d_opt = optarg;
808             break;
809         case 'e':
810             e_opt = optarg;
811             break;
812         case 'f':
813             f_opt = TRUE;
814             break;
815         case 'i':
816             input_name = optarg;
817             break;
818         case 'n':
819             n_opt = TRUE;
820             break;
821         case 'q':
822             q_opt = TRUE;
823             break;
824         case 'r':
825             if ((r_opt = atoi(optarg)) <= 0)
826                 usage();
827             break;
828         case 's':
829             s_opt = TRUE;
830             break;
831 #ifdef NCURSES_VERSION
832         case 'x':
833             x_opt = TRUE;
834             break;
835         case 'y':
836             y_opt = TRUE;
837             x_opt = TRUE;
838             break;
839 #endif
840         default:
841             usage();
842             break;
843         }
844     }
845
846 #if HAVE_USE_EXTENDED_NAMES
847     use_extended_names(x_opt);
848 #endif
849
850     if (!(b_opt || n_opt || s_opt)) {
851         b_opt = TRUE;
852         n_opt = TRUE;
853         s_opt = TRUE;
854     }
855
856     make_dblist();
857
858     if (a_opt) {
859         for (repeat = 0; repeat < r_opt; ++repeat) {
860             if (optind < argc) {
861                 for (n = optind; n < argc; ++n) {
862                     brute_force(argv[n]);
863                 }
864             } else if ((name = getenv("TERM")) != 0) {
865                 brute_force(name);
866             } else {
867                 static char dumb[] = "dumb";
868                 brute_force(dumb);
869             }
870         }
871     } else {
872         if (input_name != 0) {
873             parse_description(input_name);
874         }
875 #if USE_CODE_LISTS
876         else {
877             my_boolcodes = copy_code_list(boolnames);
878             my_numcodes = copy_code_list(numnames);
879             my_strcodes = copy_code_list(strnames);
880         }
881 #else
882         else {
883             failed("no capability-lists available (use -i option)");
884         }
885 #endif /* USE_CODE_LISTS */
886         for (repeat = 0; repeat < r_opt; ++repeat) {
887             if (optind < argc) {
888                 for (n = optind; n < argc; ++n) {
889                     demo_terminfo(argv[n]);
890                 }
891             } else if ((name = getenv("TERM")) != 0) {
892                 demo_terminfo(name);
893             } else {
894                 static char dumb[] = "dumb";
895                 demo_terminfo(dumb);
896             }
897         }
898     }
899
900 #define PLURAL(n) n, (n != 1) ? "s" : ""
901     printf("%ld value%s (%ld boolean%s, %ld number%s, %ld string%s)\n",
902            PLURAL(total_values),
903            PLURAL(total_b_values),
904            PLURAL(total_n_values),
905            PLURAL(total_s_values));
906
907 #ifdef NO_LEAKS
908     free_dblist();
909     if (my_blob != 0) {
910         free(my_blob);
911         free(my_boolcodes);
912         free(my_numcodes);
913         free(my_numvalues);
914         free(my_strcodes);
915         free(my_strvalues);
916     }
917 #endif
918
919     ExitProgram(EXIT_SUCCESS);
920 }
921
922 #else /* !HAVE_TIGETSTR */
923 int
924 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
925 {
926     failed("This program requires the terminfo functions such as tigetstr");
927     ExitProgram(EXIT_FAILURE);
928 }
929 #endif /* HAVE_TIGETSTR */