1 /****************************************************************************
2 * Copyright 2020,2021 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.20 2021/03/20 15:58: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 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.",
190 " -T TERM override $TERM; this may be a comma-separated list or \"-\"",
191 " to read a list from standard-input",
192 " -a test all combinations of parameters",
193 " [value1...] forms a vector of maximum parameter-values.",
194 " -p test capabilities with no parameters but having padding",
195 " -r NUM repeat tests NUM times",
196 " -v show values and results",
199 for (n = 0; n < SIZEOF(msg); ++n) {
200 fprintf(stderr, "%s\n", msg[n]);
202 ExitProgram(EXIT_FAILURE);
205 #define PLURAL(n) n, (n != 1) ? "s" : ""
206 #define COLONS(n) (n >= 1) ? ":" : ""
208 #define NUMFORM "%10ld"
211 main(int argc, char *argv[])
214 int r_run, t_run, n_run;
215 char *old_term = getenv("TERM");
219 int len_caps = 0; /* cur # of items in all_caps[] */
220 int max_caps = 10; /* max # of items in all_caps[] */
221 char **all_caps = typeCalloc(char *, max_caps);
223 int all_parms[10]; /* workspace for "-a" option */
225 int len_terms = 0; /* cur # of items in all_terms[] */
226 int max_terms = 10; /* max # of items in all_terms[] */
227 char **all_terms = typeCalloc(char *, max_terms);
233 int len_parms = 0; /* cur # of items in num_parms[], str_parms[] */
234 int max_parms = argc + 10; /* max # of items in num_parms[], str_parms[] */
235 int *num_parms = typeCalloc(int, max_parms);
236 char **str_parms = typeCalloc(char *, max_parms);
239 if (all_caps == 0 || all_terms == 0 || num_parms == 0 || str_parms == 0)
242 while ((n = getopt(argc, argv, "T:apr:v")) != -1) {
254 r_opt = atoi(optarg);
266 * If there is a nonnumeric parameter after the options, use that as the
270 if (!isNumeric(argv[optind])) {
271 all_caps[len_caps++] = strdup(argv[optind++]);
276 * Any remaining arguments must be possible parameter values. If numeric,
277 * and "-a" is not set, use those as the actual values for which the
278 * capabilities are tested.
280 while (optind < argc) {
281 if (isNumeric(argv[optind])) {
283 long value = strtol(argv[optind], &dummy, 0);
284 num_parms[len_parms] = (int) value;
286 str_parms[len_parms] = argv[optind];
290 for (n = len_parms; n < max_parms; ++n) {
291 static char dummy[1];
292 str_parms[n] = dummy;
295 printf("%d parameter%s%s\n", PLURAL(len_parms), COLONS(len_parms));
297 for (n = 0; n < len_parms; ++n) {
298 printf(" %d: %d (%s)\n", n + 1, num_parms[n], str_parms[n]);
304 * Make a list of values for $TERM. Accept "-" for standard input to
305 * simplify scripting a check of the whole database.
307 old_term = strdup((old_term == 0) ? "unknown" : old_term);
309 if (!strcmp(t_opt, "-")) {
311 while (fgets(buffer, sizeof(buffer) - 1, stdin) != 0) {
314 while (isspace(UChar(s[0])))
317 while (t != s && isspace(UChar(t[-1])))
320 if (len_terms + 2 >= max_terms) {
322 all_terms = typeRealloc(char *, max_terms, all_terms);
324 failed("no memory: all_terms");
326 all_terms[len_terms++] = s;
331 while ((t = strtok(s, ",")) != 0) {
333 if (len_terms + 2 >= max_terms) {
335 all_terms = typeRealloc(char *, max_terms, all_terms);
337 failed("no memory: all_terms");
339 all_terms[len_terms++] = strdup(t);
343 all_terms[len_terms++] = strdup(old_term);
345 all_terms[len_terms] = 0;
347 printf("%d term%s:\n", PLURAL(len_terms));
349 for (n = 0; n < len_terms; ++n) {
350 printf(" %d: %s\n", n + 1, all_terms[n]);
356 * If no capability name was selected, use the predefined list of string
359 * TODO: To address the "other" systems which do not follow SVr4,
360 * just use the output from infocmp on $TERM.
363 #if defined(HAVE_CURSES_DATA_BOOLNAMES) || defined(DECL_CURSES_DATA_BOOLNAMES)
364 for (n = 0; strnames[n] != 0; ++n) {
365 if (len_caps + 2 >= max_caps) {
367 all_caps = typeRealloc(char *, max_caps, all_caps);
369 failed("no memory: all_caps");
372 all_caps[len_caps++] = strdup(strnames[n]);
375 all_caps[len_caps++] = strdup("cup");
376 all_caps[len_caps++] = strdup("sgr");
379 all_caps[len_caps] = 0;
381 printf("%d name%s%s\n", PLURAL(len_caps), COLONS(len_caps));
383 for (n = 0; n < len_caps; ++n) {
384 printf(" %d: %s\n", n + 1, all_caps[n]);
389 cap_name = typeMalloc(char *, len_caps);
390 cap_data = typeMalloc(char *, len_caps);
396 for (n = 0; n < max_parms; ++n)
398 use_parms *= (num_parms[n] + 1);
401 for (r_run = 0; r_run < r_opt; ++r_run) {
402 for (t_run = 0; t_run < len_terms; ++t_run) {
405 if (setupterm(all_terms[t_run], fileno(stdout), &errs) != OK) {
406 printf("** skipping %s (errs:%d)\n", all_terms[t_run], errs);
410 * Most of the capabilities have no parameters, e.g., they are
411 * function-keys or simple operations such as clear-display.
412 * Ignore those, since they do not really exercise tparm.
415 for (n = 0; n < len_caps; ++n) {
416 char *value = tigetstr(all_caps[n]);
417 if (relevant(all_caps[n], value)) {
418 cap_name[use_caps] = all_caps[n];
419 cap_data[use_caps] = value;
425 printf("[%d:%d] %d cap%s * %ld param%s \"%s\"\n",
432 memset(all_parms, 0, sizeof(all_parms));
434 /* for each combination of values */
436 for (n_run = 0; n_run < use_caps; ++n_run) {
437 test_tparm(cap_name[n_run], cap_data[n_run], all_parms);
440 while (increment(all_parms, num_parms, len_parms, 0));
442 /* for the given values */
443 for (n_run = 0; n_run < use_caps; ++n_run) {
444 test_tparm(cap_name[n_run], cap_data[n_run], all_parms);
448 del_curterm(cur_term);
450 printf("? no cur_term\n");
456 printf(NUMFORM " total\n", total_tests);
458 printf(NUMFORM " failed\n", total_fails);
459 printf("Characters:\n");
460 printf(NUMFORM " nulls\n", total_nulls);
461 printf(NUMFORM " controls\n", total_ctrls);
462 printf(NUMFORM " printable\n", total_print);
463 printf(NUMFORM " total\n", total_nulls + total_ctrls + total_print);
465 for (n = 0; n < len_caps; ++n) {
470 for (n = 0; n < len_terms; ++n) {
480 ExitProgram(EXIT_SUCCESS);
483 #else /* !HAVE_TIGETSTR */
485 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
487 failed("This program requires the terminfo functions such as tigetstr");
489 #endif /* HAVE_TIGETSTR */