1 /****************************************************************************
2 * Copyright (c) 2009 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.5 2009/07/11 17:39:41 tom Exp $
34 * A simple demo of the terminfo interface.
37 #include <test.priv.h>
39 #ifdef NCURSES_VERSION
41 #include <term_entry.h>
45 static bool b_opt = FALSE;
46 static bool f_opt = FALSE;
47 static bool n_opt = FALSE;
48 static bool s_opt = FALSE;
49 static bool x_opt = FALSE;
52 #define FNAME(type) "%s %-*s = ", #type, FCOLS
55 dumpit(NCURSES_CONST char *cap)
58 * One of the limitations of the termcap interface is that the library
59 * cannot determine the size of the buffer passed via tgetstr(), nor the
60 * amount of space remaining. This demo simply reuses the whole buffer
61 * for each call; a normal termcap application would try to use the buffer
62 * to hold all of the strings extracted from the terminal entry.
67 if ((str = tigetstr(cap)) != 0 && (str != (char *) -1)) {
69 * Note that the strings returned are mostly terminfo format, since
70 * ncurses does not convert except for a handful of special cases.
72 printf(FNAME(str), cap);
74 int ch = UChar(*str++);
101 fputs("\\^", stdout);
104 fputs("\\072", stdout);
107 fputs("\\\\", stdout);
113 printf("^%c", ch + '@');
115 printf("\\%03o", ch);
120 } else if ((num = tigetnum(cap)) >= 0) {
121 printf(FNAME(num), cap);
122 printf(" %d\n", num);
123 } else if ((num = tigetflag(cap)) >= 0) {
124 printf(FNAME(flg), cap);
125 printf("%s\n", num ? "true" : "false");
131 demo_terminfo(char *name)
134 NCURSES_CONST char *cap;
136 printf("Terminal type \"%s\"\n", name);
137 setupterm(name, 1, (int *) 0);
141 cap = f_opt ? boolfnames[n] : boolnames[n];
150 cap = f_opt ? numfnames[n] : numnames[n];
159 cap = f_opt ? strfnames[n] : strnames[n];
165 #ifdef NCURSES_VERSION
170 TERMTYPE *term = &(cur_term->type);
172 && ((NUM_BOOLEANS(term) != BOOLCOUNT)
173 || (NUM_NUMBERS(term) != NUMCOUNT)
174 || (NUM_STRINGS(term) != STRCOUNT))) {
175 for (n = BOOLCOUNT; n < NUM_BOOLEANS(term); ++n) {
176 dumpit(ExtBoolname(term, n, boolnames));
178 for (n = NUMCOUNT; n < NUM_NUMBERS(term); ++n) {
179 dumpit(ExtNumname(term, n, numnames));
181 for (n = STRCOUNT; n < NUM_STRINGS(term); ++n) {
182 dumpit(ExtStrname(term, n, strnames));
188 static const char *xterm_keys[] =
190 "kDC", "kDN", "kEND", "kHOM", "kIC",
191 "kLFT", "kNXT", "kPRV", "kRIT", "kUP",
193 for (n = 0; n < SIZEOF(xterm_keys); ++n) {
194 for (mod = 0; mod < 8; ++mod) {
196 strcpy(temp, xterm_keys[n]);
198 sprintf(temp, "%s%d", xterm_keys[n], mod);
211 static const char *msg[] =
213 "Usage: demo_terminfo [options] [terminal]",
215 "If no options are given, print all (boolean, numeric, string)",
216 "capabilities for the given terminal, using short names.",
219 " -b print boolean-capabilities",
220 " -f print full names",
221 " -n print numeric-capabilities",
222 " -r COUNT repeat for given count",
223 " -s print string-capabilities",
224 #ifdef NCURSES_VERSION
225 " -x print extended capabilities",
229 for (n = 0; n < SIZEOF(msg); ++n) {
230 fprintf(stderr, "%s\n", msg[n]);
232 ExitProgram(EXIT_FAILURE);
236 main(int argc, char *argv[])
243 while ((n = getopt(argc, argv, "bfnr:sx")) != -1) {
255 if ((r_opt = atoi(optarg)) <= 0)
261 #ifdef NCURSES_VERSION
264 use_extended_names(TRUE);
273 if (!(b_opt || n_opt || s_opt || x_opt)) {
279 for (repeat = 0; repeat < r_opt; ++repeat) {
281 for (n = optind; n < argc; ++n) {
282 demo_terminfo(argv[n]);
284 } else if ((name = getenv("TERM")) != 0) {
287 static char dumb[] = "dumb";
292 ExitProgram(EXIT_SUCCESS);