1 /****************************************************************************
2 * Copyright 2020-2022,2023 Thomas E. Dickey *
3 * Copyright 2015,2016 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 /*
31 * Author: Thomas E. Dickey
32 *
33 * $Id: test_setupterm.c,v 1.17 2023/06/24 14:19:52 tom Exp $
34 *
35 * A simple demo of setupterm/restartterm.
36 */
37 #include <test.priv.h>
38
39 #if HAVE_TIGETSTR
40
41 static bool a_opt = FALSE;
42 static bool f_opt = FALSE;
43 static bool n_opt = FALSE;
44 static bool r_opt = FALSE;
45
46 #if NO_LEAKS
47 static TERMINAL **saved_terminals;
48 static size_t num_saved;
49 static size_t max_saved;
50
51 static void
failed(const char * msg)52 failed(const char *msg)
53 {
54 perror(msg);
55 ExitProgram(EXIT_FAILURE);
56 }
57
58 static void
finish(int code)59 finish(int code)
60 {
61 size_t n;
62 for (n = 0; n < num_saved; ++n)
63 del_curterm(saved_terminals[n]);
64 free(saved_terminals);
65 ExitProgram(code);
66 }
67
68 static void
save_curterm(void)69 save_curterm(void)
70 {
71 size_t n;
72 bool found = FALSE;
73 for (n = 0; n < num_saved; ++n) {
74 if (saved_terminals[n] == cur_term) {
75 found = TRUE;
76 break;
77 }
78 }
79 if (!found) {
80 if (num_saved + 1 >= max_saved) {
81 max_saved += 100;
82 saved_terminals = typeRealloc(TERMINAL *, max_saved, saved_terminals);
83 if (saved_terminals == NULL)
84 failed("realloc");
85 }
86 saved_terminals[num_saved++] = cur_term;
87 }
88 }
89
90 #else
91 #define finish(code) ExitProgram(code)
92 #define save_curterm() /* nothing */
93 #endif
94
95 static void
test_rc(NCURSES_CONST char * name,int actual_rc,int actual_err)96 test_rc(NCURSES_CONST char *name, int actual_rc, int actual_err)
97 {
98 int expect_rc = -1;
99 int expect_err = -1;
100
101 if (name == 0)
102 name = getenv("TERM");
103 if (name == 0)
104 name = "?";
105
106 switch (*name) {
107 case 'v': /* vt100 is normal */
108 case 'd': /* dumb has no special flags */
109 expect_rc = 0;
110 expect_err = 1;
111 break;
112 case 'l': /* lpr is hardcopy */
113 expect_err = 1;
114 break;
115 case 'u': /* unknown is generic */
116 expect_err = 0;
117 break;
118 default:
119 break;
120 }
121 if (n_opt) {
122 expect_rc = -1;
123 expect_err = -1;
124 }
125 printf("%s",
126 ((actual_rc == expect_rc && actual_err == expect_err)
127 ? "OK"
128 : "ERR"));
129 printf(" '%s'", name);
130 if (actual_rc == expect_rc) {
131 printf(" rc=%d", actual_rc);
132 } else {
133 printf(" rc=%d (%d)", actual_rc, expect_rc);
134 }
135 if (actual_err == expect_err) {
136 printf(" err=%d", actual_err);
137 } else {
138 printf(" err=%d (%d)", actual_err, expect_err);
139 }
140 printf("\n");
141 }
142
143 static void
test_setupterm(NCURSES_CONST char * name)144 test_setupterm(NCURSES_CONST char *name)
145 {
146 int rc;
147 int err = -99;
148
149 #if HAVE_RESTARTTERM
150 if (r_opt)
151 rc = restartterm(name, 0, f_opt ? NULL : &err);
152 else
153 #endif
154 rc = setupterm(name, 0, f_opt ? NULL : &err);
155 test_rc(name, rc, err);
156 save_curterm();
157 }
158
159 static void
usage(int ok)160 usage(int ok)
161 {
162 static const char *msg[] =
163 {
164 "Usage: test_setupterm [options] [terminal]"
165 ,""
166 ,USAGE_COMMON
167 ,"Demonstrate error-checking for setupterm and restartterm."
168 ,""
169 ,"Options:"
170 ," -a automatic test for each success/error code"
171 ," -f treat errors as fatal"
172 ," -n set environment to disable terminfo database, assuming"
173 ," the compiled-in paths for database also fail"
174 #if HAVE_RESTARTTERM
175 ," -r test restartterm rather than setupterm"
176 #endif
177 };
178 unsigned n;
179 for (n = 0; n < SIZEOF(msg); ++n) {
180 fprintf(stderr, "%s\n", msg[n]);
181 }
182 finish(ok ? EXIT_SUCCESS : EXIT_FAILURE);
183 }
184 /* *INDENT-OFF* */
VERSION_COMMON()185 VERSION_COMMON()
186 /* *INDENT-ON* */
187
188 int
189 main(int argc, char *argv[])
190 {
191 int ch;
192 int n;
193
194 while ((ch = getopt(argc, argv, OPTS_COMMON "afnr")) != -1) {
195 switch (ch) {
196 case 'a':
197 a_opt = TRUE;
198 break;
199 case 'f':
200 f_opt = TRUE;
201 break;
202 case 'n':
203 n_opt = TRUE;
204 break;
205 #if HAVE_RESTARTTERM
206 case 'r':
207 r_opt = TRUE;
208 break;
209 #endif
210 case OPTS_VERSION:
211 show_version(argv);
212 ExitProgram(EXIT_SUCCESS);
213 default:
214 usage(ch == OPTS_USAGE);
215 /* NOTREACHED */
216 }
217 }
218
219 if (n_opt) {
220 static char none[][25] =
221 {
222 "HOME=/GUI",
223 "TERMINFO=/GUI",
224 "TERMINFO_DIRS=/GUI"
225 };
226 /*
227 * We can turn this off, but not on again, because ncurses caches the
228 * directory locations.
229 */
230 printf("** without database\n");
231 for (n = 0; n < 3; ++n)
232 putenv(none[n]);
233 } else {
234 printf("** with database\n");
235 }
236
237 /*
238 * The restartterm relies on an existing screen, so we make one here.
239 */
240 if (r_opt) {
241 newterm("ansi", stdout, stdin);
242 reset_shell_mode();
243 save_curterm();
244 }
245
246 if (a_opt) {
247 static char predef[][9] =
248 {"vt100", "dumb", "lpr", "unknown", "none-such"};
249 if (optind < argc) {
250 usage(FALSE);
251 }
252 for (n = 0; n < 4; ++n) {
253 test_setupterm(predef[n]);
254 }
255 } else {
256 if (optind < argc) {
257 for (n = optind; n < argc; ++n) {
258 test_setupterm(argv[n]);
259 }
260 } else {
261 test_setupterm(NULL);
262 }
263 }
264
265 finish(EXIT_SUCCESS);
266 }
267
268 #else /* !HAVE_TIGETSTR */
269 int
main(void)270 main(void)
271 {
272 printf("This program requires the terminfo functions such as tigetstr\n");
273 ExitProgram(EXIT_FAILURE);
274 }
275 #endif /* HAVE_TIGETSTR */
276