1 /****************************************************************************
2 * Copyright 2019,2020 Thomas E. Dickey *
3 * Copyright 2015-2016,2017 Free Software Foundation, Inc. *
5 * Permission is hereby granted, free of charge, to any person obtaining a *
6 * copy of this software and associated documentation files (the *
7 * "Software"), to deal in the Software without restriction, including *
8 * without limitation the rights to use, copy, modify, merge, publish, *
9 * distribute, distribute with modifications, sublicense, and/or sell *
10 * copies of the Software, and to permit persons to whom the Software is *
11 * furnished to do so, subject to the following conditions: *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
19 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
22 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
24 * Except as contained in this notice, the name(s) of the above copyright *
25 * holders shall not be used in advertising or otherwise to promote the *
26 * sale, use or other dealings in this Software without prior written *
28 ****************************************************************************/
31 * Author: Thomas E. Dickey
33 * $Id: test_sgr.c,v 1.15 2020/02/02 23:34:34 tom Exp $
35 * A simple demo of the sgr/sgr0 terminal capabilities.
38 #include <test.priv.h>
41 static void failed(const char *) GCC_NORETURN;
44 failed(const char *msg)
46 fprintf(stderr, "%s\n", msg);
47 ExitProgram(EXIT_FAILURE);
53 static bool no_init = FALSE;
54 static bool q_opt = FALSE;
58 static char **db_list;
61 static long total_values;
64 make_dbitem(char *p, char *q)
66 size_t need = strlen(e_opt) + 2 + (size_t) (p - q);
67 char *result = malloc(need);
68 _nc_SPRINTF(result, _nc_SLIMIT(need) "%s=%.*s", e_opt, (int) (p - q), q);
78 for (pass = 0; pass < 2; ++pass) {
82 for (p = q = d_opt; *p != '\0'; ++p) {
86 db_list[count] = make_dbitem(p, q);
95 db_list[count] = make_dbitem(p, q);
100 db_list = typeCalloc(char *, count + 1);
112 if ((result = db_list[db_item]) == 0) {
119 printf("** %s\n", result);
129 for (n = 0; db_list[n]; ++n)
138 #define MAXSGR (1 << MAXPAR)
139 #define BITS2P(n) (count & (1 << (n - 1)))
140 #define MASK_SMSO (1 << 0)
141 #define MASK_BOLD (1 << 5)
142 #define MASK_REV (1 << 2)
145 dumpit(unsigned bits, unsigned ignore, const char *sgr, const char *sgr0)
147 static const char sample[] = "abcdefghijklm";
148 static char params[] = "SURBDBIPA";
151 printf("%4u ", bits);
153 for (n = 0; n < MAXPAR; ++n) {
154 putchar((int) ((bits & (unsigned) (1 << n)) ? params[n] : '-'));
164 one_bit(unsigned a, unsigned b)
166 unsigned c = (a ^ b);
178 brute_force(const char *name)
186 char *my_name = strdup(name);
189 putenv(next_dbitem());
193 printf("Terminal type \"%s\"\n", my_name);
198 setupterm((NCURSES_CONST char *) my_name, 1, (int *) 0);
202 if (strcmp(my_name, ttytype))
203 printf("... actual \"%s\"\n", ttytype);
206 my_sgr = tigetstr("sgr");
207 my_sgr0 = tigetstr("sgr0");
208 my_bold = tigetstr("bold");
209 my_revs = tigetstr("rev");
210 my_smso = tigetstr("smso");
212 if (!VALID_STRING(my_sgr)) {
213 fprintf(stderr, "no \"sgr\" capability found\n");
214 } else if (!VALID_STRING(my_sgr0)) {
215 fprintf(stderr, "no \"sgr0\" capability found\n");
217 char *values[MAXSGR + MAXPAR];
222 for (count = 0; count < MAXSGR; ++count) {
223 values[count] = tparm(my_sgr,
233 if (values[count] != 0) {
234 values[count] = strdup(values[count]);
237 for (count = 0; count < MAXSGR; ++count) {
238 if (values[count] != 0) {
239 for (j = count + 1; j < MAXSGR; ++j) {
242 if (strcmp(values[count], values[j]))
244 if (one_bit(count, j)) {
251 for (j = 0; j < MAXPAR; ++j) {
252 unsigned mask = (unsigned) (1 << j);
253 for (count = 0; count < MAXSGR; ++count) {
254 if ((count & mask) != 0)
256 if (values[count] != 0 && values[count + mask] != 0) {
263 /* smso is tested first, but often duplicates bold or reverse. */
264 if (VALID_STRING(my_smso)) {
265 if (VALID_STRING(my_bold) && !strcmp(my_bold, my_smso)) {
269 if (VALID_STRING(my_revs) && !strcmp(my_revs, my_smso)) {
274 for (count = 0; count < MAXSGR; ++count) {
275 if (values[count] != 0) {
277 if ((repeat & MASK_SMSO) != 0
278 && (count & MASK_SMSO) != 0) {
281 for (j = 0; j < count; ++j) {
282 if (values[j] != 0 && !strcmp(values[j], values[count])) {
283 if ((repeat & MASK_SMSO) != 0
284 && (j & MASK_SMSO) != 0
285 && (count & reason) != 0) {
294 dumpit(count, ignore, values[count], my_sgr0);
299 for (count = 0; count < MAXSGR; ++count) {
304 del_curterm(cur_term);
310 static const char *msg[] =
312 "Usage: test_sgr [options] [terminal]",
314 "Print all distinct combinations of sgr capability.",
317 " -d LIST colon-separated list of databases to use",
318 " -e NAME environment variable to set with -d option",
319 " -n do not initialize terminal, to test error-checking",
320 " -q quiet (prints only counts)",
323 for (n = 0; n < SIZEOF(msg); ++n) {
324 fprintf(stderr, "%s\n", msg[n]);
326 ExitProgram(EXIT_FAILURE);
330 main(int argc, char *argv[])
335 while ((n = getopt(argc, argv, "d:e:nq")) != -1) {
358 for (n = optind; n < argc; ++n) {
359 brute_force(argv[n]);
361 } else if ((name = getenv("TERM")) != 0) {
364 static char dumb[] = "dumb";
368 printf("%ld distinct values\n", total_values);
374 ExitProgram(EXIT_SUCCESS);
377 #else /* !HAVE_TIGETSTR */
379 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
381 failed("This program requires the terminfo functions such as tigetstr");
382 ExitProgram(EXIT_FAILURE);
384 #endif /* HAVE_TIGETSTR */