]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/test_setupterm.c
ncurses 6.0 - patch 20161008
[ncurses.git] / test / test_setupterm.c
1 /****************************************************************************
2  * Copyright (c) 2015,2016 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.9 2016/06/18 23:54:35 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 HAVE_RESTARTTERM
100     if (r_opt)
101         rc = restartterm(name, 0, f_opt ? NULL : &err);
102     else
103 #endif
104         rc = setupterm(name, 0, f_opt ? NULL : &err);
105     test_rc(name, rc, err);
106 }
107
108 static void
109 usage(void)
110 {
111     static const char *msg[] =
112     {
113         "Usage: test_setupterm [options] [terminal]",
114         "",
115         "Demonstrate error-checking for setupterm and restartterm.",
116         "",
117         "Options:",
118         " -a       automatic test for each success/error code",
119         " -f       treat errors as fatal",
120         " -n       set environment to disable terminfo database, assuming",
121         "          the compiled-in paths for database also fail",
122 #if HAVE_RESTARTTERM
123         " -r       test restartterm rather than setupterm",
124 #endif
125     };
126     unsigned n;
127     for (n = 0; n < SIZEOF(msg); ++n) {
128         fprintf(stderr, "%s\n", msg[n]);
129     }
130     ExitProgram(EXIT_FAILURE);
131 }
132
133 int
134 main(int argc, char *argv[])
135 {
136     int n;
137
138     while ((n = getopt(argc, argv, "afnr")) != -1) {
139         switch (n) {
140         case 'a':
141             a_opt = TRUE;
142             break;
143         case 'f':
144             f_opt = TRUE;
145             break;
146         case 'n':
147             n_opt = TRUE;
148             break;
149 #if HAVE_RESTARTTERM
150         case 'r':
151             r_opt = TRUE;
152             break;
153 #endif
154         default:
155             usage();
156             break;
157         }
158     }
159
160     if (n_opt) {
161         static char none[][25] =
162         {
163             "HOME=/GUI",
164             "TERMINFO=/GUI",
165             "TERMINFO_DIRS=/GUI"
166         };
167         /*
168          * We can turn this off, but not on again, because ncurses caches the
169          * directory locations.
170          */
171         printf("** without database\n");
172         for (n = 0; n < 3; ++n)
173             putenv(none[n]);
174     } else {
175         printf("** with database\n");
176     }
177
178     /*
179      * The restartterm relies on an existing screen, so we make one here.
180      */
181     if (r_opt) {
182         newterm("ansi", stdout, stdin);
183         reset_shell_mode();
184     }
185
186     if (a_opt) {
187         static char predef[][9] =
188         {"vt100", "dumb", "lpr", "unknown", "none-such"};
189         if (optind < argc) {
190             usage();
191         }
192         for (n = 0; n < 4; ++n) {
193             test_setupterm(predef[n]);
194         }
195     } else {
196         if (optind < argc) {
197             for (n = optind; n < argc; ++n) {
198                 test_setupterm(argv[n]);
199             }
200         } else {
201             test_setupterm(NULL);
202         }
203     }
204
205     ExitProgram(EXIT_SUCCESS);
206 }
207
208 #else /* !HAVE_TIGETSTR */
209 int
210 main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
211 {
212     printf("This program requires the terminfo functions such as tigetstr\n");
213     ExitProgram(EXIT_FAILURE);
214 }
215 #endif /* HAVE_TIGETSTR */