1 /****************************************************************************
2 * Copyright 2020-2022,2023 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.30 2023/04/15 22:31:47 tom Exp $
34 * Exercise tparm, either for all possible capabilities with fixed parameters,
35 * or one capability with all possible parameters.
37 * TODO: optionally test tiparm
38 * TODO: add checks/logic to handle "%s" in tparm
41 #include <test.priv.h>
45 #include <term_entry.h>
48 #define NCURSES_XNAMES 0
52 #define GrowArray(array,limit,length) \
53 if (length + 2 >= limit) { \
55 array = typeRealloc(char *, limit, array); \
57 failed("no memory: " #array); \
61 static GCC_NORETURN void failed(const char *);
64 failed(const char *msg)
66 fprintf(stderr, "%s\n", msg);
67 ExitProgram(EXIT_FAILURE);
77 * Total tests (and failures):
79 static long total_tests;
80 static long total_fails;
83 * Total characters formatted for tputs:
85 static long total_nulls;
86 static long total_ctrls;
87 static long total_print;
94 } else if (ch < 32 || (ch >= 127 && ch < 160)) {
103 isNumeric(char *source)
106 long value = strtol(source, &next, 0);
107 int result = (next == 0 || next == source || *next != '\0') ? 0 : 1;
113 relevant(const char *name, const char *value)
116 if (VALID_STRING(value)) {
117 if (strstr(value, "%p") == 0
118 && strstr(value, "%d") == 0
119 && strstr(value, "%s") == 0
120 && (!p_opt || strstr(value, "$<") == 0)) {
122 printf("? %s noparams\n", name);
128 (value == ABSENT_STRING)
139 increment(long *all_parms, int *num_parms, int len_parms, int end_parms)
147 if (end_parms < len_parms) {
148 if (all_parms[end_parms]++ >= num_parms[end_parms]) {
149 all_parms[end_parms] = 0;
150 increment(all_parms, num_parms, len_parms, end_parms + 1);
153 for (n = 0; n < len_parms; ++n) {
154 if (all_parms[n] != 0) {
159 /* return 1 until the vector resets to all 0's */
163 /* parse the format string to determine which positional parameters
164 * are assumed to be strings.
166 #if HAVE__NC_TPARM_ANALYZE
167 extern int _nc_tparm_analyze(TERMINAL *, const char *, char **, int *);
170 analyze_format(const char *format, char **p_is_s)
173 int analyzed = _nc_tparm_analyze(cur_term, format, p_is_s, &popcount);
174 if (analyzed < popcount) {
180 /* TODO: make this work without direct use of ncurses internals. */
182 analyze_format(const char *format, char **p_is_s)
185 char *filler = strstr(format, "%s");
186 for (n = 0; n < 9; ++n) {
193 #define NumStr(n) use_strings[n] \
194 ? (long) (number[n] \
200 test_tparm(const char *name, const char *format, long *number, char **string)
202 char *use_strings[9];
206 nparam = analyze_format(format, use_strings);
207 result = tparm(format,
218 if (result != NULL) {
219 tputs(result, 1, output_func);
225 printf(".. %3d =", result != 0 ? (int) strlen(result) : -1);
226 for (n = 0; n < nparam; ++n) {
227 if (use_strings[n]) {
229 printf(" \"%s\"", string[n]);
234 printf(" %2ld", number[n]);
237 printf(" %s\n", name);
244 static const char *msg[] =
246 "Usage: test_tparm [options] [capability] [value1 [value2 [...]]]"
248 ,"Use tparm/tputs for all distinct combinations of given capability."
252 ," -T TERM override $TERM; this may be a comma-separated list or \"-\""
253 ," to read a list from standard-input"
254 ," -a test all combinations of parameters"
255 ," [value1...] forms a vector of maximum parameter-values."
256 ," -p test capabilities with no parameters but having padding"
257 ," -r NUM repeat tests NUM times"
258 ," -v show values and results"
261 for (n = 0; n < SIZEOF(msg); ++n) {
262 fprintf(stderr, "%s\n", msg[n]);
264 ExitProgram(ok ? EXIT_SUCCESS : EXIT_FAILURE);
267 #define PLURAL(n) n, (n != 1) ? "s" : ""
268 #define COLONS(n) (n >= 1) ? ":" : ""
270 #define NUMFORM "%10ld"
276 main(int argc, char *argv[])
280 int r_run, t_run, n_run;
281 char *old_term = getenv("TERM");
285 int std_caps = 0; /* predefine items in all_caps[] */
286 int len_caps = 0; /* cur # of items in all_caps[] */
287 int max_caps = 10; /* max # of items in all_caps[] */
288 char **all_caps = typeCalloc(char *, max_caps);
290 long all_parms[10]; /* workspace for "-a" option */
292 int len_terms = 0; /* cur # of items in all_terms[] */
293 int max_terms = 10; /* max # of items in all_terms[] */
294 char **all_terms = typeCalloc(char *, max_terms);
300 int len_parms = 0; /* cur # of items in num_parms[], str_parms[] */
301 int max_parms = argc + 10; /* max # of items in num_parms[], str_parms[] */
302 int *num_parms = typeCalloc(int, max_parms);
303 char **str_parms = typeCalloc(char *, max_parms);
306 if (all_caps == 0 || all_terms == 0 || num_parms == 0 || str_parms == 0)
309 while ((ch = getopt(argc, argv, OPTS_COMMON "T:apr:v")) != -1) {
321 r_opt = atoi(optarg);
328 ExitProgram(EXIT_SUCCESS);
330 usage(ch == OPTS_USAGE);
336 * If there is a nonnumeric parameter after the options, use that as the
340 if (!isNumeric(argv[optind])) {
341 all_caps[len_caps++] = strdup(argv[optind++]);
346 * Any remaining arguments must be possible parameter values. If numeric,
347 * and "-a" is not set, use those as the actual values for which the
348 * capabilities are tested.
350 while (optind < argc) {
351 if (isNumeric(argv[optind])) {
353 long value = strtol(argv[optind], &dummy, 0);
354 num_parms[len_parms] = (int) value;
356 str_parms[len_parms] = argv[optind];
360 for (n = len_parms; n < max_parms; ++n) {
361 static char dummy[1];
362 str_parms[n] = dummy;
365 printf("%d parameter%s%s\n", PLURAL(len_parms), COLONS(len_parms));
367 for (n = 0; n < len_parms; ++n) {
368 printf(" %d: %d (%s)\n", n + 1, num_parms[n], str_parms[n]);
374 * Make a list of values for $TERM. Accept "-" for standard input to
375 * simplify scripting a check of the whole database.
377 old_term = strdup((old_term == 0) ? "unknown" : old_term);
379 if (!strcmp(t_opt, "-")) {
381 while (fgets(buffer, sizeof(buffer) - 1, stdin) != 0) {
384 while (isspace(UChar(s[0])))
387 while (t != s && isspace(UChar(t[-1])))
390 if (len_terms + 2 >= max_terms) {
392 all_terms = typeRealloc(char *, max_terms, all_terms);
394 failed("no memory: all_terms");
396 all_terms[len_terms++] = s;
401 while ((t = strtok(s, ",")) != 0) {
403 if (len_terms + 2 >= max_terms) {
405 all_terms = typeRealloc(char *, max_terms, all_terms);
407 failed("no memory: all_terms");
409 all_terms[len_terms++] = strdup(t);
413 all_terms[len_terms++] = strdup(old_term);
415 all_terms[len_terms] = 0;
417 printf("%d term%s:\n", PLURAL(len_terms));
419 for (n = 0; n < len_terms; ++n) {
420 printf(" %d: %s\n", n + 1, all_terms[n]);
426 * If no capability name was selected, use the predefined list of string
429 * TODO: To address the "other" systems which do not follow SVr4,
430 * just use the output from infocmp on $TERM.
433 #if defined(HAVE_CURSES_DATA_BOOLNAMES) || defined(DECL_CURSES_DATA_BOOLNAMES)
434 for (n = 0; strnames[n] != 0; ++n) {
435 GrowArray(all_caps, max_caps, len_caps);
436 all_caps[len_caps++] = strdup(strnames[n]);
439 all_caps[len_caps++] = strdup("cup");
440 all_caps[len_caps++] = strdup("sgr");
444 all_caps[len_caps] = 0;
446 printf("%d name%s%s\n", PLURAL(len_caps), COLONS(len_caps));
448 for (n = 0; n < len_caps; ++n) {
449 printf(" %d: %s\n", n + 1, all_caps[n]);
454 cap_name = typeMalloc(char *, 1 + len_caps);
455 cap_data = typeMalloc(char *, 1 + len_caps);
461 for (n = 0; n < max_parms; ++n)
463 use_parms *= (num_parms[n] + 1);
466 for (r_run = 0; r_run < r_opt; ++r_run) {
467 for (t_run = 0; t_run < len_terms; ++t_run) {
470 if (setupterm(all_terms[t_run], fileno(stdout), &errs) != OK) {
471 printf("** skipping %s (errs:%d)\n", all_terms[t_run], errs);
476 TERMTYPE *term = (TERMTYPE *) cur_term;
477 for (n = STRCOUNT; n < NUM_STRINGS(term); ++n) {
478 GrowArray(all_caps, max_caps, len_caps);
479 all_caps[len_caps++] = strdup(ExtStrname(term, (int) n, strnames));
485 * Most of the capabilities have no parameters, e.g., they are
486 * function-keys or simple operations such as clear-display.
487 * Ignore those, since they do not really exercise tparm.
490 for (n = 0; n < len_caps; ++n) {
491 char *value = tigetstr(all_caps[n]);
492 if (relevant(all_caps[n], value)) {
493 cap_name[use_caps] = all_caps[n];
494 cap_data[use_caps] = value;
500 printf("[%d:%d] %d paramerized cap%s * %ld test-case%s \"%s\"\n",
507 memset(all_parms, 0, sizeof(all_parms));
509 /* for each combination of values */
511 for (n_run = 0; n_run < use_caps; ++n_run) {
512 test_tparm(cap_name[n_run],
518 while (increment(all_parms, num_parms, len_parms, 0));
520 /* for the given values */
521 for (n_run = 0; n_run < use_caps; ++n_run) {
522 test_tparm(cap_name[n_run],
529 for (n = std_caps; n < len_caps; ++n) {
534 del_curterm(cur_term);
536 printf("? no cur_term\n");
542 printf(NUMFORM " total\n", total_tests);
544 printf(NUMFORM " failed\n", total_fails);
545 printf("Characters:\n");
546 printf(NUMFORM " nulls\n", total_nulls);
547 printf(NUMFORM " controls\n", total_ctrls);
548 printf(NUMFORM " printable\n", total_print);
549 printf(NUMFORM " total\n", total_nulls + total_ctrls + total_print);
551 for (n = 0; n < std_caps; ++n) {
556 for (n = 0; n < len_terms; ++n) {
566 ExitProgram(EXIT_SUCCESS);
569 #else /* !HAVE_TIGETSTR */
573 failed("This program requires the terminfo functions such as tigetstr");
575 #endif /* HAVE_TIGETSTR */