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