summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMaxime Coste <frrrwww@gmail.com>2016-08-30 22:56:47 +0100
committerMaxime Coste <frrrwww@gmail.com>2016-08-30 22:56:47 +0100
commit8b02bb749dece7690f4919372d19cd40907b91b1 (patch)
tree98e636beca5e83f909d3872586f383e95095851b /src
parentd0a29511d28f1481622ac9bb3a6ebfa9f8988c2b (diff)
Add a fd_readable(int fd) helper function
Use it instead of direct calls to select scatered around the code base.
Diffstat (limited to 'src')
-rw-r--r--src/buffer_utils.cc11
-rw-r--r--src/file.cc11
-rw-r--r--src/file.hh1
-rw-r--r--src/json_ui.cc15
-rw-r--r--src/remote.cc29
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 <unistd.h>
-#include <sys/select.h>
-
#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 <unistd.h>
#include <dirent.h>
#include <stdlib.h>
+#include <sys/select.h>
#if defined(__FreeBSD__)
#include <sys/sysctl.h>
@@ -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<StringView, StringView> 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<UserInterface>&&
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;