]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/test_setupterm.c
ncurses 6.4 - patch 20240420
[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.12 2022/05/15 16:36:00 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(void)
152 {
153     static const char *msg[] =
154     {
155         "Usage: test_setupterm [options] [terminal]",
156         "",
157         "Demonstrate error-checking for setupterm and restartterm.",
158         "",
159         "Options:",
160         " -a       automatic test for each success/error code",
161         " -f       treat errors as fatal",
162         " -n       set environment to disable terminfo database, assuming",
163         "          the compiled-in paths for database also fail",
164 #if HAVE_RESTARTTERM
165         " -r       test restartterm rather than setupterm",
166 #endif
167     };
168     unsigned n;
169     for (n = 0; n < SIZEOF(msg); ++n) {
170         fprintf(stderr, "%s\n", msg[n]);
171     }
172     finish(EXIT_FAILURE);
173 }
174
175 int
176 main(int argc, char *argv[])
177 {
178     int n;
179
180     while ((n = getopt(argc, argv, "afnr")) != -1) {
181         switch (n) {
182         case 'a':
183             a_opt = TRUE;
184             break;
185         case 'f':
186             f_opt = TRUE;
187             break;
188         case 'n':
189             n_opt = TRUE;
190             break;
191 #if HAVE_RESTARTTERM
192         case 'r':
193             r_opt = TRUE;
194             break;
195 #endif
196         default:
197             usage();
198             break;
199         }
200     }
201
202     if (n_opt) {
203         static char none[][25] =
204         {
205             "HOME=/GUI",
206             "TERMINFO=/GUI",
207             "TERMINFO_DIRS=/GUI"
208         };
209         /*
210          * We can turn this off, but not on again, because ncurses caches the
211          * directory locations.
212          */
213         printf("** without database\n");
214         for (n = 0; n < 3; ++n)
215             putenv(none[n]);
216     } else {
217         printf("** with database\n");
218     }
219
220     /*
221      * The restartterm relies on an existing screen, so we make one here.
222      */
223     if (r_opt) {
224         newterm("ansi", stdout, stdin);
225         reset_shell_mode();
226         save_curterm();
227     }
228
229     if (a_opt) {
230         static char predef[][9] =
231         {"vt100", "dumb", "lpr", "unknown", "none-such"};
232         if (optind < argc) {
233             usage();
234         }
235         for (n = 0; n < 4; ++n) {
236             test_setupterm(predef[n]);
237         }
238     } else {
239         if (optind < argc) {
240             for (n = optind; n < argc; ++n) {
241                 test_setupterm(argv[n]);
242             }
243         } else {
244             test_setupterm(NULL);
245         }
246     }
247
248     finish(EXIT_SUCCESS);
249 }
250
251 #else /* !HAVE_TIGETSTR */
252 int
253 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
254 {
255     printf("This program requires the terminfo functions such as tigetstr\n");
256     ExitProgram(EXIT_FAILURE);
257 }
258 #endif /* HAVE_TIGETSTR */