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

Annotation of src/usr.bin/tmux/cmd-save-buffer.c, Revision 1.42

1.42    ! nicm        1: /* $OpenBSD: cmd-save-buffer.c,v 1.41 2017/01/06 13:26:09 nicm Exp $ */
1.1       nicm        2:
                      3: /*
                      4:  * Copyright (c) 2009 Tiago Cunha <me@tiagocunha.org>
                      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: #include <sys/stat.h>
                     21:
                     22: #include <errno.h>
1.21      nicm       23: #include <fcntl.h>
1.15      nicm       24: #include <stdlib.h>
1.1       nicm       25: #include <string.h>
1.21      nicm       26: #include <unistd.h>
1.18      nicm       27: #include <vis.h>
1.1       nicm       28:
                     29: #include "tmux.h"
                     30:
                     31: /*
1.11      nicm       32:  * Saves a paste buffer to a file.
1.1       nicm       33:  */
                     34:
1.40      nicm       35: static enum cmd_retval cmd_save_buffer_exec(struct cmd *, struct cmdq_item *);
1.1       nicm       36:
                     37: const struct cmd_entry cmd_save_buffer_entry = {
1.36      nicm       38:        .name = "save-buffer",
                     39:        .alias = "saveb",
                     40:
                     41:        .args = { "ab:", 1, 1 },
                     42:        .usage = "[-a] " CMD_BUFFER_USAGE " path",
                     43:
1.39      nicm       44:        .flags = CMD_AFTERHOOK,
1.36      nicm       45:        .exec = cmd_save_buffer_exec
1.1       nicm       46: };
                     47:
1.18      nicm       48: const struct cmd_entry cmd_show_buffer_entry = {
1.36      nicm       49:        .name = "show-buffer",
                     50:        .alias = "showb",
                     51:
                     52:        .args = { "b:", 0, 0 },
                     53:        .usage = CMD_BUFFER_USAGE,
                     54:
1.39      nicm       55:        .flags = CMD_AFTERHOOK,
1.36      nicm       56:        .exec = cmd_save_buffer_exec
1.18      nicm       57: };
                     58:
1.38      nicm       59: static enum cmd_retval
1.40      nicm       60: cmd_save_buffer_exec(struct cmd *self, struct cmdq_item *item)
1.1       nicm       61: {
1.10      nicm       62:        struct args             *args = self->args;
1.40      nicm       63:        struct client           *c = item->client;
1.1       nicm       64:        struct paste_buffer     *pb;
1.42    ! nicm       65:        const char              *path, *bufname, *bufdata, *start, *end;
1.31      nicm       66:        const char              *flags;
1.42    ! nicm       67:        char                    *msg, *file;
1.29      nicm       68:        size_t                   size, used, msglen, bufsize;
1.8       nicm       69:        FILE                    *f;
1.1       nicm       70:
1.10      nicm       71:        if (!args_has(args, 'b')) {
1.29      nicm       72:                if ((pb = paste_get_top(NULL)) == NULL) {
1.40      nicm       73:                        cmdq_error(item, "no buffers");
1.16      nicm       74:                        return (CMD_RETURN_ERROR);
1.1       nicm       75:                }
                     76:        } else {
1.25      nicm       77:                bufname = args_get(args, 'b');
                     78:                pb = paste_get_name(bufname);
1.9       nicm       79:                if (pb == NULL) {
1.40      nicm       80:                        cmdq_error(item, "no buffer %s", bufname);
1.16      nicm       81:                        return (CMD_RETURN_ERROR);
1.1       nicm       82:                }
                     83:        }
1.29      nicm       84:        bufdata = paste_buffer_data(pb, &bufsize);
1.1       nicm       85:
1.18      nicm       86:        if (self->entry == &cmd_show_buffer_entry)
                     87:                path = "-";
                     88:        else
                     89:                path = args->argv[0];
1.10      nicm       90:        if (strcmp(path, "-") == 0) {
1.19      nicm       91:                if (c == NULL) {
1.40      nicm       92:                        cmdq_error(item, "can't write to stdout");
1.19      nicm       93:                        return (CMD_RETURN_ERROR);
                     94:                }
                     95:                if (c->session == NULL || (c->flags & CLIENT_CONTROL))
1.18      nicm       96:                        goto do_stdout;
1.19      nicm       97:                goto do_print;
1.18      nicm       98:        }
                     99:
1.31      nicm      100:        flags = "wb";
                    101:        if (args_has(self->args, 'a'))
                    102:                flags = "ab";
                    103:
1.42    ! nicm      104:        file = server_client_get_path(c, path);
        !           105:        f = fopen(file, flags);
1.18      nicm      106:        if (f == NULL) {
1.42    ! nicm      107:                cmdq_error(item, "%s: %s", file, strerror(errno));
        !           108:                free(file);
1.18      nicm      109:                return (CMD_RETURN_ERROR);
                    110:        }
1.31      nicm      111:
1.29      nicm      112:        if (fwrite(bufdata, 1, bufsize, f) != bufsize) {
1.42    ! nicm      113:                cmdq_error(item, "%s: write error", file);
1.18      nicm      114:                fclose(f);
                    115:                return (CMD_RETURN_ERROR);
                    116:        }
1.42    ! nicm      117:
1.18      nicm      118:        fclose(f);
1.42    ! nicm      119:        free(file);
1.18      nicm      120:
                    121:        return (CMD_RETURN_NORMAL);
                    122:
                    123: do_stdout:
1.29      nicm      124:        evbuffer_add(c->stdout_data, bufdata, bufsize);
1.34      nicm      125:        server_client_push_stdout(c);
1.18      nicm      126:        return (CMD_RETURN_NORMAL);
                    127:
                    128: do_print:
1.29      nicm      129:        if (bufsize > (INT_MAX / 4) - 1) {
1.40      nicm      130:                cmdq_error(item, "buffer too big");
1.18      nicm      131:                return (CMD_RETURN_ERROR);
                    132:        }
                    133:        msg = NULL;
1.12      nicm      134:
1.18      nicm      135:        used = 0;
1.29      nicm      136:        while (used != bufsize) {
                    137:                start = bufdata + used;
                    138:                end = memchr(start, '\n', bufsize - used);
1.18      nicm      139:                if (end != NULL)
                    140:                        size = end - start;
1.7       nicm      141:                else
1.29      nicm      142:                        size = bufsize - used;
1.18      nicm      143:
                    144:                msglen = size * 4 + 1;
1.26      nicm      145:                msg = xrealloc(msg, msglen);
1.18      nicm      146:
                    147:                strvisx(msg, start, size, VIS_OCTAL|VIS_TAB);
1.40      nicm      148:                cmdq_print(item, "%s", msg);
1.18      nicm      149:
                    150:                used += size + (end != NULL);
1.1       nicm      151:        }
                    152:
1.18      nicm      153:        free(msg);
1.16      nicm      154:        return (CMD_RETURN_NORMAL);
1.1       nicm      155: }