1 /****************************************************************************
2 * Copyright (c) 2005-2011,2012 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.22 2012/07/07 23:37:27 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 q_opt = FALSE;
54 static bool s_opt = FALSE;
59 static char **db_list;
62 static long total_values;
64 #define isCapName(c) (isgraph(c) && strchr("^#=:\\", c) == 0)
66 #if NO_LEAKS && USE_CODE_LISTS
68 #define MYSCR struct _myscr
74 static MYSCR *my_screens;
79 MYSCR *obj = malloc(sizeof(MYSCR));
80 obj->next = my_screens;
85 #define save_screen() /* nothing */
89 make_dbitem(char *p, char *q)
91 char *result = malloc(strlen(e_opt) + 2 + (size_t) (p - q));
92 sprintf(result, "%s=%.*s", e_opt, (int) (p - q), q);
102 for (pass = 0; pass < 2; ++pass) {
106 for (p = q = d_opt; *p != '\0'; ++p) {
110 db_list[count] = make_dbitem(p, q);
119 db_list[count] = make_dbitem(p, q);
124 db_list = typeCalloc(char *, count + 1);
136 if ((result = db_list[db_item]) == 0) {
143 printf("** %s\n", result);
152 for (n = 0; db_list[n]; ++n)
160 dumpit(NCURSES_CONST char *cap)
163 * One of the limitations of the termcap interface is that the library
164 * cannot determine the size of the buffer passed via tgetstr(), nor the
165 * amount of space remaining. This demo simply reuses the whole buffer
166 * for each call; a normal termcap application would try to use the buffer
167 * to hold all of the strings extracted from the terminal entry.
169 char area[1024], *ap = area;
173 if ((str = tgetstr(cap, &ap)) != 0) {
177 * Note that the strings returned are mostly terminfo format, since
178 * ncurses does not convert except for a handful of special cases.
180 printf(FNAME(str), cap);
182 int ch = UChar(*str++);
188 fputs("\\E", stdout);
191 fputs("\\b", stdout);
194 fputs("\\f", stdout);
197 fputs("\\n", stdout);
200 fputs("\\r", stdout);
203 fputs("\\s", stdout);
206 fputs("\\t", stdout);
209 fputs("\\^", stdout);
212 fputs("\\072", stdout);
215 fputs("\\\\", stdout);
221 printf("^%c", ch + '@');
223 printf("\\%03o", ch);
229 } else if ((num = tgetnum(cap)) >= 0) {
232 printf(FNAME(num), cap);
233 printf(" %d\n", num);
235 } else if (tgetflag(cap) > 0) {
238 printf(FNAME(flg), cap);
239 printf("%s\n", "true");
248 brute_force(const char *name)
253 putenv(next_dbitem());
255 printf("Terminal type %s\n", name);
256 if (tgetent(buffer, name) >= 0) {
261 for (c1 = 0; c1 < 256; ++c1) {
264 for (c2 = 0; c2 < 256; ++c2) {
277 demo_termcap(NCURSES_CONST char *name)
280 NCURSES_CONST char *cap;
283 putenv(next_dbitem());
285 printf("Terminal type \"%s\"\n", name);
287 setupterm(name, 1, (int *) 0);
324 static const char *msg[] =
326 "Usage: demo_termcap [options] [terminal]",
328 "If no options are given, print all (boolean, numeric, string)",
329 "capabilities for the given terminal, using short names.",
332 " -a try all names, print capabilities found",
333 " -b print boolean-capabilities",
334 " -d LIST colon-separated list of databases to use",
335 " -e NAME environment variable to set with -d option",
336 " -n print numeric-capabilities",
337 " -q quiet (prints only counts)",
338 " -r COUNT repeat for given count",
339 " -s print string-capabilities",
342 for (n = 0; n < SIZEOF(msg); ++n) {
343 fprintf(stderr, "%s\n", msg[n]);
345 ExitProgram(EXIT_FAILURE);
350 main(int argc, char *argv[])
360 while ((n = getopt(argc, argv, "abd:e:nqr:s")) != -1) {
381 if ((r_opt = atoi(optarg)) <= 0)
393 if (!(b_opt || n_opt || s_opt)) {
406 for (n = optind; n < argc; ++n) {
407 brute_force(argv[n]);
409 } else if ((name = getenv("TERM")) != 0) {
412 static char dumb[] = "dumb";
418 for (repeat = 0; repeat < r_opt; ++repeat) {
420 for (n = optind; n < argc; ++n) {
421 demo_termcap(argv[n]);
423 } else if ((name = getenv("TERM")) != 0) {
426 static char dumb[] = "dumb";
432 * ncurses' tgetent() interface caches some entries and its no-leaks
433 * code discards those. The calls to setupterm() on the other hand
434 * are not cached, and each call allocates a chunk of memory, even
435 * if the same terminal type is requested repeatedly.
437 while (my_screens != 0) {
438 MYSCR *next = my_screens->next;
439 del_curterm(my_screens->term);
445 #endif /* USE_CODE_LISTS */
447 printf("%ld values\n", total_values);
451 ExitProgram(EXIT_SUCCESS);
456 main(int argc GCC_UNUSED,
457 char *argv[]GCC_UNUSED)
459 printf("This program requires termcap\n");
460 ExitProgram(EXIT_FAILURE);