xref: /openbsd-src/usr.bin/tmux/cmd-load-buffer.c (revision 48950c12d106c85f315112191a0228d7b83b9510)
1 /* $OpenBSD: cmd-load-buffer.c,v 1.23 2013/03/24 09:54:10 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 <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26 
27 #include "tmux.h"
28 
29 /*
30  * Loads a paste buffer from a file.
31  */
32 
33 enum cmd_retval	 cmd_load_buffer_exec(struct cmd *, struct cmd_q *);
34 void		 cmd_load_buffer_callback(struct client *, int, void *);
35 
36 const struct cmd_entry cmd_load_buffer_entry = {
37 	"load-buffer", "loadb",
38 	"b:", 1, 1,
39 	CMD_BUFFER_USAGE " path",
40 	0,
41 	NULL,
42 	NULL,
43 	cmd_load_buffer_exec
44 };
45 
46 enum cmd_retval
47 cmd_load_buffer_exec(struct cmd *self, struct cmd_q *cmdq)
48 {
49 	struct args	*args = self->args;
50 	struct client	*c = cmdq->client;
51 	struct session  *s;
52 	FILE		*f;
53 	const char	*path, *newpath, *wd;
54 	char		*pdata, *new_pdata, *cause;
55 	size_t		 psize;
56 	u_int		 limit;
57 	int		 ch, error, buffer, *buffer_ptr;
58 
59 	if (!args_has(args, 'b'))
60 		buffer = -1;
61 	else {
62 		buffer = args_strtonum(args, 'b', 0, INT_MAX, &cause);
63 		if (cause != NULL) {
64 			cmdq_error(cmdq, "buffer %s", cause);
65 			free(cause);
66 			return (CMD_RETURN_ERROR);
67 		}
68 	}
69 
70 	path = args->argv[0];
71 	if (strcmp(path, "-") == 0) {
72 		buffer_ptr = xmalloc(sizeof *buffer_ptr);
73 		*buffer_ptr = buffer;
74 
75 		error = server_set_stdin_callback (c, cmd_load_buffer_callback,
76 		    buffer_ptr, &cause);
77 		if (error != 0) {
78 			cmdq_error(cmdq, "%s: %s", path, cause);
79 			free(cause);
80 			return (CMD_RETURN_ERROR);
81 		}
82 		return (CMD_RETURN_WAIT);
83 	}
84 
85 	if (c != NULL)
86 		wd = c->cwd;
87 	else if ((s = cmd_current_session(cmdq, 0)) != NULL) {
88 		wd = options_get_string(&s->options, "default-path");
89 		if (*wd == '\0')
90 			wd = s->cwd;
91 	} else
92 		wd = NULL;
93 	if (wd != NULL && *wd != '\0') {
94 		newpath = get_full_path(wd, path);
95 		if (newpath != NULL)
96 			path = newpath;
97 	}
98 	if ((f = fopen(path, "rb")) == NULL) {
99 		cmdq_error(cmdq, "%s: %s", path, strerror(errno));
100 		return (CMD_RETURN_ERROR);
101 	}
102 
103 	pdata = NULL;
104 	psize = 0;
105 	while ((ch = getc(f)) != EOF) {
106 		/* Do not let the server die due to memory exhaustion. */
107 		if ((new_pdata = realloc(pdata, psize + 2)) == NULL) {
108 			cmdq_error(cmdq, "realloc error: %s", strerror(errno));
109 			goto error;
110 		}
111 		pdata = new_pdata;
112 		pdata[psize++] = ch;
113 	}
114 	if (ferror(f)) {
115 		cmdq_error(cmdq, "%s: read error", path);
116 		goto error;
117 	}
118 	if (pdata != NULL)
119 		pdata[psize] = '\0';
120 
121 	fclose(f);
122 
123 	limit = options_get_number(&global_options, "buffer-limit");
124 	if (buffer == -1) {
125 		paste_add(&global_buffers, pdata, psize, limit);
126 		return (CMD_RETURN_NORMAL);
127 	}
128 	if (paste_replace(&global_buffers, buffer, pdata, psize) != 0) {
129 		cmdq_error(cmdq, "no buffer %d", buffer);
130 		free(pdata);
131 		return (CMD_RETURN_ERROR);
132 	}
133 
134 	return (CMD_RETURN_NORMAL);
135 
136 error:
137 	free(pdata);
138 	if (f != NULL)
139 		fclose(f);
140 	return (CMD_RETURN_ERROR);
141 }
142 
143 void
144 cmd_load_buffer_callback(struct client *c, int closed, void *data)
145 {
146 	int	*buffer = data;
147 	char	*pdata;
148 	size_t	 psize;
149 	u_int	 limit;
150 
151 	if (!closed)
152 		return;
153 	c->stdin_callback = NULL;
154 
155 	c->references--;
156 	if (c->flags & CLIENT_DEAD)
157 		return;
158 
159 	psize = EVBUFFER_LENGTH(c->stdin_data);
160 	if (psize == 0 || (pdata = malloc(psize + 1)) == NULL) {
161 		free(data);
162 		goto out;
163 	}
164 	memcpy(pdata, EVBUFFER_DATA(c->stdin_data), psize);
165 	pdata[psize] = '\0';
166 	evbuffer_drain(c->stdin_data, psize);
167 
168 	limit = options_get_number(&global_options, "buffer-limit");
169 	if (*buffer == -1)
170 		paste_add(&global_buffers, pdata, psize, limit);
171 	else if (paste_replace(&global_buffers, *buffer, pdata, psize) != 0) {
172 		/* No context so can't use server_client_msg_error. */
173 		evbuffer_add_printf(c->stderr_data, "no buffer %d\n", *buffer);
174 		server_push_stderr(c);
175 	}
176 
177 	free(data);
178 
179 out:
180 	cmdq_continue(c->cmdq);
181 }
182