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