[BACK]Return to cmd-capture-pane.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / tmux

Annotation of src/usr.bin/tmux/cmd-capture-pane.c, Revision 1.28

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