1 /****************************************************************************
2 * Copyright (c) 2015,2016 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.5 2016/01/09 18:15:53 tom Exp $
34 * A simple demo of the sgr/sgr0 terminal capabilities.
37 #include <test.priv.h>
39 static void failed(const char *) GCC_NORETURN;
42 failed(const char *msg)
44 fprintf(stderr, "%s\n", msg);
45 ExitProgram(EXIT_FAILURE);
50 static bool q_opt = FALSE;
54 static char **db_list;
57 static long total_values;
60 make_dbitem(char *p, char *q)
62 char *result = malloc(strlen(e_opt) + 2 + (size_t) (p - q));
63 sprintf(result, "%s=%.*s", e_opt, (int) (p - q), q);
73 for (pass = 0; pass < 2; ++pass) {
77 for (p = q = d_opt; *p != '\0'; ++p) {
81 db_list[count] = make_dbitem(p, q);
90 db_list[count] = make_dbitem(p, q);
95 db_list = typeCalloc(char *, count + 1);
107 if ((result = db_list[db_item]) == 0) {
114 printf("** %s\n", result);
124 for (n = 0; db_list[n]; ++n)
133 #define MAXSGR (1 << MAXPAR)
134 #define BITS2P(n) (count & (1 << (n - 1)))
135 #define MASK_SMSO (1 << 0)
136 #define MASK_BOLD (1 << 5)
137 #define MASK_REV (1 << 2)
140 dumpit(unsigned bits, unsigned ignore, const char *sgr, const char *sgr0)
142 static const char sample[] = "abcdefghijklm";
143 static char params[] = "SURBDBIPA";
146 printf("%4d ", bits);
148 for (n = 0; n < MAXPAR; ++n) {
149 putchar((int) ((bits & (unsigned) (1 << n)) ? params[n] : '-'));
159 one_bit(unsigned a, unsigned b)
161 unsigned c = (a ^ b);
173 brute_force(const char *name)
183 putenv(next_dbitem());
186 printf("Terminal type \"%s\"\n", name);
187 setupterm((NCURSES_CONST char *) name, 1, (int *) 0);
189 if (strcmp(name, ttytype))
190 printf("... actual \"%s\"\n", ttytype);
193 my_sgr = tigetstr("sgr");
194 my_sgr0 = tigetstr("sgr0");
195 my_bold = tigetstr("bold");
196 my_revs = tigetstr("rev");
197 my_smso = tigetstr("smso");
199 if (!VALID_STRING(my_sgr)) {
200 fprintf(stderr, "no \"sgr\" capability found\n");
201 } else if (!VALID_STRING(my_sgr0)) {
202 fprintf(stderr, "no \"sgr0\" capability found\n");
204 char *values[MAXSGR];
209 for (count = 0; count < MAXSGR; ++count) {
210 values[count] = tparm(my_sgr,
220 if (values[count] != 0) {
221 values[count] = strdup(values[count]);
224 for (count = 0; count < MAXSGR; ++count) {
225 if (values[count] != 0) {
226 for (j = count + 1; j < MAXSGR; ++j) {
229 if (strcmp(values[count], values[j]))
231 if (one_bit(count, j)) {
238 for (j = 0; j < MAXPAR; ++j) {
239 unsigned mask = (unsigned) (1 << j);
240 for (count = 0; count < MAXSGR; ++count) {
241 if ((count & mask) != 0)
243 if (values[count] != 0 && values[count + mask] != 0) {
250 /* smso is tested first, but often duplicates bold or reverse. */
251 if (VALID_STRING(my_smso)) {
252 if (VALID_STRING(my_bold) && !strcmp(my_bold, my_smso)) {
256 if (VALID_STRING(my_revs) && !strcmp(my_revs, my_smso)) {
261 for (count = 0; count < MAXSGR; ++count) {
262 if (values[count] != 0) {
264 if ((repeat & MASK_SMSO) != 0
265 && (count & MASK_SMSO) != 0) {
268 for (j = 0; j < count; ++j) {
269 if (values[j] != 0 && !strcmp(values[j], values[count])) {
270 if ((repeat & MASK_SMSO) != 0
271 && (j & MASK_SMSO) != 0
272 && (count & reason) != 0) {
281 dumpit(count, ignore, values[count], my_sgr0);
287 del_curterm(cur_term);
293 static const char *msg[] =
295 "Usage: test_sgr [options] [terminal]",
297 "Print all distinct combinations of sgr capability.",
300 " -d LIST colon-separated list of databases to use",
301 " -e NAME environment variable to set with -d option",
302 " -q quiet (prints only counts)",
305 for (n = 0; n < SIZEOF(msg); ++n) {
306 fprintf(stderr, "%s\n", msg[n]);
308 ExitProgram(EXIT_FAILURE);
312 main(int argc, char *argv[])
317 while ((n = getopt(argc, argv, "d:e:q")) != -1) {
337 for (n = optind; n < argc; ++n) {
338 brute_force(argv[n]);
340 } else if ((name = getenv("TERM")) != 0) {
343 static char dumb[] = "dumb";
347 printf("%ld distinct values\n", total_values);
353 ExitProgram(EXIT_SUCCESS);
356 #else /* !HAVE_TIGETSTR */
358 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
360 failed("This program requires the terminfo functions such as tigetstr");
361 ExitProgram(EXIT_FAILURE);
363 #endif /* HAVE_TIGETSTR */