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",
44 const struct cmd_entry cmd_show_buffer_entry
= {
45 "show-buffer", "showb",
54 cmd_save_buffer_exec(struct cmd
*self
, struct cmd_q
*cmdq
)
56 struct args
*args
= self
->args
;
59 struct paste_buffer
*pb
;
60 const char *path
, *newpath
, *wd
;
61 char *cause
, *start
, *end
;
69 if (!args_has(args
, 'b')) {
70 if ((pb
= paste_get_top(&global_buffers
)) == NULL
) {
71 cmdq_error(cmdq
, "no buffers");
72 return (CMD_RETURN_ERROR
);
75 buffer
= args_strtonum(args
, 'b', 0, INT_MAX
, &cause
);
77 cmdq_error(cmdq
, "buffer %s", cause
);
79 return (CMD_RETURN_ERROR
);
82 pb
= paste_get_index(&global_buffers
, buffer
);
84 cmdq_error(cmdq
, "no buffer %d", buffer
);
85 return (CMD_RETURN_ERROR
);
89 if (self
->entry
== &cmd_show_buffer_entry
)
93 if (strcmp(path
, "-") == 0) {
96 cmdq_error(cmdq
, "can't write to stdout");
97 return (CMD_RETURN_ERROR
);
99 if (c
->session
== NULL
|| (c
->flags
& CLIENT_CONTROL
))
107 else if ((s
= cmd_current_session(cmdq
, 0)) != NULL
) {
108 wd
= options_get_string(&s
->options
, "default-path");
113 if (wd
!= NULL
&& *wd
!= '\0') {
114 newpath
= get_full_path(wd
, path
);
119 mask
= umask(S_IRWXG
| S_IRWXO
);
120 if (args_has(self
->args
, 'a'))
121 f
= fopen(path
, "ab");
123 f
= fopen(path
, "wb");
126 cmdq_error(cmdq
, "%s: %s", path
, strerror(errno
));
127 return (CMD_RETURN_ERROR
);
129 if (fwrite(pb
->data
, 1, pb
->size
, f
) != pb
->size
) {
130 cmdq_error(cmdq
, "%s: fwrite error", path
);
132 return (CMD_RETURN_ERROR
);
136 return (CMD_RETURN_NORMAL
);
139 evbuffer_add(c
->stdout_data
, pb
->data
, pb
->size
);
140 server_push_stdout(c
);
141 return (CMD_RETURN_NORMAL
);
144 if (pb
->size
> (INT_MAX
/ 4) - 1) {
145 cmdq_error(cmdq
, "buffer too big");
146 return (CMD_RETURN_ERROR
);
152 while (used
!= pb
->size
) {
153 start
= pb
->data
+ used
;
154 end
= memchr(start
, '\n', pb
->size
- used
);
158 size
= pb
->size
- used
;
160 msglen
= size
* 4 + 1;
161 msg
= xrealloc(msg
, 1, msglen
);
163 strvisx(msg
, start
, size
, VIS_OCTAL
|VIS_TAB
);
164 cmdq_print(cmdq
, "%s", msg
);
166 used
+= size
+ (end
!= NULL
);
170 return (CMD_RETURN_NORMAL
);