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