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

1.4     ! nicm        1: /* $OpenBSD: cmd-capture-pane.c,v 1.3 2010/01/20 18:30:20 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: /*
                     27:  * Write the entire contents of a pane to a buffer.
                     28:  */
                     29:
1.4     ! nicm       30: int    cmd_capture_pane_parse(struct cmd *, int, char **, char **);
1.1       nicm       31: int    cmd_capture_pane_exec(struct cmd *, struct cmd_ctx *);
1.4     ! nicm       32: void   cmd_capture_pane_free(struct cmd *);
        !            33: void   cmd_capture_pane_init(struct cmd *, int);
        !            34: size_t cmd_capture_pane_print(struct cmd *, char *, size_t);
        !            35:
        !            36: struct cmd_capture_pane_data {
        !            37:        char    *target;
        !            38:        int      buffer;
        !            39: };
1.1       nicm       40:
                     41: const struct cmd_entry cmd_capture_pane_entry = {
                     42:        "capture-pane", "capturep",
                     43:        CMD_BUFFER_PANE_USAGE,
                     44:        0, "",
1.4     ! nicm       45:        cmd_capture_pane_init,
        !            46:        cmd_capture_pane_parse,
1.1       nicm       47:        cmd_capture_pane_exec,
1.4     ! nicm       48:        cmd_capture_pane_free,
        !            49:        cmd_capture_pane_print
1.1       nicm       50: };
                     51:
1.4     ! nicm       52: /* ARGSUSED */
        !            53: void
        !            54: cmd_capture_pane_init(struct cmd *self, unused int arg)
        !            55: {
        !            56:        struct cmd_capture_pane_data    *data;
        !            57:
        !            58:        self->data = data = xmalloc(sizeof *data);
        !            59:        data->buffer = -1;
        !            60:        data->target = NULL;
        !            61: }
        !            62:
        !            63: int
        !            64: cmd_capture_pane_parse(struct cmd *self, int argc, char **argv, char **cause)
        !            65: {
        !            66:        struct cmd_capture_pane_data    *data;
        !            67:        const char                      *errstr;
        !            68:        int                              n, opt;
        !            69:
        !            70:        self->entry->init(self, KEYC_NONE);
        !            71:        data = self->data;
        !            72:
        !            73:        while ((opt = getopt(argc, argv, "b:t:")) != -1) {
        !            74:                switch (opt) {
        !            75:                case 'b':
        !            76:                        if (data->buffer == -1) {
        !            77:                                n = strtonum(optarg, 0, INT_MAX, &errstr);
        !            78:                                if (errstr != NULL) {
        !            79:                                        xasprintf(cause, "buffer %s", errstr);
        !            80:                                        goto error;
        !            81:                                }
        !            82:                                data->buffer = n;
        !            83:                        }
        !            84:                        break;
        !            85:                case 't':
        !            86:                        if (data->target == NULL)
        !            87:                                data->target = xstrdup(optarg);
        !            88:                        break;
        !            89:                default:
        !            90:                        goto usage;
        !            91:                }
        !            92:        }
        !            93:        argc -= optind;
        !            94:        argv += optind;
        !            95:
        !            96:        return (0);
        !            97:
        !            98: usage:
        !            99:        xasprintf(cause, "usage: %s %s", self->entry->name, self->entry->usage);
        !           100:
        !           101: error:
        !           102:        self->entry->free(self);
        !           103:        return (-1);
        !           104: }
        !           105:
1.1       nicm      106: int
                    107: cmd_capture_pane_exec(struct cmd *self, struct cmd_ctx *ctx)
                    108: {
1.4     ! nicm      109:        struct cmd_capture_pane_data    *data = self->data;
        !           110:        struct window_pane              *wp;
        !           111:        char                            *buf, *line;
        !           112:        struct screen                   *s;
        !           113:        u_int                            i, limit;
        !           114:        size_t                           len, linelen;
1.1       nicm      115:
1.4     ! nicm      116:        if (cmd_find_pane(ctx, data->target, NULL, &wp) == NULL)
1.1       nicm      117:                return (-1);
                    118:        s = &wp->base;
                    119:
                    120:        buf = NULL;
                    121:        len = 0;
                    122:
                    123:        for (i = 0; i < screen_size_y(s); i++) {
                    124:               line = grid_view_string_cells(s->grid, 0, i, screen_size_x(s));
                    125:               linelen = strlen(line);
                    126:
                    127:               buf = xrealloc(buf, 1, len + linelen + 1);
                    128:               memcpy(buf + len, line, linelen);
                    129:               len += linelen;
                    130:               buf[len++] = '\n';
1.3       nicm      131:
                    132:               xfree(line);
1.1       nicm      133:        }
                    134:
1.4     ! nicm      135:        limit = options_get_number(&global_options, "buffer-limit");
1.1       nicm      136:        if (data->buffer == -1) {
1.4     ! nicm      137:                paste_add(&global_buffers, buf, len, limit);
1.1       nicm      138:                return (0);
                    139:        }
1.4     ! nicm      140:        if (paste_replace(&global_buffers, data->buffer, buf, len) != 0) {
1.1       nicm      141:                ctx->error(ctx, "no buffer %d", data->buffer);
                    142:                xfree(buf);
                    143:                return (-1);
                    144:        }
                    145:        return (0);
1.4     ! nicm      146: }
        !           147:
        !           148: void
        !           149: cmd_capture_pane_free(struct cmd *self)
        !           150: {
        !           151:        struct cmd_capture_pane_data    *data = self->data;
        !           152:
        !           153:        if (data->target != NULL)
        !           154:                xfree(data->target);
        !           155:        xfree(data);
        !           156: }
        !           157:
        !           158: size_t
        !           159: cmd_capture_pane_print(struct cmd *self, char *buf, size_t len)
        !           160: {
        !           161:        struct cmd_capture_pane_data    *data = self->data;
        !           162:        size_t                           off = 0;
        !           163:
        !           164:        off += xsnprintf(buf, len, "%s", self->entry->name);
        !           165:        if (data == NULL)
        !           166:                return (off);
        !           167:        if (off < len && data->buffer != -1)
        !           168:                off += xsnprintf(buf + off, len - off, " -b %d", data->buffer);
        !           169:        if (off < len && data->target != NULL)
        !           170:                off += xsnprintf(buf + off, len - off, " -t %s", data->target);
        !           171:        return (off);
1.1       nicm      172: }