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 * Loads a paste buffer from a file.
34 enum cmd_retval
cmd_load_buffer_exec(struct cmd
*, struct cmd_q
*);
35 void cmd_load_buffer_callback(struct client
*, int, void *);
37 const struct cmd_entry cmd_load_buffer_entry
= {
38 "load-buffer", "loadb",
40 CMD_BUFFER_USAGE
" path",
47 cmd_load_buffer_exec(struct cmd
*self
, struct cmd_q
*cmdq
)
49 struct args
*args
= self
->args
;
50 struct client
*c
= cmdq
->client
;
54 char *pdata
, *new_pdata
, *cause
;
57 int ch
, error
, buffer
, *buffer_ptr
, cwd
, fd
;
59 if (!args_has(args
, 'b'))
62 buffer
= args_strtonum(args
, 'b', 0, INT_MAX
, &cause
);
64 cmdq_error(cmdq
, "buffer %s", cause
);
66 return (CMD_RETURN_ERROR
);
71 if (strcmp(path
, "-") == 0) {
72 buffer_ptr
= xmalloc(sizeof *buffer_ptr
);
75 error
= server_set_stdin_callback (c
, cmd_load_buffer_callback
,
78 cmdq_error(cmdq
, "%s: %s", path
, cause
);
80 return (CMD_RETURN_ERROR
);
82 return (CMD_RETURN_WAIT
);
85 if (c
!= NULL
&& c
->session
== NULL
)
87 else if ((s
= cmd_current_session(cmdq
, 0)) != NULL
)
92 if ((fd
= openat(cwd
, path
, O_RDONLY
)) == -1 ||
93 (f
= fdopen(fd
, "rb")) == NULL
) {
96 cmdq_error(cmdq
, "%s: %s", path
, strerror(errno
));
97 return (CMD_RETURN_ERROR
);
102 while ((ch
= getc(f
)) != EOF
) {
103 /* Do not let the server die due to memory exhaustion. */
104 if ((new_pdata
= realloc(pdata
, psize
+ 2)) == NULL
) {
105 cmdq_error(cmdq
, "realloc error: %s", strerror(errno
));
112 cmdq_error(cmdq
, "%s: read error", path
);
120 limit
= options_get_number(&global_options
, "buffer-limit");
122 paste_add(&global_buffers
, pdata
, psize
, limit
);
123 return (CMD_RETURN_NORMAL
);
125 if (paste_replace(&global_buffers
, buffer
, pdata
, psize
) != 0) {
126 cmdq_error(cmdq
, "no buffer %d", buffer
);
128 return (CMD_RETURN_ERROR
);
131 return (CMD_RETURN_NORMAL
);
137 return (CMD_RETURN_ERROR
);
141 cmd_load_buffer_callback(struct client
*c
, int closed
, void *data
)
150 c
->stdin_callback
= NULL
;
153 if (c
->flags
& CLIENT_DEAD
)
156 psize
= EVBUFFER_LENGTH(c
->stdin_data
);
157 if (psize
== 0 || (pdata
= malloc(psize
+ 1)) == NULL
) {
161 memcpy(pdata
, EVBUFFER_DATA(c
->stdin_data
), psize
);
163 evbuffer_drain(c
->stdin_data
, psize
);
165 limit
= options_get_number(&global_options
, "buffer-limit");
167 paste_add(&global_buffers
, pdata
, psize
, limit
);
168 else if (paste_replace(&global_buffers
, *buffer
, pdata
, psize
) != 0) {
169 /* No context so can't use server_client_msg_error. */
170 evbuffer_add_printf(c
->stderr_data
, "no buffer %d\n", *buffer
);
171 server_push_stderr(c
);
177 cmdq_continue(c
->cmdq
);