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 enum cmd_retval
cmd_save_buffer_exec(struct cmd
*, struct cmd_q
*);
36 const struct cmd_entry cmd_save_buffer_entry
= {
37 "save-buffer", "saveb",
39 "[-a] " CMD_BUFFER_USAGE
" path",
45 const struct cmd_entry cmd_show_buffer_entry
= {
46 "show-buffer", "showb",
55 cmd_save_buffer_exec(struct cmd
*self
, struct cmd_q
*cmdq
)
57 struct args
*args
= self
->args
;
58 struct client
*c
= cmdq
->client
;
60 struct paste_buffer
*pb
;
62 char *cause
, *start
, *end
, *msg
;
63 size_t size
, used
, msglen
;
67 if (!args_has(args
, 'b')) {
68 if ((pb
= paste_get_top(&global_buffers
)) == NULL
) {
69 cmdq_error(cmdq
, "no buffers");
70 return (CMD_RETURN_ERROR
);
73 buffer
= args_strtonum(args
, 'b', 0, INT_MAX
, &cause
);
75 cmdq_error(cmdq
, "buffer %s", cause
);
77 return (CMD_RETURN_ERROR
);
80 pb
= paste_get_index(&global_buffers
, buffer
);
82 cmdq_error(cmdq
, "no buffer %d", buffer
);
83 return (CMD_RETURN_ERROR
);
87 if (self
->entry
== &cmd_show_buffer_entry
)
91 if (strcmp(path
, "-") == 0) {
93 cmdq_error(cmdq
, "can't write to stdout");
94 return (CMD_RETURN_ERROR
);
96 if (c
->session
== NULL
|| (c
->flags
& CLIENT_CONTROL
))
101 if (c
!= NULL
&& c
->session
== NULL
)
103 else if ((s
= cmd_current_session(cmdq
, 0)) != NULL
)
109 if (args_has(self
->args
, 'a')) {
110 fd
= openat(cwd
, path
, O_CREAT
|O_RDWR
|O_APPEND
, 0600);
112 f
= fdopen(fd
, "ab");
114 fd
= openat(cwd
, path
, O_CREAT
|O_RDWR
, 0600);
116 f
= fdopen(fd
, "wb");
121 cmdq_error(cmdq
, "%s: %s", path
, strerror(errno
));
122 return (CMD_RETURN_ERROR
);
124 if (fwrite(pb
->data
, 1, pb
->size
, f
) != pb
->size
) {
125 cmdq_error(cmdq
, "%s: fwrite error", path
);
127 return (CMD_RETURN_ERROR
);
131 return (CMD_RETURN_NORMAL
);
134 evbuffer_add(c
->stdout_data
, pb
->data
, pb
->size
);
135 server_push_stdout(c
);
136 return (CMD_RETURN_NORMAL
);
139 if (pb
->size
> (INT_MAX
/ 4) - 1) {
140 cmdq_error(cmdq
, "buffer too big");
141 return (CMD_RETURN_ERROR
);
147 while (used
!= pb
->size
) {
148 start
= pb
->data
+ used
;
149 end
= memchr(start
, '\n', pb
->size
- used
);
153 size
= pb
->size
- used
;
155 msglen
= size
* 4 + 1;
156 msg
= xrealloc(msg
, 1, msglen
);
158 strvisx(msg
, start
, size
, VIS_OCTAL
|VIS_TAB
);
159 cmdq_print(cmdq
, "%s", msg
);
161 used
+= size
+ (end
!= NULL
);
165 return (CMD_RETURN_NORMAL
);