1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
|
/****************************************************************************
* Copyright 2020,2022 Thomas E. Dickey *
* Copyright 2015,2016 Free Software Foundation, Inc. *
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the *
* "Software"), to deal in the Software without restriction, including *
* without limitation the rights to use, copy, modify, merge, publish, *
* distribute, distribute with modifications, sublicense, and/or sell *
* copies of the Software, and to permit persons to whom the Software is *
* furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included *
* in all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
* IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
* THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
* *
* Except as contained in this notice, the name(s) of the above copyright *
* holders shall not be used in advertising or otherwise to promote the *
* sale, use or other dealings in this Software without prior written *
* authorization. *
****************************************************************************/
/*
* Author: Thomas E. Dickey
*
* $Id: test_setupterm.c,v 1.16 2022/12/10 23:23:27 tom Exp $
*
* A simple demo of setupterm/restartterm.
*/
#include <test.priv.h>
#if HAVE_TIGETSTR
static bool a_opt = FALSE;
static bool f_opt = FALSE;
static bool n_opt = FALSE;
static bool r_opt = FALSE;
#if NO_LEAKS
static TERMINAL **saved_terminals;
static size_t num_saved;
static size_t max_saved;
static void
finish(int code)
{
size_t n;
for (n = 0; n < num_saved; ++n)
del_curterm(saved_terminals[n]);
free(saved_terminals);
ExitProgram(code);
}
static void
save_curterm(void)
{
size_t n;
bool found = FALSE;
for (n = 0; n < num_saved; ++n) {
if (saved_terminals[n] == cur_term) {
found = TRUE;
break;
}
}
if (!found) {
if (num_saved + 1 >= max_saved) {
max_saved += 100;
saved_terminals = typeRealloc(TERMINAL *, max_saved, saved_terminals);
}
saved_terminals[num_saved++] = cur_term;
}
}
#else
#define finish(code) ExitProgram(code)
#define save_curterm() /* nothing */
#endif
static void
test_rc(NCURSES_CONST char *name, int actual_rc, int actual_err)
{
int expect_rc = -1;
int expect_err = -1;
if (name == 0)
name = getenv("TERM");
if (name == 0)
name = "?";
switch (*name) {
case 'v': /* vt100 is normal */
case 'd': /* dumb has no special flags */
expect_rc = 0;
expect_err = 1;
break;
case 'l': /* lpr is hardcopy */
expect_err = 1;
break;
case 'u': /* unknown is generic */
expect_err = 0;
break;
default:
break;
}
if (n_opt) {
expect_rc = -1;
expect_err = -1;
}
printf("%s",
((actual_rc == expect_rc && actual_err == expect_err)
? "OK"
: "ERR"));
printf(" '%s'", name);
if (actual_rc == expect_rc) {
printf(" rc=%d", actual_rc);
} else {
printf(" rc=%d (%d)", actual_rc, expect_rc);
}
if (actual_err == expect_err) {
printf(" err=%d", actual_err);
} else {
printf(" err=%d (%d)", actual_err, expect_err);
}
printf("\n");
}
static void
test_setupterm(NCURSES_CONST char *name)
{
int rc;
int err = -99;
#if HAVE_RESTARTTERM
if (r_opt)
rc = restartterm(name, 0, f_opt ? NULL : &err);
else
#endif
rc = setupterm(name, 0, f_opt ? NULL : &err);
test_rc(name, rc, err);
save_curterm();
}
static void
usage(int ok)
{
static const char *msg[] =
{
"Usage: test_setupterm [options] [terminal]"
,""
,USAGE_COMMON
,"Demonstrate error-checking for setupterm and restartterm."
,""
,"Options:"
," -a automatic test for each success/error code"
," -f treat errors as fatal"
," -n set environment to disable terminfo database, assuming"
," the compiled-in paths for database also fail"
#if HAVE_RESTARTTERM
," -r test restartterm rather than setupterm"
#endif
};
unsigned n;
for (n = 0; n < SIZEOF(msg); ++n) {
fprintf(stderr, "%s\n", msg[n]);
}
finish(ok ? EXIT_SUCCESS : EXIT_FAILURE);
}
/* *INDENT-OFF* */
VERSION_COMMON()
/* *INDENT-ON* */
int
main(int argc, char *argv[])
{
int ch;
int n;
while ((ch = getopt(argc, argv, OPTS_COMMON "afnr")) != -1) {
switch (ch) {
case 'a':
a_opt = TRUE;
break;
case 'f':
f_opt = TRUE;
break;
case 'n':
n_opt = TRUE;
break;
#if HAVE_RESTARTTERM
case 'r':
r_opt = TRUE;
break;
#endif
case OPTS_VERSION:
show_version(argv);
ExitProgram(EXIT_SUCCESS);
default:
usage(ch == OPTS_USAGE);
/* NOTREACHED */
}
}
if (n_opt) {
static char none[][25] =
{
"HOME=/GUI",
"TERMINFO=/GUI",
"TERMINFO_DIRS=/GUI"
};
/*
* We can turn this off, but not on again, because ncurses caches the
* directory locations.
*/
printf("** without database\n");
for (n = 0; n < 3; ++n)
putenv(none[n]);
} else {
printf("** with database\n");
}
/*
* The restartterm relies on an existing screen, so we make one here.
*/
if (r_opt) {
newterm("ansi", stdout, stdin);
reset_shell_mode();
save_curterm();
}
if (a_opt) {
static char predef[][9] =
{"vt100", "dumb", "lpr", "unknown", "none-such"};
if (optind < argc) {
usage(FALSE);
}
for (n = 0; n < 4; ++n) {
test_setupterm(predef[n]);
}
} else {
if (optind < argc) {
for (n = optind; n < argc; ++n) {
test_setupterm(argv[n]);
}
} else {
test_setupterm(NULL);
}
}
finish(EXIT_SUCCESS);
}
#else /* !HAVE_TIGETSTR */
int
main(void)
{
printf("This program requires the terminfo functions such as tigetstr\n");
ExitProgram(EXIT_FAILURE);
}
#endif /* HAVE_TIGETSTR */
|