From 453a76c8b169474eadc4f036e8c610b04731400e Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Wed, 2 Dec 2015 17:14:12 +0100 Subject: use specified rate MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- src/rq.c | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/rq.c b/src/rq.c index 6bf1070..e223944 100644 --- a/src/rq.c +++ b/src/rq.c @@ -33,6 +33,7 @@ #include #include #include +#include #define t(...) do { if (__VA_ARGS__) goto fail; } while (0) @@ -58,6 +59,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. */ @@ -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)); -- cgit v1.2.3-70-g09d2