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