1 /****************************************************************************
2 * Copyright (c) 2005-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_termcap.c,v 1.13 2010/05/01 22:04:08 tom Exp $
34 * A simple demo of the termcap interface.
37 #include <test.priv.h>
41 #if defined(HAVE_CURSES_DATA_BOOLNAMES) || defined(DECL_CURSES_DATA_BOOLNAMES)
42 #define USE_CODE_LISTS 1
44 #define USE_CODE_LISTS 0
48 #define FNAME(type) "%s %-*s = ", #type, FCOLS
51 static bool b_opt = FALSE;
52 static bool n_opt = FALSE;
53 static bool s_opt = FALSE;
56 #define isCapName(c) (isgraph(c) && strchr("^#=:\\", c) == 0)
59 dumpit(NCURSES_CONST char *cap)
62 * One of the limitations of the termcap interface is that the library
63 * cannot determine the size of the buffer passed via tgetstr(), nor the
64 * amount of space remaining. This demo simply reuses the whole buffer
65 * for each call; a normal termcap application would try to use the buffer
66 * to hold all of the strings extracted from the terminal entry.
68 char area[1024], *ap = area;
72 if ((str = tgetstr(cap, &ap)) != 0) {
74 * Note that the strings returned are mostly terminfo format, since
75 * ncurses does not convert except for a handful of special cases.
77 printf(FNAME(str), cap);
79 int ch = UChar(*str++);
100 fputs("\\s", stdout);
103 fputs("\\t", stdout);
106 fputs("\\^", stdout);
109 fputs("\\072", stdout);
112 fputs("\\\\", stdout);
118 printf("^%c", ch + '@');
120 printf("\\%03o", ch);
125 } else if ((num = tgetnum(cap)) >= 0) {
126 printf(FNAME(num), cap);
127 printf(" %d\n", num);
128 } else if (tgetflag(cap) > 0) {
129 printf(FNAME(flg), cap);
130 printf("%s\n", "true");
136 brute_force(const char *name)
140 printf("Terminal type %s\n", name);
141 if (tgetent(buffer, name) >= 0) {
146 for (c1 = 0; c1 < 256; ++c1) {
149 for (c2 = 0; c2 < 256; ++c2) {
162 demo_terminfo(NCURSES_CONST char *name)
165 NCURSES_CONST char *cap;
167 printf("Terminal type \"%s\"\n", name);
168 setupterm(name, 1, (int *) 0);
201 static const char *msg[] =
203 "Usage: demo_terminfo [options] [terminal]",
205 "If no options are given, print all (boolean, numeric, string)",
206 "capabilities for the given terminal, using short names.",
209 " -a try all names, print capabilities found",
210 " -b print boolean-capabilities",
211 " -n print numeric-capabilities",
212 " -r COUNT repeat for given count",
213 " -s print string-capabilities",
216 for (n = 0; n < SIZEOF(msg); ++n) {
217 fprintf(stderr, "%s\n", msg[n]);
219 ExitProgram(EXIT_FAILURE);
224 main(int argc, char *argv[])
234 while ((n = getopt(argc, argv, "abnr:s")) != -1) {
246 if ((r_opt = atoi(optarg)) <= 0)
258 if (!(b_opt || n_opt || s_opt)) {
269 for (n = optind; n < argc; ++n) {
270 brute_force(argv[n]);
272 } else if ((name = getenv("TERM")) != 0) {
275 static char dumb[] = "dumb";
281 for (repeat = 0; repeat < r_opt; ++repeat) {
283 for (n = optind; n < argc; ++n) {
284 demo_terminfo(argv[n]);
286 } else if ((name = getenv("TERM")) != 0) {
289 static char dumb[] = "dumb";
296 ExitProgram(EXIT_SUCCESS);
301 main(int argc GCC_UNUSED,
302 char *argv[]GCC_UNUSED)
304 printf("This program requires termcap\n");