/src/tmux/cmd-capture-pane.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* $OpenBSD$ */ |
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:JMNpPqS:Tt:", 0, 0, NULL }, |
43 | | .usage = "[-aCeJMNpPqT] " CMD_BUFFER_USAGE " [-E end-line] " |
44 | | "[-S start-line] " CMD_TARGET_PANE_USAGE, |
45 | | |
46 | | .target = { 't', CMD_FIND_PANE, 0 }, |
47 | | |
48 | | .flags = CMD_AFTERHOOK, |
49 | | .exec = cmd_capture_pane_exec |
50 | | }; |
51 | | |
52 | | const struct cmd_entry cmd_clear_history_entry = { |
53 | | .name = "clear-history", |
54 | | .alias = "clearhist", |
55 | | |
56 | | .args = { "Ht:", 0, 0, NULL }, |
57 | | .usage = "[-H] " CMD_TARGET_PANE_USAGE, |
58 | | |
59 | | .target = { 't', CMD_FIND_PANE, 0 }, |
60 | | |
61 | | .flags = CMD_AFTERHOOK, |
62 | | .exec = cmd_capture_pane_exec |
63 | | }; |
64 | | |
65 | | static char * |
66 | | cmd_capture_pane_append(char *buf, size_t *len, char *line, size_t linelen) |
67 | 0 | { |
68 | 0 | buf = xrealloc(buf, *len + linelen + 1); |
69 | 0 | memcpy(buf + *len, line, linelen); |
70 | 0 | *len += linelen; |
71 | 0 | return (buf); |
72 | 0 | } |
73 | | |
74 | | static char * |
75 | | cmd_capture_pane_pending(struct args *args, struct window_pane *wp, |
76 | | size_t *len) |
77 | 0 | { |
78 | 0 | struct evbuffer *pending; |
79 | 0 | char *buf, *line, tmp[5]; |
80 | 0 | size_t linelen; |
81 | 0 | u_int i; |
82 | |
|
83 | 0 | pending = input_pending(wp->ictx); |
84 | 0 | if (pending == NULL) |
85 | 0 | return (xstrdup("")); |
86 | | |
87 | 0 | line = EVBUFFER_DATA(pending); |
88 | 0 | linelen = EVBUFFER_LENGTH(pending); |
89 | |
|
90 | 0 | buf = xstrdup(""); |
91 | 0 | if (args_has(args, 'C')) { |
92 | 0 | for (i = 0; i < linelen; i++) { |
93 | 0 | if (line[i] >= ' ' && line[i] != '\\') { |
94 | 0 | tmp[0] = line[i]; |
95 | 0 | tmp[1] = '\0'; |
96 | 0 | } else |
97 | 0 | xsnprintf(tmp, sizeof tmp, "\\%03hho", line[i]); |
98 | 0 | buf = cmd_capture_pane_append(buf, len, tmp, |
99 | 0 | strlen(tmp)); |
100 | 0 | } |
101 | 0 | } else |
102 | 0 | buf = cmd_capture_pane_append(buf, len, line, linelen); |
103 | 0 | return (buf); |
104 | 0 | } |
105 | | |
106 | | static char * |
107 | | cmd_capture_pane_history(struct args *args, struct cmdq_item *item, |
108 | | struct window_pane *wp, size_t *len) |
109 | 0 | { |
110 | 0 | struct grid *gd; |
111 | 0 | const struct grid_line *gl; |
112 | 0 | struct screen *s; |
113 | 0 | struct grid_cell *gc = NULL; |
114 | 0 | struct window_mode_entry *wme; |
115 | 0 | int n, join_lines, flags = 0; |
116 | 0 | u_int i, sx, top, bottom, tmp; |
117 | 0 | char *cause, *buf, *line; |
118 | 0 | const char *Sflag, *Eflag; |
119 | 0 | size_t linelen; |
120 | |
|
121 | 0 | sx = screen_size_x(&wp->base); |
122 | 0 | if (args_has(args, 'a')) { |
123 | 0 | gd = wp->base.saved_grid; |
124 | 0 | if (gd == NULL) { |
125 | 0 | if (!args_has(args, 'q')) { |
126 | 0 | cmdq_error(item, "no alternate screen"); |
127 | 0 | return (NULL); |
128 | 0 | } |
129 | 0 | return (xstrdup("")); |
130 | 0 | } |
131 | 0 | s = &wp->base; |
132 | 0 | } else if (args_has(args, 'M')) { |
133 | 0 | wme = TAILQ_FIRST(&wp->modes); |
134 | 0 | if (wme != NULL && wme->mode->get_screen != NULL) { |
135 | 0 | s = wme->mode->get_screen (wme); |
136 | 0 | gd = s->grid; |
137 | 0 | } else { |
138 | 0 | s = &wp->base; |
139 | 0 | gd = wp->base.grid; |
140 | 0 | } |
141 | 0 | } else { |
142 | 0 | s = &wp->base; |
143 | 0 | gd = wp->base.grid; |
144 | 0 | } |
145 | | |
146 | 0 | Sflag = args_get(args, 'S'); |
147 | 0 | if (Sflag != NULL && strcmp(Sflag, "-") == 0) |
148 | 0 | top = 0; |
149 | 0 | else { |
150 | 0 | n = args_strtonum_and_expand(args, 'S', INT_MIN, SHRT_MAX, |
151 | 0 | item, &cause); |
152 | 0 | if (cause != NULL) { |
153 | 0 | top = gd->hsize; |
154 | 0 | free(cause); |
155 | 0 | } else if (n < 0 && (u_int) -n > gd->hsize) |
156 | 0 | top = 0; |
157 | 0 | else |
158 | 0 | top = gd->hsize + n; |
159 | 0 | if (top > gd->hsize + gd->sy - 1) |
160 | 0 | top = gd->hsize + gd->sy - 1; |
161 | 0 | } |
162 | |
|
163 | 0 | Eflag = args_get(args, 'E'); |
164 | 0 | if (Eflag != NULL && strcmp(Eflag, "-") == 0) |
165 | 0 | bottom = gd->hsize + gd->sy - 1; |
166 | 0 | else { |
167 | 0 | n = args_strtonum_and_expand(args, 'E', INT_MIN, SHRT_MAX, |
168 | 0 | item, &cause); |
169 | 0 | if (cause != NULL) { |
170 | 0 | bottom = gd->hsize + gd->sy - 1; |
171 | 0 | free(cause); |
172 | 0 | } else if (n < 0 && (u_int) -n > gd->hsize) |
173 | 0 | bottom = 0; |
174 | 0 | else |
175 | 0 | bottom = gd->hsize + n; |
176 | 0 | if (bottom > gd->hsize + gd->sy - 1) |
177 | 0 | bottom = gd->hsize + gd->sy - 1; |
178 | 0 | } |
179 | |
|
180 | 0 | if (bottom < top) { |
181 | 0 | tmp = bottom; |
182 | 0 | bottom = top; |
183 | 0 | top = tmp; |
184 | 0 | } |
185 | |
|
186 | 0 | join_lines = args_has(args, 'J'); |
187 | 0 | if (args_has(args, 'e')) |
188 | 0 | flags |= GRID_STRING_WITH_SEQUENCES; |
189 | 0 | if (args_has(args, 'C')) |
190 | 0 | flags |= GRID_STRING_ESCAPE_SEQUENCES; |
191 | 0 | if (!join_lines && !args_has(args, 'T')) |
192 | 0 | flags |= GRID_STRING_EMPTY_CELLS; |
193 | 0 | if (!join_lines && !args_has(args, 'N')) |
194 | 0 | flags |= GRID_STRING_TRIM_SPACES; |
195 | |
|
196 | 0 | buf = NULL; |
197 | 0 | for (i = top; i <= bottom; i++) { |
198 | 0 | line = grid_string_cells(gd, 0, i, sx, &gc, flags, s); |
199 | 0 | linelen = strlen(line); |
200 | |
|
201 | 0 | buf = cmd_capture_pane_append(buf, len, line, linelen); |
202 | |
|
203 | 0 | gl = grid_peek_line(gd, i); |
204 | 0 | if (!join_lines || !(gl->flags & GRID_LINE_WRAPPED)) |
205 | 0 | buf[(*len)++] = '\n'; |
206 | |
|
207 | 0 | free(line); |
208 | 0 | } |
209 | 0 | return (buf); |
210 | 0 | } |
211 | | |
212 | | static enum cmd_retval |
213 | | cmd_capture_pane_exec(struct cmd *self, struct cmdq_item *item) |
214 | 0 | { |
215 | 0 | struct args *args = cmd_get_args(self); |
216 | 0 | struct client *c = cmdq_get_client(item); |
217 | 0 | struct window_pane *wp = cmdq_get_target(item)->wp; |
218 | 0 | char *buf, *cause; |
219 | 0 | const char *bufname; |
220 | 0 | size_t len; |
221 | |
|
222 | 0 | if (cmd_get_entry(self) == &cmd_clear_history_entry) { |
223 | 0 | window_pane_reset_mode_all(wp); |
224 | 0 | grid_clear_history(wp->base.grid); |
225 | 0 | if (args_has(args, 'H')) |
226 | 0 | screen_reset_hyperlinks(wp->screen); |
227 | 0 | return (CMD_RETURN_NORMAL); |
228 | 0 | } |
229 | | |
230 | 0 | len = 0; |
231 | 0 | if (args_has(args, 'P')) |
232 | 0 | buf = cmd_capture_pane_pending(args, wp, &len); |
233 | 0 | else |
234 | 0 | buf = cmd_capture_pane_history(args, item, wp, &len); |
235 | 0 | if (buf == NULL) |
236 | 0 | return (CMD_RETURN_ERROR); |
237 | | |
238 | 0 | if (args_has(args, 'p')) { |
239 | 0 | if (len > 0 && buf[len - 1] == '\n') |
240 | 0 | len--; |
241 | 0 | if (c->flags & CLIENT_CONTROL) |
242 | 0 | control_write(c, "%.*s", (int)len, buf); |
243 | 0 | else { |
244 | 0 | if (!file_can_print(c)) { |
245 | 0 | cmdq_error(item, "can't write to client"); |
246 | 0 | free(buf); |
247 | 0 | return (CMD_RETURN_ERROR); |
248 | 0 | } |
249 | 0 | file_print_buffer(c, buf, len); |
250 | 0 | file_print(c, "\n"); |
251 | 0 | free(buf); |
252 | 0 | } |
253 | 0 | } else { |
254 | 0 | bufname = NULL; |
255 | 0 | if (args_has(args, 'b')) |
256 | 0 | bufname = args_get(args, 'b'); |
257 | |
|
258 | 0 | if (paste_set(buf, len, bufname, &cause) != 0) { |
259 | 0 | cmdq_error(item, "%s", cause); |
260 | 0 | free(cause); |
261 | 0 | free(buf); |
262 | 0 | return (CMD_RETURN_ERROR); |
263 | 0 | } |
264 | 0 | } |
265 | | |
266 | 0 | return (CMD_RETURN_NORMAL); |
267 | 0 | } |