xref: /openbsd-src/usr.bin/tmux/cmd-load-buffer.c (revision 765b9a582c208af75c7304abae7e7e4c757934cb)
1 /* $OpenBSD: cmd-load-buffer.c,v 1.45 2016/10/16 17:55:14 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 cmd_q *);
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 cmd_q	*cmdq;
51 	char		*bufname;
52 };
53 
54 static enum cmd_retval
55 cmd_load_buffer_exec(struct cmd *self, struct cmd_q *cmdq)
56 {
57 	struct args			*args = self->args;
58 	struct cmd_load_buffer_data	*cdata;
59 	struct client			*c = cmdq->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->cmdq = cmdq;
76 		cdata->bufname = xstrdup(bufname);
77 
78 		error = server_set_stdin_callback(c, cmd_load_buffer_callback,
79 		    cdata, &cause);
80 		if (error != 0) {
81 			cmdq_error(cmdq, "%s: %s", path, cause);
82 			free(cause);
83 			return (CMD_RETURN_ERROR);
84 		}
85 		return (CMD_RETURN_WAIT);
86 	}
87 
88 	if (c != NULL && c->session == NULL && c->cwd != NULL)
89 		cwd = c->cwd;
90 	else if ((s = c->session) != NULL && s->cwd != NULL)
91 		cwd = s->cwd;
92 	else
93 		cwd = ".";
94 
95 	if (*path == '/')
96 		file = xstrdup(path);
97 	else
98 		xasprintf(&file, "%s/%s", cwd, path);
99 	if (realpath(file, resolved) == NULL &&
100 	    strlcpy(resolved, file, sizeof resolved) >= sizeof resolved) {
101 		cmdq_error(cmdq, "%s: %s", file, strerror(ENAMETOOLONG));
102 		return (CMD_RETURN_ERROR);
103 	}
104 	f = fopen(resolved, "rb");
105 	free(file);
106 	if (f == NULL) {
107 		cmdq_error(cmdq, "%s: %s", resolved, strerror(errno));
108 		return (CMD_RETURN_ERROR);
109 	}
110 
111 	pdata = NULL;
112 	psize = 0;
113 	while ((ch = getc(f)) != EOF) {
114 		/* Do not let the server die due to memory exhaustion. */
115 		if ((new_pdata = realloc(pdata, psize + 2)) == NULL) {
116 			cmdq_error(cmdq, "realloc error: %s", strerror(errno));
117 			goto error;
118 		}
119 		pdata = new_pdata;
120 		pdata[psize++] = ch;
121 	}
122 	if (ferror(f)) {
123 		cmdq_error(cmdq, "%s: read error", resolved);
124 		goto error;
125 	}
126 	if (pdata != NULL)
127 		pdata[psize] = '\0';
128 
129 	fclose(f);
130 
131 	if (paste_set(pdata, psize, bufname, &cause) != 0) {
132 		cmdq_error(cmdq, "%s", cause);
133 		free(pdata);
134 		free(cause);
135 		return (CMD_RETURN_ERROR);
136 	}
137 
138 	return (CMD_RETURN_NORMAL);
139 
140 error:
141 	free(pdata);
142 	if (f != NULL)
143 		fclose(f);
144 	return (CMD_RETURN_ERROR);
145 }
146 
147 static void
148 cmd_load_buffer_callback(struct client *c, int closed, void *data)
149 {
150 	struct cmd_load_buffer_data	*cdata = data;
151 	char				*pdata, *cause, *saved;
152 	size_t				 psize;
153 
154 	if (!closed)
155 		return;
156 	c->stdin_callback = NULL;
157 
158 	server_client_unref(c);
159 	if (c->flags & CLIENT_DEAD)
160 		goto out;
161 
162 	psize = EVBUFFER_LENGTH(c->stdin_data);
163 	if (psize == 0 || (pdata = malloc(psize + 1)) == NULL)
164 		goto out;
165 
166 	memcpy(pdata, EVBUFFER_DATA(c->stdin_data), psize);
167 	pdata[psize] = '\0';
168 	evbuffer_drain(c->stdin_data, psize);
169 
170 	if (paste_set(pdata, psize, cdata->bufname, &cause) != 0) {
171 		/* No context so can't use server_client_msg_error. */
172 		if (~c->flags & CLIENT_UTF8) {
173 			saved = cause;
174 			cause = utf8_sanitize(saved);
175 			free(saved);
176 		}
177 		evbuffer_add_printf(c->stderr_data, "%s", cause);
178 		server_client_push_stderr(c);
179 		free(pdata);
180 		free(cause);
181 	}
182 out:
183 	cdata->cmdq->flags &= ~CMD_Q_WAITING;
184 
185 	free(cdata->bufname);
186 	free(cdata);
187 }
188