blob: 7243c104b1730c7f42a9263bcfcf3038544d04a9 [file] [log] [blame]
Steve Kondikae271bc2015-11-15 02:50:53 +01001/****************************************************************************
micky3879b9f5e72025-07-08 18:04:53 -04002 * Copyright 2019-2020,2022 Thomas E. Dickey *
3 * Copyright 1998-2016,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 * Grand digital clock for curses compatible terminals
31 * Usage: gdc [-s] [-t hh:mm:ss] [n] -- run for n seconds (default infinity)
32 * Flags: -s: scroll
33 *
34 * modified 10-18-89 for curses (jrl)
35 * 10-18-89 added signal handling
36 *
micky3879b9f5e72025-07-08 18:04:53 -040037 * $Id: gdc.c,v 1.57 2022/12/04 00:40:11 tom Exp $
Steve Kondikae271bc2015-11-15 02:50:53 +010038 */
39
40#include <test.priv.h>
41
42#include <time.h>
43
44#define YBASE 10
45#define XBASE 10
46#define XLENGTH 54
47#define YDEPTH 5
48
49#define PAIR_DIGITS 1
50#define PAIR_OTHERS 2
51#define PAIR_FRAMES 3
52
53static short disp[11] =
54{
55 075557, 011111, 071747, 071717, 055711,
56 074717, 074757, 071111, 075757, 075717, 002020
57};
58static long older[6], next[6], newer[6], mask;
59
60static int sigtermed = 0;
61static bool redirected = FALSE;
62static bool hascolor = FALSE;
63
64static void
65sighndl(int signo)
66{
67 signal(signo, sighndl);
68 sigtermed = signo;
69 if (redirected) {
micky3879b9f5e72025-07-08 18:04:53 -040070 stop_curses();
Steve Kondikae271bc2015-11-15 02:50:53 +010071 ExitProgram(EXIT_FAILURE);
72 }
73}
74
75static void
76check_term(void)
77{
78 if (sigtermed) {
79 (void) standend();
micky3879b9f5e72025-07-08 18:04:53 -040080 stop_curses();
Steve Kondikae271bc2015-11-15 02:50:53 +010081 fprintf(stderr, "gdc terminated by signal %d\n", sigtermed);
82 ExitProgram(EXIT_FAILURE);
83 }
84}
85
86static void
87drawbox(bool scrolling)
88{
89 chtype bottom[XLENGTH + 1];
Steve Kondikae271bc2015-11-15 02:50:53 +010090
91 if (hascolor)
92 (void) attrset(AttrArg(COLOR_PAIR(PAIR_FRAMES), 0));
93
94 MvAddCh(YBASE - 1, XBASE - 1, ACS_ULCORNER);
95 hline(ACS_HLINE, XLENGTH);
96 MvAddCh(YBASE - 1, XBASE + XLENGTH, ACS_URCORNER);
97
98 MvAddCh(YBASE + YDEPTH, XBASE - 1, ACS_LLCORNER);
99 if ((mvinchnstr(YBASE + YDEPTH, XBASE, bottom, XLENGTH)) != ERR) {
micky3879b9f5e72025-07-08 18:04:53 -0400100 int n;
Steve Kondikae271bc2015-11-15 02:50:53 +0100101 for (n = 0; n < XLENGTH; n++) {
102 if (!scrolling)
103 bottom[n] &= ~A_COLOR;
104 bottom[n] = ACS_HLINE | (bottom[n] & (A_ATTRIBUTES | A_COLOR));
105 }
106 (void) mvaddchnstr(YBASE + YDEPTH, XBASE, bottom, XLENGTH);
107 }
108 MvAddCh(YBASE + YDEPTH, XBASE + XLENGTH, ACS_LRCORNER);
109
110 move(YBASE, XBASE - 1);
111 vline(ACS_VLINE, YDEPTH);
112
113 move(YBASE, XBASE + XLENGTH);
114 vline(ACS_VLINE, YDEPTH);
115
116 if (hascolor)
117 (void) attrset(AttrArg(COLOR_PAIR(PAIR_OTHERS), 0));
118}
119
120static void
121standt(int on)
122{
123 if (on) {
124 if (hascolor) {
125 attron(COLOR_PAIR(PAIR_DIGITS));
126 } else {
127 attron(A_STANDOUT);
128 }
129 } else {
130 if (hascolor) {
131 attron(COLOR_PAIR(PAIR_OTHERS));
132 } else {
133 attroff(A_STANDOUT);
134 }
135 }
136}
137
138static void
139set(int t, int n)
140{
141 int i, m;
142
143 m = 7 << n;
144 for (i = 0; i < 5; i++) {
145 next[i] |= ((disp[t] >> ((4 - i) * 3)) & 07) << n;
146 mask |= (next[i] ^ older[i]) & m;
147 }
148 if (mask & m)
149 mask |= m;
150}
151
152static void
micky3879b9f5e72025-07-08 18:04:53 -0400153usage(int ok)
Steve Kondikae271bc2015-11-15 02:50:53 +0100154{
155 static const char *msg[] =
156 {
157 "Usage: gdc [options] [count]"
158 ,""
micky3879b9f5e72025-07-08 18:04:53 -0400159 ,USAGE_COMMON
Steve Kondikae271bc2015-11-15 02:50:53 +0100160 ,"Options:"
micky3879b9f5e72025-07-08 18:04:53 -0400161#if HAVE_USE_DEFAULT_COLORS
162 ," -d invoke use_default_colors"
163#endif
164 ," -n redirect input to /dev/null"
165 ," -s scroll each number into place, rather than flipping"
166 ," -t TIME specify starting time as hh:mm:ss (default is ``now'')"
Steve Kondikae271bc2015-11-15 02:50:53 +0100167 ,""
168 ,"If you specify a count, gdc runs for that number of seconds"
169 };
170 unsigned j;
171 for (j = 0; j < SIZEOF(msg); j++)
172 fprintf(stderr, "%s\n", msg[j]);
micky3879b9f5e72025-07-08 18:04:53 -0400173 ExitProgram(ok ? EXIT_SUCCESS : EXIT_FAILURE);
Steve Kondikae271bc2015-11-15 02:50:53 +0100174}
175
176static time_t
177parse_time(const char *value)
178{
179 int hh, mm, ss;
180 int check;
181 time_t result;
182 char c;
183 struct tm *tm;
184
185 if (sscanf(value, "%d:%d:%d%c", &hh, &mm, &ss, &c) != 3) {
186 if (sscanf(value, "%02d%02d%02d%c", &hh, &mm, &ss, &c) != 3) {
micky3879b9f5e72025-07-08 18:04:53 -0400187 usage(FALSE);
Steve Kondikae271bc2015-11-15 02:50:53 +0100188 }
189 }
190
191 if ((hh < 0) || (hh >= 24) ||
192 (mm < 0) || (mm >= 60) ||
193 (ss < 0) || (ss >= 60)) {
micky3879b9f5e72025-07-08 18:04:53 -0400194 usage(FALSE);
Steve Kondikae271bc2015-11-15 02:50:53 +0100195 }
196
197 /* adjust so that the localtime in the main loop will give usable time */
198 result = (hh * 3600) + ((mm * 60) + ss);
199 for (check = 0; check < 24; ++check) {
200 tm = localtime(&result);
201 if (tm->tm_hour == hh)
202 break;
203 result += 3600;
204 }
205
206 if (tm->tm_hour != hh) {
207 fprintf(stderr, "Cannot find local time for %s!\n", value);
micky3879b9f5e72025-07-08 18:04:53 -0400208 usage(FALSE);
Steve Kondikae271bc2015-11-15 02:50:53 +0100209 }
210 return result;
211}
micky3879b9f5e72025-07-08 18:04:53 -0400212/* *INDENT-OFF* */
213VERSION_COMMON()
214/* *INDENT-ON* */
Steve Kondikae271bc2015-11-15 02:50:53 +0100215
216int
217main(int argc, char *argv[])
218{
219 time_t now;
220 struct tm *tm;
221 long t, a;
micky3879b9f5e72025-07-08 18:04:53 -0400222 int i, j, s, k, ch;
Steve Kondikae271bc2015-11-15 02:50:53 +0100223 int count = 0;
224 FILE *ofp = stdout;
225 FILE *ifp = stdin;
226 bool smooth = FALSE;
227 bool stages = FALSE;
228 time_t starts = 0;
micky3879b9f5e72025-07-08 18:04:53 -0400229#if HAVE_USE_DEFAULT_COLORS
230 bool d_option = FALSE;
231#endif
Steve Kondikae271bc2015-11-15 02:50:53 +0100232
233 setlocale(LC_ALL, "");
234
micky3879b9f5e72025-07-08 18:04:53 -0400235 while ((ch = getopt(argc, argv, OPTS_COMMON "dnst:")) != -1) {
236 switch (ch) {
237#if HAVE_USE_DEFAULT_COLORS
238 case 'd':
239 d_option = TRUE;
240 break;
241#endif
Steve Kondikae271bc2015-11-15 02:50:53 +0100242 case 'n':
243 ifp = fopen("/dev/null", "r");
244 redirected = TRUE;
245 break;
246 case 's':
247 smooth = TRUE;
248 break;
249 case 't':
250 starts = parse_time(optarg);
251 break;
micky3879b9f5e72025-07-08 18:04:53 -0400252 case OPTS_VERSION:
253 show_version(argv);
254 ExitProgram(EXIT_SUCCESS);
Steve Kondikae271bc2015-11-15 02:50:53 +0100255 default:
micky3879b9f5e72025-07-08 18:04:53 -0400256 usage(ch == OPTS_USAGE);
257 /* NOTREACHED */
Steve Kondikae271bc2015-11-15 02:50:53 +0100258 }
259 }
260 if (optind < argc) {
261 count = atoi(argv[optind++]);
262 assert(count >= 0);
micky3879b9f5e72025-07-08 18:04:53 -0400263 if (optind < argc)
264 usage(FALSE);
Steve Kondikae271bc2015-11-15 02:50:53 +0100265 }
Steve Kondikae271bc2015-11-15 02:50:53 +0100266
micky3879b9f5e72025-07-08 18:04:53 -0400267 InitAndCatch({
268 if (redirected) {
269 char *name = getenv("TERM");
270 if (name == 0
271 || newterm(name, ofp, ifp) == 0) {
272 fprintf(stderr, "cannot open terminal\n");
273 ExitProgram(EXIT_FAILURE);
274 }
275 } else {
276 initscr();
Steve Kondikae271bc2015-11-15 02:50:53 +0100277 }
Steve Kondikae271bc2015-11-15 02:50:53 +0100278 }
micky3879b9f5e72025-07-08 18:04:53 -0400279 ,sighndl);
280
Steve Kondikae271bc2015-11-15 02:50:53 +0100281 cbreak();
282 noecho();
283 nodelay(stdscr, 1);
284 curs_set(0);
285
286 hascolor = has_colors();
287
288 if (hascolor) {
289 short bg = COLOR_BLACK;
290 start_color();
291#if HAVE_USE_DEFAULT_COLORS
micky3879b9f5e72025-07-08 18:04:53 -0400292 if (d_option && (use_default_colors() == OK))
Steve Kondikae271bc2015-11-15 02:50:53 +0100293 bg = -1;
294#endif
295 init_pair(PAIR_DIGITS, COLOR_BLACK, COLOR_RED);
296 init_pair(PAIR_OTHERS, COLOR_RED, bg);
297 init_pair(PAIR_FRAMES, COLOR_WHITE, bg);
298 (void) attrset(AttrArg(COLOR_PAIR(PAIR_OTHERS), 0));
299 }
300
301 restart:
302 for (j = 0; j < 5; j++)
303 older[j] = newer[j] = next[j] = 0;
304
305 clear();
306 drawbox(FALSE);
307
308 do {
309 char buf[40];
310
311 if (starts != 0) {
312 now = ++starts;
313 } else {
314 time(&now);
315 }
316 tm = localtime(&now);
317
318 mask = 0;
319 set(tm->tm_sec % 10, 0);
320 set(tm->tm_sec / 10, 4);
321 set(tm->tm_min % 10, 10);
322 set(tm->tm_min / 10, 14);
323 set(tm->tm_hour % 10, 20);
324 set(tm->tm_hour / 10, 24);
325 set(10, 7);
326 set(10, 17);
327
328 for (k = 0; k < 6; k++) {
329 if (smooth) {
330 for (i = 0; i < 5; i++)
331 newer[i] = (newer[i] & ~mask) | (newer[i + 1] & mask);
332 newer[5] = (newer[5] & ~mask) | (next[k] & mask);
333 } else {
334 newer[k] = (newer[k] & ~mask) | (next[k] & mask);
335 }
336 next[k] = 0;
337 for (s = 1; s >= 0; s--) {
338 standt(s);
339 for (i = 0; i < 6; i++) {
340 if ((a = (newer[i] ^ older[i]) & (s ? newer : older)[i])
341 != 0) {
342 for (j = 0, t = 1 << 26; t; t >>= 1, j++) {
343 if (a & t) {
344 if (!(a & (t << 1))) {
345 move(YBASE + i, XBASE + 2 * j);
346 }
347 addstr(" ");
348 }
349 }
350 }
351 if (!s) {
352 older[i] = newer[i];
353 }
354 }
355 if (!s) {
356 if (smooth)
357 drawbox(TRUE);
358 refresh();
359 /*
360 * If we're scrolling, space out the refreshes to fake
361 * movement. That's 7 frames, or 6 intervals, which would
362 * be 166 msec if we spread it out over a second. It looks
363 * better (but will work on a slow terminal, e.g., less
364 * than 9600bd) to squeeze that into a half-second, and use
365 * half of 170 msec to ensure that the program doesn't eat
366 * a lot of time when asking what time it is, at the top of
367 * this loop -T.Dickey
368 */
369 if (smooth)
370 napms(85);
371 if (stages) {
372 stages = FALSE;
373 switch (wgetch(stdscr)) {
374 case 'q':
375 count = 1;
376 break;
377 case 'S':
378 stages = TRUE;
379 /* FALLTHRU */
380 case 's':
381 nodelay(stdscr, FALSE);
382 break;
383 case ' ':
384 nodelay(stdscr, TRUE);
385 break;
386#ifdef KEY_RESIZE
387 case KEY_RESIZE:
388#endif
389 case '?':
390 goto restart;
391 case ERR:
392 check_term();
393 /* FALLTHRU */
394 default:
395 continue;
396 }
397 }
398 }
399 }
400 }
401
402 /* this depends on the detailed format of ctime(3) */
micky3879b9f5e72025-07-08 18:04:53 -0400403 _nc_STRNCPY(buf, ctime(&now), (size_t) 30);
Steve Kondikae271bc2015-11-15 02:50:53 +0100404 {
405 char *d2 = buf + 10;
406 char *s2 = buf + 19;
407 while ((*d2++ = *s2++) != '\0') ;
408 }
409 MvAddStr(16, 30, buf);
410
411 move(6, 0);
412 drawbox(FALSE);
413 refresh();
414
415 /*
416 * If we're not smooth-scrolling, wait 1000 msec (1 sec). Use napms()
417 * rather than sleep() because the latter does odd things on some
418 * systems, e.g., suspending output as well.
419 */
420 if (smooth)
421 napms(500);
422 else
423 napms(1000);
424
425 /*
426 * This is a safe way to check if we're interrupted - making the signal
427 * handler set a flag that we can check. Since we're running
428 * nodelay(), the wgetch() call returns immediately, and in particular
429 * will return an error if interrupted. This works only if we can
430 * read from the input, of course.
431 */
432 stages = FALSE;
433 switch (wgetch(stdscr)) {
434 case 'q':
435 count = 1;
436 break;
437 case 'S':
438 stages = TRUE;
439 /* FALLTHRU */
440 case 's':
441 nodelay(stdscr, FALSE);
442 break;
443 case ' ':
444 nodelay(stdscr, TRUE);
445 break;
446#ifdef KEY_RESIZE
447 case KEY_RESIZE:
448#endif
449 case '?':
450 goto restart;
451 case ERR:
452 check_term();
453 /* FALLTHRU */
454 default:
455 continue;
456 }
457 } while (--count);
458 (void) standend();
micky3879b9f5e72025-07-08 18:04:53 -0400459 stop_curses();
Steve Kondikae271bc2015-11-15 02:50:53 +0100460 ExitProgram(EXIT_SUCCESS);
461}