summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFrédéric Brière <fbriere@fbriere.net>2019-06-28 23:48:04 -0400
committerFrédéric Brière <fbriere@fbriere.net>2019-06-29 19:48:27 -0400
commitd26fa24087265dd4e9dccf0db261da0e6f093c8e (patch)
tree8de0bd9f14ada32c8c47448d7c387cf7d600e5e8
parentcedd61a753785831f71ae5582c134c816c92700d (diff)
downloadtwinkle-d26fa24087265dd4e9dccf0db261da0e6f093c8e.tar
twinkle-d26fa24087265dd4e9dccf0db261da0e6f093c8e.tar.gz
twinkle-d26fa24087265dd4e9dccf0db261da0e6f093c8e.tar.lz
twinkle-d26fa24087265dd4e9dccf0db261da0e6f093c8e.tar.xz
twinkle-d26fa24087265dd4e9dccf0db261da0e6f093c8e.zip
Make Readline non-blocking: Wrap select(2) around Readline call
By only invoking rl_callback_read_char() when there is actual input on stdin, we can now avoid blocking on Readline calls.
-rw-r--r--src/userintf.cpp24
1 files changed, 23 insertions, 1 deletions
diff --git a/src/userintf.cpp b/src/userintf.cpp
index 6534e99..21888ac 100644
--- a/src/userintf.cpp
+++ b/src/userintf.cpp
@@ -17,9 +17,13 @@
#include <iostream>
#include <cstdlib>
+#include <errno.h>
#include <readline/readline.h>
#include <readline/history.h>
#include <signal.h>
+#include <stdio.h>
+#include <string>
+#include <sys/select.h>
#include "address_book.h"
#include "events.h"
#include "line.h"
@@ -2228,12 +2232,30 @@ void t_userintf::run(void) {
rl_callback_handler_install(CLI_PROMPT, tw_readline_cb);
while (!end_interface) {
+ fd_set fds;
+ FD_ZERO(&fds);
+ FD_SET(fileno(rl_instream), &fds);
+
+ int ret = select(FD_SETSIZE, &fds, NULL, NULL, NULL);
+ if ((ret == -1) && (errno != EINTR)) {
+ string msg("select() failed: ");
+ msg += get_error_str(errno);
+ ui->cb_show_msg(msg, MSG_CRITICAL);
+ break;
+ }
// Relay any SIGWINCH to Readline
if (sigwinch_received) {
rl_resize_terminal();
sigwinch_received = 0;
}
- rl_callback_read_char();
+ if (ret == -1) {
+ // errno == EINTR
+ continue;
+ }
+
+ if (FD_ISSET(fileno(rl_instream), &fds)) {
+ rl_callback_read_char();
+ }
}
rl_callback_handler_remove();