1 /****************************************************************************
2 * Copyright 2019-2021,2022 Thomas E. Dickey *
3 * Copyright 2005-2016,2017 Free Software Foundation, Inc. *
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: *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
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. *
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 *
28 ****************************************************************************/
31 * Author: Thomas E. Dickey
33 * $Id: demo_termcap.c,v 1.64 2022/12/10 23:22:09 tom Exp $
35 * A simple demo of the termcap interface.
38 #include <test.priv.h>
43 #include <term_entry.h>
46 #define NCURSES_XNAMES 0
50 #if defined(NCURSES_VERSION)
51 #if HAVE_NCURSES_TERMCAP_H
52 #include <ncurses/termcap.h>
58 static GCC_NORETURN void failed(const char *);
61 failed(const char *msg)
63 fprintf(stderr, "%s\n", msg);
64 ExitProgram(EXIT_FAILURE);
69 #if defined(HAVE_CURSES_DATA_BOOLNAMES) || defined(DECL_CURSES_DATA_BOOLNAMES)
70 #define USE_CODE_LISTS 1
72 #define USE_CODE_LISTS 0
76 #define FNAME(type) "%s %-*s = ", #type, FCOLS
78 static bool b_opt = FALSE;
79 static bool n_opt = FALSE;
80 static bool s_opt = FALSE;
81 static bool q_opt = FALSE;
82 #ifdef NCURSES_VERSION
83 static bool x_opt = FALSE;
84 static bool y_opt = FALSE;
89 static char **db_list;
93 static char **my_boolcodes;
94 static char **my_numcodes;
95 static char **my_numvalues;
96 static char **my_strcodes;
97 static char **my_strvalues;
99 static long total_values;
100 static long total_b_values;
101 static long total_n_values;
102 static long total_s_values;
104 #define isCapName(c) (isgraph(c) && strchr("^=:\\", c) == 0)
105 #define EachCapName(n) n = 33; n < 127; ++n
108 make_dbitem(char *p, char *q)
110 size_t need = strlen(e_opt) + 2 + (size_t) (p - q);
111 char *result = malloc(need);
112 _nc_SPRINTF(result, _nc_SLIMIT(need) "%s=%.*s", e_opt, (int) (p - q), q);
119 if (d_opt && e_opt) {
122 for (pass = 0; pass < 2; ++pass) {
126 for (p = q = d_opt; *p != '\0'; ++p) {
130 db_list[count] = make_dbitem(p, q);
139 db_list[count] = make_dbitem(p, q);
144 db_list = typeCalloc(char *, count + 1);
156 if ((result = db_list[db_item]) == 0) {
164 printf("** %s\n", result);
174 for (n = 0; db_list[n]; ++n)
180 #endif /* NO_LEAKS */
183 show_string(const char *name, const char *value)
185 printf(FNAME(str), name);
186 if (value == ((char *) -1)) {
188 } else if (value == ((char *) 0)) {
191 while (*value != 0) {
192 int ch = UChar(*value++);
198 fputs("\\E", stdout);
201 fputs("\\b", stdout);
204 fputs("\\f", stdout);
207 fputs("\\n", stdout);
210 fputs("\\r", stdout);
213 fputs("\\s", stdout);
216 fputs("\\t", stdout);
219 fputs("\\^", stdout);
222 fputs("\\072", stdout);
225 fputs("\\\\", stdout);
231 printf("^%c", ch + '@');
233 printf("\\%03o", ch);
242 show_number(const char *name, int value)
244 printf(FNAME(num), name);
245 printf(" %d\n", value);
249 dumpit(NCURSES_CONST char *cap)
252 * One of the limitations of the termcap interface is that the library
253 * cannot determine the size of the buffer passed via tgetstr(), nor the
254 * amount of space remaining. This demo simply reuses the whole buffer
255 * for each call; a normal termcap application would try to use the buffer
256 * to hold all of the strings extracted from the terminal entry.
258 char area[1024], *ap = area;
262 if ((str = tgetstr(cap, &ap)) != 0) {
267 * Note that the strings returned are mostly terminfo format, since
268 * ncurses does not convert except for a handful of special cases.
270 show_string(cap, str);
272 } else if ((num = tgetnum(cap)) >= 0) {
276 show_number(cap, num);
278 } else if (tgetflag(cap) > 0) {
282 printf(FNAME(flg), cap);
283 printf("%s\n", "true");
292 brute_force(const char *name)
297 putenv(next_dbitem());
300 printf("Terminal type \"%s\"\n", name);
301 if (tgetent(buffer, name) >= 0) {
306 for (EachCapName(c1)) {
309 for (EachCapName(c2)) {
322 dump_xname(NCURSES_CONST char *cap)
324 if (strlen(cap) == 2)
330 demo_termcap(NCURSES_CONST char *name)
335 putenv(next_dbitem());
338 printf("Terminal type \"%s\"\n", name);
339 if (tgetent(buffer, name) >= 0) {
340 NCURSES_CONST char *cap;
345 cap = my_boolcodes[n];
354 cap = my_numcodes[n];
363 cap = my_strcodes[n];
369 #ifdef NCURSES_VERSION
370 if (x_opt && (my_blob == 0) && y_opt) {
372 TERMTYPE *term = (TERMTYPE *) cur_term;
374 && ((NUM_BOOLEANS(term) != BOOLCOUNT)
375 || (NUM_NUMBERS(term) != NUMCOUNT)
376 || (NUM_STRINGS(term) != STRCOUNT))) {
377 for (n = BOOLCOUNT; n < NUM_BOOLEANS(term); ++n) {
378 dump_xname(ExtBoolname(term, (int) n, boolnames));
380 for (n = NUMCOUNT; n < NUM_NUMBERS(term); ++n) {
381 dump_xname(ExtNumname(term, (int) n, numnames));
383 for (n = STRCOUNT; n < NUM_STRINGS(term); ++n) {
384 dump_xname(ExtStrname(term, (int) n, strnames));
405 parse_description(const char *input_name)
407 static char empty[1];
411 size_t count_bools = 0;
412 size_t count_nums = 0;
413 size_t count_strs = 0;
418 if (stat(input_name, &sb) != 0
419 || (sb.st_mode & S_IFMT) != S_IFREG) {
420 failed("input is not a file");
423 if (sb.st_size == 0) {
424 failed("input is empty");
428 * None of the arrays could be larger than the input-file, and since it
429 * is small, just allocate the maximum for simplicity.
431 if ((my_blob = malloc((size_t) sb.st_size + 1)) == 0 ||
432 (my_boolcodes = typeCalloc(char *, sb.st_size)) == 0 ||
433 (my_numcodes = typeCalloc(char *, sb.st_size)) == 0 ||
434 (my_numvalues = typeCalloc(char *, sb.st_size)) == 0 ||
435 (my_strcodes = typeCalloc(char *, sb.st_size)) == 0 ||
436 (my_strvalues = typeCalloc(char *, sb.st_size)) == 0) {
437 failed("cannot allocate memory for input-file");
440 if ((fp = fopen(input_name, "r")) == 0)
441 failed("cannot open input-file");
442 len = fread(my_blob, sizeof(char), (size_t) sb.st_size, fp);
443 my_blob[sb.st_size] = '\0';
447 * First, get rid of comments and escaped newlines, as well as repeated
448 * colons to construct a canonical entry.
450 * FIXME: actually this should make an additional pass just to strip
451 * comment-lines and escaped newlines. But it is workable for infocmp
455 for (j = k = 0; j < len; ++j) {
474 state = pDescription;
489 my_blob[k++] = (char) ch;
507 my_blob[k++] = (char) ch;
526 state = pDescription;
529 my_blob[k++] = (char) ch;
543 my_blob[k++] = (char) ch;
548 if (my_blob[j + 1] == '\0') {
560 my_blob[k++] = (char) ch;
570 * Then, parse what's left, making indexes of the names and values.
573 for (j = 0; my_blob[j] != '\0'; ++j) {
576 switch (my_blob[j]) {
582 if (my_blob[j + 1] != '\0' && my_blob[j + 1] != ':')
595 * Commented-out capabilities might be accessible (they are in
598 if (my_blob[j] == '.' && my_blob[j + 1] == '.') {
601 if (my_blob[j + 1] != '\0') {
602 switch (my_blob[j + 2]) {
604 my_numvalues[count_nums] = &my_blob[j + 3];
605 my_numcodes[count_nums++] = &my_blob[j];
606 my_blob[j + 2] = '\0';
611 my_strvalues[count_strs] = &my_blob[j + 3];
612 my_strcodes[count_strs++] = &my_blob[j];
613 my_blob[j + 2] = '\0';
618 if (my_blob[j + 2] == '@') {
620 * We cannot get the type for a cancelled item
621 * directly, but can infer it assuming the input
622 * came from infocmp, which puts the data in a
626 my_strvalues[count_strs] = empty;
627 my_strcodes[count_strs++] = &my_blob[j];
628 } else if (count_nums) {
629 my_numvalues[count_nums] = empty;
630 my_numcodes[count_nums++] = &my_blob[j];
632 my_boolcodes[count_bools++] = &my_blob[j];
635 my_boolcodes[count_bools++] = &my_blob[j];
643 if (!isdigit(UChar(my_blob[j]))) {
649 switch (my_blob[j]) {
651 if (my_blob[j + 1] == '\0') {
674 my_boolcodes[count_bools] = 0;
675 my_numcodes[count_nums] = 0;
676 my_numvalues[count_nums] = 0;
677 my_strcodes[count_strs] = 0;
678 my_strvalues[count_strs] = 0;
681 printf("bools:%d\n", (int) count_bools);
682 for (j = 0; my_boolcodes[j]; ++j)
683 printf("%5d:%s\n", (int) j, my_boolcodes[j]);
685 printf("numbers:%d\n", (int) count_nums);
686 for (j = 0; my_numcodes[j]; ++j)
687 printf("%5d:%s(%s)\n", (int) j, my_numcodes[j], my_numvalues[j]);
689 printf("strings:%d\n", (int) count_strs);
690 for (j = 0; my_strcodes[j]; ++j)
691 printf("%5d:%s(%s)\n", (int) j, my_strcodes[j], my_strvalues[j]);
697 copy_code_list(NCURSES_CONST char *const *list)
705 for (pass = 0; pass < 2; ++pass) {
706 for (count = 0; list[count] != 0; ++count) {
707 size_t chunk = strlen(list[count]) + 1;
711 result[count] = unused;
712 _nc_STRCPY(unused, list[count], length);
717 char *blob = malloc(length);
718 result = typeCalloc(char *, count + 1);
720 if (blob == 0 || result == 0)
721 failed("copy_code_list failed");
730 free_code_list(char **list)
737 #endif /* NO_LEAKS */
738 #endif /* USE_CODE_LISTS */
743 static const char *msg[] =
745 "Usage: demo_termcap [options] [terminal]"
747 ,"If no options are given, print all (boolean, numeric, string)"
748 ,"capabilities for the given terminal, using short names."
752 ," -a try all names, print capabilities found"
753 ," -b print boolean-capabilities"
754 ," -d LIST colon-separated list of databases to use"
755 ," -e NAME environment variable to set with -d option"
756 ," -i NAME terminal description to use as names for \"-a\" option, etc."
757 ," -n print numeric-capabilities"
758 ," -q quiet (prints only counts)"
759 ," -r COUNT repeat for given count"
760 ," -s print string-capabilities"
761 ," -v print termcap-variables"
762 #ifdef NCURSES_VERSION
763 ," -x print extended capabilities"
767 for (n = 0; n < SIZEOF(msg); ++n) {
768 fprintf(stderr, "%s\n", msg[n]);
770 ExitProgram(ok ? EXIT_SUCCESS : EXIT_FAILURE);
777 main(int argc, char *argv[])
783 #if defined(NCURSES_VERSION) || defined(HAVE_CURSES_DATA_OSPEED)
786 char *input_name = 0;
791 while ((ch = getopt(argc, argv, OPTS_COMMON "abd:e:i:nqr:svxy")) != -1) {
815 if ((r_opt = atoi(optarg)) <= 0)
821 #if defined(NCURSES_VERSION) || defined(HAVE_CURSES_DATA_OSPEED)
826 #ifdef NCURSES_VERSION
839 ExitProgram(EXIT_SUCCESS);
841 usage(ch == OPTS_USAGE);
846 #if HAVE_USE_EXTENDED_NAMES
847 use_extended_names(x_opt);
850 if (!(b_opt || n_opt || s_opt)) {
859 for (repeat = 0; repeat < r_opt; ++repeat) {
861 for (n = optind; n < argc; ++n) {
862 brute_force(argv[n]);
864 } else if ((name = getenv("TERM")) != 0) {
867 static char dumb[] = "dumb";
872 if (input_name != 0) {
873 parse_description(input_name);
877 my_boolcodes = copy_code_list(boolcodes);
878 my_numcodes = copy_code_list(numcodes);
879 my_strcodes = copy_code_list(strcodes);
883 failed("no capability-lists available (use -i option)");
885 #endif /* USE_CODE_LISTS */
886 for (repeat = 0; repeat < r_opt; ++repeat) {
888 for (n = optind; n < argc; ++n) {
889 demo_termcap(argv[n]);
891 } else if ((name = getenv("TERM")) != 0) {
894 static char dumb[] = "dumb";
900 printf("%ld values (%ld booleans, %ld numbers, %ld strings)\n",
901 total_values, total_b_values, total_n_values, total_s_values);
903 #if defined(NCURSES_VERSION) || defined(HAVE_CURSES_DATA_OSPEED)
905 show_number("PC", PC);
906 show_string("UP", UP);
907 show_string("BC", BC);
908 show_number("ospeed", (int) ospeed);
915 free_code_list(my_boolcodes);
916 free_code_list(my_numcodes);
917 free_code_list(my_strcodes);
919 #endif /* NO_LEAKS */
921 ExitProgram(EXIT_SUCCESS);
928 failed("This program requires termcap");