cmd-save-buffer.c revision 1.22
1/* $OpenBSD: cmd-save-buffer.c,v 1.22 2014/04/07 10:32:16 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
35enum cmd_retval	 cmd_save_buffer_exec(struct cmd *, struct cmd_q *);
36
37const 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
46const 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
55enum cmd_retval
56cmd_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;
63	char			*cause, *start, *end, *msg;
64	size_t			 size, used, msglen;
65	int			 cwd, fd, buffer;
66	FILE			*f;
67
68	if (!args_has(args, 'b')) {
69		if ((pb = paste_get_top(&global_buffers)) == NULL) {
70			cmdq_error(cmdq, "no buffers");
71			return (CMD_RETURN_ERROR);
72		}
73	} else {
74		buffer = args_strtonum(args, 'b', 0, INT_MAX, &cause);
75		if (cause != NULL) {
76			cmdq_error(cmdq, "buffer %s", cause);
77			free(cause);
78			return (CMD_RETURN_ERROR);
79		}
80
81		pb = paste_get_index(&global_buffers, buffer);
82		if (pb == NULL) {
83			cmdq_error(cmdq, "no buffer %d", buffer);
84			return (CMD_RETURN_ERROR);
85		}
86	}
87
88	if (self->entry == &cmd_show_buffer_entry)
89		path = "-";
90	else
91		path = args->argv[0];
92	if (strcmp(path, "-") == 0) {
93		if (c == NULL) {
94			cmdq_error(cmdq, "can't write to stdout");
95			return (CMD_RETURN_ERROR);
96		}
97		if (c->session == NULL || (c->flags & CLIENT_CONTROL))
98			goto do_stdout;
99		goto do_print;
100	}
101
102	if (c != NULL && c->session == NULL)
103		cwd = c->cwd;
104	else if ((s = cmd_current_session(cmdq, 0)) != NULL)
105		cwd = s->cwd;
106	else
107		cwd = AT_FDCWD;
108
109	f = NULL;
110	if (args_has(self->args, 'a')) {
111		fd = openat(cwd, path, O_CREAT|O_RDWR|O_APPEND, 0600);
112		if (fd != -1)
113			f = fdopen(fd, "ab");
114	} else {
115		fd = openat(cwd, path, O_CREAT|O_RDWR|O_TRUNC, 0600);
116		if (fd != -1)
117			f = fdopen(fd, "wb");
118	}
119	if (f == NULL) {
120		if (fd != -1)
121			close(fd);
122		cmdq_error(cmdq, "%s: %s", path, strerror(errno));
123		return (CMD_RETURN_ERROR);
124	}
125	if (fwrite(pb->data, 1, pb->size, f) != pb->size) {
126		cmdq_error(cmdq, "%s: fwrite error", path);
127		fclose(f);
128		return (CMD_RETURN_ERROR);
129	}
130	fclose(f);
131
132	return (CMD_RETURN_NORMAL);
133
134do_stdout:
135	evbuffer_add(c->stdout_data, pb->data, pb->size);
136	server_push_stdout(c);
137	return (CMD_RETURN_NORMAL);
138
139do_print:
140	if (pb->size > (INT_MAX / 4) - 1) {
141		cmdq_error(cmdq, "buffer too big");
142		return (CMD_RETURN_ERROR);
143	}
144	msg = NULL;
145	msglen = 0;
146
147	used = 0;
148	while (used != pb->size) {
149		start = pb->data + used;
150		end = memchr(start, '\n', pb->size - used);
151		if (end != NULL)
152			size = end - start;
153		else
154			size = pb->size - used;
155
156		msglen = size * 4 + 1;
157		msg = xrealloc(msg, 1, msglen);
158
159		strvisx(msg, start, size, VIS_OCTAL|VIS_TAB);
160		cmdq_print(cmdq, "%s", msg);
161
162		used += size + (end != NULL);
163	}
164
165	free(msg);
166	return (CMD_RETURN_NORMAL);
167}
168