summaryrefslogtreecommitdiffstats
path: root/usr.bin/tmux/cmd-load-buffer.c
diff options
context:
space:
mode:
authornicm <nicm@openbsd.org>2011-01-04 00:42:46 +0000
committernicm <nicm@openbsd.org>2011-01-04 00:42:46 +0000
commitca7befcc5189fa88f5a692b06394473e8d9ada88 (patch)
treed067816fbc088f99c228eb4c8bc0954e84c0eab1 /usr.bin/tmux/cmd-load-buffer.c
parentPartial cleanup of argument count validation in mdoc(7): (diff)
downloadwireguard-openbsd-ca7befcc5189fa88f5a692b06394473e8d9ada88.tar.xz
wireguard-openbsd-ca7befcc5189fa88f5a692b06394473e8d9ada88.zip
Clean up and simplify tmux command argument parsing.
Originally, tmux commands were parsed in the client process into a struct with the command data which was then serialised and sent to the server to be executed. The parsing was later moved into the server (an argv was sent from the client), but the parse step and intermediate struct was kept. This change removes that struct and the separate parse step. Argument parsing and printing is now common to all commands (in arguments.c) with each command left with just an optional check function (to validate the arguments at parse time), the exec function and a function to set up any key bindings (renamed from the old init function). This is overall more simple and consistent. There should be no changes to any commands behaviour or syntax although as this touches every command please watch for any unexpected changes.
Diffstat (limited to 'usr.bin/tmux/cmd-load-buffer.c')
-rw-r--r--usr.bin/tmux/cmd-load-buffer.c80
1 files changed, 48 insertions, 32 deletions
diff --git a/usr.bin/tmux/cmd-load-buffer.c b/usr.bin/tmux/cmd-load-buffer.c
index b48e1264a3f..570c8b53997 100644
--- a/usr.bin/tmux/cmd-load-buffer.c
+++ b/usr.bin/tmux/cmd-load-buffer.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: cmd-load-buffer.c,v 1.15 2010/12/30 23:16:18 nicm Exp $ */
+/* $OpenBSD: cmd-load-buffer.c,v 1.16 2011/01/04 00:42:46 nicm 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);
}