]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/demo_terminfo.c
ncurses 5.7 - patch 20091003
[ncurses.git] / test / demo_terminfo.c
1 /****************************************************************************
2  * Copyright (c) 2009 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_terminfo.c,v 1.6 2009/07/17 01:02:08 tom Exp $
33  *
34  * A simple demo of the terminfo interface.
35  */
36 #define USE_TINFO
37 #include <test.priv.h>
38
39 #ifdef NCURSES_VERSION
40 #if NCURSES_XNAMES
41 #include <term_entry.h>
42 #endif
43 #endif
44
45 #if defined(HAVE_CURSES_DATA_BOOLNAMES) || defined(DECL_CURSES_DATA_BOOLNAMES)
46
47 static bool b_opt = FALSE;
48 static bool f_opt = FALSE;
49 static bool n_opt = FALSE;
50 static bool s_opt = FALSE;
51 static bool x_opt = FALSE;
52
53 #define FCOLS 8
54 #define FNAME(type) "%s %-*s = ", #type, FCOLS
55
56 static void
57 dumpit(NCURSES_CONST char *cap)
58 {
59     /*
60      * One of the limitations of the termcap interface is that the library
61      * cannot determine the size of the buffer passed via tgetstr(), nor the
62      * amount of space remaining.  This demo simply reuses the whole buffer
63      * for each call; a normal termcap application would try to use the buffer
64      * to hold all of the strings extracted from the terminal entry.
65      */
66     const char *str;
67     int num;
68
69     if ((str = tigetstr(cap)) != 0 && (str != (char *) -1)) {
70         /*
71          * Note that the strings returned are mostly terminfo format, since
72          * ncurses does not convert except for a handful of special cases.
73          */
74         printf(FNAME(str), cap);
75         while (*str != 0) {
76             int ch = UChar(*str++);
77             switch (ch) {
78             case '\177':
79                 fputs("^?", stdout);
80                 break;
81             case '\033':
82                 fputs("\\E", stdout);
83                 break;
84             case '\b':
85                 fputs("\\b", stdout);
86                 break;
87             case '\f':
88                 fputs("\\f", stdout);
89                 break;
90             case '\n':
91                 fputs("\\n", stdout);
92                 break;
93             case '\r':
94                 fputs("\\r", stdout);
95                 break;
96             case ' ':
97                 fputs("\\s", stdout);
98                 break;
99             case '\t':
100                 fputs("\\t", stdout);
101                 break;
102             case '^':
103                 fputs("\\^", stdout);
104                 break;
105             case ':':
106                 fputs("\\072", stdout);
107                 break;
108             case '\\':
109                 fputs("\\\\", stdout);
110                 break;
111             default:
112                 if (isgraph(ch))
113                     fputc(ch, stdout);
114                 else if (ch < 32)
115                     printf("^%c", ch + '@');
116                 else
117                     printf("\\%03o", ch);
118                 break;
119             }
120         }
121         printf("\n");
122     } else if ((num = tigetnum(cap)) >= 0) {
123         printf(FNAME(num), cap);
124         printf(" %d\n", num);
125     } else if ((num = tigetflag(cap)) >= 0) {
126         printf(FNAME(flg), cap);
127         printf("%s\n", num ? "true" : "false");
128     }
129     fflush(stdout);
130 }
131
132 static void
133 demo_terminfo(char *name)
134 {
135     unsigned n;
136     NCURSES_CONST char *cap;
137
138     printf("Terminal type \"%s\"\n", name);
139     setupterm(name, 1, (int *) 0);
140
141     if (b_opt) {
142         for (n = 0;; ++n) {
143             cap = f_opt ? boolfnames[n] : boolnames[n];
144             if (cap == 0)
145                 break;
146             dumpit(cap);
147         }
148     }
149
150     if (n_opt) {
151         for (n = 0;; ++n) {
152             cap = f_opt ? numfnames[n] : numnames[n];
153             if (cap == 0)
154                 break;
155             dumpit(cap);
156         }
157     }
158
159     if (s_opt) {
160         for (n = 0;; ++n) {
161             cap = f_opt ? strfnames[n] : strnames[n];
162             if (cap == 0)
163                 break;
164             dumpit(cap);
165         }
166     }
167 #ifdef NCURSES_VERSION
168     if (x_opt) {
169         int mod;
170         if (f_opt) {
171 #if NCURSES_XNAMES
172             TERMTYPE *term = &(cur_term->type);
173             if (term != 0
174                 && ((NUM_BOOLEANS(term) != BOOLCOUNT)
175                     || (NUM_NUMBERS(term) != NUMCOUNT)
176                     || (NUM_STRINGS(term) != STRCOUNT))) {
177                 for (n = BOOLCOUNT; n < NUM_BOOLEANS(term); ++n) {
178                     dumpit(ExtBoolname(term, n, boolnames));
179                 }
180                 for (n = NUMCOUNT; n < NUM_NUMBERS(term); ++n) {
181                     dumpit(ExtNumname(term, n, numnames));
182                 }
183                 for (n = STRCOUNT; n < NUM_STRINGS(term); ++n) {
184                     dumpit(ExtStrname(term, n, strnames));
185                 }
186             }
187 #endif
188         } else {
189             char temp[10];
190             static const char *xterm_keys[] =
191             {
192                 "kDC", "kDN", "kEND", "kHOM", "kIC",
193                 "kLFT", "kNXT", "kPRV", "kRIT", "kUP",
194             };
195             for (n = 0; n < SIZEOF(xterm_keys); ++n) {
196                 for (mod = 0; mod < 8; ++mod) {
197                     if (mod == 0)
198                         strcpy(temp, xterm_keys[n]);
199                     else
200                         sprintf(temp, "%s%d", xterm_keys[n], mod);
201                     dumpit(temp);
202                 }
203             }
204         }
205     }
206 #endif
207
208 }
209
210 static void
211 usage(void)
212 {
213     static const char *msg[] =
214     {
215         "Usage: demo_terminfo [options] [terminal]",
216         "",
217         "If no options are given, print all (boolean, numeric, string)",
218         "capabilities for the given terminal, using short names.",
219         "",
220         "Options:",
221         " -b       print boolean-capabilities",
222         " -f       print full names",
223         " -n       print numeric-capabilities",
224         " -r COUNT repeat for given count",
225         " -s       print string-capabilities",
226 #ifdef NCURSES_VERSION
227         " -x       print extended capabilities",
228 #endif
229     };
230     unsigned n;
231     for (n = 0; n < SIZEOF(msg); ++n) {
232         fprintf(stderr, "%s\n", msg[n]);
233     }
234     ExitProgram(EXIT_FAILURE);
235 }
236
237 int
238 main(int argc, char *argv[])
239 {
240     int n;
241     int repeat;
242     char *name;
243     int r_opt = 1;
244
245     while ((n = getopt(argc, argv, "bfnr:sx")) != -1) {
246         switch (n) {
247         case 'b':
248             b_opt = TRUE;
249             break;
250         case 'f':
251             f_opt = TRUE;
252             break;
253         case 'n':
254             n_opt = TRUE;
255             break;
256         case 'r':
257             if ((r_opt = atoi(optarg)) <= 0)
258                 usage();
259             break;
260         case 's':
261             s_opt = TRUE;
262             break;
263 #ifdef NCURSES_VERSION
264         case 'x':
265             x_opt = TRUE;
266             use_extended_names(TRUE);
267             break;
268 #endif
269         default:
270             usage();
271             break;
272         }
273     }
274
275     if (!(b_opt || n_opt || s_opt || x_opt)) {
276         b_opt = TRUE;
277         n_opt = TRUE;
278         s_opt = TRUE;
279     }
280
281     for (repeat = 0; repeat < r_opt; ++repeat) {
282         if (optind < argc) {
283             for (n = optind; n < argc; ++n) {
284                 demo_terminfo(argv[n]);
285             }
286         } else if ((name = getenv("TERM")) != 0) {
287             demo_terminfo(name);
288         } else {
289             static char dumb[] = "dumb";
290             demo_terminfo(dumb);
291         }
292     }
293
294     ExitProgram(EXIT_SUCCESS);
295 }
296
297 #else
298 int
299 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
300 {
301     printf("This program requires the terminfo arrays\n");
302     ExitProgram(EXIT_FAILURE);
303 }
304 #endif