=================================================================== RCS file: /cvsrepo/anoncvs/cvs/src/usr.bin/tmux/cmd-load-buffer.c,v retrieving revision 1.15 retrieving revision 1.16 diff -u -r1.15 -r1.16 --- src/usr.bin/tmux/cmd-load-buffer.c 2010/12/30 23:16:18 1.15 +++ src/usr.bin/tmux/cmd-load-buffer.c 2011/01/04 00:42:46 1.16 @@ -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 @@ -35,41 +35,57 @@ 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; + 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 (strcmp(data->arg, "-") == 0) { + 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 @@ 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 @@ 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 @@ 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 @@ 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 @@ c->flags |= CLIENT_EXIT; psize = EVBUFFER_LENGTH(c->stdin_event->input); - if (psize == 0) + if (psize == 0 || (pdata = malloc(psize + 1)) == NULL) { + free(data); return; - - pdata = malloc(psize + 1); - if (pdata == NULL) - return; + } bufferevent_read(c->stdin_event, pdata, psize); pdata[psize] = '\0'; @@ -155,4 +169,6 @@ c->stderr_event->output, "no buffer %d\n", *buffer); bufferevent_enable(c->stderr_event, EV_WRITE); } + + free (data); }