]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/test_arrays.c
ncurses 6.4 - patch 20240420
[ncurses.git] / test / test_arrays.c
1 /****************************************************************************
2  * Copyright 2020,2022 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.13 2022/12/10 23:23:27 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(int ok)
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         ,USAGE_COMMON
148         ,"Options:"
149         ," -C       print termcap names"
150         ," -T       print terminfo names"
151         ," -c       print termcap names"
152         ," -f       print full terminfo names"
153         ," -n       print short terminfo names"
154         ," -t       print the result as CSV table"
155     };
156     unsigned n;
157     for (n = 0; n < SIZEOF(msg); ++n) {
158         fprintf(stderr, "%s\n", msg[n]);
159     }
160     ExitProgram(ok ? EXIT_SUCCESS : EXIT_FAILURE);
161 }
162 /* *INDENT-OFF* */
163 VERSION_COMMON()
164 /* *INDENT-ON* */
165
166 int
167 main(int argc, char *argv[])
168 {
169     int ch;
170
171     while ((ch = getopt(argc, argv, OPTS_COMMON "CTcfnt")) != -1) {
172         switch (ch) {
173         case 'C':
174             opt_C = TRUE;
175             break;
176         case 'T':
177             opt_T = TRUE;
178             break;
179         case 'c':
180             opt_c = TRUE;
181             break;
182         case 'f':
183             opt_f = TRUE;
184             break;
185         case 'n':
186             opt_n = TRUE;
187             break;
188         case 't':
189             opt_t = TRUE;
190             break;
191         case OPTS_VERSION:
192             show_version(argv);
193             ExitProgram(EXIT_SUCCESS);
194         default:
195             usage(ch == OPTS_USAGE);
196             /* NOTREACHED */
197         }
198     }
199     if (optind < argc)
200         usage(FALSE);
201
202     if (!(opt_T || opt_C)) {
203         opt_T = opt_C = TRUE;
204     }
205     if (!(opt_c || opt_f || opt_n)) {
206         opt_c = opt_f = opt_n = TRUE;
207     }
208
209     if (opt_t) {
210         dump_table();
211     } else {
212         dump_plain();
213     }
214
215     ExitProgram(EXIT_SUCCESS);
216 }
217
218 #else
219 int
220 main(void)
221 {
222     printf("This program requires the terminfo arrays\n");
223     ExitProgram(EXIT_FAILURE);
224 }
225 #endif
226 #else /* !HAVE_TIGETSTR */
227 int
228 main(void)
229 {
230     printf("This program requires the terminfo functions such as tigetstr\n");
231     ExitProgram(EXIT_FAILURE);
232 }
233 #endif /* HAVE_TIGETSTR */