]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/test_setupterm.c
ncurses 6.3 - patch 20221203
[ncurses.git] / test / test_setupterm.c
1 /****************************************************************************
2  * Copyright 2020,2022 Thomas E. Dickey                                     *
3  * Copyright 2015,2016 Free Software Foundation, Inc.                       *
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  *
33  * $Id: test_setupterm.c,v 1.15 2022/12/04 00:40:11 tom Exp $
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
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 finish(int code)
53 {
54     size_t n;
55     for (n = 0; n < num_saved; ++n)
56         del_curterm(saved_terminals[n]);
57     free(saved_terminals);
58     ExitProgram(code);
59 }
60
61 static void
62 save_curterm(void)
63 {
64     size_t n;
65     bool found = FALSE;
66     for (n = 0; n < num_saved; ++n) {
67         if (saved_terminals[n] == cur_term) {
68             found = TRUE;
69             break;
70         }
71     }
72     if (!found) {
73         if (num_saved + 1 >= max_saved) {
74             max_saved += 100;
75             saved_terminals = typeRealloc(TERMINAL *, max_saved, saved_terminals);
76         }
77         saved_terminals[num_saved++] = cur_term;
78     }
79 }
80
81 #else
82 #define finish(code) ExitProgram(code)
83 #define save_curterm()          /* nothing */
84 #endif
85
86 static void
87 test_rc(NCURSES_CONST char *name, int actual_rc, int actual_err)
88 {
89     int expect_rc = -1;
90     int expect_err = -1;
91
92     if (name == 0)
93         name = getenv("TERM");
94     if (name == 0)
95         name = "?";
96
97     switch (*name) {
98     case 'v':                   /* vt100 is normal */
99     case 'd':                   /* dumb has no special flags */
100         expect_rc = 0;
101         expect_err = 1;
102         break;
103     case 'l':                   /* lpr is hardcopy */
104         expect_err = 1;
105         break;
106     case 'u':                   /* unknown is generic */
107         expect_err = 0;
108         break;
109     default:
110         break;
111     }
112     if (n_opt) {
113         expect_rc = -1;
114         expect_err = -1;
115     }
116     printf("%s",
117            ((actual_rc == expect_rc && actual_err == expect_err)
118             ? "OK"
119             : "ERR"));
120     printf(" '%s'", name);
121     if (actual_rc == expect_rc) {
122         printf(" rc=%d", actual_rc);
123     } else {
124         printf(" rc=%d (%d)", actual_rc, expect_rc);
125     }
126     if (actual_err == expect_err) {
127         printf(" err=%d", actual_err);
128     } else {
129         printf(" err=%d (%d)", actual_err, expect_err);
130     }
131     printf("\n");
132 }
133
134 static void
135 test_setupterm(NCURSES_CONST char *name)
136 {
137     int rc;
138     int err = -99;
139
140 #if HAVE_RESTARTTERM
141     if (r_opt)
142         rc = restartterm(name, 0, f_opt ? NULL : &err);
143     else
144 #endif
145         rc = setupterm(name, 0, f_opt ? NULL : &err);
146     test_rc(name, rc, err);
147     save_curterm();
148 }
149
150 static void
151 usage(int ok)
152 {
153     static const char *msg[] =
154     {
155         "Usage: test_setupterm [options] [terminal]"
156         ,""
157         ,USAGE_COMMON
158         ,"Demonstrate error-checking for setupterm and restartterm."
159         ,""
160         ,"Options:"
161         ," -a       automatic test for each success/error code"
162         ," -f       treat errors as fatal"
163         ," -n       set environment to disable terminfo database, assuming"
164         ,"          the compiled-in paths for database also fail"
165 #if HAVE_RESTARTTERM
166         ," -r       test restartterm rather than setupterm"
167 #endif
168     };
169     unsigned n;
170     for (n = 0; n < SIZEOF(msg); ++n) {
171         fprintf(stderr, "%s\n", msg[n]);
172     }
173     finish(ok ? EXIT_SUCCESS : EXIT_FAILURE);
174 }
175 /* *INDENT-OFF* */
176 VERSION_COMMON()
177 /* *INDENT-ON* */
178
179 int
180 main(int argc, char *argv[])
181 {
182     int ch;
183     int n;
184
185     while ((ch = getopt(argc, argv, OPTS_COMMON "afnr")) != -1) {
186         switch (ch) {
187         case 'a':
188             a_opt = TRUE;
189             break;
190         case 'f':
191             f_opt = TRUE;
192             break;
193         case 'n':
194             n_opt = TRUE;
195             break;
196 #if HAVE_RESTARTTERM
197         case 'r':
198             r_opt = TRUE;
199             break;
200 #endif
201         case OPTS_VERSION:
202             show_version(argv);
203             ExitProgram(EXIT_SUCCESS);
204         default:
205             usage(ch == OPTS_USAGE);
206             /* NOTREACHED */
207         }
208     }
209
210     if (n_opt) {
211         static char none[][25] =
212         {
213             "HOME=/GUI",
214             "TERMINFO=/GUI",
215             "TERMINFO_DIRS=/GUI"
216         };
217         /*
218          * We can turn this off, but not on again, because ncurses caches the
219          * directory locations.
220          */
221         printf("** without database\n");
222         for (n = 0; n < 3; ++n)
223             putenv(none[n]);
224     } else {
225         printf("** with database\n");
226     }
227
228     /*
229      * The restartterm relies on an existing screen, so we make one here.
230      */
231     if (r_opt) {
232         newterm("ansi", stdout, stdin);
233         reset_shell_mode();
234         save_curterm();
235     }
236
237     if (a_opt) {
238         static char predef[][9] =
239         {"vt100", "dumb", "lpr", "unknown", "none-such"};
240         if (optind < argc) {
241             usage(FALSE);
242         }
243         for (n = 0; n < 4; ++n) {
244             test_setupterm(predef[n]);
245         }
246     } else {
247         if (optind < argc) {
248             for (n = optind; n < argc; ++n) {
249                 test_setupterm(argv[n]);
250             }
251         } else {
252             test_setupterm(NULL);
253         }
254     }
255
256     finish(EXIT_SUCCESS);
257 }
258
259 #else /* !HAVE_TIGETSTR */
260 int
261 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
262 {
263     printf("This program requires the terminfo functions such as tigetstr\n");
264     ExitProgram(EXIT_FAILURE);
265 }
266 #endif /* HAVE_TIGETSTR */