1 /****************************************************************************
2 * Copyright 2020 Thomas E. Dickey *
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: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
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. *
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 *
27 ****************************************************************************/
30 * Author: Thomas E. Dickey
32 * $Id: test_tparm.c,v 1.4 2020/05/31 00:51:32 tom Exp $
34 * Exercise tparm, either for all possible capabilities with fixed parameters,
35 * or one capability with all possible parameters.
37 * TODO: incorporate tic.h and _nc_tparm_analyze
38 * TODO: optionally test tiparm
39 * TODO: add checks/logic to handle "%s" in tparm
42 #include <test.priv.h>
44 static void failed(const char *) GCC_NORETURN;
47 failed(const char *msg)
49 fprintf(stderr, "%s\n", msg);
50 ExitProgram(EXIT_FAILURE);
59 isNumeric(char *source)
62 long value = strtol(source, &next, 0);
63 int result = (next == 0 || next == source || *next != '\0') ? 0 : 1;
69 validate(const char *name)
71 char *value = tigetstr(name);
72 if (!VALID_STRING(value)) {
75 (value == ABSENT_STRING)
86 increment(int *all_parms, int *num_parms, int len_parms, int end_parms)
94 if (end_parms < len_parms) {
95 if (all_parms[end_parms]++ >= num_parms[end_parms]) {
96 all_parms[end_parms] = 0;
97 increment(all_parms, num_parms, len_parms, end_parms + 1);
100 for (n = 0; n < len_parms; ++n) {
101 if (all_parms[n] != 0) {
106 /* return 1 until the vector resets to all 0's */
111 test_tparm(const char *name, int *number)
113 char *format = tigetstr(name);
114 if ((format = validate(name)) != 0) {
115 char *result = tparm(format,
126 printf(".. %2d = %2d %2d %2d %2d %2d %2d %2d %2d %2d %s\n",
127 result != 0 ? (int) strlen(result) : -1,
144 static const char *msg[] =
146 "Usage: test_tparm [options] [capability] [value1 [value2 [...]]]",
148 "Print all distinct combinations of given capability.",
151 " -T TERM override $TERM; this may be a comma-separated list or \"-\"",
152 " to read a list from standard-input",
153 " -a if capability is given, test all combinations of values",
154 " -r NUM repeat tests NUM times",
155 " -v show values and results",
158 for (n = 0; n < SIZEOF(msg); ++n) {
159 fprintf(stderr, "%s\n", msg[n]);
161 ExitProgram(EXIT_FAILURE);
164 #define PLURAL(n) n, (n != 1) ? "s" : ""
165 #define COLONS(n) (n >= 1) ? ":" : ""
168 main(int argc, char *argv[])
171 int r_run, t_run, n_run;
172 char *old_term = getenv("TERM");
175 int len_names = 0; /* cur # of items in all_names[] */
176 int use_names = 10; /* max # of items in all_names[] */
177 char **all_names = typeCalloc(char *, use_names);
178 int all_parms[10]; /* workspace for "-a" option */
179 int len_terms = 0; /* cur # of items in all_terms[] */
180 int use_terms = 10; /* max # of items in all_terms[] */
181 char **all_terms = typeCalloc(char *, use_terms);
182 int len_parms = 0; /* cur # of items in num_parms[], str_parms[] */
183 int use_parms = argc + 10; /* max # of items in num_parms[], str_parms[] */
184 int *num_parms = typeCalloc(int, use_parms);
185 char **str_parms = typeCalloc(char *, use_parms);
187 if (all_names == 0 || all_terms == 0 || num_parms == 0 || str_parms == 0)
190 while ((n = getopt(argc, argv, "T:ar:v")) != -1) {
199 r_opt = atoi(optarg);
211 * If there is a nonnumeric parameter after the options, use that as the
215 if (!isNumeric(argv[optind])) {
216 all_names[len_names++] = strdup(argv[optind++]);
221 * Any remaining arguments must be possible parameter values. If numeric,
222 * and "-a" is not set, use those as the maximum values within which the
223 * test parameters should vary.
225 while (optind < argc) {
226 if (isNumeric(argv[optind])) {
228 long value = strtol(argv[optind], &dummy, 0);
229 num_parms[len_parms] = (int) value;
231 str_parms[len_parms] = argv[optind];
235 for (n = len_parms; n < use_parms; ++n) {
236 static char dummy[1];
237 str_parms[n] = dummy;
240 printf("%d parameter%s%s\n", PLURAL(len_parms), COLONS(len_parms));
241 for (n = 0; n < len_parms; ++n) {
242 printf(" %d: %d (%s)\n", n + 1, num_parms[n], str_parms[n]);
247 * Make a list of values for $TERM. Accept "-" for standard input to
248 * simplify scripting a check of the whole database.
250 old_term = strdup((old_term == 0) ? "unknown" : old_term);
252 if (!strcmp(t_opt, "-")) {
254 while (fgets(buffer, sizeof(buffer) - 1, stdin) != 0) {
257 while (isspace(UChar(s[0])))
260 while (t != s && isspace(UChar(t[-1])))
263 if (len_terms + 2 >= use_terms) {
265 all_terms = typeRealloc(char *, use_terms, all_terms);
267 failed("no memory: all_terms");
269 all_terms[len_terms++] = s;
274 while ((t = strtok(s, ",")) != 0) {
276 if (len_terms + 2 >= use_terms) {
278 all_terms = typeRealloc(char *, use_terms, all_terms);
280 failed("no memory: all_terms");
282 all_terms[len_terms++] = strdup(t);
286 all_terms[len_terms++] = strdup(old_term);
288 all_terms[len_terms] = 0;
290 printf("%d term%s:\n", PLURAL(len_terms));
291 for (n = 0; n < len_terms; ++n) {
292 printf(" %d: %s\n", n + 1, all_terms[n]);
297 * If no capability name was selected, use the predefined list of string
300 * TODO: To address the "other" systems which do not follow SVr4,
301 * just use the output from infocmp on $TERM.
303 if (len_names == 0) {
304 #if defined(HAVE_CURSES_DATA_BOOLNAMES) || defined(DECL_CURSES_DATA_BOOLNAMES)
305 for (n = 0; strnames[n] != 0; ++n) {
306 if (len_names + 2 >= use_names) {
308 all_names = typeRealloc(char *, use_names, all_names);
309 if (all_names == 0) {
310 failed("no memory: all_names");
313 all_names[len_names++] = strdup(strnames[n]);
316 all_names[len_names++] = strdup("cup");
317 all_names[len_names++] = strdup("sgr");
320 all_names[len_names] = 0;
322 printf("%d name%s%s\n", PLURAL(len_names), COLONS(len_names));
323 for (n = 0; n < len_names; ++n) {
324 printf(" %d: %s\n", n + 1, all_names[n]);
331 for (r_run = 0; r_run < r_opt; ++r_run) {
332 for (t_run = 0; t_run < len_terms; ++t_run) {
335 if (setupterm(all_terms[t_run], fileno(stdout), &errs) != OK) {
336 printf("** skipping %s (errs:%d)\n", all_terms[t_run], errs);
340 printf("** testing %s\n", all_terms[t_run]);
341 if (len_names == 1) {
343 /* for each combination of values */
344 memset(all_parms, 0, sizeof(all_parms));
346 test_tparm(all_names[0], all_parms);
348 while (increment(all_parms, num_parms, len_parms, 0));
350 /* for the given values */
351 test_tparm(all_names[0], num_parms);
354 for (n_run = 0; n_run < len_names; ++n_run) {
355 test_tparm(all_names[n_run], num_parms);
359 del_curterm(cur_term);
361 printf("? no cur_term\n");
366 for (n = 0; n < len_names; ++n) {
371 for (n = 0; n < len_terms; ++n) {
379 ExitProgram(EXIT_SUCCESS);
382 #else /* !HAVE_TIGETSTR */
384 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
386 failed("This program requires the terminfo functions such as tigetstr");
388 #endif /* HAVE_TIGETSTR */