diff options
| author | Marc André Tanner <mat@brain-dump.org> | 2014-10-21 16:50:53 +0200 |
|---|---|---|
| committer | Marc André Tanner <mat@brain-dump.org> | 2014-10-21 16:50:53 +0200 |
| commit | ae1c161dcf449da454f0a2dfd7681d95a2a4ff09 (patch) | |
| tree | 179de92b299302472956931c3e3caa5d1143d704 | |
| parent | b1b73eac95a08d5284d21b43e2d4c1ec9cecd5da (diff) | |
| download | vis-ae1c161dcf449da454f0a2dfd7681d95a2a4ff09.tar.gz vis-ae1c161dcf449da454f0a2dfd7681d95a2a4ff09.tar.xz | |
Read text to edit from stdin if it isn't a terminal
This allows usage as a filter without specifying "-" as file name.
| -rw-r--r-- | vis.c | 25 |
1 files changed, 15 insertions, 10 deletions
@@ -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); |
