From bebc73f12fb20d0fe5d2665112e006eb30001352 Mon Sep 17 00:00:00 2001 From: nicm Date: Fri, 21 Apr 2017 14:01:19 +0000 Subject: [PATCH] Store state shared between multiple commands in the queue in a shared structure. --- usr.bin/tmux/cmd-attach-session.c | 4 ++-- usr.bin/tmux/cmd-copy-mode.c | 7 ++++--- usr.bin/tmux/cmd-find.c | 8 ++++---- usr.bin/tmux/cmd-if-shell.c | 7 ++++--- usr.bin/tmux/cmd-list-keys.c | 6 +++--- usr.bin/tmux/cmd-new-session.c | 4 ++-- usr.bin/tmux/cmd-queue.c | 32 ++++++++++++++++++------------- usr.bin/tmux/cmd-resize-pane.c | 7 ++++--- usr.bin/tmux/cmd-send-keys.c | 4 ++-- usr.bin/tmux/cmd-switch-client.c | 4 ++-- usr.bin/tmux/format.c | 12 +++++++----- usr.bin/tmux/key-bindings.c | 10 ++++++---- usr.bin/tmux/server-client.c | 7 ++++--- usr.bin/tmux/tmux.h | 27 ++++++++++++++++++-------- 14 files changed, 82 insertions(+), 57 deletions(-) diff --git a/usr.bin/tmux/cmd-attach-session.c b/usr.bin/tmux/cmd-attach-session.c index 8136886b634..d9af1a2cd15 100644 --- a/usr.bin/tmux/cmd-attach-session.c +++ b/usr.bin/tmux/cmd-attach-session.c @@ -1,4 +1,4 @@ -/* $OpenBSD: cmd-attach-session.c,v 1.70 2017/03/08 13:36:12 nicm Exp $ */ +/* $OpenBSD: cmd-attach-session.c,v 1.71 2017/04/21 14:01:19 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott @@ -92,7 +92,7 @@ cmd_attach_session(struct cmdq_item *item, int dflag, int rflag, environ_update(s->options, c->environ, s->environ); c->session = s; - if (!item->repeat) + if (~item->shared->flags & CMDQ_SHARED_REPEAT) server_client_set_key_table(c, NULL); status_timer_start(c); notify_client("client-session-changed", c); diff --git a/usr.bin/tmux/cmd-copy-mode.c b/usr.bin/tmux/cmd-copy-mode.c index 5a85d7bb293..dd0018378bb 100644 --- a/usr.bin/tmux/cmd-copy-mode.c +++ b/usr.bin/tmux/cmd-copy-mode.c @@ -1,4 +1,4 @@ -/* $OpenBSD: cmd-copy-mode.c,v 1.31 2016/10/16 19:04:05 nicm Exp $ */ +/* $OpenBSD: cmd-copy-mode.c,v 1.32 2017/04/21 14:01:19 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott @@ -56,12 +56,13 @@ static enum cmd_retval cmd_copy_mode_exec(struct cmd *self, struct cmdq_item *item) { struct args *args = self->args; + struct cmdq_shared *shared = item->shared; struct client *c = item->client; struct session *s; struct window_pane *wp = item->state.tflag.wp; if (args_has(args, 'M')) { - if ((wp = cmd_mouse_pane(&item->mouse, &s, NULL)) == NULL) + if ((wp = cmd_mouse_pane(&shared->mouse, &s, NULL)) == NULL) return (CMD_RETURN_NORMAL); if (c == NULL || c->session != s) return (CMD_RETURN_NORMAL); @@ -80,7 +81,7 @@ cmd_copy_mode_exec(struct cmd *self, struct cmdq_item *item) if (args_has(args, 'M')) { if (wp->mode != NULL && wp->mode != &window_copy_mode) return (CMD_RETURN_NORMAL); - window_copy_start_drag(c, &item->mouse); + window_copy_start_drag(c, &shared->mouse); } if (wp->mode == &window_copy_mode && args_has(self->args, 'u')) window_copy_pageup(wp, 0); diff --git a/usr.bin/tmux/cmd-find.c b/usr.bin/tmux/cmd-find.c index 7fb3438861b..84e9756afa3 100644 --- a/usr.bin/tmux/cmd-find.c +++ b/usr.bin/tmux/cmd-find.c @@ -1,4 +1,4 @@ -/* $OpenBSD: cmd-find.c,v 1.44 2017/04/05 11:04:48 nicm Exp $ */ +/* $OpenBSD: cmd-find.c,v 1.45 2017/04/21 14:01:19 nicm Exp $ */ /* * Copyright (c) 2015 Nicholas Marriott @@ -998,8 +998,8 @@ cmd_find_target(struct cmd_find_state *fs, struct cmd_find_state *current, if (server_check_marked() && (flags & CMD_FIND_DEFAULT_MARKED)) { fs->current = &marked_pane; log_debug("%s: current is marked pane", __func__); - } else if (cmd_find_valid_state(&item->current)) { - fs->current = &item->current; + } else if (cmd_find_valid_state(&item->shared->current)) { + fs->current = &item->shared->current; log_debug("%s: current is from queue", __func__); } else { fs->current = current; @@ -1015,7 +1015,7 @@ cmd_find_target(struct cmd_find_state *fs, struct cmd_find_state *current, /* Mouse target is a plain = or {mouse}. */ if (strcmp(target, "=") == 0 || strcmp(target, "{mouse}") == 0) { - m = &item->mouse; + m = &item->shared->mouse; switch (type) { case CMD_FIND_PANE: fs->wp = cmd_mouse_pane(m, &fs->s, &fs->wl); diff --git a/usr.bin/tmux/cmd-if-shell.c b/usr.bin/tmux/cmd-if-shell.c index b5029fbdabf..eec73e572d9 100644 --- a/usr.bin/tmux/cmd-if-shell.c +++ b/usr.bin/tmux/cmd-if-shell.c @@ -1,4 +1,4 @@ -/* $OpenBSD: cmd-if-shell.c,v 1.52 2017/04/20 09:20:22 nicm Exp $ */ +/* $OpenBSD: cmd-if-shell.c,v 1.53 2017/04/21 14:01:19 nicm Exp $ */ /* * Copyright (c) 2009 Tiago Cunha @@ -65,6 +65,7 @@ static enum cmd_retval cmd_if_shell_exec(struct cmd *self, struct cmdq_item *item) { struct args *args = self->args; + struct cmdq_shared *shared = item->shared; struct cmd_if_shell_data *cdata; char *shellcmd, *cmd, *cause; struct cmd_list *cmdlist; @@ -100,7 +101,7 @@ cmd_if_shell_exec(struct cmd *self, struct cmdq_item *item) } return (CMD_RETURN_ERROR); } - new_item = cmdq_get_command(cmdlist, NULL, &item->mouse, 0); + new_item = cmdq_get_command(cmdlist, NULL, &shared->mouse, 0); cmdq_insert_after(item, new_item); cmd_list_free(cmdlist); return (CMD_RETURN_NORMAL); @@ -125,7 +126,7 @@ cmd_if_shell_exec(struct cmd *self, struct cmdq_item *item) cdata->item = item; else cdata->item = NULL; - memcpy(&cdata->mouse, &item->mouse, sizeof cdata->mouse); + memcpy(&cdata->mouse, &shared->mouse, sizeof cdata->mouse); job_run(shellcmd, s, cwd, NULL, cmd_if_shell_callback, cmd_if_shell_free, cdata); diff --git a/usr.bin/tmux/cmd-list-keys.c b/usr.bin/tmux/cmd-list-keys.c index 8a378e47050..aab3c77bf67 100644 --- a/usr.bin/tmux/cmd-list-keys.c +++ b/usr.bin/tmux/cmd-list-keys.c @@ -1,4 +1,4 @@ -/* $OpenBSD: cmd-list-keys.c,v 1.42 2017/02/03 11:57:27 nicm Exp $ */ +/* $OpenBSD: cmd-list-keys.c,v 1.43 2017/04/21 14:01:19 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott @@ -81,7 +81,7 @@ cmd_list_keys_exec(struct cmd *self, struct cmdq_item *item) RB_FOREACH(bd, key_bindings, &table->key_bindings) { key = key_string_lookup_key(bd->key); - if (bd->can_repeat) + if (bd->flags & KEY_BINDING_REPEAT) repeat = 1; width = utf8_cstrwidth(table->name); @@ -101,7 +101,7 @@ cmd_list_keys_exec(struct cmd *self, struct cmdq_item *item) if (!repeat) r = ""; - else if (bd->can_repeat) + else if (bd->flags & KEY_BINDING_REPEAT) r = "-r "; else r = " "; diff --git a/usr.bin/tmux/cmd-new-session.c b/usr.bin/tmux/cmd-new-session.c index 337d2e80173..341dd8f6bde 100644 --- a/usr.bin/tmux/cmd-new-session.c +++ b/usr.bin/tmux/cmd-new-session.c @@ -1,4 +1,4 @@ -/* $OpenBSD: cmd-new-session.c,v 1.102 2017/03/08 13:36:12 nicm Exp $ */ +/* $OpenBSD: cmd-new-session.c,v 1.103 2017/04/21 14:01:19 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott @@ -297,7 +297,7 @@ cmd_new_session_exec(struct cmd *self, struct cmdq_item *item) } else if (c->session != NULL) c->last_session = c->session; c->session = s; - if (!item->repeat) + if (~item->shared->flags & CMDQ_SHARED_REPEAT) server_client_set_key_table(c, NULL); status_timer_start(c); notify_client("client-session-changed", c); diff --git a/usr.bin/tmux/cmd-queue.c b/usr.bin/tmux/cmd-queue.c index f1f7512a7a8..b4313ea2ec4 100644 --- a/usr.bin/tmux/cmd-queue.c +++ b/usr.bin/tmux/cmd-queue.c @@ -1,4 +1,4 @@ -/* $OpenBSD: cmd-queue.c,v 1.48 2017/02/03 11:57:27 nicm Exp $ */ +/* $OpenBSD: cmd-queue.c,v 1.49 2017/04/21 14:01:19 nicm Exp $ */ /* * Copyright (c) 2013 Nicholas Marriott @@ -102,8 +102,11 @@ cmdq_insert_after(struct cmdq_item *after, struct cmdq_item *item) static void cmdq_remove(struct cmdq_item *item) { - if (item->formats != NULL) - format_free(item->formats); + if (item->shared != NULL && --item->shared->references == 0) { + if (item->shared->formats != NULL) + format_free(item->shared->formats); + free(item->shared); + } if (item->client != NULL) server_client_unref(item->client); @@ -150,6 +153,13 @@ cmdq_get_command(struct cmd_list *cmdlist, struct cmd_find_state *current, struct cmd *cmd; u_int group = cmdq_next_group(); char *tmp; + struct cmdq_shared *shared; + + shared = xcalloc(1, sizeof *shared); + if (current != NULL) + cmd_find_copy_state(&shared->current, current); + if (m != NULL) + memcpy(&shared->mouse, m, sizeof shared->mouse); TAILQ_FOREACH(cmd, &cmdlist->list, qentry) { xasprintf(&tmp, "command[%s]", cmd->entry->name); @@ -161,13 +171,11 @@ cmdq_get_command(struct cmd_list *cmdlist, struct cmd_find_state *current, item->group = group; item->flags = flags; + item->shared = shared; item->cmdlist = cmdlist; item->cmd = cmd; - if (current != NULL) - cmd_find_copy_state(&item->current, current); - if (m != NULL) - memcpy(&item->mouse, m, sizeof item->mouse); + shared->references++; cmdlist->references++; if (first == NULL) @@ -258,19 +266,17 @@ cmdq_fire_callback(struct cmdq_item *item) void cmdq_format(struct cmdq_item *item, const char *key, const char *fmt, ...) { + struct cmdq_shared *shared = item->shared; va_list ap; - struct cmdq_item *loop; char *value; va_start(ap, fmt); xvasprintf(&value, fmt, ap); va_end(ap); - for (loop = item; loop != NULL; loop = item->next) { - if (loop->formats == NULL) - loop->formats = format_create(NULL, FORMAT_NONE, 0); - format_add(loop->formats, key, "%s", value); - } + if (shared->formats == NULL) + shared->formats = format_create(NULL, FORMAT_NONE, 0); + format_add(shared->formats, key, "%s", value); free(value); } diff --git a/usr.bin/tmux/cmd-resize-pane.c b/usr.bin/tmux/cmd-resize-pane.c index c3cabafa0e0..4dc020e49f4 100644 --- a/usr.bin/tmux/cmd-resize-pane.c +++ b/usr.bin/tmux/cmd-resize-pane.c @@ -1,4 +1,4 @@ -/* $OpenBSD: cmd-resize-pane.c,v 1.28 2016/10/16 19:04:05 nicm Exp $ */ +/* $OpenBSD: cmd-resize-pane.c,v 1.29 2017/04/21 14:01:19 nicm Exp $ */ /* * Copyright (c) 2009 Nicholas Marriott @@ -49,6 +49,7 @@ static enum cmd_retval cmd_resize_pane_exec(struct cmd *self, struct cmdq_item *item) { struct args *args = self->args; + struct cmdq_shared *shared = item->shared; struct window_pane *wp = item->state.tflag.wp; struct winlink *wl = item->state.tflag.wl; struct window *w = wl->window; @@ -60,12 +61,12 @@ cmd_resize_pane_exec(struct cmd *self, struct cmdq_item *item) int x, y; if (args_has(args, 'M')) { - if (cmd_mouse_window(&item->mouse, &s) == NULL) + if (cmd_mouse_window(&shared->mouse, &s) == NULL) return (CMD_RETURN_NORMAL); if (c == NULL || c->session != s) return (CMD_RETURN_NORMAL); c->tty.mouse_drag_update = cmd_resize_pane_mouse_update; - cmd_resize_pane_mouse_update(c, &item->mouse); + cmd_resize_pane_mouse_update(c, &shared->mouse); return (CMD_RETURN_NORMAL); } diff --git a/usr.bin/tmux/cmd-send-keys.c b/usr.bin/tmux/cmd-send-keys.c index 636f4dd757e..75c6d379879 100644 --- a/usr.bin/tmux/cmd-send-keys.c +++ b/usr.bin/tmux/cmd-send-keys.c @@ -1,4 +1,4 @@ -/* $OpenBSD: cmd-send-keys.c,v 1.37 2017/01/07 15:28:13 nicm Exp $ */ +/* $OpenBSD: cmd-send-keys.c,v 1.38 2017/04/21 14:01:19 nicm Exp $ */ /* * Copyright (c) 2008 Nicholas Marriott @@ -62,7 +62,7 @@ cmd_send_keys_exec(struct cmd *self, struct cmdq_item *item) struct client *c = item->state.c; struct window_pane *wp = item->state.tflag.wp; struct session *s = item->state.tflag.s; - struct mouse_event *m = &item->mouse; + struct mouse_event *m = &item->shared->mouse; struct utf8_data *ud, *uc; wchar_t wc; int i, literal; diff --git a/usr.bin/tmux/cmd-switch-client.c b/usr.bin/tmux/cmd-switch-client.c index 775b545b2d1..71837df72be 100644 --- a/usr.bin/tmux/cmd-switch-client.c +++ b/usr.bin/tmux/cmd-switch-client.c @@ -1,4 +1,4 @@ -/* $OpenBSD: cmd-switch-client.c,v 1.48 2017/02/06 15:00:41 nicm Exp $ */ +/* $OpenBSD: cmd-switch-client.c,v 1.49 2017/04/21 14:01:19 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott @@ -108,7 +108,7 @@ cmd_switch_client_exec(struct cmd *self, struct cmdq_item *item) if (c->session != NULL && c->session != s) c->last_session = c->session; c->session = s; - if (!item->repeat) + if (~item->shared->flags & CMDQ_SHARED_REPEAT) server_client_set_key_table(c, NULL); status_timer_start(c); session_update_activity(s, NULL); diff --git a/usr.bin/tmux/format.c b/usr.bin/tmux/format.c index 525cfc105a3..f5bd2794942 100644 --- a/usr.bin/tmux/format.c +++ b/usr.bin/tmux/format.c @@ -1,4 +1,4 @@ -/* $OpenBSD: format.c,v 1.129 2017/04/20 09:43:45 nicm Exp $ */ +/* $OpenBSD: format.c,v 1.130 2017/04/21 14:01:19 nicm Exp $ */ /* * Copyright (c) 2011 Nicholas Marriott @@ -554,10 +554,12 @@ format_create(struct cmdq_item *item, int tag, int flags) format_add(ft, "socket_path", "%s", socket_path); format_add_tv(ft, "start_time", &start_time); - if (item != NULL && item->cmd != NULL) - format_add(ft, "command", "%s", item->cmd->entry->name); - if (item != NULL && item->formats != NULL) - format_merge(ft, item->formats); + if (item != NULL) { + if (item->cmd != NULL) + format_add(ft, "command", "%s", item->cmd->entry->name); + if (item->shared != NULL && item->shared->formats != NULL) + format_merge(ft, item->shared->formats); + } return (ft); } diff --git a/usr.bin/tmux/key-bindings.c b/usr.bin/tmux/key-bindings.c index 206665ceb73..39be6aaa6ba 100644 --- a/usr.bin/tmux/key-bindings.c +++ b/usr.bin/tmux/key-bindings.c @@ -1,4 +1,4 @@ -/* $OpenBSD: key-bindings.c,v 1.74 2017/04/05 12:14:18 nicm Exp $ */ +/* $OpenBSD: key-bindings.c,v 1.75 2017/04/21 14:01:19 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott @@ -84,7 +84,7 @@ key_bindings_unref_table(struct key_table *table) } void -key_bindings_add(const char *name, key_code key, int can_repeat, +key_bindings_add(const char *name, key_code key, int repeat, struct cmd_list *cmdlist) { struct key_table *table; @@ -104,7 +104,8 @@ key_bindings_add(const char *name, key_code key, int can_repeat, bd->key = key; RB_INSERT(key_bindings, &table->key_bindings, bd); - bd->can_repeat = can_repeat; + if (repeat) + bd->flags |= KEY_BINDING_REPEAT; bd->cmdlist = cmdlist; } @@ -415,7 +416,8 @@ key_bindings_dispatch(struct key_binding *bd, struct client *c, cmdq_append(c, cmdq_get_callback(key_bindings_read_only, NULL)); else { item = cmdq_get_command(bd->cmdlist, fs, m, 0); - item->repeat = bd->can_repeat; + if (bd->flags & KEY_BINDING_REPEAT) + item->shared->flags |= CMDQ_SHARED_REPEAT; cmdq_append(c, item); } } diff --git a/usr.bin/tmux/server-client.c b/usr.bin/tmux/server-client.c index 9fcf8d01fcc..cf6d687fc37 100644 --- a/usr.bin/tmux/server-client.c +++ b/usr.bin/tmux/server-client.c @@ -1,4 +1,4 @@ -/* $OpenBSD: server-client.c,v 1.221 2017/04/20 15:16:20 nicm Exp $ */ +/* $OpenBSD: server-client.c,v 1.222 2017/04/21 14:01:19 nicm Exp $ */ /* * Copyright (c) 2009 Nicholas Marriott @@ -908,7 +908,8 @@ retry: * non-repeating binding was found, stop repeating and try * again in the root table. */ - if ((c->flags & CLIENT_REPEAT) && !bd->can_repeat) { + if ((c->flags & CLIENT_REPEAT) && + (~bd->flags & KEY_BINDING_REPEAT)) { server_client_set_key_table(c, NULL); c->flags &= ~CLIENT_REPEAT; server_status_client(c); @@ -926,7 +927,7 @@ retry: * the client back to the root table. */ xtimeout = options_get_number(s->options, "repeat-time"); - if (xtimeout != 0 && bd->can_repeat) { + if (xtimeout != 0 && (bd->flags & KEY_BINDING_REPEAT)) { c->flags |= CLIENT_REPEAT; tv.tv_sec = xtimeout / 1000; diff --git a/usr.bin/tmux/tmux.h b/usr.bin/tmux/tmux.h index 3c46b8ded0b..2c02e644613 100644 --- a/usr.bin/tmux/tmux.h +++ b/usr.bin/tmux/tmux.h @@ -1,4 +1,4 @@ -/* $OpenBSD: tmux.h,v 1.744 2017/04/20 17:49:26 nicm Exp $ */ +/* $OpenBSD: tmux.h,v 1.745 2017/04/21 14:01:19 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott @@ -41,6 +41,7 @@ extern char **environ; struct args; struct client; struct cmdq_item; +struct cmdq_subitem; struct cmdq_list; struct environ; struct input_ctx; @@ -1204,6 +1205,19 @@ enum cmdq_type { CMDQ_CALLBACK, }; +/* Command queue item shared state. */ +struct cmdq_shared { + int references; + + int flags; +#define CMDQ_SHARED_REPEAT 0x1 + + struct format_tree *formats; + + struct mouse_event mouse; + struct cmd_find_state current; +}; + /* Command queue item. */ typedef enum cmd_retval (*cmdq_cb) (struct cmdq_item *, void *); struct cmdq_item { @@ -1219,25 +1233,20 @@ struct cmdq_item { u_int number; time_t time; - struct format_tree *formats; - int flags; #define CMDQ_FIRED 0x1 #define CMDQ_WAITING 0x2 #define CMDQ_NOHOOKS 0x4 + struct cmdq_shared *shared; struct cmd_list *cmdlist; struct cmd *cmd; - int repeat; cmdq_cb cb; void *data; - struct cmd_find_state current; struct cmd_state state; - struct mouse_event mouse; - TAILQ_ENTRY(cmdq_item) entry; }; TAILQ_HEAD(cmdq_list, cmdq_item); @@ -1394,7 +1403,9 @@ TAILQ_HEAD(clients, client); struct key_binding { key_code key; struct cmd_list *cmdlist; - int can_repeat; + + int flags; +#define KEY_BINDING_REPEAT 0x1 RB_ENTRY(key_binding) entry; }; -- 2.20.1