1 /****************************************************************************
2 * Copyright (c) 2005-2013,2014 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.26 2014/07/19 22:49:52 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 static bool q_opt = FALSE;
60 static char **db_list;
63 static long total_values;
65 #define isCapName(c) (isgraph(c) && strchr("^#=:\\", c) == 0)
68 make_dbitem(char *p, char *q)
70 char *result = malloc(strlen(e_opt) + 2 + (size_t) (p - q));
71 sprintf(result, "%s=%.*s", e_opt, (int) (p - q), q);
81 for (pass = 0; pass < 2; ++pass) {
85 for (p = q = d_opt; *p != '\0'; ++p) {
89 db_list[count] = make_dbitem(p, q);
98 db_list[count] = make_dbitem(p, q);
103 db_list = typeCalloc(char *, count + 1);
115 if ((result = db_list[db_item]) == 0) {
122 printf("** %s\n", result);
131 for (n = 0; db_list[n]; ++n)
139 dumpit(NCURSES_CONST char *cap)
142 * One of the limitations of the termcap interface is that the library
143 * cannot determine the size of the buffer passed via tgetstr(), nor the
144 * amount of space remaining. This demo simply reuses the whole buffer
145 * for each call; a normal termcap application would try to use the buffer
146 * to hold all of the strings extracted from the terminal entry.
148 char area[1024], *ap = area;
152 if ((str = tgetstr(cap, &ap)) != 0) {
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 = tgetnum(cap)) >= 0) {
211 printf(FNAME(num), cap);
212 printf(" %d\n", num);
214 } else if (tgetflag(cap) > 0) {
217 printf(FNAME(flg), cap);
218 printf("%s\n", "true");
227 brute_force(const char *name)
232 putenv(next_dbitem());
234 printf("Terminal type %s\n", name);
235 if (tgetent(buffer, name) >= 0) {
240 for (c1 = 0; c1 < 256; ++c1) {
243 for (c2 = 0; c2 < 256; ++c2) {
256 demo_termcap(NCURSES_CONST char *name)
259 NCURSES_CONST char *cap;
263 putenv(next_dbitem());
265 printf("Terminal type \"%s\"\n", name);
266 if (tgetent(buffer, name) >= 0) {
300 static const char *msg[] =
302 "Usage: demo_termcap [options] [terminal]",
304 "If no options are given, print all (boolean, numeric, string)",
305 "capabilities for the given terminal, using short names.",
308 " -a try all names, print capabilities found",
309 " -b print boolean-capabilities",
310 " -d LIST colon-separated list of databases to use",
311 " -e NAME environment variable to set with -d option",
312 " -n print numeric-capabilities",
313 " -q quiet (prints only counts)",
314 " -r COUNT repeat for given count",
315 " -s print string-capabilities",
316 #ifdef NCURSES_VERSION
317 " -y disable extended capabilities",
321 for (n = 0; n < SIZEOF(msg); ++n) {
322 fprintf(stderr, "%s\n", msg[n]);
324 ExitProgram(EXIT_FAILURE);
329 main(int argc, char *argv[])
339 while ((n = getopt(argc, argv, "abd:e:nqr:sy")) != -1) {
360 if ((r_opt = atoi(optarg)) <= 0)
368 use_extended_names(FALSE);
377 if (!(b_opt || n_opt || s_opt)) {
390 for (n = optind; n < argc; ++n) {
391 brute_force(argv[n]);
393 } else if ((name = getenv("TERM")) != 0) {
396 static char dumb[] = "dumb";
402 for (repeat = 0; repeat < r_opt; ++repeat) {
404 for (n = optind; n < argc; ++n) {
405 demo_termcap(argv[n]);
407 } else if ((name = getenv("TERM")) != 0) {
410 static char dumb[] = "dumb";
415 #endif /* USE_CODE_LISTS */
417 printf("%ld values\n", total_values);
421 ExitProgram(EXIT_SUCCESS);
426 main(int argc GCC_UNUSED,
427 char *argv[]GCC_UNUSED)
429 printf("This program requires termcap\n");
430 ExitProgram(EXIT_FAILURE);