Annotation of src/usr.bin/tmux/cmd-save-buffer.c, Revision 1.47
1.47 ! nicm 1: /* $OpenBSD: cmd-save-buffer.c,v 1.46 2019/06/13 21:44:13 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.47 ! nicm 59: static void
! 60: cmd_save_buffer_done(__unused struct client *c, const char *path, int error,
! 61: __unused int closed, __unused struct evbuffer *buffer, void *data)
! 62: {
! 63: struct cmdq_item *item = data;
! 64:
! 65: if (!closed)
! 66: return;
! 67:
! 68: if (error != 0)
! 69: cmdq_error(item, "%s: %s", path, strerror(error));
! 70: cmdq_continue(item);
! 71: }
! 72:
1.38 nicm 73: static enum cmd_retval
1.40 nicm 74: cmd_save_buffer_exec(struct cmd *self, struct cmdq_item *item)
1.1 nicm 75: {
1.10 nicm 76: struct args *args = self->args;
1.44 nicm 77: struct client *c = cmd_find_client(item, NULL, 1);
78: struct session *s = item->target.s;
79: struct winlink *wl = item->target.wl;
80: struct window_pane *wp = item->target.wp;
1.1 nicm 81: struct paste_buffer *pb;
1.47 ! nicm 82: int flags;
! 83: const char *bufname = args_get(args, 'b'), *bufdata;
! 84: size_t bufsize;
! 85: char *path;
1.1 nicm 86:
1.47 ! nicm 87: if (bufname == NULL) {
1.29 nicm 88: if ((pb = paste_get_top(NULL)) == NULL) {
1.40 nicm 89: cmdq_error(item, "no buffers");
1.16 nicm 90: return (CMD_RETURN_ERROR);
1.1 nicm 91: }
92: } else {
1.25 nicm 93: pb = paste_get_name(bufname);
1.9 nicm 94: if (pb == NULL) {
1.40 nicm 95: cmdq_error(item, "no buffer %s", bufname);
1.16 nicm 96: return (CMD_RETURN_ERROR);
1.1 nicm 97: }
98: }
1.29 nicm 99: bufdata = paste_buffer_data(pb, &bufsize);
1.1 nicm 100:
1.18 nicm 101: if (self->entry == &cmd_show_buffer_entry)
1.44 nicm 102: path = xstrdup("-");
1.18 nicm 103: else
1.44 nicm 104: path = format_single(item, args->argv[0], c, s, wl, wp);
1.31 nicm 105: if (args_has(self->args, 'a'))
1.47 ! nicm 106: flags = O_APPEND;
! 107: else
! 108: flags = 0;
! 109: file_write(item->client, path, flags, bufdata, bufsize,
! 110: cmd_save_buffer_done, item);
1.45 nicm 111: free(path);
112:
1.47 ! nicm 113: return (CMD_RETURN_WAIT);
1.1 nicm 114: }