1 /****************************************************************************
2 * Copyright 2020-2022,2023 Thomas E. Dickey *
3 * Copyright 2015,2016 Free Software Foundation, Inc. *
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: *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
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. *
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 *
28 ****************************************************************************/
31 * Author: Thomas E. Dickey
33 * $Id: test_setupterm.c,v 1.17 2023/06/24 14:19:52 tom Exp $
35 * A simple demo of setupterm/restartterm.
37 #include <test.priv.h>
41 static bool a_opt = FALSE;
42 static bool f_opt = FALSE;
43 static bool n_opt = FALSE;
44 static bool r_opt = FALSE;
47 static TERMINAL **saved_terminals;
48 static size_t num_saved;
49 static size_t max_saved;
52 failed(const char *msg)
55 ExitProgram(EXIT_FAILURE);
62 for (n = 0; n < num_saved; ++n)
63 del_curterm(saved_terminals[n]);
64 free(saved_terminals);
73 for (n = 0; n < num_saved; ++n) {
74 if (saved_terminals[n] == cur_term) {
80 if (num_saved + 1 >= max_saved) {
82 saved_terminals = typeRealloc(TERMINAL *, max_saved, saved_terminals);
83 if (saved_terminals == NULL)
86 saved_terminals[num_saved++] = cur_term;
91 #define finish(code) ExitProgram(code)
92 #define save_curterm() /* nothing */
96 test_rc(NCURSES_CONST char *name, int actual_rc, int actual_err)
102 name = getenv("TERM");
107 case 'v': /* vt100 is normal */
108 case 'd': /* dumb has no special flags */
112 case 'l': /* lpr is hardcopy */
115 case 'u': /* unknown is generic */
126 ((actual_rc == expect_rc && actual_err == expect_err)
129 printf(" '%s'", name);
130 if (actual_rc == expect_rc) {
131 printf(" rc=%d", actual_rc);
133 printf(" rc=%d (%d)", actual_rc, expect_rc);
135 if (actual_err == expect_err) {
136 printf(" err=%d", actual_err);
138 printf(" err=%d (%d)", actual_err, expect_err);
144 test_setupterm(NCURSES_CONST char *name)
151 rc = restartterm(name, 0, f_opt ? NULL : &err);
154 rc = setupterm(name, 0, f_opt ? NULL : &err);
155 test_rc(name, rc, err);
162 static const char *msg[] =
164 "Usage: test_setupterm [options] [terminal]"
167 ,"Demonstrate error-checking for setupterm and restartterm."
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"
175 ," -r test restartterm rather than setupterm"
179 for (n = 0; n < SIZEOF(msg); ++n) {
180 fprintf(stderr, "%s\n", msg[n]);
182 finish(ok ? EXIT_SUCCESS : EXIT_FAILURE);
189 main(int argc, char *argv[])
194 while ((ch = getopt(argc, argv, OPTS_COMMON "afnr")) != -1) {
212 ExitProgram(EXIT_SUCCESS);
214 usage(ch == OPTS_USAGE);
220 static char none[][25] =
227 * We can turn this off, but not on again, because ncurses caches the
228 * directory locations.
230 printf("** without database\n");
231 for (n = 0; n < 3; ++n)
234 printf("** with database\n");
238 * The restartterm relies on an existing screen, so we make one here.
241 newterm("ansi", stdout, stdin);
247 static char predef[][9] =
248 {"vt100", "dumb", "lpr", "unknown", "none-such"};
252 for (n = 0; n < 4; ++n) {
253 test_setupterm(predef[n]);
257 for (n = optind; n < argc; ++n) {
258 test_setupterm(argv[n]);
261 test_setupterm(NULL);
265 finish(EXIT_SUCCESS);
268 #else /* !HAVE_TIGETSTR */
272 printf("This program requires the terminfo functions such as tigetstr\n");
273 ExitProgram(EXIT_FAILURE);
275 #endif /* HAVE_TIGETSTR */