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.43 2014/08/31 23:11: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
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)
374 size_t count_bools = 0;
375 size_t count_nums = 0;
376 size_t count_strs = 0;
381 if (stat(input_name, &sb) != 0
382 || (sb.st_mode & S_IFMT) != S_IFREG) {
383 failed("input is not a file");
386 if (sb.st_size == 0) {
387 failed("input is empty");
391 * None of the arrays could be larger than the input-file, and since it
392 * is small, just allocate the maximum for simplicity.
394 if ((my_blob = malloc((size_t) sb.st_size + 1)) == 0 ||
395 (my_boolcodes = typeCalloc(char *, sb.st_size)) == 0 ||
396 (my_numcodes = typeCalloc(char *, sb.st_size)) == 0 ||
397 (my_numvalues = typeCalloc(char *, sb.st_size)) == 0 ||
398 (my_strcodes = typeCalloc(char *, sb.st_size)) == 0 ||
399 (my_strvalues = typeCalloc(char *, sb.st_size)) == 0) {
400 failed("cannot allocate memory for input-file");
403 if ((fp = fopen(input_name, "r")) == 0)
404 failed("cannot open input-file");
405 len = fread(my_blob, sizeof(char), (size_t) sb.st_size, fp);
409 * First, get rid of comments and escaped newlines, as well as repeated
410 * colons to construct a canonical entry.
412 * FIXME: actually this should make an additional pass just to strip
413 * comment-lines and escaped newlines. But it is workable for infocmp
417 for (j = k = 0; j < len; ++j) {
436 state = pDescription;
451 my_blob[k++] = (char) ch;
469 my_blob[k++] = (char) ch;
488 state = pDescription;
491 my_blob[k++] = (char) ch;
505 my_blob[k++] = (char) ch;
510 if (my_blob[j + 1] == '\0') {
522 my_blob[k++] = (char) ch;
532 * Then, parse what's left, making indexes of the names and values.
535 for (j = 0; my_blob[j] != '\0'; ++j) {
538 switch (my_blob[j]) {
544 if (my_blob[j + 1] != '\0' && my_blob[j + 1] != ':')
557 * Commented-out capabilities might be accessible (they are in
560 if (my_blob[j] == '.' && my_blob[j + 1] == '.') {
563 if (my_blob[j + 1] != '\0') {
564 switch (my_blob[j + 2]) {
566 my_numvalues[count_nums] = &my_blob[j + 3];
567 my_numcodes[count_nums++] = &my_blob[j];
568 my_blob[j + 2] = '\0';
573 my_strvalues[count_strs] = &my_blob[j + 3];
574 my_strcodes[count_strs++] = &my_blob[j];
575 my_blob[j + 2] = '\0';
580 if (my_blob[j + 2] == '@') {
582 * We cannot get the type for a cancelled item
583 * directly, but can infer it assuming the input
584 * came from infocmp, which puts the data in a
588 my_strvalues[count_strs] = "";
589 my_strcodes[count_strs++] = &my_blob[j];
590 } else if (count_nums) {
591 my_numvalues[count_nums] = "";
592 my_numcodes[count_nums++] = &my_blob[j];
594 my_boolcodes[count_bools++] = &my_blob[j];
597 my_boolcodes[count_bools++] = &my_blob[j];
605 if (!isdigit(UChar(my_blob[j]))) {
611 switch (my_blob[j]) {
613 if (my_blob[j + 1] == '\0') {
636 my_boolcodes[count_bools] = 0;
637 my_numcodes[count_nums] = 0;
638 my_numvalues[count_nums] = 0;
639 my_strcodes[count_strs] = 0;
640 my_strvalues[count_strs] = 0;
643 printf("bools:%d\n", (int) count_bools);
644 for (j = 0; my_boolcodes[j]; ++j)
645 printf("%5d:%s\n", (int) j, my_boolcodes[j]);
647 printf("numbers:%d\n", (int) count_nums);
648 for (j = 0; my_numcodes[j]; ++j)
649 printf("%5d:%s(%s)\n", (int) j, my_numcodes[j], my_numvalues[j]);
651 printf("strings:%d\n", (int) count_strs);
652 for (j = 0; my_strcodes[j]; ++j)
653 printf("%5d:%s(%s)\n", (int) j, my_strcodes[j], my_strvalues[j]);
659 copy_code_list(NCURSES_CONST char *const *list)
668 for (pass = 0; pass < 2; ++pass) {
669 for (count = 0; list[count] != 0; ++count) {
670 size_t chunk = strlen(list[count]) + 1;
674 result[count] = unused;
675 strcpy(unused, list[count]);
680 blob = malloc(length);
681 result = typeCalloc(char *, count + 1);
683 if (blob == 0 || result == 0)
684 failed("copy_code_list failed");
695 static const char *msg[] =
697 "Usage: demo_termcap [options] [terminal]",
699 "If no options are given, print all (boolean, numeric, string)",
700 "capabilities for the given terminal, using short names.",
703 " -a try all names, print capabilities found",
704 " -b print boolean-capabilities",
705 " -d LIST colon-separated list of databases to use",
706 " -e NAME environment variable to set with -d option",
707 " -i NAME terminal description to use as names for \"-a\" option, etc.",
708 " -n print numeric-capabilities",
709 " -q quiet (prints only counts)",
710 " -r COUNT repeat for given count",
711 " -s print string-capabilities",
712 #ifdef NCURSES_VERSION
713 " -x print extended capabilities",
717 for (n = 0; n < SIZEOF(msg); ++n) {
718 fprintf(stderr, "%s\n", msg[n]);
720 ExitProgram(EXIT_FAILURE);
724 main(int argc, char *argv[])
729 char *input_name = 0;
734 while ((n = getopt(argc, argv, "abd:e:i:nqr:sxy")) != -1) {
758 if ((r_opt = atoi(optarg)) <= 0)
780 use_extended_names(x_opt);
783 if (!(b_opt || n_opt || s_opt)) {
792 for (repeat = 0; repeat < r_opt; ++repeat) {
794 for (n = optind; n < argc; ++n) {
795 brute_force(argv[n]);
797 } else if ((name = getenv("TERM")) != 0) {
800 static char dumb[] = "dumb";
805 if (input_name != 0) {
806 parse_description(input_name);
810 my_boolcodes = copy_code_list(boolcodes);
811 my_numcodes = copy_code_list(numcodes);
812 my_strcodes = copy_code_list(strcodes);
816 failed("no capability-lists available (use -i option)");
818 #endif /* USE_CODE_LISTS */
819 for (repeat = 0; repeat < r_opt; ++repeat) {
821 for (n = optind; n < argc; ++n) {
822 demo_termcap(argv[n]);
824 } else if ((name = getenv("TERM")) != 0) {
827 static char dumb[] = "dumb";
833 printf("%ld values (%ld booleans, %ld numbers, %ld strings)\n",
834 total_values, total_b_values, total_n_values, total_s_values);
838 ExitProgram(EXIT_SUCCESS);
843 main(int argc GCC_UNUSED,
844 char *argv[]GCC_UNUSED)
846 failed("This program requires termcap");