]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/test_tparm.c
ncurses 6.4 - patch 20230429
[ncurses.git] / test / test_tparm.c
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.37 2023/04/28 23:12:00 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
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 i_opt;
73 static int p_opt;
74 static int s_opt;
75 static int v_opt;
76
77 /*
78  * Total tests (and failures):
79  */
80 static long total_tests;
81 static long total_fails;
82
83 /*
84  * Total characters formatted for tputs:
85  */
86 static long total_nulls;
87 static long total_ctrls;
88 static long total_print;
89
90 static int
91 output_func(int ch)
92 {
93     if (ch == 0) {
94         total_nulls++;
95     } else if (ch < 32 || (ch >= 127 && ch < 160)) {
96         total_ctrls++;
97     } else {
98         total_print++;
99     }
100     return ch;
101 }
102
103 static int
104 isNumeric(char *source)
105 {
106     char *next = 0;
107     long value = strtol(source, &next, 0);
108     int result = (next == 0 || next == source || *next != '\0') ? 0 : 1;
109     (void) value;
110     return result;
111 }
112
113 static int
114 relevant(const char *name, const char *value)
115 {
116     int code = 1;
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)) {
122             if (v_opt > 2)
123                 printf("? %s noparams\n", name);
124             code = 0;
125         }
126     } else {
127         if (v_opt > 2) {
128             printf("? %s %s\n",
129                    (value == ABSENT_STRING)
130                    ? "absent"
131                    : "cancel",
132                    name);
133         }
134         code = 0;
135     }
136     return code;
137 }
138
139 static int
140 increment(long *all_parms, int *num_parms, int len_parms, int end_parms)
141 {
142     int rc = 0;
143     int n;
144
145     if (len_parms > MAX_PARM)
146         len_parms = MAX_PARM;
147
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);
152         }
153     }
154     for (n = 0; n < len_parms; ++n) {
155         if (all_parms[n] != 0) {
156             rc = 1;
157             break;
158         }
159     }
160     /* return 1 until the vector resets to all 0's */
161     return rc;
162 }
163
164 /* parse the format string to determine which positional parameters
165  * are assumed to be strings.
166  */
167 #if HAVE_TISCAN_S
168 static int
169 analyze_format(const char *format, int *mask, char **p_is_s)
170 {
171     int arg_count;
172     int arg_mask;
173     int n;
174     if (tiscan_s(&arg_count, &arg_mask, format) == OK) {
175         *mask = arg_mask;
176         for (n = 0; n < MAX_PARM; ++n) {
177             static char dummy[1];
178             p_is_s[n] = (arg_mask & 1) ? dummy : NULL;
179             arg_mask >>= 1;
180         }
181     } else {
182         *mask = 0;
183         arg_count = 0;
184         for (n = 0; n < MAX_PARM; ++n) {
185             p_is_s[n] = NULL;
186         }
187     }
188     return arg_count;
189 }
190 #elif HAVE__NC_TPARM_ANALYZE
191 extern int _nc_tparm_analyze(TERMINAL *, const char *, char **, int *);
192
193 static int
194 analyze_format(const char *format, int *mask, char **p_is_s)
195 {
196     int popcount = 0;
197     int analyzed = _nc_tparm_analyze(cur_term, format, p_is_s, &popcount);
198     int n;
199     if (analyzed < popcount) {
200         analyzed = popcount;
201     }
202     *mask = 0;
203     for (n = 0; n < MAX_PARM; ++n) {
204         if (p_is_s[n])
205             *mask |= (1 << n);
206     }
207     return analyzed;
208 }
209 #else
210 /* TODO: make this work without direct use of ncurses internals. */
211 static int
212 analyze_format(const char *format, int *mask, char **p_is_s)
213 {
214     int n;
215     char *filler = strstr(format, "%s");
216     *mask = 0;
217     for (n = 0; n < MAX_PARM; ++n) {
218         p_is_s[n] = filler;
219     }
220     return n;
221 }
222 #endif
223
224 #define NumStr(n) use_strings[n] \
225                   ? (long) (my_intptr_t) (number[n] \
226                      ? string[n] \
227                      : NULL) \
228                   : number[n]
229
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)
240
241 static void
242 test_tparm(const char *name, const char *format, long *number, char **string)
243 {
244     char *use_strings[MAX_PARM];
245     char *result = NULL;
246     int nparam;
247     int mask;
248
249     nparam = analyze_format(format, &mask, use_strings);
250 #if HAVE_TIPARM_S
251     if (s_opt) {
252         switch (nparam) {
253         case 0:
254             result = tiparm_s(0, mask, NS_0(format));
255             break;
256         case 1:
257             result = tiparm_s(1, mask, NS_1(format));
258             break;
259         case 2:
260             result = tiparm_s(2, mask, NS_2(format));
261             break;
262         case 3:
263             result = tiparm_s(3, mask, NS_3(format));
264             break;
265         case 4:
266             result = tiparm_s(4, mask, NS_4(format));
267             break;
268         case 5:
269             result = tiparm_s(5, mask, NS_5(format));
270             break;
271         case 6:
272             result = tiparm_s(6, mask, NS_6(format));
273             break;
274         case 7:
275             result = tiparm_s(7, mask, NS_7(format));
276             break;
277         case 8:
278             result = tiparm_s(8, mask, NS_8(format));
279             break;
280         case 9:
281             result = tiparm_s(9, mask, NS_9(format));
282             break;
283         }
284     } else
285 #endif
286 #if HAVE_TIPARM
287     if (i_opt) {
288         switch (nparam) {
289         case 0:
290             result = tiparm(NS_0(format));
291             break;
292         case 1:
293             result = tiparm(NS_1(format));
294             break;
295         case 2:
296             result = tiparm(NS_2(format));
297             break;
298         case 3:
299             result = tiparm(NS_3(format));
300             break;
301         case 4:
302             result = tiparm(NS_4(format));
303             break;
304         case 5:
305             result = tiparm(NS_5(format));
306             break;
307         case 6:
308             result = tiparm(NS_6(format));
309             break;
310         case 7:
311             result = tiparm(NS_7(format));
312             break;
313         case 8:
314             result = tiparm(NS_8(format));
315             break;
316         case 9:
317             result = tiparm(NS_9(format));
318             break;
319         }
320     } else
321 #endif
322         result = tparm(NS_9(format));
323     total_tests++;
324     if (result != NULL) {
325         tputs(result, 1, output_func);
326     } else {
327         total_fails++;
328     }
329     if (v_opt > 1) {
330         int n;
331         printf(".. %3d =", result != 0 ? (int) strlen(result) : -1);
332         for (n = 0; n < nparam; ++n) {
333             if (use_strings[n]) {
334                 if (number[n]) {
335                     printf(" \"%s\"", string[n]);
336                 } else {
337                     printf("  ?");
338                 }
339             } else {
340                 printf(" %2ld", number[n]);
341             }
342         }
343         printf(" %s\n", name);
344     }
345 }
346
347 static void
348 usage(int ok)
349 {
350     static const char *msg[] =
351     {
352         "Usage: test_tparm [options] [capability] [value1 [value2 [...]]]"
353         ,""
354         ,"Use tparm/tputs for all distinct combinations of given capability."
355         ,""
356         ,USAGE_COMMON
357         ,"Options:"
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."
362 #if HAVE_TIPARM
363         ," -i       test tiparm rather than tparm"
364 #endif
365         ," -p       test capabilities with no parameters but having padding"
366         ," -r NUM   repeat tests NUM times"
367 #if HAVE_TIPARM_S
368         ," -s       test tiparm_s rather than tparm"
369 #endif
370         ," -v       show values and results"
371     };
372     unsigned n;
373     for (n = 0; n < SIZEOF(msg); ++n) {
374         fprintf(stderr, "%s\n", msg[n]);
375     }
376     ExitProgram(ok ? EXIT_SUCCESS : EXIT_FAILURE);
377 }
378
379 #define PLURAL(n) n, (n != 1) ? "s" : ""
380 #define COLONS(n) (n >= 1) ? ":" : ""
381
382 #define NUMFORM "%10ld"
383 /* *INDENT-OFF* */
384 VERSION_COMMON()
385 /* *INDENT-ON* */
386
387 int
388 main(int argc, char *argv[])
389 {
390     int ch;
391     int n;
392     int r_run, t_run, n_run;
393     char *old_term = getenv("TERM");
394     int r_opt = 1;
395     char *t_opt = 0;
396
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);
401
402     long all_parms[10];         /* workspace for "-a" option */
403
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);
407
408     int use_caps;
409     int max_name = 10;          /* max # of items in cap_name[] */
410     int max_data = 10;          /* max # of items in cap_data[] */
411     char **cap_name;
412     char **cap_data;
413
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);
418     long use_parms = 1;
419
420     if (all_caps == 0 || all_terms == 0 || num_parms == 0 || str_parms == 0)
421         failed("no memory");
422
423     while ((ch = getopt(argc, argv, OPTS_COMMON "T:aipr:sv")) != -1) {
424         switch (ch) {
425         case 'T':
426             t_opt = optarg;
427             break;
428         case 'a':
429             ++a_opt;
430             break;
431 #if HAVE_TIPARM
432         case 'i':
433             ++i_opt;
434             break;
435 #endif
436         case 'p':
437             ++p_opt;
438             break;
439         case 'r':
440             r_opt = atoi(optarg);
441             break;
442 #if HAVE_TIPARM_S
443         case 's':
444             ++s_opt;
445             break;
446 #endif
447         case 'v':
448             ++v_opt;
449             break;
450         case OPTS_VERSION:
451             show_version(argv);
452             ExitProgram(EXIT_SUCCESS);
453         default:
454             usage(ch == OPTS_USAGE);
455             /* NOTREACHED */
456         }
457     }
458
459     /*
460      * If there is a nonnumeric parameter after the options, use that as the
461      * capability name.
462      */
463     if (optind < argc) {
464         if (!isNumeric(argv[optind])) {
465             all_caps[len_caps++] = strdup(argv[optind++]);
466         }
467     }
468
469     /*
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.
473      */
474     while (optind < argc) {
475         if (isNumeric(argv[optind])) {
476             char *dummy = 0;
477             long value = strtol(argv[optind], &dummy, 0);
478             num_parms[len_parms] = (int) value;
479         }
480         str_parms[len_parms] = argv[optind];
481         ++optind;
482         ++len_parms;
483     }
484     for (n = len_parms; n < max_parms; ++n) {
485         static char dummy[1];
486         str_parms[n] = dummy;
487     }
488     if (v_opt) {
489         printf("%d parameter%s%s\n", PLURAL(len_parms), COLONS(len_parms));
490         if (v_opt > 3) {
491             for (n = 0; n < len_parms; ++n) {
492                 printf(" %d: %d (%s)\n", n + 1, num_parms[n], str_parms[n]);
493             }
494         }
495     }
496
497     /*
498      * Make a list of values for $TERM.  Accept "-" for standard input to
499      * simplify scripting a check of the whole database.
500      */
501     old_term = strdup((old_term == 0) ? "unknown" : old_term);
502     if (t_opt != 0) {
503         if (!strcmp(t_opt, "-")) {
504             char buffer[BUFSIZ];
505             while (fgets(buffer, sizeof(buffer) - 1, stdin) != 0) {
506                 char *s = buffer;
507                 char *t;
508                 while (isspace(UChar(s[0])))
509                     ++s;
510                 t = s + strlen(s);
511                 while (t != s && isspace(UChar(t[-1])))
512                     *--t = '\0';
513                 s = strdup(s);
514                 if (len_terms + 2 >= max_terms) {
515                     max_terms *= 2;
516                     all_terms = typeRealloc(char *, max_terms, all_terms);
517                     if (all_terms == 0)
518                         failed("no memory: all_terms");
519                 }
520                 all_terms[len_terms++] = s;
521             }
522         } else {
523             char *s = t_opt;
524             char *t;
525             while ((t = strtok(s, ",")) != 0) {
526                 s = 0;
527                 if (len_terms + 2 >= max_terms) {
528                     max_terms *= 2;
529                     all_terms = typeRealloc(char *, max_terms, all_terms);
530                     if (all_terms == 0)
531                         failed("no memory: all_terms");
532                 }
533                 all_terms[len_terms++] = strdup(t);
534             }
535         }
536     } else {
537         all_terms[len_terms++] = strdup(old_term);
538     }
539     all_terms[len_terms] = 0;
540     if (v_opt) {
541         printf("%d term%s:\n", PLURAL(len_terms));
542         if (v_opt > 3) {
543             for (n = 0; n < len_terms; ++n) {
544                 printf(" %d: %s\n", n + 1, all_terms[n]);
545             }
546         }
547     }
548
549     /*
550      * If no capability name was selected, use the predefined list of string
551      * capabilities.
552      *
553      * TODO: To address the "other" systems which do not follow SVr4,
554      * just use the output from infocmp on $TERM.
555      */
556     if (len_caps == 0) {
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]);
561         }
562 #else
563         all_caps[len_caps++] = strdup("cup");
564         all_caps[len_caps++] = strdup("sgr");
565 #endif
566     }
567     std_caps = len_caps;
568     all_caps[len_caps] = 0;
569     if (v_opt) {
570         printf("%d name%s%s\n", PLURAL(len_caps), COLONS(len_caps));
571         if (v_opt > 3) {
572             for (n = 0; n < len_caps; ++n) {
573                 printf(" %d: %s\n", n + 1, all_caps[n]);
574             }
575         }
576     }
577
578     cap_name = typeMalloc(char *, (max_name = 1 + len_caps));
579     cap_data = typeMalloc(char *, (max_data = 1 + len_caps));
580
581     if (r_opt <= 0)
582         r_opt = 1;
583
584     if (a_opt) {
585         for (n = 0; n < max_parms; ++n)
586             if (num_parms[n])
587                 use_parms *= (num_parms[n] + 1);
588     }
589
590     for (r_run = 0; r_run < r_opt; ++r_run) {
591         for (t_run = 0; t_run < len_terms; ++t_run) {
592             int errs;
593
594             if (setupterm(all_terms[t_run], fileno(stdout), &errs) != OK) {
595                 printf("** skipping %s (errs:%d)\n", all_terms[t_run], errs);
596             }
597 #if NCURSES_XNAMES
598             len_caps = std_caps;
599             if (cur_term) {
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));
606                 }
607             }
608 #endif
609
610             /*
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.
614              */
615             use_caps = 0;
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;
621                     use_caps++;
622                 }
623             }
624
625             if (v_opt) {
626                 printf("[%d:%d] %d paramerized cap%s * %ld test-case%s \"%s\"\n",
627                        r_run + 1, r_opt,
628                        PLURAL(use_caps),
629                        PLURAL(use_parms),
630                        all_terms[t_run]);
631             }
632
633             memset(all_parms, 0, sizeof(all_parms));
634             if (a_opt) {
635                 /* for each combination of values */
636                 do {
637                     for (n_run = 0; n_run < use_caps; ++n_run) {
638                         test_tparm(cap_name[n_run],
639                                    cap_data[n_run],
640                                    all_parms,
641                                    str_parms);
642                     }
643                 }
644                 while (increment(all_parms, num_parms, len_parms, 0));
645             } else {
646                 /* for the given values */
647                 for (n_run = 0; n_run < use_caps; ++n_run) {
648                     test_tparm(cap_name[n_run],
649                                cap_data[n_run],
650                                all_parms,
651                                str_parms);
652                 }
653             }
654 #if NCURSES_XNAMES
655             for (n = std_caps; n < len_caps; ++n) {
656                 free(all_caps[n]);
657             }
658 #endif
659             if (cur_term != 0) {
660                 del_curterm(cur_term);
661             } else {
662                 printf("? no cur_term\n");
663             }
664         }
665     }
666
667     printf("Tests:\n");
668     printf(NUMFORM " total\n", total_tests);
669     if (total_fails)
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);
676 #if NO_LEAKS
677     for (n = 0; n < std_caps; ++n) {
678         free(all_caps[n]);
679     }
680     free(all_caps);
681     free(old_term);
682     for (n = 0; n < len_terms; ++n) {
683         free(all_terms[n]);
684     }
685     free(all_terms);
686     free(num_parms);
687     free(str_parms);
688     free(cap_name);
689     free(cap_data);
690 #endif
691
692     ExitProgram(EXIT_SUCCESS);
693 }
694
695 #else /* !HAVE_TIGETSTR */
696 int
697 main(void)
698 {
699     failed("This program requires the terminfo functions such as tigetstr");
700 }
701 #endif /* HAVE_TIGETSTR */