Annotation of src/usr.bin/tmux/cmd-paste-buffer.c, Revision 1.12
1.12 ! nicm 1: /* $OpenBSD: cmd-paste-buffer.c,v 1.11 2010/03/22 19:13:28 nicm Exp $ */
1.1 nicm 2:
3: /*
4: * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
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 <string.h>
22:
23: #include "tmux.h"
24:
25: /*
26: * Paste paste buffer if present.
27: */
28:
29: int cmd_paste_buffer_exec(struct cmd *, struct cmd_ctx *);
1.7 nicm 30: void cmd_paste_buffer_lf2cr(struct window_pane *, const char *, size_t);
1.1 nicm 31:
32: const struct cmd_entry cmd_paste_buffer_entry = {
33: "paste-buffer", "pasteb",
1.11 nicm 34: "[-dr] " CMD_BUFFER_PANE_USAGE,
1.8 nicm 35: 0, "dr",
1.1 nicm 36: cmd_buffer_init,
37: cmd_buffer_parse,
38: cmd_paste_buffer_exec,
39: cmd_buffer_free,
40: cmd_buffer_print
41: };
42:
43: int
44: cmd_paste_buffer_exec(struct cmd *self, struct cmd_ctx *ctx)
45: {
46: struct cmd_buffer_data *data = self->data;
1.6 nicm 47: struct window_pane *wp;
1.1 nicm 48: struct session *s;
49: struct paste_buffer *pb;
50:
1.12 ! nicm 51: if (cmd_find_pane(ctx, data->target, &s, &wp) == NULL)
1.1 nicm 52: return (-1);
53:
54: if (data->buffer == -1)
55: pb = paste_get_top(&s->buffers);
56: else {
57: if ((pb = paste_get_index(&s->buffers, data->buffer)) == NULL) {
58: ctx->error(ctx, "no buffer %d", data->buffer);
59: return (-1);
60: }
61: }
62:
1.9 nicm 63: if (pb != NULL) {
1.3 nicm 64: /* -r means raw data without LF->CR conversion. */
1.8 nicm 65: if (cmd_check_flag(data->chflags, 'r'))
1.7 nicm 66: bufferevent_write(wp->event, pb->data, pb->size);
1.6 nicm 67: else
1.7 nicm 68: cmd_paste_buffer_lf2cr(wp, pb->data, pb->size);
1.3 nicm 69: }
1.1 nicm 70:
71: /* Delete the buffer if -d. */
1.8 nicm 72: if (cmd_check_flag(data->chflags, 'd')) {
1.1 nicm 73: if (data->buffer == -1)
74: paste_free_top(&s->buffers);
75: else
76: paste_free_index(&s->buffers, data->buffer);
77: }
78:
1.10 nicm 79: return (0);
1.3 nicm 80: }
81:
82: /* Add bytes to a buffer but change every '\n' to '\r'. */
83: void
1.7 nicm 84: cmd_paste_buffer_lf2cr(struct window_pane *wp, const char *data, size_t size)
1.3 nicm 85: {
86: const char *end = data + size;
87: const char *lf;
88:
89: while ((lf = memchr(data, '\n', end - data)) != NULL) {
90: if (lf != data)
1.7 nicm 91: bufferevent_write(wp->event, data, lf - data);
92: bufferevent_write(wp->event, "\r", 1);
1.3 nicm 93: data = lf + 1;
94: }
95:
96: if (end != data)
1.7 nicm 97: bufferevent_write(wp->event, data, end - data);
1.1 nicm 98: }