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