Annotation of src/usr.bin/tmux/cmd-capture-pane.c, Revision 1.37
1.37 ! nicm 1: /* $OpenBSD: cmd-capture-pane.c,v 1.36 2015/12/13 14:32:38 nicm Exp $ */
1.1 nicm 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:
1.4 nicm 21: #include <stdlib.h>
1.1 nicm 22: #include <string.h>
23:
24: #include "tmux.h"
25:
26: /*
1.14 nicm 27: * Write the entire contents of a pane to a buffer or stdout.
1.1 nicm 28: */
29:
1.19 nicm 30: enum cmd_retval cmd_capture_pane_exec(struct cmd *, struct cmd_q *);
1.1 nicm 31:
1.23 nicm 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:
1.1 nicm 38: const struct cmd_entry cmd_capture_pane_entry = {
1.37 ! nicm 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: .flags = CMD_PANE_T,
! 47: .exec = cmd_capture_pane_exec
1.1 nicm 48: };
49:
1.23 nicm 50: char *
51: cmd_capture_pane_append(char *buf, size_t *len, char *line, size_t linelen)
52: {
1.29 nicm 53: buf = xrealloc(buf, *len + linelen + 1);
1.23 nicm 54: memcpy(buf + *len, line, linelen);
55: *len += linelen;
56: return (buf);
57: }
58:
59: char *
60: cmd_capture_pane_pending(struct args *args, struct window_pane *wp,
61: size_t *len)
62: {
1.32 nicm 63: struct evbuffer *pending;
64: char *buf, *line, tmp[5];
65: size_t linelen;
66: u_int i;
1.23 nicm 67:
1.32 nicm 68: pending = input_pending(wp);
69: if (pending == NULL)
1.23 nicm 70: return (xstrdup(""));
71:
1.32 nicm 72: line = EVBUFFER_DATA(pending);
73: linelen = EVBUFFER_LENGTH(pending);
1.23 nicm 74:
1.24 nicm 75: buf = xstrdup("");
1.23 nicm 76: if (args_has(args, 'C')) {
77: for (i = 0; i < linelen; i++) {
78: if (line[i] >= ' ') {
79: tmp[0] = line[i];
80: tmp[1] = '\0';
81: } else
1.31 nicm 82: xsnprintf(tmp, sizeof tmp, "\\%03hho", line[i]);
1.23 nicm 83: buf = cmd_capture_pane_append(buf, len, tmp,
84: strlen(tmp));
85: }
86: } else
87: buf = cmd_capture_pane_append(buf, len, line, linelen);
88: return (buf);
89: }
90:
91: char *
92: cmd_capture_pane_history(struct args *args, struct cmd_q *cmdq,
93: struct window_pane *wp, size_t *len)
1.1 nicm 94: {
1.7 nicm 95: struct grid *gd;
1.18 nicm 96: const struct grid_line *gl;
1.23 nicm 97: struct grid_cell *gc = NULL;
98: int n, with_codes, escape_c0, join_lines;
99: u_int i, sx, top, bottom, tmp;
100: char *cause, *buf, *line;
1.28 nicm 101: const char *Sflag, *Eflag;
1.23 nicm 102: size_t linelen;
1.14 nicm 103:
1.23 nicm 104: sx = screen_size_x(&wp->base);
1.20 nicm 105: if (args_has(args, 'a')) {
106: gd = wp->saved_grid;
1.23 nicm 107: if (gd == NULL) {
108: if (!args_has(args, 'q')) {
109: cmdq_error(cmdq, "no alternate screen");
110: return (NULL);
111: }
112: return (xstrdup(""));
1.20 nicm 113: }
1.23 nicm 114: } else
115: gd = wp->base.grid;
116:
1.28 nicm 117: Sflag = args_get(args, 'S');
118: if (Sflag != NULL && strcmp(Sflag, "-") == 0)
1.23 nicm 119: top = 0;
1.28 nicm 120: else {
121: n = args_strtonum(args, 'S', INT_MIN, SHRT_MAX, &cause);
122: if (cause != NULL) {
123: top = gd->hsize;
124: free(cause);
125: } else if (n < 0 && (u_int) -n > gd->hsize)
126: top = 0;
127: else
128: top = gd->hsize + n;
129: if (top > gd->hsize + gd->sy - 1)
130: top = gd->hsize + gd->sy - 1;
131: }
1.23 nicm 132:
1.28 nicm 133: Eflag = args_get(args, 'E');
134: if (Eflag != NULL && strcmp(Eflag, "-") == 0)
1.23 nicm 135: bottom = gd->hsize + gd->sy - 1;
1.28 nicm 136: else {
137: n = args_strtonum(args, 'E', INT_MIN, SHRT_MAX, &cause);
138: if (cause != NULL) {
139: bottom = gd->hsize + gd->sy - 1;
140: free(cause);
141: } else if (n < 0 && (u_int) -n > gd->hsize)
142: bottom = 0;
143: else
144: bottom = gd->hsize + n;
145: if (bottom > gd->hsize + gd->sy - 1)
146: bottom = gd->hsize + gd->sy - 1;
147: }
1.23 nicm 148:
149: if (bottom < top) {
150: tmp = bottom;
151: bottom = top;
152: top = tmp;
1.20 nicm 153: }
1.1 nicm 154:
1.23 nicm 155: with_codes = args_has(args, 'e');
156: escape_c0 = args_has(args, 'C');
157: join_lines = args_has(args, 'J');
158:
1.1 nicm 159: buf = NULL;
1.23 nicm 160: for (i = top; i <= bottom; i++) {
161: line = grid_string_cells(gd, 0, i, sx, &gc, with_codes,
162: escape_c0, !join_lines);
163: linelen = strlen(line);
164:
165: buf = cmd_capture_pane_append(buf, len, line, linelen);
166:
167: gl = grid_peek_line(gd, i);
168: if (!join_lines || !(gl->flags & GRID_LINE_WRAPPED))
169: buf[(*len)++] = '\n';
170:
171: free(line);
172: }
173: return (buf);
174: }
1.1 nicm 175:
1.23 nicm 176: enum cmd_retval
177: cmd_capture_pane_exec(struct cmd *self, struct cmd_q *cmdq)
178: {
179: struct args *args = self->args;
180: struct client *c;
1.36 nicm 181: struct window_pane *wp = cmdq->state.tflag.wp;
1.23 nicm 182: char *buf, *cause;
1.27 nicm 183: const char *bufname;
1.23 nicm 184: size_t len;
1.1 nicm 185:
1.23 nicm 186: len = 0;
187: if (args_has(args, 'P'))
188: buf = cmd_capture_pane_pending(args, wp, &len);
189: else
190: buf = cmd_capture_pane_history(args, cmdq, wp, &len);
191: if (buf == NULL)
192: return (CMD_RETURN_ERROR);
1.1 nicm 193:
1.14 nicm 194: if (args_has(args, 'p')) {
1.19 nicm 195: c = cmdq->client;
196: if (c == NULL ||
197: (c->session != NULL && !(c->flags & CLIENT_CONTROL))) {
198: cmdq_error(cmdq, "can't write to stdout");
1.33 nicm 199: free(buf);
1.14 nicm 200: return (CMD_RETURN_ERROR);
201: }
202: evbuffer_add(c->stdout_data, buf, len);
1.34 nicm 203: free(buf);
1.24 nicm 204: if (args_has(args, 'P') && len > 0)
205: evbuffer_add(c->stdout_data, "\n", 1);
1.35 nicm 206: server_client_push_stdout(c);
1.14 nicm 207: } else {
1.27 nicm 208: bufname = NULL;
209: if (args_has(args, 'b'))
210: bufname = args_get(args, 'b');
211:
212: if (paste_set(buf, len, bufname, &cause) != 0) {
213: cmdq_error(cmdq, "%s", cause);
1.33 nicm 214: free(cause);
1.14 nicm 215: free(buf);
216: return (CMD_RETURN_ERROR);
217: }
1.1 nicm 218: }
1.6 nicm 219:
1.12 nicm 220: return (CMD_RETURN_NORMAL);
1.1 nicm 221: }