]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/test_setupterm.c
ncurses 6.2 - patch 20200907
[ncurses.git] / test / test_setupterm.c
1 /****************************************************************************
2  * Copyright 2020 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.10 2020/02/02 23:34:34 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 static void
47 test_rc(NCURSES_CONST char *name, int actual_rc, int actual_err)
48 {
49     int expect_rc = -1;
50     int expect_err = -1;
51
52     if (name == 0)
53         name = getenv("TERM");
54     if (name == 0)
55         name = "?";
56
57     switch (*name) {
58     case 'v':                   /* vt100 is normal */
59     case 'd':                   /* dumb has no special flags */
60         expect_rc = 0;
61         expect_err = 1;
62         break;
63     case 'l':                   /* lpr is hardcopy */
64         expect_err = 1;
65         break;
66     case 'u':                   /* unknown is generic */
67         expect_err = 0;
68         break;
69     default:
70         break;
71     }
72     if (n_opt) {
73         expect_rc = -1;
74         expect_err = -1;
75     }
76     printf("%s",
77            ((actual_rc == expect_rc && actual_err == expect_err)
78             ? "OK"
79             : "ERR"));
80     printf(" '%s'", name);
81     if (actual_rc == expect_rc) {
82         printf(" rc=%d", actual_rc);
83     } else {
84         printf(" rc=%d (%d)", actual_rc, expect_rc);
85     }
86     if (actual_err == expect_err) {
87         printf(" err=%d", actual_err);
88     } else {
89         printf(" err=%d (%d)", actual_err, expect_err);
90     }
91     printf("\n");
92 }
93
94 static void
95 test_setupterm(NCURSES_CONST char *name)
96 {
97     int rc;
98     int err = -99;
99
100 #if HAVE_RESTARTTERM
101     if (r_opt)
102         rc = restartterm(name, 0, f_opt ? NULL : &err);
103     else
104 #endif
105         rc = setupterm(name, 0, f_opt ? NULL : &err);
106     test_rc(name, rc, err);
107 }
108
109 static void
110 usage(void)
111 {
112     static const char *msg[] =
113     {
114         "Usage: test_setupterm [options] [terminal]",
115         "",
116         "Demonstrate error-checking for setupterm and restartterm.",
117         "",
118         "Options:",
119         " -a       automatic test for each success/error code",
120         " -f       treat errors as fatal",
121         " -n       set environment to disable terminfo database, assuming",
122         "          the compiled-in paths for database also fail",
123 #if HAVE_RESTARTTERM
124         " -r       test restartterm rather than setupterm",
125 #endif
126     };
127     unsigned n;
128     for (n = 0; n < SIZEOF(msg); ++n) {
129         fprintf(stderr, "%s\n", msg[n]);
130     }
131     ExitProgram(EXIT_FAILURE);
132 }
133
134 int
135 main(int argc, char *argv[])
136 {
137     int n;
138
139     while ((n = getopt(argc, argv, "afnr")) != -1) {
140         switch (n) {
141         case 'a':
142             a_opt = TRUE;
143             break;
144         case 'f':
145             f_opt = TRUE;
146             break;
147         case 'n':
148             n_opt = TRUE;
149             break;
150 #if HAVE_RESTARTTERM
151         case 'r':
152             r_opt = TRUE;
153             break;
154 #endif
155         default:
156             usage();
157             break;
158         }
159     }
160
161     if (n_opt) {
162         static char none[][25] =
163         {
164             "HOME=/GUI",
165             "TERMINFO=/GUI",
166             "TERMINFO_DIRS=/GUI"
167         };
168         /*
169          * We can turn this off, but not on again, because ncurses caches the
170          * directory locations.
171          */
172         printf("** without database\n");
173         for (n = 0; n < 3; ++n)
174             putenv(none[n]);
175     } else {
176         printf("** with database\n");
177     }
178
179     /*
180      * The restartterm relies on an existing screen, so we make one here.
181      */
182     if (r_opt) {
183         newterm("ansi", stdout, stdin);
184         reset_shell_mode();
185     }
186
187     if (a_opt) {
188         static char predef[][9] =
189         {"vt100", "dumb", "lpr", "unknown", "none-such"};
190         if (optind < argc) {
191             usage();
192         }
193         for (n = 0; n < 4; ++n) {
194             test_setupterm(predef[n]);
195         }
196     } else {
197         if (optind < argc) {
198             for (n = optind; n < argc; ++n) {
199                 test_setupterm(argv[n]);
200             }
201         } else {
202             test_setupterm(NULL);
203         }
204     }
205
206     ExitProgram(EXIT_SUCCESS);
207 }
208
209 #else /* !HAVE_TIGETSTR */
210 int
211 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
212 {
213     printf("This program requires the terminfo functions such as tigetstr\n");
214     ExitProgram(EXIT_FAILURE);
215 }
216 #endif /* HAVE_TIGETSTR */