cmd-capture-pane.c revision 1.4 1 1.3 christos /* $OpenBSD$ */
2 1.1 jmmv
3 1.1 jmmv /*
4 1.1 jmmv * Copyright (c) 2009 Jonathan Alvarado <radobobo (at) users.sourceforge.net>
5 1.1 jmmv *
6 1.1 jmmv * Permission to use, copy, modify, and distribute this software for any
7 1.1 jmmv * purpose with or without fee is hereby granted, provided that the above
8 1.1 jmmv * copyright notice and this permission notice appear in all copies.
9 1.1 jmmv *
10 1.1 jmmv * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 1.1 jmmv * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 1.1 jmmv * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 1.1 jmmv * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 1.1 jmmv * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15 1.1 jmmv * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16 1.1 jmmv * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 1.1 jmmv */
18 1.1 jmmv
19 1.1 jmmv #include <sys/types.h>
20 1.1 jmmv
21 1.2 christos #include <stdlib.h>
22 1.1 jmmv #include <string.h>
23 1.1 jmmv
24 1.1 jmmv #include "tmux.h"
25 1.1 jmmv
26 1.1 jmmv /*
27 1.2 christos * Write the entire contents of a pane to a buffer or stdout.
28 1.1 jmmv */
29 1.1 jmmv
30 1.2 christos enum cmd_retval cmd_capture_pane_exec(struct cmd *, struct cmd_q *);
31 1.2 christos
32 1.2 christos char *cmd_capture_pane_append(char *, size_t *, char *, size_t);
33 1.2 christos char *cmd_capture_pane_pending(struct args *, struct window_pane *,
34 1.2 christos size_t *);
35 1.2 christos char *cmd_capture_pane_history(struct args *, struct cmd_q *,
36 1.2 christos struct window_pane *, size_t *);
37 1.1 jmmv
38 1.1 jmmv const struct cmd_entry cmd_capture_pane_entry = {
39 1.4 christos .name = "capture-pane",
40 1.4 christos .alias = "capturep",
41 1.4 christos
42 1.4 christos .args = { "ab:CeE:JpPqS:t:", 0, 0 },
43 1.4 christos .usage = "[-aCeJpPq] " CMD_BUFFER_USAGE " [-E end-line] "
44 1.4 christos "[-S start-line]" CMD_TARGET_PANE_USAGE,
45 1.4 christos
46 1.4 christos .tflag = CMD_PANE,
47 1.4 christos
48 1.4 christos .flags = 0,
49 1.4 christos .exec = cmd_capture_pane_exec
50 1.1 jmmv };
51 1.1 jmmv
52 1.2 christos char *
53 1.2 christos cmd_capture_pane_append(char *buf, size_t *len, char *line, size_t linelen)
54 1.2 christos {
55 1.3 christos buf = xrealloc(buf, *len + linelen + 1);
56 1.2 christos memcpy(buf + *len, line, linelen);
57 1.2 christos *len += linelen;
58 1.2 christos return (buf);
59 1.2 christos }
60 1.2 christos
61 1.2 christos char *
62 1.2 christos cmd_capture_pane_pending(struct args *args, struct window_pane *wp,
63 1.2 christos size_t *len)
64 1.2 christos {
65 1.3 christos struct evbuffer *pending;
66 1.3 christos char *buf, *line, tmp[5];
67 1.3 christos size_t linelen;
68 1.3 christos u_int i;
69 1.2 christos
70 1.3 christos pending = input_pending(wp);
71 1.3 christos if (pending == NULL)
72 1.2 christos return (xstrdup(""));
73 1.2 christos
74 1.3 christos line = (char *)EVBUFFER_DATA(pending);
75 1.3 christos linelen = EVBUFFER_LENGTH(pending);
76 1.2 christos
77 1.2 christos buf = xstrdup("");
78 1.2 christos if (args_has(args, 'C')) {
79 1.2 christos for (i = 0; i < linelen; i++) {
80 1.2 christos if (line[i] >= ' ') {
81 1.2 christos tmp[0] = line[i];
82 1.2 christos tmp[1] = '\0';
83 1.2 christos } else
84 1.3 christos xsnprintf(tmp, sizeof tmp, "\\%03hho", line[i]);
85 1.2 christos buf = cmd_capture_pane_append(buf, len, tmp,
86 1.2 christos strlen(tmp));
87 1.2 christos }
88 1.2 christos } else
89 1.2 christos buf = cmd_capture_pane_append(buf, len, line, linelen);
90 1.2 christos return (buf);
91 1.2 christos }
92 1.2 christos
93 1.2 christos char *
94 1.2 christos cmd_capture_pane_history(struct args *args, struct cmd_q *cmdq,
95 1.2 christos struct window_pane *wp, size_t *len)
96 1.1 jmmv {
97 1.2 christos struct grid *gd;
98 1.2 christos const struct grid_line *gl;
99 1.2 christos struct grid_cell *gc = NULL;
100 1.2 christos int n, with_codes, escape_c0, join_lines;
101 1.2 christos u_int i, sx, top, bottom, tmp;
102 1.2 christos char *cause, *buf, *line;
103 1.3 christos const char *Sflag, *Eflag;
104 1.2 christos size_t linelen;
105 1.2 christos
106 1.2 christos sx = screen_size_x(&wp->base);
107 1.2 christos if (args_has(args, 'a')) {
108 1.2 christos gd = wp->saved_grid;
109 1.2 christos if (gd == NULL) {
110 1.2 christos if (!args_has(args, 'q')) {
111 1.2 christos cmdq_error(cmdq, "no alternate screen");
112 1.2 christos return (NULL);
113 1.2 christos }
114 1.2 christos return (xstrdup(""));
115 1.2 christos }
116 1.2 christos } else
117 1.2 christos gd = wp->base.grid;
118 1.2 christos
119 1.3 christos Sflag = args_get(args, 'S');
120 1.3 christos if (Sflag != NULL && strcmp(Sflag, "-") == 0)
121 1.2 christos top = 0;
122 1.3 christos else {
123 1.3 christos n = args_strtonum(args, 'S', INT_MIN, SHRT_MAX, &cause);
124 1.3 christos if (cause != NULL) {
125 1.3 christos top = gd->hsize;
126 1.3 christos free(cause);
127 1.3 christos } else if (n < 0 && (u_int) -n > gd->hsize)
128 1.3 christos top = 0;
129 1.3 christos else
130 1.3 christos top = gd->hsize + n;
131 1.3 christos if (top > gd->hsize + gd->sy - 1)
132 1.3 christos top = gd->hsize + gd->sy - 1;
133 1.3 christos }
134 1.2 christos
135 1.3 christos Eflag = args_get(args, 'E');
136 1.3 christos if (Eflag != NULL && strcmp(Eflag, "-") == 0)
137 1.2 christos bottom = gd->hsize + gd->sy - 1;
138 1.3 christos else {
139 1.3 christos n = args_strtonum(args, 'E', INT_MIN, SHRT_MAX, &cause);
140 1.3 christos if (cause != NULL) {
141 1.3 christos bottom = gd->hsize + gd->sy - 1;
142 1.3 christos free(cause);
143 1.3 christos } else if (n < 0 && (u_int) -n > gd->hsize)
144 1.3 christos bottom = 0;
145 1.3 christos else
146 1.3 christos bottom = gd->hsize + n;
147 1.3 christos if (bottom > gd->hsize + gd->sy - 1)
148 1.3 christos bottom = gd->hsize + gd->sy - 1;
149 1.3 christos }
150 1.2 christos
151 1.2 christos if (bottom < top) {
152 1.2 christos tmp = bottom;
153 1.2 christos bottom = top;
154 1.2 christos top = tmp;
155 1.2 christos }
156 1.2 christos
157 1.2 christos with_codes = args_has(args, 'e');
158 1.2 christos escape_c0 = args_has(args, 'C');
159 1.2 christos join_lines = args_has(args, 'J');
160 1.1 jmmv
161 1.1 jmmv buf = NULL;
162 1.2 christos for (i = top; i <= bottom; i++) {
163 1.2 christos line = grid_string_cells(gd, 0, i, sx, &gc, with_codes,
164 1.2 christos escape_c0, !join_lines);
165 1.2 christos linelen = strlen(line);
166 1.2 christos
167 1.2 christos buf = cmd_capture_pane_append(buf, len, line, linelen);
168 1.2 christos
169 1.2 christos gl = grid_peek_line(gd, i);
170 1.2 christos if (!join_lines || !(gl->flags & GRID_LINE_WRAPPED))
171 1.2 christos buf[(*len)++] = '\n';
172 1.2 christos
173 1.2 christos free(line);
174 1.2 christos }
175 1.2 christos return (buf);
176 1.2 christos }
177 1.2 christos
178 1.2 christos enum cmd_retval
179 1.2 christos cmd_capture_pane_exec(struct cmd *self, struct cmd_q *cmdq)
180 1.2 christos {
181 1.2 christos struct args *args = self->args;
182 1.2 christos struct client *c;
183 1.4 christos struct window_pane *wp = cmdq->state.tflag.wp;
184 1.2 christos char *buf, *cause;
185 1.3 christos const char *bufname;
186 1.2 christos size_t len;
187 1.1 jmmv
188 1.2 christos len = 0;
189 1.2 christos if (args_has(args, 'P'))
190 1.2 christos buf = cmd_capture_pane_pending(args, wp, &len);
191 1.2 christos else
192 1.2 christos buf = cmd_capture_pane_history(args, cmdq, wp, &len);
193 1.2 christos if (buf == NULL)
194 1.2 christos return (CMD_RETURN_ERROR);
195 1.2 christos
196 1.2 christos if (args_has(args, 'p')) {
197 1.2 christos c = cmdq->client;
198 1.2 christos if (c == NULL ||
199 1.2 christos (c->session != NULL && !(c->flags & CLIENT_CONTROL))) {
200 1.2 christos cmdq_error(cmdq, "can't write to stdout");
201 1.3 christos free(buf);
202 1.2 christos return (CMD_RETURN_ERROR);
203 1.2 christos }
204 1.2 christos evbuffer_add(c->stdout_data, buf, len);
205 1.3 christos free(buf);
206 1.2 christos if (args_has(args, 'P') && len > 0)
207 1.2 christos evbuffer_add(c->stdout_data, "\n", 1);
208 1.4 christos server_client_push_stdout(c);
209 1.2 christos } else {
210 1.3 christos bufname = NULL;
211 1.3 christos if (args_has(args, 'b'))
212 1.3 christos bufname = args_get(args, 'b');
213 1.2 christos
214 1.3 christos if (paste_set(buf, len, bufname, &cause) != 0) {
215 1.3 christos cmdq_error(cmdq, "%s", cause);
216 1.2 christos free(cause);
217 1.2 christos free(buf);
218 1.2 christos return (CMD_RETURN_ERROR);
219 1.2 christos }
220 1.1 jmmv }
221 1.1 jmmv
222 1.2 christos return (CMD_RETURN_NORMAL);
223 1.1 jmmv }
224