xref: /openbsd/usr.bin/tmux/cmd-save-buffer.c (revision 3a775927)
1 /* $OpenBSD: cmd-save-buffer.c,v 1.56 2024/11/22 12:58:05 nicm Exp $ */
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 #include <vis.h>
28 
29 #include "tmux.h"
30 
31 /*
32  * Saves a paste buffer to a file.
33  */
34 
35 static enum cmd_retval	cmd_save_buffer_exec(struct cmd *, struct cmdq_item *);
36 
37 const struct cmd_entry cmd_save_buffer_entry = {
38 	.name = "save-buffer",
39 	.alias = "saveb",
40 
41 	.args = { "ab:", 1, 1, NULL },
42 	.usage = "[-a] " CMD_BUFFER_USAGE " path",
43 
44 	.flags = CMD_AFTERHOOK,
45 	.exec = cmd_save_buffer_exec
46 };
47 
48 const struct cmd_entry cmd_show_buffer_entry = {
49 	.name = "show-buffer",
50 	.alias = "showb",
51 
52 	.args = { "b:", 0, 0, NULL },
53 	.usage = CMD_BUFFER_USAGE,
54 
55 	.flags = CMD_AFTERHOOK,
56 	.exec = cmd_save_buffer_exec
57 };
58 
59 static void
cmd_save_buffer_done(__unused struct client * c,const char * path,int error,__unused int closed,__unused struct evbuffer * buffer,void * data)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 
73 static enum cmd_retval
cmd_save_buffer_exec(struct cmd * self,struct cmdq_item * item)74 cmd_save_buffer_exec(struct cmd *self, struct cmdq_item *item)
75 {
76 	struct args		*args = cmd_get_args(self);
77 	struct client		*c = cmdq_get_client(item);
78 	struct paste_buffer	*pb;
79 	int			 flags;
80 	const char		*bufname = args_get(args, 'b'), *bufdata;
81 	size_t			 bufsize;
82 	char			*path;
83 	struct evbuffer		*evb;
84 
85 	if (bufname == NULL) {
86 		if ((pb = paste_get_top(NULL)) == NULL) {
87 			cmdq_error(item, "no buffers");
88 			return (CMD_RETURN_ERROR);
89 		}
90 	} else {
91 		pb = paste_get_name(bufname);
92 		if (pb == NULL) {
93 			cmdq_error(item, "no buffer %s", bufname);
94 			return (CMD_RETURN_ERROR);
95 		}
96 	}
97 	bufdata = paste_buffer_data(pb, &bufsize);
98 
99 	if (cmd_get_entry(self) == &cmd_show_buffer_entry) {
100 		if (c->session != NULL || (c->flags & CLIENT_CONTROL)) {
101 			evb = evbuffer_new();
102 			if (evb == NULL)
103 				fatalx("out of memory");
104 			evbuffer_add(evb, bufdata, bufsize);
105 			cmdq_print_data(item, evb);
106 			evbuffer_free(evb);
107 			return (CMD_RETURN_NORMAL);
108 		}
109 		path = xstrdup("-");
110 	} else
111 		path = format_single_from_target(item, args_string(args, 0));
112 	if (args_has(args, 'a'))
113 		flags = O_APPEND;
114 	else
115 		flags = O_TRUNC;
116 	file_write(cmdq_get_client(item), path, flags, bufdata, bufsize,
117 	    cmd_save_buffer_done, item);
118 	free(path);
119 
120 	return (CMD_RETURN_WAIT);
121 }
122