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