1 /****************************************************************************
2 * Copyright (c) 2005-2017,2019 Free Software Foundation, Inc. *
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: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
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. *
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 *
27 ****************************************************************************/
30 * Author: Thomas E. Dickey
32 * $Id: demo_termcap.c,v 1.57 2019/01/21 22:50:46 tom Exp $
34 * A simple demo of the termcap interface.
37 #include <test.priv.h>
42 #include <term_entry.h>
45 #define NCURSES_XNAMES 0
49 #if defined(NCURSES_VERSION)
50 #if HAVE_NCURSES_TERMCAP_H
51 #include <ncurses/termcap.h>
57 static void failed(const char *) GCC_NORETURN;
60 failed(const char *msg)
62 fprintf(stderr, "%s\n", msg);
63 ExitProgram(EXIT_FAILURE);
68 #if defined(HAVE_CURSES_DATA_BOOLNAMES) || defined(DECL_CURSES_DATA_BOOLNAMES)
69 #define USE_CODE_LISTS 1
71 #define USE_CODE_LISTS 0
75 #define FNAME(type) "%s %-*s = ", #type, FCOLS
77 static bool b_opt = FALSE;
78 static bool n_opt = FALSE;
79 static bool s_opt = FALSE;
80 static bool q_opt = FALSE;
81 #ifdef NCURSES_VERSION
82 static bool x_opt = FALSE;
83 static bool y_opt = FALSE;
88 static char **db_list;
92 static char **my_boolcodes;
93 static char **my_numcodes;
94 static char **my_numvalues;
95 static char **my_strcodes;
96 static char **my_strvalues;
98 static long total_values;
99 static long total_b_values;
100 static long total_n_values;
101 static long total_s_values;
103 #define isCapName(c) (isgraph(c) && strchr("^=:\\", c) == 0)
104 #define EachCapName(n) n = 33; n < 127; ++n
107 make_dbitem(char *p, char *q)
109 size_t need = strlen(e_opt) + 2 + (size_t) (p - q);
110 char *result = malloc(need);
111 _nc_SPRINTF(result, _nc_SLIMIT(need) "%s=%.*s", e_opt, (int) (p - q), q);
118 if (d_opt && e_opt) {
121 for (pass = 0; pass < 2; ++pass) {
125 for (p = q = d_opt; *p != '\0'; ++p) {
129 db_list[count] = make_dbitem(p, q);
138 db_list[count] = make_dbitem(p, q);
143 db_list = typeCalloc(char *, count + 1);
155 if ((result = db_list[db_item]) == 0) {
162 printf("** %s\n", result);
172 for (n = 0; db_list[n]; ++n)
178 #endif /* NO_LEAKS */
181 show_string(const char *name, const char *value)
183 printf(FNAME(str), name);
184 if (value == ((char *) -1)) {
186 } else if (value == ((char *) 0)) {
189 while (*value != 0) {
190 int ch = UChar(*value++);
196 fputs("\\E", stdout);
199 fputs("\\b", stdout);
202 fputs("\\f", stdout);
205 fputs("\\n", stdout);
208 fputs("\\r", stdout);
211 fputs("\\s", stdout);
214 fputs("\\t", stdout);
217 fputs("\\^", stdout);
220 fputs("\\072", stdout);
223 fputs("\\\\", stdout);
229 printf("^%c", ch + '@');
231 printf("\\%03o", ch);
240 show_number(const char *name, int value)
242 printf(FNAME(num), name);
243 printf(" %d\n", value);
247 dumpit(NCURSES_CONST char *cap)
250 * One of the limitations of the termcap interface is that the library
251 * cannot determine the size of the buffer passed via tgetstr(), nor the
252 * amount of space remaining. This demo simply reuses the whole buffer
253 * for each call; a normal termcap application would try to use the buffer
254 * to hold all of the strings extracted from the terminal entry.
256 char area[1024], *ap = area;
260 if ((str = tgetstr(cap, &ap)) != 0) {
265 * Note that the strings returned are mostly terminfo format, since
266 * ncurses does not convert except for a handful of special cases.
268 show_string(cap, str);
270 } else if ((num = tgetnum(cap)) >= 0) {
274 show_number(cap, num);
276 } else if (tgetflag(cap) > 0) {
280 printf(FNAME(flg), cap);
281 printf("%s\n", "true");
290 brute_force(const char *name)
295 putenv(next_dbitem());
298 printf("Terminal type \"%s\"\n", name);
299 if (tgetent(buffer, name) >= 0) {
304 for (EachCapName(c1)) {
307 for (EachCapName(c2)) {
320 dump_xname(NCURSES_CONST char *cap)
322 if (strlen(cap) == 2)
328 demo_termcap(NCURSES_CONST char *name)
331 NCURSES_CONST char *cap;
335 putenv(next_dbitem());
338 printf("Terminal type \"%s\"\n", name);
339 if (tgetent(buffer, name) >= 0) {
343 cap = my_boolcodes[n];
352 cap = my_numcodes[n];
361 cap = my_strcodes[n];
367 #ifdef NCURSES_VERSION
368 if (x_opt && (my_blob == 0) && y_opt) {
370 TERMTYPE *term = (TERMTYPE *) cur_term;
372 && ((NUM_BOOLEANS(term) != BOOLCOUNT)
373 || (NUM_NUMBERS(term) != NUMCOUNT)
374 || (NUM_STRINGS(term) != STRCOUNT))) {
375 for (n = BOOLCOUNT; n < NUM_BOOLEANS(term); ++n) {
376 dump_xname(ExtBoolname(term, (int) n, boolnames));
378 for (n = NUMCOUNT; n < NUM_NUMBERS(term); ++n) {
379 dump_xname(ExtNumname(term, (int) n, numnames));
381 for (n = STRCOUNT; n < NUM_STRINGS(term); ++n) {
382 dump_xname(ExtStrname(term, (int) n, strnames));
403 parse_description(const char *input_name)
405 static char empty[1];
409 size_t count_bools = 0;
410 size_t count_nums = 0;
411 size_t count_strs = 0;
416 if (stat(input_name, &sb) != 0
417 || (sb.st_mode & S_IFMT) != S_IFREG) {
418 failed("input is not a file");
421 if (sb.st_size == 0) {
422 failed("input is empty");
426 * None of the arrays could be larger than the input-file, and since it
427 * is small, just allocate the maximum for simplicity.
429 if ((my_blob = malloc((size_t) sb.st_size + 1)) == 0 ||
430 (my_boolcodes = typeCalloc(char *, sb.st_size)) == 0 ||
431 (my_numcodes = typeCalloc(char *, sb.st_size)) == 0 ||
432 (my_numvalues = typeCalloc(char *, sb.st_size)) == 0 ||
433 (my_strcodes = typeCalloc(char *, sb.st_size)) == 0 ||
434 (my_strvalues = typeCalloc(char *, sb.st_size)) == 0) {
435 failed("cannot allocate memory for input-file");
438 if ((fp = fopen(input_name, "r")) == 0)
439 failed("cannot open input-file");
440 len = fread(my_blob, sizeof(char), (size_t) sb.st_size, fp);
441 my_blob[sb.st_size] = '\0';
445 * First, get rid of comments and escaped newlines, as well as repeated
446 * colons to construct a canonical entry.
448 * FIXME: actually this should make an additional pass just to strip
449 * comment-lines and escaped newlines. But it is workable for infocmp
453 for (j = k = 0; j < len; ++j) {
472 state = pDescription;
487 my_blob[k++] = (char) ch;
505 my_blob[k++] = (char) ch;
524 state = pDescription;
527 my_blob[k++] = (char) ch;
541 my_blob[k++] = (char) ch;
546 if (my_blob[j + 1] == '\0') {
558 my_blob[k++] = (char) ch;
568 * Then, parse what's left, making indexes of the names and values.
571 for (j = 0; my_blob[j] != '\0'; ++j) {
574 switch (my_blob[j]) {
580 if (my_blob[j + 1] != '\0' && my_blob[j + 1] != ':')
593 * Commented-out capabilities might be accessible (they are in
596 if (my_blob[j] == '.' && my_blob[j + 1] == '.') {
599 if (my_blob[j + 1] != '\0') {
600 switch (my_blob[j + 2]) {
602 my_numvalues[count_nums] = &my_blob[j + 3];
603 my_numcodes[count_nums++] = &my_blob[j];
604 my_blob[j + 2] = '\0';
609 my_strvalues[count_strs] = &my_blob[j + 3];
610 my_strcodes[count_strs++] = &my_blob[j];
611 my_blob[j + 2] = '\0';
616 if (my_blob[j + 2] == '@') {
618 * We cannot get the type for a cancelled item
619 * directly, but can infer it assuming the input
620 * came from infocmp, which puts the data in a
624 my_strvalues[count_strs] = empty;
625 my_strcodes[count_strs++] = &my_blob[j];
626 } else if (count_nums) {
627 my_numvalues[count_nums] = empty;
628 my_numcodes[count_nums++] = &my_blob[j];
630 my_boolcodes[count_bools++] = &my_blob[j];
633 my_boolcodes[count_bools++] = &my_blob[j];
641 if (!isdigit(UChar(my_blob[j]))) {
647 switch (my_blob[j]) {
649 if (my_blob[j + 1] == '\0') {
672 my_boolcodes[count_bools] = 0;
673 my_numcodes[count_nums] = 0;
674 my_numvalues[count_nums] = 0;
675 my_strcodes[count_strs] = 0;
676 my_strvalues[count_strs] = 0;
679 printf("bools:%d\n", (int) count_bools);
680 for (j = 0; my_boolcodes[j]; ++j)
681 printf("%5d:%s\n", (int) j, my_boolcodes[j]);
683 printf("numbers:%d\n", (int) count_nums);
684 for (j = 0; my_numcodes[j]; ++j)
685 printf("%5d:%s(%s)\n", (int) j, my_numcodes[j], my_numvalues[j]);
687 printf("strings:%d\n", (int) count_strs);
688 for (j = 0; my_strcodes[j]; ++j)
689 printf("%5d:%s(%s)\n", (int) j, my_strcodes[j], my_strvalues[j]);
695 copy_code_list(NCURSES_CONST char *const *list)
704 for (pass = 0; pass < 2; ++pass) {
705 for (count = 0; list[count] != 0; ++count) {
706 size_t chunk = strlen(list[count]) + 1;
710 result[count] = unused;
711 _nc_STRCPY(unused, list[count], length);
716 blob = malloc(length);
717 result = typeCalloc(char *, count + 1);
719 if (blob == 0 || result == 0)
720 failed("copy_code_list failed");
729 free_code_list(char **list)
736 #endif /* NO_LEAKS */
737 #endif /* USE_CODE_LISTS */
742 static const char *msg[] =
744 "Usage: demo_termcap [options] [terminal]",
746 "If no options are given, print all (boolean, numeric, string)",
747 "capabilities for the given terminal, using short names.",
750 " -a try all names, print capabilities found",
751 " -b print boolean-capabilities",
752 " -d LIST colon-separated list of databases to use",
753 " -e NAME environment variable to set with -d option",
754 " -i NAME terminal description to use as names for \"-a\" option, etc.",
755 " -n print numeric-capabilities",
756 " -q quiet (prints only counts)",
757 " -r COUNT repeat for given count",
758 " -s print string-capabilities",
759 " -v print termcap-variables",
760 #ifdef NCURSES_VERSION
761 " -x print extended capabilities",
765 for (n = 0; n < SIZEOF(msg); ++n) {
766 fprintf(stderr, "%s\n", msg[n]);
768 ExitProgram(EXIT_FAILURE);
772 main(int argc, char *argv[])
777 #if defined(NCURSES_VERSION) || defined(HAVE_CURSES_DATA_OSPEED)
780 char *input_name = 0;
785 while ((n = getopt(argc, argv, "abd:e:i:nqr:svxy")) != -1) {
809 if ((r_opt = atoi(optarg)) <= 0)
815 #if defined(NCURSES_VERSION) || defined(HAVE_CURSES_DATA_OSPEED)
820 #ifdef NCURSES_VERSION
837 #if HAVE_USE_EXTENDED_NAMES
838 use_extended_names(x_opt);
841 if (!(b_opt || n_opt || s_opt)) {
850 for (repeat = 0; repeat < r_opt; ++repeat) {
852 for (n = optind; n < argc; ++n) {
853 brute_force(argv[n]);
855 } else if ((name = getenv("TERM")) != 0) {
858 static char dumb[] = "dumb";
863 if (input_name != 0) {
864 parse_description(input_name);
868 my_boolcodes = copy_code_list(boolcodes);
869 my_numcodes = copy_code_list(numcodes);
870 my_strcodes = copy_code_list(strcodes);
874 failed("no capability-lists available (use -i option)");
876 #endif /* USE_CODE_LISTS */
877 for (repeat = 0; repeat < r_opt; ++repeat) {
879 for (n = optind; n < argc; ++n) {
880 demo_termcap(argv[n]);
882 } else if ((name = getenv("TERM")) != 0) {
885 static char dumb[] = "dumb";
891 printf("%ld values (%ld booleans, %ld numbers, %ld strings)\n",
892 total_values, total_b_values, total_n_values, total_s_values);
894 #if defined(NCURSES_VERSION) || defined(HAVE_CURSES_DATA_OSPEED)
896 show_number("PC", PC);
897 show_string("UP", UP);
898 show_string("BC", BC);
899 show_number("ospeed", (int) ospeed);
906 free_code_list(my_boolcodes);
907 free_code_list(my_numcodes);
908 free_code_list(my_strcodes);
910 #endif /* NO_LEAKS */
912 ExitProgram(EXIT_SUCCESS);
917 main(int argc GCC_UNUSED,
918 char *argv[]GCC_UNUSED)
920 failed("This program requires termcap");