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.26 2020/09/05 17:58:47 juergen 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
TPUTS_PROTO(outc,c)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
outs(char * s)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
cleanup(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
onsig(int n GCC_UNUSED)141 onsig(int n GCC_UNUSED)
142 {
143 interrupted = TRUE;
144 }
145
146 static double
ranf(void)147 ranf(void)
148 {
149 long r = (rand() & 077777);
150 return ((double) r / 32768.);
151 }
152
153 static void
my_napms(int ms)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
get_number(NCURSES_CONST char * cap,const char * env)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
usage(void)183 usage(void)
184 {
185 static const char *msg[] =
186 {
187 "Usage: dots_termcap [options]"
188 ,""
189 ,"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)"
195 };
196 size_t n;
197
198 for (n = 0; n < SIZEOF(msg); n++)
199 fprintf(stderr, "%s\n", msg[n]);
200
201 ExitProgram(EXIT_FAILURE);
202 }
203
204 int
main(int argc,char * argv[])205 main(int argc, char *argv[])
206 {
207 int ch;
208 int num_colors;
209 int num_lines;
210 int num_columns;
211 int e_option = 0;
212 int m_option = 2;
213 int r_option = 0;
214 int s_option = 1;
215 double r;
216 double c;
217 char buffer[1024];
218 char area[1024];
219 char *name;
220 size_t need;
221 char *my_env;
222
223 while ((ch = getopt(argc, argv, "T:em:r:s:")) != -1) {
224 switch (ch) {
225 case 'T':
226 need = 6 + strlen(optarg);
227 my_env = malloc(need);
228 _nc_SPRINTF(my_env, _nc_SLIMIT(need) "TERM=%s", optarg);
229 putenv(my_env);
230 break;
231 case 'e':
232 e_option = 1;
233 break;
234 case 'm':
235 m_option = atoi(optarg);
236 break;
237 case 'r':
238 r_option = atoi(optarg);
239 break;
240 case 's':
241 s_option = atoi(optarg);
242 break;
243 default:
244 usage();
245 break;
246 }
247 }
248
249 if ((name = getenv("TERM")) == 0) {
250 fprintf(stderr, "TERM is not set\n");
251 ExitProgram(EXIT_FAILURE);
252 }
253
254 srand((unsigned) time(0));
255
256 SetupAlarm((unsigned) r_option);
257 InitAndCatch(ch = tgetent(buffer, name), onsig);
258 if (ch < 0) {
259 fprintf(stderr, "terminal description not found\n");
260 ExitProgram(EXIT_FAILURE);
261 } else {
262 size_t t;
263 char *ap = area;
264 for (t = 0; t < SIZEOF(my_caps); ++t) {
265 *(my_caps[t].value) = tgetstr((NCURSES_CONST char *)
266 my_caps[t].name, &ap);
267 }
268 }
269
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");
274
275 outs(t_cl);
276 outs(t_vi);
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)))
281 num_colors = -1;
282 }
283
284 r = (double) (num_lines - (2 * m_option));
285 c = (double) (num_columns - (2 * m_option));
286 started = time((time_t *) 0);
287
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) ? '*' : ' ';
292
293 tputs(tgoto(t_cm, x, y), 1, outc);
294 if (num_colors > 0) {
295 int z = (int) (ranf() * num_colors);
296 if (ranf() > 0.01) {
297 tputs(tgoto(t_AF, 0, z), 1, outc);
298 } else {
299 tputs(tgoto(t_AB, 0, z), 1, outc);
300 if (s_option)
301 my_napms(s_option);
302 }
303 } else if (VALID_STRING(t_me)
304 && VALID_STRING(t_mr)) {
305 if (ranf() <= 0.01) {
306 outs((ranf() > 0.6)
307 ? t_mr
308 : t_me);
309 if (s_option)
310 my_napms(s_option);
311 }
312 }
313 outc(p);
314 fflush(stdout);
315 ++total_chars;
316 }
317 cleanup();
318 ExitProgram(EXIT_SUCCESS);
319 }
320 #else
321 int
main(int argc GCC_UNUSED,char * argv[]GCC_UNUSED)322 main(int argc GCC_UNUSED,
323 char *argv[]GCC_UNUSED)
324 {
325 fprintf(stderr, "This program requires termcap\n");
326 exit(EXIT_FAILURE);
327 }
328 #endif
329