xref: /openbsd/usr.bin/tmux/cmd-save-buffer.c (revision 8529ddd3)
1 /* $OpenBSD: cmd-save-buffer.c,v 1.28 2015/04/27 16:25:57 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 	cmd_save_buffer_exec
43 };
44 
45 const struct cmd_entry cmd_show_buffer_entry = {
46 	"show-buffer", "showb",
47 	"b:", 0, 0,
48 	CMD_BUFFER_USAGE,
49 	0,
50 	cmd_save_buffer_exec
51 };
52 
53 enum cmd_retval
54 cmd_save_buffer_exec(struct cmd *self, struct cmd_q *cmdq)
55 {
56 	struct args		*args = self->args;
57 	struct client		*c = cmdq->client;
58 	struct session          *s;
59 	struct paste_buffer	*pb;
60 	const char		*path, *bufname;
61 	char			*start, *end, *msg;
62 	size_t			 size, used, msglen;
63 	int			 cwd, fd;
64 	FILE			*f;
65 
66 	if (!args_has(args, 'b')) {
67 		if ((pb = paste_get_top()) == NULL) {
68 			cmdq_error(cmdq, "no buffers");
69 			return (CMD_RETURN_ERROR);
70 		}
71 	} else {
72 		bufname = args_get(args, 'b');
73 		pb = paste_get_name(bufname);
74 		if (pb == NULL) {
75 			cmdq_error(cmdq, "no buffer %s", bufname);
76 			return (CMD_RETURN_ERROR);
77 		}
78 	}
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(pb->data, 1, pb->size, f) != pb->size) {
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, pb->data, pb->size);
128 	server_push_stdout(c);
129 	return (CMD_RETURN_NORMAL);
130 
131 do_print:
132 	if (pb->size > (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 != pb->size) {
140 		start = pb->data + used;
141 		end = memchr(start, '\n', pb->size - used);
142 		if (end != NULL)
143 			size = end - start;
144 		else
145 			size = pb->size - 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