summaryrefslogtreecommitdiffstats
path: root/cmd-load-buffer.c
diff options
context:
space:
mode:
Diffstat (limited to 'cmd-load-buffer.c')
-rw-r--r--cmd-load-buffer.c80
1 files changed, 48 insertions, 32 deletions
diff --git a/cmd-load-buffer.c b/cmd-load-buffer.c
index 05125894..7984ff05 100644
--- a/cmd-load-buffer.c
+++ b/cmd-load-buffer.c
@@ -1,4 +1,4 @@
-/* $Id: cmd-load-buffer.c,v 1.19 2010-12-30 22:39:49 tcunha Exp $ */
+/* $Id: cmd-load-buffer.c,v 1.20 2011-01-07 14:45:34 tcunha Exp $ */
/*
* Copyright (c) 2009 Tiago Cunha <me@tiagocunha.org>
@@ -35,41 +35,57 @@ void cmd_load_buffer_callback(struct client *, void *);
const struct cmd_entry cmd_load_buffer_entry = {
"load-buffer", "loadb",
+ "b:", 1, 1,
CMD_BUFFER_USAGE " path",
- CMD_ARG1, "",
- cmd_buffer_init,
- cmd_buffer_parse,
- cmd_load_buffer_exec,
- cmd_buffer_free,
- cmd_buffer_print
+ 0,
+ NULL,
+ NULL,
+ cmd_load_buffer_exec
};
int
cmd_load_buffer_exec(struct cmd *self, struct cmd_ctx *ctx)
{
- struct cmd_buffer_data *data = self->data;
- struct client *c = ctx->cmdclient;
- FILE *f;
- char *pdata, *new_pdata;
- size_t psize;
- u_int limit;
- int ch;
-
- if (strcmp(data->arg, "-") == 0) {
+ struct args *args = self->args;
+ struct client *c = ctx->cmdclient;
+ FILE *f;
+ const char *path;
+ char *pdata, *new_pdata, *cause;
+ size_t psize;
+ u_int limit;
+ int ch, buffer;
+ int *buffer_ptr;
+
+ if (!args_has(args, 'b'))
+ buffer = -1;
+ else {
+ buffer = args_strtonum(args, 'b', 0, INT_MAX, &cause);
+ if (cause != NULL) {
+ ctx->error(ctx, "buffer %s", cause);
+ xfree(cause);
+ return (-1);
+ }
+ }
+
+ path = args->argv[0];
+ if (strcmp(path, "-") == 0) {
if (c == NULL) {
- ctx->error(ctx, "%s: can't read from stdin", data->arg);
+ ctx->error(ctx, "%s: can't read from stdin", path);
return (-1);
}
if (c->flags & CLIENT_TERMINAL) {
- ctx->error(ctx, "%s: stdin is a tty", data->arg);
+ ctx->error(ctx, "%s: stdin is a tty", path);
return (-1);
}
if (c->stdin_fd == -1) {
- ctx->error(ctx, "%s: can't read from stdin", data->arg);
+ ctx->error(ctx, "%s: can't read from stdin", path);
return (-1);
}
- c->stdin_data = &data->buffer;
+ buffer_ptr = xmalloc(sizeof *buffer_ptr);
+ *buffer_ptr = buffer;
+
+ c->stdin_data = buffer_ptr;
c->stdin_callback = cmd_load_buffer_callback;
c->references++;
@@ -77,8 +93,8 @@ cmd_load_buffer_exec(struct cmd *self, struct cmd_ctx *ctx)
return (1);
}
- if ((f = fopen(data->arg, "rb")) == NULL) {
- ctx->error(ctx, "%s: %s", data->arg, strerror(errno));
+ if ((f = fopen(path, "rb")) == NULL) {
+ ctx->error(ctx, "%s: %s", path, strerror(errno));
return (-1);
}
@@ -94,7 +110,7 @@ cmd_load_buffer_exec(struct cmd *self, struct cmd_ctx *ctx)
pdata[psize++] = ch;
}
if (ferror(f)) {
- ctx->error(ctx, "%s: read error", data->arg);
+ ctx->error(ctx, "%s: read error", path);
goto error;
}
if (pdata != NULL)
@@ -103,12 +119,12 @@ cmd_load_buffer_exec(struct cmd *self, struct cmd_ctx *ctx)
fclose(f);
limit = options_get_number(&global_options, "buffer-limit");
- if (data->buffer == -1) {
+ if (buffer == -1) {
paste_add(&global_buffers, pdata, psize, limit);
return (0);
}
- if (paste_replace(&global_buffers, data->buffer, pdata, psize) != 0) {
- ctx->error(ctx, "no buffer %d", data->buffer);
+ if (paste_replace(&global_buffers, buffer, pdata, psize) != 0) {
+ ctx->error(ctx, "no buffer %d", buffer);
return (-1);
}
@@ -125,10 +141,10 @@ error:
void
cmd_load_buffer_callback(struct client *c, void *data)
{
+ int *buffer = data;
char *pdata;
size_t psize;
u_int limit;
- int *buffer = data;
/*
* Event callback has already checked client is not dead and reduced
@@ -137,12 +153,10 @@ cmd_load_buffer_callback(struct client *c, void *data)
c->flags |= CLIENT_EXIT;
psize = EVBUFFER_LENGTH(c->stdin_event->input);
- if (psize == 0)
- return;
-
- pdata = malloc(psize + 1);
- if (pdata == NULL)
+ if (psize == 0 || (pdata = malloc(psize + 1)) == NULL) {
+ free(data);
return;
+ }
bufferevent_read(c->stdin_event, pdata, psize);
pdata[psize] = '\0';
@@ -155,4 +169,6 @@ cmd_load_buffer_callback(struct client *c, void *data)
c->stderr_event->output, "no buffer %d\n", *buffer);
bufferevent_enable(c->stderr_event, EV_WRITE);
}
+
+ free (data);
}