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