aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarc André Tanner <mat@brain-dump.org>2015-07-06 19:58:57 +0200
committerMarc André Tanner <mat@brain-dump.org>2015-07-06 19:58:57 +0200
commit071b5f3c94c4b157a153586b087229f9e6b465d3 (patch)
treeacd7a2e5c7ef332a004d6d825a2a26e197ae0ee1
parent913d767475110bd73ce4fe772ff2444004b8f359 (diff)
downloadvis-071b5f3c94c4b157a153586b087229f9e6b465d3.tar.gz
vis-071b5f3c94c4b157a153586b087229f9e6b465d3.tar.xz
text: remove text_load_fd and text_fd_get
-rw-r--r--editor.c17
-rw-r--r--editor.h2
-rw-r--r--text.c39
-rw-r--r--text.h4
-rw-r--r--vis.c35
5 files changed, 33 insertions, 64 deletions
diff --git a/editor.c b/editor.c
index 11704e9..87072a3 100644
--- a/editor.c
+++ b/editor.c
@@ -346,23 +346,6 @@ bool editor_window_new(Editor *ed, const char *filename) {
return true;
}
-bool editor_window_new_fd(Editor *ed, int fd) {
- Text *text = text_load_fd(fd);
- if (!text)
- return false;
- File *file = file_new_text(ed, text);
- if (!file) {
- file_free(ed, file);
- return false;
- }
- Win *win = window_new_file(ed, file);
- if (!win) {
- file_free(ed, file);
- return false;
- }
- return true;
-}
-
void editor_window_close(Win *win) {
Editor *ed = win->editor;
file_free(ed, win->file);
diff --git a/editor.h b/editor.h
index 5a288b6..5192d31 100644
--- a/editor.h
+++ b/editor.h
@@ -194,6 +194,7 @@ enum Mark {
struct File {
Text *text;
const char *name;
+ bool stdin;
int refcount;
Mark marks[MARK_LAST];
File *next, *prev;
@@ -281,7 +282,6 @@ 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(Win*);
void editor_window_close(Win*);
diff --git a/text.c b/text.c
index d131adc..764d7cb 100644
--- a/text.c
+++ b/text.c
@@ -132,7 +132,6 @@ struct Text {
Action *saved_action; /* the last action at the time of the save operation */
size_t size; /* current file content size in bytes */
struct stat info; /* stat as proped on load time */
- int fd; /* the file descriptor of the original mmap-ed data */
LineCache lines; /* mapping between absolute pos in bytes and logical line breaks */
enum TextNewLine newlines; /* which type of new lines does the file use */
};
@@ -905,7 +904,7 @@ bool text_save(Text *txt, const char *filename) {
bool text_range_save(Text *txt, Filerange *range, const char *filename) {
struct stat meta;
int fd = -1, newfd = -1;
- if (text_range_save_atomic(txt, range, filename))
+ if (!filename || text_range_save_atomic(txt, range, filename))
goto ok;
if ((fd = open(filename, O_CREAT|O_WRONLY, S_IRUSR|S_IWUSR)) == -1)
goto err;
@@ -997,7 +996,7 @@ Text *text_load(const char *filename) {
Text *txt = calloc(1, sizeof(Text));
if (!txt)
return NULL;
- txt->fd = -1;
+ int fd = -1;
txt->begin.index = 1;
txt->end.index = 2;
txt->piece_count = 2;
@@ -1005,10 +1004,9 @@ Text *text_load(const char *filename) {
piece_init(&txt->end, &txt->begin, NULL, NULL, 0);
lineno_cache_invalidate(&txt->lines);
if (filename) {
- txt->fd = open(filename, O_RDONLY);
- if (txt->fd == -1)
+ if ((fd = open(filename, O_RDONLY)) == -1)
goto out;
- if (fstat(txt->fd, &txt->info) == -1)
+ if (fstat(fd, &txt->info) == -1)
goto out;
if (!S_ISREG(txt->info.st_mode)) {
errno = S_ISDIR(txt->info.st_mode) ? EISDIR : ENOTSUP;
@@ -1017,9 +1015,9 @@ Text *text_load(const char *filename) {
// XXX: use lseek(fd, 0, SEEK_END); instead?
size_t size = txt->info.st_size;
if (size < BUFFER_MMAP_SIZE)
- txt->buf = buffer_read(txt, size, txt->fd);
+ txt->buf = buffer_read(txt, size, fd);
else
- txt->buf = buffer_mmap(txt, size, txt->fd, 0);
+ txt->buf = buffer_mmap(txt, size, fd, 0);
if (!txt->buf)
goto out;
Piece *p = piece_alloc(txt);
@@ -1035,29 +1033,16 @@ Text *text_load(const char *filename) {
text_snapshot(txt);
txt->saved_action = txt->history;
+ if (fd != -1)
+ close(fd);
return txt;
out:
- if (txt->fd > 2) {
- close(txt->fd);
- txt->fd = -1;
- }
+ if (fd != -1)
+ close(fd);
text_free(txt);
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);
- txt->saved_action = txt->history;
- text_snapshot(txt);
- txt->fd = fd;
- return txt;
-}
-
/* A delete operation can either start/stop midway through a piece or at
* a boundry. In the former case a new piece is created to represent the
* remaining text before/after the modification point.
@@ -1466,10 +1451,6 @@ size_t text_history_get(Text *txt, size_t index) {
return EPOS;
}
-int text_fd_get(Text *txt) {
- return txt->fd;
-}
-
Regex *text_regex_new(void) {
Regex *r = calloc(1, sizeof(Regex));
if (!r)
diff --git a/text.h b/text.h
index 2c847d1..725fa26 100644
--- a/text.h
+++ b/text.h
@@ -35,10 +35,6 @@ typedef struct {
text_iterator_next(&it))
Text *text_load(const char *file);
-Text *text_load_fd(int fd);
-/* return the fd from which this text was loaded or -1 if it was
- * loaded from a filename */
-int text_fd_get(Text*);
bool text_insert(Text*, size_t pos, const char *data, size_t len);
bool text_delete(Text*, size_t pos, size_t len);
void text_snapshot(Text*);
diff --git a/vis.c b/vis.c
index aa40fee..076f0c7 100644
--- a/vis.c
+++ b/vis.c
@@ -1665,9 +1665,17 @@ static bool cmd_write(Filerange *range, enum CmdOpt opt, const char *argv[]) {
if (!argv[1])
argv[1] = file->name;
if (!argv[1]) {
- if (text_fd_get(text) == STDIN_FILENO) {
- if (strchr(argv[0], 'q'))
- return text_range_write(text, range, STDOUT_FILENO) >= 0;
+ if (file->stdin) {
+ if (strchr(argv[0], 'q')) {
+ ssize_t written = text_range_write(text, range, STDOUT_FILENO);
+ if (written == -1 || (size_t)written != text_range_size(range)) {
+ editor_info_show(vis, "Can not write to stdout");
+ return false;
+ }
+ /* make sure the file is marked as saved i.e. not modified */
+ text_range_save(text, range, NULL);
+ return true;
+ }
editor_info_show(vis, "No filename given, use 'wq' to write to stdout");
return false;
}
@@ -2169,15 +2177,6 @@ static bool vis_window_new(const char *file) {
return true;
}
-static bool vis_window_new_fd(int fd) {
- if (!editor_window_new_fd(vis, fd))
- return false;
- Syntax *s = view_syntax_get(vis->win->view);
- if (s)
- settings_apply(s->settings);
- return true;
-}
-
static bool vis_window_split(Win *win) {
if (!editor_window_split(win))
return false;
@@ -2380,8 +2379,18 @@ int main(int argc, char *argv[]) {
if (!vis->windows) {
if (!strcmp(argv[argc-1], "-") || !isatty(STDIN_FILENO)) {
- if (!vis_window_new_fd(STDIN_FILENO))
+ if (!vis_window_new(NULL))
+ die("Can not create empty buffer\n");
+ ssize_t len = 0;
+ char buf[PIPE_BUF];
+ File *file = vis->win->file;
+ Text *txt = file->text;
+ file->stdin = true;
+ while ((len = read(STDIN_FILENO, buf, sizeof buf)) > 0)
+ text_insert(txt, text_size(txt), buf, len);
+ if (len == -1)
die("Can not read from stdin\n");
+ text_snapshot(txt);
int fd = open("/dev/tty", O_RDONLY);
if (fd == -1)
die("Can not reopen stdin\n");