]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/test_sgr.c
ncurses 6.0 - patch 20161008
[ncurses.git] / test / test_sgr.c
1 /****************************************************************************
2  * Copyright (c) 2015,2016 Free Software Foundation, Inc.                   *
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_sgr.c,v 1.8 2016/09/04 23:30:56 tom Exp $
33  *
34  * A simple demo of the sgr/sgr0 terminal capabilities.
35  */
36 #define USE_TINFO
37 #include <test.priv.h>
38
39 #if !HAVE_TIGETSTR
40 static void failed(const char *) GCC_NORETURN;
41
42 static void
43 failed(const char *msg)
44 {
45     fprintf(stderr, "%s\n", msg);
46     ExitProgram(EXIT_FAILURE);
47 }
48 #endif
49
50 #if HAVE_TIGETSTR
51
52 static bool q_opt = FALSE;
53
54 static char *d_opt;
55 static char *e_opt;
56 static char **db_list;
57 static int db_item;
58
59 static long total_values;
60
61 static char *
62 make_dbitem(char *p, char *q)
63 {
64     size_t need = strlen(e_opt) + 2 + (size_t) (p - q);
65     char *result = malloc(need);
66     _nc_SPRINTF(result, _nc_SLIMIT(need) "%s=%.*s", e_opt, (int) (p - q), q);
67     return result;
68 }
69
70 static void
71 make_dblist(void)
72 {
73     if (d_opt && e_opt) {
74         int pass;
75
76         for (pass = 0; pass < 2; ++pass) {
77             char *p, *q;
78             size_t count = 0;
79
80             for (p = q = d_opt; *p != '\0'; ++p) {
81                 if (*p == ':') {
82                     if (p != q + 1) {
83                         if (pass) {
84                             db_list[count] = make_dbitem(p, q);
85                         }
86                         count++;
87                     }
88                     q = p + 1;
89                 }
90             }
91             if (p != q + 1) {
92                 if (pass) {
93                     db_list[count] = make_dbitem(p, q);
94                 }
95                 count++;
96             }
97             if (!pass) {
98                 db_list = typeCalloc(char *, count + 1);
99             }
100         }
101     }
102 }
103
104 static char *
105 next_dbitem(void)
106 {
107     char *result = 0;
108
109     if (db_list) {
110         if ((result = db_list[db_item]) == 0) {
111             db_item = 0;
112             result = db_list[0];
113         } else {
114             db_item++;
115         }
116     }
117     printf("** %s\n", result);
118     return result;
119 }
120
121 #ifdef NO_LEAKS
122 static void
123 free_dblist(void)
124 {
125     if (db_list) {
126         int n;
127         for (n = 0; db_list[n]; ++n)
128             free(db_list[n]);
129         free(db_list);
130         db_list = 0;
131     }
132 }
133 #endif
134
135 #define MAXPAR    9
136 #define MAXSGR    (1 << MAXPAR)
137 #define BITS2P(n) (count & (1 << (n - 1)))
138 #define MASK_SMSO (1 << 0)
139 #define MASK_BOLD (1 << 5)
140 #define MASK_REV  (1 << 2)
141
142 static void
143 dumpit(unsigned bits, unsigned ignore, const char *sgr, const char *sgr0)
144 {
145     static const char sample[] = "abcdefghijklm";
146     static char params[] = "SURBDBIPA";
147     unsigned n;
148
149     printf("%4d ", bits);
150     bits &= ~ignore;
151     for (n = 0; n < MAXPAR; ++n) {
152         putchar((int) ((bits & (unsigned) (1 << n)) ? params[n] : '-'));
153     }
154     putchar(' ');
155     putp(sgr);
156     putp(sample);
157     putp(sgr0);
158     putchar('\n');
159 }
160
161 static bool
162 one_bit(unsigned a, unsigned b)
163 {
164     unsigned c = (a ^ b);
165     bool result = FALSE;
166     if (c) {
167         while (!(c & 1)) {
168             c >>= 1;
169         }
170         result = (c == 1);
171     }
172     return result;
173 }
174
175 static void
176 brute_force(const char *name)
177 {
178     unsigned count;
179     char *my_sgr;
180     char *my_sgr0;
181     char *my_bold;
182     char *my_revs;
183     char *my_smso;
184
185     if (db_list) {
186         putenv(next_dbitem());
187     }
188     if (!q_opt)
189         printf("Terminal type \"%s\"\n", name);
190     setupterm((NCURSES_CONST char *) name, 1, (int *) 0);
191     if (!q_opt) {
192         if (strcmp(name, ttytype))
193             printf("... actual \"%s\"\n", ttytype);
194     }
195
196     my_sgr = tigetstr("sgr");
197     my_sgr0 = tigetstr("sgr0");
198     my_bold = tigetstr("bold");
199     my_revs = tigetstr("rev");
200     my_smso = tigetstr("smso");
201
202     if (!VALID_STRING(my_sgr)) {
203         fprintf(stderr, "no \"sgr\" capability found\n");
204     } else if (!VALID_STRING(my_sgr0)) {
205         fprintf(stderr, "no \"sgr0\" capability found\n");
206     } else {
207         char *values[MAXSGR + MAXPAR];
208         unsigned j;
209         unsigned ignore = 0;
210         unsigned reason = 0;
211         unsigned repeat = 0;
212         for (count = 0; count < MAXSGR; ++count) {
213             values[count] = tparm(my_sgr,
214                                   BITS2P(1),
215                                   BITS2P(2),
216                                   BITS2P(3),
217                                   BITS2P(4),
218                                   BITS2P(5),
219                                   BITS2P(6),
220                                   BITS2P(7),
221                                   BITS2P(8),
222                                   BITS2P(9));
223             if (values[count] != 0) {
224                 values[count] = strdup(values[count]);
225             }
226         }
227         for (count = 0; count < MAXSGR; ++count) {
228             if (values[count] != 0) {
229                 for (j = count + 1; j < MAXSGR; ++j) {
230                     if (values[j] == 0)
231                         continue;
232                     if (strcmp(values[count], values[j]))
233                         continue;
234                     if (one_bit(count, j)) {
235                         free(values[j]);
236                         values[j] = 0;
237                     }
238                 }
239             }
240         }
241         for (j = 0; j < MAXPAR; ++j) {
242             unsigned mask = (unsigned) (1 << j);
243             for (count = 0; count < MAXSGR; ++count) {
244                 if ((count & mask) != 0)
245                     continue;
246                 if (values[count] != 0 && values[count + mask] != 0) {
247                     mask = 0;
248                     break;
249                 }
250             }
251             ignore |= mask;
252         }
253         /* smso is tested first, but often duplicates bold or reverse. */
254         if (VALID_STRING(my_smso)) {
255             if (VALID_STRING(my_bold) && !strcmp(my_bold, my_smso)) {
256                 repeat |= MASK_SMSO;
257                 reason = MASK_BOLD;
258             }
259             if (VALID_STRING(my_revs) && !strcmp(my_revs, my_smso)) {
260                 repeat |= MASK_SMSO;
261                 reason = MASK_REV;
262             }
263         }
264         for (count = 0; count < MAXSGR; ++count) {
265             if (values[count] != 0) {
266                 bool found = FALSE;
267                 if ((repeat & MASK_SMSO) != 0
268                     && (count & MASK_SMSO) != 0) {
269                     found = TRUE;
270                 } else {
271                     for (j = 0; j < count; ++j) {
272                         if (values[j] != 0 && !strcmp(values[j], values[count])) {
273                             if ((repeat & MASK_SMSO) != 0
274                                 && (j & MASK_SMSO) != 0
275                                 && (count & reason) != 0) {
276                                 continue;
277                             }
278                             found = TRUE;
279                             break;
280                         }
281                     }
282                 }
283                 if (!found) {
284                     dumpit(count, ignore, values[count], my_sgr0);
285                     ++total_values;
286                 }
287             }
288         }
289     }
290     del_curterm(cur_term);
291 }
292
293 static void
294 usage(void)
295 {
296     static const char *msg[] =
297     {
298         "Usage: test_sgr [options] [terminal]",
299         "",
300         "Print all distinct combinations of sgr capability.",
301         "",
302         "Options:",
303         " -d LIST  colon-separated list of databases to use",
304         " -e NAME  environment variable to set with -d option",
305         " -q       quiet (prints only counts)",
306     };
307     unsigned n;
308     for (n = 0; n < SIZEOF(msg); ++n) {
309         fprintf(stderr, "%s\n", msg[n]);
310     }
311     ExitProgram(EXIT_FAILURE);
312 }
313
314 int
315 main(int argc, char *argv[])
316 {
317     int n;
318     char *name;
319
320     while ((n = getopt(argc, argv, "d:e:q")) != -1) {
321         switch (n) {
322         case 'd':
323             d_opt = optarg;
324             break;
325         case 'e':
326             e_opt = optarg;
327             break;
328         case 'q':
329             q_opt = TRUE;
330             break;
331         default:
332             usage();
333             break;
334         }
335     }
336
337     make_dblist();
338
339     if (optind < argc) {
340         for (n = optind; n < argc; ++n) {
341             brute_force(argv[n]);
342         }
343     } else if ((name = getenv("TERM")) != 0) {
344         brute_force(name);
345     } else {
346         static char dumb[] = "dumb";
347         brute_force(dumb);
348     }
349
350     printf("%ld distinct values\n", total_values);
351
352 #ifdef NO_LEAKS
353     free_dblist();
354 #endif
355
356     ExitProgram(EXIT_SUCCESS);
357 }
358
359 #else /* !HAVE_TIGETSTR */
360 int
361 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
362 {
363     failed("This program requires the terminfo functions such as tigetstr");
364     ExitProgram(EXIT_FAILURE);
365 }
366 #endif /* HAVE_TIGETSTR */