blob: 65786c411a4ae1bde374e677bd556fe70fe547c4 [file] [log] [blame]
Steve Kondikae271bc2015-11-15 02:50:53 +01001/****************************************************************************
micky3879b9f5e72025-07-08 18:04:53 -04002 * Copyright 2018-2022,2023 Thomas E. Dickey *
3 * Copyright 2013-2014,2017 Free Software Foundation, Inc. *
Steve Kondikae271bc2015-11-15 02:50:53 +01004 * *
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 *
micky3879b9f5e72025-07-08 18:04:53 -040033 * $Id: dots_termcap.c,v 1.32 2023/02/25 18:11:21 tom Exp $
Steve Kondikae271bc2015-11-15 02:50:53 +010034 *
35 * A simple demo of the termcap interface.
36 */
37#define USE_TINFO
38#include <test.priv.h>
39
micky3879b9f5e72025-07-08 18:04:53 -040040#if !defined(_NC_WINDOWS)
Steve Kondikae271bc2015-11-15 02:50:53 +010041#include <sys/time.h>
42#endif
43
44#if HAVE_TGETENT
45
46#include <time.h>
47
Steve Kondikae271bc2015-11-15 02:50:53 +010048static bool interrupted = FALSE;
49static long total_chars = 0;
50static time_t started;
51
52static char *t_AB;
53static char *t_AF;
54static char *t_cl;
55static char *t_cm;
56static char *t_me;
57static char *t_mr;
58static char *t_oc;
59static char *t_op;
60static char *t_ve;
61static char *t_vi;
62
63static struct {
micky3879b9f5e72025-07-08 18:04:53 -040064 NCURSES_CONST char *name;
Steve Kondikae271bc2015-11-15 02:50:53 +010065 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
100static
101TPUTS_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
115static bool
116outs(char *s)
117{
micky3879b9f5e72025-07-08 18:04:53 -0400118 if (VALID_STRING(s)) {
Steve Kondikae271bc2015-11-15 02:50:53 +0100119 tputs(s, 1, outc);
120 return TRUE;
121 }
122 return FALSE;
123}
124
125static void
126cleanup(void)
127{
128 outs(t_me);
129 if (!outs(t_oc))
130 outs(t_op);
131 outs(t_cl);
132 outs(t_ve);
133
micky3879b9f5e72025-07-08 18:04:53 -0400134 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)));
Steve Kondikae271bc2015-11-15 02:50:53 +0100138}
139
140static void
141onsig(int n GCC_UNUSED)
142{
143 interrupted = TRUE;
144}
145
146static double
147ranf(void)
148{
149 long r = (rand() & 077777);
150 return ((double) r / 32768.);
151}
152
micky3879b9f5e72025-07-08 18:04:53 -0400153/*
154 * napms is a curses function which happens to be usable without initializing
155 * the screen, but if this program happened to be build with a "real" termcap
156 * library, there is nothing like napms.
157 */
158#if HAVE_NAPMS
159#define my_napms(ms) napms(ms)
160#else
Steve Kondikae271bc2015-11-15 02:50:53 +0100161static void
162my_napms(int ms)
163{
micky3879b9f5e72025-07-08 18:04:53 -0400164 if (ms > 0) {
165#if defined(_NC_WINDOWS)
166 Sleep((unsigned int) ms);
Steve Kondikae271bc2015-11-15 02:50:53 +0100167#else
micky3879b9f5e72025-07-08 18:04:53 -0400168 struct timeval data;
169 data.tv_sec = 0;
170 data.tv_usec = ms * 1000;
171 select(0, NULL, NULL, NULL, &data);
Steve Kondikae271bc2015-11-15 02:50:53 +0100172#endif
micky3879b9f5e72025-07-08 18:04:53 -0400173 }
174}
175#endif
176
177static int
178get_number(NCURSES_CONST char *cap, const char *env)
179{
180 int result = tgetnum(cap);
181 char *value = env ? getenv(env) : 0;
182 if (value != 0 && *value != 0) {
183 char *next = 0;
184 long check = strtol(value, &next, 10);
185 if (check > 0 && *next == '\0')
186 result = (int) check;
187 }
188 return result;
Steve Kondikae271bc2015-11-15 02:50:53 +0100189}
190
micky3879b9f5e72025-07-08 18:04:53 -0400191static void
192usage(int ok)
Steve Kondikae271bc2015-11-15 02:50:53 +0100193{
micky3879b9f5e72025-07-08 18:04:53 -0400194 static const char *msg[] =
195 {
196 "Usage: dots_termcap [options]"
197 ,""
198 ,USAGE_COMMON
199 ,"Options:"
200 ," -T TERM override $TERM"
201 ," -e allow environment $LINES / $COLUMNS"
202 ," -m SIZE set margin (default: 2)"
203 ," -r SECS self-interrupt/exit after specified number of seconds"
204 ," -s MSECS delay 1% of the time (default: 1 msecs)"
205 };
206 size_t n;
207
208 for (n = 0; n < SIZEOF(msg); n++)
209 fprintf(stderr, "%s\n", msg[n]);
210
211 ExitProgram(ok ? EXIT_SUCCESS : EXIT_FAILURE);
212}
213/* *INDENT-OFF* */
214VERSION_COMMON()
215/* *INDENT-ON* */
216
217int
218main(int argc, char *argv[])
219{
220 int ch;
Steve Kondikae271bc2015-11-15 02:50:53 +0100221 int num_colors;
222 int num_lines;
223 int num_columns;
micky3879b9f5e72025-07-08 18:04:53 -0400224 int e_option = 0;
225 int m_option = 2;
226 int r_option = 0;
227 int s_option = 1;
Steve Kondikae271bc2015-11-15 02:50:53 +0100228 double r;
229 double c;
230 char buffer[1024];
231 char area[1024];
232 char *name;
micky3879b9f5e72025-07-08 18:04:53 -0400233 size_t need;
234 char *my_env;
Steve Kondikae271bc2015-11-15 02:50:53 +0100235
micky3879b9f5e72025-07-08 18:04:53 -0400236 while ((ch = getopt(argc, argv, OPTS_COMMON "T:em:r:s:")) != -1) {
237 switch (ch) {
238 case 'T':
239 need = 6 + strlen(optarg);
240 if ((my_env = malloc(need)) != NULL) {
241 _nc_SPRINTF(my_env, _nc_SLIMIT(need) "TERM=%s", optarg);
242 putenv(my_env);
243 }
244 break;
245 case 'e':
246 e_option = 1;
247 break;
248 case 'm':
249 m_option = atoi(optarg);
250 break;
251 case 'r':
252 r_option = atoi(optarg);
253 break;
254 case 's':
255 s_option = atoi(optarg);
256 break;
257 case OPTS_VERSION:
258 show_version(argv);
259 ExitProgram(EXIT_SUCCESS);
260 default:
261 usage(ch == OPTS_USAGE);
262 /* NOTREACHED */
263 }
264 }
Steve Kondikae271bc2015-11-15 02:50:53 +0100265
266 if ((name = getenv("TERM")) == 0) {
267 fprintf(stderr, "TERM is not set\n");
268 ExitProgram(EXIT_FAILURE);
micky3879b9f5e72025-07-08 18:04:53 -0400269 }
270
271 srand((unsigned) time(0));
272
273 SetupAlarm((unsigned) r_option);
274 InitAndCatch(ch = tgetent(buffer, name), onsig);
275 if (ch < 0) {
Steve Kondikae271bc2015-11-15 02:50:53 +0100276 fprintf(stderr, "terminal description not found\n");
277 ExitProgram(EXIT_FAILURE);
278 } else {
279 size_t t;
280 char *ap = area;
281 for (t = 0; t < SIZEOF(my_caps); ++t) {
282 *(my_caps[t].value) = tgetstr((NCURSES_CONST char *)
283 my_caps[t].name, &ap);
284 }
285 }
286
287 num_colors = tgetnum("Co");
micky3879b9f5e72025-07-08 18:04:53 -0400288#define GetNumber(cap,env) get_number(cap, e_option ? env : 0)
289 num_lines = GetNumber("li", "LINES");
290 num_columns = GetNumber("co", "COLUMNS");
Steve Kondikae271bc2015-11-15 02:50:53 +0100291
292 outs(t_cl);
293 outs(t_vi);
294 if (num_colors > 1) {
micky3879b9f5e72025-07-08 18:04:53 -0400295 if (!VALID_STRING(t_AF)
296 || !VALID_STRING(t_AB)
297 || (!VALID_STRING(t_oc) && !VALID_STRING(t_op)))
Steve Kondikae271bc2015-11-15 02:50:53 +0100298 num_colors = -1;
299 }
300
micky3879b9f5e72025-07-08 18:04:53 -0400301 r = (double) (num_lines - (2 * m_option));
302 c = (double) (num_columns - (2 * m_option));
Steve Kondikae271bc2015-11-15 02:50:53 +0100303 started = time((time_t *) 0);
304
305 while (!interrupted) {
micky3879b9f5e72025-07-08 18:04:53 -0400306 int x = (int) (c * ranf()) + m_option;
307 int y = (int) (r * ranf()) + m_option;
308 int p = (ranf() > 0.9) ? '*' : ' ';
Steve Kondikae271bc2015-11-15 02:50:53 +0100309
310 tputs(tgoto(t_cm, x, y), 1, outc);
311 if (num_colors > 0) {
micky3879b9f5e72025-07-08 18:04:53 -0400312 int z = (int) (ranf() * num_colors);
Steve Kondikae271bc2015-11-15 02:50:53 +0100313 if (ranf() > 0.01) {
314 tputs(tgoto(t_AF, 0, z), 1, outc);
315 } else {
316 tputs(tgoto(t_AB, 0, z), 1, outc);
micky3879b9f5e72025-07-08 18:04:53 -0400317 if (s_option)
318 my_napms(s_option);
Steve Kondikae271bc2015-11-15 02:50:53 +0100319 }
micky3879b9f5e72025-07-08 18:04:53 -0400320 } else if (VALID_STRING(t_me)
321 && VALID_STRING(t_mr)) {
Steve Kondikae271bc2015-11-15 02:50:53 +0100322 if (ranf() <= 0.01) {
323 outs((ranf() > 0.6)
324 ? t_mr
325 : t_me);
micky3879b9f5e72025-07-08 18:04:53 -0400326 if (s_option)
327 my_napms(s_option);
Steve Kondikae271bc2015-11-15 02:50:53 +0100328 }
329 }
330 outc(p);
331 fflush(stdout);
332 ++total_chars;
333 }
334 cleanup();
335 ExitProgram(EXIT_SUCCESS);
336}
337#else
338int
micky3879b9f5e72025-07-08 18:04:53 -0400339main(void)
Steve Kondikae271bc2015-11-15 02:50:53 +0100340{
341 fprintf(stderr, "This program requires termcap\n");
342 exit(EXIT_FAILURE);
343}
344#endif