version 1.19, 2011/10/23 08:34:01 |
version 1.20, 2012/05/21 18:27:42 |
|
|
*/ |
*/ |
|
|
int cmd_load_buffer_exec(struct cmd *, struct cmd_ctx *); |
int cmd_load_buffer_exec(struct cmd *, struct cmd_ctx *); |
void cmd_load_buffer_callback(struct client *, void *); |
void cmd_load_buffer_callback(struct client *, int, void *); |
|
|
const struct cmd_entry cmd_load_buffer_entry = { |
const struct cmd_entry cmd_load_buffer_entry = { |
"load-buffer", "loadb", |
"load-buffer", "loadb", |
|
|
char *pdata, *new_pdata, *cause; |
char *pdata, *new_pdata, *cause; |
size_t psize; |
size_t psize; |
u_int limit; |
u_int limit; |
int ch, buffer; |
int ch, error, buffer, *buffer_ptr; |
int *buffer_ptr; |
|
|
|
if (!args_has(args, 'b')) |
if (!args_has(args, 'b')) |
buffer = -1; |
buffer = -1; |
|
|
|
|
path = args->argv[0]; |
path = args->argv[0]; |
if (strcmp(path, "-") == 0) { |
if (strcmp(path, "-") == 0) { |
if (c == NULL) { |
|
ctx->error(ctx, "%s: can't read from stdin", path); |
|
return (-1); |
|
} |
|
if (c->flags & CLIENT_TERMINAL) { |
|
ctx->error(ctx, "%s: stdin is a tty", path); |
|
return (-1); |
|
} |
|
if (c->stdin_fd == -1) { |
|
ctx->error(ctx, "%s: can't read from stdin", path); |
|
return (-1); |
|
} |
|
|
|
buffer_ptr = xmalloc(sizeof *buffer_ptr); |
buffer_ptr = xmalloc(sizeof *buffer_ptr); |
*buffer_ptr = buffer; |
*buffer_ptr = buffer; |
|
|
c->stdin_data = buffer_ptr; |
error = server_set_stdin_callback (c, cmd_load_buffer_callback, |
c->stdin_callback = cmd_load_buffer_callback; |
buffer_ptr, &cause); |
|
if (error != 0) { |
c->references++; |
ctx->error(ctx, "%s: %s", path, cause); |
bufferevent_enable(c->stdin_event, EV_READ); |
xfree(cause); |
|
return (-1); |
|
} |
return (1); |
return (1); |
} |
} |
|
|
|
|
} |
} |
|
|
void |
void |
cmd_load_buffer_callback(struct client *c, void *data) |
cmd_load_buffer_callback(struct client *c, int closed, void *data) |
{ |
{ |
int *buffer = data; |
int *buffer = data; |
char *pdata; |
char *pdata; |
size_t psize; |
size_t psize; |
u_int limit; |
u_int limit; |
|
|
/* |
if (!closed) |
* Event callback has already checked client is not dead and reduced |
return; |
* its reference count. But tell it to exit. |
c->stdin_callback = NULL; |
*/ |
|
|
c->references--; |
c->flags |= CLIENT_EXIT; |
c->flags |= CLIENT_EXIT; |
|
|
psize = EVBUFFER_LENGTH(c->stdin_event->input); |
psize = EVBUFFER_LENGTH(c->stdin_data); |
if (psize == 0 || (pdata = malloc(psize + 1)) == NULL) { |
if (psize == 0 || (pdata = malloc(psize + 1)) == NULL) { |
xfree(data); |
xfree(data); |
return; |
return; |
} |
} |
bufferevent_read(c->stdin_event, pdata, psize); |
memcpy(pdata, EVBUFFER_DATA(c->stdin_data), psize); |
pdata[psize] = '\0'; |
pdata[psize] = '\0'; |
|
evbuffer_drain(c->stdin_data, psize); |
|
|
limit = options_get_number(&global_options, "buffer-limit"); |
limit = options_get_number(&global_options, "buffer-limit"); |
if (*buffer == -1) |
if (*buffer == -1) |
paste_add(&global_buffers, pdata, psize, limit); |
paste_add(&global_buffers, pdata, psize, limit); |
else if (paste_replace(&global_buffers, *buffer, pdata, psize) != 0) { |
else if (paste_replace(&global_buffers, *buffer, pdata, psize) != 0) { |
/* No context so can't use server_client_msg_error. */ |
/* No context so can't use server_client_msg_error. */ |
evbuffer_add_printf( |
evbuffer_add_printf(c->stderr_data, "no buffer %d\n", *buffer); |
c->stderr_event->output, "no buffer %d\n", *buffer); |
server_push_stderr(c); |
bufferevent_enable(c->stderr_event, EV_WRITE); |
|
} |
} |
|
|
xfree(data); |
xfree(data); |