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