cmd-capture-pane.c revision 1.4 1 /* $OpenBSD$ */
2
3 /*
4 * Copyright (c) 2009 Jonathan Alvarado <radobobo (at) 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 .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 = 0,
49 .exec = cmd_capture_pane_exec
50 };
51
52 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 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 = (char *)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] >= ' ') {
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 char *
94 cmd_capture_pane_history(struct args *args, struct cmd_q *cmdq,
95 struct window_pane *wp, size_t *len)
96 {
97 struct grid *gd;
98 const struct grid_line *gl;
99 struct grid_cell *gc = NULL;
100 int n, with_codes, escape_c0, join_lines;
101 u_int i, sx, top, bottom, tmp;
102 char *cause, *buf, *line;
103 const char *Sflag, *Eflag;
104 size_t linelen;
105
106 sx = screen_size_x(&wp->base);
107 if (args_has(args, 'a')) {
108 gd = wp->saved_grid;
109 if (gd == NULL) {
110 if (!args_has(args, 'q')) {
111 cmdq_error(cmdq, "no alternate screen");
112 return (NULL);
113 }
114 return (xstrdup(""));
115 }
116 } else
117 gd = wp->base.grid;
118
119 Sflag = args_get(args, 'S');
120 if (Sflag != NULL && strcmp(Sflag, "-") == 0)
121 top = 0;
122 else {
123 n = args_strtonum(args, 'S', INT_MIN, SHRT_MAX, &cause);
124 if (cause != NULL) {
125 top = gd->hsize;
126 free(cause);
127 } else if (n < 0 && (u_int) -n > gd->hsize)
128 top = 0;
129 else
130 top = gd->hsize + n;
131 if (top > gd->hsize + gd->sy - 1)
132 top = gd->hsize + gd->sy - 1;
133 }
134
135 Eflag = args_get(args, 'E');
136 if (Eflag != NULL && strcmp(Eflag, "-") == 0)
137 bottom = gd->hsize + gd->sy - 1;
138 else {
139 n = args_strtonum(args, 'E', INT_MIN, SHRT_MAX, &cause);
140 if (cause != NULL) {
141 bottom = gd->hsize + gd->sy - 1;
142 free(cause);
143 } else if (n < 0 && (u_int) -n > gd->hsize)
144 bottom = 0;
145 else
146 bottom = gd->hsize + n;
147 if (bottom > gd->hsize + gd->sy - 1)
148 bottom = gd->hsize + gd->sy - 1;
149 }
150
151 if (bottom < top) {
152 tmp = bottom;
153 bottom = top;
154 top = tmp;
155 }
156
157 with_codes = args_has(args, 'e');
158 escape_c0 = args_has(args, 'C');
159 join_lines = args_has(args, 'J');
160
161 buf = NULL;
162 for (i = top; i <= bottom; i++) {
163 line = grid_string_cells(gd, 0, i, sx, &gc, with_codes,
164 escape_c0, !join_lines);
165 linelen = strlen(line);
166
167 buf = cmd_capture_pane_append(buf, len, line, linelen);
168
169 gl = grid_peek_line(gd, i);
170 if (!join_lines || !(gl->flags & GRID_LINE_WRAPPED))
171 buf[(*len)++] = '\n';
172
173 free(line);
174 }
175 return (buf);
176 }
177
178 enum cmd_retval
179 cmd_capture_pane_exec(struct cmd *self, struct cmd_q *cmdq)
180 {
181 struct args *args = self->args;
182 struct client *c;
183 struct window_pane *wp = cmdq->state.tflag.wp;
184 char *buf, *cause;
185 const char *bufname;
186 size_t len;
187
188 len = 0;
189 if (args_has(args, 'P'))
190 buf = cmd_capture_pane_pending(args, wp, &len);
191 else
192 buf = cmd_capture_pane_history(args, cmdq, wp, &len);
193 if (buf == NULL)
194 return (CMD_RETURN_ERROR);
195
196 if (args_has(args, 'p')) {
197 c = cmdq->client;
198 if (c == NULL ||
199 (c->session != NULL && !(c->flags & CLIENT_CONTROL))) {
200 cmdq_error(cmdq, "can't write to stdout");
201 free(buf);
202 return (CMD_RETURN_ERROR);
203 }
204 evbuffer_add(c->stdout_data, buf, len);
205 free(buf);
206 if (args_has(args, 'P') && len > 0)
207 evbuffer_add(c->stdout_data, "\n", 1);
208 server_client_push_stdout(c);
209 } else {
210 bufname = NULL;
211 if (args_has(args, 'b'))
212 bufname = args_get(args, 'b');
213
214 if (paste_set(buf, len, bufname, &cause) != 0) {
215 cmdq_error(cmdq, "%s", cause);
216 free(cause);
217 free(buf);
218 return (CMD_RETURN_ERROR);
219 }
220 }
221
222 return (CMD_RETURN_NORMAL);
223 }
224