1 /****************************************************************************
2 * Copyright 2020 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.10 2020/02/02 23:34:34 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 test_rc(NCURSES_CONST char *name, int actual_rc, int actual_err)
53 name = getenv("TERM");
58 case 'v': /* vt100 is normal */
59 case 'd': /* dumb has no special flags */
63 case 'l': /* lpr is hardcopy */
66 case 'u': /* unknown is generic */
77 ((actual_rc == expect_rc && actual_err == expect_err)
80 printf(" '%s'", name);
81 if (actual_rc == expect_rc) {
82 printf(" rc=%d", actual_rc);
84 printf(" rc=%d (%d)", actual_rc, expect_rc);
86 if (actual_err == expect_err) {
87 printf(" err=%d", actual_err);
89 printf(" err=%d (%d)", actual_err, expect_err);
95 test_setupterm(NCURSES_CONST char *name)
102 rc = restartterm(name, 0, f_opt ? NULL : &err);
105 rc = setupterm(name, 0, f_opt ? NULL : &err);
106 test_rc(name, rc, err);
112 static const char *msg[] =
114 "Usage: test_setupterm [options] [terminal]",
116 "Demonstrate error-checking for setupterm and restartterm.",
119 " -a automatic test for each success/error code",
120 " -f treat errors as fatal",
121 " -n set environment to disable terminfo database, assuming",
122 " the compiled-in paths for database also fail",
124 " -r test restartterm rather than setupterm",
128 for (n = 0; n < SIZEOF(msg); ++n) {
129 fprintf(stderr, "%s\n", msg[n]);
131 ExitProgram(EXIT_FAILURE);
135 main(int argc, char *argv[])
139 while ((n = getopt(argc, argv, "afnr")) != -1) {
162 static char none[][25] =
169 * We can turn this off, but not on again, because ncurses caches the
170 * directory locations.
172 printf("** without database\n");
173 for (n = 0; n < 3; ++n)
176 printf("** with database\n");
180 * The restartterm relies on an existing screen, so we make one here.
183 newterm("ansi", stdout, stdin);
188 static char predef[][9] =
189 {"vt100", "dumb", "lpr", "unknown", "none-such"};
193 for (n = 0; n < 4; ++n) {
194 test_setupterm(predef[n]);
198 for (n = optind; n < argc; ++n) {
199 test_setupterm(argv[n]);
202 test_setupterm(NULL);
206 ExitProgram(EXIT_SUCCESS);
209 #else /* !HAVE_TIGETSTR */
211 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
213 printf("This program requires the terminfo functions such as tigetstr\n");
214 ExitProgram(EXIT_FAILURE);
216 #endif /* HAVE_TIGETSTR */