Move struct paste_buffer out of tmux.h.

This commit is contained in:
nicm 2015-08-29 09:25:00 +00:00
parent b9f0571780
commit b569585000
9 changed files with 108 additions and 88 deletions

View File

@ -65,7 +65,7 @@ cmd_choose_buffer_exec(struct cmd *self, struct cmd_q *cmdq)
return (CMD_RETURN_ERROR); return (CMD_RETURN_ERROR);
utf8flag = options_get_number(&wl->window->options, "utf8"); utf8flag = options_get_number(&wl->window->options, "utf8");
if (paste_get_top() == NULL) if (paste_get_top(NULL) == NULL)
return (CMD_RETURN_NORMAL); return (CMD_RETURN_NORMAL);
if (window_pane_set_mode(wl->window->active, &window_choose_mode) != 0) if (window_pane_set_mode(wl->window->active, &window_choose_mode) != 0)
@ -85,7 +85,7 @@ cmd_choose_buffer_exec(struct cmd *self, struct cmd_q *cmdq)
cdata->ft_template = xstrdup(template); cdata->ft_template = xstrdup(template);
format_defaults_paste_buffer(cdata->ft, pb, utf8flag); format_defaults_paste_buffer(cdata->ft, pb, utf8flag);
xasprintf(&action_data, "%s", pb->name); xasprintf(&action_data, "%s", paste_buffer_name(pb));
cdata->command = cmd_template_replace(action, action_data, 1); cdata->command = cmd_template_replace(action, action_data, 1);
free(action_data); free(action_data);

View File

