1 /****************************************************************************
2 * Copyright (c) 2005-2015,2016 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.51 2016/09/10 21:22:39 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 #ifdef NCURSES_VERSION
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 static bool x_opt = FALSE;
78 static bool y_opt = FALSE;
82 static char **db_list;
86 static char **my_boolcodes;
87 static char **my_numcodes;
88 static char **my_numvalues;
89 static char **my_strcodes;
90 static char **my_strvalues;
92 static long total_values;
93 static long total_b_values;
94 static long total_n_values;
95 static long total_s_values;
97 #define isCapName(c) (isgraph(c) && strchr("^=:\\", c) == 0)
98 #define EachCapName(n) n = 33; n < 127; ++n
101 make_dbitem(char *p, char *q)
103 size_t need = strlen(e_opt) + 2 + (size_t) (p - q);
104 char *result = malloc(need);
105 _nc_SPRINTF(result, _nc_SLIMIT(need) "%s=%.*s", e_opt, (int) (p - q), q);
112 if (d_opt && e_opt) {
115 for (pass = 0; pass < 2; ++pass) {
119 for (p = q = d_opt; *p != '\0'; ++p) {
123 db_list[count] = make_dbitem(p, q);
132 db_list[count] = make_dbitem(p, q);
137 db_list = typeCalloc(char *, count + 1);
149 if ((result = db_list[db_item]) == 0) {
156 printf("** %s\n", result);
165 for (n = 0; db_list[n]; ++n)
173 show_string(const char *name, const char *value)
175 printf(FNAME(str), name);
176 if (value == ((char *) -1)) {
178 } else if (value == ((char *) 0)) {
181 while (*value != 0) {
182 int ch = UChar(*value++);
188 fputs("\\E", stdout);
191 fputs("\\b", stdout);
194 fputs("\\f", stdout);
197 fputs("\\n", stdout);
200 fputs("\\r", stdout);
203 fputs("\\s", stdout);
206 fputs("\\t", stdout);
209 fputs("\\^", stdout);
212 fputs("\\072", stdout);
215 fputs("\\\\", stdout);
221 printf("^%c", ch + '@');
223 printf("\\%03o", ch);
232 show_number(const char *name, int value)
234 printf(FNAME(num), name);
235 printf(" %d\n", value);
239 dumpit(NCURSES_CONST char *cap)
242 * One of the limitations of the termcap interface is that the library
243 * cannot determine the size of the buffer passed via tgetstr(), nor the
244 * amount of space remaining. This demo simply reuses the whole buffer
245 * for each call; a normal termcap application would try to use the buffer
246 * to hold all of the strings extracted from the terminal entry.
248 char area[1024], *ap = area;
252 if ((str = tgetstr(cap, &ap)) != 0) {
257 * Note that the strings returned are mostly terminfo format, since
258 * ncurses does not convert except for a handful of special cases.
260 show_string(cap, str);
262 } else if ((num = tgetnum(cap)) >= 0) {
266 show_number(cap, num);
268 } else if (tgetflag(cap) > 0) {
272 printf(FNAME(flg), cap);
273 printf("%s\n", "true");
282 brute_force(const char *name)
287 putenv(next_dbitem());
290 printf("Terminal type \"%s\"\n", name);
291 if (tgetent(buffer, name) >= 0) {
296 for (EachCapName(c1)) {
299 for (EachCapName(c2)) {
312 dump_xname(NCURSES_CONST char *cap)
314 if (strlen(cap) == 2)
320 demo_termcap(NCURSES_CONST char *name)
323 NCURSES_CONST char *cap;
327 putenv(next_dbitem());
330 printf("Terminal type \"%s\"\n", name);
331 if (tgetent(buffer, name) >= 0) {
335 cap = my_boolcodes[n];
344 cap = my_numcodes[n];
353 cap = my_strcodes[n];
359 #ifdef NCURSES_VERSION
360 if (x_opt && (my_blob == 0) && y_opt) {
362 TERMTYPE *term = &(cur_term->type);
364 && ((NUM_BOOLEANS(term) != BOOLCOUNT)
365 || (NUM_NUMBERS(term) != NUMCOUNT)
366 || (NUM_STRINGS(term) != STRCOUNT))) {
367 for (n = BOOLCOUNT; n < NUM_BOOLEANS(term); ++n) {
368 dump_xname(ExtBoolname(term, (int) n, boolnames));
370 for (n = NUMCOUNT; n < NUM_NUMBERS(term); ++n) {
371 dump_xname(ExtNumname(term, (int) n, numnames));
373 for (n = STRCOUNT; n < NUM_STRINGS(term); ++n) {
374 dump_xname(ExtStrname(term, (int) n, strnames));
395 parse_description(const char *input_name)
397 static char empty[1];
401 size_t count_bools = 0;
402 size_t count_nums = 0;
403 size_t count_strs = 0;
408 if (stat(input_name, &sb) != 0
409 || (sb.st_mode & S_IFMT) != S_IFREG) {
410 failed("input is not a file");
413 if (sb.st_size == 0) {
414 failed("input is empty");
418 * None of the arrays could be larger than the input-file, and since it
419 * is small, just allocate the maximum for simplicity.
421 if ((my_blob = malloc((size_t) sb.st_size + 1)) == 0 ||
422 (my_boolcodes = typeCalloc(char *, sb.st_size)) == 0 ||
423 (my_numcodes = typeCalloc(char *, sb.st_size)) == 0 ||
424 (my_numvalues = typeCalloc(char *, sb.st_size)) == 0 ||
425 (my_strcodes = typeCalloc(char *, sb.st_size)) == 0 ||
426 (my_strvalues = typeCalloc(char *, sb.st_size)) == 0) {
427 failed("cannot allocate memory for input-file");
430 if ((fp = fopen(input_name, "r")) == 0)
431 failed("cannot open input-file");
432 len = fread(my_blob, sizeof(char), (size_t) sb.st_size, fp);
433 my_blob[sb.st_size] = '\0';
437 * First, get rid of comments and escaped newlines, as well as repeated
438 * colons to construct a canonical entry.
440 * FIXME: actually this should make an additional pass just to strip
441 * comment-lines and escaped newlines. But it is workable for infocmp
445 for (j = k = 0; j < len; ++j) {
464 state = pDescription;
479 my_blob[k++] = (char) ch;
497 my_blob[k++] = (char) ch;
516 state = pDescription;
519 my_blob[k++] = (char) ch;
533 my_blob[k++] = (char) ch;
538 if (my_blob[j + 1] == '\0') {
550 my_blob[k++] = (char) ch;
560 * Then, parse what's left, making indexes of the names and values.
563 for (j = 0; my_blob[j] != '\0'; ++j) {
566 switch (my_blob[j]) {
572 if (my_blob[j + 1] != '\0' && my_blob[j + 1] != ':')
585 * Commented-out capabilities might be accessible (they are in
588 if (my_blob[j] == '.' && my_blob[j + 1] == '.') {
591 if (my_blob[j + 1] != '\0') {
592 switch (my_blob[j + 2]) {
594 my_numvalues[count_nums] = &my_blob[j + 3];
595 my_numcodes[count_nums++] = &my_blob[j];
596 my_blob[j + 2] = '\0';
601 my_strvalues[count_strs] = &my_blob[j + 3];
602 my_strcodes[count_strs++] = &my_blob[j];
603 my_blob[j + 2] = '\0';
608 if (my_blob[j + 2] == '@') {
610 * We cannot get the type for a cancelled item
611 * directly, but can infer it assuming the input
612 * came from infocmp, which puts the data in a
616 my_strvalues[count_strs] = empty;
617 my_strcodes[count_strs++] = &my_blob[j];
618 } else if (count_nums) {
619 my_numvalues[count_nums] = empty;
620 my_numcodes[count_nums++] = &my_blob[j];
622 my_boolcodes[count_bools++] = &my_blob[j];
625 my_boolcodes[count_bools++] = &my_blob[j];
633 if (!isdigit(UChar(my_blob[j]))) {
639 switch (my_blob[j]) {
641 if (my_blob[j + 1] == '\0') {
664 my_boolcodes[count_bools] = 0;
665 my_numcodes[count_nums] = 0;
666 my_numvalues[count_nums] = 0;
667 my_strcodes[count_strs] = 0;
668 my_strvalues[count_strs] = 0;
671 printf("bools:%d\n", (int) count_bools);
672 for (j = 0; my_boolcodes[j]; ++j)
673 printf("%5d:%s\n", (int) j, my_boolcodes[j]);
675 printf("numbers:%d\n", (int) count_nums);
676 for (j = 0; my_numcodes[j]; ++j)
677 printf("%5d:%s(%s)\n", (int) j, my_numcodes[j], my_numvalues[j]);
679 printf("strings:%d\n", (int) count_strs);
680 for (j = 0; my_strcodes[j]; ++j)
681 printf("%5d:%s(%s)\n", (int) j, my_strcodes[j], my_strvalues[j]);
687 copy_code_list(NCURSES_CONST char *const *list)
696 for (pass = 0; pass < 2; ++pass) {
697 for (count = 0; list[count] != 0; ++count) {
698 size_t chunk = strlen(list[count]) + 1;
702 result[count] = unused;
703 _nc_STRCPY(unused, list[count], length);
708 blob = malloc(length);
709 result = typeCalloc(char *, count + 1);
711 if (blob == 0 || result == 0)
712 failed("copy_code_list failed");
723 static const char *msg[] =
725 "Usage: demo_termcap [options] [terminal]",
727 "If no options are given, print all (boolean, numeric, string)",
728 "capabilities for the given terminal, using short names.",
731 " -a try all names, print capabilities found",
732 " -b print boolean-capabilities",
733 " -d LIST colon-separated list of databases to use",
734 " -e NAME environment variable to set with -d option",
735 " -i NAME terminal description to use as names for \"-a\" option, etc.",
736 " -n print numeric-capabilities",
737 " -q quiet (prints only counts)",
738 " -r COUNT repeat for given count",
739 " -s print string-capabilities",
740 " -v print termcap-variables",
741 #ifdef NCURSES_VERSION
742 " -x print extended capabilities",
746 for (n = 0; n < SIZEOF(msg); ++n) {
747 fprintf(stderr, "%s\n", msg[n]);
749 ExitProgram(EXIT_FAILURE);
753 main(int argc, char *argv[])
759 char *input_name = 0;
764 while ((n = getopt(argc, argv, "abd:e:i:nqr:svxy")) != -1) {
788 if ((r_opt = atoi(optarg)) <= 0)
812 #if HAVE_USE_EXTENDED_NAMES
813 use_extended_names(x_opt);
816 if (!(b_opt || n_opt || s_opt)) {
825 for (repeat = 0; repeat < r_opt; ++repeat) {
827 for (n = optind; n < argc; ++n) {
828 brute_force(argv[n]);
830 } else if ((name = getenv("TERM")) != 0) {
833 static char dumb[] = "dumb";
838 if (input_name != 0) {
839 parse_description(input_name);
843 my_boolcodes = copy_code_list(boolcodes);
844 my_numcodes = copy_code_list(numcodes);
845 my_strcodes = copy_code_list(strcodes);
849 failed("no capability-lists available (use -i option)");
851 #endif /* USE_CODE_LISTS */
852 for (repeat = 0; repeat < r_opt; ++repeat) {
854 for (n = optind; n < argc; ++n) {
855 demo_termcap(argv[n]);
857 } else if ((name = getenv("TERM")) != 0) {
860 static char dumb[] = "dumb";
866 printf("%ld values (%ld booleans, %ld numbers, %ld strings)\n",
867 total_values, total_b_values, total_n_values, total_s_values);
869 #if defined(NCURSES_VERSION) || defined(HAVE_CURSES_DATA_OSPEED)
871 show_number("PC", PC);
872 show_string("UP", UP);
873 show_string("BC", BC);
874 show_number("ospeed", ospeed);
880 ExitProgram(EXIT_SUCCESS);
885 main(int argc GCC_UNUSED,
886 char *argv[]GCC_UNUSED)
888 failed("This program requires termcap");