1 /****************************************************************************
2 * Copyright (c) 2009,2010 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.9 2010/11/28 00:15:27 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 s_opt = FALSE;
56 static bool x_opt = FALSE;
59 #define FNAME(type) "%s %-*s = ", #type, FCOLS
62 dumpit(NCURSES_CONST char *cap)
65 * One of the limitations of the termcap interface is that the library
66 * cannot determine the size of the buffer passed via tgetstr(), nor the
67 * amount of space remaining. This demo simply reuses the whole buffer
68 * for each call; a normal termcap application would try to use the buffer
69 * to hold all of the strings extracted from the terminal entry.
74 if ((str = tigetstr(cap)) != 0 && (str != (char *) -1)) {
76 * Note that the strings returned are mostly terminfo format, since
77 * ncurses does not convert except for a handful of special cases.
79 printf(FNAME(str), cap);
81 int ch = UChar(*str++);
102 fputs("\\s", stdout);
105 fputs("\\t", stdout);
108 fputs("\\^", stdout);
111 fputs("\\072", stdout);
114 fputs("\\\\", stdout);
120 printf("^%c", ch + '@');
122 printf("\\%03o", ch);
127 } else if ((num = tigetnum(cap)) >= 0) {
128 printf(FNAME(num), cap);
129 printf(" %d\n", num);
130 } else if ((num = tigetflag(cap)) >= 0) {
131 printf(FNAME(flg), cap);
132 printf("%s\n", num ? "true" : "false");
138 demo_terminfo(char *name)
141 NCURSES_CONST char *cap;
143 printf("Terminal type \"%s\"\n", name);
144 setupterm(name, 1, (int *) 0);
148 cap = f_opt ? boolfnames[n] : boolnames[n];
157 cap = f_opt ? numfnames[n] : numnames[n];
166 cap = f_opt ? strfnames[n] : strnames[n];
172 #ifdef NCURSES_VERSION
177 TERMTYPE *term = &(cur_term->type);
179 && ((NUM_BOOLEANS(term) != BOOLCOUNT)
180 || (NUM_NUMBERS(term) != NUMCOUNT)
181 || (NUM_STRINGS(term) != STRCOUNT))) {
182 for (n = BOOLCOUNT; n < NUM_BOOLEANS(term); ++n) {
183 dumpit(ExtBoolname(term, (int) n, boolnames));
185 for (n = NUMCOUNT; n < NUM_NUMBERS(term); ++n) {
186 dumpit(ExtNumname(term, (int) n, numnames));
188 for (n = STRCOUNT; n < NUM_STRINGS(term); ++n) {
189 dumpit(ExtStrname(term, (int) n, strnames));
195 static const char *xterm_keys[] =
197 "kDC", "kDN", "kEND", "kHOM", "kIC",
198 "kLFT", "kNXT", "kPRV", "kRIT", "kUP",
200 for (n = 0; n < SIZEOF(xterm_keys); ++n) {
201 for (mod = 0; mod < 8; ++mod) {
203 strcpy(temp, xterm_keys[n]);
205 sprintf(temp, "%s%d", xterm_keys[n], mod);
218 static const char *msg[] =
220 "Usage: demo_terminfo [options] [terminal]",
222 "If no options are given, print all (boolean, numeric, string)",
223 "capabilities for the given terminal, using short names.",
226 " -b print boolean-capabilities",
227 " -f print full names",
228 " -n print numeric-capabilities",
229 " -r COUNT repeat for given count",
230 " -s print string-capabilities",
231 #ifdef NCURSES_VERSION
232 " -x print extended capabilities",
236 for (n = 0; n < SIZEOF(msg); ++n) {
237 fprintf(stderr, "%s\n", msg[n]);
239 ExitProgram(EXIT_FAILURE);
243 main(int argc, char *argv[])
250 while ((n = getopt(argc, argv, "bfnr:sx")) != -1) {
262 if ((r_opt = atoi(optarg)) <= 0)
268 #ifdef NCURSES_VERSION
271 use_extended_names(TRUE);
280 if (!(b_opt || n_opt || s_opt || x_opt)) {
286 for (repeat = 0; repeat < r_opt; ++repeat) {
288 for (n = optind; n < argc; ++n) {
289 demo_terminfo(argv[n]);
291 } else if ((name = getenv("TERM")) != 0) {
294 static char dumb[] = "dumb";
299 ExitProgram(EXIT_SUCCESS);
304 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
306 printf("This program requires the terminfo arrays\n");
307 ExitProgram(EXIT_FAILURE);
310 #else /* !HAVE_TIGETSTR */
312 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
314 printf("This program requires the terminfo functions such as tigetstr\n");
315 ExitProgram(EXIT_FAILURE);
317 #endif /* HAVE_TIGETSTR */