1 /****************************************************************************
2 * Copyright 2020-2021,2022 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.24 2022/12/10 23:23:27 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 GCC_NORETURN void failed(const char *);
47 failed(const char *msg)
49 fprintf(stderr, "%s\n", msg);
50 ExitProgram(EXIT_FAILURE);
60 * Total tests (and failures):
62 static long total_tests;
63 static long total_fails;
66 * Total characters formatted for tputs:
68 static long total_nulls;
69 static long total_ctrls;
70 static long total_print;
77 } else if (ch < 32 || (ch >= 127 && ch < 160)) {
86 isNumeric(char *source)
89 long value = strtol(source, &next, 0);
90 int result = (next == 0 || next == source || *next != '\0') ? 0 : 1;
96 relevant(const char *name, const char *value)
99 if (VALID_STRING(value)) {
100 if (strstr(value, "%p") == 0
101 && strstr(value, "%d") == 0
102 && strstr(value, "%s") == 0
103 && (!p_opt || strstr(value, "$<") == 0)) {
105 printf("? %s noparams\n", name);
111 (value == ABSENT_STRING)
122 increment(int *all_parms, int *num_parms, int len_parms, int end_parms)
130 if (end_parms < len_parms) {
131 if (all_parms[end_parms]++ >= num_parms[end_parms]) {
132 all_parms[end_parms] = 0;
133 increment(all_parms, num_parms, len_parms, end_parms + 1);
136 for (n = 0; n < len_parms; ++n) {
137 if (all_parms[n] != 0) {
142 /* return 1 until the vector resets to all 0's */
147 test_tparm(const char *name, const char *format, int *number)
149 char *result = tparm(format,
160 if (result != NULL) {
161 tputs(result, 1, output_func);
166 printf(".. %2d = %2d %2d %2d %2d %2d %2d %2d %2d %2d %s\n",
167 result != 0 ? (int) strlen(result) : -1,
183 static const char *msg[] =
185 "Usage: test_tparm [options] [capability] [value1 [value2 [...]]]"
187 ,"Use tparm/tputs for all distinct combinations of given capability."
191 ," -T TERM override $TERM; this may be a comma-separated list or \"-\""
192 ," to read a list from standard-input"
193 ," -a test all combinations of parameters"
194 ," [value1...] forms a vector of maximum parameter-values."
195 ," -p test capabilities with no parameters but having padding"
196 ," -r NUM repeat tests NUM times"
197 ," -v show values and results"
200 for (n = 0; n < SIZEOF(msg); ++n) {
201 fprintf(stderr, "%s\n", msg[n]);
203 ExitProgram(ok ? EXIT_SUCCESS : EXIT_FAILURE);
206 #define PLURAL(n) n, (n != 1) ? "s" : ""
207 #define COLONS(n) (n >= 1) ? ":" : ""
209 #define NUMFORM "%10ld"
215 main(int argc, char *argv[])
219 int r_run, t_run, n_run;
220 char *old_term = getenv("TERM");
224 int len_caps = 0; /* cur # of items in all_caps[] */
225 int max_caps = 10; /* max # of items in all_caps[] */
226 char **all_caps = typeCalloc(char *, max_caps);
228 int all_parms[10]; /* workspace for "-a" option */
230 int len_terms = 0; /* cur # of items in all_terms[] */
231 int max_terms = 10; /* max # of items in all_terms[] */
232 char **all_terms = typeCalloc(char *, max_terms);
238 int len_parms = 0; /* cur # of items in num_parms[], str_parms[] */
239 int max_parms = argc + 10; /* max # of items in num_parms[], str_parms[] */
240 int *num_parms = typeCalloc(int, max_parms);
241 char **str_parms = typeCalloc(char *, max_parms);
244 if (all_caps == 0 || all_terms == 0 || num_parms == 0 || str_parms == 0)
247 while ((ch = getopt(argc, argv, OPTS_COMMON "T:apr:v")) != -1) {
259 r_opt = atoi(optarg);
266 ExitProgram(EXIT_SUCCESS);
268 usage(ch == OPTS_USAGE);
274 * If there is a nonnumeric parameter after the options, use that as the
278 if (!isNumeric(argv[optind])) {
279 all_caps[len_caps++] = strdup(argv[optind++]);
284 * Any remaining arguments must be possible parameter values. If numeric,
285 * and "-a" is not set, use those as the actual values for which the
286 * capabilities are tested.
288 while (optind < argc) {
289 if (isNumeric(argv[optind])) {
291 long value = strtol(argv[optind], &dummy, 0);
292 num_parms[len_parms] = (int) value;
294 str_parms[len_parms] = argv[optind];
298 for (n = len_parms; n < max_parms; ++n) {
299 static char dummy[1];
300 str_parms[n] = dummy;
303 printf("%d parameter%s%s\n", PLURAL(len_parms), COLONS(len_parms));
305 for (n = 0; n < len_parms; ++n) {
306 printf(" %d: %d (%s)\n", n + 1, num_parms[n], str_parms[n]);
312 * Make a list of values for $TERM. Accept "-" for standard input to
313 * simplify scripting a check of the whole database.
315 old_term = strdup((old_term == 0) ? "unknown" : old_term);
317 if (!strcmp(t_opt, "-")) {
319 while (fgets(buffer, sizeof(buffer) - 1, stdin) != 0) {
322 while (isspace(UChar(s[0])))
325 while (t != s && isspace(UChar(t[-1])))
328 if (len_terms + 2 >= max_terms) {
330 all_terms = typeRealloc(char *, max_terms, all_terms);
332 failed("no memory: all_terms");
334 all_terms[len_terms++] = s;
339 while ((t = strtok(s, ",")) != 0) {
341 if (len_terms + 2 >= max_terms) {
343 all_terms = typeRealloc(char *, max_terms, all_terms);
345 failed("no memory: all_terms");
347 all_terms[len_terms++] = strdup(t);
351 all_terms[len_terms++] = strdup(old_term);
353 all_terms[len_terms] = 0;
355 printf("%d term%s:\n", PLURAL(len_terms));
357 for (n = 0; n < len_terms; ++n) {
358 printf(" %d: %s\n", n + 1, all_terms[n]);
364 * If no capability name was selected, use the predefined list of string
367 * TODO: To address the "other" systems which do not follow SVr4,
368 * just use the output from infocmp on $TERM.
371 #if defined(HAVE_CURSES_DATA_BOOLNAMES) || defined(DECL_CURSES_DATA_BOOLNAMES)
372 for (n = 0; strnames[n] != 0; ++n) {
373 if (len_caps + 2 >= max_caps) {
375 all_caps = typeRealloc(char *, max_caps, all_caps);
377 failed("no memory: all_caps");
380 all_caps[len_caps++] = strdup(strnames[n]);
383 all_caps[len_caps++] = strdup("cup");
384 all_caps[len_caps++] = strdup("sgr");
387 all_caps[len_caps] = 0;
389 printf("%d name%s%s\n", PLURAL(len_caps), COLONS(len_caps));
391 for (n = 0; n < len_caps; ++n) {
392 printf(" %d: %s\n", n + 1, all_caps[n]);
397 cap_name = typeMalloc(char *, len_caps);
398 cap_data = typeMalloc(char *, len_caps);
404 for (n = 0; n < max_parms; ++n)
406 use_parms *= (num_parms[n] + 1);
409 for (r_run = 0; r_run < r_opt; ++r_run) {
410 for (t_run = 0; t_run < len_terms; ++t_run) {
413 if (setupterm(all_terms[t_run], fileno(stdout), &errs) != OK) {
414 printf("** skipping %s (errs:%d)\n", all_terms[t_run], errs);
418 * Most of the capabilities have no parameters, e.g., they are
419 * function-keys or simple operations such as clear-display.
420 * Ignore those, since they do not really exercise tparm.
423 for (n = 0; n < len_caps; ++n) {
424 char *value = tigetstr(all_caps[n]);
425 if (relevant(all_caps[n], value)) {
426 cap_name[use_caps] = all_caps[n];
427 cap_data[use_caps] = value;
433 printf("[%d:%d] %d cap%s * %ld param%s \"%s\"\n",
440 memset(all_parms, 0, sizeof(all_parms));
442 /* for each combination of values */
444 for (n_run = 0; n_run < use_caps; ++n_run) {
445 test_tparm(cap_name[n_run], cap_data[n_run], all_parms);
448 while (increment(all_parms, num_parms, len_parms, 0));
450 /* for the given values */
451 for (n_run = 0; n_run < use_caps; ++n_run) {
452 test_tparm(cap_name[n_run], cap_data[n_run], all_parms);
456 del_curterm(cur_term);
458 printf("? no cur_term\n");
464 printf(NUMFORM " total\n", total_tests);
466 printf(NUMFORM " failed\n", total_fails);
467 printf("Characters:\n");
468 printf(NUMFORM " nulls\n", total_nulls);
469 printf(NUMFORM " controls\n", total_ctrls);
470 printf(NUMFORM " printable\n", total_print);
471 printf(NUMFORM " total\n", total_nulls + total_ctrls + total_print);
473 for (n = 0; n < len_caps; ++n) {
478 for (n = 0; n < len_terms; ++n) {
488 ExitProgram(EXIT_SUCCESS);
491 #else /* !HAVE_TIGETSTR */
495 failed("This program requires the terminfo functions such as tigetstr");
497 #endif /* HAVE_TIGETSTR */