From e9180cf83ade29192d8407337f614e5a024017a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Andr=C3=A9=20Tanner?= Date: Fri, 17 Oct 2014 15:43:48 +0200 Subject: Make editor usable as a filter: echo foo | vis - | cat The terminal output is by default redirected to stderr, making stdout available for communications purposes. If a file is "opened" from stdin (i.e. vis is given '-' as argument) and a subsequent write without a filename is performed as in ":wq" the output is written to stdout. --- text.c | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) (limited to 'text.c') diff --git a/text.c b/text.c index 7757e70..235c98c 100644 --- a/text.c +++ b/text.c @@ -645,12 +645,36 @@ err: return -1; } +ssize_t text_write(Text *txt, int fd) { + ssize_t len = 0; + text_iterate(txt, it, 0) { + size_t plen = it.end - it.start, poff = 0; + while (plen > 0) { + ssize_t res = write(fd, it.start + poff, plen); + if (res < 0) { + if (errno == EAGAIN || errno == EINTR) + continue; + return -1; + } + if (res == 0) + break; + poff += res; + plen -= res; + } + len += plen; + } + txt->saved_action = txt->undo; + text_snapshot(txt); + return len; +} + /* load the given file as starting point for further editing operations. * to start with an empty document, pass NULL as filename. */ Text *text_load(const char *filename) { Text *txt = calloc(1, sizeof(Text)); if (!txt) return NULL; + txt->fd = -1; txt->begin.index = 1; txt->end.index = 2; txt->piece_count = 2; @@ -686,8 +710,10 @@ Text *text_load(const char *filename) { } return txt; out: - if (txt->fd > 2) + if (txt->fd > 2) { close(txt->fd); + txt->fd = -1; + } text_free(txt); return NULL; } @@ -1124,6 +1150,10 @@ void text_mark_clear_all(Text *txt) { text_mark_clear(txt, mark); } +int text_fd_get(Text *txt) { + return txt->fd; +} + const char *text_filename_get(Text *txt) { return txt->filename; } -- cgit v1.2.3