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