1 /****************************************************************************
2 * Copyright (c) 2009-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_terminfo.c,v 1.34 2014/08/31 23:11:39 tom Exp $
34 * A simple demo of the terminfo 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
64 static bool a_opt = FALSE;
65 static bool b_opt = FALSE;
66 static bool f_opt = FALSE;
67 static bool n_opt = FALSE;
68 static bool q_opt = FALSE;
69 static bool s_opt = FALSE;
70 static bool x_opt = FALSE;
71 static bool y_opt = FALSE;
75 static char **db_list;
79 static char **my_boolcodes;
80 static char **my_numcodes;
81 static char **my_numvalues;
82 static char **my_strcodes;
83 static char **my_strvalues;
85 static long total_values;
86 static long total_b_values;
87 static long total_n_values;
88 static long total_s_values;
91 #define FNAME(type) "%s %-*s = ", #type, FCOLS
94 make_dbitem(char *p, char *q)
96 char *result = malloc(strlen(e_opt) + 2 + (size_t) (p - q));
97 sprintf(result, "%s=%.*s", e_opt, (int) (p - q), q);
104 if (d_opt && e_opt) {
107 for (pass = 0; pass < 2; ++pass) {
111 for (p = q = d_opt; *p != '\0'; ++p) {
115 db_list[count] = make_dbitem(p, q);
124 db_list[count] = make_dbitem(p, q);
129 db_list = typeCalloc(char *, count + 1);
141 if ((result = db_list[db_item]) == 0) {
148 printf("** %s\n", result);
158 for (n = 0; db_list[n]; ++n)
167 dumpit(NCURSES_CONST char *cap)
172 if ((str = tigetstr(cap)) != 0 && (str != (char *) -1)) {
176 printf(FNAME(str), cap);
178 int ch = UChar(*str++);
184 fputs("\\E", stdout);
187 fputs("\\b", stdout);
190 fputs("\\f", stdout);
193 fputs("\\n", stdout);
196 fputs("\\r", stdout);
199 fputs("\\s", stdout);
202 fputs("\\t", stdout);
205 fputs("\\^", stdout);
208 fputs("\\072", stdout);
211 fputs("\\\\", stdout);
217 printf("^%c", ch + '@');
219 printf("\\%03o", ch);
225 } else if ((num = tigetnum(cap)) >= 0) {
229 printf(FNAME(num), cap);
230 printf(" %d\n", num);
232 } else if ((num = tigetflag(cap)) >= 0) {
236 printf(FNAME(flg), cap);
237 printf("%s\n", num ? "true" : "false");
245 #define isCapName(c) (isalnum(UChar(c)) || ((c) == '_'))
246 #define LegalItem(c,n) (n)
249 brute_force(const char *name)
251 #define MAX_FORCE 5 /* omit "colors", since CPU-time is a problem */
252 static const char legal[] = "\
254 ABCDEFGHIJKLMNOPQRSTUVWXYZ\
255 abcdefghijklmnopqrstuvwxyz_";
260 char cap[MAX_FORCE + 1];
261 int item[MAX_FORCE + 1];
264 putenv(next_dbitem());
267 printf("Terminal type \"%s\"\n", name);
268 setupterm((NCURSES_CONST char *) name, 1, (int *) 0);
270 for (length = 1; length <= MAX_FORCE; ++length) {
271 /* set all digits to zeros */
272 for (j = 0; j < length; ++j) {
273 item[j] = LegalItem(j, 0);
278 /* copy digits to cap-name */
279 for (j = 0; j < length; ++j) {
280 cap[j] = legal[item[j]];
288 for (; k >= 0; --k) {
290 if (legal[item[k]]) {
295 legal[item[k - 1] + 1]) {
296 for (j = k; j < length; ++j) {
297 item[j] = LegalItem(j, 0);
306 del_curterm(cur_term);
310 demo_terminfo(char *name)
313 NCURSES_CONST char *cap;
316 putenv(next_dbitem());
319 printf("Terminal type \"%s\"\n", name);
320 setupterm(name, 1, (int *) 0);
324 cap = f_opt ? boolfnames[n] : my_boolcodes[n];
333 cap = f_opt ? numfnames[n] : my_numcodes[n];
342 cap = f_opt ? strfnames[n] : my_strcodes[n];
348 #ifdef NCURSES_VERSION
349 if (x_opt && (my_blob == 0)) {
353 TERMTYPE *term = &(cur_term->type);
355 && ((NUM_BOOLEANS(term) != BOOLCOUNT)
356 || (NUM_NUMBERS(term) != NUMCOUNT)
357 || (NUM_STRINGS(term) != STRCOUNT))) {
358 for (n = BOOLCOUNT; n < NUM_BOOLEANS(term); ++n) {
359 dumpit(ExtBoolname(term, (int) n, boolnames));
361 for (n = NUMCOUNT; n < NUM_NUMBERS(term); ++n) {
362 dumpit(ExtNumname(term, (int) n, numnames));
364 for (n = STRCOUNT; n < NUM_STRINGS(term); ++n) {
365 dumpit(ExtStrname(term, (int) n, strnames));
371 static const char *xterm_keys[] =
373 "kDC", "kDN", "kEND", "kHOM", "kIC",
374 "kLFT", "kNXT", "kPRV", "kRIT", "kUP",
376 for (n = 0; n < SIZEOF(xterm_keys); ++n) {
377 for (mod = 0; mod < 8; ++mod) {
379 /* these happen to be standard - avoid duplicates */
380 if (!strcmp(xterm_keys[n], "kDC") ||
381 !strcmp(xterm_keys[n], "kEND") ||
382 !strcmp(xterm_keys[n], "kHOM") ||
383 !strcmp(xterm_keys[n], "kLFT") ||
384 !strcmp(xterm_keys[n], "kRIT")) {
387 sprintf(temp, "%.*s", 8, xterm_keys[n]);
389 sprintf(temp, "%.*s%d", 8, xterm_keys[n], mod);
397 del_curterm(cur_term);
412 parse_description(const char *input_name)
416 size_t count_bools = 0;
417 size_t count_nums = 0;
418 size_t count_strs = 0;
423 if (stat(input_name, &sb) != 0
424 || (sb.st_mode & S_IFMT) != S_IFREG) {
425 failed("input is not a file");
428 if (sb.st_size == 0) {
429 failed("input is empty");
433 * None of the arrays could be larger than the input-file, and since it
434 * is small, just allocate the maximum for simplicity.
436 if ((my_blob = malloc((size_t) sb.st_size + 1)) == 0 ||
437 (my_boolcodes = typeCalloc(char *, sb.st_size)) == 0 ||
438 (my_numcodes = typeCalloc(char *, sb.st_size)) == 0 ||
439 (my_numvalues = typeCalloc(char *, sb.st_size)) == 0 ||
440 (my_strcodes = typeCalloc(char *, sb.st_size)) == 0 ||
441 (my_strvalues = typeCalloc(char *, sb.st_size)) == 0) {
442 failed("cannot allocate memory for input-file");
445 if ((fp = fopen(input_name, "r")) == 0)
446 failed("cannot open input-file");
447 len = fread(my_blob, sizeof(char), (size_t) sb.st_size, fp);
451 * First, get rid of comments and escaped newlines, as well as repeated
452 * colons to construct a canonical entry.
455 for (j = k = 0; j < len; ++j) {
474 state = pDescription;
485 my_blob[k++] = (char) ch;
488 if (isalpha(UChar(ch)))
491 fprintf(stderr, "OOPS @%d:%.20s\n", __LINE__, my_blob + j);
492 my_blob[k++] = (char) ch;
512 my_blob[k++] = (char) ch;
534 state = pDescription;
537 my_blob[k++] = (char) ch;
551 my_blob[k++] = (char) ch;
560 my_blob[k++] = (char) ch;
563 my_blob[k++] = (char) ch;
575 * Then, parse what's left, making indexes of the names and values.
578 for (j = 0; my_blob[j] != '\0'; ++j) {
581 switch (my_blob[j]) {
587 if (my_blob[j + 1] != '\0' && my_blob[j + 1] != ',')
599 if (isalpha(UChar(my_blob[j]))) {
600 for (jl = 1; isalnum(UChar(my_blob[j + jl])); ++jl) {
607 switch (my_blob[j + jl]) {
609 my_numvalues[count_nums] = &my_blob[j + jl + 1];
610 my_numcodes[count_nums++] = &my_blob[j];
611 my_blob[j + jl] = '\0';
616 my_strvalues[count_strs] = &my_blob[j + jl + 1];
617 my_strcodes[count_strs++] = &my_blob[j];
618 my_blob[j + jl] = '\0';
623 if (my_blob[j + jl] == '@') {
625 * We cannot get the type for a cancelled item
626 * directly, but can infer it assuming the input
627 * came from infocmp, which puts the data in a
631 my_strvalues[count_strs] = "";
632 my_strcodes[count_strs++] = &my_blob[j];
633 } else if (count_nums) {
634 my_numvalues[count_nums] = "";
635 my_numcodes[count_nums++] = &my_blob[j];
637 my_boolcodes[count_bools++] = &my_blob[j];
639 my_blob[j + jl] = '\0';
642 my_boolcodes[count_bools++] = &my_blob[j];
643 my_blob[j + jl] = '\0';
646 state = (isCapName(my_blob[j + 1])
654 if (!isdigit(UChar(my_blob[j]))) {
660 switch (my_blob[j]) {
662 if (my_blob[j + 1] != '\0') {
682 my_boolcodes[count_bools] = 0;
683 my_numcodes[count_nums] = 0;
684 my_numvalues[count_nums] = 0;
685 my_strcodes[count_strs] = 0;
686 my_strvalues[count_strs] = 0;
689 printf("# bools:%d\n", (int) count_bools);
690 for (j = 0; my_boolcodes[j]; ++j)
691 printf("\t%s,\n", my_boolcodes[j]);
693 printf("# numbers:%d\n", (int) count_nums);
694 for (j = 0; my_numcodes[j]; ++j)
695 printf("\t%s#%s,\n", my_numcodes[j], my_numvalues[j]);
697 printf("# strings:%d\n", (int) count_strs);
698 for (j = 0; my_strcodes[j]; ++j)
699 printf("\t%s=%s,\n", my_strcodes[j], my_strvalues[j]);
705 copy_code_list(NCURSES_CONST char *const *list)
714 for (pass = 0; pass < 2; ++pass) {
715 for (count = 0; list[count] != 0; ++count) {
716 size_t chunk = strlen(list[count]) + 1;
720 result[count] = unused;
721 strcpy(unused, list[count]);
726 blob = malloc(length);
727 result = typeCalloc(char *, count + 1);
729 if (blob == 0 || result == 0)
730 failed("copy_code_list failed");
741 static const char *msg[] =
743 "Usage: demo_terminfo [options] [terminal]",
745 "If no options are given, print all (boolean, numeric, string)",
746 "capabilities for the given terminal, using short names.",
749 " -a try all names, print capabilities found",
750 " -b print boolean-capabilities",
751 " -d LIST colon-separated list of databases to use",
752 " -e NAME environment variable to set with -d option",
753 " -f print full names",
754 " -i NAME terminal description to use as names for \"-a\" option",
755 " -n print numeric-capabilities",
756 " -q quiet (prints only counts)",
757 " -r COUNT repeat for given count",
758 " -s print string-capabilities",
759 #ifdef NCURSES_VERSION
760 " -x print extended capabilities",
761 " -y direct-lookup names of extended capabilities",
765 for (n = 0; n < SIZEOF(msg); ++n) {
766 fprintf(stderr, "%s\n", msg[n]);
768 ExitProgram(EXIT_FAILURE);
772 main(int argc, char *argv[])
778 char *input_name = 0;
780 while ((n = getopt(argc, argv, "abd:e:fi:nqr:sxy")) != -1) {
807 if ((r_opt = atoi(optarg)) <= 0)
813 #ifdef NCURSES_VERSION
829 use_extended_names(x_opt);
832 if (!(b_opt || n_opt || s_opt)) {
841 for (repeat = 0; repeat < r_opt; ++repeat) {
843 for (n = optind; n < argc; ++n) {
844 brute_force(argv[n]);
846 } else if ((name = getenv("TERM")) != 0) {
849 static char dumb[] = "dumb";
854 if (input_name != 0) {
855 parse_description(input_name);
859 my_boolcodes = copy_code_list(boolnames);
860 my_numcodes = copy_code_list(numnames);
861 my_strcodes = copy_code_list(strnames);
865 failed("no capability-lists available (use -i option)");
867 #endif /* USE_CODE_LISTS */
868 for (repeat = 0; repeat < r_opt; ++repeat) {
870 for (n = optind; n < argc; ++n) {
871 demo_terminfo(argv[n]);
873 } else if ((name = getenv("TERM")) != 0) {
876 static char dumb[] = "dumb";
882 printf("%ld values (%ld booleans, %ld numbers, %ld strings)\n",
883 total_values, total_b_values, total_n_values, total_s_values);
897 ExitProgram(EXIT_SUCCESS);
900 #else /* !HAVE_TIGETSTR */
902 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
904 printf("This program requires the terminfo functions such as tigetstr\n");
905 ExitProgram(EXIT_FAILURE);
907 #endif /* HAVE_TIGETSTR */