[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.50

1.50    ! nicm        1: /* $OpenBSD: cmd-capture-pane.c,v 1.49 2019/12/12 11:39:56 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.41      nicm       30: static enum cmd_retval cmd_capture_pane_exec(struct cmd *, struct cmdq_item *);
1.1       nicm       31:
1.41      nicm       32: static char    *cmd_capture_pane_append(char *, size_t *, char *, size_t);
                     33: static char    *cmd_capture_pane_pending(struct args *, struct window_pane *,
                     34:                     size_t *);
                     35: static char    *cmd_capture_pane_history(struct args *, struct cmdq_item *,
                     36:                     struct window_pane *, size_t *);
1.23      nicm       37:
1.1       nicm       38: const struct cmd_entry cmd_capture_pane_entry = {
1.37      nicm       39:        .name = "capture-pane",
                     40:        .alias = "capturep",
                     41:
1.48      nicm       42:        .args = { "ab:CeE:JNpPqS:t:", 0, 0 },
                     43:        .usage = "[-aCeJNpPq] " CMD_BUFFER_USAGE " [-E end-line] "
1.47      nicm       44:                 "[-S start-line] " CMD_TARGET_PANE_USAGE,
1.37      nicm       45:
1.44      nicm       46:        .target = { 't', CMD_FIND_PANE, 0 },
1.38      nicm       47:
1.40      nicm       48:        .flags = CMD_AFTERHOOK,
1.37      nicm       49:        .exec = cmd_capture_pane_exec
1.1       nicm       50: };
                     51:
1.43      nicm       52: const struct cmd_entry cmd_clear_history_entry = {
                     53:        .name = "clear-history",
                     54:        .alias = "clearhist",
                     55:
                     56:        .args = { "t:", 0, 0 },
                     57:        .usage = CMD_TARGET_PANE_USAGE,
                     58:
1.44      nicm       59:        .target = { 't', CMD_FIND_PANE, 0 },
1.43      nicm       60:
                     61:        .flags = CMD_AFTERHOOK,
                     62:        .exec = cmd_capture_pane_exec
                     63: };
                     64:
1.39      nicm       65: static char *
1.23      nicm       66: cmd_capture_pane_append(char *buf, size_t *len, char *line, size_t linelen)
                     67: {
1.29      nicm       68:        buf = xrealloc(buf, *len + linelen + 1);
1.23      nicm       69:        memcpy(buf + *len, line, linelen);
                     70:        *len += linelen;
                     71:        return (buf);
                     72: }
                     73:
1.39      nicm       74: static char *
1.23      nicm       75: cmd_capture_pane_pending(struct args *args, struct window_pane *wp,
                     76:     size_t *len)
                     77: {
1.32      nicm       78:        struct evbuffer *pending;
                     79:        char            *buf, *line, tmp[5];
                     80:        size_t           linelen;
                     81:        u_int            i;
1.23      nicm       82:
1.50    ! nicm       83:        pending = input_pending(wp->ictx);
1.32      nicm       84:        if (pending == NULL)
1.23      nicm       85:                return (xstrdup(""));
                     86:
1.32      nicm       87:        line = EVBUFFER_DATA(pending);
                     88:        linelen = EVBUFFER_LENGTH(pending);
1.23      nicm       89:
1.24      nicm       90:        buf = xstrdup("");
1.23      nicm       91:        if (args_has(args, 'C')) {
                     92:                for (i = 0; i < linelen; i++) {
1.42      nicm       93:                        if (line[i] >= ' ' && line[i] != '\\') {
1.23      nicm       94:                                tmp[0] = line[i];
                     95:                                tmp[1] = '\0';
                     96:                        } else
1.31      nicm       97:                                xsnprintf(tmp, sizeof tmp, "\\%03hho", line[i]);
1.23      nicm       98:                        buf = cmd_capture_pane_append(buf, len, tmp,
                     99:                            strlen(tmp));
                    100:                }
                    101:        } else
                    102:                buf = cmd_capture_pane_append(buf, len, line, linelen);
                    103:        return (buf);
                    104: }
                    105:
1.39      nicm      106: static char *
1.41      nicm      107: cmd_capture_pane_history(struct args *args, struct cmdq_item *item,
1.23      nicm      108:     struct window_pane *wp, size_t *len)
1.1       nicm      109: {
1.7       nicm      110:        struct grid             *gd;
1.18      nicm      111:        const struct grid_line  *gl;
1.23      nicm      112:        struct grid_cell        *gc = NULL;
1.48      nicm      113:        int                      n, with_codes, escape_c0, join_lines, no_trim;
1.23      nicm      114:        u_int                    i, sx, top, bottom, tmp;
                    115:        char                    *cause, *buf, *line;
1.28      nicm      116:        const char              *Sflag, *Eflag;
1.23      nicm      117:        size_t                   linelen;
1.14      nicm      118:
1.23      nicm      119:        sx = screen_size_x(&wp->base);
1.20      nicm      120:        if (args_has(args, 'a')) {
                    121:                gd = wp->saved_grid;
1.23      nicm      122:                if (gd == NULL) {
                    123:                        if (!args_has(args, 'q')) {
1.41      nicm      124:                                cmdq_error(item, "no alternate screen");
1.23      nicm      125:                                return (NULL);
                    126:                        }
                    127:                        return (xstrdup(""));
1.20      nicm      128:                }
1.23      nicm      129:        } else
                    130:                gd = wp->base.grid;
                    131:
1.28      nicm      132:        Sflag = args_get(args, 'S');
                    133:        if (Sflag != NULL && strcmp(Sflag, "-") == 0)
1.23      nicm      134:                top = 0;
1.28      nicm      135:        else {
                    136:                n = args_strtonum(args, 'S', INT_MIN, SHRT_MAX, &cause);
                    137:                if (cause != NULL) {
                    138:                        top = gd->hsize;
                    139:                        free(cause);
                    140:                } else if (n < 0 && (u_int) -n > gd->hsize)
                    141:                        top = 0;
                    142:                else
                    143:                        top = gd->hsize + n;
                    144:                if (top > gd->hsize + gd->sy - 1)
                    145:                        top = gd->hsize + gd->sy - 1;
                    146:        }
1.23      nicm      147:
1.28      nicm      148:        Eflag = args_get(args, 'E');
                    149:        if (Eflag != NULL && strcmp(Eflag, "-") == 0)
1.23      nicm      150:                bottom = gd->hsize + gd->sy - 1;
1.28      nicm      151:        else {
                    152:                n = args_strtonum(args, 'E', INT_MIN, SHRT_MAX, &cause);
                    153:                if (cause != NULL) {
                    154:                        bottom = gd->hsize + gd->sy - 1;
                    155:                        free(cause);
                    156:                } else if (n < 0 && (u_int) -n > gd->hsize)
                    157:                        bottom = 0;
                    158:                else
                    159:                        bottom = gd->hsize + n;
                    160:                if (bottom > gd->hsize + gd->sy - 1)
                    161:                        bottom = gd->hsize + gd->sy - 1;
                    162:        }
1.23      nicm      163:
                    164:        if (bottom < top) {
                    165:                tmp = bottom;
                    166:                bottom = top;
                    167:                top = tmp;
1.20      nicm      168:        }
1.1       nicm      169:
1.23      nicm      170:        with_codes = args_has(args, 'e');
                    171:        escape_c0 = args_has(args, 'C');
                    172:        join_lines = args_has(args, 'J');
1.48      nicm      173:        no_trim = args_has(args, 'N');
1.23      nicm      174:
1.1       nicm      175:        buf = NULL;
1.23      nicm      176:        for (i = top; i <= bottom; i++) {
                    177:                line = grid_string_cells(gd, 0, i, sx, &gc, with_codes,
1.48      nicm      178:                    escape_c0, !join_lines && !no_trim);
1.23      nicm      179:                linelen = strlen(line);
                    180:
                    181:                buf = cmd_capture_pane_append(buf, len, line, linelen);
                    182:
                    183:                gl = grid_peek_line(gd, i);
                    184:                if (!join_lines || !(gl->flags & GRID_LINE_WRAPPED))
                    185:                        buf[(*len)++] = '\n';
                    186:
                    187:                free(line);
                    188:        }
                    189:        return (buf);
                    190: }
1.1       nicm      191:
1.39      nicm      192: static enum cmd_retval
1.41      nicm      193: cmd_capture_pane_exec(struct cmd *self, struct cmdq_item *item)
1.23      nicm      194: {
                    195:        struct args             *args = self->args;
1.49      nicm      196:        struct client           *c = item->client;
1.44      nicm      197:        struct window_pane      *wp = item->target.wp;
1.23      nicm      198:        char                    *buf, *cause;
1.27      nicm      199:        const char              *bufname;
1.23      nicm      200:        size_t                   len;
1.43      nicm      201:
                    202:        if (self->entry == &cmd_clear_history_entry) {
1.46      nicm      203:                window_pane_reset_mode_all(wp);
1.43      nicm      204:                grid_clear_history(wp->base.grid);
                    205:                return (CMD_RETURN_NORMAL);
                    206:        }
1.1       nicm      207:
1.23      nicm      208:        len = 0;
                    209:        if (args_has(args, 'P'))
                    210:                buf = cmd_capture_pane_pending(args, wp, &len);
                    211:        else
1.41      nicm      212:                buf = cmd_capture_pane_history(args, item, wp, &len);
1.23      nicm      213:        if (buf == NULL)
                    214:                return (CMD_RETURN_ERROR);
1.1       nicm      215:
1.14      nicm      216:        if (args_has(args, 'p')) {
1.49      nicm      217:                if (!file_can_print(c)) {
                    218:                        cmdq_error(item, "can't write output to client");
1.33      nicm      219:                        free(buf);
1.14      nicm      220:                        return (CMD_RETURN_ERROR);
                    221:                }
1.49      nicm      222:                file_print_buffer(c, buf, len);
                    223:                if (args_has(args, 'P') && len > 0)
                    224:                        file_print(c, "\n");
1.34      nicm      225:                free(buf);
1.14      nicm      226:        } else {
1.27      nicm      227:                bufname = NULL;
                    228:                if (args_has(args, 'b'))
                    229:                        bufname = args_get(args, 'b');
                    230:
                    231:                if (paste_set(buf, len, bufname, &cause) != 0) {
1.41      nicm      232:                        cmdq_error(item, "%s", cause);
1.33      nicm      233:                        free(cause);
1.14      nicm      234:                        free(buf);
                    235:                        return (CMD_RETURN_ERROR);
                    236:                }
1.1       nicm      237:        }
1.6       nicm      238:
1.12      nicm      239:        return (CMD_RETURN_NORMAL);
1.1       nicm      240: }