1 /****************************************************************************
2 * Copyright 2020,2022 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.16 2022/12/10 23:23:27 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;
55 for (n = 0; n < num_saved; ++n)
56 del_curterm(saved_terminals[n]);
57 free(saved_terminals);
66 for (n = 0; n < num_saved; ++n) {
67 if (saved_terminals[n] == cur_term) {
73 if (num_saved + 1 >= max_saved) {
75 saved_terminals = typeRealloc(TERMINAL *, max_saved, saved_terminals);
77 saved_terminals[num_saved++] = cur_term;
82 #define finish(code) ExitProgram(code)
83 #define save_curterm() /* nothing */
87 test_rc(NCURSES_CONST char *name, int actual_rc, int actual_err)
93 name = getenv("TERM");
98 case 'v': /* vt100 is normal */
99 case 'd': /* dumb has no special flags */
103 case 'l': /* lpr is hardcopy */
106 case 'u': /* unknown is generic */
117 ((actual_rc == expect_rc && actual_err == expect_err)
120 printf(" '%s'", name);
121 if (actual_rc == expect_rc) {
122 printf(" rc=%d", actual_rc);
124 printf(" rc=%d (%d)", actual_rc, expect_rc);
126 if (actual_err == expect_err) {
127 printf(" err=%d", actual_err);
129 printf(" err=%d (%d)", actual_err, expect_err);
135 test_setupterm(NCURSES_CONST char *name)
142 rc = restartterm(name, 0, f_opt ? NULL : &err);
145 rc = setupterm(name, 0, f_opt ? NULL : &err);
146 test_rc(name, rc, err);
153 static const char *msg[] =
155 "Usage: test_setupterm [options] [terminal]"
158 ,"Demonstrate error-checking for setupterm and restartterm."
161 ," -a automatic test for each success/error code"
162 ," -f treat errors as fatal"
163 ," -n set environment to disable terminfo database, assuming"
164 ," the compiled-in paths for database also fail"
166 ," -r test restartterm rather than setupterm"
170 for (n = 0; n < SIZEOF(msg); ++n) {
171 fprintf(stderr, "%s\n", msg[n]);
173 finish(ok ? EXIT_SUCCESS : EXIT_FAILURE);
180 main(int argc, char *argv[])
185 while ((ch = getopt(argc, argv, OPTS_COMMON "afnr")) != -1) {
203 ExitProgram(EXIT_SUCCESS);
205 usage(ch == OPTS_USAGE);
211 static char none[][25] =
218 * We can turn this off, but not on again, because ncurses caches the
219 * directory locations.
221 printf("** without database\n");
222 for (n = 0; n < 3; ++n)
225 printf("** with database\n");
229 * The restartterm relies on an existing screen, so we make one here.
232 newterm("ansi", stdout, stdin);
238 static char predef[][9] =
239 {"vt100", "dumb", "lpr", "unknown", "none-such"};
243 for (n = 0; n < 4; ++n) {
244 test_setupterm(predef[n]);
248 for (n = optind; n < argc; ++n) {
249 test_setupterm(argv[n]);
252 test_setupterm(NULL);
256 finish(EXIT_SUCCESS);
259 #else /* !HAVE_TIGETSTR */
263 printf("This program requires the terminfo functions such as tigetstr\n");
264 ExitProgram(EXIT_FAILURE);
266 #endif /* HAVE_TIGETSTR */