blob: 24d7c46c7773846b1b5c83f6ce0fc498f0182a2d [file] [log] [blame]
Steve Kondikae271bc2015-11-15 02:50:53 +01001/****************************************************************************
micky3879b9f5e72025-07-08 18:04:53 -04002 * Copyright 2020-2022,2023 Thomas E. Dickey *
3 * Copyright 2015,2016 Free Software Foundation, Inc. *
Steve Kondikae271bc2015-11-15 02:50:53 +01004 * *
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 *
micky3879b9f5e72025-07-08 18:04:53 -040033 * $Id: test_setupterm.c,v 1.17 2023/06/24 14:19:52 tom Exp $
Steve Kondikae271bc2015-11-15 02:50:53 +010034 *
35 * A simple demo of setupterm/restartterm.
36 */
37#include <test.priv.h>
38
39#if HAVE_TIGETSTR
40
41static bool a_opt = FALSE;
42static bool f_opt = FALSE;
43static bool n_opt = FALSE;
44static bool r_opt = FALSE;
45
micky3879b9f5e72025-07-08 18:04:53 -040046#if NO_LEAKS
47static TERMINAL **saved_terminals;
48static size_t num_saved;
49static size_t max_saved;
50
51static void
52failed(const char *msg)
53{
54 perror(msg);
55 ExitProgram(EXIT_FAILURE);
56}
57
58static void
59finish(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
68static void
69save_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
Steve Kondikae271bc2015-11-15 02:50:53 +010095static void
96test_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
143static void
144test_setupterm(NCURSES_CONST char *name)
145{
146 int rc;
147 int err = -99;
148
micky3879b9f5e72025-07-08 18:04:53 -0400149#if HAVE_RESTARTTERM
150 if (r_opt)
Steve Kondikae271bc2015-11-15 02:50:53 +0100151 rc = restartterm(name, 0, f_opt ? NULL : &err);
micky3879b9f5e72025-07-08 18:04:53 -0400152 else
153#endif
Steve Kondikae271bc2015-11-15 02:50:53 +0100154 rc = setupterm(name, 0, f_opt ? NULL : &err);
Steve Kondikae271bc2015-11-15 02:50:53 +0100155 test_rc(name, rc, err);
micky3879b9f5e72025-07-08 18:04:53 -0400156 save_curterm();
Steve Kondikae271bc2015-11-15 02:50:53 +0100157}
158
159static void
micky3879b9f5e72025-07-08 18:04:53 -0400160usage(int ok)
Steve Kondikae271bc2015-11-15 02:50:53 +0100161{
162 static const char *msg[] =
163 {
micky3879b9f5e72025-07-08 18:04:53 -0400164 "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
Steve Kondikae271bc2015-11-15 02:50:53 +0100177 };
178 unsigned n;
179 for (n = 0; n < SIZEOF(msg); ++n) {
180 fprintf(stderr, "%s\n", msg[n]);
181 }
micky3879b9f5e72025-07-08 18:04:53 -0400182 finish(ok ? EXIT_SUCCESS : EXIT_FAILURE);
Steve Kondikae271bc2015-11-15 02:50:53 +0100183}
micky3879b9f5e72025-07-08 18:04:53 -0400184/* *INDENT-OFF* */
185VERSION_COMMON()
186/* *INDENT-ON* */
Steve Kondikae271bc2015-11-15 02:50:53 +0100187
188int
189main(int argc, char *argv[])
190{
micky3879b9f5e72025-07-08 18:04:53 -0400191 int ch;
Steve Kondikae271bc2015-11-15 02:50:53 +0100192 int n;
193
micky3879b9f5e72025-07-08 18:04:53 -0400194 while ((ch = getopt(argc, argv, OPTS_COMMON "afnr")) != -1) {
195 switch (ch) {
Steve Kondikae271bc2015-11-15 02:50:53 +0100196 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;
micky3879b9f5e72025-07-08 18:04:53 -0400205#if HAVE_RESTARTTERM
Steve Kondikae271bc2015-11-15 02:50:53 +0100206 case 'r':
207 r_opt = TRUE;
208 break;
micky3879b9f5e72025-07-08 18:04:53 -0400209#endif
210 case OPTS_VERSION:
211 show_version(argv);
212 ExitProgram(EXIT_SUCCESS);
Steve Kondikae271bc2015-11-15 02:50:53 +0100213 default:
micky3879b9f5e72025-07-08 18:04:53 -0400214 usage(ch == OPTS_USAGE);
215 /* NOTREACHED */
Steve Kondikae271bc2015-11-15 02:50:53 +0100216 }
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();
micky3879b9f5e72025-07-08 18:04:53 -0400243 save_curterm();
Steve Kondikae271bc2015-11-15 02:50:53 +0100244 }
245
246 if (a_opt) {
247 static char predef[][9] =
248 {"vt100", "dumb", "lpr", "unknown", "none-such"};
249 if (optind < argc) {
micky3879b9f5e72025-07-08 18:04:53 -0400250 usage(FALSE);
Steve Kondikae271bc2015-11-15 02:50:53 +0100251 }
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
micky3879b9f5e72025-07-08 18:04:53 -0400265 finish(EXIT_SUCCESS);
Steve Kondikae271bc2015-11-15 02:50:53 +0100266}
267
268#else /* !HAVE_TIGETSTR */
269int
micky3879b9f5e72025-07-08 18:04:53 -0400270main(void)
Steve Kondikae271bc2015-11-15 02:50:53 +0100271{
272 printf("This program requires the terminfo functions such as tigetstr\n");
273 ExitProgram(EXIT_FAILURE);
274}
275#endif /* HAVE_TIGETSTR */