]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/test_arrays.c
ncurses 6.2 - patch 20200829
[ncurses.git] / test / test_arrays.c
1 /****************************************************************************
2  * Copyright 2020 Thomas E. Dickey                                          *
3  * Copyright 2007-2010,2017 Free Software Foundation, Inc.                  *
4  *                                                                          *
5  * Permission is hereby granted, free of charge, to any person obtaining a  *
6  * copy of this software and associated documentation files (the            *
7  * "Software"), to deal in the Software without restriction, including      *
8  * without limitation the rights to use, copy, modify, merge, publish,      *
9  * distribute, distribute with modifications, sublicense, and/or sell       *
10  * copies of the Software, and to permit persons to whom the Software is    *
11  * furnished to do so, subject to the following conditions:                 *
12  *                                                                          *
13  * The above copyright notice and this permission notice shall be included  *
14  * in all copies or substantial portions of the Software.                   *
15  *                                                                          *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
19  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
20  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
21  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
23  *                                                                          *
24  * Except as contained in this notice, the name(s) of the above copyright   *
25  * holders shall not be used in advertising or otherwise to promote the     *
26  * sale, use or other dealings in this Software without prior written       *
27  * authorization.                                                           *
28  ****************************************************************************/
29 /*
30  * $Id: test_arrays.c,v 1.9 2020/02/02 23:34:34 tom Exp $
31  *
32  * Author: Thomas E Dickey
33  *
34  * Demonstrate the public arrays from the terminfo library.
35
36 extern NCURSES_EXPORT_VAR(NCURSES_CONST char * const ) boolnames[];
37 extern NCURSES_EXPORT_VAR(NCURSES_CONST char * const ) boolcodes[];
38 extern NCURSES_EXPORT_VAR(NCURSES_CONST char * const ) boolfnames[];
39 extern NCURSES_EXPORT_VAR(NCURSES_CONST char * const ) numnames[];
40 extern NCURSES_EXPORT_VAR(NCURSES_CONST char * const ) numcodes[];
41 extern NCURSES_EXPORT_VAR(NCURSES_CONST char * const ) numfnames[];
42 extern NCURSES_EXPORT_VAR(NCURSES_CONST char * const ) strnames[];
43 extern NCURSES_EXPORT_VAR(NCURSES_CONST char * const ) strcodes[];
44 extern NCURSES_EXPORT_VAR(NCURSES_CONST char * const ) strfnames[];
45
46  */
47
48 #define USE_TINFO
49 #include <test.priv.h>
50
51 #if HAVE_TIGETSTR
52 #if defined(HAVE_CURSES_DATA_BOOLNAMES) || defined(DECL_CURSES_DATA_BOOLNAMES)
53
54 static bool opt_C;
55 static bool opt_T;
56 static bool opt_c;
57 static bool opt_f;
58 static bool opt_n;
59 static bool opt_t;
60
61 #define PLAIN(opts, name) if (opts) dump_array(#name, name)
62
63 static void
64 dump_array(const char *name, NCURSES_CONST char *const *list)
65 {
66     int n;
67
68     printf("%s:\n", name);
69     for (n = 0; list[n] != 0; ++n) {
70         printf("%5d:%s\n", n, list[n]);
71     }
72 }
73
74 static void
75 dump_plain(void)
76 {
77     PLAIN(opt_T && opt_n, boolnames);
78     PLAIN(opt_C && opt_c, boolcodes);
79     PLAIN(opt_T && opt_f, boolfnames);
80
81     PLAIN(opt_T && opt_n, numnames);
82     PLAIN(opt_C && opt_c, numcodes);
83     PLAIN(opt_T && opt_f, numfnames);
84
85     PLAIN(opt_T && opt_n, strnames);
86     PLAIN(opt_C && opt_c, strcodes);
87     PLAIN(opt_T && opt_f, strfnames);
88 }
89
90 #define STRING(opts, name) if (opts) { printf("%s\"%s\"", c++ ? "," : "", name); }
91 #define NUMBER(opts, value) if (opts) { printf("%s%d", c++ ? "," : "", value); }
92
93 static void
94 dump_table(void)
95 {
96     int c = 0;
97     int r;
98
99     STRING(opt_t, "Index");
100     STRING(opt_t, "Type");
101     STRING(opt_n, "Name");
102     STRING(opt_c, "Code");
103     STRING(opt_f, "FName");
104     printf("\n");
105
106     for (r = 0; boolnames[r]; ++r) {
107         c = 0;
108         NUMBER(opt_t, r);
109         STRING(opt_t, "bool");
110         STRING(opt_T && opt_n, boolnames[r]);
111         STRING(opt_C && opt_c, boolcodes[r]);
112         STRING(opt_T && opt_f, boolfnames[r]);
113         printf("\n");
114     }
115
116     for (r = 0; numnames[r]; ++r) {
117         c = 0;
118         NUMBER(opt_t, r);
119         STRING(opt_t, "num");
120         STRING(opt_T && opt_n, numnames[r]);
121         STRING(opt_C && opt_c, numcodes[r]);
122         STRING(opt_T && opt_f, numfnames[r]);
123         printf("\n");
124     }
125
126     for (r = 0; strnames[r]; ++r) {
127         c = 0;
128         NUMBER(opt_t, r);
129         STRING(opt_t, "str");
130         STRING(opt_T && opt_n, strnames[r]);
131         STRING(opt_C && opt_c, strcodes[r]);
132         STRING(opt_T && opt_f, strfnames[r]);
133         printf("\n");
134     }
135 }
136
137 static void
138 usage(void)
139 {
140     static const char *msg[] =
141     {
142         "Usage: test_arrays [options]",
143         "",
144         "If no options are given, print all (boolean, numeric, string)",
145         "capability names showing their index within the tables.",
146         "",
147         "Options:",
148         " -C       print termcap names",
149         " -T       print terminfo names",
150         " -c       print termcap names",
151         " -f       print full terminfo names",
152         " -n       print short terminfo names",
153         " -t       print the result as CSV table",
154     };
155     unsigned n;
156     for (n = 0; n < SIZEOF(msg); ++n) {
157         fprintf(stderr, "%s\n", msg[n]);
158     }
159     ExitProgram(EXIT_FAILURE);
160 }
161
162 int
163 main(int argc, char *argv[])
164 {
165     int n;
166
167     while ((n = getopt(argc, argv, "CTcfnt")) != -1) {
168         switch (n) {
169         case 'C':
170             opt_C = TRUE;
171             break;
172         case 'T':
173             opt_T = TRUE;
174             break;
175         case 'c':
176             opt_c = TRUE;
177             break;
178         case 'f':
179             opt_f = TRUE;
180             break;
181         case 'n':
182             opt_n = TRUE;
183             break;
184         case 't':
185             opt_t = TRUE;
186             break;
187         default:
188             usage();
189             /* NOTREACHED */
190         }
191     }
192     if (optind < argc)
193         usage();
194
195     if (!(opt_T || opt_C)) {
196         opt_T = opt_C = TRUE;
197     }
198     if (!(opt_c || opt_f || opt_n)) {
199         opt_c = opt_f = opt_n = TRUE;
200     }
201
202     if (opt_t) {
203         dump_table();
204     } else {
205         dump_plain();
206     }
207
208     ExitProgram(EXIT_SUCCESS);
209 }
210
211 #else
212 int
213 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
214 {
215     printf("This program requires the terminfo arrays\n");
216     ExitProgram(EXIT_FAILURE);
217 }
218 #endif
219 #else /* !HAVE_TIGETSTR */
220 int
221 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
222 {
223     printf("This program requires the terminfo functions such as tigetstr\n");
224     ExitProgram(EXIT_FAILURE);
225 }
226 #endif /* HAVE_TIGETSTR */