1 /****************************************************************************
2 * Copyright (c) 2015-2017,2019 Free Software Foundation, Inc. *
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: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
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. *
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 *
27 ****************************************************************************/
30 * Author: Thomas E. Dickey
32 * $Id: test_sgr.c,v 1.12 2019/06/22 00:20:06 tom Exp $
34 * A simple demo of the sgr/sgr0 terminal capabilities.
37 #include <test.priv.h>
40 static void failed(const char *) GCC_NORETURN;
43 failed(const char *msg)
45 fprintf(stderr, "%s\n", msg);
46 ExitProgram(EXIT_FAILURE);
52 static bool no_init = FALSE;
53 static bool q_opt = FALSE;
57 static char **db_list;
60 static long total_values;
63 make_dbitem(char *p, char *q)
65 size_t need = strlen(e_opt) + 2 + (size_t) (p - q);
66 char *result = malloc(need);
67 _nc_SPRINTF(result, _nc_SLIMIT(need) "%s=%.*s", e_opt, (int) (p - q), q);
77 for (pass = 0; pass < 2; ++pass) {
81 for (p = q = d_opt; *p != '\0'; ++p) {
85 db_list[count] = make_dbitem(p, q);
94 db_list[count] = make_dbitem(p, q);
99 db_list = typeCalloc(char *, count + 1);
111 if ((result = db_list[db_item]) == 0) {
118 printf("** %s\n", result);
128 for (n = 0; db_list[n]; ++n)
137 #define MAXSGR (1 << MAXPAR)
138 #define BITS2P(n) (count & (1 << (n - 1)))
139 #define MASK_SMSO (1 << 0)
140 #define MASK_BOLD (1 << 5)
141 #define MASK_REV (1 << 2)
144 dumpit(unsigned bits, unsigned ignore, const char *sgr, const char *sgr0)
146 static const char sample[] = "abcdefghijklm";
147 static char params[] = "SURBDBIPA";
150 printf("%4d ", bits);
152 for (n = 0; n < MAXPAR; ++n) {
153 putchar((int) ((bits & (unsigned) (1 << n)) ? params[n] : '-'));
163 one_bit(unsigned a, unsigned b)
165 unsigned c = (a ^ b);
177 brute_force(const char *name)
185 char *my_name = strdup(name);
188 putenv(next_dbitem());
192 printf("Terminal type \"%s\"\n", my_name);
197 setupterm((NCURSES_CONST char *) my_name, 1, (int *) 0);
201 if (strcmp(my_name, ttytype))
202 printf("... actual \"%s\"\n", ttytype);
205 my_sgr = tigetstr("sgr");
206 my_sgr0 = tigetstr("sgr0");
207 my_bold = tigetstr("bold");
208 my_revs = tigetstr("rev");
209 my_smso = tigetstr("smso");
211 if (!VALID_STRING(my_sgr)) {
212 fprintf(stderr, "no \"sgr\" capability found\n");
213 } else if (!VALID_STRING(my_sgr0)) {
214 fprintf(stderr, "no \"sgr0\" capability found\n");
216 char *values[MAXSGR + MAXPAR];
221 for (count = 0; count < MAXSGR; ++count) {
222 values[count] = tparm(my_sgr,
232 if (values[count] != 0) {
233 values[count] = strdup(values[count]);
236 for (count = 0; count < MAXSGR; ++count) {
237 if (values[count] != 0) {
238 for (j = count + 1; j < MAXSGR; ++j) {
241 if (strcmp(values[count], values[j]))
243 if (one_bit(count, j)) {
250 for (j = 0; j < MAXPAR; ++j) {
251 unsigned mask = (unsigned) (1 << j);
252 for (count = 0; count < MAXSGR; ++count) {
253 if ((count & mask) != 0)
255 if (values[count] != 0 && values[count + mask] != 0) {
262 /* smso is tested first, but often duplicates bold or reverse. */
263 if (VALID_STRING(my_smso)) {
264 if (VALID_STRING(my_bold) && !strcmp(my_bold, my_smso)) {
268 if (VALID_STRING(my_revs) && !strcmp(my_revs, my_smso)) {
273 for (count = 0; count < MAXSGR; ++count) {
274 if (values[count] != 0) {
276 if ((repeat & MASK_SMSO) != 0
277 && (count & MASK_SMSO) != 0) {
280 for (j = 0; j < count; ++j) {
281 if (values[j] != 0 && !strcmp(values[j], values[count])) {
282 if ((repeat & MASK_SMSO) != 0
283 && (j & MASK_SMSO) != 0
284 && (count & reason) != 0) {
293 dumpit(count, ignore, values[count], my_sgr0);
298 for (count = 0; count < MAXSGR; ++count) {
302 del_curterm(cur_term);
308 static const char *msg[] =
310 "Usage: test_sgr [options] [terminal]",
312 "Print all distinct combinations of sgr capability.",
315 " -d LIST colon-separated list of databases to use",
316 " -e NAME environment variable to set with -d option",
317 " -n do not initialize terminal, to test error-checking",
318 " -q quiet (prints only counts)",
321 for (n = 0; n < SIZEOF(msg); ++n) {
322 fprintf(stderr, "%s\n", msg[n]);
324 ExitProgram(EXIT_FAILURE);
328 main(int argc, char *argv[])
333 while ((n = getopt(argc, argv, "d:e:nq")) != -1) {
356 for (n = optind; n < argc; ++n) {
357 brute_force(argv[n]);
359 } else if ((name = getenv("TERM")) != 0) {
362 static char dumb[] = "dumb";
366 printf("%ld distinct values\n", total_values);
372 ExitProgram(EXIT_SUCCESS);
375 #else /* !HAVE_TIGETSTR */
377 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
379 failed("This program requires the terminfo functions such as tigetstr");
380 ExitProgram(EXIT_FAILURE);
382 #endif /* HAVE_TIGETSTR */