]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/test_termattrs.c
ncurses 6.0 - patch 20171014
[ncurses.git] / test / test_termattrs.c
1 /****************************************************************************
2  * Copyright (c) 2017 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  * $Id: test_termattrs.c,v 1.2 2017/10/11 08:16:02 tom Exp $
30  *
31  * Demonstrate the termattrs and term_attrs functions.
32  */
33
34 #define USE_TINFO
35 #include <test.priv.h>
36
37 #if HAVE_SETUPTERM
38
39 static FILE *my_fp;
40
41 static void
42 test_termattrs(unsigned long value)
43 {
44 #define DATA(name) { name, #name }
45     static struct {
46         unsigned long code;
47         const char *name;
48     } table[] = {
49 #ifdef A_ATTRIBUTES
50         DATA(A_ATTRIBUTES),
51 #endif
52 #ifdef A_CHARTEXT
53             DATA(A_CHARTEXT),
54 #endif
55 #ifdef A_COLOR
56             DATA(A_COLOR),
57 #endif
58 #ifdef A_STANDOUT
59             DATA(A_STANDOUT),
60 #endif
61 #ifdef A_UNDERLINE
62             DATA(A_UNDERLINE),
63 #endif
64 #ifdef A_REVERSE
65             DATA(A_REVERSE),
66 #endif
67 #ifdef A_BLINK
68             DATA(A_BLINK),
69 #endif
70 #ifdef A_DIM
71             DATA(A_DIM),
72 #endif
73 #ifdef A_BOLD
74             DATA(A_BOLD),
75 #endif
76 #ifdef A_ALTCHARSET
77             DATA(A_ALTCHARSET),
78 #endif
79 #ifdef A_INVIS
80             DATA(A_INVIS),
81 #endif
82 #ifdef A_PROTECT
83             DATA(A_PROTECT),
84 #endif
85 #ifdef A_HORIZONTAL
86             DATA(A_HORIZONTAL),
87 #endif
88 #ifdef A_LEFT
89             DATA(A_LEFT),
90 #endif
91 #ifdef A_LOW
92             DATA(A_LOW),
93 #endif
94 #ifdef A_RIGHT
95             DATA(A_RIGHT),
96 #endif
97 #ifdef A_TOP
98             DATA(A_TOP),
99 #endif
100 #ifdef A_VERTICAL
101             DATA(A_VERTICAL),
102 #endif
103 #ifdef A_ITALIC
104             DATA(A_ITALIC),
105 #endif
106     };
107     size_t n;
108     fprintf(my_fp, "Result: %08lX\r\n", value);
109     for (n = 0; n < SIZEOF(table); ++n) {
110         if ((value & table[n].code) != 0) {
111             fprintf(my_fp, "%08lX %08lX %s\r\n",
112                     table[n].code, value & table[n].code, table[n].name);
113         }
114     };
115     fputs("\r\n", my_fp);
116 }
117
118 static void
119 usage(void)
120 {
121     static const char *tbl[] =
122     {
123         "Usage: test_termattrs [options]"
124         ,""
125         ,"Options:"
126         ,"  -e      use stderr (default stdout)"
127         ,"  -n      do not initialize terminal"
128         ,"  -s      use setupterm rather than newterm"
129 #if USE_WIDEC_SUPPORT
130         ,"  -w      use term_attrs rather than termattrs"
131 #endif
132     };
133     unsigned n;
134     for (n = 0; n < SIZEOF(tbl); ++n)
135         fprintf(stderr, "%s\n", tbl[n]);
136     ExitProgram(EXIT_FAILURE);
137 }
138
139 int
140 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
141 {
142     int ch;
143     bool no_init = FALSE;
144     bool s_opt = FALSE;
145 #if USE_WIDEC_SUPPORT
146     bool w_opt = FALSE;
147 #endif
148
149     my_fp = stdout;
150
151     while ((ch = getopt(argc, argv, "ensw")) != -1) {
152         switch (ch) {
153         case 'e':
154             my_fp = stderr;
155             break;
156         case 'n':
157             no_init = TRUE;
158             break;
159         case 's':
160             s_opt = TRUE;
161             break;
162 #if USE_WIDEC_SUPPORT
163         case 'w':
164             w_opt = TRUE;
165             break;
166 #endif
167         default:
168             usage();
169             break;
170         }
171     }
172     if (optind < argc)
173         usage();
174
175     if (no_init) {
176         START_TRACE();
177     } else if (s_opt) {
178         setupterm((char *) 0, fileno(my_fp), (int *) 0);
179     } else {
180         newterm((char *) 0, my_fp, stdin);
181     }
182 #if USE_WIDEC_SUPPORT
183     if (w_opt)
184         test_termattrs((unsigned long) term_attrs());
185     else
186 #endif
187         test_termattrs((unsigned long) termattrs());
188     ExitProgram(EXIT_SUCCESS);
189 }
190
191 #else
192 int
193 main(int argc GCC_UNUSED,
194      char *argv[]GCC_UNUSED)
195 {
196     fprintf(stderr, "This program requires terminfo\n");
197     exit(EXIT_FAILURE);
198 }
199 #endif