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.38 2014/10/10 00:35:28 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 #define fullname(type,n) f_opt ? type##fnames[n] : my_##type##codes[n]
312 #define fullname(type,n) my_##type##codes[n]
316 demo_terminfo(char *name)
319 NCURSES_CONST char *cap;
322 putenv(next_dbitem());
325 printf("Terminal type \"%s\"\n", name);
326 setupterm(name, 1, (int *) 0);
330 cap = fullname(bool, n);
339 cap = fullname(num, n);
348 cap = fullname(str, n);
354 #ifdef NCURSES_VERSION
355 if (x_opt && (my_blob == 0)) {
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 dumpit(ExtBoolname(term, (int) n, boolnames));
367 for (n = NUMCOUNT; n < NUM_NUMBERS(term); ++n) {
368 dumpit(ExtNumname(term, (int) n, numnames));
370 for (n = STRCOUNT; n < NUM_STRINGS(term); ++n) {
371 dumpit(ExtStrname(term, (int) n, strnames));
377 static const char *xterm_keys[] =
379 "kDC", "kDN", "kEND", "kHOM", "kIC",
380 "kLFT", "kNXT", "kPRV", "kRIT", "kUP",
382 for (n = 0; n < SIZEOF(xterm_keys); ++n) {
383 for (mod = 0; mod < 8; ++mod) {
385 /* these happen to be standard - avoid duplicates */
386 if (!strcmp(xterm_keys[n], "kDC") ||
387 !strcmp(xterm_keys[n], "kEND") ||
388 !strcmp(xterm_keys[n], "kHOM") ||
389 !strcmp(xterm_keys[n], "kLFT") ||
390 !strcmp(xterm_keys[n], "kRIT")) {
393 sprintf(temp, "%.*s", 8, xterm_keys[n]);
395 sprintf(temp, "%.*s%d", 8, xterm_keys[n], mod);
403 del_curterm(cur_term);
418 parse_description(const char *input_name)
420 static char empty[1];
424 size_t count_bools = 0;
425 size_t count_nums = 0;
426 size_t count_strs = 0;
431 if (stat(input_name, &sb) != 0
432 || (sb.st_mode & S_IFMT) != S_IFREG) {
433 failed("input is not a file");
436 if (sb.st_size == 0) {
437 failed("input is empty");
441 * None of the arrays could be larger than the input-file, and since it
442 * is small, just allocate the maximum for simplicity.
444 if ((my_blob = malloc((size_t) sb.st_size + 1)) == 0 ||
445 (my_boolcodes = typeCalloc(char *, sb.st_size)) == 0 ||
446 (my_numcodes = typeCalloc(char *, sb.st_size)) == 0 ||
447 (my_numvalues = typeCalloc(char *, sb.st_size)) == 0 ||
448 (my_strcodes = typeCalloc(char *, sb.st_size)) == 0 ||
449 (my_strvalues = typeCalloc(char *, sb.st_size)) == 0) {
450 failed("cannot allocate memory for input-file");
453 if ((fp = fopen(input_name, "r")) == 0)
454 failed("cannot open input-file");
455 len = fread(my_blob, sizeof(char), (size_t) sb.st_size, fp);
456 my_blob[sb.st_size] = '\0';
460 * First, get rid of comments and escaped newlines, as well as repeated
461 * colons to construct a canonical entry.
464 for (j = k = 0; j < len; ++j) {
483 state = pDescription;
494 my_blob[k++] = (char) ch;
497 if (isalpha(UChar(ch)))
500 fprintf(stderr, "OOPS @%d:%.20s\n", __LINE__, my_blob + j);
501 my_blob[k++] = (char) ch;
521 my_blob[k++] = (char) ch;
543 state = pDescription;
546 my_blob[k++] = (char) ch;
560 my_blob[k++] = (char) ch;
569 my_blob[k++] = (char) ch;
572 my_blob[k++] = (char) ch;
584 * Then, parse what's left, making indexes of the names and values.
587 for (j = 0; my_blob[j] != '\0'; ++j) {
590 switch (my_blob[j]) {
596 if (my_blob[j + 1] != '\0' && my_blob[j + 1] != ',')
608 if (isalpha(UChar(my_blob[j]))) {
609 for (jl = 1; isalnum(UChar(my_blob[j + jl])); ++jl) {
616 switch (my_blob[j + jl]) {
618 my_numvalues[count_nums] = &my_blob[j + jl + 1];
619 my_numcodes[count_nums++] = &my_blob[j];
620 my_blob[j + jl] = '\0';
625 my_strvalues[count_strs] = &my_blob[j + jl + 1];
626 my_strcodes[count_strs++] = &my_blob[j];
627 my_blob[j + jl] = '\0';
632 if (my_blob[j + jl] == '@') {
634 * We cannot get the type for a cancelled item
635 * directly, but can infer it assuming the input
636 * came from infocmp, which puts the data in a
640 my_strvalues[count_strs] = empty;
641 my_strcodes[count_strs++] = &my_blob[j];
642 } else if (count_nums) {
643 my_numvalues[count_nums] = empty;
644 my_numcodes[count_nums++] = &my_blob[j];
646 my_boolcodes[count_bools++] = &my_blob[j];
648 my_blob[j + jl] = '\0';
651 my_boolcodes[count_bools++] = &my_blob[j];
652 my_blob[j + jl] = '\0';
655 state = (isCapName(my_blob[j + 1])
663 if (!isdigit(UChar(my_blob[j]))) {
669 switch (my_blob[j]) {
671 if (my_blob[j + 1] != '\0') {
691 my_boolcodes[count_bools] = 0;
692 my_numcodes[count_nums] = 0;
693 my_numvalues[count_nums] = 0;
694 my_strcodes[count_strs] = 0;
695 my_strvalues[count_strs] = 0;
698 printf("# bools:%d\n", (int) count_bools);
699 for (j = 0; my_boolcodes[j]; ++j)
700 printf("\t%s,\n", my_boolcodes[j]);
702 printf("# numbers:%d\n", (int) count_nums);
703 for (j = 0; my_numcodes[j]; ++j)
704 printf("\t%s#%s,\n", my_numcodes[j], my_numvalues[j]);
706 printf("# strings:%d\n", (int) count_strs);
707 for (j = 0; my_strcodes[j]; ++j)
708 printf("\t%s=%s,\n", my_strcodes[j], my_strvalues[j]);
714 copy_code_list(NCURSES_CONST char *const *list)
723 for (pass = 0; pass < 2; ++pass) {
724 for (count = 0; list[count] != 0; ++count) {
725 size_t chunk = strlen(list[count]) + 1;
729 result[count] = unused;
730 strcpy(unused, list[count]);
735 blob = malloc(length);
736 result = typeCalloc(char *, count + 1);
738 if (blob == 0 || result == 0)
739 failed("copy_code_list failed");
750 static const char *msg[] =
752 "Usage: demo_terminfo [options] [terminal]",
754 "If no options are given, print all (boolean, numeric, string)",
755 "capabilities for the given terminal, using short names.",
758 " -a try all names, print capabilities found",
759 " -b print boolean-capabilities",
760 " -d LIST colon-separated list of databases to use",
761 " -e NAME environment variable to set with -d option",
762 " -f print full names",
763 " -i NAME terminal description to use as names for \"-a\" option",
764 " -n print numeric-capabilities",
765 " -q quiet (prints only counts)",
766 " -r COUNT repeat for given count",
767 " -s print string-capabilities",
768 #ifdef NCURSES_VERSION
769 " -x print extended capabilities",
770 " -y direct-lookup names of extended capabilities",
774 for (n = 0; n < SIZEOF(msg); ++n) {
775 fprintf(stderr, "%s\n", msg[n]);
777 ExitProgram(EXIT_FAILURE);
781 main(int argc, char *argv[])
787 char *input_name = 0;
789 while ((n = getopt(argc, argv, "abd:e:fi:nqr:sxy")) != -1) {
816 if ((r_opt = atoi(optarg)) <= 0)
822 #ifdef NCURSES_VERSION
837 #if HAVE_USE_EXTENDED_NAMES
838 use_extended_names(x_opt);
841 if (!(b_opt || n_opt || s_opt)) {
850 for (repeat = 0; repeat < r_opt; ++repeat) {
852 for (n = optind; n < argc; ++n) {
853 brute_force(argv[n]);
855 } else if ((name = getenv("TERM")) != 0) {
858 static char dumb[] = "dumb";
863 if (input_name != 0) {
864 parse_description(input_name);
868 my_boolcodes = copy_code_list(boolnames);
869 my_numcodes = copy_code_list(numnames);
870 my_strcodes = copy_code_list(strnames);
874 failed("no capability-lists available (use -i option)");
876 #endif /* USE_CODE_LISTS */
877 for (repeat = 0; repeat < r_opt; ++repeat) {
879 for (n = optind; n < argc; ++n) {
880 demo_terminfo(argv[n]);
882 } else if ((name = getenv("TERM")) != 0) {
885 static char dumb[] = "dumb";
891 printf("%ld values (%ld booleans, %ld numbers, %ld strings)\n",
892 total_values, total_b_values, total_n_values, total_s_values);
906 ExitProgram(EXIT_SUCCESS);
909 #else /* !HAVE_TIGETSTR */
911 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
913 printf("This program requires the terminfo functions such as tigetstr\n");
914 ExitProgram(EXIT_FAILURE);
916 #endif /* HAVE_TIGETSTR */