Annotation of src/usr.bin/tmux/cmd-copy-buffer.c, Revision 1.2
1.2 ! nicm 1: /* $OpenBSD: cmd-copy-buffer.c,v 1.1 2009/06/01 22:58:49 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:
19: #include <stdlib.h>
20:
21: #include "tmux.h"
22:
23: /*
24: * Copies a session paste buffer to another session.
25: */
26:
27: int cmd_copy_buffer_parse(struct cmd *, int, char **, char **);
28: int cmd_copy_buffer_exec(struct cmd *, struct cmd_ctx *);
29: void cmd_copy_buffer_send(struct cmd *, struct buffer *);
30: void cmd_copy_buffer_recv(struct cmd *, struct buffer *);
31: void cmd_copy_buffer_free(struct cmd *);
32: void cmd_copy_buffer_init(struct cmd *, int);
33: size_t cmd_copy_buffer_print(struct cmd *, char *, size_t);
34:
35: struct cmd_copy_buffer_data {
36: char *dst_session;
37: char *src_session;
38: int dst_idx;
39: int src_idx;
40: };
41:
42: const struct cmd_entry cmd_copy_buffer_entry = {
43: "copy-buffer", "copyb",
44: "[-a src-index] [-b dst-index] [-s src-session] [-t dst-session]",
1.2 ! nicm 45: 0, 0,
1.1 nicm 46: cmd_copy_buffer_init,
47: cmd_copy_buffer_parse,
48: cmd_copy_buffer_exec,
49: cmd_copy_buffer_send,
50: cmd_copy_buffer_recv,
51: cmd_copy_buffer_free,
52: cmd_copy_buffer_print
53: };
54:
55: void
56: cmd_copy_buffer_init(struct cmd *self, unused int arg)
57: {
58: struct cmd_copy_buffer_data *data;
59:
60: self->data = data = xmalloc(sizeof *data);
61: data->dst_session = NULL;
62: data->src_session = NULL;
63: data->dst_idx = -1;
64: data->src_idx = -1;
65: }
66:
67: int
68: cmd_copy_buffer_parse(struct cmd *self, int argc, char **argv, char **cause)
69: {
70: struct cmd_copy_buffer_data *data;
71: const char *errstr;
72: int n, opt;
73:
74: self->entry->init(self, 0);
75: data = self->data;
76:
77: while ((opt = getopt(argc, argv, "a:b:s:t:")) != -1) {
78: switch (opt) {
79: case 'a':
80: if (data->src_idx == -1) {
81: n = strtonum(optarg, 0, INT_MAX, &errstr);
82: if (errstr != NULL) {
83: xasprintf(cause, "buffer %s", errstr);
84: goto error;
85: }
86: data->src_idx = n;
87: }
88: break;
89: case 'b':
90: if (data->dst_idx == -1) {
91: n = strtonum(optarg, 0, INT_MAX, &errstr);
92: if (errstr != NULL) {
93: xasprintf(cause, "buffer %s", errstr);
94: goto error;
95: }
96: data->dst_idx = n;
97: }
98: break;
99: case 's':
100: if (data->src_session == NULL)
101: data->src_session = xstrdup(optarg);
102: break;
103: case 't':
104: if (data->dst_session == NULL)
105: data->dst_session = xstrdup(optarg);
106: break;
107: default:
108: goto usage;
109: }
110: }
111: argc -= optind;
112: argv += optind;
113:
114: return (0);
115:
116: usage:
117: xasprintf(cause, "usage: %s %s", self->entry->name, self->entry->usage);
118:
119: error:
120: self->entry->free(self);
121: return (-1);
122: }
123:
124: int
125: cmd_copy_buffer_exec(struct cmd *self, struct cmd_ctx *ctx)
126: {
127: struct cmd_copy_buffer_data *data = self->data;
128: struct paste_buffer *pb;
129: struct session *dst_session, *src_session;
130: u_int limit;
131:
132: if ((dst_session = cmd_find_session(ctx, data->dst_session)) == NULL ||
133: (src_session = cmd_find_session(ctx, data->src_session)) == NULL)
134: return (-1);
135:
136: if (data->src_idx == -1) {
137: if ((pb = paste_get_top(&src_session->buffers)) == NULL) {
138: ctx->error(ctx, "no buffers");
139: return (-1);
140: }
141: } else {
142: if ((pb = paste_get_index(&src_session->buffers,
143: data->src_idx)) == NULL) {
144: ctx->error(ctx, "no buffer %d", data->src_idx);
145: return (-1);
146: }
147: }
148:
149: limit = options_get_number(&dst_session->options, "buffer-limit");
150: if (data->dst_idx == -1) {
151: paste_add(&dst_session->buffers, xstrdup(pb->data), limit);
152: return (0);
153: }
154: if (paste_replace(&dst_session->buffers, data->dst_idx,
155: xstrdup(pb->data)) != 0) {
156: ctx->error(ctx, "no buffer %d", data->dst_idx);
157: return (-1);
158: }
159:
160: return (0);
161: }
162:
163: void
164: cmd_copy_buffer_send(struct cmd *self, struct buffer *b)
165: {
166: struct cmd_copy_buffer_data *data = self->data;
167:
168: buffer_write(b, data, sizeof *data);
169: cmd_send_string(b, data->dst_session);
170: cmd_send_string(b, data->src_session);
171: }
172:
173: void
174: cmd_copy_buffer_recv(struct cmd *self, struct buffer *b)
175: {
176: struct cmd_copy_buffer_data *data;
177:
178: self->data = data = xmalloc(sizeof *data);
179: buffer_read(b, data, sizeof *data);
180: data->dst_session = cmd_recv_string(b);
181: data->src_session = cmd_recv_string(b);
182: }
183:
184: void
185: cmd_copy_buffer_free(struct cmd *self)
186: {
187: struct cmd_copy_buffer_data *data = self->data;
188:
189: if (data->dst_session != NULL)
190: xfree(data->dst_session);
191: if (data->src_session != NULL)
192: xfree(data->src_session);
193: xfree(data);
194: }
195:
196: size_t
197: cmd_copy_buffer_print(struct cmd *self, char *buf, size_t len)
198: {
199: struct cmd_copy_buffer_data *data = self->data;
200: size_t off = 0;
201:
202: off += xsnprintf(buf, len, "%s", self->entry->name);
203: if (data == NULL)
204: return (off);
205: if (off < len && data->src_idx != -1) {
206: off += xsnprintf(buf + off, len - off, " -a %d",
207: data->src_idx);
208: }
209: if (off < len && data->dst_idx != -1) {
210: off += xsnprintf(buf + off, len - off, " -b %d",
211: data->dst_idx);
212: }
213: if (off < len && data->src_session != NULL) {
214: off += cmd_prarg(buf + off, len - off, " -s ",
215: data->src_session);
216: }
217: if (off < len && data->dst_session != NULL) {
218: off += cmd_prarg(buf + off, len - off, " -t ",
219: data->dst_session);
220: }
221: return (off);
222: }