1 /****************************************************************************
2 * Copyright (c) 2005-2016,2017 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.56 2017/12/26 22:41:47 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) && HAVE_TERMCAP_H
53 static void failed(const char *) GCC_NORETURN;
56 failed(const char *msg)
58 fprintf(stderr, "%s\n", msg);
59 ExitProgram(EXIT_FAILURE);
64 #if defined(HAVE_CURSES_DATA_BOOLNAMES) || defined(DECL_CURSES_DATA_BOOLNAMES)
65 #define USE_CODE_LISTS 1
67 #define USE_CODE_LISTS 0
71 #define FNAME(type) "%s %-*s = ", #type, FCOLS
73 static bool b_opt = FALSE;
74 static bool n_opt = FALSE;
75 static bool s_opt = FALSE;
76 static bool q_opt = FALSE;
77 #ifdef NCURSES_VERSION
78 static bool x_opt = FALSE;
79 static bool y_opt = FALSE;
84 static char **db_list;
88 static char **my_boolcodes;
89 static char **my_numcodes;
90 static char **my_numvalues;
91 static char **my_strcodes;
92 static char **my_strvalues;
94 static long total_values;
95 static long total_b_values;
96 static long total_n_values;
97 static long total_s_values;
99 #define isCapName(c) (isgraph(c) && strchr("^=:\\", c) == 0)
100 #define EachCapName(n) n = 33; n < 127; ++n
103 make_dbitem(char *p, char *q)
105 size_t need = strlen(e_opt) + 2 + (size_t) (p - q);
106 char *result = malloc(need);
107 _nc_SPRINTF(result, _nc_SLIMIT(need) "%s=%.*s", e_opt, (int) (p - q), q);
114 if (d_opt && e_opt) {
117 for (pass = 0; pass < 2; ++pass) {
121 for (p = q = d_opt; *p != '\0'; ++p) {
125 db_list[count] = make_dbitem(p, q);
134 db_list[count] = make_dbitem(p, q);
139 db_list = typeCalloc(char *, count + 1);
151 if ((result = db_list[db_item]) == 0) {
158 printf("** %s\n", result);
168 for (n = 0; db_list[n]; ++n)
174 #endif /* NO_LEAKS */
177 show_string(const char *name, const char *value)
179 printf(FNAME(str), name);
180 if (value == ((char *) -1)) {
182 } else if (value == ((char *) 0)) {
185 while (*value != 0) {
186 int ch = UChar(*value++);
192 fputs("\\E", stdout);
195 fputs("\\b", stdout);
198 fputs("\\f", stdout);
201 fputs("\\n", stdout);
204 fputs("\\r", stdout);
207 fputs("\\s", stdout);
210 fputs("\\t", stdout);
213 fputs("\\^", stdout);
216 fputs("\\072", stdout);
219 fputs("\\\\", stdout);
225 printf("^%c", ch + '@');
227 printf("\\%03o", ch);
236 show_number(const char *name, int value)
238 printf(FNAME(num), name);
239 printf(" %d\n", value);
243 dumpit(NCURSES_CONST char *cap)
246 * One of the limitations of the termcap interface is that the library
247 * cannot determine the size of the buffer passed via tgetstr(), nor the
248 * amount of space remaining. This demo simply reuses the whole buffer
249 * for each call; a normal termcap application would try to use the buffer
250 * to hold all of the strings extracted from the terminal entry.
252 char area[1024], *ap = area;
256 if ((str = tgetstr(cap, &ap)) != 0) {
261 * Note that the strings returned are mostly terminfo format, since
262 * ncurses does not convert except for a handful of special cases.
264 show_string(cap, str);
266 } else if ((num = tgetnum(cap)) >= 0) {
270 show_number(cap, num);
272 } else if (tgetflag(cap) > 0) {
276 printf(FNAME(flg), cap);
277 printf("%s\n", "true");
286 brute_force(const char *name)
291 putenv(next_dbitem());
294 printf("Terminal type \"%s\"\n", name);
295 if (tgetent(buffer, name) >= 0) {
300 for (EachCapName(c1)) {
303 for (EachCapName(c2)) {
316 dump_xname(NCURSES_CONST char *cap)
318 if (strlen(cap) == 2)
324 demo_termcap(NCURSES_CONST char *name)
327 NCURSES_CONST char *cap;
331 putenv(next_dbitem());
334 printf("Terminal type \"%s\"\n", name);
335 if (tgetent(buffer, name) >= 0) {
339 cap = my_boolcodes[n];
348 cap = my_numcodes[n];
357 cap = my_strcodes[n];
363 #ifdef NCURSES_VERSION
364 if (x_opt && (my_blob == 0) && y_opt) {
366 TERMTYPE *term = (TERMTYPE *) cur_term;
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 dump_xname(ExtBoolname(term, (int) n, boolnames));
374 for (n = NUMCOUNT; n < NUM_NUMBERS(term); ++n) {
375 dump_xname(ExtNumname(term, (int) n, numnames));
377 for (n = STRCOUNT; n < NUM_STRINGS(term); ++n) {
378 dump_xname(ExtStrname(term, (int) n, strnames));
399 parse_description(const char *input_name)
401 static char empty[1];
405 size_t count_bools = 0;
406 size_t count_nums = 0;
407 size_t count_strs = 0;
412 if (stat(input_name, &sb) != 0
413 || (sb.st_mode & S_IFMT) != S_IFREG) {
414 failed("input is not a file");
417 if (sb.st_size == 0) {
418 failed("input is empty");
422 * None of the arrays could be larger than the input-file, and since it
423 * is small, just allocate the maximum for simplicity.
425 if ((my_blob = malloc((size_t) sb.st_size + 1)) == 0 ||
426 (my_boolcodes = typeCalloc(char *, sb.st_size)) == 0 ||
427 (my_numcodes = typeCalloc(char *, sb.st_size)) == 0 ||
428 (my_numvalues = typeCalloc(char *, sb.st_size)) == 0 ||
429 (my_strcodes = typeCalloc(char *, sb.st_size)) == 0 ||
430 (my_strvalues = typeCalloc(char *, sb.st_size)) == 0) {
431 failed("cannot allocate memory for input-file");
434 if ((fp = fopen(input_name, "r")) == 0)
435 failed("cannot open input-file");
436 len = fread(my_blob, sizeof(char), (size_t) sb.st_size, fp);
437 my_blob[sb.st_size] = '\0';
441 * First, get rid of comments and escaped newlines, as well as repeated
442 * colons to construct a canonical entry.
444 * FIXME: actually this should make an additional pass just to strip
445 * comment-lines and escaped newlines. But it is workable for infocmp
449 for (j = k = 0; j < len; ++j) {
468 state = pDescription;
483 my_blob[k++] = (char) ch;
501 my_blob[k++] = (char) ch;
520 state = pDescription;
523 my_blob[k++] = (char) ch;
537 my_blob[k++] = (char) ch;
542 if (my_blob[j + 1] == '\0') {
554 my_blob[k++] = (char) ch;
564 * Then, parse what's left, making indexes of the names and values.
567 for (j = 0; my_blob[j] != '\0'; ++j) {
570 switch (my_blob[j]) {
576 if (my_blob[j + 1] != '\0' && my_blob[j + 1] != ':')
589 * Commented-out capabilities might be accessible (they are in
592 if (my_blob[j] == '.' && my_blob[j + 1] == '.') {
595 if (my_blob[j + 1] != '\0') {
596 switch (my_blob[j + 2]) {
598 my_numvalues[count_nums] = &my_blob[j + 3];
599 my_numcodes[count_nums++] = &my_blob[j];
600 my_blob[j + 2] = '\0';
605 my_strvalues[count_strs] = &my_blob[j + 3];
606 my_strcodes[count_strs++] = &my_blob[j];
607 my_blob[j + 2] = '\0';
612 if (my_blob[j + 2] == '@') {
614 * We cannot get the type for a cancelled item
615 * directly, but can infer it assuming the input
616 * came from infocmp, which puts the data in a
620 my_strvalues[count_strs] = empty;
621 my_strcodes[count_strs++] = &my_blob[j];
622 } else if (count_nums) {
623 my_numvalues[count_nums] = empty;
624 my_numcodes[count_nums++] = &my_blob[j];
626 my_boolcodes[count_bools++] = &my_blob[j];
629 my_boolcodes[count_bools++] = &my_blob[j];
637 if (!isdigit(UChar(my_blob[j]))) {
643 switch (my_blob[j]) {
645 if (my_blob[j + 1] == '\0') {
668 my_boolcodes[count_bools] = 0;
669 my_numcodes[count_nums] = 0;
670 my_numvalues[count_nums] = 0;
671 my_strcodes[count_strs] = 0;
672 my_strvalues[count_strs] = 0;
675 printf("bools:%d\n", (int) count_bools);
676 for (j = 0; my_boolcodes[j]; ++j)
677 printf("%5d:%s\n", (int) j, my_boolcodes[j]);
679 printf("numbers:%d\n", (int) count_nums);
680 for (j = 0; my_numcodes[j]; ++j)
681 printf("%5d:%s(%s)\n", (int) j, my_numcodes[j], my_numvalues[j]);
683 printf("strings:%d\n", (int) count_strs);
684 for (j = 0; my_strcodes[j]; ++j)
685 printf("%5d:%s(%s)\n", (int) j, my_strcodes[j], my_strvalues[j]);
691 copy_code_list(NCURSES_CONST char *const *list)
700 for (pass = 0; pass < 2; ++pass) {
701 for (count = 0; list[count] != 0; ++count) {
702 size_t chunk = strlen(list[count]) + 1;
706 result[count] = unused;
707 _nc_STRCPY(unused, list[count], length);
712 blob = malloc(length);
713 result = typeCalloc(char *, count + 1);
715 if (blob == 0 || result == 0)
716 failed("copy_code_list failed");
725 free_code_list(char **list)
732 #endif /* NO_LEAKS */
733 #endif /* USE_CODE_LISTS */
738 static const char *msg[] =
740 "Usage: demo_termcap [options] [terminal]",
742 "If no options are given, print all (boolean, numeric, string)",
743 "capabilities for the given terminal, using short names.",
746 " -a try all names, print capabilities found",
747 " -b print boolean-capabilities",
748 " -d LIST colon-separated list of databases to use",
749 " -e NAME environment variable to set with -d option",
750 " -i NAME terminal description to use as names for \"-a\" option, etc.",
751 " -n print numeric-capabilities",
752 " -q quiet (prints only counts)",
753 " -r COUNT repeat for given count",
754 " -s print string-capabilities",
755 " -v print termcap-variables",
756 #ifdef NCURSES_VERSION
757 " -x print extended capabilities",
761 for (n = 0; n < SIZEOF(msg); ++n) {
762 fprintf(stderr, "%s\n", msg[n]);
764 ExitProgram(EXIT_FAILURE);
768 main(int argc, char *argv[])
773 #if defined(NCURSES_VERSION) || defined(HAVE_CURSES_DATA_OSPEED)
776 char *input_name = 0;
781 while ((n = getopt(argc, argv, "abd:e:i:nqr:svxy")) != -1) {
805 if ((r_opt = atoi(optarg)) <= 0)
811 #if defined(NCURSES_VERSION) || defined(HAVE_CURSES_DATA_OSPEED)
816 #ifdef NCURSES_VERSION
833 #if HAVE_USE_EXTENDED_NAMES
834 use_extended_names(x_opt);
837 if (!(b_opt || n_opt || s_opt)) {
846 for (repeat = 0; repeat < r_opt; ++repeat) {
848 for (n = optind; n < argc; ++n) {
849 brute_force(argv[n]);
851 } else if ((name = getenv("TERM")) != 0) {
854 static char dumb[] = "dumb";
859 if (input_name != 0) {
860 parse_description(input_name);
864 my_boolcodes = copy_code_list(boolcodes);
865 my_numcodes = copy_code_list(numcodes);
866 my_strcodes = copy_code_list(strcodes);
870 failed("no capability-lists available (use -i option)");
872 #endif /* USE_CODE_LISTS */
873 for (repeat = 0; repeat < r_opt; ++repeat) {
875 for (n = optind; n < argc; ++n) {
876 demo_termcap(argv[n]);
878 } else if ((name = getenv("TERM")) != 0) {
881 static char dumb[] = "dumb";
887 printf("%ld values (%ld booleans, %ld numbers, %ld strings)\n",
888 total_values, total_b_values, total_n_values, total_s_values);
890 #if defined(NCURSES_VERSION) || defined(HAVE_CURSES_DATA_OSPEED)
892 show_number("PC", PC);
893 show_string("UP", UP);
894 show_string("BC", BC);
895 show_number("ospeed", (int) ospeed);
902 free_code_list(my_boolcodes);
903 free_code_list(my_numcodes);
904 free_code_list(my_strcodes);
906 #endif /* NO_LEAKS */
908 ExitProgram(EXIT_SUCCESS);
913 main(int argc GCC_UNUSED,
914 char *argv[]GCC_UNUSED)
916 failed("This program requires termcap");