xref: /openbsd-src/usr.bin/tmux/cmd-save-buffer.c (revision 64cf113cfda174a3f12284d623b95b76b54db679)
1 /* $OpenBSD: cmd-save-buffer.c,v 1.26 2014/10/08 17:35:58 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 enum cmd_retval	 cmd_save_buffer_exec(struct cmd *, struct cmd_q *);
36 
37 const struct cmd_entry cmd_save_buffer_entry = {
38 	"save-buffer", "saveb",
39 	"ab:", 1, 1,
40 	"[-a] " CMD_BUFFER_USAGE " path",
41 	0,
42 	NULL,
43 	cmd_save_buffer_exec
44 };
45 
46 const struct cmd_entry cmd_show_buffer_entry = {
47 	"show-buffer", "showb",
48 	"b:", 0, 0,
49 	CMD_BUFFER_USAGE,
50 	0,
51 	NULL,
52 	cmd_save_buffer_exec
53 };
54 
55 enum cmd_retval
56 cmd_save_buffer_exec(struct cmd *self, struct cmd_q *cmdq)
57 {
58 	struct args		*args = self->args;
59 	struct client		*c = cmdq->client;
60 	struct session          *s;
61 	struct paste_buffer	*pb;
62 	const char		*path, *bufname;
63 	char			*start, *end, *msg;
64 	size_t			 size, used, msglen;
65 	int			 cwd, fd;
66 	FILE			*f;
67 
68 	if (!args_has(args, 'b')) {
69 		if ((pb = paste_get_top()) == NULL) {
70 			cmdq_error(cmdq, "no buffers");
71 			return (CMD_RETURN_ERROR);
72 		}
73 	} else {
74 		bufname = args_get(args, 'b');
75 		pb = paste_get_name(bufname);
76 		if (pb == NULL) {
77 			cmdq_error(cmdq, "no buffer %s", bufname);
78 			return (CMD_RETURN_ERROR);
79 		}
80 	}
81 
82 	if (self->entry == &cmd_show_buffer_entry)
83 		path = "-";
84 	else
85 		path = args->argv[0];
86 	if (strcmp(path, "-") == 0) {
87 		if (c == NULL) {
88 			cmdq_error(cmdq, "can't write to stdout");
89 			return (CMD_RETURN_ERROR);
90 		}
91 		if (c->session == NULL || (c->flags & CLIENT_CONTROL))
92 			goto do_stdout;
93 		goto do_print;
94 	}
95 
96 	if (c != NULL && c->session == NULL)
97 		cwd = c->cwd;
98 	else if ((s = cmd_current_session(cmdq, 0)) != NULL)
99 		cwd = s->cwd;
100 	else
101 		cwd = AT_FDCWD;
102 
103 	f = NULL;
104 	if (args_has(self->args, 'a')) {
105 		fd = openat(cwd, path, O_CREAT|O_RDWR|O_APPEND, 0600);
106 		if (fd != -1)
107 			f = fdopen(fd, "ab");
108 	} else {
109 		fd = openat(cwd, path, O_CREAT|O_RDWR|O_TRUNC, 0600);
110 		if (fd != -1)
111 			f = fdopen(fd, "wb");
112 	}
113 	if (f == NULL) {
114 		if (fd != -1)
115 			close(fd);
116 		cmdq_error(cmdq, "%s: %s", path, strerror(errno));
117 		return (CMD_RETURN_ERROR);
118 	}
119 	if (fwrite(pb->data, 1, pb->size, f) != pb->size) {
120 		cmdq_error(cmdq, "%s: fwrite error", path);
121 		fclose(f);
122 		return (CMD_RETURN_ERROR);
123 	}
124 	fclose(f);
125 
126 	return (CMD_RETURN_NORMAL);
127 
128 do_stdout:
129 	evbuffer_add(c->stdout_data, pb->data, pb->size);
130 	server_push_stdout(c);
131 	return (CMD_RETURN_NORMAL);
132 
133 do_print:
134 	if (pb->size > (INT_MAX / 4) - 1) {
135 		cmdq_error(cmdq, "buffer too big");
136 		return (CMD_RETURN_ERROR);
137 	}
138 	msg = NULL;
139 
140 	used = 0;
141 	while (used != pb->size) {
142 		start = pb->data + used;
143 		end = memchr(start, '\n', pb->size - used);
144 		if (end != NULL)
145 			size = end - start;
146 		else
147 			size = pb->size - used;
148 
149 		msglen = size * 4 + 1;
150 		msg = xrealloc(msg, msglen);
151 
152 		strvisx(msg, start, size, VIS_OCTAL|VIS_TAB);
153 		cmdq_print(cmdq, "%s", msg);
154 
155 		used += size + (end != NULL);
156 	}
157 
158 	free(msg);
159 	return (CMD_RETURN_NORMAL);
160 }
161