1 /****************************************************************************
2 * Copyright 2018-2019,2020 Thomas E. Dickey *
3 * Copyright 2013-2014,2017 Free Software Foundation, Inc. *
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: *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
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. *
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 *
28 ****************************************************************************/
31 * Author: Thomas E. Dickey
33 * $Id: dots_termcap.c,v 1.20 2020/02/02 23:34:34 tom Exp $
35 * A simple demo of the termcap interface.
38 #include <test.priv.h>
48 static bool interrupted = FALSE;
49 static long total_chars = 0;
50 static time_t started;
64 NCURSES_CONST char *name;
107 if (write(STDOUT_FILENO, &tmp, (size_t) 1) == -1)
110 rc = putc(c, stdout);
118 if (VALID_STRING(s)) {
134 printf("\n\n%ld total cells, rate %.2f/sec\n",
136 ((double) (total_chars) / (double) (time((time_t *) 0) - started)));
140 onsig(int n GCC_UNUSED)
148 long r = (rand() & 077777);
149 return ((double) r / 32768.);
156 #if defined(_WIN32) || !HAVE_GETTIMEOFDAY
161 data.tv_usec = ms * 1000;
162 select(0, NULL, NULL, NULL, &data);
168 get_number(NCURSES_CONST char *cap, const char *env)
170 int result = tgetnum(cap);
171 char *value = env ? getenv(env) : 0;
172 if (value != 0 && *value != 0) {
174 long check = strtol(value, &next, 10);
175 if (check > 0 && *next == '\0')
176 result = (int) check;
184 static const char *msg[] =
186 "Usage: dots_termcap [options]"
189 ," -T TERM override $TERM"
190 ," -e allow environment $LINES / $COLUMNS"
191 ," -m SIZE set margin (default: 2)"
192 ," -s MSECS delay 1% of the time (default: 1 msecs)"
196 for (n = 0; n < SIZEOF(msg); n++)
197 fprintf(stderr, "%s\n", msg[n]);
199 ExitProgram(EXIT_FAILURE);
203 main(int argc, char *argv[])
220 while ((ch = getopt(argc, argv, "T:em:s:")) != -1) {
223 need = 6 + strlen(optarg);
224 my_env = malloc(need);
225 _nc_SPRINTF(my_env, _nc_SLIMIT(need) "TERM=%s", optarg);
232 m_option = atoi(optarg);
235 s_option = atoi(optarg);
243 if ((name = getenv("TERM")) == 0) {
244 fprintf(stderr, "TERM is not set\n");
245 ExitProgram(EXIT_FAILURE);
248 srand((unsigned) time(0));
250 InitAndCatch(ch = tgetent(buffer, name), onsig);
252 fprintf(stderr, "terminal description not found\n");
253 ExitProgram(EXIT_FAILURE);
257 for (t = 0; t < SIZEOF(my_caps); ++t) {
258 *(my_caps[t].value) = tgetstr((NCURSES_CONST char *)
259 my_caps[t].name, &ap);
263 num_colors = tgetnum("Co");
264 #define GetNumber(cap,env) get_number(cap, e_option ? env : 0)
265 num_lines = GetNumber("li", "LINES");
266 num_columns = GetNumber("co", "COLUMNS");
270 if (num_colors > 1) {
271 if (!VALID_STRING(t_AF)
272 || !VALID_STRING(t_AB)
273 || (!VALID_STRING(t_oc) && !VALID_STRING(t_op)))
277 r = (double) (num_lines - (2 * m_option));
278 c = (double) (num_columns - (2 * m_option));
279 started = time((time_t *) 0);
281 while (!interrupted) {
282 int x = (int) (c * ranf()) + m_option;
283 int y = (int) (r * ranf()) + m_option;
284 int p = (ranf() > 0.9) ? '*' : ' ';
286 tputs(tgoto(t_cm, x, y), 1, outc);
287 if (num_colors > 0) {
288 int z = (int) (ranf() * num_colors);
290 tputs(tgoto(t_AF, 0, z), 1, outc);
292 tputs(tgoto(t_AB, 0, z), 1, outc);
295 } else if (VALID_STRING(t_me)
296 && VALID_STRING(t_mr)) {
297 if (ranf() <= 0.01) {
309 ExitProgram(EXIT_SUCCESS);
313 main(int argc GCC_UNUSED,
314 char *argv[]GCC_UNUSED)
316 fprintf(stderr, "This program requires termcap\n");