Skip to content

Commit

Permalink
Move struct paste_buffer out of tmux.h.
Browse files Browse the repository at this point in the history
  • Loading branch information
nicm committed Aug 29, 2015
1 parent b9f0571 commit b569585
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 88 deletions.
4 changes: 2 additions & 2 deletions cmd-choose-buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ cmd_choose_buffer_exec(struct cmd *self, struct cmd_q *cmdq)
return (CMD_RETURN_ERROR);
utf8flag = options_get_number(&wl->window->options, "utf8");

if (paste_get_top() == NULL)
if (paste_get_top(NULL) == NULL)
return (CMD_RETURN_NORMAL);

if (window_pane_set_mode(wl->window->active, &window_choose_mode) != 0)
Expand All @@ -85,7 +85,7 @@ cmd_choose_buffer_exec(struct cmd *self, struct cmd_q *cmdq)
cdata->ft_template = xstrdup(template);
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);
free(action_data);

Expand Down
2 changes: 1 addition & 1 deletion cmd-paste-buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ cmd_paste_buffer_exec(struct cmd *self, struct cmd_q *cmdq)
bufname = args_get(args, 'b');

if (bufname == NULL)
pb = paste_get_top();
pb = paste_get_top(NULL);
else {
pb = paste_get_name(bufname);
if (pb == NULL) {
Expand Down
21 changes: 11 additions & 10 deletions cmd-save-buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,14 @@ cmd_save_buffer_exec(struct cmd *self, struct cmd_q *cmdq)
struct client *c = cmdq->client;
struct session *s;
struct paste_buffer *pb;
const char *path, *bufname;
const char *path, *bufname, *bufdata;
char *start, *end, *msg;
size_t size, used, msglen;
size_t size, used, msglen, bufsize;
int cwd, fd;
FILE *f;

if (!args_has(args, 'b')) {
if ((pb = paste_get_top()) == NULL) {
if ((pb = paste_get_top(NULL)) == NULL) {
cmdq_error(cmdq, "no buffers");
return (CMD_RETURN_ERROR);
}
Expand All @@ -76,6 +76,7 @@ cmd_save_buffer_exec(struct cmd *self, struct cmd_q *cmdq)
return (CMD_RETURN_ERROR);
}
}
bufdata = paste_buffer_data(pb, &bufsize);

if (self->entry == &cmd_show_buffer_entry)
path = "-";
Expand Down Expand Up @@ -114,7 +115,7 @@ cmd_save_buffer_exec(struct cmd *self, struct cmd_q *cmdq)
cmdq_error(cmdq, "%s: %s", path, strerror(errno));
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);
fclose(f);
return (CMD_RETURN_ERROR);
Expand All @@ -124,25 +125,25 @@ cmd_save_buffer_exec(struct cmd *self, struct cmd_q *cmdq)
return (CMD_RETURN_NORMAL);

do_stdout:
evbuffer_add(c->stdout_data, pb->data, pb->size);
evbuffer_add(c->stdout_data, bufdata, bufsize);
server_push_stdout(c);
return (CMD_RETURN_NORMAL);

do_print:
if (pb->size > (INT_MAX / 4) - 1) {
if (bufsize > (INT_MAX / 4) - 1) {
cmdq_error(cmdq, "buffer too big");
return (CMD_RETURN_ERROR);
}
msg = NULL;

used = 0;
while (used != pb->size) {
start = pb->data + used;
end = memchr(start, '\n', pb->size - used);
while (used != bufsize) {
start = bufdata + used;
end = memchr(start, '\n', bufsize - used);
if (end != NULL)
size = end - start;
else
size = pb->size - used;
size = bufsize - used;

msglen = size * 4 + 1;
msg = xrealloc(msg, msglen);
Expand Down
39 changes: 17 additions & 22 deletions cmd-set-buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ cmd_set_buffer_exec(struct cmd *self, struct cmd_q *cmdq)
{
struct args *args = self->args;
struct paste_buffer *pb;
char *pdata, *cause;
const char *bufname;
size_t psize, newsize;
char *bufdata, *cause;
const char *bufname, *olddata;
size_t bufsize, newsize;

bufname = NULL;

Expand All @@ -58,12 +58,11 @@ cmd_set_buffer_exec(struct cmd *self, struct cmd_q *cmdq)
bufname = args_get(args, 'b');

if (bufname == NULL) {
pb = paste_get_top();
pb = paste_get_top(&bufname);
if (pb == NULL) {
cmdq_error(cmdq, "no buffer");
return (CMD_RETURN_ERROR);
}
bufname = pb->name;
}

if (paste_rename(bufname, args_get(args, 'n'), &cause) != 0) {
Expand All @@ -79,37 +78,33 @@ cmd_set_buffer_exec(struct cmd *self, struct cmd_q *cmdq)
cmdq_error(cmdq, "no data specified");
return (CMD_RETURN_ERROR);
}

psize = 0;
pdata = NULL;

pb = NULL;

bufsize = 0;
bufdata = NULL;

if ((newsize = strlen(args->argv[0])) == 0)
return (CMD_RETURN_NORMAL);

if (args_has(args, 'b')) {
bufname = args_get(args, 'b');
pb = paste_get_name(bufname);
} else if (args_has(args, 'a')) {
pb = paste_get_top();
if (pb != NULL)
bufname = pb->name;
}
} else if (args_has(args, 'a'))
pb = paste_get_top(&bufname);

if (args_has(args, 'a') && pb != NULL) {
psize = pb->size;
pdata = xmalloc(psize);
memcpy(pdata, pb->data, psize);
olddata = paste_buffer_data(pb, &bufsize);
bufdata = xmalloc(bufsize);
memcpy(bufdata, olddata, bufsize);
}

pdata = xrealloc(pdata, psize + newsize);
memcpy(pdata + psize, args->argv[0], newsize);
psize += newsize;
bufdata = xrealloc(bufdata, bufsize + newsize);
memcpy(bufdata + bufsize, args->argv[0], newsize);
bufsize += newsize;

if (paste_set(pdata, psize, bufname, &cause) != 0) {
if (paste_set(bufdata, bufsize, bufname, &cause) != 0) {
cmdq_error(cmdq, "%s", cause);
free(pdata);
free(bufdata);
free(cause);
return (CMD_RETURN_ERROR);
}
Expand Down
6 changes: 4 additions & 2 deletions format.c
Original file line number Diff line number Diff line change
Expand Up @@ -1074,10 +1074,12 @@ void
format_defaults_paste_buffer(struct format_tree *ft, struct paste_buffer *pb,
int utf8flag)
{
size_t bufsize;
char *s;

format_add(ft, "buffer_size", "%zu", pb->size);
format_add(ft, "buffer_name", "%s", pb->name);
paste_buffer_data(pb, &bufsize);
format_add(ft, "buffer_size", "%zu", bufsize);
format_add(ft, "buffer_name", "%s", paste_buffer_name(pb));

s = paste_make_sample(pb, utf8flag);
format_add(ft, "buffer_sample", "%s", s);
Expand Down
34 changes: 32 additions & 2 deletions paste.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@
* 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_order;
u_int paste_num_automatic;
Expand Down Expand Up @@ -60,6 +72,22 @@ paste_cmp_times(const struct paste_buffer *a, const struct paste_buffer *b)
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. */
struct paste_buffer *
paste_walk(struct paste_buffer *pb)
Expand All @@ -71,13 +99,15 @@ paste_walk(struct paste_buffer *pb)

/* Get the most recent automatic buffer. */
struct paste_buffer *
paste_get_top(void)
paste_get_top(const char **name)
{
struct paste_buffer *pb;

pb = RB_MIN(paste_time_tree, &paste_by_time);
if (pb == NULL)
return (NULL);
if (name != NULL)
*name = pb->name;
return (pb);
}

Expand All @@ -87,7 +117,7 @@ paste_free_top(void)
{
struct paste_buffer *pb;

pb = paste_get_top();
pb = paste_get_top(NULL);
if (pb == NULL)
return (-1);
return (paste_free_name(pb->name));
Expand Down
16 changes: 8 additions & 8 deletions status.c
Original file line number Diff line number Diff line change
Expand Up @@ -815,10 +815,9 @@ status_prompt_key(struct client *c, int key)
struct options *oo = &sess->options;
struct paste_buffer *pb;
char *s, *first, *last, word[64], swapc;
const char *histstr;
const char *wsep = NULL;
const char *histstr, *bufdata, *wsep = NULL;
u_char ch;
size_t size, n, off, idx;
size_t size, n, off, idx, bufsize;

size = strlen(c->prompt_buffer);
switch (mode_key_lookup(&c->prompt_mdata, key, NULL)) {
Expand Down Expand Up @@ -1067,24 +1066,25 @@ status_prompt_key(struct client *c, int key)
c->flags |= CLIENT_STATUS;
break;
case MODEKEYEDIT_PASTE:
if ((pb = paste_get_top()) == NULL)
if ((pb = paste_get_top(NULL)) == NULL)
break;
for (n = 0; n < pb->size; n++) {
ch = (u_char) pb->data[n];
bufdata = paste_buffer_data(pb, &bufsize);
for (n = 0; n < bufsize; n++) {
ch = (u_char)bufdata[n];
if (ch < 32 || ch == 127)
break;
}

c->prompt_buffer = xrealloc(c->prompt_buffer, size + n + 1);
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_buffer[c->prompt_index] = '\0';
} else {
memmove(c->prompt_buffer + c->prompt_index + n,
c->prompt_buffer + 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;
}

Expand Down
42 changes: 16 additions & 26 deletions tmux.h
Original file line number Diff line number Diff line change
Expand Up @@ -957,19 +957,6 @@ struct layout_cell {
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. */
struct environ_entry {
char *name;
Expand Down Expand Up @@ -1452,6 +1439,22 @@ void cfg_add_cause(const char *, ...);
void cfg_print_causes(struct cmd_q *);
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 */
struct format_tree;
struct format_tree *format_create(void);
Expand Down Expand Up @@ -1636,19 +1639,6 @@ void tty_keys_build(struct tty *);
void tty_keys_free(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 */
int args_cmp(struct args_entry *, struct args_entry *);
RB_PROTOTYPE(args_tree, args_entry, entry, args_cmp);
Expand Down
Loading

0 comments on commit b569585

Please sign in to comment.