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