1 /****************************************************************************
2 * Copyright (c) 2009-2012,2013 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.16 2013/01/19 19:30:58 tom Exp $
34 * A simple demo of the terminfo interface.
37 #include <test.priv.h>
39 #ifdef NCURSES_VERSION
40 #if !(defined(HAVE_TERM_ENTRY_H) && HAVE_TERM_ENTRY_H)
42 #define NCURSES_XNAMES 0
45 #include <term_entry.h>
50 #if defined(HAVE_CURSES_DATA_BOOLNAMES) || defined(DECL_CURSES_DATA_BOOLNAMES)
52 static bool b_opt = FALSE;
53 static bool f_opt = FALSE;
54 static bool n_opt = FALSE;
55 static bool q_opt = FALSE;
56 static bool s_opt = FALSE;
57 static bool x_opt = FALSE;
61 static char **db_list;
64 static long total_values;
67 #define FNAME(type) "%s %-*s = ", #type, FCOLS
70 make_dbitem(char *p, char *q)
72 char *result = malloc(strlen(e_opt) + 2 + (size_t) (p - q));
73 sprintf(result, "%s=%.*s", e_opt, (int) (p - q), q);
83 for (pass = 0; pass < 2; ++pass) {
87 for (p = q = d_opt; *p != '\0'; ++p) {
91 db_list[count] = make_dbitem(p, q);
100 db_list[count] = make_dbitem(p, q);
105 db_list = typeCalloc(char *, count + 1);
117 if ((result = db_list[db_item]) == 0) {
124 printf("** %s\n", result);
133 for (n = 0; db_list[n]; ++n)
140 dumpit(NCURSES_CONST char *cap)
143 * One of the limitations of the termcap interface is that the library
144 * cannot determine the size of the buffer passed via tgetstr(), nor the
145 * amount of space remaining. This demo simply reuses the whole buffer
146 * for each call; a normal termcap application would try to use the buffer
147 * to hold all of the strings extracted from the terminal entry.
152 if ((str = tigetstr(cap)) != 0 && (str != (char *) -1)) {
156 * Note that the strings returned are mostly terminfo format, since
157 * ncurses does not convert except for a handful of special cases.
159 printf(FNAME(str), cap);
161 int ch = UChar(*str++);
167 fputs("\\E", stdout);
170 fputs("\\b", stdout);
173 fputs("\\f", stdout);
176 fputs("\\n", stdout);
179 fputs("\\r", stdout);
182 fputs("\\s", stdout);
185 fputs("\\t", stdout);
188 fputs("\\^", stdout);
191 fputs("\\072", stdout);
194 fputs("\\\\", stdout);
200 printf("^%c", ch + '@');
202 printf("\\%03o", ch);
208 } else if ((num = tigetnum(cap)) >= 0) {
211 printf(FNAME(num), cap);
212 printf(" %d\n", num);
214 } else if ((num = tigetflag(cap)) >= 0) {
217 printf(FNAME(flg), cap);
218 printf("%s\n", num ? "true" : "false");
227 demo_terminfo(char *name)
230 NCURSES_CONST char *cap;
233 putenv(next_dbitem());
235 printf("Terminal type \"%s\"\n", name);
236 setupterm(name, 1, (int *) 0);
240 cap = f_opt ? boolfnames[n] : boolnames[n];
249 cap = f_opt ? numfnames[n] : numnames[n];
258 cap = f_opt ? strfnames[n] : strnames[n];
264 #ifdef NCURSES_VERSION
269 TERMTYPE *term = &(cur_term->type);
271 && ((NUM_BOOLEANS(term) != BOOLCOUNT)
272 || (NUM_NUMBERS(term) != NUMCOUNT)
273 || (NUM_STRINGS(term) != STRCOUNT))) {
274 for (n = BOOLCOUNT; n < NUM_BOOLEANS(term); ++n) {
275 dumpit(ExtBoolname(term, (int) n, boolnames));
277 for (n = NUMCOUNT; n < NUM_NUMBERS(term); ++n) {
278 dumpit(ExtNumname(term, (int) n, numnames));
280 for (n = STRCOUNT; n < NUM_STRINGS(term); ++n) {
281 dumpit(ExtStrname(term, (int) n, strnames));
287 static const char *xterm_keys[] =
289 "kDC", "kDN", "kEND", "kHOM", "kIC",
290 "kLFT", "kNXT", "kPRV", "kRIT", "kUP",
292 for (n = 0; n < SIZEOF(xterm_keys); ++n) {
293 for (mod = 0; mod < 8; ++mod) {
295 sprintf(temp, "%.*s", 8, xterm_keys[n]);
297 sprintf(temp, "%.*s%d", 8, xterm_keys[n], mod);
310 static const char *msg[] =
312 "Usage: demo_terminfo [options] [terminal]",
314 "If no options are given, print all (boolean, numeric, string)",
315 "capabilities for the given terminal, using short names.",
318 " -b print boolean-capabilities",
319 " -d LIST colon-separated list of databases to use",
320 " -e NAME environment variable to set with -d option",
321 " -f print full names",
322 " -n print numeric-capabilities",
323 " -q quiet (prints only counts)",
324 " -r COUNT repeat for given count",
325 " -s print string-capabilities",
326 #ifdef NCURSES_VERSION
327 " -x print extended capabilities",
328 " -y disable extended capabilities",
332 for (n = 0; n < SIZEOF(msg); ++n) {
333 fprintf(stderr, "%s\n", msg[n]);
335 ExitProgram(EXIT_FAILURE);
339 main(int argc, char *argv[])
345 #ifdef NCURSES_VERSION
346 bool xy_opt = TRUE; /* by default, use_extended_names is true */
349 while ((n = getopt(argc, argv, "bd:e:fnqr:sxy")) != -1) {
370 if ((r_opt = atoi(optarg)) <= 0)
376 #ifdef NCURSES_VERSION
391 #ifdef NCURSES_VERSION
392 use_extended_names(xy_opt);
395 if (!(b_opt || n_opt || s_opt || x_opt)) {
403 for (repeat = 0; repeat < r_opt; ++repeat) {
405 for (n = optind; n < argc; ++n) {
406 demo_terminfo(argv[n]);
408 } else if ((name = getenv("TERM")) != 0) {
411 static char dumb[] = "dumb";
416 printf("%ld values\n", total_values);
420 ExitProgram(EXIT_SUCCESS);
425 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
427 printf("This program requires the terminfo arrays\n");
428 ExitProgram(EXIT_FAILURE);
431 #else /* !HAVE_TIGETSTR */
433 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
435 printf("This program requires the terminfo functions such as tigetstr\n");
436 ExitProgram(EXIT_FAILURE);
438 #endif /* HAVE_TIGETSTR */