]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/dots_termcap.c
ncurses 6.4 - patch 20240420
[ncurses.git] / test / dots_termcap.c
1 /****************************************************************************
2  * Copyright 2018-2022,2023 Thomas E. Dickey                                *
3  * Copyright 2013-2014,2017 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: dots_termcap.c,v 1.31 2023/01/07 17:21:48 tom Exp $
34  *
35  * A simple demo of the termcap interface.
36  */
37 #define USE_TINFO
38 #include <test.priv.h>
39
40 #if !defined(_NC_WINDOWS)
41 #include <sys/time.h>
42 #endif
43
44 #if HAVE_TGETENT
45
46 #include <time.h>
47
48 static bool interrupted = FALSE;
49 static long total_chars = 0;
50 static time_t started;
51
52 static char *t_AB;
53 static char *t_AF;
54 static char *t_cl;
55 static char *t_cm;
56 static char *t_me;
57 static char *t_mr;
58 static char *t_oc;
59 static char *t_op;
60 static char *t_ve;
61 static char *t_vi;
62
63 static struct {
64     NCURSES_CONST char *name;
65     char **value;
66 } my_caps[] = {
67
68     {
69         "AB", &t_AB
70     },
71     {
72         "AF", &t_AF
73     },
74     {
75         "cl", &t_cl
76     },
77     {
78         "cm", &t_cm
79     },
80     {
81         "me", &t_me
82     },
83     {
84         "mr", &t_mr
85     },
86     {
87         "oc", &t_oc
88     },
89     {
90         "op", &t_op
91     },
92     {
93         "ve", &t_ve
94     },
95     {
96         "vi", &t_vi
97     },
98 };
99
100 static
101 TPUTS_PROTO(outc, c)
102 {
103     int rc = c;
104
105     if (interrupted) {
106         char tmp = (char) c;
107         if (write(STDOUT_FILENO, &tmp, (size_t) 1) == -1)
108             rc = EOF;
109     } else {
110         rc = putc(c, stdout);
111     }
112     TPUTS_RETURN(rc);
113 }
114
115 static bool
116 outs(char *s)
117 {
118     if (VALID_STRING(s)) {
119         tputs(s, 1, outc);
120         return TRUE;
121     }
122     return FALSE;
123 }
124
125 static void
126 cleanup(void)
127 {
128     outs(t_me);
129     if (!outs(t_oc))
130         outs(t_op);
131     outs(t_cl);
132     outs(t_ve);
133
134     fflush(stdout);
135     fprintf(stderr, "\n\n%ld total cells, rate %.2f/sec\n",
136             total_chars,
137             ((double) (total_chars) / (double) (time((time_t *) 0) - started)));
138 }
139
140 static void
141 onsig(int n GCC_UNUSED)
142 {
143     interrupted = TRUE;
144 }
145
146 static double
147 ranf(void)
148 {
149     long r = (rand() & 077777);
150     return ((double) r / 32768.);
151 }
152
153 static void
154 my_napms(int ms)
155 {
156     if (ms > 0) {
157 #if defined(_NC_WINDOWS) || !HAVE_GETTIMEOFDAY
158         Sleep((unsigned int) ms);
159 #else
160         struct timeval data;
161         data.tv_sec = 0;
162         data.tv_usec = ms * 1000;
163         select(0, NULL, NULL, NULL, &data);
164 #endif
165     }
166 }
167
168 static int
169 get_number(NCURSES_CONST char *cap, const char *env)
170 {
171     int result = tgetnum(cap);
172     char *value = env ? getenv(env) : 0;
173     if (value != 0 && *value != 0) {
174         char *next = 0;
175         long check = strtol(value, &next, 10);
176         if (check > 0 && *next == '\0')
177             result = (int) check;
178     }
179     return result;
180 }
181
182 static void
183 usage(int ok)
184 {
185     static const char *msg[] =
186     {
187         "Usage: dots_termcap [options]"
188         ,""
189         ,USAGE_COMMON
190         ,"Options:"
191         ," -T TERM  override $TERM"
192         ," -e       allow environment $LINES / $COLUMNS"
193         ," -m SIZE  set margin (default: 2)"
194         ," -r SECS  self-interrupt/exit after specified number of seconds"
195         ," -s MSECS delay 1% of the time (default: 1 msecs)"
196     };
197     size_t n;
198
199     for (n = 0; n < SIZEOF(msg); n++)
200         fprintf(stderr, "%s\n", msg[n]);
201
202     ExitProgram(ok ? EXIT_SUCCESS : EXIT_FAILURE);
203 }
204 /* *INDENT-OFF* */
205 VERSION_COMMON()
206 /* *INDENT-ON* */
207
208 int
209 main(int argc, char *argv[])
210 {
211     int ch;
212     int num_colors;
213     int num_lines;
214     int num_columns;
215     int e_option = 0;
216     int m_option = 2;
217     int r_option = 0;
218     int s_option = 1;
219     double r;
220     double c;
221     char buffer[1024];
222     char area[1024];
223     char *name;
224     size_t need;
225     char *my_env;
226
227     while ((ch = getopt(argc, argv, OPTS_COMMON "T:em:r:s:")) != -1) {
228         switch (ch) {
229         case 'T':
230             need = 6 + strlen(optarg);
231             if ((my_env = malloc(need)) != NULL) {
232                 _nc_SPRINTF(my_env, _nc_SLIMIT(need) "TERM=%s", optarg);
233                 putenv(my_env);
234             }
235             break;
236         case 'e':
237             e_option = 1;
238             break;
239         case 'm':
240             m_option = atoi(optarg);
241             break;
242         case 'r':
243             r_option = atoi(optarg);
244             break;
245         case 's':
246             s_option = atoi(optarg);
247             break;
248         case OPTS_VERSION:
249             show_version(argv);
250             ExitProgram(EXIT_SUCCESS);
251         default:
252             usage(ch == OPTS_USAGE);
253             /* NOTREACHED */
254         }
255     }
256
257     if ((name = getenv("TERM")) == 0) {
258         fprintf(stderr, "TERM is not set\n");
259         ExitProgram(EXIT_FAILURE);
260     }
261
262     srand((unsigned) time(0));
263
264     SetupAlarm((unsigned) r_option);
265     InitAndCatch(ch = tgetent(buffer, name), onsig);
266     if (ch < 0) {
267         fprintf(stderr, "terminal description not found\n");
268         ExitProgram(EXIT_FAILURE);
269     } else {
270         size_t t;
271         char *ap = area;
272         for (t = 0; t < SIZEOF(my_caps); ++t) {
273             *(my_caps[t].value) = tgetstr((NCURSES_CONST char *)
274                                           my_caps[t].name, &ap);
275         }
276     }
277
278     num_colors = tgetnum("Co");
279 #define GetNumber(cap,env) get_number(cap, e_option ? env : 0)
280     num_lines = GetNumber("li", "LINES");
281     num_columns = GetNumber("co", "COLUMNS");
282
283     outs(t_cl);
284     outs(t_vi);
285     if (num_colors > 1) {
286         if (!VALID_STRING(t_AF)
287             || !VALID_STRING(t_AB)
288             || (!VALID_STRING(t_oc) && !VALID_STRING(t_op)))
289             num_colors = -1;
290     }
291
292     r = (double) (num_lines - (2 * m_option));
293     c = (double) (num_columns - (2 * m_option));
294     started = time((time_t *) 0);
295
296     while (!interrupted) {
297         int x = (int) (c * ranf()) + m_option;
298         int y = (int) (r * ranf()) + m_option;
299         int p = (ranf() > 0.9) ? '*' : ' ';
300
301         tputs(tgoto(t_cm, x, y), 1, outc);
302         if (num_colors > 0) {
303             int z = (int) (ranf() * num_colors);
304             if (ranf() > 0.01) {
305                 tputs(tgoto(t_AF, 0, z), 1, outc);
306             } else {
307                 tputs(tgoto(t_AB, 0, z), 1, outc);
308                 if (s_option)
309                     my_napms(s_option);
310             }
311         } else if (VALID_STRING(t_me)
312                    && VALID_STRING(t_mr)) {
313             if (ranf() <= 0.01) {
314                 outs((ranf() > 0.6)
315                      ? t_mr
316                      : t_me);
317                 if (s_option)
318                     my_napms(s_option);
319             }
320         }
321         outc(p);
322         fflush(stdout);
323         ++total_chars;
324     }
325     cleanup();
326     ExitProgram(EXIT_SUCCESS);
327 }
328 #else
329 int
330 main(void)
331 {
332     fprintf(stderr, "This program requires termcap\n");
333     exit(EXIT_FAILURE);
334 }
335 #endif