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