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.54 2017/04/13 09:12:40 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);
166 for (n = 0; db_list[n]; ++n)
172 #endif /* NO_LEAKS */
175 show_string(const char *name, const char *value)
177 printf(FNAME(str), name);
178 if (value == ((char *) -1)) {
180 } else if (value == ((char *) 0)) {
183 while (*value != 0) {
184 int ch = UChar(*value++);
190 fputs("\\E", stdout);
193 fputs("\\b", stdout);
196 fputs("\\f", stdout);
199 fputs("\\n", stdout);
202 fputs("\\r", stdout);
205 fputs("\\s", stdout);
208 fputs("\\t", stdout);
211 fputs("\\^", stdout);
214 fputs("\\072", stdout);
217 fputs("\\\\", stdout);
223 printf("^%c", ch + '@');
225 printf("\\%03o", ch);
234 show_number(const char *name, int value)
236 printf(FNAME(num), name);
237 printf(" %d\n", value);
241 dumpit(NCURSES_CONST char *cap)
244 * One of the limitations of the termcap interface is that the library
245 * cannot determine the size of the buffer passed via tgetstr(), nor the
246 * amount of space remaining. This demo simply reuses the whole buffer
247 * for each call; a normal termcap application would try to use the buffer
248 * to hold all of the strings extracted from the terminal entry.
250 char area[1024], *ap = area;
254 if ((str = tgetstr(cap, &ap)) != 0) {
259 * Note that the strings returned are mostly terminfo format, since
260 * ncurses does not convert except for a handful of special cases.
262 show_string(cap, str);
264 } else if ((num = tgetnum(cap)) >= 0) {
268 show_number(cap, num);
270 } else if (tgetflag(cap) > 0) {
274 printf(FNAME(flg), cap);
275 printf("%s\n", "true");
284 brute_force(const char *name)
289 putenv(next_dbitem());
292 printf("Terminal type \"%s\"\n", name);
293 if (tgetent(buffer, name) >= 0) {
298 for (EachCapName(c1)) {
301 for (EachCapName(c2)) {
314 dump_xname(NCURSES_CONST char *cap)
316 if (strlen(cap) == 2)
322 demo_termcap(NCURSES_CONST char *name)
325 NCURSES_CONST char *cap;
329 putenv(next_dbitem());
332 printf("Terminal type \"%s\"\n", name);
333 if (tgetent(buffer, name) >= 0) {
337 cap = my_boolcodes[n];
346 cap = my_numcodes[n];
355 cap = my_strcodes[n];
361 #ifdef NCURSES_VERSION
362 if (x_opt && (my_blob == 0) && y_opt) {
364 TERMTYPE *term = (TERMTYPE *) cur_term;
366 && ((NUM_BOOLEANS(term) != BOOLCOUNT)
367 || (NUM_NUMBERS(term) != NUMCOUNT)
368 || (NUM_STRINGS(term) != STRCOUNT))) {
369 for (n = BOOLCOUNT; n < NUM_BOOLEANS(term); ++n) {
370 dump_xname(ExtBoolname(term, (int) n, boolnames));
372 for (n = NUMCOUNT; n < NUM_NUMBERS(term); ++n) {
373 dump_xname(ExtNumname(term, (int) n, numnames));
375 for (n = STRCOUNT; n < NUM_STRINGS(term); ++n) {
376 dump_xname(ExtStrname(term, (int) n, strnames));
397 parse_description(const char *input_name)
399 static char empty[1];
403 size_t count_bools = 0;
404 size_t count_nums = 0;
405 size_t count_strs = 0;
410 if (stat(input_name, &sb) != 0
411 || (sb.st_mode & S_IFMT) != S_IFREG) {
412 failed("input is not a file");
415 if (sb.st_size == 0) {
416 failed("input is empty");
420 * None of the arrays could be larger than the input-file, and since it
421 * is small, just allocate the maximum for simplicity.
423 if ((my_blob = malloc((size_t) sb.st_size + 1)) == 0 ||
424 (my_boolcodes = typeCalloc(char *, sb.st_size)) == 0 ||
425 (my_numcodes = typeCalloc(char *, sb.st_size)) == 0 ||
426 (my_numvalues = typeCalloc(char *, sb.st_size)) == 0 ||
427 (my_strcodes = typeCalloc(char *, sb.st_size)) == 0 ||
428 (my_strvalues = typeCalloc(char *, sb.st_size)) == 0) {
429 failed("cannot allocate memory for input-file");
432 if ((fp = fopen(input_name, "r")) == 0)
433 failed("cannot open input-file");
434 len = fread(my_blob, sizeof(char), (size_t) sb.st_size, fp);
435 my_blob[sb.st_size] = '\0';
439 * First, get rid of comments and escaped newlines, as well as repeated
440 * colons to construct a canonical entry.
442 * FIXME: actually this should make an additional pass just to strip
443 * comment-lines and escaped newlines. But it is workable for infocmp
447 for (j = k = 0; j < len; ++j) {
466 state = pDescription;
481 my_blob[k++] = (char) ch;
499 my_blob[k++] = (char) ch;
518 state = pDescription;
521 my_blob[k++] = (char) ch;
535 my_blob[k++] = (char) ch;
540 if (my_blob[j + 1] == '\0') {
552 my_blob[k++] = (char) ch;
562 * Then, parse what's left, making indexes of the names and values.
565 for (j = 0; my_blob[j] != '\0'; ++j) {
568 switch (my_blob[j]) {
574 if (my_blob[j + 1] != '\0' && my_blob[j + 1] != ':')
587 * Commented-out capabilities might be accessible (they are in
590 if (my_blob[j] == '.' && my_blob[j + 1] == '.') {
593 if (my_blob[j + 1] != '\0') {
594 switch (my_blob[j + 2]) {
596 my_numvalues[count_nums] = &my_blob[j + 3];
597 my_numcodes[count_nums++] = &my_blob[j];
598 my_blob[j + 2] = '\0';
603 my_strvalues[count_strs] = &my_blob[j + 3];
604 my_strcodes[count_strs++] = &my_blob[j];
605 my_blob[j + 2] = '\0';
610 if (my_blob[j + 2] == '@') {
612 * We cannot get the type for a cancelled item
613 * directly, but can infer it assuming the input
614 * came from infocmp, which puts the data in a
618 my_strvalues[count_strs] = empty;
619 my_strcodes[count_strs++] = &my_blob[j];
620 } else if (count_nums) {
621 my_numvalues[count_nums] = empty;
622 my_numcodes[count_nums++] = &my_blob[j];
624 my_boolcodes[count_bools++] = &my_blob[j];
627 my_boolcodes[count_bools++] = &my_blob[j];
635 if (!isdigit(UChar(my_blob[j]))) {
641 switch (my_blob[j]) {
643 if (my_blob[j + 1] == '\0') {
666 my_boolcodes[count_bools] = 0;
667 my_numcodes[count_nums] = 0;
668 my_numvalues[count_nums] = 0;
669 my_strcodes[count_strs] = 0;
670 my_strvalues[count_strs] = 0;
673 printf("bools:%d\n", (int) count_bools);
674 for (j = 0; my_boolcodes[j]; ++j)
675 printf("%5d:%s\n", (int) j, my_boolcodes[j]);
677 printf("numbers:%d\n", (int) count_nums);
678 for (j = 0; my_numcodes[j]; ++j)
679 printf("%5d:%s(%s)\n", (int) j, my_numcodes[j], my_numvalues[j]);
681 printf("strings:%d\n", (int) count_strs);
682 for (j = 0; my_strcodes[j]; ++j)
683 printf("%5d:%s(%s)\n", (int) j, my_strcodes[j], my_strvalues[j]);
689 copy_code_list(NCURSES_CONST char *const *list)
698 for (pass = 0; pass < 2; ++pass) {
699 for (count = 0; list[count] != 0; ++count) {
700 size_t chunk = strlen(list[count]) + 1;
704 result[count] = unused;
705 _nc_STRCPY(unused, list[count], length);
710 blob = malloc(length);
711 result = typeCalloc(char *, count + 1);
713 if (blob == 0 || result == 0)
714 failed("copy_code_list failed");
723 free_code_list(char **list)
730 #endif /* NO_LEAKS */
731 #endif /* USE_CODE_LISTS */
736 static const char *msg[] =
738 "Usage: demo_termcap [options] [terminal]",
740 "If no options are given, print all (boolean, numeric, string)",
741 "capabilities for the given terminal, using short names.",
744 " -a try all names, print capabilities found",
745 " -b print boolean-capabilities",
746 " -d LIST colon-separated list of databases to use",
747 " -e NAME environment variable to set with -d option",
748 " -i NAME terminal description to use as names for \"-a\" option, etc.",
749 " -n print numeric-capabilities",
750 " -q quiet (prints only counts)",
751 " -r COUNT repeat for given count",
752 " -s print string-capabilities",
753 " -v print termcap-variables",
754 #ifdef NCURSES_VERSION
755 " -x print extended capabilities",
759 for (n = 0; n < SIZEOF(msg); ++n) {
760 fprintf(stderr, "%s\n", msg[n]);
762 ExitProgram(EXIT_FAILURE);
766 main(int argc, char *argv[])
772 char *input_name = 0;
777 while ((n = getopt(argc, argv, "abd:e:i:nqr:svxy")) != -1) {
801 if ((r_opt = atoi(optarg)) <= 0)
825 #if HAVE_USE_EXTENDED_NAMES
826 use_extended_names(x_opt);
829 if (!(b_opt || n_opt || s_opt)) {
838 for (repeat = 0; repeat < r_opt; ++repeat) {
840 for (n = optind; n < argc; ++n) {
841 brute_force(argv[n]);
843 } else if ((name = getenv("TERM")) != 0) {
846 static char dumb[] = "dumb";
851 if (input_name != 0) {
852 parse_description(input_name);
856 my_boolcodes = copy_code_list(boolcodes);
857 my_numcodes = copy_code_list(numcodes);
858 my_strcodes = copy_code_list(strcodes);
862 failed("no capability-lists available (use -i option)");
864 #endif /* USE_CODE_LISTS */
865 for (repeat = 0; repeat < r_opt; ++repeat) {
867 for (n = optind; n < argc; ++n) {
868 demo_termcap(argv[n]);
870 } else if ((name = getenv("TERM")) != 0) {
873 static char dumb[] = "dumb";
879 printf("%ld values (%ld booleans, %ld numbers, %ld strings)\n",
880 total_values, total_b_values, total_n_values, total_s_values);
882 #if defined(NCURSES_VERSION) || defined(HAVE_CURSES_DATA_OSPEED)
884 show_number("PC", PC);
885 show_string("UP", UP);
886 show_string("BC", BC);
887 show_number("ospeed", (int) ospeed);
894 free_code_list(my_boolcodes);
895 free_code_list(my_numcodes);
896 free_code_list(my_strcodes);
898 #endif /* NO_LEAKS */
900 ExitProgram(EXIT_SUCCESS);
905 main(int argc GCC_UNUSED,
906 char *argv[]GCC_UNUSED)
908 failed("This program requires termcap");