aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarc André Tanner <mat@brain-dump.org>2014-10-17 12:51:13 +0200
committerMarc André Tanner <mat@brain-dump.org>2014-10-17 14:15:31 +0200
commitbd354843c2751ec6c7b5b85ef08f9145f6232e6e (patch)
tree384528ba3c384160b5a07159f7e49fe6efdd0f01
parent9c4999fc9ee4a071bba068e68d690ba637ec28f5 (diff)
downloadvis-bd354843c2751ec6c7b5b85ef08f9145f6232e6e.tar.gz
vis-bd354843c2751ec6c7b5b85ef08f9145f6232e6e.tar.xz
Read stdin when given - as filename
-rw-r--r--editor.c11
-rw-r--r--editor.h1
-rw-r--r--text.c12
-rw-r--r--text.h1
-rw-r--r--vis.c23
5 files changed, 44 insertions, 4 deletions
diff --git a/editor.c b/editor.c
index 4af96d9..f0123c9 100644
--- a/editor.c
+++ b/editor.c
@@ -365,6 +365,17 @@ bool editor_window_new(Editor *ed, const char *filename) {
return true;
}
+bool editor_window_new_fd(Editor *ed, int fd) {
+ Text *txt = text_load_fd(fd);
+ if (!txt)
+ return false;
+ EditorWin *win = editor_window_new_text(ed, txt);
+ if (!win)
+ return false;
+ editor_draw(ed);
+ return true;
+}
+
static void editor_window_detach(Editor *ed, EditorWin *win) {
if (win->prev)
win->prev->next = win->next;
diff --git a/editor.h b/editor.h
index e42e99c..44c2669 100644
--- a/editor.h
+++ b/editor.h
@@ -137,6 +137,7 @@ void editor_syntax_unload(Editor*);
* in another window, share the underlying text that is changes will be
* visible in both windows */
bool editor_window_new(Editor*, const char *filename);
+bool editor_window_new_fd(Editor*, int fd);
/* reload the file currently displayed in the window from disk */
bool editor_window_reload(EditorWin*);
void editor_window_close(EditorWin*);
diff --git a/text.c b/text.c
index 89e3512..7757e70 100644
--- a/text.c
+++ b/text.c
@@ -692,6 +692,18 @@ out:
return NULL;
}
+Text *text_load_fd(int fd) {
+ Text *txt = text_load(NULL);
+ if (!txt)
+ return NULL;
+ char buf[1024];
+ for (ssize_t len = 0; (len = read(fd, buf, sizeof buf)) > 0;)
+ text_insert(txt, text_size(txt), buf, len);
+ text_snapshot(txt);
+ txt->fd = fd;
+ return txt;
+}
+
static void print_piece(Piece *p) {
fprintf(stderr, "index: %d\tnext: %d\tprev: %d\t len: %d\t data: %p\n", p->index,
p->next ? p->next->index : -1,
diff --git a/text.h b/text.h
index 52a5fad..34e350d 100644
--- a/text.h
+++ b/text.h
@@ -33,6 +33,7 @@ typedef struct {
text_iterator_next(&it))
Text *text_load(const char *file);
+Text *text_load_fd(int fd);
/* the filename from which this text was loaded or first saved to */
const char *text_filename_get(Text*);
/* associate a filename with the yet unnamed buffer */
diff --git a/vis.c b/vis.c
index 087ff08..53a7dbd 100644
--- a/vis.c
+++ b/vis.c
@@ -1671,12 +1671,27 @@ int main(int argc, char *argv[]) {
die("Could not load syntax highlighting definitions\n");
editor_statusbar_set(vis, config->statusbar);
- for (int i = 1; i < MAX(argc, 2); i++) {
- const char *file = i < argc ? argv[i] : NULL;
- if (!editor_window_new(vis, file))
- die("Could not load `%s': %s\n", file, strerror(errno));
+ 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);
+ break;
+ }
+ } else if (!editor_window_new(vis, argv[i])) {
+ die("Can not load `%s': %s\n", argv[i], strerror(errno));
+ }
}
+ if (!vis->windows && !editor_window_new(vis, NULL))
+ die("Can not create empty buffer\n");
+
mainloop();
editor_free(vis);
endwin();