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