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