xref: /openbsd-src/usr.bin/tmux/cmd-save-buffer.c (revision d1df930ffab53da22f3324c32bed7ac5709915e6)
1 /* $OpenBSD: cmd-save-buffer.c,v 1.45 2018/07/31 13:06:44 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 },
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 },
53 	.usage = CMD_BUFFER_USAGE,
54 
55 	.flags = CMD_AFTERHOOK,
56 	.exec = cmd_save_buffer_exec
57 };
58 
59 static enum cmd_retval
60 cmd_save_buffer_exec(struct cmd *self, struct cmdq_item *item)
61 {
62 	struct args		*args = self->args;
63 	struct client		*c = cmd_find_client(item, NULL, 1);
64 	struct session		*s = item->target.s;
65 	struct winlink		*wl = item->target.wl;
66 	struct window_pane	*wp = item->target.wp;
67 	struct paste_buffer	*pb;
68 	const char		*bufname, *bufdata, *start, *end, *flags;
69 	char			*msg, *path, *file;
70 	size_t			 size, used, msglen, bufsize;
71 	FILE			*f;
72 
73 	if (!args_has(args, 'b')) {
74 		if ((pb = paste_get_top(NULL)) == NULL) {
75 			cmdq_error(item, "no buffers");
76 			return (CMD_RETURN_ERROR);
77 		}
78 	} else {
79 		bufname = args_get(args, 'b');
80 		pb = paste_get_name(bufname);
81 		if (pb == NULL) {
82 			cmdq_error(item, "no buffer %s", bufname);
83 			return (CMD_RETURN_ERROR);
84 		}
85 	}
86 	bufdata = paste_buffer_data(pb, &bufsize);
87 
88 	if (self->entry == &cmd_show_buffer_entry)
89 		path = xstrdup("-");
90 	else
91 		path = format_single(item, args->argv[0], c, s, wl, wp);
92 	if (strcmp(path, "-") == 0) {
93 		free(path);
94 		c = item->client;
95 		if (c == NULL) {
96 			cmdq_error(item, "can't write to stdout");
97 			return (CMD_RETURN_ERROR);
98 		}
99 		if (c->session == NULL || (c->flags & CLIENT_CONTROL))
100 			goto do_stdout;
101 		goto do_print;
102 	}
103 
104 	flags = "wb";
105 	if (args_has(self->args, 'a'))
106 		flags = "ab";
107 
108 	file = server_client_get_path(c, path);
109 	free(path);
110 
111 	f = fopen(file, flags);
112 	if (f == NULL) {
113 		cmdq_error(item, "%s: %s", file, strerror(errno));
114 		free(file);
115 		return (CMD_RETURN_ERROR);
116 	}
117 
118 	if (fwrite(bufdata, 1, bufsize, f) != bufsize) {
119 		cmdq_error(item, "%s: write error", file);
120 		fclose(f);
121 		free(file);
122 		return (CMD_RETURN_ERROR);
123 	}
124 
125 	fclose(f);
126 	free(file);
127 
128 	return (CMD_RETURN_NORMAL);
129 
130 do_stdout:
131 	evbuffer_add(c->stdout_data, bufdata, bufsize);
132 	server_client_push_stdout(c);
133 	return (CMD_RETURN_NORMAL);
134 
135 do_print:
136 	if (bufsize > (INT_MAX / 4) - 1) {
137 		cmdq_error(item, "buffer too big");
138 		return (CMD_RETURN_ERROR);
139 	}
140 	msg = NULL;
141 
142 	used = 0;
143 	while (used != bufsize) {
144 		start = bufdata + used;
145 		end = memchr(start, '\n', bufsize - used);
146 		if (end != NULL)
147 			size = end - start;
148 		else
149 			size = bufsize - used;
150 
151 		msglen = size * 4 + 1;
152 		msg = xrealloc(msg, msglen);
153 
154 		strvisx(msg, start, size, VIS_OCTAL|VIS_TAB);
155 		cmdq_print(item, "%s", msg);
156 
157 		used += size + (end != NULL);
158 	}
159 
160 	free(msg);
161 	return (CMD_RETURN_NORMAL);
162 }
163