1 /****************************************************************************
2 * Copyright (c) 2005-2013,2014 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.46 2014/10/10 00:35:43 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
50 failed(const char *msg)
52 fprintf(stderr, "%s\n", msg);
53 ExitProgram(EXIT_FAILURE);
58 #if defined(HAVE_CURSES_DATA_BOOLNAMES) || defined(DECL_CURSES_DATA_BOOLNAMES)
59 #define USE_CODE_LISTS 1
61 #define USE_CODE_LISTS 0
65 #define FNAME(type) "%s %-*s = ", #type, FCOLS
67 static bool b_opt = FALSE;
68 static bool n_opt = FALSE;
69 static bool s_opt = FALSE;
70 static bool q_opt = FALSE;
71 static bool x_opt = FALSE;
72 static bool y_opt = FALSE;
76 static char **db_list;
80 static char **my_boolcodes;
81 static char **my_numcodes;
82 static char **my_numvalues;
83 static char **my_strcodes;
84 static char **my_strvalues;
86 static long total_values;
87 static long total_b_values;
88 static long total_n_values;
89 static long total_s_values;
91 #define isCapName(c) (isgraph(c) && strchr("^=:\\", c) == 0)
92 #define EachCapName(n) n = 33; n < 127; ++n
95 make_dbitem(char *p, char *q)
97 char *result = malloc(strlen(e_opt) + 2 + (size_t) (p - q));
98 sprintf(result, "%s=%.*s", e_opt, (int) (p - q), q);
105 if (d_opt && e_opt) {
108 for (pass = 0; pass < 2; ++pass) {
112 for (p = q = d_opt; *p != '\0'; ++p) {
116 db_list[count] = make_dbitem(p, q);
125 db_list[count] = make_dbitem(p, q);
130 db_list = typeCalloc(char *, count + 1);
142 if ((result = db_list[db_item]) == 0) {
149 printf("** %s\n", result);
158 for (n = 0; db_list[n]; ++n)
166 dumpit(NCURSES_CONST char *cap)
169 * One of the limitations of the termcap interface is that the library
170 * cannot determine the size of the buffer passed via tgetstr(), nor the
171 * amount of space remaining. This demo simply reuses the whole buffer
172 * for each call; a normal termcap application would try to use the buffer
173 * to hold all of the strings extracted from the terminal entry.
175 char area[1024], *ap = area;
179 if ((str = tgetstr(cap, &ap)) != 0) {
184 * Note that the strings returned are mostly terminfo format, since
185 * ncurses does not convert except for a handful of special cases.
187 printf(FNAME(str), cap);
189 int ch = UChar(*str++);
195 fputs("\\E", stdout);
198 fputs("\\b", stdout);
201 fputs("\\f", stdout);
204 fputs("\\n", stdout);
207 fputs("\\r", stdout);
210 fputs("\\s", stdout);
213 fputs("\\t", stdout);
216 fputs("\\^", stdout);
219 fputs("\\072", stdout);
222 fputs("\\\\", stdout);
228 printf("^%c", ch + '@');
230 printf("\\%03o", ch);
236 } else if ((num = tgetnum(cap)) >= 0) {
240 printf(FNAME(num), cap);
241 printf(" %d\n", num);
243 } else if (tgetflag(cap) > 0) {
247 printf(FNAME(flg), cap);
248 printf("%s\n", "true");
257 brute_force(const char *name)
262 putenv(next_dbitem());
265 printf("Terminal type \"%s\"\n", name);
266 if (tgetent(buffer, name) >= 0) {
271 for (EachCapName(c1)) {
274 for (EachCapName(c2)) {
287 dump_xname(NCURSES_CONST char *cap)
289 if (strlen(cap) == 2)
295 demo_termcap(NCURSES_CONST char *name)
298 NCURSES_CONST char *cap;
302 putenv(next_dbitem());
305 printf("Terminal type \"%s\"\n", name);
306 if (tgetent(buffer, name) >= 0) {
310 cap = my_boolcodes[n];
319 cap = my_numcodes[n];
328 cap = my_strcodes[n];
334 #ifdef NCURSES_VERSION
335 if (x_opt && (my_blob == 0) && y_opt) {
337 TERMTYPE *term = &(cur_term->type);
339 && ((NUM_BOOLEANS(term) != BOOLCOUNT)
340 || (NUM_NUMBERS(term) != NUMCOUNT)
341 || (NUM_STRINGS(term) != STRCOUNT))) {
342 for (n = BOOLCOUNT; n < NUM_BOOLEANS(term); ++n) {
343 dump_xname(ExtBoolname(term, (int) n, boolnames));
345 for (n = NUMCOUNT; n < NUM_NUMBERS(term); ++n) {
346 dump_xname(ExtNumname(term, (int) n, numnames));
348 for (n = STRCOUNT; n < NUM_STRINGS(term); ++n) {
349 dump_xname(ExtStrname(term, (int) n, strnames));
370 parse_description(const char *input_name)
372 static char empty[1];
376 size_t count_bools = 0;
377 size_t count_nums = 0;
378 size_t count_strs = 0;
383 if (stat(input_name, &sb) != 0
384 || (sb.st_mode & S_IFMT) != S_IFREG) {
385 failed("input is not a file");
388 if (sb.st_size == 0) {
389 failed("input is empty");
393 * None of the arrays could be larger than the input-file, and since it
394 * is small, just allocate the maximum for simplicity.
396 if ((my_blob = malloc((size_t) sb.st_size + 1)) == 0 ||
397 (my_boolcodes = typeCalloc(char *, sb.st_size)) == 0 ||
398 (my_numcodes = typeCalloc(char *, sb.st_size)) == 0 ||
399 (my_numvalues = typeCalloc(char *, sb.st_size)) == 0 ||
400 (my_strcodes = typeCalloc(char *, sb.st_size)) == 0 ||
401 (my_strvalues = typeCalloc(char *, sb.st_size)) == 0) {
402 failed("cannot allocate memory for input-file");
405 if ((fp = fopen(input_name, "r")) == 0)
406 failed("cannot open input-file");
407 len = fread(my_blob, sizeof(char), (size_t) sb.st_size, fp);
408 my_blob[sb.st_size] = '\0';
412 * First, get rid of comments and escaped newlines, as well as repeated
413 * colons to construct a canonical entry.
415 * FIXME: actually this should make an additional pass just to strip
416 * comment-lines and escaped newlines. But it is workable for infocmp
420 for (j = k = 0; j < len; ++j) {
439 state = pDescription;
454 my_blob[k++] = (char) ch;
472 my_blob[k++] = (char) ch;
491 state = pDescription;
494 my_blob[k++] = (char) ch;
508 my_blob[k++] = (char) ch;
513 if (my_blob[j + 1] == '\0') {
525 my_blob[k++] = (char) ch;
535 * Then, parse what's left, making indexes of the names and values.
538 for (j = 0; my_blob[j] != '\0'; ++j) {
541 switch (my_blob[j]) {
547 if (my_blob[j + 1] != '\0' && my_blob[j + 1] != ':')
560 * Commented-out capabilities might be accessible (they are in
563 if (my_blob[j] == '.' && my_blob[j + 1] == '.') {
566 if (my_blob[j + 1] != '\0') {
567 switch (my_blob[j + 2]) {
569 my_numvalues[count_nums] = &my_blob[j + 3];
570 my_numcodes[count_nums++] = &my_blob[j];
571 my_blob[j + 2] = '\0';
576 my_strvalues[count_strs] = &my_blob[j + 3];
577 my_strcodes[count_strs++] = &my_blob[j];
578 my_blob[j + 2] = '\0';
583 if (my_blob[j + 2] == '@') {
585 * We cannot get the type for a cancelled item
586 * directly, but can infer it assuming the input
587 * came from infocmp, which puts the data in a
591 my_strvalues[count_strs] = empty;
592 my_strcodes[count_strs++] = &my_blob[j];
593 } else if (count_nums) {
594 my_numvalues[count_nums] = empty;
595 my_numcodes[count_nums++] = &my_blob[j];
597 my_boolcodes[count_bools++] = &my_blob[j];
600 my_boolcodes[count_bools++] = &my_blob[j];
608 if (!isdigit(UChar(my_blob[j]))) {
614 switch (my_blob[j]) {
616 if (my_blob[j + 1] == '\0') {
639 my_boolcodes[count_bools] = 0;
640 my_numcodes[count_nums] = 0;
641 my_numvalues[count_nums] = 0;
642 my_strcodes[count_strs] = 0;
643 my_strvalues[count_strs] = 0;
646 printf("bools:%d\n", (int) count_bools);
647 for (j = 0; my_boolcodes[j]; ++j)
648 printf("%5d:%s\n", (int) j, my_boolcodes[j]);
650 printf("numbers:%d\n", (int) count_nums);
651 for (j = 0; my_numcodes[j]; ++j)
652 printf("%5d:%s(%s)\n", (int) j, my_numcodes[j], my_numvalues[j]);
654 printf("strings:%d\n", (int) count_strs);
655 for (j = 0; my_strcodes[j]; ++j)
656 printf("%5d:%s(%s)\n", (int) j, my_strcodes[j], my_strvalues[j]);
662 copy_code_list(NCURSES_CONST char *const *list)
671 for (pass = 0; pass < 2; ++pass) {
672 for (count = 0; list[count] != 0; ++count) {
673 size_t chunk = strlen(list[count]) + 1;
677 result[count] = unused;
678 strcpy(unused, list[count]);
683 blob = malloc(length);
684 result = typeCalloc(char *, count + 1);
686 if (blob == 0 || result == 0)
687 failed("copy_code_list failed");
698 static const char *msg[] =
700 "Usage: demo_termcap [options] [terminal]",
702 "If no options are given, print all (boolean, numeric, string)",
703 "capabilities for the given terminal, using short names.",
706 " -a try all names, print capabilities found",
707 " -b print boolean-capabilities",
708 " -d LIST colon-separated list of databases to use",
709 " -e NAME environment variable to set with -d option",
710 " -i NAME terminal description to use as names for \"-a\" option, etc.",
711 " -n print numeric-capabilities",
712 " -q quiet (prints only counts)",
713 " -r COUNT repeat for given count",
714 " -s print string-capabilities",
715 #ifdef NCURSES_VERSION
716 " -x print extended capabilities",
720 for (n = 0; n < SIZEOF(msg); ++n) {
721 fprintf(stderr, "%s\n", msg[n]);
723 ExitProgram(EXIT_FAILURE);
727 main(int argc, char *argv[])
732 char *input_name = 0;
737 while ((n = getopt(argc, argv, "abd:e:i:nqr:sxy")) != -1) {
761 if ((r_opt = atoi(optarg)) <= 0)
782 #if HAVE_USE_EXTENDED_NAMES
783 use_extended_names(x_opt);
786 if (!(b_opt || n_opt || s_opt)) {
795 for (repeat = 0; repeat < r_opt; ++repeat) {
797 for (n = optind; n < argc; ++n) {
798 brute_force(argv[n]);
800 } else if ((name = getenv("TERM")) != 0) {
803 static char dumb[] = "dumb";
808 if (input_name != 0) {
809 parse_description(input_name);
813 my_boolcodes = copy_code_list(boolcodes);
814 my_numcodes = copy_code_list(numcodes);
815 my_strcodes = copy_code_list(strcodes);
819 failed("no capability-lists available (use -i option)");
821 #endif /* USE_CODE_LISTS */
822 for (repeat = 0; repeat < r_opt; ++repeat) {
824 for (n = optind; n < argc; ++n) {
825 demo_termcap(argv[n]);
827 } else if ((name = getenv("TERM")) != 0) {
830 static char dumb[] = "dumb";
836 printf("%ld values (%ld booleans, %ld numbers, %ld strings)\n",
837 total_values, total_b_values, total_n_values, total_s_values);
841 ExitProgram(EXIT_SUCCESS);
846 main(int argc GCC_UNUSED,
847 char *argv[]GCC_UNUSED)
849 failed("This program requires termcap");