xref: /openbsd-src/usr.bin/tmux/cmd-load-buffer.c (revision 07a4101d3bc59add46debd94e095c61c0adf35c9)
1 /* $OpenBSD: cmd-load-buffer.c,v 1.48 2017/01/06 13:26:09 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 
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27 
28 #include "tmux.h"
29 
30 /*
31  * Loads a paste buffer from a file.
32  */
33 
34 static enum cmd_retval	cmd_load_buffer_exec(struct cmd *, struct cmdq_item *);
35 
36 static void		cmd_load_buffer_callback(struct client *, int, void *);
37 
38 const struct cmd_entry cmd_load_buffer_entry = {
39 	.name = "load-buffer",
40 	.alias = "loadb",
41 
42 	.args = { "b:", 1, 1 },
43 	.usage = CMD_BUFFER_USAGE " path",
44 
45 	.flags = CMD_AFTERHOOK,
46 	.exec = cmd_load_buffer_exec
47 };
48 
49 struct cmd_load_buffer_data {
50 	struct cmdq_item	*item;
51 	char			*bufname;
52 };
53 
54 static enum cmd_retval
55 cmd_load_buffer_exec(struct cmd *self, struct cmdq_item *item)
56 {
57 	struct args			*args = self->args;
58 	struct cmd_load_buffer_data	*cdata;
59 	struct client			*c = item->client;
60 	struct session  		*s;
61 	FILE				*f;
62 	const char			*path, *bufname, *cwd;
63 	char				*pdata, *new_pdata, *cause, *file;
64 	char				 resolved[PATH_MAX];
65 	size_t				 psize;
66 	int				 ch, error;
67 
68 	bufname = NULL;
69 	if (args_has(args, 'b'))
70 		bufname = args_get(args, 'b');
71 
72 	path = args->argv[0];
73 	if (strcmp(path, "-") == 0) {
74 		cdata = xcalloc(1, sizeof *cdata);
75 		cdata->item = item;
76 
77 		if (bufname != NULL)
78 			cdata->bufname = xstrdup(bufname);
79 
80 		error = server_set_stdin_callback(c, cmd_load_buffer_callback,
81 		    cdata, &cause);
82 		if (error != 0) {
83 			cmdq_error(item, "%s: %s", path, cause);
84 			free(cause);
85 			return (CMD_RETURN_ERROR);
86 		}
87 		return (CMD_RETURN_WAIT);
88 	}
89 
90 	if (c != NULL && c->session == NULL && c->cwd != NULL)
91 		cwd = c->cwd;
92 	else if (c != NULL && (s = c->session) != NULL && s->cwd != NULL)
93 		cwd = s->cwd;
94 	else
95 		cwd = ".";
96 
97 	if (*path == '/')
98 		file = xstrdup(path);
99 	else
100 		xasprintf(&file, "%s/%s", cwd, path);
101 	if (realpath(file, resolved) == NULL &&
102 	    strlcpy(resolved, file, sizeof resolved) >= sizeof resolved) {
103 		cmdq_error(item, "%s: %s", file, strerror(ENAMETOOLONG));
104 		return (CMD_RETURN_ERROR);
105 	}
106 	f = fopen(resolved, "rb");
107 	free(file);
108 	if (f == NULL) {
109 		cmdq_error(item, "%s: %s", resolved, strerror(errno));
110 		return (CMD_RETURN_ERROR);
111 	}
112 
113 	pdata = NULL;
114 	psize = 0;
115 	while ((ch = getc(f)) != EOF) {
116 		/* Do not let the server die due to memory exhaustion. */
117 		if ((new_pdata = realloc(pdata, psize + 2)) == NULL) {
118 			cmdq_error(item, "realloc error: %s", strerror(errno));
119 			goto error;
120 		}
121 		pdata = new_pdata;
122 		pdata[psize++] = ch;
123 	}
124 	if (ferror(f)) {
125 		cmdq_error(item, "%s: read error", resolved);
126 		goto error;
127 	}
128 	if (pdata != NULL)
129 		pdata[psize] = '\0';
130 
131 	fclose(f);
132 
133 	if (paste_set(pdata, psize, bufname, &cause) != 0) {
134 		cmdq_error(item, "%s", cause);
135 		free(pdata);
136 		free(cause);
137 		return (CMD_RETURN_ERROR);
138 	}
139 
140 	return (CMD_RETURN_NORMAL);
141 
142 error:
143 	free(pdata);
144 	if (f != NULL)
145 		fclose(f);
146 	return (CMD_RETURN_ERROR);
147 }
148 
149 static void
150 cmd_load_buffer_callback(struct client *c, int closed, void *data)
151 {
152 	struct cmd_load_buffer_data	*cdata = data;
153 	char				*pdata, *cause, *saved;
154 	size_t				 psize;
155 
156 	if (!closed)
157 		return;
158 	c->stdin_callback = NULL;
159 
160 	server_client_unref(c);
161 	if (c->flags & CLIENT_DEAD)
162 		goto out;
163 
164 	psize = EVBUFFER_LENGTH(c->stdin_data);
165 	if (psize == 0 || (pdata = malloc(psize + 1)) == NULL)
166 		goto out;
167 
168 	memcpy(pdata, EVBUFFER_DATA(c->stdin_data), psize);
169 	pdata[psize] = '\0';
170 	evbuffer_drain(c->stdin_data, psize);
171 
172 	if (paste_set(pdata, psize, cdata->bufname, &cause) != 0) {
173 		/* No context so can't use server_client_msg_error. */
174 		if (~c->flags & CLIENT_UTF8) {
175 			saved = cause;
176 			cause = utf8_sanitize(saved);
177 			free(saved);
178 		}
179 		evbuffer_add_printf(c->stderr_data, "%s", cause);
180 		server_client_push_stderr(c);
181 		free(pdata);
182 		free(cause);
183 	}
184 out:
185 	cdata->item->flags &= ~CMDQ_WAITING;
186 
187 	free(cdata->bufname);
188 	free(cdata);
189 }
190