4 * Copyright (c) 2009 Tiago Cunha <me@tiagocunha.org>
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>
27 * Copies a session paste buffer to another session.
30 int cmd_copy_buffer_parse(struct cmd
*, int, char **, char **);
31 int cmd_copy_buffer_exec(struct cmd
*, struct cmd_ctx
*);
32 void cmd_copy_buffer_free(struct cmd
*);
33 void cmd_copy_buffer_init(struct cmd
*, int);
34 size_t cmd_copy_buffer_print(struct cmd
*, char *, size_t);
36 struct cmd_copy_buffer_data
{
43 const struct cmd_entry cmd_copy_buffer_entry
= {
44 "copy-buffer", "copyb",
45 "[-a src-index] [-b dst-index] [-s src-session] [-t dst-session]",
48 cmd_copy_buffer_parse
,
56 cmd_copy_buffer_init(struct cmd
*self
, unused
int arg
)
58 struct cmd_copy_buffer_data
*data
;
60 self
->data
= data
= xmalloc(sizeof *data
);
61 data
->dst_session
= NULL
;
62 data
->src_session
= NULL
;
68 cmd_copy_buffer_parse(struct cmd
*self
, int argc
, char **argv
, char **cause
)
70 struct cmd_copy_buffer_data
*data
;
74 self
->entry
->init(self
, KEYC_NONE
);
77 while ((opt
= getopt(argc
, argv
, "a:b:s:t:")) != -1) {
80 if (data
->src_idx
== -1) {
81 n
= strtonum(optarg
, 0, INT_MAX
, &errstr
);
83 xasprintf(cause
, "buffer %s", errstr
);
90 if (data
->dst_idx
== -1) {
91 n
= strtonum(optarg
, 0, INT_MAX
, &errstr
);
93 xasprintf(cause
, "buffer %s", errstr
);
100 if (data
->src_session
== NULL
)
101 data
->src_session
= xstrdup(optarg
);
104 if (data
->dst_session
== NULL
)
105 data
->dst_session
= xstrdup(optarg
);
117 xasprintf(cause
, "usage: %s %s", self
->entry
->name
, self
->entry
->usage
);
120 self
->entry
->free(self
);
125 cmd_copy_buffer_exec(struct cmd
*self
, struct cmd_ctx
*ctx
)
127 struct cmd_copy_buffer_data
*data
= self
->data
;
128 struct paste_buffer
*pb
;
129 struct paste_stack
*dst_ps
, *src_ps
;
131 struct session
*dst_session
, *src_session
;
134 if ((dst_session
= cmd_find_session(ctx
, data
->dst_session
)) == NULL
||
135 (src_session
= cmd_find_session(ctx
, data
->src_session
)) == NULL
)
137 dst_ps
= &dst_session
->buffers
;
138 src_ps
= &src_session
->buffers
;
140 if (data
->src_idx
== -1) {
141 if ((pb
= paste_get_top(src_ps
)) == NULL
) {
142 ctx
->error(ctx
, "no buffers");
146 if ((pb
= paste_get_index(src_ps
, data
->src_idx
)) == NULL
) {
147 ctx
->error(ctx
, "no buffer %d", data
->src_idx
);
151 limit
= options_get_number(&dst_session
->options
, "buffer-limit");
153 pdata
= xmalloc(pb
->size
);
154 memcpy(pdata
, pb
->data
, pb
->size
);
156 if (data
->dst_idx
== -1)
157 paste_add(dst_ps
, pdata
, pb
->size
, limit
);
158 else if (paste_replace(dst_ps
, data
->dst_idx
, pdata
, pb
->size
) != 0) {
159 ctx
->error(ctx
, "no buffer %d", data
->dst_idx
);
168 cmd_copy_buffer_free(struct cmd
*self
)
170 struct cmd_copy_buffer_data
*data
= self
->data
;
172 if (data
->dst_session
!= NULL
)
173 xfree(data
->dst_session
);
174 if (data
->src_session
!= NULL
)
175 xfree(data
->src_session
);
180 cmd_copy_buffer_print(struct cmd
*self
, char *buf
, size_t len
)
182 struct cmd_copy_buffer_data
*data
= self
->data
;
185 off
+= xsnprintf(buf
, len
, "%s", self
->entry
->name
);
188 if (off
< len
&& data
->src_idx
!= -1) {
189 off
+= xsnprintf(buf
+ off
, len
- off
, " -a %d",
192 if (off
< len
&& data
->dst_idx
!= -1) {
193 off
+= xsnprintf(buf
+ off
, len
- off
, " -b %d",
196 if (off
< len
&& data
->src_session
!= NULL
) {
197 off
+= cmd_prarg(buf
+ off
, len
- off
, " -s ",
200 if (off
< len
&& data
->dst_session
!= NULL
) {
201 off
+= cmd_prarg(buf
+ off
, len
- off
, " -t ",