]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/test_termattrs.c
ncurses 6.2 - patch 20200711
[ncurses.git] / test / test_termattrs.c
1 /****************************************************************************
2  * Copyright 2020 Thomas E. Dickey                                          *
3  * Copyright 2017 Free Software Foundation, Inc.                            *
4  *                                                                          *
5  * Permission is hereby granted, free of charge, to any person obtaining a  *
6  * copy of this software and associated documentation files (the            *
7  * "Software"), to deal in the Software without restriction, including      *
8  * without limitation the rights to use, copy, modify, merge, publish,      *
9  * distribute, distribute with modifications, sublicense, and/or sell       *
10  * copies of the Software, and to permit persons to whom the Software is    *
11  * furnished to do so, subject to the following conditions:                 *
12  *                                                                          *
13  * The above copyright notice and this permission notice shall be included  *
14  * in all copies or substantial portions of the Software.                   *
15  *                                                                          *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
19  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
20  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
21  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
23  *                                                                          *
24  * Except as contained in this notice, the name(s) of the above copyright   *
25  * holders shall not be used in advertising or otherwise to promote the     *
26  * sale, use or other dealings in this Software without prior written       *
27  * authorization.                                                           *
28  ****************************************************************************/
29 /*
30  * $Id: test_termattrs.c,v 1.3 2020/02/02 23:34:34 tom Exp $
31  *
32  * Demonstrate the termattrs and term_attrs functions.
33  */
34
35 #define USE_TINFO
36 #include <test.priv.h>
37
38 #if HAVE_SETUPTERM
39
40 static FILE *my_fp;
41
42 static void
43 test_termattrs(unsigned long value)
44 {
45 #define DATA(name) { name, #name }
46     static struct {
47         unsigned long code;
48         const char *name;
49     } table[] = {
50 #ifdef A_ATTRIBUTES
51         DATA(A_ATTRIBUTES),
52 #endif
53 #ifdef A_CHARTEXT
54             DATA(A_CHARTEXT),
55 #endif
56 #ifdef A_COLOR
57             DATA(A_COLOR),
58 #endif
59 #ifdef A_STANDOUT
60             DATA(A_STANDOUT),
61 #endif
62 #ifdef A_UNDERLINE
63             DATA(A_UNDERLINE),
64 #endif
65 #ifdef A_REVERSE
66             DATA(A_REVERSE),
67 #endif
68 #ifdef A_BLINK
69             DATA(A_BLINK),
70 #endif
71 #ifdef A_DIM
72             DATA(A_DIM),
73 #endif
74 #ifdef A_BOLD
75             DATA(A_BOLD),
76 #endif
77 #ifdef A_ALTCHARSET
78             DATA(A_ALTCHARSET),
79 #endif
80 #ifdef A_INVIS
81             DATA(A_INVIS),
82 #endif
83 #ifdef A_PROTECT
84             DATA(A_PROTECT),
85 #endif
86 #ifdef A_HORIZONTAL
87             DATA(A_HORIZONTAL),
88 #endif
89 #ifdef A_LEFT
90             DATA(A_LEFT),
91 #endif
92 #ifdef A_LOW
93             DATA(A_LOW),
94 #endif
95 #ifdef A_RIGHT
96             DATA(A_RIGHT),
97 #endif
98 #ifdef A_TOP
99             DATA(A_TOP),
100 #endif
101 #ifdef A_VERTICAL
102             DATA(A_VERTICAL),
103 #endif
104 #ifdef A_ITALIC
105             DATA(A_ITALIC),
106 #endif
107     };
108     size_t n;
109     fprintf(my_fp, "Result: %08lX\r\n", value);
110     for (n = 0; n < SIZEOF(table); ++n) {
111         if ((value & table[n].code) != 0) {
112             fprintf(my_fp, "%08lX %08lX %s\r\n",
113                     table[n].code, value & table[n].code, table[n].name);
114         }
115     };
116     fputs("\r\n", my_fp);
117 }
118
119 static void
120 usage(void)
121 {
122     static const char *tbl[] =
123     {
124         "Usage: test_termattrs [options]"
125         ,""
126         ,"Options:"
127         ,"  -e      use stderr (default stdout)"
128         ,"  -n      do not initialize terminal"
129         ,"  -s      use setupterm rather than newterm"
130 #if USE_WIDEC_SUPPORT
131         ,"  -w      use term_attrs rather than termattrs"
132 #endif
133     };
134     unsigned n;
135     for (n = 0; n < SIZEOF(tbl); ++n)
136         fprintf(stderr, "%s\n", tbl[n]);
137     ExitProgram(EXIT_FAILURE);
138 }
139
140 int
141 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
142 {
143     int ch;
144     bool no_init = FALSE;
145     bool s_opt = FALSE;
146 #if USE_WIDEC_SUPPORT
147     bool w_opt = FALSE;
148 #endif
149
150     my_fp = stdout;
151
152     while ((ch = getopt(argc, argv, "ensw")) != -1) {
153         switch (ch) {
154         case 'e':
155             my_fp = stderr;
156             break;
157         case 'n':
158             no_init = TRUE;
159             break;
160         case 's':
161             s_opt = TRUE;
162             break;
163 #if USE_WIDEC_SUPPORT
164         case 'w':
165             w_opt = TRUE;
166             break;
167 #endif
168         default:
169             usage();
170             break;
171         }
172     }
173     if (optind < argc)
174         usage();
175
176     if (no_init) {
177         START_TRACE();
178     } else if (s_opt) {
179         setupterm((char *) 0, fileno(my_fp), (int *) 0);
180     } else {
181         newterm((char *) 0, my_fp, stdin);
182     }
183 #if USE_WIDEC_SUPPORT
184     if (w_opt)
185         test_termattrs((unsigned long) term_attrs());
186     else
187 #endif
188         test_termattrs((unsigned long) termattrs());
189     ExitProgram(EXIT_SUCCESS);
190 }
191
192 #else
193 int
194 main(int argc GCC_UNUSED,
195      char *argv[]GCC_UNUSED)
196 {
197     fprintf(stderr, "This program requires terminfo\n");
198     exit(EXIT_FAILURE);
199 }
200 #endif