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