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