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