int cmd_set_buffer_exec(struct cmd *self, struct cmd_ctx *ctx) { struct args *args = self->args; u_int limit; char *pdata, *cause; size_t psize; int buffer; limit = options_get_number(&global_options, "buffer-limit"); pdata = xstrdup(args->argv[0]); psize = strlen(pdata); if (!args_has(args, 'b')) { paste_add(&global_buffers, pdata, psize, limit); return (0); } buffer = args_strtonum(args, 'b', 0, INT_MAX, &cause); if (cause != NULL) { ctx->error(ctx, "buffer %s", cause); xfree(cause); xfree(pdata); return (-1); } if (paste_replace(&global_buffers, buffer, pdata, psize) != 0) { ctx->error(ctx, "no buffer %d", buffer); xfree(pdata); return (-1); } return (0); }
void cmd_load_buffer_callback(struct client *c, void *data) { int *buffer = data; char *pdata; size_t psize; u_int limit; /* * Event callback has already checked client is not dead and reduced * its reference count. But tell it to exit. */ c->flags |= CLIENT_EXIT; psize = EVBUFFER_LENGTH(c->stdin_event->input); if (psize == 0 || (pdata = malloc(psize + 1)) == NULL) { xfree(data); return; } bufferevent_read(c->stdin_event, pdata, psize); pdata[psize] = '\0'; limit = options_get_number(&global_options, "buffer-limit"); if (*buffer == -1) paste_add(&global_buffers, pdata, psize, limit); else if (paste_replace(&global_buffers, *buffer, pdata, psize) != 0) { /* No context so can't use server_client_msg_error. */ evbuffer_add_printf( c->stderr_event->output, "no buffer %d\n", *buffer); bufferevent_enable(c->stderr_event, EV_WRITE); } xfree(data); }
int cmd_set_buffer_exec(struct cmd *self, struct cmd_ctx *ctx) { struct cmd_buffer_data *data = self->data; struct session *s; u_int limit; char *pdata; size_t psize; if ((s = cmd_find_session(ctx, data->target)) == NULL) return (-1); limit = options_get_number(&s->options, "buffer-limit"); pdata = xstrdup(data->arg); psize = strlen(pdata); if (data->buffer == -1) { paste_add(&s->buffers, pdata, psize, limit); return (0); } if (paste_replace(&s->buffers, data->buffer, pdata, psize) != 0) { ctx->error(ctx, "no buffer %d", data->buffer); xfree(pdata); return (-1); } return (0); }
enum cmd_retval cmd_capture_pane_exec(struct cmd *self, struct cmd_q *cmdq) { struct args *args = self->args; struct client *c; struct window_pane *wp; char *buf, *cause; int buffer; u_int limit; size_t len; if (cmd_find_pane(cmdq, args_get(args, 't'), NULL, &wp) == NULL) return (CMD_RETURN_ERROR); len = 0; if (args_has(args, 'P')) buf = cmd_capture_pane_pending(args, wp, &len); else buf = cmd_capture_pane_history(args, cmdq, wp, &len); if (buf == NULL) return (CMD_RETURN_ERROR); if (args_has(args, 'p')) { c = cmdq->client; if (c == NULL || (c->session != NULL && !(c->flags & CLIENT_CONTROL))) { cmdq_error(cmdq, "can't write to stdout"); return (CMD_RETURN_ERROR); } evbuffer_add(c->stdout_data, buf, len); if (args_has(args, 'P') && len > 0) evbuffer_add(c->stdout_data, "\n", 1); server_push_stdout(c); } else { limit = options_get_number(&global_options, "buffer-limit"); if (!args_has(args, 'b')) { paste_add(buf, len, limit); return (CMD_RETURN_NORMAL); } buffer = args_strtonum(args, 'b', 0, INT_MAX, &cause); if (cause != NULL) { cmdq_error(cmdq, "buffer %s", cause); free(buf); free(cause); return (CMD_RETURN_ERROR); } if (paste_replace(buffer, buf, len) != 0) { cmdq_error(cmdq, "no buffer %d", buffer); free(buf); return (CMD_RETURN_ERROR); } } return (CMD_RETURN_NORMAL); }
int cmd_load_buffer_exec(struct cmd *self, struct cmd_ctx *ctx) { struct cmd_buffer_data *data = self->data; struct session *s; FILE *f; char *pdata, *new_pdata; size_t psize; u_int limit; int ch; if ((s = cmd_find_session(ctx, data->target)) == NULL) return (-1); if ((f = fopen(data->arg, "rb")) == NULL) { ctx->error(ctx, "%s: %s", data->arg, strerror(errno)); return (-1); } pdata = NULL; psize = 0; while ((ch = getc(f)) != EOF) { /* Do not let the server die due to memory exhaustion. */ if ((new_pdata = realloc(pdata, psize + 2)) == NULL) { ctx->error(ctx, "realloc error: %s", strerror(errno)); goto error; } pdata = new_pdata; pdata[psize++] = ch; } if (ferror(f)) { ctx->error(ctx, "%s: read error", data->arg); goto error; } if (pdata != NULL) pdata[psize] = '\0'; fclose(f); limit = options_get_number(&s->options, "buffer-limit"); if (data->buffer == -1) { paste_add(&s->buffers, pdata, psize, limit); return (0); } if (paste_replace(&s->buffers, data->buffer, pdata, psize) != 0) { ctx->error(ctx, "no buffer %d", data->buffer); goto error; } return (0); error: if (pdata != NULL) xfree(pdata); fclose(f); return (-1); }
enum cmd_retval cmd_set_buffer_exec(struct cmd *self, struct cmd_q *cmdq) { struct args *args = self->args; struct paste_buffer *pb; u_int limit; char *pdata, *cause; size_t psize, newsize; int buffer; limit = options_get_number(&global_options, "buffer-limit"); psize = 0; pdata = NULL; pb = NULL; buffer = -1; if (args_has(args, 'b')) { buffer = args_strtonum(args, 'b', 0, INT_MAX, &cause); if (cause != NULL) { cmdq_error(cmdq, "buffer %s", cause); free(cause); return (CMD_RETURN_ERROR); } pb = paste_get_index(&global_buffers, buffer); if (pb == NULL) { cmdq_error(cmdq, "no buffer %d", buffer); return (CMD_RETURN_ERROR); } } else if (args_has(args, 'a')) { pb = paste_get_top(&global_buffers); if (pb != NULL) buffer = 0; } if (args_has(args, 'a') && pb != NULL) { psize = pb->size; pdata = xmalloc(psize); memcpy(pdata, pb->data, psize); } newsize = strlen(args->argv[0]); pdata = xrealloc(pdata, 1, psize + newsize); memcpy(pdata + psize, args->argv[0], newsize); psize += newsize; if (buffer == -1) paste_add(&global_buffers, pdata, psize, limit); else paste_replace(&global_buffers, buffer, pdata, psize); return (CMD_RETURN_NORMAL); }
int cmd_copy_buffer_exec(struct cmd *self, struct cmd_ctx *ctx) { struct cmd_copy_buffer_data *data = self->data; struct paste_buffer *pb; struct paste_stack *dst_ps, *src_ps; u_char *pdata; struct session *dst_session, *src_session; u_int limit; if ((dst_session = cmd_find_session(ctx, data->dst_session)) == NULL || (src_session = cmd_find_session(ctx, data->src_session)) == NULL) return (-1); dst_ps = &dst_session->buffers; src_ps = &src_session->buffers; if (data->src_idx == -1) { if ((pb = paste_get_top(src_ps)) == NULL) { ctx->error(ctx, "no buffers"); return (-1); } } else { if ((pb = paste_get_index(src_ps, data->src_idx)) == NULL) { ctx->error(ctx, "no buffer %d", data->src_idx); return (-1); } } limit = options_get_number(&dst_session->options, "buffer-limit"); pdata = xmalloc(pb->size); memcpy(pdata, pb->data, pb->size); if (data->dst_idx == -1) paste_add(dst_ps, pdata, pb->size, limit); else if (paste_replace(dst_ps, data->dst_idx, pdata, pb->size) != 0) { ctx->error(ctx, "no buffer %d", data->dst_idx); xfree(pdata); return (-1); } return (0); }
int cmd_capture_pane_exec(struct cmd *self, struct cmd_ctx *ctx) { struct cmd_capture_pane_data *data = self->data; struct window_pane *wp; char *buf, *line; struct screen *s; u_int i, limit; size_t len, linelen; if (cmd_find_pane(ctx, data->target, NULL, &wp) == NULL) return (-1); s = &wp->base; buf = NULL; len = 0; for (i = 0; i < screen_size_y(s); i++) { line = grid_view_string_cells(s->grid, 0, i, screen_size_x(s)); linelen = strlen(line); buf = xrealloc(buf, 1, len + linelen + 1); memcpy(buf + len, line, linelen); len += linelen; buf[len++] = '\n'; xfree(line); } limit = options_get_number(&global_options, "buffer-limit"); if (data->buffer == -1) { paste_add(&global_buffers, buf, len, limit); return (0); } if (paste_replace(&global_buffers, data->buffer, buf, len) != 0) { ctx->error(ctx, "no buffer %d", data->buffer); xfree(buf); return (-1); } return (0); }
void cmd_load_buffer_callback(struct client *c, int closed, void *data) { int *buffer = data; char *pdata; size_t psize; u_int limit; if (!closed) return; c->stdin_callback = NULL; c->references--; if (c->flags & CLIENT_DEAD) return; psize = EVBUFFER_LENGTH(c->stdin_data); if (psize == 0 || (pdata = malloc(psize + 1)) == NULL) { free(data); goto out; } memcpy(pdata, EVBUFFER_DATA(c->stdin_data), psize); pdata[psize] = '\0'; evbuffer_drain(c->stdin_data, psize); limit = options_get_number(&global_options, "buffer-limit"); if (*buffer == -1) paste_add(&global_buffers, pdata, psize, limit); else if (paste_replace(&global_buffers, *buffer, pdata, psize) != 0) { /* No context so can't use server_client_msg_error. */ evbuffer_add_printf(c->stderr_data, "no buffer %d\n", *buffer); server_push_stderr(c); free(pdata); } free(data); out: cmdq_continue(c->cmdq); }
int cmd_capture_pane_exec(struct cmd *self, struct cmd_ctx *ctx) { struct args *args = self->args; struct window_pane *wp; char *buf, *line, *cause; struct screen *s; struct grid *gd; int buffer, n; u_int i, limit, top, bottom, tmp; size_t len, linelen; if (cmd_find_pane(ctx, args_get(args, 't'), NULL, &wp) == NULL) return (-1); s = &wp->base; gd = s->grid; buf = NULL; len = 0; n = args_strtonum(args, 'S', SHRT_MIN, SHRT_MAX, &cause); if (cause != NULL) { top = gd->hsize; xfree(cause); } else if (n < 0 && (u_int) -n > gd->hsize) top = 0; else top = gd->hsize + n; if (top > gd->hsize + gd->sy - 1) top = gd->hsize + gd->sy - 1; n = args_strtonum(args, 'E', SHRT_MIN, SHRT_MAX, &cause); if (cause != NULL) { bottom = gd->hsize + gd->sy - 1; xfree(cause); } else if (n < 0 && (u_int) -n > gd->hsize) bottom = 0; else bottom = gd->hsize + n; if (bottom > gd->hsize + gd->sy - 1) bottom = gd->hsize + gd->sy - 1; if (bottom < top) { tmp = bottom; bottom = top; top = tmp; } for (i = top; i <= bottom; i++) { line = grid_string_cells(s->grid, 0, i, screen_size_x(s)); linelen = strlen(line); buf = xrealloc(buf, 1, len + linelen + 1); memcpy(buf + len, line, linelen); len += linelen; buf[len++] = '\n'; xfree(line); } limit = options_get_number(&global_options, "buffer-limit"); if (!args_has(args, 'b')) { paste_add(&global_buffers, buf, len, limit); return (0); } buffer = args_strtonum(args, 'b', 0, INT_MAX, &cause); if (cause != NULL) { ctx->error(ctx, "buffer %s", cause); xfree(cause); return (-1); } if (paste_replace(&global_buffers, buffer, buf, len) != 0) { ctx->error(ctx, "no buffer %d", buffer); xfree(buf); return (-1); } return (0); }
int cmd_load_buffer_exec(struct cmd *self, struct cmd_ctx *ctx) { struct args *args = self->args; struct client *c = ctx->cmdclient; FILE *f; const char *path; char *pdata, *new_pdata, *cause; size_t psize; u_int limit; int ch, buffer; int *buffer_ptr; if (!args_has(args, 'b')) buffer = -1; else { buffer = args_strtonum(args, 'b', 0, INT_MAX, &cause); if (cause != NULL) { ctx->error(ctx, "buffer %s", cause); xfree(cause); return (-1); } } path = args->argv[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 = buffer; c->stdin_data = buffer_ptr; c->stdin_callback = cmd_load_buffer_callback; c->references++; bufferevent_enable(c->stdin_event, EV_READ); return (1); } if ((f = fopen(path, "rb")) == NULL) { ctx->error(ctx, "%s: %s", path, strerror(errno)); return (-1); } pdata = NULL; psize = 0; while ((ch = getc(f)) != EOF) { /* Do not let the server die due to memory exhaustion. */ if ((new_pdata = realloc(pdata, psize + 2)) == NULL) { ctx->error(ctx, "realloc error: %s", strerror(errno)); goto error; } pdata = new_pdata; pdata[psize++] = ch; } if (ferror(f)) { ctx->error(ctx, "%s: read error", path); goto error; } if (pdata != NULL) pdata[psize] = '\0'; fclose(f); limit = options_get_number(&global_options, "buffer-limit"); if (buffer == -1) { paste_add(&global_buffers, pdata, psize, limit); return (0); } if (paste_replace(&global_buffers, buffer, pdata, psize) != 0) { ctx->error(ctx, "no buffer %d", buffer); return (-1); } return (0); error: if (pdata != NULL) xfree(pdata); if (f != NULL) fclose(f); return (-1); }
enum cmd_retval cmd_load_buffer_exec(struct cmd *self, struct cmd_q *cmdq) { struct args *args = self->args; struct client *c = cmdq->client; struct session *s; FILE *f; const char *path; char *pdata, *new_pdata, *cause; size_t psize; u_int limit; int ch, error, buffer, *buffer_ptr, cwd, fd; if (!args_has(args, 'b')) buffer = -1; else { buffer = args_strtonum(args, 'b', 0, INT_MAX, &cause); if (cause != NULL) { cmdq_error(cmdq, "buffer %s", cause); free(cause); return (CMD_RETURN_ERROR); } } path = args->argv[0]; if (strcmp(path, "-") == 0) { buffer_ptr = xmalloc(sizeof *buffer_ptr); *buffer_ptr = buffer; error = server_set_stdin_callback(c, cmd_load_buffer_callback, buffer_ptr, &cause); if (error != 0) { cmdq_error(cmdq, "%s: %s", path, cause); free(cause); return (CMD_RETURN_ERROR); } return (CMD_RETURN_WAIT); } if (c != NULL && c->session == NULL) cwd = c->cwd; else if ((s = cmd_current_session(cmdq, 0)) != NULL) cwd = s->cwd; else cwd = AT_FDCWD; if ((fd = openat(cwd, path, O_RDONLY)) == -1 || (f = fdopen(fd, "rb")) == NULL) { if (fd != -1) close(fd); cmdq_error(cmdq, "%s: %s", path, strerror(errno)); return (CMD_RETURN_ERROR); } pdata = NULL; psize = 0; while ((ch = getc(f)) != EOF) { /* Do not let the server die due to memory exhaustion. */ if ((new_pdata = realloc(pdata, psize + 2)) == NULL) { cmdq_error(cmdq, "realloc error: %s", strerror(errno)); goto error; } pdata = new_pdata; pdata[psize++] = ch; } if (ferror(f)) { cmdq_error(cmdq, "%s: read error", path); goto error; } if (pdata != NULL) pdata[psize] = '\0'; fclose(f); limit = options_get_number(&global_options, "buffer-limit"); if (buffer == -1) { paste_add(&global_buffers, pdata, psize, limit); return (CMD_RETURN_NORMAL); } if (paste_replace(&global_buffers, buffer, pdata, psize) != 0) { cmdq_error(cmdq, "no buffer %d", buffer); free(pdata); return (CMD_RETURN_ERROR); } return (CMD_RETURN_NORMAL); error: free(pdata); if (f != NULL) fclose(f); return (CMD_RETURN_ERROR); }