Annotation of src/usr.bin/tmux/cmd-load-buffer.c, Revision 1.63
1.63 ! nicm 1: /* $OpenBSD: cmd-load-buffer.c,v 1.62 2020/09/04 12:24:25 nicm Exp $ */
1.1 nicm 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:
1.12 nicm 19: #include <sys/types.h>
20:
1.1 nicm 21: #include <errno.h>
1.25 nicm 22: #include <fcntl.h>
1.11 nicm 23: #include <stdio.h>
1.1 nicm 24: #include <stdlib.h>
25: #include <string.h>
1.12 nicm 26: #include <unistd.h>
1.1 nicm 27:
28: #include "tmux.h"
29:
30: /*
1.18 nicm 31: * Loads a paste buffer from a file.
1.1 nicm 32: */
33:
1.46 nicm 34: static enum cmd_retval cmd_load_buffer_exec(struct cmd *, struct cmdq_item *);
1.43 nicm 35:
1.1 nicm 36: const struct cmd_entry cmd_load_buffer_entry = {
1.41 nicm 37: .name = "load-buffer",
38: .alias = "loadb",
39:
1.61 nicm 40: .args = { "b:t:w", 1, 1 },
41: .usage = CMD_BUFFER_USAGE " " CMD_TARGET_CLIENT_USAGE " path",
1.41 nicm 42:
1.61 nicm 43: .flags = CMD_AFTERHOOK|CMD_CLIENT_TFLAG|CMD_CLIENT_CANFAIL,
1.41 nicm 44: .exec = cmd_load_buffer_exec
1.1 nicm 45: };
46:
1.45 nicm 47: struct cmd_load_buffer_data {
1.61 nicm 48: struct client *client;
1.46 nicm 49: struct cmdq_item *item;
1.57 nicm 50: char *name;
1.45 nicm 51: };
52:
1.57 nicm 53: static void
54: cmd_load_buffer_done(__unused struct client *c, const char *path, int error,
55: int closed, struct evbuffer *buffer, void *data)
56: {
57: struct cmd_load_buffer_data *cdata = data;
1.61 nicm 58: struct client *tc = cdata->client;
1.57 nicm 59: struct cmdq_item *item = cdata->item;
60: void *bdata = EVBUFFER_DATA(buffer);
61: size_t bsize = EVBUFFER_LENGTH(buffer);
62: void *copy;
63: char *cause;
64:
65: if (!closed)
66: return;
67:
68: if (error != 0)
69: cmdq_error(item, "%s: %s", path, strerror(error));
70: else if (bsize != 0) {
71: copy = xmalloc(bsize);
72: memcpy(copy, bdata, bsize);
73: if (paste_set(copy, bsize, cdata->name, &cause) != 0) {
74: cmdq_error(item, "%s", cause);
75: free(cause);
76: free(copy);
1.61 nicm 77: } else if (tc != NULL &&
78: tc->session != NULL &&
79: (~tc->flags & CLIENT_DEAD))
80: tty_set_selection(&tc->tty, copy, bsize);
81: if (tc != NULL)
82: server_client_unref(tc);
1.57 nicm 83: }
84: cmdq_continue(item);
85:
86: free(cdata->name);
87: free(cdata);
88: }
89:
1.43 nicm 90: static enum cmd_retval
1.46 nicm 91: cmd_load_buffer_exec(struct cmd *self, struct cmdq_item *item)
1.1 nicm 92: {
1.58 nicm 93: struct args *args = cmd_get_args(self);
1.61 nicm 94: struct client *tc = cmdq_get_target_client(item);
1.45 nicm 95: struct cmd_load_buffer_data *cdata;
1.57 nicm 96: const char *bufname = args_get(args, 'b');
97: char *path;
98:
1.62 nicm 99: cdata = xcalloc(1, sizeof *cdata);
1.57 nicm 100: cdata->item = item;
101: if (bufname != NULL)
102: cdata->name = xstrdup(bufname);
1.61 nicm 103: if (args_has(args, 'w') && tc != NULL) {
104: cdata->client = tc;
105: cdata->client->references++;
106: }
1.1 nicm 107:
1.63 ! nicm 108: path = format_single_from_target(item, args_string(args, 0));
1.59 nicm 109: file_read(cmdq_get_client(item), path, cmd_load_buffer_done, cdata);
1.53 nicm 110: free(path);
111:
1.57 nicm 112: return (CMD_RETURN_WAIT);
1.1 nicm 113: }