• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /****************************************************************************
2  * Copyright 2020-2022,2023 Thomas E. Dickey                                *
3  *                                                                          *
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:                 *
11  *                                                                          *
12  * The above copyright notice and this permission notice shall be included  *
13  * in all copies or substantial portions of the Software.                   *
14  *                                                                          *
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.                               *
22  *                                                                          *
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       *
26  * authorization.                                                           *
27  ****************************************************************************/
28 
29 /*
30  * Author: Thomas E. Dickey
31  *
32  * $Id: test_tparm.c,v 1.39 2023/11/11 01:00:03 tom Exp $
33  *
34  * Exercise tparm/tiparm, either for all possible capabilities with fixed
35  * parameters, or one capability with specific combinations of parameters.
36  */
37 #define USE_TINFO
38 #include <test.priv.h>
39 
40 #if NCURSES_XNAMES
41 #if HAVE_TERM_ENTRY_H
42 #include <term_entry.h>
43 #else
44 #undef NCURSES_XNAMES
45 #define NCURSES_XNAMES 0
46 #endif
47 #endif
48 
49 #define MAX_PARM 9
50 
51 #define GrowArray(array,limit,length) \
52 	    if (length + 2 >= limit) { \
53 		limit *= 2; \
54 		array = typeRealloc(char *, limit, array); \
55 		if (array == 0) { \
56 		    failed("no memory: " #array); \
57 		} \
58 	    }
59 
60 static GCC_NORETURN void failed(const char *);
61 
62 static void
failed(const char * msg)63 failed(const char *msg)
64 {
65     fprintf(stderr, "%s\n", msg);
66     ExitProgram(EXIT_FAILURE);
67 }
68 
69 #if HAVE_TIGETSTR
70 
71 static int a_opt;
72 static int p_opt;
73 static int v_opt;
74 
75 #if HAVE_TIPARM
76 static int i_opt;
77 #endif
78 
79 #if HAVE_TIPARM_S
80 static int s_opt;
81 #endif
82 
83 /*
84  * Total tests (and failures):
85  */
86 static long total_tests;
87 static long total_fails;
88 
89 /*
90  * Total characters formatted for tputs:
91  */
92 static long total_nulls;
93 static long total_ctrls;
94 static long total_print;
95 
96 static int
output_func(int ch)97 output_func(int ch)
98 {
99     if (ch == 0) {
100 	total_nulls++;
101     } else if (ch < 32 || (ch >= 127 && ch < 160)) {
102 	total_ctrls++;
103     } else {
104 	total_print++;
105     }
106     return ch;
107 }
108 
109 static int
isNumeric(char * source)110 isNumeric(char *source)
111 {
112     char *next = 0;
113     long value = strtol(source, &next, 0);
114     int result = (next == 0 || next == source || *next != '\0') ? 0 : 1;
115     (void) value;
116     return result;
117 }
118 
119 static int
relevant(const char * name,const char * value)120 relevant(const char *name, const char *value)
121 {
122     int code = 1;
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)) {
128 	    if (v_opt > 2)
129 		printf("? %s noparams\n", name);
130 	    code = 0;
131 	}
132     } else {
133 	if (v_opt > 2) {
134 	    printf("? %s %s\n",
135 		   (value == ABSENT_STRING)
136 		   ? "absent"
137 		   : "cancel",
138 		   name);
139 	}
140 	code = 0;
141     }
142     return code;
143 }
144 
145 static int
increment(long * all_parms,int * num_parms,int len_parms,int end_parms)146 increment(long *all_parms, int *num_parms, int len_parms, int end_parms)
147 {
148     int rc = 0;
149     int n;
150 
151     if (len_parms > MAX_PARM)
152 	len_parms = MAX_PARM;
153 
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);
158 	}
159     }
160     for (n = 0; n < len_parms; ++n) {
161 	if (all_parms[n] != 0) {
162 	    rc = 1;
163 	    break;
164 	}
165     }
166     /* return 1 until the vector resets to all 0's */
167     return rc;
168 }
169 
170 /* parse the format string to determine which positional parameters
171  * are assumed to be strings.
172  */
173 #if HAVE_TISCAN_S
174 static int
analyze_format(const char * format,int * mask,char ** p_is_s)175 analyze_format(const char *format, int *mask, char **p_is_s)
176 {
177     int arg_count;
178     int arg_mask;
179     int n;
180     if (tiscan_s(&arg_count, &arg_mask, format) == OK) {
181 	*mask = arg_mask;
182 	for (n = 0; n < MAX_PARM; ++n) {
183 	    static char dummy[1];
184 	    p_is_s[n] = (arg_mask & 1) ? dummy : NULL;
185 	    arg_mask >>= 1;
186 	}
187     } else {
188 	*mask = 0;
189 	arg_count = 0;
190 	for (n = 0; n < MAX_PARM; ++n) {
191 	    p_is_s[n] = NULL;
192 	}
193     }
194     return arg_count;
195 }
196 #elif HAVE__NC_TPARM_ANALYZE
197 extern int _nc_tparm_analyze(TERMINAL *, const char *, char **, int *);
198 
199 static int
analyze_format(const char * format,int * mask,char ** p_is_s)200 analyze_format(const char *format, int *mask, char **p_is_s)
201 {
202     int popcount = 0;
203     int analyzed = _nc_tparm_analyze(cur_term, format, p_is_s, &popcount);
204     int n;
205     if (analyzed < popcount) {
206 	analyzed = popcount;
207     }
208     *mask = 0;
209     for (n = 0; n < MAX_PARM; ++n) {
210 	if (p_is_s[n])
211 	    *mask |= (1 << n);
212     }
213     return analyzed;
214 }
215 #else
216 /* TODO: make this work without direct use of ncurses internals. */
217 static int
analyze_format(const char * format,int * mask,char ** p_is_s)218 analyze_format(const char *format, int *mask, char **p_is_s)
219 {
220     int n;
221     char *filler = strstr(format, "%s");
222     *mask = 0;
223     for (n = 0; n < MAX_PARM; ++n) {
224 	p_is_s[n] = filler;
225     }
226     return n;
227 }
228 #endif
229 
230 #define NumStr(n) use_strings[n] \
231  		  ? (long) (my_intptr_t) (number[n] \
232 		     ? string[n] \
233 		     : NULL) \
234 		  : number[n]
235 
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)
246 
247 static void
test_tparm(const char * name,const char * format,long * number,char ** string)248 test_tparm(const char *name, const char *format, long *number, char **string)
249 {
250     char *use_strings[MAX_PARM];
251     char *result = NULL;
252     int nparam;
253     int mask;
254 
255     nparam = analyze_format(format, &mask, use_strings);
256 #if HAVE_TIPARM_S
257     if (s_opt) {
258 	switch (nparam) {
259 	case 0:
260 	    result = tiparm_s(0, mask, NS_0(format));
261 	    break;
262 	case 1:
263 	    result = tiparm_s(1, mask, NS_1(format));
264 	    break;
265 	case 2:
266 	    result = tiparm_s(2, mask, NS_2(format));
267 	    break;
268 	case 3:
269 	    result = tiparm_s(3, mask, NS_3(format));
270 	    break;
271 	case 4:
272 	    result = tiparm_s(4, mask, NS_4(format));
273 	    break;
274 	case 5:
275 	    result = tiparm_s(5, mask, NS_5(format));
276 	    break;
277 	case 6:
278 	    result = tiparm_s(6, mask, NS_6(format));
279 	    break;
280 	case 7:
281 	    result = tiparm_s(7, mask, NS_7(format));
282 	    break;
283 	case 8:
284 	    result = tiparm_s(8, mask, NS_8(format));
285 	    break;
286 	case 9:
287 	    result = tiparm_s(9, mask, NS_9(format));
288 	    break;
289 	}
290     } else
291 #endif
292 #if HAVE_TIPARM
293     if (i_opt) {
294 	switch (nparam) {
295 	case 0:
296 	    result = tiparm(NS_0(format));
297 	    break;
298 	case 1:
299 	    result = tiparm(NS_1(format));
300 	    break;
301 	case 2:
302 	    result = tiparm(NS_2(format));
303 	    break;
304 	case 3:
305 	    result = tiparm(NS_3(format));
306 	    break;
307 	case 4:
308 	    result = tiparm(NS_4(format));
309 	    break;
310 	case 5:
311 	    result = tiparm(NS_5(format));
312 	    break;
313 	case 6:
314 	    result = tiparm(NS_6(format));
315 	    break;
316 	case 7:
317 	    result = tiparm(NS_7(format));
318 	    break;
319 	case 8:
320 	    result = tiparm(NS_8(format));
321 	    break;
322 	case 9:
323 	    result = tiparm(NS_9(format));
324 	    break;
325 	}
326     } else
327 #endif
328 	result = tparm(NS_9(format));
329     total_tests++;
330     if (result != NULL) {
331 	tputs(result, 1, output_func);
332     } else {
333 	total_fails++;
334     }
335     if (v_opt > 1) {
336 	int n;
337 	printf(".. %3d =", result != 0 ? (int) strlen(result) : -1);
338 	for (n = 0; n < nparam; ++n) {
339 	    if (use_strings[n]) {
340 		if (number[n]) {
341 		    printf(" \"%s\"", string[n]);
342 		} else {
343 		    printf("  ?");
344 		}
345 	    } else {
346 		printf(" %2ld", number[n]);
347 	    }
348 	}
349 	printf(" %s\n", name);
350     }
351 }
352 
353 static void
usage(int ok)354 usage(int ok)
355 {
356     static const char *msg[] =
357     {
358 	"Usage: test_tparm [options] [capability] [value1 [value2 [...]]]"
359 	,""
360 	,"Use tparm/tputs for all distinct combinations of given capability."
361 	,""
362 	,USAGE_COMMON
363 	,"Options:"
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."
368 #if HAVE_TIPARM
369 	," -i       test tiparm rather than tparm"
370 #endif
371 	," -p       test capabilities with no parameters but having padding"
372 	," -r NUM   repeat tests NUM times"
373 #if HAVE_TIPARM_S
374 	," -s       test tiparm_s rather than tparm"
375 #endif
376 	," -v       show values and results"
377     };
378     unsigned n;
379     for (n = 0; n < SIZEOF(msg); ++n) {
380 	fprintf(stderr, "%s\n", msg[n]);
381     }
382     ExitProgram(ok ? EXIT_SUCCESS : EXIT_FAILURE);
383 }
384 
385 #define PLURAL(n) n, (n != 1) ? "s" : ""
386 #define COLONS(n) (n >= 1) ? ":" : ""
387 
388 #define NUMFORM "%10ld"
389 /* *INDENT-OFF* */
VERSION_COMMON()390 VERSION_COMMON()
391 /* *INDENT-ON* */
392 
393 int
394 main(int argc, char *argv[])
395 {
396     int ch;
397     int n;
398     int r_run, t_run, n_run;
399     char *old_term = getenv("TERM");
400     int r_opt = 1;
401     char *t_opt = 0;
402 
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);
407 
408     long all_parms[10];		/* workspace for "-a" option */
409 
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);
413 
414     int use_caps;
415     int max_name = 10;		/* max # of items in cap_name[] */
416     int max_data = 10;		/* max # of items in cap_data[] */
417     char **cap_name;
418     char **cap_data;
419 
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);
424     long use_parms = 1;
425 
426     if (all_caps == 0 || all_terms == 0 || num_parms == 0 || str_parms == 0)
427 	failed("no memory");
428 
429     while ((ch = getopt(argc, argv, OPTS_COMMON "T:aipr:sv")) != -1) {
430 	switch (ch) {
431 	case 'T':
432 	    t_opt = optarg;
433 	    break;
434 	case 'a':
435 	    ++a_opt;
436 	    break;
437 #if HAVE_TIPARM
438 	case 'i':
439 	    ++i_opt;
440 	    break;
441 #endif
442 	case 'p':
443 	    ++p_opt;
444 	    break;
445 	case 'r':
446 	    r_opt = atoi(optarg);
447 	    break;
448 #if HAVE_TIPARM_S
449 	case 's':
450 	    ++s_opt;
451 	    break;
452 #endif
453 	case 'v':
454 	    ++v_opt;
455 	    break;
456 	case OPTS_VERSION:
457 	    show_version(argv);
458 	    ExitProgram(EXIT_SUCCESS);
459 	default:
460 	    usage(ch == OPTS_USAGE);
461 	    /* NOTREACHED */
462 	}
463     }
464 
465     /*
466      * If there is a nonnumeric parameter after the options, use that as the
467      * capability name.
468      */
469     if (optind < argc) {
470 	if (!isNumeric(argv[optind])) {
471 	    all_caps[len_caps++] = strdup(argv[optind++]);
472 	}
473     }
474 
475     /*
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.
479      */
480     while (optind < argc) {
481 	if (isNumeric(argv[optind])) {
482 	    char *dummy = 0;
483 	    long value = strtol(argv[optind], &dummy, 0);
484 	    num_parms[len_parms] = (int) value;
485 	}
486 	str_parms[len_parms] = argv[optind];
487 	++optind;
488 	++len_parms;
489     }
490     for (n = len_parms; n < max_parms; ++n) {
491 	static char dummy[1];
492 	str_parms[n] = dummy;
493     }
494     if (v_opt) {
495 	printf("%d parameter%s%s\n", PLURAL(len_parms), COLONS(len_parms));
496 	if (v_opt > 3) {
497 	    for (n = 0; n < len_parms; ++n) {
498 		printf(" %d: %d (%s)\n", n + 1, num_parms[n], str_parms[n]);
499 	    }
500 	}
501     }
502 
503     /*
504      * Make a list of values for $TERM.  Accept "-" for standard input to
505      * simplify scripting a check of the whole database.
506      */
507     old_term = strdup((old_term == 0) ? "unknown" : old_term);
508     if (t_opt != 0) {
509 	if (!strcmp(t_opt, "-")) {
510 	    char buffer[BUFSIZ];
511 	    while (fgets(buffer, sizeof(buffer) - 1, stdin) != 0) {
512 		char *s = buffer;
513 		char *t;
514 		while (isspace(UChar(s[0])))
515 		    ++s;
516 		t = s + strlen(s);
517 		while (t != s && isspace(UChar(t[-1])))
518 		    *--t = '\0';
519 		s = strdup(s);
520 		if (len_terms + 2 >= max_terms) {
521 		    max_terms *= 2;
522 		    all_terms = typeRealloc(char *, max_terms, all_terms);
523 		    if (all_terms == 0)
524 			failed("no memory: all_terms");
525 		}
526 		all_terms[len_terms++] = s;
527 	    }
528 	} else {
529 	    char *s = t_opt;
530 	    char *t;
531 	    while ((t = strtok(s, ",")) != 0) {
532 		s = 0;
533 		if (len_terms + 2 >= max_terms) {
534 		    max_terms *= 2;
535 		    all_terms = typeRealloc(char *, max_terms, all_terms);
536 		    if (all_terms == 0)
537 			failed("no memory: all_terms");
538 		}
539 		all_terms[len_terms++] = strdup(t);
540 	    }
541 	}
542     } else {
543 	all_terms[len_terms++] = strdup(old_term);
544     }
545     all_terms[len_terms] = 0;
546     if (v_opt) {
547 	printf("%d term%s:\n", PLURAL(len_terms));
548 	if (v_opt > 3) {
549 	    for (n = 0; n < len_terms; ++n) {
550 		printf(" %d: %s\n", n + 1, all_terms[n]);
551 	    }
552 	}
553     }
554 
555     /*
556      * If no capability name was selected, use the predefined list of string
557      * capabilities.
558      *
559      * TODO: To address the "other" systems which do not follow SVr4,
560      * just use the output from infocmp on $TERM.
561      */
562     if (len_caps == 0) {
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]);
567 	}
568 #else
569 	all_caps[len_caps++] = strdup("cup");
570 	all_caps[len_caps++] = strdup("sgr");
571 #endif
572     }
573     std_caps = len_caps;
574     all_caps[len_caps] = 0;
575     if (v_opt) {
576 	printf("%d name%s%s\n", PLURAL(len_caps), COLONS(len_caps));
577 	if (v_opt > 3) {
578 	    for (n = 0; n < len_caps; ++n) {
579 		printf(" %d: %s\n", n + 1, all_caps[n]);
580 	    }
581 	}
582     }
583 
584     cap_name = typeMalloc(char *, (max_name = 1 + len_caps));
585     cap_data = typeMalloc(char *, (max_data = 1 + len_caps));
586 
587     if (r_opt <= 0)
588 	r_opt = 1;
589 
590     if (a_opt) {
591 	for (n = 0; n < max_parms; ++n)
592 	    if (num_parms[n])
593 		use_parms *= (num_parms[n] + 1);
594     }
595 
596     for (r_run = 0; r_run < r_opt; ++r_run) {
597 	for (t_run = 0; t_run < len_terms; ++t_run) {
598 	    int errs;
599 
600 	    if (setupterm(all_terms[t_run], fileno(stdout), &errs) != OK) {
601 		printf("** skipping %s (errs:%d)\n", all_terms[t_run], errs);
602 	    }
603 #if NCURSES_XNAMES
604 	    len_caps = std_caps;
605 	    if (cur_term) {
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));
612 		}
613 	    }
614 #else
615 	    (void) std_caps;
616 #endif
617 
618 	    /*
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.
622 	     */
623 	    use_caps = 0;
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;
629 		    use_caps++;
630 		}
631 	    }
632 
633 	    if (v_opt) {
634 		printf("[%d:%d] %d paramerized cap%s * %ld test-case%s \"%s\"\n",
635 		       r_run + 1, r_opt,
636 		       PLURAL(use_caps),
637 		       PLURAL(use_parms),
638 		       all_terms[t_run]);
639 	    }
640 
641 	    memset(all_parms, 0, sizeof(all_parms));
642 	    if (a_opt) {
643 		/* for each combination of values */
644 		do {
645 		    for (n_run = 0; n_run < use_caps; ++n_run) {
646 			test_tparm(cap_name[n_run],
647 				   cap_data[n_run],
648 				   all_parms,
649 				   str_parms);
650 		    }
651 		}
652 		while (increment(all_parms, num_parms, len_parms, 0));
653 	    } else {
654 		/* for the given values */
655 		for (n_run = 0; n_run < use_caps; ++n_run) {
656 		    test_tparm(cap_name[n_run],
657 			       cap_data[n_run],
658 			       all_parms,
659 			       str_parms);
660 		}
661 	    }
662 #if NCURSES_XNAMES
663 	    for (n = std_caps; n < len_caps; ++n) {
664 		free(all_caps[n]);
665 	    }
666 #endif
667 	    if (cur_term != 0) {
668 		del_curterm(cur_term);
669 	    } else {
670 		printf("? no cur_term\n");
671 	    }
672 	}
673     }
674 
675     printf("Tests:\n");
676     printf(NUMFORM " total\n", total_tests);
677     if (total_fails)
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);
684 #if NO_LEAKS
685     for (n = 0; n < std_caps; ++n) {
686 	free(all_caps[n]);
687     }
688     free(all_caps);
689     free(old_term);
690     for (n = 0; n < len_terms; ++n) {
691 	free(all_terms[n]);
692     }
693     free(all_terms);
694     free(num_parms);
695     free(str_parms);
696     free(cap_name);
697     free(cap_data);
698 #endif
699 
700     ExitProgram(EXIT_SUCCESS);
701 }
702 
703 #else /* !HAVE_TIGETSTR */
704 int
main(void)705 main(void)
706 {
707     failed("This program requires the terminfo functions such as tigetstr");
708 }
709 #endif /* HAVE_TIGETSTR */
710