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.
27 * Loads a session paste buffer from a file.
30 int cmd_load_buffer_exec(struct cmd
*, struct cmd_ctx
*);
32 const struct cmd_entry cmd_load_buffer_entry
= {
33 "load-buffer", "loadb",
34 CMD_BUFFER_SESSION_USAGE
" path",
44 cmd_load_buffer_exec(struct cmd
*self
, struct cmd_ctx
*ctx
)
46 struct cmd_buffer_data
*data
= self
->data
;
49 char *pdata
, *new_pdata
;
54 if ((s
= cmd_find_session(ctx
, data
->target
)) == NULL
)
57 if ((f
= fopen(data
->arg
, "rb")) == NULL
) {
58 ctx
->error(ctx
, "%s: %s", data
->arg
, strerror(errno
));
64 while ((ch
= getc(f
)) != EOF
) {
65 /* Do not let the server die due to memory exhaustion. */
66 if ((new_pdata
= realloc(pdata
, psize
+ 2)) == NULL
) {
67 ctx
->error(ctx
, "realloc error: %s", strerror(errno
));
74 ctx
->error(ctx
, "%s: read error", data
->arg
);
82 limit
= options_get_number(&s
->options
, "buffer-limit");
83 if (data
->buffer
== -1) {
84 paste_add(&s
->buffers
, pdata
, psize
, limit
);
87 if (paste_replace(&s
->buffers
, data
->buffer
, pdata
, psize
) != 0) {
88 ctx
->error(ctx
, "no buffer %d", data
->buffer
);