]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/test_tparm.c
ncurses 6.4 - patch 20240420
[ncurses.git] / test / test_tparm.c
1 /****************************************************************************
2  * Copyright 2020-2021,2022 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.24 2022/12/10 23:23:27 tom Exp $
33  *
34  * Exercise tparm, either for all possible capabilities with fixed parameters,
35  * or one capability with all possible parameters.
36  *
37  * TODO: incorporate tic.h and _nc_tparm_analyze
38  * TODO: optionally test tiparm
39  * TODO: add checks/logic to handle "%s" in tparm
40  */
41 #define USE_TINFO
42 #include <test.priv.h>
43
44 static GCC_NORETURN void failed(const char *);
45
46 static void
47 failed(const char *msg)
48 {
49     fprintf(stderr, "%s\n", msg);
50     ExitProgram(EXIT_FAILURE);
51 }
52
53 #if HAVE_TIGETSTR
54
55 static int a_opt;
56 static int p_opt;
57 static int v_opt;
58
59 /*
60  * Total tests (and failures):
61  */
62 static long total_tests;
63 static long total_fails;
64
65 /*
66  * Total characters formatted for tputs:
67  */
68 static long total_nulls;
69 static long total_ctrls;
70 static long total_print;
71
72 static int
73 output_func(int ch)
74 {
75     if (ch == 0) {
76         total_nulls++;
77     } else if (ch < 32 || (ch >= 127 && ch < 160)) {
78         total_ctrls++;
79     } else {
80         total_print++;
81     }
82     return ch;
83 }
84
85 static int
86 isNumeric(char *source)
87 {
88     char *next = 0;
89     long value = strtol(source, &next, 0);
90     int result = (next == 0 || next == source || *next != '\0') ? 0 : 1;
91     (void) value;
92     return result;
93 }
94
95 static int
96 relevant(const char *name, const char *value)
97 {
98     int code = 1;
99     if (VALID_STRING(value)) {
100         if (strstr(value, "%p") == 0
101             && strstr(value, "%d") == 0
102             && strstr(value, "%s") == 0
103             && (!p_opt || strstr(value, "$<") == 0)) {
104             if (v_opt > 2)
105                 printf("? %s noparams\n", name);
106             code = 0;
107         }
108     } else {
109         if (v_opt > 2) {
110             printf("? %s %s\n",
111                    (value == ABSENT_STRING)
112                    ? "absent"
113                    : "cancel",
114                    name);
115         }
116         code = 0;
117     }
118     return code;
119 }
120
121 static int
122 increment(int *all_parms, int *num_parms, int len_parms, int end_parms)
123 {
124     int rc = 0;
125     int n;
126
127     if (len_parms > 9)
128         len_parms = 9;
129
130     if (end_parms < len_parms) {
131         if (all_parms[end_parms]++ >= num_parms[end_parms]) {
132             all_parms[end_parms] = 0;
133             increment(all_parms, num_parms, len_parms, end_parms + 1);
134         }
135     }
136     for (n = 0; n < len_parms; ++n) {
137         if (all_parms[n] != 0) {
138             rc = 1;
139             break;
140         }
141     }
142     /* return 1 until the vector resets to all 0's */
143     return rc;
144 }
145
146 static void
147 test_tparm(const char *name, const char *format, int *number)
148 {
149     char *result = tparm(format,
150                          number[0],
151                          number[1],
152                          number[2],
153                          number[3],
154                          number[4],
155                          number[5],
156                          number[6],
157                          number[7],
158                          number[8]);
159     total_tests++;
160     if (result != NULL) {
161         tputs(result, 1, output_func);
162     } else {
163         total_fails++;
164     }
165     if (v_opt > 1)
166         printf(".. %2d = %2d %2d %2d %2d %2d %2d %2d %2d %2d %s\n",
167                result != 0 ? (int) strlen(result) : -1,
168                number[0],
169                number[1],
170                number[2],
171                number[3],
172                number[4],
173                number[5],
174                number[6],
175                number[7],
176                number[8],
177                name);
178 }
179
180 static void
181 usage(int ok)
182 {
183     static const char *msg[] =
184     {
185         "Usage: test_tparm [options] [capability] [value1 [value2 [...]]]"
186         ,""
187         ,"Use tparm/tputs for all distinct combinations of given capability."
188         ,""
189         ,USAGE_COMMON
190         ,"Options:"
191         ," -T TERM  override $TERM; this may be a comma-separated list or \"-\""
192         ,"          to read a list from standard-input"
193         ," -a       test all combinations of parameters"
194         ,"          [value1...] forms a vector of maximum parameter-values."
195         ," -p       test capabilities with no parameters but having padding"
196         ," -r NUM   repeat tests NUM times"
197         ," -v       show values and results"
198     };
199     unsigned n;
200     for (n = 0; n < SIZEOF(msg); ++n) {
201         fprintf(stderr, "%s\n", msg[n]);
202     }
203     ExitProgram(ok ? EXIT_SUCCESS : EXIT_FAILURE);
204 }
205
206 #define PLURAL(n) n, (n != 1) ? "s" : ""
207 #define COLONS(n) (n >= 1) ? ":" : ""
208
209 #define NUMFORM "%10ld"
210 /* *INDENT-OFF* */
211 VERSION_COMMON()
212 /* *INDENT-ON* */
213
214 int
215 main(int argc, char *argv[])
216 {
217     int ch;
218     int n;
219     int r_run, t_run, n_run;
220     char *old_term = getenv("TERM");
221     int r_opt = 1;
222     char *t_opt = 0;
223
224     int len_caps = 0;           /* cur # of items in all_caps[] */
225     int max_caps = 10;          /* max # of items in all_caps[] */
226     char **all_caps = typeCalloc(char *, max_caps);
227
228     int all_parms[10];          /* workspace for "-a" option */
229
230     int len_terms = 0;          /* cur # of items in all_terms[] */
231     int max_terms = 10;         /* max # of items in all_terms[] */
232     char **all_terms = typeCalloc(char *, max_terms);
233
234     int use_caps;
235     char **cap_name;
236     char **cap_data;
237
238     int len_parms = 0;          /* cur # of items in num_parms[], str_parms[] */
239     int max_parms = argc + 10;  /* max # of items in num_parms[], str_parms[] */
240     int *num_parms = typeCalloc(int, max_parms);
241     char **str_parms = typeCalloc(char *, max_parms);
242     long use_parms = 1;
243
244     if (all_caps == 0 || all_terms == 0 || num_parms == 0 || str_parms == 0)
245         failed("no memory");
246
247     while ((ch = getopt(argc, argv, OPTS_COMMON "T:apr:v")) != -1) {
248         switch (ch) {
249         case 'T':
250             t_opt = optarg;
251             break;
252         case 'a':
253             ++a_opt;
254             break;
255         case 'p':
256             ++p_opt;
257             break;
258         case 'r':
259             r_opt = atoi(optarg);
260             break;
261         case 'v':
262             ++v_opt;
263             break;
264         case OPTS_VERSION:
265             show_version(argv);
266             ExitProgram(EXIT_SUCCESS);
267         default:
268             usage(ch == OPTS_USAGE);
269             /* NOTREACHED */
270         }
271     }
272
273     /*
274      * If there is a nonnumeric parameter after the options, use that as the
275      * capability name.
276      */
277     if (optind < argc) {
278         if (!isNumeric(argv[optind])) {
279             all_caps[len_caps++] = strdup(argv[optind++]);
280         }
281     }
282
283     /*
284      * Any remaining arguments must be possible parameter values.  If numeric,
285      * and "-a" is not set, use those as the actual values for which the
286      * capabilities are tested.
287      */
288     while (optind < argc) {
289         if (isNumeric(argv[optind])) {
290             char *dummy = 0;
291             long value = strtol(argv[optind], &dummy, 0);
292             num_parms[len_parms] = (int) value;
293         }
294         str_parms[len_parms] = argv[optind];
295         ++optind;
296         ++len_parms;
297     }
298     for (n = len_parms; n < max_parms; ++n) {
299         static char dummy[1];
300         str_parms[n] = dummy;
301     }
302     if (v_opt) {
303         printf("%d parameter%s%s\n", PLURAL(len_parms), COLONS(len_parms));
304         if (v_opt > 3) {
305             for (n = 0; n < len_parms; ++n) {
306                 printf(" %d: %d (%s)\n", n + 1, num_parms[n], str_parms[n]);
307             }
308         }
309     }
310
311     /*
312      * Make a list of values for $TERM.  Accept "-" for standard input to
313      * simplify scripting a check of the whole database.
314      */
315     old_term = strdup((old_term == 0) ? "unknown" : old_term);
316     if (t_opt != 0) {
317         if (!strcmp(t_opt, "-")) {
318             char buffer[BUFSIZ];
319             while (fgets(buffer, sizeof(buffer) - 1, stdin) != 0) {
320                 char *s = buffer;
321                 char *t;
322                 while (isspace(UChar(s[0])))
323                     ++s;
324                 t = s + strlen(s);
325                 while (t != s && isspace(UChar(t[-1])))
326                     *--t = '\0';
327                 s = strdup(s);
328                 if (len_terms + 2 >= max_terms) {
329                     max_terms *= 2;
330                     all_terms = typeRealloc(char *, max_terms, all_terms);
331                     if (all_terms == 0)
332                         failed("no memory: all_terms");
333                 }
334                 all_terms[len_terms++] = s;
335             }
336         } else {
337             char *s = t_opt;
338             char *t;
339             while ((t = strtok(s, ",")) != 0) {
340                 s = 0;
341                 if (len_terms + 2 >= max_terms) {
342                     max_terms *= 2;
343                     all_terms = typeRealloc(char *, max_terms, all_terms);
344                     if (all_terms == 0)
345                         failed("no memory: all_terms");
346                 }
347                 all_terms[len_terms++] = strdup(t);
348             }
349         }
350     } else {
351         all_terms[len_terms++] = strdup(old_term);
352     }
353     all_terms[len_terms] = 0;
354     if (v_opt) {
355         printf("%d term%s:\n", PLURAL(len_terms));
356         if (v_opt > 3) {
357             for (n = 0; n < len_terms; ++n) {
358                 printf(" %d: %s\n", n + 1, all_terms[n]);
359             }
360         }
361     }
362
363     /*
364      * If no capability name was selected, use the predefined list of string
365      * capabilities.
366      *
367      * TODO: To address the "other" systems which do not follow SVr4,
368      * just use the output from infocmp on $TERM.
369      */
370     if (len_caps == 0) {
371 #if defined(HAVE_CURSES_DATA_BOOLNAMES) || defined(DECL_CURSES_DATA_BOOLNAMES)
372         for (n = 0; strnames[n] != 0; ++n) {
373             if (len_caps + 2 >= max_caps) {
374                 max_caps *= 2;
375                 all_caps = typeRealloc(char *, max_caps, all_caps);
376                 if (all_caps == 0) {
377                     failed("no memory: all_caps");
378                 }
379             }
380             all_caps[len_caps++] = strdup(strnames[n]);
381         }
382 #else
383         all_caps[len_caps++] = strdup("cup");
384         all_caps[len_caps++] = strdup("sgr");
385 #endif
386     }
387     all_caps[len_caps] = 0;
388     if (v_opt) {
389         printf("%d name%s%s\n", PLURAL(len_caps), COLONS(len_caps));
390         if (v_opt > 3) {
391             for (n = 0; n < len_caps; ++n) {
392                 printf(" %d: %s\n", n + 1, all_caps[n]);
393             }
394         }
395     }
396
397     cap_name = typeMalloc(char *, len_caps);
398     cap_data = typeMalloc(char *, len_caps);
399
400     if (r_opt <= 0)
401         r_opt = 1;
402
403     if (a_opt) {
404         for (n = 0; n < max_parms; ++n)
405             if (num_parms[n])
406                 use_parms *= (num_parms[n] + 1);
407     }
408
409     for (r_run = 0; r_run < r_opt; ++r_run) {
410         for (t_run = 0; t_run < len_terms; ++t_run) {
411             int errs;
412
413             if (setupterm(all_terms[t_run], fileno(stdout), &errs) != OK) {
414                 printf("** skipping %s (errs:%d)\n", all_terms[t_run], errs);
415             }
416
417             /*
418              * Most of the capabilities have no parameters, e.g., they are
419              * function-keys or simple operations such as clear-display.
420              * Ignore those, since they do not really exercise tparm.
421              */
422             use_caps = 0;
423             for (n = 0; n < len_caps; ++n) {
424                 char *value = tigetstr(all_caps[n]);
425                 if (relevant(all_caps[n], value)) {
426                     cap_name[use_caps] = all_caps[n];
427                     cap_data[use_caps] = value;
428                     use_caps++;
429                 }
430             }
431
432             if (v_opt) {
433                 printf("[%d:%d] %d cap%s * %ld param%s \"%s\"\n",
434                        r_run + 1, r_opt,
435                        PLURAL(use_caps),
436                        PLURAL(use_parms),
437                        all_terms[t_run]);
438             }
439
440             memset(all_parms, 0, sizeof(all_parms));
441             if (a_opt) {
442                 /* for each combination of values */
443                 do {
444                     for (n_run = 0; n_run < use_caps; ++n_run) {
445                         test_tparm(cap_name[n_run], cap_data[n_run], all_parms);
446                     }
447                 }
448                 while (increment(all_parms, num_parms, len_parms, 0));
449             } else {
450                 /* for the given values */
451                 for (n_run = 0; n_run < use_caps; ++n_run) {
452                     test_tparm(cap_name[n_run], cap_data[n_run], all_parms);
453                 }
454             }
455             if (cur_term != 0) {
456                 del_curterm(cur_term);
457             } else {
458                 printf("? no cur_term\n");
459             }
460         }
461     }
462
463     printf("Tests:\n");
464     printf(NUMFORM " total\n", total_tests);
465     if (total_fails)
466         printf(NUMFORM " failed\n", total_fails);
467     printf("Characters:\n");
468     printf(NUMFORM " nulls\n", total_nulls);
469     printf(NUMFORM " controls\n", total_ctrls);
470     printf(NUMFORM " printable\n", total_print);
471     printf(NUMFORM " total\n", total_nulls + total_ctrls + total_print);
472 #if NO_LEAKS
473     for (n = 0; n < len_caps; ++n) {
474         free(all_caps[n]);
475     }
476     free(all_caps);
477     free(old_term);
478     for (n = 0; n < len_terms; ++n) {
479         free(all_terms[n]);
480     }
481     free(all_terms);
482     free(num_parms);
483     free(str_parms);
484     free(cap_name);
485     free(cap_data);
486 #endif
487
488     ExitProgram(EXIT_SUCCESS);
489 }
490
491 #else /* !HAVE_TIGETSTR */
492 int
493 main(void)
494 {
495     failed("This program requires the terminfo functions such as tigetstr");
496 }
497 #endif /* HAVE_TIGETSTR */