@ -58,7 +58,7 @@ cmd_paste_buffer_exec(struct cmd *self, struct cmd_q *cmdq)
bufname = args_get(args, 'b'); bufname = args_get(args, 'b');
if (bufname == NULL) if (bufname == NULL)
pb = paste_get_top(); pb = paste_get_top(NULL);
else { else {
pb = paste_get_name(bufname); pb = paste_get_name(bufname);
if (pb == NULL) { if (pb == NULL) {

View File

@ -57,14 +57,14 @@ cmd_save_buffer_exec(struct cmd *self, struct cmd_q *cmdq)
struct client *c = cmdq->client; struct client *c = cmdq->client;
struct session *s; struct session *s;
struct paste_buffer *pb; struct paste_buffer *pb;
const char *path, *bufname; const char *path, *bufname, *bufdata;
char *start, *end, *msg; char *start, *end, *msg;
size_t size, used, msglen; size_t size, used, msglen, bufsize;
int cwd, fd; int cwd, fd;
FILE *f; FILE *f;
if (!args_has(args, 'b')) { if (!args_has(args, 'b')) {
if ((pb = paste_get_top()) == NULL) { if ((pb = paste_get_top(NULL)) == NULL) {
cmdq_error(cmdq, "no buffers"); cmdq_error(cmdq, "no buffers");
return (CMD_RETURN_ERROR); return (CMD_RETURN_ERROR);
} }
@ -76,6 +76,7 @@ cmd_save_buffer_exec(struct cmd *self, struct cmd_q *cmdq)
return (CMD_RETURN_ERROR); return (CMD_RETURN_ERROR);
} }
} }
bufdata = paste_buffer_data(pb, &bufsize);
if (self->entry == &cmd_show_buffer_entry) if (self->entry == &cmd_show_buffer_entry)
path = "-"; path = "-";
@ -114,7 +115,7 @@ cmd_save_buffer_exec(struct cmd *self, struct cmd_q *cmdq)
cmdq_error(cmdq, "%s: %s", path, strerror(errno)); cmdq_error(cmdq, "%s: %s", path, strerror(errno));
return (CMD_RETURN_ERROR); return (CMD_RETURN_ERROR);
} }
if (fwrite(pb->data, 1, pb->size, f) != pb->size) { if (fwrite(bufdata, 1, bufsize, f) != bufsize) {
cmdq_error(cmdq, "%s: fwrite error", path); cmdq_error(cmdq, "%s: fwrite error", path);
fclose(f); fclose(f);
return (CMD_RETURN_ERROR); return (CMD_RETURN_ERROR);
@ -124,25 +125,25 @@ cmd_save_buffer_exec(struct cmd *self, struct cmd_q *cmdq)
return (CMD_RETURN_NORMAL); return (CMD_RETURN_NORMAL);
do_stdout: do_stdout:
evbuffer_add(c->stdout_data, pb->data, pb->size); evbuffer_add(c->stdout_data, bufdata, bufsize);
server_push_stdout(c); server_push_stdout(c);
return (CMD_RETURN_NORMAL); return (CMD_RETURN_NORMAL);
do_print: do_print:
if (pb->size > (INT_MAX / 4) - 1) { if (bufsize > (INT_MAX / 4) - 1) {
cmdq_error(cmdq, "buffer too big"); cmdq_error(cmdq, "buffer too big");
return (CMD_RETURN_ERROR); return (CMD_RETURN_ERROR);
} }
msg = NULL; msg = NULL;
used = 0; used = 0;
while (used != pb->size) { while (used != bufsize) {
start = pb->data + used; start = bufdata + used;
end = memchr(start, '\n', pb->size - used); end = memchr(start, '\n', bufsize - used);
if (end != NULL) if (end != NULL)
size = end - start; size = end - start;
else else
size = pb->size - used; size = bufsize - used;
msglen = size * 4 + 1; msglen = size * 4 + 1;
msg = xrealloc(msg, msglen); msg = xrealloc(msg, msglen);

View File

@ -42,9 +42,9 @@ cmd_set_buffer_exec(struct cmd *self, struct cmd_q *cmdq)
{ {
struct args *args = self->args; struct args *args = self->args;
struct paste_buffer *pb; struct paste_buffer *pb;
char *pdata, *cause; char *bufdata, *cause;
const char *bufname; const char *bufname, *olddata;
size_t psize, newsize; size_t bufsize, newsize;
bufname = NULL; bufname = NULL;
@ -58,12 +58,11 @@ cmd_set_buffer_exec(struct cmd *self, struct cmd_q *cmdq)
bufname = args_get(args, 'b'); bufname = args_get(args, 'b');
if (bufname == NULL) { if (bufname == NULL) {
pb = paste_get_top(); pb = paste_get_top(&bufname);
if (pb == NULL) { if (pb == NULL) {
cmdq_error(cmdq, "no buffer"); cmdq_error(cmdq, "no buffer");
return (CMD_RETURN_ERROR); return (CMD_RETURN_ERROR);
} }
bufname = pb->name;
} }
if (paste_rename(bufname, args_get(args, 'n'), &cause) != 0) { if (paste_rename(bufname, args_get(args, 'n'), &cause) != 0) {
@ -79,37 +78,33 @@ cmd_set_buffer_exec(struct cmd *self, struct cmd_q *cmdq)
cmdq_error(cmdq, "no data specified"); cmdq_error(cmdq, "no data specified");
return (CMD_RETURN_ERROR); return (CMD_RETURN_ERROR);
} }
psize = 0;
pdata = NULL;
pb = NULL; pb = NULL;
bufsize = 0;
bufdata = NULL;
if ((newsize = strlen(args->argv[0])) == 0) if ((newsize = strlen(args->argv[0])) == 0)
return (CMD_RETURN_NORMAL); return (CMD_RETURN_NORMAL);
if (args_has(args, 'b')) { if (args_has(args, 'b')) {
bufname = args_get(args, 'b'); bufname = args_get(args, 'b');
pb = paste_get_name(bufname); pb = paste_get_name(bufname);
} else if (args_has(args, 'a')) { } else if (args_has(args, 'a'))
pb = paste_get_top(); pb = paste_get_top(&bufname);
if (pb != NULL)
bufname = pb->name;
}
if (args_has(args, 'a') && pb != NULL) { if (args_has(args, 'a') && pb != NULL) {
psize = pb->size; olddata = paste_buffer_data(pb, &bufsize);
pdata = xmalloc(psize); bufdata = xmalloc(bufsize);
memcpy(pdata, pb->data, psize); memcpy(bufdata, olddata, bufsize);
} }
pdata = xrealloc(pdata, psize + newsize); bufdata = xrealloc(bufdata, bufsize + newsize);
memcpy(pdata + psize, args->argv[0], newsize); memcpy(bufdata + bufsize, args->argv[0], newsize);
psize += newsize; bufsize += newsize;
if (paste_set(pdata, psize, bufname, &cause) != 0) { if (paste_set(bufdata, bufsize, bufname, &cause) != 0) {
cmdq_error(cmdq, "%s", cause); cmdq_error(cmdq, "%s", cause);
free(pdata); free(bufdata);
free(cause); free(cause);
return (CMD_RETURN_ERROR); return (CMD_RETURN_ERROR);
} }

View File

@ -1074,10 +1074,12 @@ void
format_defaults_paste_buffer(struct format_tree *ft, struct paste_buffer *pb, format_defaults_paste_buffer(struct format_tree *ft, struct paste_buffer *pb,
int utf8flag) int utf8flag)
{ {
size_t bufsize;
char *s; char *s;
format_add(ft, "buffer_size", "%zu", pb->size); paste_buffer_data(pb, &bufsize);
format_add(ft, "buffer_name", "%s", pb->name); format_add(ft, "buffer_size", "%zu", bufsize);
format_add(ft, "buffer_name", "%s", paste_buffer_name(pb));
s = paste_make_sample(pb, utf8flag); s = paste_make_sample(pb, utf8flag);
format_add(ft, "buffer_sample", "%s", s); format_add(ft, "buffer_sample", "%s", s);

34
paste.c
View File

@ -30,6 +30,18 @@
* string! * string!
*/ */
struct paste_buffer {
char *data;
size_t size;
char *name;
int automatic;
u_int order;
RB_ENTRY(paste_buffer) name_entry;
RB_ENTRY(paste_buffer) time_entry;
};
u_int paste_next_index; u_int paste_next_index;
u_int paste_next_order; u_int paste_next_order;
u_int paste_num_automatic; u_int paste_num_automatic;
@ -60,6 +72,22 @@ paste_cmp_times(const struct paste_buffer *a, const struct paste_buffer *b)
return (0); return (0);
} }
/* Get paste buffer name. */
const char *
paste_buffer_name(struct paste_buffer *pb)
{
return (pb->name);
}
/* Get paste buffer data. */
const char *
paste_buffer_data(struct paste_buffer *pb, size_t *size)
{
if (size != NULL)
*size = pb->size;
return (pb->data);
}
/* Walk paste buffers by name. */ /* Walk paste buffers by name. */
struct paste_buffer * struct paste_buffer *
paste_walk(struct paste_buffer *pb) paste_walk(struct paste_buffer *pb)
@ -71,13 +99,15 @@ paste_walk(struct paste_buffer *pb)
/* Get the most recent automatic buffer. */ /* Get the most recent automatic buffer. */
struct paste_buffer * struct paste_buffer *
paste_get_top(void) paste_get_top(const char **name)
{ {
struct paste_buffer *pb; struct paste_buffer *pb;
pb = RB_MIN(paste_time_tree, &paste_by_time); pb = RB_MIN(paste_time_tree, &paste_by_time);
if (pb == NULL) if (pb == NULL)
return (NULL); return (NULL);
if (name != NULL)
*name = pb->name;
return (pb); return (pb);
} }
@ -87,7 +117,7 @@ paste_free_top(void)
{ {
struct paste_buffer *pb; struct paste_buffer *pb;
pb = paste_get_top(); pb = paste_get_top(NULL);
if (pb == NULL) if (pb == NULL)
return (-1); return (-1);
return (paste_free_name(pb->name)); return (paste_free_name(pb->name));

View File

@ -815,10 +815,9 @@ status_prompt_key(struct client *c, int key)
struct options *oo = &sess->options; struct options *oo = &sess->options;
struct paste_buffer *pb; struct paste_buffer *pb;
char *s, *first, *last, word[64], swapc; char *s, *first, *last, word[64], swapc;
const char *histstr; const char *histstr, *bufdata, *wsep = NULL;
const char *wsep = NULL;
u_char ch; u_char ch;
size_t size, n, off, idx; size_t size, n, off, idx, bufsize;
size = strlen(c->prompt_buffer); size = strlen(c->prompt_buffer);
switch (mode_key_lookup(&c->prompt_mdata, key, NULL)) { switch (mode_key_lookup(&c->prompt_mdata, key, NULL)) {
@ -1067,24 +1066,25 @@ status_prompt_key(struct client *c, int key)
c->flags |= CLIENT_STATUS; c->flags |= CLIENT_STATUS;
break; break;
case MODEKEYEDIT_PASTE: case MODEKEYEDIT_PASTE:
if ((pb = paste_get_top()) == NULL) if ((pb = paste_get_top(NULL)) == NULL)
break; break;
for (n = 0; n < pb->size; n++) { bufdata = paste_buffer_data(pb, &bufsize);
ch = (u_char) pb->data[n]; for (n = 0; n < bufsize; n++) {
ch = (u_char)bufdata[n];
if (ch < 32 || ch == 127) if (ch < 32 || ch == 127)
break; break;
} }
c->prompt_buffer = xrealloc(c->prompt_buffer, size + n + 1); c->prompt_buffer = xrealloc(c->prompt_buffer, size + n + 1);
if (c->prompt_index == size) { if (c->prompt_index == size) {
memcpy(c->prompt_buffer + c->prompt_index, pb->data, n); memcpy(c->prompt_buffer + c->prompt_index, bufdata, n);
c->prompt_index += n; c->prompt_index += n;
c->prompt_buffer[c->prompt_index] = '\0'; c->prompt_buffer[c->prompt_index] = '\0';
} else { } else {
memmove(c->prompt_buffer + c->prompt_index + n, memmove(c->prompt_buffer + c->prompt_index + n,
c->prompt_buffer + c->prompt_index, c->prompt_buffer + c->prompt_index,
size + 1 - c->prompt_index); size + 1 - c->prompt_index);
memcpy(c->prompt_buffer + c->prompt_index, pb->data, n); memcpy(c->prompt_buffer + c->prompt_index, bufdata, n);
c->prompt_index += n; c->prompt_index += n;
} }

42
tmux.h
View File

@ -957,19 +957,6 @@ struct layout_cell {
TAILQ_ENTRY(layout_cell) entry; TAILQ_ENTRY(layout_cell) entry;
}; };
/* Paste buffer. */
struct paste_buffer {
char *data;
size_t size;
char *name;
int automatic;
u_int order;
RB_ENTRY(paste_buffer) name_entry;
RB_ENTRY(paste_buffer) time_entry;
};
/* Environment variable. */ /* Environment variable. */
struct environ_entry { struct environ_entry {
char *name; char *name;
@ -1452,6 +1439,22 @@ void cfg_add_cause(const char *, ...);
void cfg_print_causes(struct cmd_q *); void cfg_print_causes(struct cmd_q *);
void cfg_show_causes(struct session *); void cfg_show_causes(struct session *);
/* paste.c */
struct paste_buffer;
const char *paste_buffer_name(struct paste_buffer *);
const char *paste_buffer_data(struct paste_buffer *, size_t *);
struct paste_buffer *paste_walk(struct paste_buffer *);
struct paste_buffer *paste_get_top(const char **);
struct paste_buffer *paste_get_name(const char *);
int paste_free_top(void);
int paste_free_name(const char *);
void paste_add(char *, size_t);
int paste_rename(const char *, const char *, char **);
int paste_set(char *, size_t, const char *, char **);
char *paste_make_sample(struct paste_buffer *, int);
void paste_send_pane(struct paste_buffer *, struct window_pane *,
const char *, int);
/* format.c */ /* format.c */
struct format_tree; struct format_tree;
struct format_tree *format_create(void); struct format_tree *format_create(void);
@ -1636,19 +1639,6 @@ void tty_keys_build(struct tty *);
void tty_keys_free(struct tty *); void tty_keys_free(struct tty *);
int tty_keys_next(struct tty *); int tty_keys_next(struct tty *);
/* paste.c */
struct paste_buffer *paste_walk(struct paste_buffer *);
struct paste_buffer *paste_get_top(void);
struct paste_buffer *paste_get_name(const char *);
int paste_free_top(void);
int paste_free_name(const char *);
void paste_add(char *, size_t);
int paste_rename(const char *, const char *, char **);
int paste_set(char *, size_t, const char *, char **);
char *paste_make_sample(struct paste_buffer *, int);
void paste_send_pane(struct paste_buffer *, struct window_pane *,
const char *, int);
/* arguments.c */ /* arguments.c */
int args_cmp(struct args_entry *, struct args_entry *); int args_cmp(struct args_entry *, struct args_entry *);
RB_PROTOTYPE(args_tree, args_entry, entry, args_cmp); RB_PROTOTYPE(args_tree, args_entry, entry, args_cmp);

View File

@ -782,7 +782,8 @@ window_copy_key_input(struct window_pane *wp, int key)
{ {
struct window_copy_mode_data *data = wp->modedata; struct window_copy_mode_data *data = wp->modedata;
struct screen *s = &data->screen; struct screen *s = &data->screen;
size_t inputlen, n; const char *bufdata;
size_t inputlen, n, bufsize;
int np; int np;
struct paste_buffer *pb; struct paste_buffer *pb;
u_char ch; u_char ch;
@ -800,17 +801,18 @@ window_copy_key_input(struct window_pane *wp, int key)
*data->inputstr = '\0'; *data->inputstr = '\0';
break; break;
case MODEKEYEDIT_PASTE: case MODEKEYEDIT_PASTE:
if ((pb = paste_get_top()) == NULL) if ((pb = paste_get_top(NULL)) == NULL)
break; break;
for (n = 0; n < pb->size; n++) { bufdata = paste_buffer_data(pb, &bufsize);
ch = (u_char) pb->data[n]; for (n = 0; n < bufsize; n++) {
ch = (u_char)bufdata[n];
if (ch < 32 || ch == 127) if (ch < 32 || ch == 127)
break; break;
} }
inputlen = strlen(data->inputstr); inputlen = strlen(data->inputstr);
data->inputstr = xrealloc(data->inputstr, inputlen + n + 1); data->inputstr = xrealloc(data->inputstr, inputlen + n + 1);
memcpy(data->inputstr + inputlen, pb->data, n); memcpy(data->inputstr + inputlen, bufdata, n);
data->inputstr[inputlen + n] = '\0'; data->inputstr[inputlen + n] = '\0';
break; break;
case MODEKEYEDIT_ENTER: case MODEKEYEDIT_ENTER:
@ -1491,7 +1493,8 @@ window_copy_append_selection(struct window_pane *wp, const char *bufname)
{ {
char *buf; char *buf;
struct paste_buffer *pb; struct paste_buffer *pb;
size_t len; const char *bufdata;
size_t len, bufsize;
struct screen_write_ctx ctx; struct screen_write_ctx ctx;
buf = window_copy_get_selection(wp, &len); buf = window_copy_get_selection(wp, &len);
@ -1504,17 +1507,16 @@ window_copy_append_selection(struct window_pane *wp, const char *bufname)
screen_write_stop(&ctx); screen_write_stop(&ctx);
} }
if (bufname == NULL || *bufname == '\0') { if (bufname == NULL || *bufname == '\0')
pb = paste_get_top(); pb = paste_get_top(&bufname);
if (pb != NULL) else
bufname = pb->name;
} else
pb = paste_get_name(bufname); pb = paste_get_name(bufname);
if (pb != NULL) { if (pb != NULL) {
buf = xrealloc(buf, len + pb->size); bufdata = paste_buffer_data(pb, &bufsize);
memmove(buf + pb->size, buf, len); buf = xrealloc(buf, len + bufsize);
memcpy(buf, pb->data, pb->size); memmove(buf + bufsize, buf, len);
len += pb->size; memcpy(buf, bufdata, bufsize);
len += bufsize;
} }
if (paste_set(buf, len, bufname, NULL) != 0) if (paste_set(buf, len, bufname, NULL) != 0)
free(buf); free(buf);