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>
31 * Saves a paste buffer to a file.
34 static enum cmd_retval
cmd_save_buffer_exec(struct cmd
*, struct cmdq_item
*);
36 const struct cmd_entry cmd_save_buffer_entry
= {
37 .name
= "save-buffer",
40 .args
= { "ab:", 1, 1, NULL
},
41 .usage
= "[-a] " CMD_BUFFER_USAGE
" path",
43 .flags
= CMD_AFTERHOOK
,
44 .exec
= cmd_save_buffer_exec
47 const struct cmd_entry cmd_show_buffer_entry
= {
48 .name
= "show-buffer",
51 .args
= { "b:", 0, 0, NULL
},
52 .usage
= CMD_BUFFER_USAGE
,
54 .flags
= CMD_AFTERHOOK
,
55 .exec
= cmd_save_buffer_exec
59 cmd_save_buffer_done(__unused
struct client
*c
, const char *path
, int error
,
60 __unused
int closed
, __unused
struct evbuffer
*buffer
, void *data
)
62 struct cmdq_item
*item
= data
;
68 cmdq_error(item
, "%s: %s", path
, strerror(error
));
72 static enum cmd_retval
73 cmd_save_buffer_exec(struct cmd
*self
, struct cmdq_item
*item
)
75 struct args
*args
= cmd_get_args(self
);
76 struct client
*c
= cmdq_get_client(item
);
77 struct paste_buffer
*pb
;
79 const char *bufname
= args_get(args
, 'b'), *bufdata
;
84 if (bufname
== NULL
) {
85 if ((pb
= paste_get_top(NULL
)) == NULL
) {
86 cmdq_error(item
, "no buffers");
87 return (CMD_RETURN_ERROR
);
90 pb
= paste_get_name(bufname
);
92 cmdq_error(item
, "no buffer %s", bufname
);
93 return (CMD_RETURN_ERROR
);
96 bufdata
= paste_buffer_data(pb
, &bufsize
);
98 if (cmd_get_entry(self
) == &cmd_show_buffer_entry
) {
99 if (c
->session
!= NULL
|| (c
->flags
& CLIENT_CONTROL
)) {
100 evb
= evbuffer_new();
102 fatalx("out of memory");
103 evbuffer_add(evb
, bufdata
, bufsize
);
104 cmdq_print_data(item
, 1, evb
);
106 return (CMD_RETURN_NORMAL
);
110 path
= format_single_from_target(item
, args_string(args
, 0));
111 if (args_has(args
, 'a'))
115 file_write(cmdq_get_client(item
), path
, flags
, bufdata
, bufsize
,
116 cmd_save_buffer_done
, item
);
119 return (CMD_RETURN_WAIT
);