Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 1 | /**************************************************************************** |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 2 | * Copyright 2020-2022,2023 Thomas E. Dickey * |
| 3 | * Copyright 2015,2016 Free Software Foundation, Inc. * |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 4 | * * |
| 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 | * |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 33 | * $Id: test_setupterm.c,v 1.17 2023/06/24 14:19:52 tom Exp $ |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 34 | * |
| 35 | * A simple demo of setupterm/restartterm. |
| 36 | */ |
| 37 | #include <test.priv.h> |
| 38 | |
| 39 | #if HAVE_TIGETSTR |
| 40 | |
| 41 | static bool a_opt = FALSE; |
| 42 | static bool f_opt = FALSE; |
| 43 | static bool n_opt = FALSE; |
| 44 | static bool r_opt = FALSE; |
| 45 | |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 46 | #if NO_LEAKS |
| 47 | static TERMINAL **saved_terminals; |
| 48 | static size_t num_saved; |
| 49 | static size_t max_saved; |
| 50 | |
| 51 | static void |
| 52 | failed(const char *msg) |
| 53 | { |
| 54 | perror(msg); |
| 55 | ExitProgram(EXIT_FAILURE); |
| 56 | } |
| 57 | |
| 58 | static void |
| 59 | finish(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 | |
| 68 | static void |
| 69 | save_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 Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 95 | static void |
| 96 | test_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 | |
| 143 | static void |
| 144 | test_setupterm(NCURSES_CONST char *name) |
| 145 | { |
| 146 | int rc; |
| 147 | int err = -99; |
| 148 | |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 149 | #if HAVE_RESTARTTERM |
| 150 | if (r_opt) |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 151 | rc = restartterm(name, 0, f_opt ? NULL : &err); |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 152 | else |
| 153 | #endif |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 154 | rc = setupterm(name, 0, f_opt ? NULL : &err); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 155 | test_rc(name, rc, err); |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 156 | save_curterm(); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | static void |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 160 | usage(int ok) |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 161 | { |
| 162 | static const char *msg[] = |
| 163 | { |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 164 | "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 Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 177 | }; |
| 178 | unsigned n; |
| 179 | for (n = 0; n < SIZEOF(msg); ++n) { |
| 180 | fprintf(stderr, "%s\n", msg[n]); |
| 181 | } |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 182 | finish(ok ? EXIT_SUCCESS : EXIT_FAILURE); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 183 | } |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 184 | /* *INDENT-OFF* */ |
| 185 | VERSION_COMMON() |
| 186 | /* *INDENT-ON* */ |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 187 | |
| 188 | int |
| 189 | main(int argc, char *argv[]) |
| 190 | { |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 191 | int ch; |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 192 | int n; |
| 193 | |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 194 | while ((ch = getopt(argc, argv, OPTS_COMMON "afnr")) != -1) { |
| 195 | switch (ch) { |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 196 | 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; |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 205 | #if HAVE_RESTARTTERM |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 206 | case 'r': |
| 207 | r_opt = TRUE; |
| 208 | break; |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 209 | #endif |
| 210 | case OPTS_VERSION: |
| 211 | show_version(argv); |
| 212 | ExitProgram(EXIT_SUCCESS); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 213 | default: |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 214 | usage(ch == OPTS_USAGE); |
| 215 | /* NOTREACHED */ |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 216 | } |
| 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(); |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 243 | save_curterm(); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | if (a_opt) { |
| 247 | static char predef[][9] = |
| 248 | {"vt100", "dumb", "lpr", "unknown", "none-such"}; |
| 249 | if (optind < argc) { |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 250 | usage(FALSE); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 251 | } |
| 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 | |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 265 | finish(EXIT_SUCCESS); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | #else /* !HAVE_TIGETSTR */ |
| 269 | int |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 270 | main(void) |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 271 | { |
| 272 | printf("This program requires the terminfo functions such as tigetstr\n"); |
| 273 | ExitProgram(EXIT_FAILURE); |
| 274 | } |
| 275 | #endif /* HAVE_TIGETSTR */ |