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.37 2023/04/28 23:12:00 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);
78 * Total tests (and failures):
80 static long total_tests;
81 static long total_fails;
84 * Total characters formatted for tputs:
86 static long total_nulls;
87 static long total_ctrls;
88 static long total_print;
95 } else if (ch < 32 || (ch >= 127 && ch < 160)) {
104 isNumeric(char *source)
107 long value = strtol(source, &next, 0);
108 int result = (next == 0 || next == source || *next != '\0') ? 0 : 1;
114 relevant(const char *name, const char *value)
117 if (VALID_STRING(value)) {
118 if (strstr(value, "%p") == 0
119 && strstr(value, "%d") == 0
120 && strstr(value, "%s") == 0
121 && (!p_opt || strstr(value, "$<") == 0)) {
123 printf("? %s noparams\n", name);
129 (value == ABSENT_STRING)
140 increment(long *all_parms, int *num_parms, int len_parms, int end_parms)
145 if (len_parms > MAX_PARM)
146 len_parms = MAX_PARM;
148 if (end_parms < len_parms) {
149 if (all_parms[end_parms]++ >= num_parms[end_parms]) {
150 all_parms[end_parms] = 0;
151 increment(all_parms, num_parms, len_parms, end_parms + 1);
154 for (n = 0; n < len_parms; ++n) {
155 if (all_parms[n] != 0) {
160 /* return 1 until the vector resets to all 0's */
164 /* parse the format string to determine which positional parameters
165 * are assumed to be strings.
169 analyze_format(const char *format, int *mask, char **p_is_s)
174 if (tiscan_s(&arg_count, &arg_mask, format) == OK) {
176 for (n = 0; n < MAX_PARM; ++n) {
177 static char dummy[1];
178 p_is_s[n] = (arg_mask & 1) ? dummy : NULL;
184 for (n = 0; n < MAX_PARM; ++n) {
190 #elif HAVE__NC_TPARM_ANALYZE
191 extern int _nc_tparm_analyze(TERMINAL *, const char *, char **, int *);
194 analyze_format(const char *format, int *mask, char **p_is_s)
197 int analyzed = _nc_tparm_analyze(cur_term, format, p_is_s, &popcount);
199 if (analyzed < popcount) {
203 for (n = 0; n < MAX_PARM; ++n) {
210 /* TODO: make this work without direct use of ncurses internals. */
212 analyze_format(const char *format, int *mask, char **p_is_s)
215 char *filler = strstr(format, "%s");
217 for (n = 0; n < MAX_PARM; ++n) {
224 #define NumStr(n) use_strings[n] \
225 ? (long) (my_intptr_t) (number[n] \
230 #define NS_0(fmt) fmt
231 #define NS_1(fmt) NS_0(fmt), NumStr(0)
232 #define NS_2(fmt) NS_1(fmt), NumStr(1)
233 #define NS_3(fmt) NS_2(fmt), NumStr(2)
234 #define NS_4(fmt) NS_3(fmt), NumStr(3)
235 #define NS_5(fmt) NS_4(fmt), NumStr(4)
236 #define NS_6(fmt) NS_5(fmt), NumStr(5)
237 #define NS_7(fmt) NS_6(fmt), NumStr(6)
238 #define NS_8(fmt) NS_7(fmt), NumStr(7)
239 #define NS_9(fmt) NS_8(fmt), NumStr(8)
242 test_tparm(const char *name, const char *format, long *number, char **string)
244 char *use_strings[MAX_PARM];
249 nparam = analyze_format(format, &mask, use_strings);
254 result = tiparm_s(0, mask, NS_0(format));
257 result = tiparm_s(1, mask, NS_1(format));
260 result = tiparm_s(2, mask, NS_2(format));
263 result = tiparm_s(3, mask, NS_3(format));
266 result = tiparm_s(4, mask, NS_4(format));
269 result = tiparm_s(5, mask, NS_5(format));
272 result = tiparm_s(6, mask, NS_6(format));
275 result = tiparm_s(7, mask, NS_7(format));
278 result = tiparm_s(8, mask, NS_8(format));
281 result = tiparm_s(9, mask, NS_9(format));
290 result = tiparm(NS_0(format));
293 result = tiparm(NS_1(format));
296 result = tiparm(NS_2(format));
299 result = tiparm(NS_3(format));
302 result = tiparm(NS_4(format));
305 result = tiparm(NS_5(format));
308 result = tiparm(NS_6(format));
311 result = tiparm(NS_7(format));
314 result = tiparm(NS_8(format));
317 result = tiparm(NS_9(format));
322 result = tparm(NS_9(format));
324 if (result != NULL) {
325 tputs(result, 1, output_func);
331 printf(".. %3d =", result != 0 ? (int) strlen(result) : -1);
332 for (n = 0; n < nparam; ++n) {
333 if (use_strings[n]) {
335 printf(" \"%s\"", string[n]);
340 printf(" %2ld", number[n]);
343 printf(" %s\n", name);
350 static const char *msg[] =
352 "Usage: test_tparm [options] [capability] [value1 [value2 [...]]]"
354 ,"Use tparm/tputs for all distinct combinations of given capability."
358 ," -T TERM override $TERM; this may be a comma-separated list or \"-\""
359 ," to read a list from standard-input"
360 ," -a test all combinations of parameters"
361 ," [value1...] forms a vector of maximum parameter-values."
363 ," -i test tiparm rather than tparm"
365 ," -p test capabilities with no parameters but having padding"
366 ," -r NUM repeat tests NUM times"
368 ," -s test tiparm_s rather than tparm"
370 ," -v show values and results"
373 for (n = 0; n < SIZEOF(msg); ++n) {
374 fprintf(stderr, "%s\n", msg[n]);
376 ExitProgram(ok ? EXIT_SUCCESS : EXIT_FAILURE);
379 #define PLURAL(n) n, (n != 1) ? "s" : ""
380 #define COLONS(n) (n >= 1) ? ":" : ""
382 #define NUMFORM "%10ld"
388 main(int argc, char *argv[])
392 int r_run, t_run, n_run;
393 char *old_term = getenv("TERM");
397 int std_caps = 0; /* predefine items in all_caps[] */
398 int len_caps = 0; /* cur # of items in all_caps[] */
399 int max_caps = 10; /* max # of items in all_caps[] */
400 char **all_caps = typeCalloc(char *, max_caps);
402 long all_parms[10]; /* workspace for "-a" option */
404 int len_terms = 0; /* cur # of items in all_terms[] */
405 int max_terms = 10; /* max # of items in all_terms[] */
406 char **all_terms = typeCalloc(char *, max_terms);
409 int max_name = 10; /* max # of items in cap_name[] */
410 int max_data = 10; /* max # of items in cap_data[] */
414 int len_parms = 0; /* cur # of items in num_parms[], str_parms[] */
415 int max_parms = argc + 10; /* max # of items in num_parms[], str_parms[] */
416 int *num_parms = typeCalloc(int, max_parms);
417 char **str_parms = typeCalloc(char *, max_parms);
420 if (all_caps == 0 || all_terms == 0 || num_parms == 0 || str_parms == 0)
423 while ((ch = getopt(argc, argv, OPTS_COMMON "T:aipr:sv")) != -1) {
440 r_opt = atoi(optarg);
452 ExitProgram(EXIT_SUCCESS);
454 usage(ch == OPTS_USAGE);
460 * If there is a nonnumeric parameter after the options, use that as the
464 if (!isNumeric(argv[optind])) {
465 all_caps[len_caps++] = strdup(argv[optind++]);
470 * Any remaining arguments must be possible parameter values. If numeric,
471 * and "-a" is not set, use those as the actual values for which the
472 * capabilities are tested.
474 while (optind < argc) {
475 if (isNumeric(argv[optind])) {
477 long value = strtol(argv[optind], &dummy, 0);
478 num_parms[len_parms] = (int) value;
480 str_parms[len_parms] = argv[optind];
484 for (n = len_parms; n < max_parms; ++n) {
485 static char dummy[1];
486 str_parms[n] = dummy;
489 printf("%d parameter%s%s\n", PLURAL(len_parms), COLONS(len_parms));
491 for (n = 0; n < len_parms; ++n) {
492 printf(" %d: %d (%s)\n", n + 1, num_parms[n], str_parms[n]);
498 * Make a list of values for $TERM. Accept "-" for standard input to
499 * simplify scripting a check of the whole database.
501 old_term = strdup((old_term == 0) ? "unknown" : old_term);
503 if (!strcmp(t_opt, "-")) {
505 while (fgets(buffer, sizeof(buffer) - 1, stdin) != 0) {
508 while (isspace(UChar(s[0])))
511 while (t != s && isspace(UChar(t[-1])))
514 if (len_terms + 2 >= max_terms) {
516 all_terms = typeRealloc(char *, max_terms, all_terms);
518 failed("no memory: all_terms");
520 all_terms[len_terms++] = s;
525 while ((t = strtok(s, ",")) != 0) {
527 if (len_terms + 2 >= max_terms) {
529 all_terms = typeRealloc(char *, max_terms, all_terms);
531 failed("no memory: all_terms");
533 all_terms[len_terms++] = strdup(t);
537 all_terms[len_terms++] = strdup(old_term);
539 all_terms[len_terms] = 0;
541 printf("%d term%s:\n", PLURAL(len_terms));
543 for (n = 0; n < len_terms; ++n) {
544 printf(" %d: %s\n", n + 1, all_terms[n]);
550 * If no capability name was selected, use the predefined list of string
553 * TODO: To address the "other" systems which do not follow SVr4,
554 * just use the output from infocmp on $TERM.
557 #if defined(HAVE_CURSES_DATA_BOOLNAMES) || defined(DECL_CURSES_DATA_BOOLNAMES)
558 for (n = 0; strnames[n] != 0; ++n) {
559 GrowArray(all_caps, max_caps, len_caps);
560 all_caps[len_caps++] = strdup(strnames[n]);
563 all_caps[len_caps++] = strdup("cup");
564 all_caps[len_caps++] = strdup("sgr");
568 all_caps[len_caps] = 0;
570 printf("%d name%s%s\n", PLURAL(len_caps), COLONS(len_caps));
572 for (n = 0; n < len_caps; ++n) {
573 printf(" %d: %s\n", n + 1, all_caps[n]);
578 cap_name = typeMalloc(char *, (max_name = 1 + len_caps));
579 cap_data = typeMalloc(char *, (max_data = 1 + len_caps));
585 for (n = 0; n < max_parms; ++n)
587 use_parms *= (num_parms[n] + 1);
590 for (r_run = 0; r_run < r_opt; ++r_run) {
591 for (t_run = 0; t_run < len_terms; ++t_run) {
594 if (setupterm(all_terms[t_run], fileno(stdout), &errs) != OK) {
595 printf("** skipping %s (errs:%d)\n", all_terms[t_run], errs);
600 TERMTYPE *term = (TERMTYPE *) cur_term;
601 for (n = STRCOUNT; n < NUM_STRINGS(term); ++n) {
602 GrowArray(all_caps, max_caps, len_caps);
603 GrowArray(cap_name, max_name, len_caps);
604 GrowArray(cap_data, max_data, len_caps);
605 all_caps[len_caps++] = strdup(ExtStrname(term, (int) n, strnames));
611 * Most of the capabilities have no parameters, e.g., they are
612 * function-keys or simple operations such as clear-display.
613 * Ignore those, since they do not really exercise tparm.
616 for (n = 0; n < len_caps; ++n) {
617 char *value = tigetstr(all_caps[n]);
618 if (relevant(all_caps[n], value)) {
619 cap_name[use_caps] = all_caps[n];
620 cap_data[use_caps] = value;
626 printf("[%d:%d] %d paramerized cap%s * %ld test-case%s \"%s\"\n",
633 memset(all_parms, 0, sizeof(all_parms));
635 /* for each combination of values */
637 for (n_run = 0; n_run < use_caps; ++n_run) {
638 test_tparm(cap_name[n_run],
644 while (increment(all_parms, num_parms, len_parms, 0));
646 /* for the given values */
647 for (n_run = 0; n_run < use_caps; ++n_run) {
648 test_tparm(cap_name[n_run],
655 for (n = std_caps; n < len_caps; ++n) {
660 del_curterm(cur_term);
662 printf("? no cur_term\n");
668 printf(NUMFORM " total\n", total_tests);
670 printf(NUMFORM " failed\n", total_fails);
671 printf("Characters:\n");
672 printf(NUMFORM " nulls\n", total_nulls);
673 printf(NUMFORM " controls\n", total_ctrls);
674 printf(NUMFORM " printable\n", total_print);
675 printf(NUMFORM " total\n", total_nulls + total_ctrls + total_print);
677 for (n = 0; n < std_caps; ++n) {
682 for (n = 0; n < len_terms; ++n) {
692 ExitProgram(EXIT_SUCCESS);
695 #else /* !HAVE_TIGETSTR */
699 failed("This program requires the terminfo functions such as tigetstr");
701 #endif /* HAVE_TIGETSTR */