blob: f89cb280f05f770809d3044b478534439c9de42b [file] [log] [blame]
Steve Kondikae271bc2015-11-15 02:50:53 +01001/****************************************************************************
micky3879b9f5e72025-07-08 18:04:53 -04002 * Copyright 2020,2022 Thomas E. Dickey *
3 * Copyright 2007-2010,2017 Free Software Foundation, Inc. *
Steve Kondikae271bc2015-11-15 02:50:53 +01004 * *
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/*
micky3879b9f5e72025-07-08 18:04:53 -040030 * $Id: test_arrays.c,v 1.13 2022/12/10 23:23:27 tom Exp $
Steve Kondikae271bc2015-11-15 02:50:53 +010031 *
32 * Author: Thomas E Dickey
33 *
34 * Demonstrate the public arrays from the terminfo library.
35
36extern NCURSES_EXPORT_VAR(NCURSES_CONST char * const ) boolnames[];
37extern NCURSES_EXPORT_VAR(NCURSES_CONST char * const ) boolcodes[];
38extern NCURSES_EXPORT_VAR(NCURSES_CONST char * const ) boolfnames[];
39extern NCURSES_EXPORT_VAR(NCURSES_CONST char * const ) numnames[];
40extern NCURSES_EXPORT_VAR(NCURSES_CONST char * const ) numcodes[];
41extern NCURSES_EXPORT_VAR(NCURSES_CONST char * const ) numfnames[];
42extern NCURSES_EXPORT_VAR(NCURSES_CONST char * const ) strnames[];
43extern NCURSES_EXPORT_VAR(NCURSES_CONST char * const ) strcodes[];
44extern 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
micky3879b9f5e72025-07-08 18:04:53 -040054static bool opt_C;
55static bool opt_T;
56static bool opt_c;
57static bool opt_f;
58static bool opt_n;
59static bool opt_t;
60
61#define PLAIN(opts, name) if (opts) dump_array(#name, name)
Steve Kondikae271bc2015-11-15 02:50:53 +010062
63static void
64dump_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
micky3879b9f5e72025-07-08 18:04:53 -040074static void
75dump_plain(void)
Steve Kondikae271bc2015-11-15 02:50:53 +010076{
micky3879b9f5e72025-07-08 18:04:53 -040077 PLAIN(opt_T && opt_n, boolnames);
78 PLAIN(opt_C && opt_c, boolcodes);
79 PLAIN(opt_T && opt_f, boolfnames);
Steve Kondikae271bc2015-11-15 02:50:53 +010080
micky3879b9f5e72025-07-08 18:04:53 -040081 PLAIN(opt_T && opt_n, numnames);
82 PLAIN(opt_C && opt_c, numcodes);
83 PLAIN(opt_T && opt_f, numfnames);
Steve Kondikae271bc2015-11-15 02:50:53 +010084
micky3879b9f5e72025-07-08 18:04:53 -040085 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
93static void
94dump_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
137static void
138usage(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* */
163VERSION_COMMON()
164/* *INDENT-ON* */
165
166int
167main(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 }
Steve Kondikae271bc2015-11-15 02:50:53 +0100214
215 ExitProgram(EXIT_SUCCESS);
216}
217
218#else
219int
micky3879b9f5e72025-07-08 18:04:53 -0400220main(void)
Steve Kondikae271bc2015-11-15 02:50:53 +0100221{
222 printf("This program requires the terminfo arrays\n");
223 ExitProgram(EXIT_FAILURE);
224}
225#endif
226#else /* !HAVE_TIGETSTR */
227int
micky3879b9f5e72025-07-08 18:04:53 -0400228main(void)
Steve Kondikae271bc2015-11-15 02:50:53 +0100229{
230 printf("This program requires the terminfo functions such as tigetstr\n");
231 ExitProgram(EXIT_FAILURE);
232}
233#endif /* HAVE_TIGETSTR */