xref: /openbsd-src/usr.bin/tmux/cmd-capture-pane.c (revision 6a13ef69787db04ae501a22e92fa10865b44fd7c)
1 /* $OpenBSD: cmd-capture-pane.c,v 1.42 2017/01/02 22:42:19 nicm Exp $ */
2 
3 /*
4  * Copyright (c) 2009 Jonathan Alvarado <radobobo@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 <stdlib.h>
22 #include <string.h>
23 
24 #include "tmux.h"
25 
26 /*
27  * Write the entire contents of a pane to a buffer or stdout.
28  */
29 
30 static enum cmd_retval	cmd_capture_pane_exec(struct cmd *, struct cmdq_item *);
31 
32 static char	*cmd_capture_pane_append(char *, size_t *, char *, size_t);
33 static char	*cmd_capture_pane_pending(struct args *, struct window_pane *,
34 		     size_t *);
35 static char	*cmd_capture_pane_history(struct args *, struct cmdq_item *,
36 		     struct window_pane *, size_t *);
37 
38 const struct cmd_entry cmd_capture_pane_entry = {
39 	.name = "capture-pane",
40 	.alias = "capturep",
41 
42 	.args = { "ab:CeE:JpPqS:t:", 0, 0 },
43 	.usage = "[-aCeJpPq] " CMD_BUFFER_USAGE " [-E end-line] "
44 		 "[-S start-line]" CMD_TARGET_PANE_USAGE,
45 
46 	.tflag = CMD_PANE,
47 
48 	.flags = CMD_AFTERHOOK,
49 	.exec = cmd_capture_pane_exec
50 };
51 
52 static char *
53 cmd_capture_pane_append(char *buf, size_t *len, char *line, size_t linelen)
54 {
55 	buf = xrealloc(buf, *len + linelen + 1);
56 	memcpy(buf + *len, line, linelen);
57 	*len += linelen;
58 	return (buf);
59 }
60 
61 static char *
62 cmd_capture_pane_pending(struct args *args, struct window_pane *wp,
63     size_t *len)
64 {
65 	struct evbuffer	*pending;
66 	char		*buf, *line, tmp[5];
67 	size_t		 linelen;
68 	u_int		 i;
69 
70 	pending = input_pending(wp);
71 	if (pending == NULL)
72 		return (xstrdup(""));
73 
74 	line = EVBUFFER_DATA(pending);
75 	linelen = EVBUFFER_LENGTH(pending);
76 
77 	buf = xstrdup("");
78 	if (args_has(args, 'C')) {
79 		for (i = 0; i < linelen; i++) {
80 			if (line[i] >= ' ' && line[i] != '\\') {
81 				tmp[0] = line[i];
82 				tmp[1] = '\0';
83 			} else
84 				xsnprintf(tmp, sizeof tmp, "\\%03hho", line[i]);
85 			buf = cmd_capture_pane_append(buf, len, tmp,
86 			    strlen(tmp));
87 		}
88 	} else
89 		buf = cmd_capture_pane_append(buf, len, line, linelen);
90 	return (buf);
91 }
92 
93 static char *
94 
95 cmd_capture_pane_history(struct args *args, struct cmdq_item *item,
96     struct window_pane *wp, size_t *len)
97 {
98 	struct grid		*gd;
99 	const struct grid_line	*gl;
100 	struct grid_cell	*gc = NULL;
101 	int			 n, with_codes, escape_c0, join_lines;
102 	u_int			 i, sx, top, bottom, tmp;
103 	char			*cause, *buf, *line;
104 	const char		*Sflag, *Eflag;
105 	size_t			 linelen;
106 
107 	sx = screen_size_x(&wp->base);
108 	if (args_has(args, 'a')) {
109 		gd = wp->saved_grid;
110 		if (gd == NULL) {
111 			if (!args_has(args, 'q')) {
112 				cmdq_error(item, "no alternate screen");
113 				return (NULL);
114 			}
115 			return (xstrdup(""));
116 		}
117 	} else
118 		gd = wp->base.grid;
119 
120 	Sflag = args_get(args, 'S');
121 	if (Sflag != NULL && strcmp(Sflag, "-") == 0)
122 		top = 0;
123 	else {
124 		n = args_strtonum(args, 'S', INT_MIN, SHRT_MAX, &cause);
125 		if (cause != NULL) {
126 			top = gd->hsize;
127 			free(cause);
128 		} else if (n < 0 && (u_int) -n > gd->hsize)
129 			top = 0;
130 		else
131 			top = gd->hsize + n;
132 		if (top > gd->hsize + gd->sy - 1)
133 			top = gd->hsize + gd->sy - 1;
134 	}
135 
136 	Eflag = args_get(args, 'E');
137 	if (Eflag != NULL && strcmp(Eflag, "-") == 0)
138 		bottom = gd->hsize + gd->sy - 1;
139 	else {
140 		n = args_strtonum(args, 'E', INT_MIN, SHRT_MAX, &cause);
141 		if (cause != NULL) {
142 			bottom = gd->hsize + gd->sy - 1;
143 			free(cause);
144 		} else if (n < 0 && (u_int) -n > gd->hsize)
145 			bottom = 0;
146 		else
147 			bottom = gd->hsize + n;
148 		if (bottom > gd->hsize + gd->sy - 1)
149 			bottom = gd->hsize + gd->sy - 1;
150 	}
151 
152 	if (bottom < top) {
153 		tmp = bottom;
154 		bottom = top;
155 		top = tmp;
156 	}
157 
158 	with_codes = args_has(args, 'e');
159 	escape_c0 = args_has(args, 'C');
160 	join_lines = args_has(args, 'J');
161 
162 	buf = NULL;
163 	for (i = top; i <= bottom; i++) {
164 		line = grid_string_cells(gd, 0, i, sx, &gc, with_codes,
165 		    escape_c0, !join_lines);
166 		linelen = strlen(line);
167 
168 		buf = cmd_capture_pane_append(buf, len, line, linelen);
169 
170 		gl = grid_peek_line(gd, i);
171 		if (!join_lines || !(gl->flags & GRID_LINE_WRAPPED))
172 			buf[(*len)++] = '\n';
173 
174 		free(line);
175 	}
176 	return (buf);
177 }
178 
179 static enum cmd_retval
180 cmd_capture_pane_exec(struct cmd *self, struct cmdq_item *item)
181 {
182 	struct args		*args = self->args;
183 	struct client		*c;
184 	struct window_pane	*wp = item->state.tflag.wp;
185 	char			*buf, *cause;
186 	const char		*bufname;
187 	size_t			 len;
188 
189 	len = 0;
190 	if (args_has(args, 'P'))
191 		buf = cmd_capture_pane_pending(args, wp, &len);
192 	else
193 		buf = cmd_capture_pane_history(args, item, wp, &len);
194 	if (buf == NULL)
195 		return (CMD_RETURN_ERROR);
196 
197 	if (args_has(args, 'p')) {
198 		c = item->client;
199 		if (c == NULL ||
200 		    (c->session != NULL && !(c->flags & CLIENT_CONTROL))) {
201 			cmdq_error(item, "can't write to stdout");
202 			free(buf);
203 			return (CMD_RETURN_ERROR);
204 		}
205 		evbuffer_add(c->stdout_data, buf, len);
206 		free(buf);
207 		if (args_has(args, 'P') && len > 0)
208 		    evbuffer_add(c->stdout_data, "\n", 1);
209 		server_client_push_stdout(c);
210 	} else {
211 		bufname = NULL;
212 		if (args_has(args, 'b'))
213 			bufname = args_get(args, 'b');
214 
215 		if (paste_set(buf, len, bufname, &cause) != 0) {
216 			cmdq_error(item, "%s", cause);
217 			free(cause);
218 			free(buf);
219 			return (CMD_RETURN_ERROR);
220 		}
221 	}
222 
223 	return (CMD_RETURN_NORMAL);
224 }
225