makefile: fixup bad merge
[tmux.git] / cmd-set-buffer.c
blob35e7295544d2fb06fda4a06fd2a0bed325a4cb86
1 /* $OpenBSD$ */
3 /*
4 * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
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.
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.
19 #include <sys/types.h>
21 #include <stdlib.h>
22 #include <string.h>
24 #include "tmux.h"
27 * Add, set, append to or delete a paste buffer.
30 static enum cmd_retval cmd_set_buffer_exec(struct cmd *, struct cmdq_item *);
32 const struct cmd_entry cmd_set_buffer_entry = {
33 .name = "set-buffer",
34 .alias = "setb",
36 .args = { "ab:t:n:w", 0, 1, NULL },
37 .usage = "[-aw] " CMD_BUFFER_USAGE " [-n new-buffer-name] "
38 CMD_TARGET_CLIENT_USAGE " data",
40 .flags = CMD_AFTERHOOK|CMD_CLIENT_TFLAG|CMD_CLIENT_CANFAIL,
41 .exec = cmd_set_buffer_exec
44 const struct cmd_entry cmd_delete_buffer_entry = {
45 .name = "delete-buffer",
46 .alias = "deleteb",
48 .args = { "b:", 0, 0, NULL },
49 .usage = CMD_BUFFER_USAGE,
51 .flags = CMD_AFTERHOOK,
52 .exec = cmd_set_buffer_exec
55 static enum cmd_retval
56 cmd_set_buffer_exec(struct cmd *self, struct cmdq_item *item)
58 struct args *args = cmd_get_args(self);
59 struct client *tc = cmdq_get_target_client(item);
60 struct paste_buffer *pb;
61 char *bufdata, *cause;
62 const char *bufname, *olddata;
63 size_t bufsize, newsize;
65 bufname = args_get(args, 'b');
66 if (bufname == NULL)
67 pb = NULL;
68 else
69 pb = paste_get_name(bufname);
71 if (cmd_get_entry(self) == &cmd_delete_buffer_entry) {
72 if (pb == NULL) {
73 if (bufname != NULL) {
74 cmdq_error(item, "unknown buffer: %s", bufname);
75 return (CMD_RETURN_ERROR);
77 pb = paste_get_top(&bufname);
79 if (pb == NULL) {
80 cmdq_error(item, "no buffer");
81 return (CMD_RETURN_ERROR);
83 paste_free(pb);
84 return (CMD_RETURN_NORMAL);
87 if (args_has(args, 'n')) {
88 if (pb == NULL) {
89 if (bufname != NULL) {
90 cmdq_error(item, "unknown buffer: %s", bufname);
91 return (CMD_RETURN_ERROR);
93 pb = paste_get_top(&bufname);
95 if (pb == NULL) {
96 cmdq_error(item, "no buffer");
97 return (CMD_RETURN_ERROR);
99 if (paste_rename(bufname, args_get(args, 'n'), &cause) != 0) {
100 cmdq_error(item, "%s", cause);
101 free(cause);
102 return (CMD_RETURN_ERROR);
104 return (CMD_RETURN_NORMAL);
107 if (args_count(args) != 1) {
108 cmdq_error(item, "no data specified");
109 return (CMD_RETURN_ERROR);
111 if ((newsize = strlen(args_string(args, 0))) == 0)
112 return (CMD_RETURN_NORMAL);
114 bufsize = 0;
115 bufdata = NULL;
117 if (args_has(args, 'a') && pb != NULL) {
118 olddata = paste_buffer_data(pb, &bufsize);
119 bufdata = xmalloc(bufsize);
120 memcpy(bufdata, olddata, bufsize);
123 bufdata = xrealloc(bufdata, bufsize + newsize);
124 memcpy(bufdata + bufsize, args_string(args, 0), newsize);
125 bufsize += newsize;
127 if (paste_set(bufdata, bufsize, bufname, &cause) != 0) {
128 cmdq_error(item, "%s", cause);
129 free(bufdata);
130 free(cause);
131 return (CMD_RETURN_ERROR);
133 if (args_has(args, 'w') && tc != NULL)
134 tty_set_selection(&tc->tty, "", bufdata, bufsize);
136 return (CMD_RETURN_NORMAL);