]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/test_tparm.c
ncurses 6.2 - patch 20210501
[ncurses.git] / test / test_tparm.c
1 /****************************************************************************
2  * Copyright 2020,2021 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.20 2021/03/20 15:58:32 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(void)
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         "Options:",
190         " -T TERM  override $TERM; this may be a comma-separated list or \"-\"",
191         "          to read a list from standard-input",
192         " -a       test all combinations of parameters",
193         "          [value1...] forms a vector of maximum parameter-values.",
194         " -p       test capabilities with no parameters but having padding",
195         " -r NUM   repeat tests NUM times",
196         " -v       show values and results",
197     };
198     unsigned n;
199     for (n = 0; n < SIZEOF(msg); ++n) {
200         fprintf(stderr, "%s\n", msg[n]);
201     }
202     ExitProgram(EXIT_FAILURE);
203 }
204
205 #define PLURAL(n) n, (n != 1) ? "s" : ""
206 #define COLONS(n) (n >= 1) ? ":" : ""
207
208 #define NUMFORM "%10ld"
209
210 int
211 main(int argc, char *argv[])
212 {
213     int n;
214     int r_run, t_run, n_run;
215     char *old_term = getenv("TERM");
216     int r_opt = 1;
217     char *t_opt = 0;
218
219     int len_caps = 0;           /* cur # of items in all_caps[] */
220     int max_caps = 10;          /* max # of items in all_caps[] */
221     char **all_caps = typeCalloc(char *, max_caps);
222
223     int all_parms[10];          /* workspace for "-a" option */
224
225     int len_terms = 0;          /* cur # of items in all_terms[] */
226     int max_terms = 10;         /* max # of items in all_terms[] */
227     char **all_terms = typeCalloc(char *, max_terms);
228
229     int use_caps;
230     char **cap_name;
231     char **cap_data;
232
233     int len_parms = 0;          /* cur # of items in num_parms[], str_parms[] */
234     int max_parms = argc + 10;  /* max # of items in num_parms[], str_parms[] */
235     int *num_parms = typeCalloc(int, max_parms);
236     char **str_parms = typeCalloc(char *, max_parms);
237     long use_parms = 1;
238
239     if (all_caps == 0 || all_terms == 0 || num_parms == 0 || str_parms == 0)
240         failed("no memory");
241
242     while ((n = getopt(argc, argv, "T:apr:v")) != -1) {
243         switch (n) {
244         case 'T':
245             t_opt = optarg;
246             break;
247         case 'a':
248             ++a_opt;
249             break;
250         case 'p':
251             ++p_opt;
252             break;
253         case 'r':
254             r_opt = atoi(optarg);
255             break;
256         case 'v':
257             ++v_opt;
258             break;
259         default:
260             usage();
261             break;
262         }
263     }
264
265     /*
266      * If there is a nonnumeric parameter after the options, use that as the
267      * capability name.
268      */
269     if (optind < argc) {
270         if (!isNumeric(argv[optind])) {
271             all_caps[len_caps++] = strdup(argv[optind++]);
272         }
273     }
274
275     /*
276      * Any remaining arguments must be possible parameter values.  If numeric,
277      * and "-a" is not set, use those as the actual values for which the
278      * capabilities are tested.
279      */
280     while (optind < argc) {
281         if (isNumeric(argv[optind])) {
282             char *dummy = 0;
283             long value = strtol(argv[optind], &dummy, 0);
284             num_parms[len_parms] = (int) value;
285         }
286         str_parms[len_parms] = argv[optind];
287         ++optind;
288         ++len_parms;
289     }
290     for (n = len_parms; n < max_parms; ++n) {
291         static char dummy[1];
292         str_parms[n] = dummy;
293     }
294     if (v_opt) {
295         printf("%d parameter%s%s\n", PLURAL(len_parms), COLONS(len_parms));
296         if (v_opt > 3) {
297             for (n = 0; n < len_parms; ++n) {
298                 printf(" %d: %d (%s)\n", n + 1, num_parms[n], str_parms[n]);
299             }
300         }
301     }
302
303     /*
304      * Make a list of values for $TERM.  Accept "-" for standard input to
305      * simplify scripting a check of the whole database.
306      */
307     old_term = strdup((old_term == 0) ? "unknown" : old_term);
308     if (t_opt != 0) {
309         if (!strcmp(t_opt, "-")) {
310             char buffer[BUFSIZ];
311             while (fgets(buffer, sizeof(buffer) - 1, stdin) != 0) {
312                 char *s = buffer;
313                 char *t;
314                 while (isspace(UChar(s[0])))
315                     ++s;
316                 t = s + strlen(s);
317                 while (t != s && isspace(UChar(t[-1])))
318                     *--t = '\0';
319                 s = strdup(s);
320                 if (len_terms + 2 >= max_terms) {
321                     max_terms *= 2;
322                     all_terms = typeRealloc(char *, max_terms, all_terms);
323                     if (all_terms == 0)
324                         failed("no memory: all_terms");
325                 }
326                 all_terms[len_terms++] = s;
327             }
328         } else {
329             char *s = t_opt;
330             char *t;
331             while ((t = strtok(s, ",")) != 0) {
332                 s = 0;
333                 if (len_terms + 2 >= max_terms) {
334                     max_terms *= 2;
335                     all_terms = typeRealloc(char *, max_terms, all_terms);
336                     if (all_terms == 0)
337                         failed("no memory: all_terms");
338                 }
339                 all_terms[len_terms++] = strdup(t);
340             }
341         }
342     } else {
343         all_terms[len_terms++] = strdup(old_term);
344     }
345     all_terms[len_terms] = 0;
346     if (v_opt) {
347         printf("%d term%s:\n", PLURAL(len_terms));
348         if (v_opt > 3) {
349             for (n = 0; n < len_terms; ++n) {
350                 printf(" %d: %s\n", n + 1, all_terms[n]);
351             }
352         }
353     }
354
355     /*
356      * If no capability name was selected, use the predefined list of string
357      * capabilities.
358      *
359      * TODO: To address the "other" systems which do not follow SVr4,
360      * just use the output from infocmp on $TERM.
361      */
362     if (len_caps == 0) {
363 #if defined(HAVE_CURSES_DATA_BOOLNAMES) || defined(DECL_CURSES_DATA_BOOLNAMES)
364         for (n = 0; strnames[n] != 0; ++n) {
365             if (len_caps + 2 >= max_caps) {
366                 max_caps *= 2;
367                 all_caps = typeRealloc(char *, max_caps, all_caps);
368                 if (all_caps == 0) {
369                     failed("no memory: all_caps");
370                 }
371             }
372             all_caps[len_caps++] = strdup(strnames[n]);
373         }
374 #else
375         all_caps[len_caps++] = strdup("cup");
376         all_caps[len_caps++] = strdup("sgr");
377 #endif
378     }
379     all_caps[len_caps] = 0;
380     if (v_opt) {
381         printf("%d name%s%s\n", PLURAL(len_caps), COLONS(len_caps));
382         if (v_opt > 3) {
383             for (n = 0; n < len_caps; ++n) {
384                 printf(" %d: %s\n", n + 1, all_caps[n]);
385             }
386         }
387     }
388
389     cap_name = typeMalloc(char *, len_caps);
390     cap_data = typeMalloc(char *, len_caps);
391
392     if (r_opt <= 0)
393         r_opt = 1;
394
395     if (a_opt) {
396         for (n = 0; n < max_parms; ++n)
397             if (num_parms[n])
398                 use_parms *= (num_parms[n] + 1);
399     }
400
401     for (r_run = 0; r_run < r_opt; ++r_run) {
402         for (t_run = 0; t_run < len_terms; ++t_run) {
403             int errs;
404
405             if (setupterm(all_terms[t_run], fileno(stdout), &errs) != OK) {
406                 printf("** skipping %s (errs:%d)\n", all_terms[t_run], errs);
407             }
408
409             /*
410              * Most of the capabilities have no parameters, e.g., they are
411              * function-keys or simple operations such as clear-display.
412              * Ignore those, since they do not really exercise tparm.
413              */
414             use_caps = 0;
415             for (n = 0; n < len_caps; ++n) {
416                 char *value = tigetstr(all_caps[n]);
417                 if (relevant(all_caps[n], value)) {
418                     cap_name[use_caps] = all_caps[n];
419                     cap_data[use_caps] = value;
420                     use_caps++;
421                 }
422             }
423
424             if (v_opt) {
425                 printf("[%d:%d] %d cap%s * %ld param%s \"%s\"\n",
426                        r_run + 1, r_opt,
427                        PLURAL(use_caps),
428                        PLURAL(use_parms),
429                        all_terms[t_run]);
430             }
431
432             memset(all_parms, 0, sizeof(all_parms));
433             if (a_opt) {
434                 /* for each combination of values */
435                 do {
436                     for (n_run = 0; n_run < use_caps; ++n_run) {
437                         test_tparm(cap_name[n_run], cap_data[n_run], all_parms);
438                     }
439                 }
440                 while (increment(all_parms, num_parms, len_parms, 0));
441             } else {
442                 /* for the given values */
443                 for (n_run = 0; n_run < use_caps; ++n_run) {
444                     test_tparm(cap_name[n_run], cap_data[n_run], all_parms);
445                 }
446             }
447             if (cur_term != 0) {
448                 del_curterm(cur_term);
449             } else {
450                 printf("? no cur_term\n");
451             }
452         }
453     }
454
455     printf("Tests:\n");
456     printf(NUMFORM " total\n", total_tests);
457     if (total_fails)
458         printf(NUMFORM " failed\n", total_fails);
459     printf("Characters:\n");
460     printf(NUMFORM " nulls\n", total_nulls);
461     printf(NUMFORM " controls\n", total_ctrls);
462     printf(NUMFORM " printable\n", total_print);
463     printf(NUMFORM " total\n", total_nulls + total_ctrls + total_print);
464 #if NO_LEAKS
465     for (n = 0; n < len_caps; ++n) {
466         free(all_caps[n]);
467     }
468     free(all_caps);
469     free(old_term);
470     for (n = 0; n < len_terms; ++n) {
471         free(all_terms[n]);
472     }
473     free(all_terms);
474     free(num_parms);
475     free(str_parms);
476     free(cap_name);
477     free(cap_data);
478 #endif
479
480     ExitProgram(EXIT_SUCCESS);
481 }
482
483 #else /* !HAVE_TIGETSTR */
484 int
485 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
486 {
487     failed("This program requires the terminfo functions such as tigetstr");
488 }
489 #endif /* HAVE_TIGETSTR */