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.39 2023/11/11 01:00:03 tom Exp $
34 * Exercise tparm/tiparm, either for all possible capabilities with fixed
35 * parameters, or one capability with specific combinations of parameters.
38 #include <test.priv.h>
42 #include <term_entry.h>
45 #define NCURSES_XNAMES 0
51 #define GrowArray(array,limit,length) \
52 if (length + 2 >= limit) { \
54 array = typeRealloc(char *, limit, array); \
56 failed("no memory: " #array); \
60 static GCC_NORETURN void failed(const char *);
63 failed(const char *msg)
65 fprintf(stderr, "%s\n", msg);
66 ExitProgram(EXIT_FAILURE);
84 * Total tests (and failures):
86 static long total_tests;
87 static long total_fails;
90 * Total characters formatted for tputs:
92 static long total_nulls;
93 static long total_ctrls;
94 static long total_print;
101 } else if (ch < 32 || (ch >= 127 && ch < 160)) {
110 isNumeric(char *source)
113 long value = strtol(source, &next, 0);
114 int result = (next == 0 || next == source || *next != '\0') ? 0 : 1;
120 relevant(const char *name, const char *value)
123 if (VALID_STRING(value)) {
124 if (strstr(value, "%p") == 0
125 && strstr(value, "%d") == 0
126 && strstr(value, "%s") == 0
127 && (!p_opt || strstr(value, "$<") == 0)) {
129 printf("? %s noparams\n", name);
135 (value == ABSENT_STRING)
146 increment(long *all_parms, int *num_parms, int len_parms, int end_parms)
151 if (len_parms > MAX_PARM)
152 len_parms = MAX_PARM;
154 if (end_parms < len_parms) {
155 if (all_parms[end_parms]++ >= num_parms[end_parms]) {
156 all_parms[end_parms] = 0;
157 increment(all_parms, num_parms, len_parms, end_parms + 1);
160 for (n = 0; n < len_parms; ++n) {
161 if (all_parms[n] != 0) {
166 /* return 1 until the vector resets to all 0's */
170 /* parse the format string to determine which positional parameters
171 * are assumed to be strings.
175 analyze_format(const char *format, int *mask, char **p_is_s)
180 if (tiscan_s(&arg_count, &arg_mask, format) == OK) {
182 for (n = 0; n < MAX_PARM; ++n) {
183 static char dummy[1];
184 p_is_s[n] = (arg_mask & 1) ? dummy : NULL;
190 for (n = 0; n < MAX_PARM; ++n) {
196 #elif HAVE__NC_TPARM_ANALYZE
197 extern int _nc_tparm_analyze(TERMINAL *, const char *, char **, int *);
200 analyze_format(const char *format, int *mask, char **p_is_s)
203 int analyzed = _nc_tparm_analyze(cur_term, format, p_is_s, &popcount);
205 if (analyzed < popcount) {
209 for (n = 0; n < MAX_PARM; ++n) {
216 /* TODO: make this work without direct use of ncurses internals. */
218 analyze_format(const char *format, int *mask, char **p_is_s)
221 char *filler = strstr(format, "%s");
223 for (n = 0; n < MAX_PARM; ++n) {
230 #define NumStr(n) use_strings[n] \
231 ? (long) (my_intptr_t) (number[n] \
236 #define NS_0(fmt) fmt
237 #define NS_1(fmt) NS_0(fmt), NumStr(0)
238 #define NS_2(fmt) NS_1(fmt), NumStr(1)
239 #define NS_3(fmt) NS_2(fmt), NumStr(2)
240 #define NS_4(fmt) NS_3(fmt), NumStr(3)
241 #define NS_5(fmt) NS_4(fmt), NumStr(4)
242 #define NS_6(fmt) NS_5(fmt), NumStr(5)
243 #define NS_7(fmt) NS_6(fmt), NumStr(6)
244 #define NS_8(fmt) NS_7(fmt), NumStr(7)
245 #define NS_9(fmt) NS_8(fmt), NumStr(8)
248 test_tparm(const char *name, const char *format, long *number, char **string)
250 char *use_strings[MAX_PARM];
255 nparam = analyze_format(format, &mask, use_strings);
260 result = tiparm_s(0, mask, NS_0(format));
263 result = tiparm_s(1, mask, NS_1(format));
266 result = tiparm_s(2, mask, NS_2(format));
269 result = tiparm_s(3, mask, NS_3(format));
272 result = tiparm_s(4, mask, NS_4(format));
275 result = tiparm_s(5, mask, NS_5(format));
278 result = tiparm_s(6, mask, NS_6(format));
281 result = tiparm_s(7, mask, NS_7(format));
284 result = tiparm_s(8, mask, NS_8(format));
287 result = tiparm_s(9, mask, NS_9(format));
296 result = tiparm(NS_0(format));
299 result = tiparm(NS_1(format));
302 result = tiparm(NS_2(format));
305 result = tiparm(NS_3(format));
308 result = tiparm(NS_4(format));
311 result = tiparm(NS_5(format));
314 result = tiparm(NS_6(format));
317 result = tiparm(NS_7(format));
320 result = tiparm(NS_8(format));
323 result = tiparm(NS_9(format));
328 result = tparm(NS_9(format));
330 if (result != NULL) {
331 tputs(result, 1, output_func);
337 printf(".. %3d =", result != 0 ? (int) strlen(result) : -1);
338 for (n = 0; n < nparam; ++n) {
339 if (use_strings[n]) {
341 printf(" \"%s\"", string[n]);
346 printf(" %2ld", number[n]);
349 printf(" %s\n", name);
356 static const char *msg[] =
358 "Usage: test_tparm [options] [capability] [value1 [value2 [...]]]"
360 ,"Use tparm/tputs for all distinct combinations of given capability."
364 ," -T TERM override $TERM; this may be a comma-separated list or \"-\""
365 ," to read a list from standard-input"
366 ," -a test all combinations of parameters"
367 ," [value1...] forms a vector of maximum parameter-values."
369 ," -i test tiparm rather than tparm"
371 ," -p test capabilities with no parameters but having padding"
372 ," -r NUM repeat tests NUM times"
374 ," -s test tiparm_s rather than tparm"
376 ," -v show values and results"
379 for (n = 0; n < SIZEOF(msg); ++n) {
380 fprintf(stderr, "%s\n", msg[n]);
382 ExitProgram(ok ? EXIT_SUCCESS : EXIT_FAILURE);
385 #define PLURAL(n) n, (n != 1) ? "s" : ""
386 #define COLONS(n) (n >= 1) ? ":" : ""
388 #define NUMFORM "%10ld"
394 main(int argc, char *argv[])
398 int r_run, t_run, n_run;
399 char *old_term = getenv("TERM");
403 int std_caps = 0; /* predefine items in all_caps[] */
404 int len_caps = 0; /* cur # of items in all_caps[] */
405 int max_caps = 10; /* max # of items in all_caps[] */
406 char **all_caps = typeCalloc(char *, max_caps);
408 long all_parms[10]; /* workspace for "-a" option */
410 int len_terms = 0; /* cur # of items in all_terms[] */
411 int max_terms = 10; /* max # of items in all_terms[] */
412 char **all_terms = typeCalloc(char *, max_terms);
415 int max_name = 10; /* max # of items in cap_name[] */
416 int max_data = 10; /* max # of items in cap_data[] */
420 int len_parms = 0; /* cur # of items in num_parms[], str_parms[] */
421 int max_parms = argc + 10; /* max # of items in num_parms[], str_parms[] */
422 int *num_parms = typeCalloc(int, max_parms);
423 char **str_parms = typeCalloc(char *, max_parms);
426 if (all_caps == 0 || all_terms == 0 || num_parms == 0 || str_parms == 0)
429 while ((ch = getopt(argc, argv, OPTS_COMMON "T:aipr:sv")) != -1) {
446 r_opt = atoi(optarg);
458 ExitProgram(EXIT_SUCCESS);
460 usage(ch == OPTS_USAGE);
466 * If there is a nonnumeric parameter after the options, use that as the
470 if (!isNumeric(argv[optind])) {
471 all_caps[len_caps++] = strdup(argv[optind++]);
476 * Any remaining arguments must be possible parameter values. If numeric,
477 * and "-a" is not set, use those as the actual values for which the
478 * capabilities are tested.
480 while (optind < argc) {
481 if (isNumeric(argv[optind])) {
483 long value = strtol(argv[optind], &dummy, 0);
484 num_parms[len_parms] = (int) value;
486 str_parms[len_parms] = argv[optind];
490 for (n = len_parms; n < max_parms; ++n) {
491 static char dummy[1];
492 str_parms[n] = dummy;
495 printf("%d parameter%s%s\n", PLURAL(len_parms), COLONS(len_parms));
497 for (n = 0; n < len_parms; ++n) {
498 printf(" %d: %d (%s)\n", n + 1, num_parms[n], str_parms[n]);
504 * Make a list of values for $TERM. Accept "-" for standard input to
505 * simplify scripting a check of the whole database.
507 old_term = strdup((old_term == 0) ? "unknown" : old_term);
509 if (!strcmp(t_opt, "-")) {
511 while (fgets(buffer, sizeof(buffer) - 1, stdin) != 0) {
514 while (isspace(UChar(s[0])))
517 while (t != s && isspace(UChar(t[-1])))
520 if (len_terms + 2 >= max_terms) {
522 all_terms = typeRealloc(char *, max_terms, all_terms);
524 failed("no memory: all_terms");
526 all_terms[len_terms++] = s;
531 while ((t = strtok(s, ",")) != 0) {
533 if (len_terms + 2 >= max_terms) {
535 all_terms = typeRealloc(char *, max_terms, all_terms);
537 failed("no memory: all_terms");
539 all_terms[len_terms++] = strdup(t);
543 all_terms[len_terms++] = strdup(old_term);
545 all_terms[len_terms] = 0;
547 printf("%d term%s:\n", PLURAL(len_terms));
549 for (n = 0; n < len_terms; ++n) {
550 printf(" %d: %s\n", n + 1, all_terms[n]);
556 * If no capability name was selected, use the predefined list of string
559 * TODO: To address the "other" systems which do not follow SVr4,
560 * just use the output from infocmp on $TERM.
563 #if defined(HAVE_CURSES_DATA_BOOLNAMES) || defined(DECL_CURSES_DATA_BOOLNAMES)
564 for (n = 0; strnames[n] != 0; ++n) {
565 GrowArray(all_caps, max_caps, len_caps);
566 all_caps[len_caps++] = strdup(strnames[n]);
569 all_caps[len_caps++] = strdup("cup");
570 all_caps[len_caps++] = strdup("sgr");
574 all_caps[len_caps] = 0;
576 printf("%d name%s%s\n", PLURAL(len_caps), COLONS(len_caps));
578 for (n = 0; n < len_caps; ++n) {
579 printf(" %d: %s\n", n + 1, all_caps[n]);
584 cap_name = typeMalloc(char *, (max_name = 1 + len_caps));
585 cap_data = typeMalloc(char *, (max_data = 1 + len_caps));
591 for (n = 0; n < max_parms; ++n)
593 use_parms *= (num_parms[n] + 1);
596 for (r_run = 0; r_run < r_opt; ++r_run) {
597 for (t_run = 0; t_run < len_terms; ++t_run) {
600 if (setupterm(all_terms[t_run], fileno(stdout), &errs) != OK) {
601 printf("** skipping %s (errs:%d)\n", all_terms[t_run], errs);
606 TERMTYPE *term = (TERMTYPE *) cur_term;
607 for (n = STRCOUNT; n < NUM_STRINGS(term); ++n) {
608 GrowArray(all_caps, max_caps, len_caps);
609 GrowArray(cap_name, max_name, len_caps);
610 GrowArray(cap_data, max_data, len_caps);
611 all_caps[len_caps++] = strdup(ExtStrname(term, (int) n, strnames));
619 * Most of the capabilities have no parameters, e.g., they are
620 * function-keys or simple operations such as clear-display.
621 * Ignore those, since they do not really exercise tparm.
624 for (n = 0; n < len_caps; ++n) {
625 char *value = tigetstr(all_caps[n]);
626 if (relevant(all_caps[n], value)) {
627 cap_name[use_caps] = all_caps[n];
628 cap_data[use_caps] = value;
634 printf("[%d:%d] %d paramerized cap%s * %ld test-case%s \"%s\"\n",
641 memset(all_parms, 0, sizeof(all_parms));
643 /* for each combination of values */
645 for (n_run = 0; n_run < use_caps; ++n_run) {
646 test_tparm(cap_name[n_run],
652 while (increment(all_parms, num_parms, len_parms, 0));
654 /* for the given values */
655 for (n_run = 0; n_run < use_caps; ++n_run) {
656 test_tparm(cap_name[n_run],
663 for (n = std_caps; n < len_caps; ++n) {
668 del_curterm(cur_term);
670 printf("? no cur_term\n");
676 printf(NUMFORM " total\n", total_tests);
678 printf(NUMFORM " failed\n", total_fails);
679 printf("Characters:\n");
680 printf(NUMFORM " nulls\n", total_nulls);
681 printf(NUMFORM " controls\n", total_ctrls);
682 printf(NUMFORM " printable\n", total_print);
683 printf(NUMFORM " total\n", total_nulls + total_ctrls + total_print);
685 for (n = 0; n < std_caps; ++n) {
690 for (n = 0; n < len_terms; ++n) {
700 ExitProgram(EXIT_SUCCESS);
703 #else /* !HAVE_TIGETSTR */
707 failed("This program requires the terminfo functions such as tigetstr");
709 #endif /* HAVE_TIGETSTR */