1
1
mirror of https://github.com/mawww/kakoune.git synced 2024-12-25 12:36:11 +03:00

Add support for running Kakoune with redirected stdin

Will read to a *stdin* fifo buffer
This commit is contained in:
Maxime Coste 2014-04-29 21:37:11 +01:00
parent f683946681
commit 479c067a40
4 changed files with 58 additions and 31 deletions

View File

@ -1,5 +1,7 @@
#include "buffer_utils.hh"
#include "event_manager.hh"
namespace Kakoune
{
@ -20,4 +22,37 @@ CharCount get_column(const Buffer& buffer,
return col;
}
Buffer* create_fifo_buffer(String name, int fd)
{
Buffer* buffer = new Buffer(std::move(name), Buffer::Flags::Fifo | Buffer::Flags::NoUndo);
auto watcher = new FDWatcher(fd, [buffer](FDWatcher& watcher) {
constexpr size_t buffer_size = 1024 * 16;
char data[buffer_size];
ssize_t count = read(watcher.fd(), data, buffer_size);
buffer->insert(buffer->end()-1, count > 0 ? String(data, data+count)
: "*** kak: fifo closed ***\n");
if (count <= 0)
{
kak_assert(buffer->flags() & Buffer::Flags::Fifo);
buffer->flags() &= ~Buffer::Flags::Fifo;
buffer->flags() &= ~Buffer::Flags::NoUndo;
close(watcher.fd());
delete &watcher;
}
});
buffer->hooks().add_hook("BufClose", "",
[buffer, watcher](const String&, const Context&) {
// Check if fifo is still alive, else watcher is already dead
if (buffer->flags() & Buffer::Flags::Fifo)
{
close(watcher->fd());
delete watcher;
}
});
return buffer;
}
}

View File

@ -41,6 +41,8 @@ inline void avoid_eol(const Buffer& buffer, Selection& sel)
CharCount get_column(const Buffer& buffer,
CharCount tabstop, BufferCoord coord);
Buffer* create_fifo_buffer(String name, int fd);
}
#endif // buffer_utils_hh_INCLUDED

View File

@ -2,6 +2,7 @@
#include "buffer.hh"
#include "buffer_manager.hh"
#include "buffer_utils.hh"
#include "client.hh"
#include "client_manager.hh"
#include "color_registry.hh"
@ -45,7 +46,7 @@ Buffer* open_or_create(const String& filename, Context& context)
return buffer;
}
Buffer* open_fifo(const String& name , const String& filename, Context& context)
Buffer* open_fifo(const String& name , const String& filename)
{
int fd = open(parse_filename(filename).c_str(), O_RDONLY);
fcntl(fd, F_SETFD, FD_CLOEXEC);
@ -54,35 +55,7 @@ Buffer* open_fifo(const String& name , const String& filename, Context& context)
BufferManager::instance().delete_buffer_if_exists(name);
Buffer* buffer = new Buffer(name, Buffer::Flags::Fifo | Buffer::Flags::NoUndo);
auto watcher = new FDWatcher(fd, [buffer](FDWatcher& watcher) {
constexpr size_t buffer_size = 1024 * 16;
char data[buffer_size];
ssize_t count = read(watcher.fd(), data, buffer_size);
buffer->insert(buffer->end()-1, count > 0 ? String(data, data+count)
: "*** kak: fifo closed ***\n");
if (count <= 0)
{
kak_assert(buffer->flags() & Buffer::Flags::Fifo);
buffer->flags() &= ~Buffer::Flags::Fifo;
buffer->flags() &= ~Buffer::Flags::NoUndo;
close(watcher.fd());
delete &watcher;
}
});
buffer->hooks().add_hook("BufClose", "",
[buffer, watcher](const String&, const Context&) {
// Check if fifo is still alive, else watcher is already dead
if (buffer->flags() & Buffer::Flags::Fifo)
{
close(watcher->fd());
delete watcher;
}
});
return buffer;
return create_fifo_buffer(std::move(name), fd);
}
const PerArgumentCommandCompleter filename_completer({
@ -138,7 +111,7 @@ void edit(const ParametersParser& parser, Context& context)
buffer = new Buffer(name, Buffer::Flags::None);
}
else if (parser.has_option("fifo"))
buffer = open_fifo(name, parser.option_value("fifo"), context);
buffer = open_fifo(name, parser.option_value("fifo"));
else
buffer = open_or_create(name, context);
}

View File

@ -30,6 +30,10 @@
#include <locale>
#include <signal.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
using namespace Kakoune;
void run_unit_tests();
@ -192,6 +196,19 @@ void create_local_client(const String& init_command)
}
};
if (not isatty(1))
throw runtime_error("stdout is not a tty");
if (not isatty(0))
{
// move stdin to another fd, and restore tty as stdin
int fd = dup(0);
int tty = open("/dev/tty", O_RDONLY);
dup2(tty, 0);
close(tty);
create_fifo_buffer("*stdin*", fd);
}
UserInterface* ui = new LocalNCursesUI{};
static Client* client = ClientManager::instance().create_client(
std::unique_ptr<UserInterface>{ui}, get_env_vars(), init_command);