diff options
author | Nicholas Marriott <nicm@openbsd.org> | 2011-01-04 00:42:46 +0000 |
---|---|---|
committer | Nicholas Marriott <nicm@openbsd.org> | 2011-01-04 00:42:46 +0000 |
commit | 7502cb3adbb26a2f94445a35626e64041d6769f9 (patch) | |
tree | 6ae4f33f69dc0a470dbe905b5140216fb19b8f01 /cmd-load-buffer.c | |
parent | ac3b78a84178a308536a56ea114b0f6f8ce6fb47 (diff) | |
download | rtmux-7502cb3adbb26a2f94445a35626e64041d6769f9.tar.gz rtmux-7502cb3adbb26a2f94445a35626e64041d6769f9.tar.bz2 rtmux-7502cb3adbb26a2f94445a35626e64041d6769f9.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 'cmd-load-buffer.c')
-rw-r--r-- | cmd-load-buffer.c | 78 |
1 files changed, 47 insertions, 31 deletions
diff --git a/cmd-load-buffer.c b/cmd-load-buffer.c index 13413e4b..55a5cd14 100644 --- a/cmd-load-buffer.c +++ b/cmd-load-buffer.c @@ -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); } |