From 8b02bb749dece7690f4919372d19cd40907b91b1 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Tue, 30 Aug 2016 22:56:47 +0100 Subject: [PATCH] Add a fd_readable(int fd) helper function Use it instead of direct calls to select scatered around the code base. --- src/buffer_utils.cc | 11 +---------- src/file.cc | 11 +++++++++++ src/file.hh | 1 + src/json_ui.cc | 15 +-------------- src/remote.cc | 29 ++++------------------------- 5 files changed, 18 insertions(+), 49 deletions(-) diff --git a/src/buffer_utils.cc b/src/buffer_utils.cc index 8e61c28e..09a65952 100644 --- a/src/buffer_utils.cc +++ b/src/buffer_utils.cc @@ -4,9 +4,6 @@ #include "event_manager.hh" #include "file.hh" -#include -#include - #if defined(__APPLE__) #define st_mtim st_mtimespec #endif @@ -120,8 +117,6 @@ Buffer* create_fifo_buffer(String name, int fd, bool scroll) size_t loops = 16; char data[buffer_size]; const int fifo = watcher.fd(); - timeval tv{ 0, 0 }; - fd_set rfds; ssize_t count = 0; do { @@ -142,12 +137,8 @@ Buffer* create_fifo_buffer(String name, int fd, bool scroll) if (data[count-1] == '\n') buffer->insert(buffer->end_coord(), "\n"); } - - FD_ZERO(&rfds); - FD_SET(fifo, &rfds); } - while (--loops and count > 0 and - select(fifo+1, &rfds, nullptr, nullptr, &tv) == 1); + while (--loops and count > 0 and fd_readable(fifo)); buffer->run_hook_in_own_context("BufReadFifo", buffer->name()); diff --git a/src/file.cc b/src/file.cc index f8928243..1bd0406e 100644 --- a/src/file.cc +++ b/src/file.cc @@ -13,6 +13,7 @@ #include #include #include +#include #if defined(__FreeBSD__) #include @@ -123,6 +124,16 @@ String compact_path(StringView filename) return filename.str(); } +bool fd_readable(int fd) +{ + fd_set rfds; + FD_ZERO(&rfds); + FD_SET(fd, &rfds); + + timeval tv{0,0}; + return select(fd+1, &rfds, nullptr, nullptr, &tv) == 1; +} + String read_fd(int fd, bool text) { String content; diff --git a/src/file.hh b/src/file.hh index 29d979e3..2dde1abb 100644 --- a/src/file.hh +++ b/src/file.hh @@ -40,6 +40,7 @@ std::pair split_path(StringView path); String get_kak_binary_path(); +bool fd_readable(int fd); String read_fd(int fd, bool text = false); String read_file(StringView filename, bool text = false); void write(int fd, StringView data); diff --git a/src/json_ui.cc b/src/json_ui.cc index 644209c0..25fa4513 100644 --- a/src/json_ui.cc +++ b/src/json_ui.cc @@ -399,24 +399,11 @@ void JsonUI::eval_json(const Value& json) throw runtime_error("unknown method"); } -static bool stdin_ready() -{ - fd_set rfds; - FD_ZERO(&rfds); - FD_SET(0, &rfds); - - timeval tv; - tv.tv_sec = 0; - tv.tv_usec = 0; - - return select(1, &rfds, nullptr, nullptr, &tv) == 1; -} - void JsonUI::parse_requests(EventMode mode) { constexpr size_t bufsize = 1024; char buf[bufsize]; - while (stdin_ready()) + while (fd_readable(0)) { ssize_t size = ::read(0, buf, bufsize); if (size == -1 or size == 0) diff --git a/src/remote.cc b/src/remote.cc index ca079427..b4886dd7 100644 --- a/src/remote.cc +++ b/src/remote.cc @@ -372,17 +372,7 @@ void RemoteUI::set_ui_options(const Options& options) bool RemoteUI::is_key_available() { - timeval tv; - fd_set rfds; - - int sock = m_socket_watcher.fd(); - FD_ZERO(&rfds); - FD_SET(sock, &rfds); - - tv.tv_sec = 0; - tv.tv_usec = 0; - int res = select(sock+1, &rfds, nullptr, nullptr, &tv); - return res == 1; + return fd_readable(m_socket_watcher.fd()); } Key RemoteUI::get_key() @@ -466,15 +456,9 @@ RemoteClient::RemoteClient(StringView session, std::unique_ptr&& void RemoteClient::process_available_messages() { int socket = m_socket_watcher->fd(); - timeval tv{ 0, 0 }; - fd_set rfds; - do { process_next_message(); - - FD_ZERO(&rfds); - FD_SET(socket, &rfds); - } while (select(socket+1, &rfds, nullptr, nullptr, &tv) == 1); + } while (fd_readable(socket)); } void RemoteClient::process_next_message() @@ -573,9 +557,7 @@ public: private: void handle_available_input() { - int socket = m_socket_watcher.fd(); - timeval tv{ 0, 0 }; - fd_set rfds; + const int socket = m_socket_watcher.fd(); do { char c; @@ -610,11 +592,8 @@ private: } else m_buffer += c; - - FD_ZERO(&rfds); - FD_SET(socket, &rfds); } - while (select(socket+1, &rfds, nullptr, nullptr, &tv) == 1); + while (fd_readable(socket)); } String m_buffer;