There should be a nicer way to quit than ctrl-c. This lets you press q to
quit. (and also checks for esc, for users who don't read the manual.)


Index: grdc.6
===================================================================
RCS file: /cvs/src/games/grdc/grdc.6,v
retrieving revision 1.10
diff -u -p -r1.10 grdc.6
--- grdc.6      17 Nov 2014 22:14:25 -0000      1.10
+++ grdc.6      6 Jan 2019 02:37:05 -0000
@@ -27,6 +27,9 @@ flag makes digits scroll as they change.
 If the terminal is too slow to keep up,
 .Nm
 will skip seconds.
+Pressing the
+.Li q
+key will exit.
 .Sh AUTHORS
 .An -nosplit
 .An Amos Shapir ,
Index: grdc.c
===================================================================
RCS file: /cvs/src/games/grdc/grdc.c,v
retrieving revision 1.29
diff -u -p -r1.29 grdc.c
--- grdc.c      23 Aug 2018 06:25:01 -0000      1.29
+++ grdc.c      6 Jan 2019 02:45:46 -0000
@@ -18,7 +18,9 @@
 #include <limits.h>
 #include <signal.h>
 #include <stdlib.h>
+#include <stdio.h>
 #include <time.h>
+#include <poll.h>
 #include <unistd.h>
 
 #define XLENGTH 58
@@ -59,10 +61,11 @@ int
 main(int argc, char *argv[])
 {
        long t, a;
-       int i, j, s, k;
+       int i, j, s, k, rv;
        int scrol;
        int n = 0;
        struct timespec delay, end;
+       struct pollfd pfd;
        const char *errstr;
        long scroldelay = 50000000;
        int xbase;
@@ -106,6 +109,9 @@ main(int argc, char *argv[])
        signal(SIGWINCH, sigresize);
        signal(SIGCONT, sigresize);     /* for resizes during suspend */
 
+       pfd.fd = STDIN_FILENO;
+       pfd.events = POLLIN;
+
        cbreak();
        noecho();
 
@@ -224,7 +230,15 @@ main(int argc, char *argv[])
                /* want scrolling to END on the second */
                if (scrol && !wintoosmall)
                        delay.tv_nsec -= 5 * scroldelay;
-               nanosleep(&delay, NULL);
+               rv = ppoll(&pfd, 1, &delay, NULL);
+               if (rv == 1) {
+                       char q = 0;
+                       read(STDIN_FILENO, &q, 1);
+                       if (q == 'q' || q == 'Q' || q == '\x1b') {
+                               n = 1;
+                               end.tv_sec = now.tv_sec;
+                       }
+               }
                now.tv_sec++;
 
                if (sigtermed) {

Reply via email to