diff options
author | Mattias Andrée <maandree@operamail.com> | 2015-12-02 17:14:12 +0100 |
---|---|---|
committer | Mattias Andrée <maandree@operamail.com> | 2015-12-02 17:14:12 +0100 |
commit | 453a76c8b169474eadc4f036e8c610b04731400e (patch) | |
tree | 4433b6a8f3dacc09473f10fb2b80ff35a9bbcbf9 /src | |
parent | typo (diff) | |
download | read-quickly-453a76c8b169474eadc4f036e8c610b04731400e.tar.gz read-quickly-453a76c8b169474eadc4f036e8c610b04731400e.tar.bz2 read-quickly-453a76c8b169474eadc4f036e8c610b04731400e.tar.xz |
use specified rate
Signed-off-by: Mattias Andrée <maandree@operamail.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/rq.c | 29 |
1 files changed, 24 insertions, 5 deletions
@@ -33,6 +33,7 @@ #include <signal.h> #include <sys/ioctl.h> #include <sys/stat.h> +#include <sys/time.h> #define t(...) do { if (__VA_ARGS__) goto fail; } while (0) @@ -59,6 +60,11 @@ static const char *argv0; static volatile sig_atomic_t caught_sigwinch = 1; /** + * Has the timer expired? + */ +static volatile sig_atomic_t caught_sigalrm = 0; + +/** * The width of the terminal. */ static size_t width = 80; @@ -81,6 +87,16 @@ static void sigwinch(int signo) } +/** + * Signal handler for SIGALRM. + * Invoked when the timer expires. + */ +static void sigalrm(int signo) +{ + signal(signo, sigalrm); + caught_sigalrm = 1; +} + /** * Get the size of the terminal. @@ -103,7 +119,6 @@ static void get_terminal_size(void) } - /** * Get the selected word rate by reading * the environment variable RQ_RATE. @@ -148,7 +163,6 @@ static long get_word_rate(void) } - /** * Count the number of character in a string. * @@ -169,7 +183,6 @@ static size_t display_len(const char *s) } - /** * Display a file word by word. * @@ -189,6 +202,8 @@ static int display_file(int fd, int ttyfd, long rate) char *s; char *end; char c; + struct itimerval interval; + memset(&interval, 0, sizeof(interval)); /* Load file. */ for (;;) { @@ -208,8 +223,12 @@ static int display_file(int fd, int ttyfd, long rate) } /* Present file. */ + interval.it_value.tv_usec = 60000000L / rate; + interval.it_value.tv_sec = interval.it_value.tv_usec / 1000000L; + interval.it_value.tv_usec %= 1000000L; for (s = buffer; *s; s = end) { - sleep(1); + setitimer(ITIMER_REAL, &interval, NULL); + pause(); get_terminal_size(); while (isspace(*s)) s++; @@ -234,7 +253,6 @@ fail: } - int main(int argc, char *argv[]) { int dashed = 0; @@ -293,6 +311,7 @@ int main(int argc, char *argv[]) tty_configured = 1; /* Display file. */ + signal(SIGALRM, sigalrm); signal(SIGWINCH, sigwinch); t (display_file(fd, ttyfd, rate)); |