1 /****************************************************************************
2 * Copyright (c) 2009-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_terminfo.c,v 1.39 2015/07/10 23:45:44 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 if (strcmp(name
, ttytype
))
271 printf("... actual \"%s\"\n", ttytype
);
274 for (length
= 1; length
<= MAX_FORCE
; ++length
) {
275 /* set all digits to zeros */
276 for (j
= 0; j
< length
; ++j
) {
277 item
[j
] = LegalItem(j
, 0);
282 /* copy digits to cap-name */
283 for (j
= 0; j
< length
; ++j
) {
284 cap
[j
] = legal
[item
[j
]];
292 for (; k
>= 0; --k
) {
294 if (legal
[item
[k
]]) {
299 legal
[item
[k
- 1] + 1]) {
300 for (j
= k
; j
< length
; ++j
) {
301 item
[j
] = LegalItem(j
, 0);
310 del_curterm(cur_term
);
314 #define fullname(type,n) f_opt ? type##fnames[n] : my_##type##codes[n]
316 #define fullname(type,n) my_##type##codes[n]
320 demo_terminfo(char *name
)
323 NCURSES_CONST
char *cap
;
326 putenv(next_dbitem());
329 printf("Terminal type \"%s\"\n", name
);
330 setupterm(name
, 1, (int *) 0);
334 cap
= fullname(bool, n
);
343 cap
= fullname(num
, n
);
352 cap
= fullname(str
, n
);
358 #ifdef NCURSES_VERSION
359 if (x_opt
&& (my_blob
== 0)) {
363 TERMTYPE
*term
= &(cur_term
->type
);
365 && ((NUM_BOOLEANS(term
) != BOOLCOUNT
)
366 || (NUM_NUMBERS(term
) != NUMCOUNT
)
367 || (NUM_STRINGS(term
) != STRCOUNT
))) {
368 for (n
= BOOLCOUNT
; n
< NUM_BOOLEANS(term
); ++n
) {
369 dumpit(ExtBoolname(term
, (int) n
, boolnames
));
371 for (n
= NUMCOUNT
; n
< NUM_NUMBERS(term
); ++n
) {
372 dumpit(ExtNumname(term
, (int) n
, numnames
));
374 for (n
= STRCOUNT
; n
< NUM_STRINGS(term
); ++n
) {
375 dumpit(ExtStrname(term
, (int) n
, strnames
));
381 static const char *xterm_keys
[] =
383 "kDC", "kDN", "kEND", "kHOM", "kIC",
384 "kLFT", "kNXT", "kPRV", "kRIT", "kUP",
386 for (n
= 0; n
< SIZEOF(xterm_keys
); ++n
) {
387 for (mod
= 0; mod
< 8; ++mod
) {
389 /* these happen to be standard - avoid duplicates */
390 if (!strcmp(xterm_keys
[n
], "kDC") ||
391 !strcmp(xterm_keys
[n
], "kEND") ||
392 !strcmp(xterm_keys
[n
], "kHOM") ||
393 !strcmp(xterm_keys
[n
], "kLFT") ||
394 !strcmp(xterm_keys
[n
], "kRIT")) {
397 sprintf(temp
, "%.*s", 8, xterm_keys
[n
]);
399 sprintf(temp
, "%.*s%d", 8, xterm_keys
[n
], mod
);
407 del_curterm(cur_term
);
422 parse_description(const char *input_name
)
424 static char empty
[1];
428 size_t count_bools
= 0;
429 size_t count_nums
= 0;
430 size_t count_strs
= 0;
435 if (stat(input_name
, &sb
) != 0
436 || (sb
.st_mode
& S_IFMT
) != S_IFREG
) {
437 failed("input is not a file");
440 if (sb
.st_size
== 0) {
441 failed("input is empty");
445 * None of the arrays could be larger than the input-file, and since it
446 * is small, just allocate the maximum for simplicity.
448 if ((my_blob
= malloc((size_t) sb
.st_size
+ 1)) == 0 ||
449 (my_boolcodes
= typeCalloc(char *, sb
.st_size
)) == 0 ||
450 (my_numcodes
= typeCalloc(char *, sb
.st_size
)) == 0 ||
451 (my_numvalues
= typeCalloc(char *, sb
.st_size
)) == 0 ||
452 (my_strcodes
= typeCalloc(char *, sb
.st_size
)) == 0 ||
453 (my_strvalues
= typeCalloc(char *, sb
.st_size
)) == 0) {
454 failed("cannot allocate memory for input-file");
457 if ((fp
= fopen(input_name
, "r")) == 0)
458 failed("cannot open input-file");
459 len
= fread(my_blob
, sizeof(char), (size_t) sb
.st_size
, fp
);
460 my_blob
[sb
.st_size
] = '\0';
464 * First, get rid of comments and escaped newlines, as well as repeated
465 * colons to construct a canonical entry.
468 for (j
= k
= 0; j
< len
; ++j
) {
487 state
= pDescription
;
498 my_blob
[k
++] = (char) ch
;
501 if (isalpha(UChar(ch
)))
504 fprintf(stderr
, "OOPS @%d:%.20s\n", __LINE__
, my_blob
+ j
);
505 my_blob
[k
++] = (char) ch
;
525 my_blob
[k
++] = (char) ch
;
547 state
= pDescription
;
550 my_blob
[k
++] = (char) ch
;
564 my_blob
[k
++] = (char) ch
;
573 my_blob
[k
++] = (char) ch
;
576 my_blob
[k
++] = (char) ch
;
588 * Then, parse what's left, making indexes of the names and values.
591 for (j
= 0; my_blob
[j
] != '\0'; ++j
) {
594 switch (my_blob
[j
]) {
600 if (my_blob
[j
+ 1] != '\0' && my_blob
[j
+ 1] != ',')
612 if (isalpha(UChar(my_blob
[j
]))) {
613 for (jl
= 1; isalnum(UChar(my_blob
[j
+ jl
])); ++jl
) {
620 switch (my_blob
[j
+ jl
]) {
622 my_numvalues
[count_nums
] = &my_blob
[j
+ jl
+ 1];
623 my_numcodes
[count_nums
++] = &my_blob
[j
];
624 my_blob
[j
+ jl
] = '\0';
629 my_strvalues
[count_strs
] = &my_blob
[j
+ jl
+ 1];
630 my_strcodes
[count_strs
++] = &my_blob
[j
];
631 my_blob
[j
+ jl
] = '\0';
636 if (my_blob
[j
+ jl
] == '@') {
638 * We cannot get the type for a cancelled item
639 * directly, but can infer it assuming the input
640 * came from infocmp, which puts the data in a
644 my_strvalues
[count_strs
] = empty
;
645 my_strcodes
[count_strs
++] = &my_blob
[j
];
646 } else if (count_nums
) {
647 my_numvalues
[count_nums
] = empty
;
648 my_numcodes
[count_nums
++] = &my_blob
[j
];
650 my_boolcodes
[count_bools
++] = &my_blob
[j
];
652 my_blob
[j
+ jl
] = '\0';
655 my_boolcodes
[count_bools
++] = &my_blob
[j
];
656 my_blob
[j
+ jl
] = '\0';
659 state
= (isCapName(my_blob
[j
+ 1])
667 if (!isdigit(UChar(my_blob
[j
]))) {
673 switch (my_blob
[j
]) {
675 if (my_blob
[j
+ 1] != '\0') {
695 my_boolcodes
[count_bools
] = 0;
696 my_numcodes
[count_nums
] = 0;
697 my_numvalues
[count_nums
] = 0;
698 my_strcodes
[count_strs
] = 0;
699 my_strvalues
[count_strs
] = 0;
702 printf("# bools:%d\n", (int) count_bools
);
703 for (j
= 0; my_boolcodes
[j
]; ++j
)
704 printf("\t%s,\n", my_boolcodes
[j
]);
706 printf("# numbers:%d\n", (int) count_nums
);
707 for (j
= 0; my_numcodes
[j
]; ++j
)
708 printf("\t%s#%s,\n", my_numcodes
[j
], my_numvalues
[j
]);
710 printf("# strings:%d\n", (int) count_strs
);
711 for (j
= 0; my_strcodes
[j
]; ++j
)
712 printf("\t%s=%s,\n", my_strcodes
[j
], my_strvalues
[j
]);
718 copy_code_list(NCURSES_CONST
char *const *list
)
727 for (pass
= 0; pass
< 2; ++pass
) {
728 for (count
= 0; list
[count
] != 0; ++count
) {
729 size_t chunk
= strlen(list
[count
]) + 1;
733 result
[count
] = unused
;
734 strcpy(unused
, list
[count
]);
739 blob
= malloc(length
);
740 result
= typeCalloc(char *, count
+ 1);
742 if (blob
== 0 || result
== 0)
743 failed("copy_code_list failed");
754 static const char *msg
[] =
756 "Usage: demo_terminfo [options] [terminal]",
758 "If no options are given, print all (boolean, numeric, string)",
759 "capabilities for the given terminal, using short names.",
762 " -a try all names, print capabilities found",
763 " -b print boolean-capabilities",
764 " -d LIST colon-separated list of databases to use",
765 " -e NAME environment variable to set with -d option",
766 " -f print full names",
767 " -i NAME terminal description to use as names for \"-a\" option",
768 " -n print numeric-capabilities",
769 " -q quiet (prints only counts)",
770 " -r COUNT repeat for given count",
771 " -s print string-capabilities",
772 #ifdef NCURSES_VERSION
773 " -x print extended capabilities",
774 " -y direct-lookup names of extended capabilities",
778 for (n
= 0; n
< SIZEOF(msg
); ++n
) {
779 fprintf(stderr
, "%s\n", msg
[n
]);
781 ExitProgram(EXIT_FAILURE
);
785 main(int argc
, char *argv
[])
791 char *input_name
= 0;
793 while ((n
= getopt(argc
, argv
, "abd:e:fi:nqr:sxy")) != -1) {
820 if ((r_opt
= atoi(optarg
)) <= 0)
826 #ifdef NCURSES_VERSION
841 #if HAVE_USE_EXTENDED_NAMES
842 use_extended_names(x_opt
);
845 if (!(b_opt
|| n_opt
|| s_opt
)) {
854 for (repeat
= 0; repeat
< r_opt
; ++repeat
) {
856 for (n
= optind
; n
< argc
; ++n
) {
857 brute_force(argv
[n
]);
859 } else if ((name
= getenv("TERM")) != 0) {
862 static char dumb
[] = "dumb";
867 if (input_name
!= 0) {
868 parse_description(input_name
);
872 my_boolcodes
= copy_code_list(boolnames
);
873 my_numcodes
= copy_code_list(numnames
);
874 my_strcodes
= copy_code_list(strnames
);
878 failed("no capability-lists available (use -i option)");
880 #endif /* USE_CODE_LISTS */
881 for (repeat
= 0; repeat
< r_opt
; ++repeat
) {
883 for (n
= optind
; n
< argc
; ++n
) {
884 demo_terminfo(argv
[n
]);
886 } else if ((name
= getenv("TERM")) != 0) {
889 static char dumb
[] = "dumb";
895 printf("%ld values (%ld booleans, %ld numbers, %ld strings)\n",
896 total_values
, total_b_values
, total_n_values
, total_s_values
);
910 ExitProgram(EXIT_SUCCESS
);
913 #else /* !HAVE_TIGETSTR */
915 main(int argc GCC_UNUSED
, char *argv
[]GCC_UNUSED
)
917 printf("This program requires the terminfo functions such as tigetstr\n");
918 ExitProgram(EXIT_FAILURE
);
920 #endif /* HAVE_TIGETSTR */