From d26fa24087265dd4e9dccf0db261da0e6f093c8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Bri=C3=A8re?= Date: Fri, 28 Jun 2019 23:48:04 -0400 Subject: 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. --- src/userintf.cpp | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) 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 #include +#include #include #include #include +#include +#include +#include #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(); -- cgit v1.2.3