Skip to content

Commit

Permalink
Add a fd_readable(int fd) helper function
Browse files Browse the repository at this point in the history
Use it instead of direct calls to select scatered around the code
base.
  • Loading branch information
mawww committed Aug 30, 2016
1 parent d0a2951 commit 8b02bb7
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 49 deletions.
11 changes: 1 addition & 10 deletions src/buffer_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
{
Expand All @@ -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());

Expand Down
11 changes: 11 additions & 0 deletions src/file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <unistd.h>
#include <dirent.h>
#include <stdlib.h>
#include <sys/select.h>

#if defined(__FreeBSD__)
#include <sys/sysctl.h>
Expand Down Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/file.hh
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
15 changes: 1 addition & 14 deletions src/json_ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
29 changes: 4 additions & 25 deletions src/remote.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -573,9 +557,7 @@ class Server::Accepter
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;
Expand Down Expand Up @@ -610,11 +592,8 @@ class Server::Accepter
}
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;
Expand Down

0 comments on commit 8b02bb7

Please sign in to comment.