1/****************************************************************************
2 * Copyright (c) 2005-2007,2008 Free Software Foundation, Inc.              *
3 *                                                                          *
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:                 *
11 *                                                                          *
12 * The above copyright notice and this permission notice shall be included  *
13 * in all copies or substantial portions of the Software.                   *
14 *                                                                          *
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.                               *
22 *                                                                          *
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       *
26 * authorization.                                                           *
27 ****************************************************************************/
28
29/*
30 * Author: Thomas E. Dickey
31 *
32 * $Id: demo_termcap.c,v 1.7 2008/02/09 18:08:36 tom Exp $
33 *
34 * A simple demo of the termcap interface.
35 */
36#define USE_TINFO
37#include <test.priv.h>
38
39#if HAVE_TGETENT
40
41#define isCapName(c) (isgraph(c) && strchr("^#=:\\", c) == 0)
42
43static void
44dumpit(char *cap)
45{
46    /*
47     * One of the limitations of the termcap interface is that the library
48     * cannot determine the size of the buffer passed via tgetstr(), nor the
49     * amount of space remaining.  This demo simply reuses the whole buffer
50     * for each call; a normal termcap application would try to use the buffer
51     * to hold all of the strings extracted from the terminal entry.
52     */
53    char area[1024], *ap = area;
54    char *str;
55    int num;
56
57    if ((str = tgetstr(cap, &ap)) != 0) {
58	/*
59	 * Note that the strings returned are mostly terminfo format, since
60	 * ncurses does not convert except for a handful of special cases.
61	 */
62	printf("str %s = ", cap);
63	while (*str != 0) {
64	    int ch = UChar(*str++);
65	    switch (ch) {
66	    case '\177':
67		fputs("^?", stdout);
68		break;
69	    case '\033':
70		fputs("\\E", stdout);
71		break;
72	    case '\b':
73		fputs("\\b", stdout);
74		break;
75	    case '\f':
76		fputs("\\f", stdout);
77		break;
78	    case '\n':
79		fputs("\\n", stdout);
80		break;
81	    case '\r':
82		fputs("\\r", stdout);
83		break;
84	    case ' ':
85		fputs("\\s", stdout);
86		break;
87	    case '\t':
88		fputs("\\t", stdout);
89		break;
90	    case '^':
91		fputs("\\^", stdout);
92		break;
93	    case ':':
94		fputs("\\072", stdout);
95		break;
96	    case '\\':
97		fputs("\\\\", stdout);
98		break;
99	    default:
100		if (isgraph(ch))
101		    fputc(ch, stdout);
102		else if (ch < 32)
103		    printf("^%c", ch + '@');
104		else
105		    printf("\\%03o", ch);
106		break;
107	    }
108	}
109	printf("\n");
110    } else if ((num = tgetnum(cap)) >= 0) {
111	printf("num %s = %d\n", cap, num);
112    } else if ((num = tgetflag(cap)) > 0) {
113	printf("flg %s\n", cap);
114    }
115    fflush(stdout);
116}
117
118static void
119demo_termcap(char *name)
120{
121    char buffer[1024];
122
123    printf("Terminal type %s\n", name);
124    if (tgetent(buffer, name) >= 0) {
125	char cap[3];
126	int c1, c2;
127
128	cap[2] = 0;
129	for (c1 = 0; c1 < 256; ++c1) {
130	    cap[0] = c1;
131	    if (isCapName(c1)) {
132		for (c2 = 0; c2 < 256; ++c2) {
133		    cap[1] = c2;
134		    if (isCapName(c2)) {
135			dumpit(cap);
136		    }
137		}
138	    }
139	}
140    }
141}
142
143int
144main(int argc, char *argv[])
145{
146    int n;
147    char *name;
148
149    if (argc > 1) {
150	for (n = 1; n < argc; ++n) {
151	    demo_termcap(argv[n]);
152	}
153    } else if ((name = getenv("TERM")) != 0) {
154	demo_termcap(name);
155    } else {
156	static char dumb[] = "dumb";
157	demo_termcap(dumb);
158    }
159
160    ExitProgram(EXIT_SUCCESS);
161}
162
163#else
164int
165main(int argc GCC_UNUSED,
166     char *argv[]GCC_UNUSED)
167{
168    printf("This program requires termcap\n");
169    exit(EXIT_FAILURE);
170}
171#endif
172