1 /****************************************************************************
2 * Copyright (c) 2013-2017,2018 Free Software Foundation, Inc. *
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: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
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. *
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 *
27 ****************************************************************************/
30 * Author: Thomas E. Dickey
32 * $Id: dots_termcap.c,v 1.17 2018/06/24 00:06:37 tom Exp $
34 * A simple demo of the termcap interface.
37 #include <test.priv.h>
47 static bool interrupted = FALSE;
48 static long total_chars = 0;
49 static time_t started;
63 NCURSES_CONST char *name;
106 if (write(STDOUT_FILENO, &tmp, (size_t) 1) == -1)
109 rc = putc(c, stdout);
117 if (VALID_STRING(s)) {
133 printf("\n\n%ld total cells, rate %.2f/sec\n",
135 ((double) (total_chars) / (double) (time((time_t *) 0) - started)));
139 onsig(int n GCC_UNUSED)
147 long r = (rand() & 077777);
148 return ((double) r / 32768.);
155 #if defined(_WIN32) || !HAVE_GETTIMEOFDAY
160 data.tv_usec = ms * 1000;
161 select(0, NULL, NULL, NULL, &data);
167 get_number(NCURSES_CONST char *cap, const char *env)
169 int result = tgetnum(cap);
170 char *value = env ? getenv(env) : 0;
171 if (value != 0 && *value != 0) {
173 long check = strtol(value, &next, 10);
174 if (check > 0 && *next == '\0')
175 result = (int) check;
183 static const char *msg[] =
185 "Usage: dots_termcap [options]"
188 ," -T TERM override $TERM"
189 ," -e allow environment $LINES / $COLUMNS"
190 ," -m SIZE set margin (default: 2)"
191 ," -s MSECS delay 1% of the time (default: 1 msecs)"
195 for (n = 0; n < SIZEOF(msg); n++)
196 fprintf(stderr, "%s\n", msg[n]);
198 ExitProgram(EXIT_FAILURE);
202 main(int argc, char *argv[])
217 while ((x = getopt(argc, argv, "T:em:s:")) != -1) {
220 putenv(strcat(strcpy(malloc(6 + strlen(optarg)), "TERM="), optarg));
226 m_option = atoi(optarg);
229 s_option = atoi(optarg);
237 if ((name = getenv("TERM")) == 0) {
238 fprintf(stderr, "TERM is not set\n");
239 ExitProgram(EXIT_FAILURE);
242 srand((unsigned) time(0));
244 InitAndCatch(z = tgetent(buffer, name), onsig);
246 fprintf(stderr, "terminal description not found\n");
247 ExitProgram(EXIT_FAILURE);
251 for (t = 0; t < SIZEOF(my_caps); ++t) {
252 *(my_caps[t].value) = tgetstr((NCURSES_CONST char *)
253 my_caps[t].name, &ap);
257 num_colors = tgetnum("Co");
258 #define GetNumber(cap,env) get_number(cap, e_option ? env : 0)
259 num_lines = GetNumber("li", "LINES");
260 num_columns = GetNumber("co", "COLUMNS");
264 if (num_colors > 1) {
265 if (!VALID_STRING(t_AF)
266 || !VALID_STRING(t_AB)
267 || (!VALID_STRING(t_oc) && !VALID_STRING(t_op)))
271 r = (double) (num_lines - (2 * m_option));
272 c = (double) (num_columns - (2 * m_option));
273 started = time((time_t *) 0);
275 while (!interrupted) {
276 x = (int) (c * ranf()) + m_option;
277 y = (int) (r * ranf()) + m_option;
278 p = (ranf() > 0.9) ? '*' : ' ';
280 tputs(tgoto(t_cm, x, y), 1, outc);
281 if (num_colors > 0) {
282 z = (int) (ranf() * num_colors);
284 tputs(tgoto(t_AF, 0, z), 1, outc);
286 tputs(tgoto(t_AB, 0, z), 1, outc);
289 } else if (VALID_STRING(t_me)
290 && VALID_STRING(t_mr)) {
291 if (ranf() <= 0.01) {
303 ExitProgram(EXIT_SUCCESS);
307 main(int argc GCC_UNUSED,
308 char *argv[]GCC_UNUSED)
310 fprintf(stderr, "This program requires termcap\n");