Annotation of src/usr.bin/tmux/cmd-load-buffer.c, Revision 1.1
1.1 ! nicm 1: /* $OpenBSD$ */
! 2:
! 3: /*
! 4: * Copyright (c) 2009 Tiago Cunha <me@tiagocunha.org>
! 5: *
! 6: * Permission to use, copy, modify, and distribute this software for any
! 7: * purpose with or without fee is hereby granted, provided that the above
! 8: * copyright notice and this permission notice appear in all copies.
! 9: *
! 10: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
! 11: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
! 12: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
! 13: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
! 14: * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
! 15: * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
! 16: * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
! 17: */
! 18:
! 19: #include <sys/types.h>
! 20: #include <sys/stat.h>
! 21:
! 22: #include <errno.h>
! 23: #include <stdlib.h>
! 24: #include <string.h>
! 25: #include <unistd.h>
! 26:
! 27: #include "tmux.h"
! 28:
! 29: /*
! 30: * Loads a session paste buffer from a file.
! 31: */
! 32:
! 33: int cmd_load_buffer_exec(struct cmd *, struct cmd_ctx *);
! 34:
! 35: const struct cmd_entry cmd_load_buffer_entry = {
! 36: "load-buffer", "loadb",
! 37: CMD_BUFFER_SESSION_USAGE " path",
! 38: CMD_ARG1,
! 39: cmd_buffer_init,
! 40: cmd_buffer_parse,
! 41: cmd_load_buffer_exec,
! 42: cmd_buffer_send,
! 43: cmd_buffer_recv,
! 44: cmd_buffer_free,
! 45: cmd_buffer_print
! 46: };
! 47:
! 48: int
! 49: cmd_load_buffer_exec(struct cmd *self, struct cmd_ctx *ctx)
! 50: {
! 51: struct cmd_buffer_data *data = self->data;
! 52: struct session *s;
! 53: struct stat statbuf;
! 54: FILE *f;
! 55: char *buf;
! 56: u_int limit;
! 57:
! 58: if ((s = cmd_find_session(ctx, data->target)) == NULL)
! 59: return (-1);
! 60:
! 61: if (stat(data->arg, &statbuf) < 0) {
! 62: ctx->error(ctx, "%s: %s", data->arg, strerror(errno));
! 63: return (-1);
! 64: }
! 65: if (!S_ISREG(statbuf.st_mode)) {
! 66: ctx->error(ctx, "%s: not a regular file", data->arg);
! 67: return (-1);
! 68: }
! 69:
! 70: if ((f = fopen(data->arg, "rb")) == NULL) {
! 71: ctx->error(ctx, "%s: %s", data->arg, strerror(errno));
! 72: return (-1);
! 73: }
! 74:
! 75: /*
! 76: * We don't want to die due to memory exhaustion, hence xmalloc can't
! 77: * be used here.
! 78: */
! 79: if ((buf = malloc(statbuf.st_size + 1)) == NULL) {
! 80: ctx->error(ctx, "malloc error: %s", strerror(errno));
! 81: return (-1);
! 82: }
! 83:
! 84: if (fread(buf, 1, statbuf.st_size, f) != (size_t) statbuf.st_size) {
! 85: ctx->error(ctx, "%s: fread error", data->arg);
! 86: xfree(buf);
! 87: fclose(f);
! 88: return (-1);
! 89: }
! 90:
! 91: buf[statbuf.st_size] = '\0';
! 92: fclose(f);
! 93:
! 94: limit = options_get_number(&s->options, "buffer-limit");
! 95: if (data->buffer == -1) {
! 96: paste_add(&s->buffers, buf, limit);
! 97: return (0);
! 98: }
! 99: if (paste_replace(&s->buffers, data->buffer, buf) != 0) {
! 100: ctx->error(ctx, "no buffer %d", data->buffer);
! 101: xfree(buf);
! 102: return (-1);
! 103: }
! 104:
! 105: return (0);
! 106: }