From ae1c161dcf449da454f0a2dfd7681d95a2a4ff09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Andr=C3=A9=20Tanner?= Date: Tue, 21 Oct 2014 16:50:53 +0200 Subject: Read text to edit from stdin if it isn't a terminal This allows usage as a filter without specifying "-" as file name. --- vis.c | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) (limited to 'vis.c') diff --git a/vis.c b/vis.c index 6c44b12..30cf126 100644 --- a/vis.c +++ b/vis.c @@ -1685,14 +1685,8 @@ int main(int argc, char *argv[]) { for (int i = 1; i < argc; i++) { if (argv[i][0] == '-') { switch (argv[i][1]) { - case '\0': - if (!editor_window_new_fd(vis, STDIN_FILENO)) - die("Can not read from stdin\n"); - int fd = open("/dev/tty", O_RDONLY); - if (fd == -1) - die("Can not reopen stdin\n"); - dup2(fd, STDIN_FILENO); - close(fd); + /* handle command line arguments */ + default: break; } } else if (!editor_window_new(vis, argv[i])) { @@ -1700,8 +1694,19 @@ int main(int argc, char *argv[]) { } } - if (!vis->windows && !editor_window_new(vis, NULL)) - die("Can not create empty buffer\n"); + if (!vis->windows) { + if (!strcmp(argv[argc-1], "-") || !isatty(STDIN_FILENO)) { + if (!editor_window_new_fd(vis, STDIN_FILENO)) + die("Can not read from stdin\n"); + int fd = open("/dev/tty", O_RDONLY); + if (fd == -1) + die("Can not reopen stdin\n"); + dup2(fd, STDIN_FILENO); + close(fd); + } else if (!editor_window_new(vis, NULL)) { + die("Can not create empty buffer\n"); + } + } mainloop(); editor_free(vis); -- cgit v1.2.3