xref: /openbsd-src/usr.bin/tmux/cmd-paste-buffer.c (revision 8a0c65a05a69cfe2b577d48e8f7a71695c5ed1a2)
1 /* $OpenBSD: cmd-paste-buffer.c,v 1.3 2009/07/11 19:14:56 nicm Exp $ */
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 *);
30 void	cmd_paste_buffer_lf2cr(struct buffer *, const char *, size_t);
31 
32 const struct cmd_entry cmd_paste_buffer_entry = {
33 	"paste-buffer", "pasteb",
34 	"[-dr] " CMD_BUFFER_WINDOW_USAGE,
35 	CMD_DFLAG|CMD_RFLAG,
36 	cmd_buffer_init,
37 	cmd_buffer_parse,
38 	cmd_paste_buffer_exec,
39 	cmd_buffer_send,
40 	cmd_buffer_recv,
41 	cmd_buffer_free,
42 	cmd_buffer_print
43 };
44 
45 int
46 cmd_paste_buffer_exec(struct cmd *self, struct cmd_ctx *ctx)
47 {
48 	struct cmd_buffer_data	*data = self->data;
49 	struct winlink		*wl;
50 	struct window		*w;
51 	struct session		*s;
52 	struct paste_buffer	*pb;
53 
54 	if ((wl = cmd_find_window(ctx, data->target, &s)) == NULL)
55 		return (-1);
56 	w = wl->window;
57 
58 	if (data->buffer == -1)
59 		pb = paste_get_top(&s->buffers);
60 	else {
61 		if ((pb = paste_get_index(&s->buffers, data->buffer)) == NULL) {
62 			ctx->error(ctx, "no buffer %d", data->buffer);
63 			return (-1);
64 		}
65 	}
66 
67 	if (pb != NULL && *pb->data != '\0') {
68 		/* -r means raw data without LF->CR conversion. */
69 		if (data->flags & CMD_RFLAG) {
70 			buffer_write(
71 			    w->active->out, pb->data, strlen(pb->data));
72 		} else {
73 			cmd_paste_buffer_lf2cr(
74 			    w->active->out, pb->data, strlen(pb->data));
75 		}
76 	}
77 
78 	/* Delete the buffer if -d. */
79 	if (data->flags & CMD_DFLAG) {
80 		if (data->buffer == -1)
81 			paste_free_top(&s->buffers);
82 		else
83 			paste_free_index(&s->buffers, data->buffer);
84 	}
85 
86  	return (0);
87 }
88 
89 /* Add bytes to a buffer but change every '\n' to '\r'. */
90 void
91 cmd_paste_buffer_lf2cr(struct buffer *b, const char *data, size_t size)
92 {
93 	const char	*end = data + size;
94 	const char	*lf;
95 
96 	while ((lf = memchr(data, '\n', end - data)) != NULL) {
97 		if (lf != data)
98 			buffer_write(b, data, lf - data);
99 		buffer_write8(b, '\r');
100 		data = lf + 1;
101 	}
102 
103 	if (end != data)
104 		buffer_write(b, data, end - data);
105 }
106