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