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.24 2020/05/29 23:04:02 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)) {
135 fprintf(stderr, "\n\n%ld total cells, rate %.2f/sec\n",
137 ((double) (total_chars) / (double) (time((time_t *) 0) - started)));
141 onsig(int n GCC_UNUSED)
149 long r = (rand() & 077777);
150 return ((double) r / 32768.);
157 #if defined(_WIN32) || !HAVE_GETTIMEOFDAY
162 data.tv_usec = ms * 1000;
163 select(0, NULL, NULL, NULL, &data);
169 get_number(NCURSES_CONST char *cap, const char *env)
171 int result = tgetnum(cap);
172 char *value = env ? getenv(env) : 0;
173 if (value != 0 && *value != 0) {
175 long check = strtol(value, &next, 10);
176 if (check > 0 && *next == '\0')
177 result = (int) check;
185 static const char *msg[] =
187 "Usage: dots_termcap [options]"
190 ," -T TERM override $TERM"
191 ," -e allow environment $LINES / $COLUMNS"
192 ," -m SIZE set margin (default: 2)"
193 ," -r SECS self-interrupt/exit after specified number of seconds"
194 ," -s MSECS delay 1% of the time (default: 1 msecs)"
198 for (n = 0; n < SIZEOF(msg); n++)
199 fprintf(stderr, "%s\n", msg[n]);
201 ExitProgram(EXIT_FAILURE);
205 main(int argc, char *argv[])
223 while ((ch = getopt(argc, argv, "T:em:r:s:")) != -1) {
226 need = 6 + strlen(optarg);
227 my_env = malloc(need);
228 _nc_SPRINTF(my_env, _nc_SLIMIT(need) "TERM=%s", optarg);
235 m_option = atoi(optarg);
238 r_option = atoi(optarg);
241 s_option = atoi(optarg);
249 if ((name = getenv("TERM")) == 0) {
250 fprintf(stderr, "TERM is not set\n");
251 ExitProgram(EXIT_FAILURE);
254 srand((unsigned) time(0));
256 SetupAlarm((unsigned) r_option);
257 InitAndCatch(ch = tgetent(buffer, name), onsig);
259 fprintf(stderr, "terminal description not found\n");
260 ExitProgram(EXIT_FAILURE);
264 for (t = 0; t < SIZEOF(my_caps); ++t) {
265 *(my_caps[t].value) = tgetstr((NCURSES_CONST char *)
266 my_caps[t].name, &ap);
270 num_colors = tgetnum("Co");
271 #define GetNumber(cap,env) get_number(cap, e_option ? env : 0)
272 num_lines = GetNumber("li", "LINES");
273 num_columns = GetNumber("co", "COLUMNS");
277 if (num_colors > 1) {
278 if (!VALID_STRING(t_AF)
279 || !VALID_STRING(t_AB)
280 || (!VALID_STRING(t_oc) && !VALID_STRING(t_op)))
284 r = (double) (num_lines - (2 * m_option));
285 c = (double) (num_columns - (2 * m_option));
286 started = time((time_t *) 0);
288 while (!interrupted) {
289 int x = (int) (c * ranf()) + m_option;
290 int y = (int) (r * ranf()) + m_option;
291 int p = (ranf() > 0.9) ? '*' : ' ';
293 tputs(tgoto(t_cm, x, y), 1, outc);
294 if (num_colors > 0) {
295 int z = (int) (ranf() * num_colors);
297 tputs(tgoto(t_AF, 0, z), 1, outc);
299 tputs(tgoto(t_AB, 0, z), 1, outc);
303 } else if (VALID_STRING(t_me)
304 && VALID_STRING(t_mr)) {
305 if (ranf() <= 0.01) {
318 ExitProgram(EXIT_SUCCESS);
322 main(int argc GCC_UNUSED,
323 char *argv[]GCC_UNUSED)
325 fprintf(stderr, "This program requires termcap\n");