1 /****************************************************************************
2 * Copyright (c) 2005-2014,2015 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.48 2015/08/08 20:25: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
54 failed(const char *msg)
56 fprintf(stderr, "%s\n", msg);
57 ExitProgram(EXIT_FAILURE);
62 #if defined(HAVE_CURSES_DATA_BOOLNAMES) || defined(DECL_CURSES_DATA_BOOLNAMES)
63 #define USE_CODE_LISTS 1
65 #define USE_CODE_LISTS 0
69 #define FNAME(type) "%s %-*s = ", #type, FCOLS
71 static bool b_opt = FALSE;
72 static bool n_opt = FALSE;
73 static bool s_opt = FALSE;
74 static bool q_opt = FALSE;
75 static bool x_opt = FALSE;
76 static bool y_opt = FALSE;
80 static char **db_list;
84 static char **my_boolcodes;
85 static char **my_numcodes;
86 static char **my_numvalues;
87 static char **my_strcodes;
88 static char **my_strvalues;
90 static long total_values;
91 static long total_b_values;
92 static long total_n_values;
93 static long total_s_values;
95 #define isCapName(c) (isgraph(c) && strchr("^=:\\", c) == 0)
96 #define EachCapName(n) n = 33; n < 127; ++n
99 make_dbitem(char *p, char *q)
101 char *result = malloc(strlen(e_opt) + 2 + (size_t) (p - q));
102 sprintf(result, "%s=%.*s", e_opt, (int) (p - q), q);
109 if (d_opt && e_opt) {
112 for (pass = 0; pass < 2; ++pass) {
116 for (p = q = d_opt; *p != '\0'; ++p) {
120 db_list[count] = make_dbitem(p, q);
129 db_list[count] = make_dbitem(p, q);
134 db_list = typeCalloc(char *, count + 1);
146 if ((result = db_list[db_item]) == 0) {
153 printf("** %s\n", result);
162 for (n = 0; db_list[n]; ++n)
170 show_string(const char *name, const char *value)
172 printf(FNAME(str), name);
173 if (value == ((char *) -1)) {
175 } else if (value == ((char *) 0)) {
178 while (*value != 0) {
179 int ch = UChar(*value++);
185 fputs("\\E", stdout);
188 fputs("\\b", stdout);
191 fputs("\\f", stdout);
194 fputs("\\n", stdout);
197 fputs("\\r", stdout);
200 fputs("\\s", stdout);
203 fputs("\\t", stdout);
206 fputs("\\^", stdout);
209 fputs("\\072", stdout);
212 fputs("\\\\", stdout);
218 printf("^%c", ch + '@');
220 printf("\\%03o", ch);
229 show_number(const char *name, int value)
231 printf(FNAME(num), name);
232 printf(" %d\n", value);
236 dumpit(NCURSES_CONST char *cap)
239 * One of the limitations of the termcap interface is that the library
240 * cannot determine the size of the buffer passed via tgetstr(), nor the
241 * amount of space remaining. This demo simply reuses the whole buffer
242 * for each call; a normal termcap application would try to use the buffer
243 * to hold all of the strings extracted from the terminal entry.
245 char area[1024], *ap = area;
249 if ((str = tgetstr(cap, &ap)) != 0) {
254 * Note that the strings returned are mostly terminfo format, since
255 * ncurses does not convert except for a handful of special cases.
257 show_string(cap, str);
259 } else if ((num = tgetnum(cap)) >= 0) {
263 show_number(cap, num);
265 } else if (tgetflag(cap) > 0) {
269 printf(FNAME(flg), cap);
270 printf("%s\n", "true");
279 brute_force(const char *name)
284 putenv(next_dbitem());
287 printf("Terminal type \"%s\"\n", name);
288 if (tgetent(buffer, name) >= 0) {
293 for (EachCapName(c1)) {
296 for (EachCapName(c2)) {
309 dump_xname(NCURSES_CONST char *cap)
311 if (strlen(cap) == 2)
317 demo_termcap(NCURSES_CONST char *name)
320 NCURSES_CONST char *cap;
324 putenv(next_dbitem());
327 printf("Terminal type \"%s\"\n", name);
328 if (tgetent(buffer, name) >= 0) {
332 cap = my_boolcodes[n];
341 cap = my_numcodes[n];
350 cap = my_strcodes[n];
356 #ifdef NCURSES_VERSION
357 if (x_opt && (my_blob == 0) && y_opt) {
359 TERMTYPE *term = &(cur_term->type);
361 && ((NUM_BOOLEANS(term) != BOOLCOUNT)
362 || (NUM_NUMBERS(term) != NUMCOUNT)
363 || (NUM_STRINGS(term) != STRCOUNT))) {
364 for (n = BOOLCOUNT; n < NUM_BOOLEANS(term); ++n) {
365 dump_xname(ExtBoolname(term, (int) n, boolnames));
367 for (n = NUMCOUNT; n < NUM_NUMBERS(term); ++n) {
368 dump_xname(ExtNumname(term, (int) n, numnames));
370 for (n = STRCOUNT; n < NUM_STRINGS(term); ++n) {
371 dump_xname(ExtStrname(term, (int) n, strnames));
392 parse_description(const char *input_name)
394 static char empty[1];
398 size_t count_bools = 0;
399 size_t count_nums = 0;
400 size_t count_strs = 0;
405 if (stat(input_name, &sb) != 0
406 || (sb.st_mode & S_IFMT) != S_IFREG) {
407 failed("input is not a file");
410 if (sb.st_size == 0) {
411 failed("input is empty");
415 * None of the arrays could be larger than the input-file, and since it
416 * is small, just allocate the maximum for simplicity.
418 if ((my_blob = malloc((size_t) sb.st_size + 1)) == 0 ||
419 (my_boolcodes = typeCalloc(char *, sb.st_size)) == 0 ||
420 (my_numcodes = typeCalloc(char *, sb.st_size)) == 0 ||
421 (my_numvalues = typeCalloc(char *, sb.st_size)) == 0 ||
422 (my_strcodes = typeCalloc(char *, sb.st_size)) == 0 ||
423 (my_strvalues = typeCalloc(char *, sb.st_size)) == 0) {
424 failed("cannot allocate memory for input-file");
427 if ((fp = fopen(input_name, "r")) == 0)
428 failed("cannot open input-file");
429 len = fread(my_blob, sizeof(char), (size_t) sb.st_size, fp);
430 my_blob[sb.st_size] = '\0';
434 * First, get rid of comments and escaped newlines, as well as repeated
435 * colons to construct a canonical entry.
437 * FIXME: actually this should make an additional pass just to strip
438 * comment-lines and escaped newlines. But it is workable for infocmp
442 for (j = k = 0; j < len; ++j) {
461 state = pDescription;
476 my_blob[k++] = (char) ch;
494 my_blob[k++] = (char) ch;
513 state = pDescription;
516 my_blob[k++] = (char) ch;
530 my_blob[k++] = (char) ch;
535 if (my_blob[j + 1] == '\0') {
547 my_blob[k++] = (char) ch;
557 * Then, parse what's left, making indexes of the names and values.
560 for (j = 0; my_blob[j] != '\0'; ++j) {
563 switch (my_blob[j]) {
569 if (my_blob[j + 1] != '\0' && my_blob[j + 1] != ':')
582 * Commented-out capabilities might be accessible (they are in
585 if (my_blob[j] == '.' && my_blob[j + 1] == '.') {
588 if (my_blob[j + 1] != '\0') {
589 switch (my_blob[j + 2]) {
591 my_numvalues[count_nums] = &my_blob[j + 3];
592 my_numcodes[count_nums++] = &my_blob[j];
593 my_blob[j + 2] = '\0';
598 my_strvalues[count_strs] = &my_blob[j + 3];
599 my_strcodes[count_strs++] = &my_blob[j];
600 my_blob[j + 2] = '\0';
605 if (my_blob[j + 2] == '@') {
607 * We cannot get the type for a cancelled item
608 * directly, but can infer it assuming the input
609 * came from infocmp, which puts the data in a
613 my_strvalues[count_strs] = empty;
614 my_strcodes[count_strs++] = &my_blob[j];
615 } else if (count_nums) {
616 my_numvalues[count_nums] = empty;
617 my_numcodes[count_nums++] = &my_blob[j];
619 my_boolcodes[count_bools++] = &my_blob[j];
622 my_boolcodes[count_bools++] = &my_blob[j];
630 if (!isdigit(UChar(my_blob[j]))) {
636 switch (my_blob[j]) {
638 if (my_blob[j + 1] == '\0') {
661 my_boolcodes[count_bools] = 0;
662 my_numcodes[count_nums] = 0;
663 my_numvalues[count_nums] = 0;
664 my_strcodes[count_strs] = 0;
665 my_strvalues[count_strs] = 0;
668 printf("bools:%d\n", (int) count_bools);
669 for (j = 0; my_boolcodes[j]; ++j)
670 printf("%5d:%s\n", (int) j, my_boolcodes[j]);
672 printf("numbers:%d\n", (int) count_nums);
673 for (j = 0; my_numcodes[j]; ++j)
674 printf("%5d:%s(%s)\n", (int) j, my_numcodes[j], my_numvalues[j]);
676 printf("strings:%d\n", (int) count_strs);
677 for (j = 0; my_strcodes[j]; ++j)
678 printf("%5d:%s(%s)\n", (int) j, my_strcodes[j], my_strvalues[j]);
684 copy_code_list(NCURSES_CONST char *const *list)
693 for (pass = 0; pass < 2; ++pass) {
694 for (count = 0; list[count] != 0; ++count) {
695 size_t chunk = strlen(list[count]) + 1;
699 result[count] = unused;
700 strcpy(unused, list[count]);
705 blob = malloc(length);
706 result = typeCalloc(char *, count + 1);
708 if (blob == 0 || result == 0)
709 failed("copy_code_list failed");
720 static const char *msg[] =
722 "Usage: demo_termcap [options] [terminal]",
724 "If no options are given, print all (boolean, numeric, string)",
725 "capabilities for the given terminal, using short names.",
728 " -a try all names, print capabilities found",
729 " -b print boolean-capabilities",
730 " -d LIST colon-separated list of databases to use",
731 " -e NAME environment variable to set with -d option",
732 " -i NAME terminal description to use as names for \"-a\" option, etc.",
733 " -n print numeric-capabilities",
734 " -q quiet (prints only counts)",
735 " -r COUNT repeat for given count",
736 " -s print string-capabilities",
737 " -v print termcap-variables",
738 #ifdef NCURSES_VERSION
739 " -x print extended capabilities",
743 for (n = 0; n < SIZEOF(msg); ++n) {
744 fprintf(stderr, "%s\n", msg[n]);
746 ExitProgram(EXIT_FAILURE);
750 main(int argc, char *argv[])
756 char *input_name = 0;
761 while ((n = getopt(argc, argv, "abd:e:i:nqr:svxy")) != -1) {
785 if ((r_opt = atoi(optarg)) <= 0)
809 #if HAVE_USE_EXTENDED_NAMES
810 use_extended_names(x_opt);
813 if (!(b_opt || n_opt || s_opt)) {
822 for (repeat = 0; repeat < r_opt; ++repeat) {
824 for (n = optind; n < argc; ++n) {
825 brute_force(argv[n]);
827 } else if ((name = getenv("TERM")) != 0) {
830 static char dumb[] = "dumb";
835 if (input_name != 0) {
836 parse_description(input_name);
840 my_boolcodes = copy_code_list(boolcodes);
841 my_numcodes = copy_code_list(numcodes);
842 my_strcodes = copy_code_list(strcodes);
846 failed("no capability-lists available (use -i option)");
848 #endif /* USE_CODE_LISTS */
849 for (repeat = 0; repeat < r_opt; ++repeat) {
851 for (n = optind; n < argc; ++n) {
852 demo_termcap(argv[n]);
854 } else if ((name = getenv("TERM")) != 0) {
857 static char dumb[] = "dumb";
863 printf("%ld values (%ld booleans, %ld numbers, %ld strings)\n",
864 total_values, total_b_values, total_n_values, total_s_values);
866 #if defined(NCURSES_VERSION) || defined(HAVE_CURSES_DATA_OSPEED)
868 show_number("PC", PC);
869 show_string("UP", UP);
870 show_string("BC", BC);
871 show_number("ospeed", ospeed);
877 ExitProgram(EXIT_SUCCESS);
882 main(int argc GCC_UNUSED,
883 char *argv[]GCC_UNUSED)
885 failed("This program requires termcap");