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.49 2015/10/10 20:41:16 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 char *result = malloc(strlen(e_opt) + 2 + (size_t) (p - q));
104 sprintf(result, "%s=%.*s", e_opt, (int) (p - q), q);
111 if (d_opt && e_opt) {
114 for (pass = 0; pass < 2; ++pass) {
118 for (p = q = d_opt; *p != '\0'; ++p) {
122 db_list[count] = make_dbitem(p, q);
131 db_list[count] = make_dbitem(p, q);
136 db_list = typeCalloc(char *, count + 1);
148 if ((result = db_list[db_item]) == 0) {
155 printf("** %s\n", result);
164 for (n = 0; db_list[n]; ++n)
172 show_string(const char *name, const char *value)
174 printf(FNAME(str), name);
175 if (value == ((char *) -1)) {
177 } else if (value == ((char *) 0)) {
180 while (*value != 0) {
181 int ch = UChar(*value++);
187 fputs("\\E", stdout);
190 fputs("\\b", stdout);
193 fputs("\\f", stdout);
196 fputs("\\n", stdout);
199 fputs("\\r", stdout);
202 fputs("\\s", stdout);
205 fputs("\\t", stdout);
208 fputs("\\^", stdout);
211 fputs("\\072", stdout);
214 fputs("\\\\", stdout);
220 printf("^%c", ch + '@');
222 printf("\\%03o", ch);
231 show_number(const char *name, int value)
233 printf(FNAME(num), name);
234 printf(" %d\n", value);
238 dumpit(NCURSES_CONST char *cap)
241 * One of the limitations of the termcap interface is that the library
242 * cannot determine the size of the buffer passed via tgetstr(), nor the
243 * amount of space remaining. This demo simply reuses the whole buffer
244 * for each call; a normal termcap application would try to use the buffer
245 * to hold all of the strings extracted from the terminal entry.
247 char area[1024], *ap = area;
251 if ((str = tgetstr(cap, &ap)) != 0) {
256 * Note that the strings returned are mostly terminfo format, since
257 * ncurses does not convert except for a handful of special cases.
259 show_string(cap, str);
261 } else if ((num = tgetnum(cap)) >= 0) {
265 show_number(cap, num);
267 } else if (tgetflag(cap) > 0) {
271 printf(FNAME(flg), cap);
272 printf("%s\n", "true");
281 brute_force(const char *name)
286 putenv(next_dbitem());
289 printf("Terminal type \"%s\"\n", name);
290 if (tgetent(buffer, name) >= 0) {
295 for (EachCapName(c1)) {
298 for (EachCapName(c2)) {
311 dump_xname(NCURSES_CONST char *cap)
313 if (strlen(cap) == 2)
319 demo_termcap(NCURSES_CONST char *name)
322 NCURSES_CONST char *cap;
326 putenv(next_dbitem());
329 printf("Terminal type \"%s\"\n", name);
330 if (tgetent(buffer, name) >= 0) {
334 cap = my_boolcodes[n];
343 cap = my_numcodes[n];
352 cap = my_strcodes[n];
358 #ifdef NCURSES_VERSION
359 if (x_opt && (my_blob == 0) && y_opt) {
361 TERMTYPE *term = &(cur_term->type);
363 && ((NUM_BOOLEANS(term) != BOOLCOUNT)
364 || (NUM_NUMBERS(term) != NUMCOUNT)
365 || (NUM_STRINGS(term) != STRCOUNT))) {
366 for (n = BOOLCOUNT; n < NUM_BOOLEANS(term); ++n) {
367 dump_xname(ExtBoolname(term, (int) n, boolnames));
369 for (n = NUMCOUNT; n < NUM_NUMBERS(term); ++n) {
370 dump_xname(ExtNumname(term, (int) n, numnames));
372 for (n = STRCOUNT; n < NUM_STRINGS(term); ++n) {
373 dump_xname(ExtStrname(term, (int) n, strnames));
394 parse_description(const char *input_name)
396 static char empty[1];
400 size_t count_bools = 0;
401 size_t count_nums = 0;
402 size_t count_strs = 0;
407 if (stat(input_name, &sb) != 0
408 || (sb.st_mode & S_IFMT) != S_IFREG) {
409 failed("input is not a file");
412 if (sb.st_size == 0) {
413 failed("input is empty");
417 * None of the arrays could be larger than the input-file, and since it
418 * is small, just allocate the maximum for simplicity.
420 if ((my_blob = malloc((size_t) sb.st_size + 1)) == 0 ||
421 (my_boolcodes = typeCalloc(char *, sb.st_size)) == 0 ||
422 (my_numcodes = typeCalloc(char *, sb.st_size)) == 0 ||
423 (my_numvalues = typeCalloc(char *, sb.st_size)) == 0 ||
424 (my_strcodes = typeCalloc(char *, sb.st_size)) == 0 ||
425 (my_strvalues = typeCalloc(char *, sb.st_size)) == 0) {
426 failed("cannot allocate memory for input-file");
429 if ((fp = fopen(input_name, "r")) == 0)
430 failed("cannot open input-file");
431 len = fread(my_blob, sizeof(char), (size_t) sb.st_size, fp);
432 my_blob[sb.st_size] = '\0';
436 * First, get rid of comments and escaped newlines, as well as repeated
437 * colons to construct a canonical entry.
439 * FIXME: actually this should make an additional pass just to strip
440 * comment-lines and escaped newlines. But it is workable for infocmp
444 for (j = k = 0; j < len; ++j) {
463 state = pDescription;
478 my_blob[k++] = (char) ch;
496 my_blob[k++] = (char) ch;
515 state = pDescription;
518 my_blob[k++] = (char) ch;
532 my_blob[k++] = (char) ch;
537 if (my_blob[j + 1] == '\0') {
549 my_blob[k++] = (char) ch;
559 * Then, parse what's left, making indexes of the names and values.
562 for (j = 0; my_blob[j] != '\0'; ++j) {
565 switch (my_blob[j]) {
571 if (my_blob[j + 1] != '\0' && my_blob[j + 1] != ':')
584 * Commented-out capabilities might be accessible (they are in
587 if (my_blob[j] == '.' && my_blob[j + 1] == '.') {
590 if (my_blob[j + 1] != '\0') {
591 switch (my_blob[j + 2]) {
593 my_numvalues[count_nums] = &my_blob[j + 3];
594 my_numcodes[count_nums++] = &my_blob[j];
595 my_blob[j + 2] = '\0';
600 my_strvalues[count_strs] = &my_blob[j + 3];
601 my_strcodes[count_strs++] = &my_blob[j];
602 my_blob[j + 2] = '\0';
607 if (my_blob[j + 2] == '@') {
609 * We cannot get the type for a cancelled item
610 * directly, but can infer it assuming the input
611 * came from infocmp, which puts the data in a
615 my_strvalues[count_strs] = empty;
616 my_strcodes[count_strs++] = &my_blob[j];
617 } else if (count_nums) {
618 my_numvalues[count_nums] = empty;
619 my_numcodes[count_nums++] = &my_blob[j];
621 my_boolcodes[count_bools++] = &my_blob[j];
624 my_boolcodes[count_bools++] = &my_blob[j];
632 if (!isdigit(UChar(my_blob[j]))) {
638 switch (my_blob[j]) {
640 if (my_blob[j + 1] == '\0') {
663 my_boolcodes[count_bools] = 0;
664 my_numcodes[count_nums] = 0;
665 my_numvalues[count_nums] = 0;
666 my_strcodes[count_strs] = 0;
667 my_strvalues[count_strs] = 0;
670 printf("bools:%d\n", (int) count_bools);
671 for (j = 0; my_boolcodes[j]; ++j)
672 printf("%5d:%s\n", (int) j, my_boolcodes[j]);
674 printf("numbers:%d\n", (int) count_nums);
675 for (j = 0; my_numcodes[j]; ++j)
676 printf("%5d:%s(%s)\n", (int) j, my_numcodes[j], my_numvalues[j]);
678 printf("strings:%d\n", (int) count_strs);
679 for (j = 0; my_strcodes[j]; ++j)
680 printf("%5d:%s(%s)\n", (int) j, my_strcodes[j], my_strvalues[j]);
686 copy_code_list(NCURSES_CONST char *const *list)
695 for (pass = 0; pass < 2; ++pass) {
696 for (count = 0; list[count] != 0; ++count) {
697 size_t chunk = strlen(list[count]) + 1;
701 result[count] = unused;
702 strcpy(unused, list[count]);
707 blob = malloc(length);
708 result = typeCalloc(char *, count + 1);
710 if (blob == 0 || result == 0)
711 failed("copy_code_list failed");
722 static const char *msg[] =
724 "Usage: demo_termcap [options] [terminal]",
726 "If no options are given, print all (boolean, numeric, string)",
727 "capabilities for the given terminal, using short names.",
730 " -a try all names, print capabilities found",
731 " -b print boolean-capabilities",
732 " -d LIST colon-separated list of databases to use",
733 " -e NAME environment variable to set with -d option",
734 " -i NAME terminal description to use as names for \"-a\" option, etc.",
735 " -n print numeric-capabilities",
736 " -q quiet (prints only counts)",
737 " -r COUNT repeat for given count",
738 " -s print string-capabilities",
739 " -v print termcap-variables",
740 #ifdef NCURSES_VERSION
741 " -x print extended capabilities",
745 for (n = 0; n < SIZEOF(msg); ++n) {
746 fprintf(stderr, "%s\n", msg[n]);
748 ExitProgram(EXIT_FAILURE);
752 main(int argc, char *argv[])
758 char *input_name = 0;
763 while ((n = getopt(argc, argv, "abd:e:i:nqr:svxy")) != -1) {
787 if ((r_opt = atoi(optarg)) <= 0)
811 #if HAVE_USE_EXTENDED_NAMES
812 use_extended_names(x_opt);
815 if (!(b_opt || n_opt || s_opt)) {
824 for (repeat = 0; repeat < r_opt; ++repeat) {
826 for (n = optind; n < argc; ++n) {
827 brute_force(argv[n]);
829 } else if ((name = getenv("TERM")) != 0) {
832 static char dumb[] = "dumb";
837 if (input_name != 0) {
838 parse_description(input_name);
842 my_boolcodes = copy_code_list(boolcodes);
843 my_numcodes = copy_code_list(numcodes);
844 my_strcodes = copy_code_list(strcodes);
848 failed("no capability-lists available (use -i option)");
850 #endif /* USE_CODE_LISTS */
851 for (repeat = 0; repeat < r_opt; ++repeat) {
853 for (n = optind; n < argc; ++n) {
854 demo_termcap(argv[n]);
856 } else if ((name = getenv("TERM")) != 0) {
859 static char dumb[] = "dumb";
865 printf("%ld values (%ld booleans, %ld numbers, %ld strings)\n",
866 total_values, total_b_values, total_n_values, total_s_values);
868 #if defined(NCURSES_VERSION) || defined(HAVE_CURSES_DATA_OSPEED)
870 show_number("PC", PC);
871 show_string("UP", UP);
872 show_string("BC", BC);
873 show_number("ospeed", ospeed);
879 ExitProgram(EXIT_SUCCESS);
884 main(int argc GCC_UNUSED,
885 char *argv[]GCC_UNUSED)
887 failed("This program requires termcap");