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 * Saves a paste buffer to a file.
33 enum cmd_retval
cmd_save_buffer_exec(struct cmd
*, struct cmd_q
*);
35 const struct cmd_entry cmd_save_buffer_entry
= {
36 "save-buffer", "saveb",
38 "[-a] " CMD_BUFFER_USAGE
" path",
45 const struct cmd_entry cmd_show_buffer_entry
= {
46 "show-buffer", "showb",
56 cmd_save_buffer_exec(struct cmd
*self
, struct cmd_q
*cmdq
)
58 struct args
*args
= self
->args
;
61 struct paste_buffer
*pb
;
62 const char *path
, *newpath
, *wd
;
63 char *cause
, *start
, *end
;
71 if (!args_has(args
, 'b')) {
72 if ((pb
= paste_get_top(&global_buffers
)) == NULL
) {
73 cmdq_error(cmdq
, "no buffers");
74 return (CMD_RETURN_ERROR
);
77 buffer
= args_strtonum(args
, 'b', 0, INT_MAX
, &cause
);
79 cmdq_error(cmdq
, "buffer %s", cause
);
81 return (CMD_RETURN_ERROR
);
84 pb
= paste_get_index(&global_buffers
, buffer
);
86 cmdq_error(cmdq
, "no buffer %d", buffer
);
87 return (CMD_RETURN_ERROR
);
91 if (self
->entry
== &cmd_show_buffer_entry
)
95 if (strcmp(path
, "-") == 0) {
98 cmdq_error(cmdq
, "can't write to stdout");
99 return (CMD_RETURN_ERROR
);
101 if (c
->session
== NULL
|| (c
->flags
& CLIENT_CONTROL
))
109 else if ((s
= cmd_current_session(cmdq
, 0)) != NULL
) {
110 wd
= options_get_string(&s
->options
, "default-path");
115 if (wd
!= NULL
&& *wd
!= '\0') {
116 newpath
= get_full_path(wd
, path
);
121 mask
= umask(S_IRWXG
| S_IRWXO
);
122 if (args_has(self
->args
, 'a'))
123 f
= fopen(path
, "ab");
125 f
= fopen(path
, "wb");
128 cmdq_error(cmdq
, "%s: %s", path
, strerror(errno
));
129 return (CMD_RETURN_ERROR
);
131 if (fwrite(pb
->data
, 1, pb
->size
, f
) != pb
->size
) {
132 cmdq_error(cmdq
, "%s: fwrite error", path
);
134 return (CMD_RETURN_ERROR
);
138 return (CMD_RETURN_NORMAL
);
141 evbuffer_add(c
->stdout_data
, pb
->data
, pb
->size
);
142 server_push_stdout(c
);
143 return (CMD_RETURN_NORMAL
);
146 if (pb
->size
> (INT_MAX
/ 4) - 1) {
147 cmdq_error(cmdq
, "buffer too big");
148 return (CMD_RETURN_ERROR
);
154 while (used
!= pb
->size
) {
155 start
= pb
->data
+ used
;
156 end
= memchr(start
, '\n', pb
->size
- used
);
160 size
= pb
->size
- used
;
162 msglen
= size
* 4 + 1;
163 msg
= xrealloc(msg
, 1, msglen
);
165 strvisx(msg
, start
, size
, VIS_OCTAL
|VIS_TAB
);
166 cmdq_print(cmdq
, "%s", msg
);
168 used
+= size
+ (end
!= NULL
);
172 return (CMD_RETURN_NORMAL
);