Annotation of src/usr.bin/tmux/cmd-capture-pane.c, Revision 1.15
1.15 ! nicm 1: /* $OpenBSD: cmd-capture-pane.c,v 1.14 2013/03/21 18:43:34 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.12 nicm 30: enum cmd_retval cmd_capture_pane_exec(struct cmd *, struct cmd_ctx *);
1.1 nicm 31:
32: const struct cmd_entry cmd_capture_pane_entry = {
33: "capture-pane", "capturep",
1.15 ! nicm 34: "b:E:pS:t:", 0, 0,
! 35: "[-p] [-b buffer-index] [-E end-line] [-S start-line]"
1.13 nicm 36: CMD_TARGET_PANE_USAGE,
1.6 nicm 37: 0,
38: NULL,
39: NULL,
40: cmd_capture_pane_exec
1.1 nicm 41: };
42:
1.12 nicm 43: enum cmd_retval
1.1 nicm 44: cmd_capture_pane_exec(struct cmd *self, struct cmd_ctx *ctx)
45: {
1.6 nicm 46: struct args *args = self->args;
1.15 ! nicm 47: struct client *c = ctx->cmdclient;
1.6 nicm 48: struct window_pane *wp;
49: char *buf, *line, *cause;
50: struct screen *s;
1.7 nicm 51: struct grid *gd;
52: int buffer, n;
53: u_int i, limit, top, bottom, tmp;
1.6 nicm 54: size_t len, linelen;
1.14 nicm 55:
1.6 nicm 56: if (cmd_find_pane(ctx, args_get(args, 't'), NULL, &wp) == NULL)
1.12 nicm 57: return (CMD_RETURN_ERROR);
1.1 nicm 58: s = &wp->base;
1.7 nicm 59: gd = s->grid;
1.1 nicm 60:
61: buf = NULL;
62: len = 0;
63:
1.10 nicm 64: n = args_strtonum(args, 'S', INT_MIN, SHRT_MAX, &cause);
1.8 nicm 65: if (cause != NULL) {
1.7 nicm 66: top = gd->hsize;
1.11 nicm 67: free(cause);
1.8 nicm 68: } else if (n < 0 && (u_int) -n > gd->hsize)
1.7 nicm 69: top = 0;
70: else
71: top = gd->hsize + n;
72: if (top > gd->hsize + gd->sy - 1)
73: top = gd->hsize + gd->sy - 1;
74:
1.10 nicm 75: n = args_strtonum(args, 'E', INT_MIN, SHRT_MAX, &cause);
1.8 nicm 76: if (cause != NULL) {
1.7 nicm 77: bottom = gd->hsize + gd->sy - 1;
1.11 nicm 78: free(cause);
1.8 nicm 79: } else if (n < 0 && (u_int) -n > gd->hsize)
1.7 nicm 80: bottom = 0;
81: else
82: bottom = gd->hsize + n;
83: if (bottom > gd->hsize + gd->sy - 1)
84: bottom = gd->hsize + gd->sy - 1;
85:
86: if (bottom < top) {
87: tmp = bottom;
88: bottom = top;
89: top = tmp;
90: }
91:
92: for (i = top; i <= bottom; i++) {
93: line = grid_string_cells(s->grid, 0, i, screen_size_x(s));
1.1 nicm 94: linelen = strlen(line);
95:
96: buf = xrealloc(buf, 1, len + linelen + 1);
97: memcpy(buf + len, line, linelen);
98: len += linelen;
99: buf[len++] = '\n';
1.3 nicm 100:
1.11 nicm 101: free(line);
1.1 nicm 102: }
103:
1.14 nicm 104: if (args_has(args, 'p')) {
105: if (c == NULL) {
106: ctx->error(ctx, "can't write to stdout");
107: return (CMD_RETURN_ERROR);
108: }
109: evbuffer_add(c->stdout_data, buf, len);
110: server_push_stdout(c);
111: } else {
112: limit = options_get_number(&global_options, "buffer-limit");
113: if (!args_has(args, 'b')) {
114: paste_add(&global_buffers, buf, len, limit);
115: return (CMD_RETURN_NORMAL);
116: }
117:
118: buffer = args_strtonum(args, 'b', 0, INT_MAX, &cause);
119: if (cause != NULL) {
120: ctx->error(ctx, "buffer %s", cause);
121: free(buf);
122: free(cause);
123: return (CMD_RETURN_ERROR);
124: }
125:
126: if (paste_replace(&global_buffers, buffer, buf, len) != 0) {
127: ctx->error(ctx, "no buffer %d", buffer);
128: free(buf);
129: return (CMD_RETURN_ERROR);
130: }
1.1 nicm 131: }
1.6 nicm 132:
1.12 nicm 133: return (CMD_RETURN_NORMAL);
1.1 nicm 134: }