Skip to content

Commit

Permalink
vis: change vis_pipe API and cleanup related code
Browse files Browse the repository at this point in the history
  • Loading branch information
martanne committed Apr 3, 2016
1 parent ab2d1dd commit 217126c
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 66 deletions.
46 changes: 13 additions & 33 deletions register.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,8 @@
#include "util.h"
#include "register.h"

typedef struct {
Buffer *stdout;
Buffer *stderr;
} Clipboard;

static ssize_t read_stdout(void *context, char *data, size_t len) {
Buffer *buf = ((Clipboard*)context)->stdout;
buffer_append(buf, data, len);
return len;
}

static ssize_t read_stderr(void *context, char *data, size_t len) {
Buffer *buf = ((Clipboard*)context)->stderr;
buffer_append(buf, data, len);
static ssize_t read_buffer(void *context, char *data, size_t len) {
buffer_append(context, data, len);
return len;
}

Expand All @@ -37,20 +25,16 @@ const char *register_get(Vis *vis, Register *reg, size_t *len) {
return reg->buf.data;
case REGISTER_CLIPBOARD:
{
Buffer stderr;
buffer_init(&stderr);
Buffer buferr;
buffer_init(&buferr);
buffer_clear(&reg->buf);
Clipboard clipboard = {
.stdout = &reg->buf,
.stderr = &stderr,
};

int status = vis_pipe(vis, &clipboard,
&(Filerange){ .start = 0, .end = 0 },
int status = vis_pipe(vis, &(Filerange){ .start = 0, .end = 0 },
(const char*[]){ "vis-clipboard", "--paste", NULL },
read_stdout, read_stderr);
&reg->buf, read_buffer, &buferr, read_buffer);

if (status != 0)
vis_info_show(vis, "Command failed %s", stderr.len > 0 ? stderr.data : "");
vis_info_show(vis, "Command failed %s", buffer_content0(&buferr));
*len = reg->buf.len;
return reg->buf.data;
}
Expand Down Expand Up @@ -83,18 +67,14 @@ bool register_put_range(Vis *vis, Register *reg, Text *txt, Filerange *range) {
}
case REGISTER_CLIPBOARD:
{
Buffer stderr;
buffer_init(&stderr);
Clipboard clipboard = {
.stderr = &stderr,
};
Buffer buferr;
buffer_init(&buferr);

int status = vis_pipe(vis, &clipboard, range,
(const char*[]){ "vis-clipboard", "--copy", NULL },
NULL, read_stderr);
int status = vis_pipe(vis, range, (const char*[]){ "vis-clipboard", "--copy", NULL },
NULL, NULL, &buferr, read_buffer);

if (status != 0)
vis_info_show(vis, "Command failed %s", stderr.len > 0 ? stderr.data : "");
vis_info_show(vis, "Command failed %s", buffer_content0(&buferr));
return status == 0;
}
case REGISTER_BLACKHOLE:
Expand Down
45 changes: 20 additions & 25 deletions sam.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ typedef struct { /* used to keep context when dealing with external proce
Vis *vis; /* editor instance */
Text *txt; /* text into which received data will be inserted */
size_t pos; /* position at which to insert new data */
Buffer err; /* used to store everything the process writes to stderr */
} Filter;

struct Address {
Expand Down Expand Up @@ -1025,30 +1024,21 @@ static bool cmd_read(Vis *vis, Win *win, Command *cmd, const char *argv[], Curso
return ret;
}

static ssize_t read_stdout(void *context, char *data, size_t len) {
static ssize_t read_text(void *context, char *data, size_t len) {
Filter *filter = context;
text_insert(filter->txt, filter->pos, data, len);
filter->pos += len;
return len;
}

static ssize_t read_stderr(void *context, char *data, size_t len) {
Filter *filter = context;
buffer_append(&filter->err, data, len);
static ssize_t read_buffer(void *context, char *data, size_t len) {
buffer_append(context, data, len);
return len;
}

static bool cmd_filter(Vis *vis, Win *win, Command *cmd, const char *argv[], Cursor *cur, Filerange *range) {
Text *txt = win->file->text;

Filter filter = {
.vis = vis,
.txt = txt,
.pos = range->end,
};

buffer_init(&filter.err);

/* The general idea is the following:
*
* 1) take a snapshot
Expand All @@ -1063,7 +1053,16 @@ static bool cmd_filter(Vis *vis, Win *win, Command *cmd, const char *argv[], Cur

text_snapshot(txt);

int status = vis_pipe(vis, &filter, range, &argv[1], read_stdout, read_stderr);
Filter filter = {
.vis = vis,
.txt = txt,
.pos = range->end,
};

Buffer buferr;
buffer_init(&buferr);

int status = vis_pipe(vis, range, &argv[1], &filter, read_text, &buferr, read_buffer);

if (status == 0) {
text_delete_range(txt, range);
Expand All @@ -1081,12 +1080,10 @@ static bool cmd_filter(Vis *vis, Win *win, Command *cmd, const char *argv[], Cur
vis_info_show(vis, "Command cancelled");
else if (status == 0)
; //vis_info_show(vis, "Command succeded");
else if (filter.err.len > 0)
vis_info_show(vis, "Command failed: %s", filter.err.data);
else
vis_info_show(vis, "Command failed");
vis_info_show(vis, "Command failed %s", buffer_content0(&buferr));

buffer_release(&filter.err);
buffer_release(&buferr);

return !vis->cancel_filter && status == 0;
}
Expand All @@ -1104,10 +1101,10 @@ static bool cmd_pipein(Vis *vis, Win *win, Command *cmd, const char *argv[], Cur
}

static bool cmd_pipeout(Vis *vis, Win *win, Command *cmd, const char *argv[], Cursor *cur, Filerange *range) {
Filter filter;
buffer_init(&filter.err);
Buffer buferr;
buffer_init(&buferr);

int status = vis_pipe(vis, &filter, range, (const char*[]){ argv[1], NULL }, NULL, read_stderr);
int status = vis_pipe(vis, range, (const char*[]){ argv[1], NULL }, NULL, NULL, &buferr, read_buffer);

if (status == 0 && cur)
view_cursors_to(cur, range->start);
Expand All @@ -1116,12 +1113,10 @@ static bool cmd_pipeout(Vis *vis, Win *win, Command *cmd, const char *argv[], Cu
vis_info_show(vis, "Command cancelled");
else if (status == 0)
; //vis_info_show(vis, "Command succeded");
else if (filter.err.len > 0)
vis_info_show(vis, "Command failed: %s", filter.err.data);
else
vis_info_show(vis, "Command failed");
vis_info_show(vis, "Command failed %s", buffer_content0(&buferr));

buffer_release(&filter.err);
buffer_release(&buferr);

return !vis->cancel_filter && status == 0;
}
Expand Down
10 changes: 5 additions & 5 deletions vis.c
Original file line number Diff line number Diff line change
Expand Up @@ -1118,9 +1118,9 @@ Regex *vis_regex(Vis *vis, const char *pattern) {
return regex;
}

int vis_pipe(Vis *vis, void *context, Filerange *range, const char *argv[],
ssize_t (*read_stdout)(void *context, char *data, size_t len),
ssize_t (*read_stderr)(void *context, char *data, size_t len)) {
int vis_pipe(Vis *vis, Filerange *range, const char *argv[],
void *stdout_context, ssize_t (*read_stdout)(void *stdout_context, char *data, size_t len),
void *stderr_context, ssize_t (*read_stderr)(void *stderr_context, char *data, size_t len)) {

/* if an invalid range was given, stdin (i.e. key board input) is passed
* through the external command. */
Expand Down Expand Up @@ -1239,7 +1239,7 @@ int vis_pipe(Vis *vis, void *context, Filerange *range, const char *argv[],
ssize_t len = read(pout[0], buf, sizeof buf);
if (len > 0) {
if (read_stdout)
(*read_stdout)(context, buf, len);
(*read_stdout)(stdout_context, buf, len);
} else if (len == 0) {
close(pout[0]);
pout[0] = -1;
Expand All @@ -1255,7 +1255,7 @@ int vis_pipe(Vis *vis, void *context, Filerange *range, const char *argv[],
ssize_t len = read(perr[0], buf, sizeof buf);
if (len > 0) {
if (read_stderr)
(*read_stderr)(context, buf, len);
(*read_stderr)(stderr_context, buf, len);
} else if (len == 0) {
close(perr[0]);
perr[0] = -1;
Expand Down
6 changes: 3 additions & 3 deletions vis.h
Original file line number Diff line number Diff line change
Expand Up @@ -399,9 +399,9 @@ bool vis_prompt_cmd(Vis*, const char *cmd);
* if read_std{out,err} are non-NULL they will be called when output from
* the forked process is available.
*/
int vis_pipe(Vis *vis, void *context, Filerange *range, const char *argv[],
ssize_t (*read_stdout)(void *context, char *data, size_t len),
ssize_t (*read_stderr)(void *context, char *data, size_t len));
int vis_pipe(Vis *vis, Filerange *range, const char *argv[],
void *stdout_context, ssize_t (*read_stdout)(void *stdout_context, char *data, size_t len),
void *stderr_context, ssize_t (*read_stderr)(void *stderr_context, char *data, size_t len));

/* given the start of a key, returns a pointer to the start of the one immediately
* following as will be processed by the input system. skips over special keys
Expand Down

0 comments on commit 217126c

Please sign in to comment.