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