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. --- vis.c | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) (limited to 'vis.c') diff --git a/vis.c b/vis.c index 9054ccc..554b4f7 100644 --- a/vis.c +++ b/vis.c @@ -1416,6 +1416,8 @@ static bool cmd_write(const char *argv[]) { if (!argv[1]) argv[1] = text_filename_get(text); if (!argv[1]) { + if (text_fd_get(text) == STDIN_FILENO) + return text_write(text, STDOUT_FILENO) >= 0; editor_info_show(vis, "Filename expected"); return false; } @@ -1490,6 +1492,15 @@ static struct Screen { bool need_resize; } screen = { .need_resize = true }; +static void die(const char *errstr, ...) { + va_list ap; + endwin(); + va_start(ap, errstr); + vfprintf(stderr, errstr, ap); + va_end(ap); + exit(EXIT_FAILURE); +} + static void sigwinch_handler(int sig) { screen.need_resize = true; } @@ -1513,7 +1524,11 @@ static void setup() { setlocale(LC_CTYPE, ""); if (!getenv("ESCDELAY")) set_escdelay(50); - initscr(); + char *term = getenv("TERM"); + if (!term) + term = DEFAULT_TERM; + if (!newterm(term, stderr, stdin) == ERR) + die("Can not initialize terminal\n"); start_color(); raw(); noecho(); @@ -1574,15 +1589,6 @@ static Key getkey(void) { return key; } -static void die(const char *errstr, ...) { - va_list ap; - endwin(); - va_start(ap, errstr); - vfprintf(stderr, errstr, ap); - va_end(ap); - exit(EXIT_FAILURE); -} - static void mainloop() { struct timeval idle = { .tv_usec = 0 }, *timeout = NULL; Key key, key_prev, *key_mod = NULL; -- cgit v1.2.3