]> ncurses.scripts.mit.edu Git - ncurses.git/blob - test/dots_termcap.c
ncurses 6.2 - patch 20200308
[ncurses.git] / test / dots_termcap.c
1 /****************************************************************************
2  * Copyright 2018-2019,2020 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.20 2020/02/02 23:34:34 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(_WIN32)
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     printf("\n\n%ld total cells, rate %.2f/sec\n",
135            total_chars,
136            ((double) (total_chars) / (double) (time((time_t *) 0) - started)));
137 }
138
139 static void
140 onsig(int n GCC_UNUSED)
141 {
142     interrupted = TRUE;
143 }
144
145 static double
146 ranf(void)
147 {
148     long r = (rand() & 077777);
149     return ((double) r / 32768.);
150 }
151
152 static void
153 my_napms(int ms)
154 {
155     if (ms > 0) {
156 #if defined(_WIN32) || !HAVE_GETTIMEOFDAY
157         Sleep((DWORD) ms);
158 #else
159         struct timeval data;
160         data.tv_sec = 0;
161         data.tv_usec = ms * 1000;
162         select(0, NULL, NULL, NULL, &data);
163 #endif
164     }
165 }
166
167 static int
168 get_number(NCURSES_CONST char *cap, const char *env)
169 {
170     int result = tgetnum(cap);
171     char *value = env ? getenv(env) : 0;
172     if (value != 0 && *value != 0) {
173         char *next = 0;
174         long check = strtol(value, &next, 10);
175         if (check > 0 && *next == '\0')
176             result = (int) check;
177     }
178     return result;
179 }
180
181 static void
182 usage(void)
183 {
184     static const char *msg[] =
185     {
186         "Usage: dots_termcap [options]"
187         ,""
188         ,"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)"
193     };
194     size_t n;
195
196     for (n = 0; n < SIZEOF(msg); n++)
197         fprintf(stderr, "%s\n", msg[n]);
198
199     ExitProgram(EXIT_FAILURE);
200 }
201
202 int
203 main(int argc, char *argv[])
204 {
205     int ch;
206     int num_colors;
207     int num_lines;
208     int num_columns;
209     int e_option = 0;
210     int m_option = 2;
211     int s_option = 1;
212     double r;
213     double c;
214     char buffer[1024];
215     char area[1024];
216     char *name;
217     size_t need;
218     char *my_env;
219
220     while ((ch = getopt(argc, argv, "T:em:s:")) != -1) {
221         switch (ch) {
222         case 'T':
223             need = 6 + strlen(optarg);
224             my_env = malloc(need);
225             _nc_SPRINTF(my_env, _nc_SLIMIT(need) "TERM=%s", optarg);
226             putenv(my_env);
227             break;
228         case 'e':
229             e_option = 1;
230             break;
231         case 'm':
232             m_option = atoi(optarg);
233             break;
234         case 's':
235             s_option = atoi(optarg);
236             break;
237         default:
238             usage();
239             break;
240         }
241     }
242
243     if ((name = getenv("TERM")) == 0) {
244         fprintf(stderr, "TERM is not set\n");
245         ExitProgram(EXIT_FAILURE);
246     }
247
248     srand((unsigned) time(0));
249
250     InitAndCatch(ch = tgetent(buffer, name), onsig);
251     if (ch < 0) {
252         fprintf(stderr, "terminal description not found\n");
253         ExitProgram(EXIT_FAILURE);
254     } else {
255         size_t t;
256         char *ap = area;
257         for (t = 0; t < SIZEOF(my_caps); ++t) {
258             *(my_caps[t].value) = tgetstr((NCURSES_CONST char *)
259                                           my_caps[t].name, &ap);
260         }
261     }
262
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");
267
268     outs(t_cl);
269     outs(t_vi);
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)))
274             num_colors = -1;
275     }
276
277     r = (double) (num_lines - (2 * m_option));
278     c = (double) (num_columns - (2 * m_option));
279     started = time((time_t *) 0);
280
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) ? '*' : ' ';
285
286         tputs(tgoto(t_cm, x, y), 1, outc);
287         if (num_colors > 0) {
288             int z = (int) (ranf() * num_colors);
289             if (ranf() > 0.01) {
290                 tputs(tgoto(t_AF, 0, z), 1, outc);
291             } else {
292                 tputs(tgoto(t_AB, 0, z), 1, outc);
293                 my_napms(s_option);
294             }
295         } else if (VALID_STRING(t_me)
296                    && VALID_STRING(t_mr)) {
297             if (ranf() <= 0.01) {
298                 outs((ranf() > 0.6)
299                      ? t_mr
300                      : t_me);
301                 my_napms(s_option);
302             }
303         }
304         outc(p);
305         fflush(stdout);
306         ++total_chars;
307     }
308     cleanup();
309     ExitProgram(EXIT_SUCCESS);
310 }
311 #else
312 int
313 main(int argc GCC_UNUSED,
314      char *argv[]GCC_UNUSED)
315 {
316     fprintf(stderr, "This program requires termcap\n");
317     exit(EXIT_FAILURE);
318 }
319 #endif