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.18 2013/09/07 17:37:20 tom Exp $
34 * A simple demo of the terminfo interface.
37 #include <test.priv.h>
40 #include <term_entry.h>
44 #if defined(HAVE_CURSES_DATA_BOOLNAMES) || defined(DECL_CURSES_DATA_BOOLNAMES)
46 static bool b_opt = FALSE;
47 static bool f_opt = FALSE;
48 static bool n_opt = FALSE;
49 static bool q_opt = FALSE;
50 static bool s_opt = FALSE;
51 static bool x_opt = FALSE;
55 static char **db_list;
58 static long total_values;
61 #define FNAME(type) "%s %-*s = ", #type, FCOLS
64 make_dbitem(char *p, char *q)
66 char *result = malloc(strlen(e_opt) + 2 + (size_t) (p - q));
67 sprintf(result, "%s=%.*s", e_opt, (int) (p - q), q);
77 for (pass = 0; pass < 2; ++pass) {
81 for (p = q = d_opt; *p != '\0'; ++p) {
85 db_list[count] = make_dbitem(p, q);
94 db_list[count] = make_dbitem(p, q);
99 db_list = typeCalloc(char *, count + 1);
111 if ((result = db_list[db_item]) == 0) {
118 printf("** %s\n", result);
127 for (n = 0; db_list[n]; ++n)
134 dumpit(NCURSES_CONST char *cap)
137 * One of the limitations of the termcap interface is that the library
138 * cannot determine the size of the buffer passed via tgetstr(), nor the
139 * amount of space remaining. This demo simply reuses the whole buffer
140 * for each call; a normal termcap application would try to use the buffer
141 * to hold all of the strings extracted from the terminal entry.
146 if ((str = tigetstr(cap)) != 0 && (str != (char *) -1)) {
150 * Note that the strings returned are mostly terminfo format, since
151 * ncurses does not convert except for a handful of special cases.
153 printf(FNAME(str), cap);
155 int ch = UChar(*str++);
161 fputs("\\E", stdout);
164 fputs("\\b", stdout);
167 fputs("\\f", stdout);
170 fputs("\\n", stdout);
173 fputs("\\r", stdout);
176 fputs("\\s", stdout);
179 fputs("\\t", stdout);
182 fputs("\\^", stdout);
185 fputs("\\072", stdout);
188 fputs("\\\\", stdout);
194 printf("^%c", ch + '@');
196 printf("\\%03o", ch);
202 } else if ((num = tigetnum(cap)) >= 0) {
205 printf(FNAME(num), cap);
206 printf(" %d\n", num);
208 } else if ((num = tigetflag(cap)) >= 0) {
211 printf(FNAME(flg), cap);
212 printf("%s\n", num ? "true" : "false");
221 demo_terminfo(char *name)
224 NCURSES_CONST char *cap;
227 putenv(next_dbitem());
229 printf("Terminal type \"%s\"\n", name);
230 setupterm(name, 1, (int *) 0);
234 cap = f_opt ? boolfnames[n] : boolnames[n];
243 cap = f_opt ? numfnames[n] : numnames[n];
252 cap = f_opt ? strfnames[n] : strnames[n];
258 #ifdef NCURSES_VERSION
263 TERMTYPE *term = &(cur_term->type);
265 && ((NUM_BOOLEANS(term) != BOOLCOUNT)
266 || (NUM_NUMBERS(term) != NUMCOUNT)
267 || (NUM_STRINGS(term) != STRCOUNT))) {
268 for (n = BOOLCOUNT; n < NUM_BOOLEANS(term); ++n) {
269 dumpit(ExtBoolname(term, (int) n, boolnames));
271 for (n = NUMCOUNT; n < NUM_NUMBERS(term); ++n) {
272 dumpit(ExtNumname(term, (int) n, numnames));
274 for (n = STRCOUNT; n < NUM_STRINGS(term); ++n) {
275 dumpit(ExtStrname(term, (int) n, strnames));
281 static const char *xterm_keys[] =
283 "kDC", "kDN", "kEND", "kHOM", "kIC",
284 "kLFT", "kNXT", "kPRV", "kRIT", "kUP",
286 for (n = 0; n < SIZEOF(xterm_keys); ++n) {
287 for (mod = 0; mod < 8; ++mod) {
289 sprintf(temp, "%.*s", 8, xterm_keys[n]);
291 sprintf(temp, "%.*s%d", 8, xterm_keys[n], mod);
304 static const char *msg[] =
306 "Usage: demo_terminfo [options] [terminal]",
308 "If no options are given, print all (boolean, numeric, string)",
309 "capabilities for the given terminal, using short names.",
312 " -b print boolean-capabilities",
313 " -d LIST colon-separated list of databases to use",
314 " -e NAME environment variable to set with -d option",
315 " -f print full names",
316 " -n print numeric-capabilities",
317 " -q quiet (prints only counts)",
318 " -r COUNT repeat for given count",
319 " -s print string-capabilities",
320 #ifdef NCURSES_VERSION
321 " -x print extended capabilities",
322 " -y disable extended capabilities",
326 for (n = 0; n < SIZEOF(msg); ++n) {
327 fprintf(stderr, "%s\n", msg[n]);
329 ExitProgram(EXIT_FAILURE);
333 main(int argc, char *argv[])
339 #ifdef NCURSES_VERSION
340 bool xy_opt = TRUE; /* by default, use_extended_names is true */
343 while ((n = getopt(argc, argv, "bd:e:fnqr:sxy")) != -1) {
364 if ((r_opt = atoi(optarg)) <= 0)
370 #ifdef NCURSES_VERSION
386 use_extended_names(xy_opt);
389 if (!(b_opt || n_opt || s_opt || x_opt)) {
397 for (repeat = 0; repeat < r_opt; ++repeat) {
399 for (n = optind; n < argc; ++n) {
400 demo_terminfo(argv[n]);
402 } else if ((name = getenv("TERM")) != 0) {
405 static char dumb[] = "dumb";
410 printf("%ld values\n", total_values);
414 ExitProgram(EXIT_SUCCESS);
419 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
421 printf("This program requires the terminfo arrays\n");
422 ExitProgram(EXIT_FAILURE);
425 #else /* !HAVE_TIGETSTR */
427 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
429 printf("This program requires the terminfo functions such as tigetstr\n");
430 ExitProgram(EXIT_FAILURE);
432 #endif /* HAVE_TIGETSTR */