From c272de7cbafa343f0e2fc25beb66ed0b924f336f Mon Sep 17 00:00:00 2001 From: Tiago Cunha Date: Mon, 7 Sep 2009 23:48:54 +0000 Subject: [PATCH] Sync OpenBSD patchset 318: Give each paste buffer a size member instead of requiring them to be zero-terminated. --- cmd-copy-buffer.c | 29 ++++++++++++++++++----------- cmd-list-buffers.c | 4 ++-- cmd-load-buffer.c | 19 +++++++++---------- cmd-paste-buffer.c | 17 +++++++---------- cmd-save-buffer.c | 4 ++-- cmd-set-buffer.c | 16 +++++++++++----- cmd-show-buffer.c | 6 ++---- paste.c | 8 +++++--- status.c | 9 +++++---- tmux.h | 7 ++++--- window-copy.c | 13 ++++++++----- 11 files changed, 73 insertions(+), 59 deletions(-) diff --git a/cmd-copy-buffer.c b/cmd-copy-buffer.c index 426d7778..30731d14 100644 --- a/cmd-copy-buffer.c +++ b/cmd-copy-buffer.c @@ -1,4 +1,4 @@ -/* $Id: cmd-copy-buffer.c,v 1.3 2009-07-28 22:12:16 tcunha Exp $ */ +/* $Id: cmd-copy-buffer.c,v 1.4 2009-09-07 23:48:54 tcunha Exp $ */ /* * Copyright (c) 2009 Tiago Cunha @@ -16,7 +16,10 @@ * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ +#include + #include +#include #include "tmux.h" @@ -122,34 +125,38 @@ 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_session->buffers)) == NULL) { + if ((pb = paste_get_top(src_ps)) == NULL) { ctx->error(ctx, "no buffers"); return (-1); } } else { - if ((pb = paste_get_index(&src_session->buffers, - data->src_idx)) == NULL) { + 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"); - if (data->dst_idx == -1) { - paste_add(&dst_session->buffers, xstrdup(pb->data), limit); - return (0); - } - if (paste_replace(&dst_session->buffers, data->dst_idx, - xstrdup(pb->data)) != 0) { + + 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); } diff --git a/cmd-list-buffers.c b/cmd-list-buffers.c index 7b98f67b..65dac8a3 100644 --- a/cmd-list-buffers.c +++ b/cmd-list-buffers.c @@ -1,4 +1,4 @@ -/* $Id: cmd-list-buffers.c,v 1.11 2009-08-20 11:33:13 tcunha Exp $ */ +/* $Id: cmd-list-buffers.c,v 1.12 2009-09-07 23:48:54 tcunha Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott @@ -54,7 +54,7 @@ cmd_list_buffers_exec(struct cmd *self, struct cmd_ctx *ctx) idx = 0; while ((pb = paste_walk_stack(&s->buffers, &idx)) != NULL) { - size = strlen(pb->data); + size = pb->size; /* Translate the first 50 characters. */ len = size; diff --git a/cmd-load-buffer.c b/cmd-load-buffer.c index 99cd1f00..7a79c502 100644 --- a/cmd-load-buffer.c +++ b/cmd-load-buffer.c @@ -1,4 +1,4 @@ -/* $Id: cmd-load-buffer.c,v 1.9 2009-07-30 21:07:23 tcunha Exp $ */ +/* $Id: cmd-load-buffer.c,v 1.10 2009-09-07 23:48:54 tcunha Exp $ */ /* * Copyright (c) 2009 Tiago Cunha @@ -48,15 +48,15 @@ cmd_load_buffer_exec(struct cmd *self, struct cmd_ctx *ctx) { struct cmd_buffer_data *data = self->data; struct session *s; - struct stat statbuf; + struct stat sb; FILE *f; - char *buf; - u_int limit; + u_char *buf; + u_int limit; if ((s = cmd_find_session(ctx, data->target)) == NULL) return (-1); - if (stat(data->arg, &statbuf) < 0) { + if (stat(data->arg, &sb) < 0) { ctx->error(ctx, "%s: %s", data->arg, strerror(errno)); return (-1); } @@ -70,28 +70,27 @@ cmd_load_buffer_exec(struct cmd *self, struct cmd_ctx *ctx) * We don't want to die due to memory exhaustion, hence xmalloc can't * be used here. */ - if ((buf = malloc(statbuf.st_size + 1)) == NULL) { + if ((buf = malloc(sb.st_size + 1)) == NULL) { ctx->error(ctx, "malloc error: %s", strerror(errno)); fclose(f); return (-1); } - if (fread(buf, 1, statbuf.st_size, f) != (size_t) statbuf.st_size) { + if (fread(buf, 1, sb.st_size, f) != (size_t) sb.st_size) { ctx->error(ctx, "%s: fread error", data->arg); xfree(buf); fclose(f); return (-1); } - buf[statbuf.st_size] = '\0'; fclose(f); limit = options_get_number(&s->options, "buffer-limit"); if (data->buffer == -1) { - paste_add(&s->buffers, buf, limit); + paste_add(&s->buffers, buf, sb.st_size, limit); return (0); } - if (paste_replace(&s->buffers, data->buffer, buf) != 0) { + if (paste_replace(&s->buffers, data->buffer, buf, sb.st_size) != 0) { ctx->error(ctx, "no buffer %d", data->buffer); xfree(buf); return (-1); diff --git a/cmd-paste-buffer.c b/cmd-paste-buffer.c index 218363d3..325a41aa 100644 --- a/cmd-paste-buffer.c +++ b/cmd-paste-buffer.c @@ -1,4 +1,4 @@ -/* $Id: cmd-paste-buffer.c,v 1.19 2009-07-28 22:12:16 tcunha Exp $ */ +/* $Id: cmd-paste-buffer.c,v 1.20 2009-09-07 23:48:54 tcunha Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott @@ -45,13 +45,13 @@ cmd_paste_buffer_exec(struct cmd *self, struct cmd_ctx *ctx) { struct cmd_buffer_data *data = self->data; struct winlink *wl; - struct window *w; + struct window_pane *wp; struct session *s; struct paste_buffer *pb; if ((wl = cmd_find_window(ctx, data->target, &s)) == NULL) return (-1); - w = wl->window; + wp = wl->window->active; if (data->buffer == -1) pb = paste_get_top(&s->buffers); @@ -64,13 +64,10 @@ cmd_paste_buffer_exec(struct cmd *self, struct cmd_ctx *ctx) if (pb != NULL && *pb->data != '\0') { /* -r means raw data without LF->CR conversion. */ - if (data->chflags & CMD_CHFLAG('r')) { - buffer_write( - w->active->out, pb->data, strlen(pb->data)); - } else { - cmd_paste_buffer_lf2cr( - w->active->out, pb->data, strlen(pb->data)); - } + if (data->chflags & CMD_CHFLAG('r')) + buffer_write(wp->out, pb->data, pb->size); + else + cmd_paste_buffer_lf2cr(wp->out, pb->data, pb->size); } /* Delete the buffer if -d. */ diff --git a/cmd-save-buffer.c b/cmd-save-buffer.c index bcecd0c5..ca11bae4 100644 --- a/cmd-save-buffer.c +++ b/cmd-save-buffer.c @@ -1,4 +1,4 @@ -/* $Id: cmd-save-buffer.c,v 1.7 2009-07-28 22:12:16 tcunha Exp $ */ +/* $Id: cmd-save-buffer.c,v 1.8 2009-09-07 23:48:54 tcunha Exp $ */ /* * Copyright (c) 2009 Tiago Cunha @@ -75,7 +75,7 @@ cmd_save_buffer_exec(struct cmd *self, struct cmd_ctx *ctx) return (-1); } - if (fwrite(pb->data, 1, strlen(pb->data), f) != strlen(pb->data)) { + if (fwrite(pb->data, 1, pb->size, f) != pb->size) { ctx->error(ctx, "%s: fwrite error", data->arg); fclose(f); return (-1); diff --git a/cmd-set-buffer.c b/cmd-set-buffer.c index 00277f0c..175b56a5 100644 --- a/cmd-set-buffer.c +++ b/cmd-set-buffer.c @@ -1,4 +1,4 @@ -/* $Id: cmd-set-buffer.c,v 1.9 2009-07-28 22:12:16 tcunha Exp $ */ +/* $Id: cmd-set-buffer.c,v 1.10 2009-09-07 23:48:54 tcunha Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott @@ -18,7 +18,7 @@ #include -#include +#include #include "tmux.h" @@ -45,17 +45,23 @@ cmd_set_buffer_exec(struct cmd *self, struct cmd_ctx *ctx) struct cmd_buffer_data *data = self->data; struct session *s; u_int limit; + u_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, xstrdup(data->arg), limit); + paste_add(&s->buffers, pdata, psize, limit); return (0); } - if (paste_replace(&s->buffers, data->buffer, xstrdup(data->arg)) != 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); diff --git a/cmd-show-buffer.c b/cmd-show-buffer.c index 58634f42..6ac8b1e8 100644 --- a/cmd-show-buffer.c +++ b/cmd-show-buffer.c @@ -1,4 +1,4 @@ -/* $Id: cmd-show-buffer.c,v 1.9 2009-08-20 11:35:16 tcunha Exp $ */ +/* $Id: cmd-show-buffer.c,v 1.10 2009-09-07 23:48:54 tcunha Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott @@ -18,8 +18,6 @@ #include -#include - #include "tmux.h" /* @@ -64,7 +62,7 @@ cmd_show_buffer_exec(struct cmd *self, struct cmd_ctx *ctx) if (pb == NULL) return (0); - size = strlen(pb->data); + size = pb->size; if (size > SIZE_MAX / 4 - 1) size = SIZE_MAX / 4 - 1; in = xmalloc(size * 4 + 1); diff --git a/paste.c b/paste.c index 80367ff2..9e41ce54 100644 --- a/paste.c +++ b/paste.c @@ -1,4 +1,4 @@ -/* $Id: paste.c,v 1.8 2009-07-31 20:33:49 tcunha Exp $ */ +/* $Id: paste.c,v 1.9 2009-09-07 23:48:54 tcunha Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott @@ -97,7 +97,7 @@ paste_free_index(struct paste_stack *ps, u_int idx) } void -paste_add(struct paste_stack *ps, char *data, u_int limit) +paste_add(struct paste_stack *ps, u_char *data, size_t size, u_int limit) { struct paste_buffer *pb; @@ -115,12 +115,13 @@ paste_add(struct paste_stack *ps, char *data, u_int limit) ARRAY_INSERT(ps, 0, pb); pb->data = data; + pb->size = size; if (gettimeofday(&pb->tv, NULL) != 0) fatal("gettimeofday"); } int -paste_replace(struct paste_stack *ps, u_int idx, char *data) +paste_replace(struct paste_stack *ps, u_int idx, u_char *data, size_t size) { struct paste_buffer *pb; @@ -131,6 +132,7 @@ paste_replace(struct paste_stack *ps, u_int idx, char *data) xfree(pb->data); pb->data = data; + pb->size = size; if (gettimeofday(&pb->tv, NULL) != 0) fatal("gettimeofday"); diff --git a/status.c b/status.c index eb965842..22617f16 100644 --- a/status.c +++ b/status.c @@ -1,4 +1,4 @@ -/* $Id: status.c,v 1.116 2009-09-07 23:37:48 tcunha Exp $ */ +/* $Id: status.c,v 1.117 2009-09-07 23:48:54 tcunha Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott @@ -928,9 +928,10 @@ status_prompt_key(struct client *c, int key) case MODEKEYEDIT_PASTE: if ((pb = paste_get_top(&c->session->buffers)) == NULL) break; - if ((last = strchr(pb->data, '\n')) == NULL) - last = strchr(pb->data, '\0'); - n = last - pb->data; + for (n = 0; n < pb->size; n++) { + if (pb->data[n] < 32 || pb->data[n] == 127) + break; + } c->prompt_buffer = xrealloc(c->prompt_buffer, 1, size + n + 1); if (c->prompt_index == size) { diff --git a/tmux.h b/tmux.h index f4cc7b39..23921b59 100644 --- a/tmux.h +++ b/tmux.h @@ -1,4 +1,4 @@ -/* $Id: tmux.h,v 1.437 2009-09-07 23:37:48 tcunha Exp $ */ +/* $Id: tmux.h,v 1.438 2009-09-07 23:48:54 tcunha Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott @@ -770,6 +770,7 @@ struct layout_cell { /* Paste buffer. */ struct paste_buffer { char *data; + size_t size; struct timeval tv; }; ARRAY_DECL(paste_stack, struct paste_buffer *); @@ -1255,8 +1256,8 @@ struct paste_buffer *paste_get_top(struct paste_stack *); struct paste_buffer *paste_get_index(struct paste_stack *, u_int); int paste_free_top(struct paste_stack *); int paste_free_index(struct paste_stack *, u_int); -void paste_add(struct paste_stack *, char *, u_int); -int paste_replace(struct paste_stack *, u_int, char *); +void paste_add(struct paste_stack *, u_char *, size_t, u_int); +int paste_replace(struct paste_stack *, u_int, u_char *, size_t); /* clock.c */ extern const char clock_table[14][5][5]; diff --git a/window-copy.c b/window-copy.c index a7aa8f4c..bafdaf75 100644 --- a/window-copy.c +++ b/window-copy.c @@ -1,4 +1,4 @@ -/* $Id: window-copy.c,v 1.84 2009-08-21 21:12:07 tcunha Exp $ */ +/* $Id: window-copy.c,v 1.85 2009-09-07 23:48:54 tcunha Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott @@ -849,13 +849,16 @@ window_copy_copy_selection(struct window_pane *wp, struct client *c) window_copy_copy_line(wp, &buf, &off, ey, 0, ex); } - /* Terminate buffer, overwriting final \n. */ - if (off != 0) - buf[off - 1] = '\0'; + /* Don't bother if no data. */ + if (off == 0) { + xfree(buf); + return; + } + off--; /* remove final \n */ /* Add the buffer to the stack. */ limit = options_get_number(&c->session->options, "buffer-limit"); - paste_add(&c->session->buffers, buf, limit); + paste_add(&c->session->buffers, buf, off, limit); } void