4 * Copyright (c) 2009 Tiago Cunha <me@tiagocunha.org>
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.
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.
19 #include <sys/types.h>
30 * Loads a session paste buffer from a file.
33 int cmd_load_buffer_exec(struct cmd
*, struct cmd_ctx
*);
34 void cmd_load_buffer_callback(struct client
*, void *);
36 const struct cmd_entry cmd_load_buffer_entry
= {
37 "load-buffer", "loadb",
38 CMD_BUFFER_SESSION_USAGE
" path",
47 struct cmd_load_buffer_cdata
{
48 struct session
*session
;
53 cmd_load_buffer_exec(struct cmd
*self
, struct cmd_ctx
*ctx
)
55 struct cmd_buffer_data
*data
= self
->data
;
56 struct cmd_load_buffer_cdata
*cdata
;
58 struct client
*c
= ctx
->cmdclient
;
60 char *pdata
, *new_pdata
;
65 if ((s
= cmd_find_session(ctx
, data
->target
)) == NULL
)
68 if (strcmp(data
->arg
, "-") == 0) {
70 ctx
->error(ctx
, "%s: can't read from stdin", data
->arg
);
73 if (c
->flags
& CLIENT_TERMINAL
) {
74 ctx
->error(ctx
, "%s: stdin is a tty", data
->arg
);
77 if (c
->stdin_fd
== -1) {
78 ctx
->error(ctx
, "%s: can't read from stdin", data
->arg
);
82 cdata
= xmalloc(sizeof *cdata
);
84 cdata
->buffer
= data
->buffer
;
85 c
->stdin_data
= cdata
;
86 c
->stdin_callback
= cmd_load_buffer_callback
;
89 bufferevent_enable(c
->stdin_event
, EV_READ
);
93 if ((f
= fopen(data
->arg
, "rb")) == NULL
) {
94 ctx
->error(ctx
, "%s: %s", data
->arg
, strerror(errno
));
100 while ((ch
= getc(f
)) != EOF
) {
101 /* Do not let the server die due to memory exhaustion. */
102 if ((new_pdata
= realloc(pdata
, psize
+ 2)) == NULL
) {
103 ctx
->error(ctx
, "realloc error: %s", strerror(errno
));
110 ctx
->error(ctx
, "%s: read error", data
->arg
);
119 limit
= options_get_number(&s
->options
, "buffer-limit");
120 if (data
->buffer
== -1) {
121 paste_add(&s
->buffers
, pdata
, psize
, limit
);
124 if (paste_replace(&s
->buffers
, data
->buffer
, pdata
, psize
) != 0) {
125 ctx
->error(ctx
, "no buffer %d", data
->buffer
);
140 cmd_load_buffer_callback(struct client
*c
, void *data
)
142 struct cmd_load_buffer_cdata
*cdata
= data
;
143 struct session
*s
= cdata
->session
;
150 * Event callback has already checked client is not dead and reduced
151 * its reference count. But tell it to exit.
153 c
->flags
|= CLIENT_EXIT
;
155 /* Does the target session still exist? */
156 if (session_index(s
, &idx
) != 0)
159 psize
= EVBUFFER_LENGTH(c
->stdin_event
->input
);
163 pdata
= malloc(psize
+ 1);
166 bufferevent_read(c
->stdin_event
, pdata
, psize
);
169 limit
= options_get_number(&s
->options
, "buffer-limit");
170 if (cdata
->buffer
== -1) {
171 paste_add(&s
->buffers
, pdata
, psize
, limit
);
174 if (paste_replace(&s
->buffers
, cdata
->buffer
, pdata
, psize
) != 0) {
175 /* No context so can't use server_client_msg_error. */
177 c
->stderr_event
->output
, "no buffer %d\n", cdata
->buffer
);
178 bufferevent_enable(c
->stderr_event
, EV_WRITE
);