xref: /netbsd-src/external/bsd/tmux/dist/cmd-save-buffer.c (revision 627f7eb200a4419d89b531d55fccd2ee3ffdcde0)
1 /* $OpenBSD$ */
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>
23 #include <fcntl.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27 
28 #include "tmux.h"
29 
30 /*
31  * Saves a paste buffer to a file.
32  */
33 
34 static enum cmd_retval	cmd_save_buffer_exec(struct cmd *, struct cmdq_item *);
35 
36 const struct cmd_entry cmd_save_buffer_entry = {
37 	.name = "save-buffer",
38 	.alias = "saveb",
39 
40 	.args = { "ab:", 1, 1 },
41 	.usage = "[-a] " CMD_BUFFER_USAGE " path",
42 
43 	.flags = CMD_AFTERHOOK,
44 	.exec = cmd_save_buffer_exec
45 };
46 
47 const struct cmd_entry cmd_show_buffer_entry = {
48 	.name = "show-buffer",
49 	.alias = "showb",
50 
51 	.args = { "b:", 0, 0 },
52 	.usage = CMD_BUFFER_USAGE,
53 
54 	.flags = CMD_AFTERHOOK,
55 	.exec = cmd_save_buffer_exec
56 };
57 
58 static void
59 cmd_save_buffer_done(__unused struct client *c, const char *path, int error,
60     __unused int closed, __unused struct evbuffer *buffer, void *data)
61 {
62 	struct cmdq_item	*item = data;
63 
64 	if (!closed)
65 		return;
66 
67 	if (error != 0)
68 		cmdq_error(item, "%s: %s", path, strerror(error));
69 	cmdq_continue(item);
70 }
71 
72 static enum cmd_retval
73 cmd_save_buffer_exec(struct cmd *self, struct cmdq_item *item)
74 {
75 	struct args		*args = self->args;
76 	struct client		*c = cmd_find_client(item, NULL, 1);
77 	struct session		*s = item->target.s;
78 	struct winlink		*wl = item->target.wl;
79 	struct window_pane	*wp = item->target.wp;
80 	struct paste_buffer	*pb;
81 	int			 flags;
82 	const char		*bufname = args_get(args, 'b'), *bufdata;
83 	size_t			 bufsize;
84 	char			*path;
85 
86 	if (bufname == NULL) {
87 		if ((pb = paste_get_top(NULL)) == NULL) {
88 			cmdq_error(item, "no buffers");
89 			return (CMD_RETURN_ERROR);
90 		}
91 	} else {
92 		pb = paste_get_name(bufname);
93 		if (pb == NULL) {
94 			cmdq_error(item, "no buffer %s", bufname);
95 			return (CMD_RETURN_ERROR);
96 		}
97 	}
98 	bufdata = paste_buffer_data(pb, &bufsize);
99 
100 	if (self->entry == &cmd_show_buffer_entry)
101 		path = xstrdup("-");
102 	else
103 		path = format_single(item, args->argv[0], c, s, wl, wp);
104 	if (args_has(self->args, 'a'))
105 		flags = O_APPEND;
106 	else
107 		flags = 0;
108 	file_write(item->client, path, flags, bufdata, bufsize,
109 	    cmd_save_buffer_done, item);
110 	free(path);
111 
112 	return (CMD_RETURN_WAIT);
113 }
114