cmd-save-buffer.c revision 1.35
1/* $OpenBSD: cmd-save-buffer.c,v 1.35 2015/12/13 14:32:38 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	cmd_save_buffer_exec
43};
44
45const 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
53enum cmd_retval
54cmd_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, *bufdata, *start, *end, *cwd;
61	const char		*flags;
62	char			*msg, *file, resolved[PATH_MAX];
63	size_t			 size, used, msglen, bufsize;
64	FILE			*f;
65
66	if (!args_has(args, 'b')) {
67		if ((pb = paste_get_top(NULL)) == 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	bufdata = paste_buffer_data(pb, &bufsize);
80
81	if (self->entry == &cmd_show_buffer_entry)
82		path = "-";
83	else
84		path = args->argv[0];
85	if (strcmp(path, "-") == 0) {
86		if (c == NULL) {
87			cmdq_error(cmdq, "can't write to stdout");
88			return (CMD_RETURN_ERROR);
89		}
90		if (c->session == NULL || (c->flags & CLIENT_CONTROL))
91			goto do_stdout;
92		goto do_print;
93	}
94
95	if (c != NULL && c->session == NULL)
96		cwd = c->cwd;
97	else if ((s = c->session) != NULL)
98		cwd = s->cwd;
99	else
100		cwd = ".";
101
102	flags = "wb";
103	if (args_has(self->args, 'a'))
104		flags = "ab";
105
106	if (*path == '/')
107		file = xstrdup(path);
108	else
109		xasprintf(&file, "%s/%s", cwd, path);
110	if (realpath(file, resolved) == NULL &&
111	    strlcpy(resolved, file, sizeof resolved) >= sizeof resolved) {
112		cmdq_error(cmdq, "%s: %s", file, strerror(ENAMETOOLONG));
113		return (CMD_RETURN_ERROR);
114	}
115	f = fopen(resolved, flags);
116	free(file);
117	if (f == NULL) {
118		cmdq_error(cmdq, "%s: %s", resolved, strerror(errno));
119		return (CMD_RETURN_ERROR);
120	}
121
122	if (fwrite(bufdata, 1, bufsize, f) != bufsize) {
123		cmdq_error(cmdq, "%s: write error", resolved);
124		fclose(f);
125		return (CMD_RETURN_ERROR);
126	}
127	fclose(f);
128
129	return (CMD_RETURN_NORMAL);
130
131do_stdout:
132	evbuffer_add(c->stdout_data, bufdata, bufsize);
133	server_client_push_stdout(c);
134	return (CMD_RETURN_NORMAL);
135
136do_print:
137	if (bufsize > (INT_MAX / 4) - 1) {
138		cmdq_error(cmdq, "buffer too big");
139		return (CMD_RETURN_ERROR);
140	}
141	msg = NULL;
142
143	used = 0;
144	while (used != bufsize) {
145		start = bufdata + used;
146		end = memchr(start, '\n', bufsize - used);
147		if (end != NULL)
148			size = end - start;
149		else
150			size = bufsize - used;
151
152		msglen = size * 4 + 1;
153		msg = xrealloc(msg, msglen);
154
155		strvisx(msg, start, size, VIS_OCTAL|VIS_TAB);
156		cmdq_print(cmdq, "%s", msg);
157
158		used += size + (end != NULL);
159	}
160
161	free(msg);
162	return (CMD_RETURN_NORMAL);
163}
164