diff options
author | Nicholas Marriott <nicm@cvs.openbsd.org> | 2010-06-26 18:20:54 +0000 |
---|---|---|
committer | Nicholas Marriott <nicm@cvs.openbsd.org> | 2010-06-26 18:20:54 +0000 |
commit | 93b2219252745f85862243d2e779585b67459f97 (patch) | |
tree | 9b7fde9bc295f473b4d27e29ceb0a10708bd794c /usr.bin | |
parent | 8304b91599f58cdfa3ec267b4f12a61214929f2e (diff) |
Setting the cmdlist pointer in the bind-key to NULL to prevent it being freed
after the command is executing is bogus because it may still be needed if the
same command is going to be executed again (for example if you "bind-key a
bind-key b ..."). Making a copy is hard, so instead add a reference count to
the cmd_list.
While here, also print bind-key -n and the rest of the flags properly.
Fixes problem reported by mcbride@.
Diffstat (limited to 'usr.bin')
-rw-r--r-- | usr.bin/tmux/cmd-bind-key.c | 15 | ||||
-rw-r--r-- | usr.bin/tmux/cmd-list.c | 22 | ||||
-rw-r--r-- | usr.bin/tmux/key-bindings.c | 9 | ||||
-rw-r--r-- | usr.bin/tmux/tmux.c | 4 | ||||
-rw-r--r-- | usr.bin/tmux/tmux.h | 7 |
5 files changed, 37 insertions, 20 deletions
diff --git a/usr.bin/tmux/cmd-bind-key.c b/usr.bin/tmux/cmd-bind-key.c index a7de6097b77..3171c7eed53 100644 --- a/usr.bin/tmux/cmd-bind-key.c +++ b/usr.bin/tmux/cmd-bind-key.c @@ -1,4 +1,4 @@ -/* $OpenBSD: cmd-bind-key.c,v 1.8 2010/01/23 17:50:56 nicm Exp $ */ +/* $OpenBSD: cmd-bind-key.c,v 1.9 2010/06/26 18:20:53 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net> @@ -130,7 +130,7 @@ cmd_bind_key_exec(struct cmd *self, unused struct cmd_ctx *ctx) return (cmd_bind_key_table(self, ctx)); key_bindings_add(data->key, data->can_repeat, data->cmdlist); - data->cmdlist = NULL; /* avoid free */ + data->cmdlist->references++; return (0); } @@ -192,8 +192,17 @@ cmd_bind_key_print(struct cmd *self, char *buf, size_t len) off += xsnprintf(buf, len, "%s", self->entry->name); if (data == NULL) return (off); + + if (off < len && data->command_key) + off += xsnprintf(buf + off, len - off, " -c"); + if (off < len && !(data->key & KEYC_PREFIX)) + off += xsnprintf(buf + off, len - off, " -n"); + if (off < len && data->can_repeat) + off += xsnprintf(buf + off, len - off, " -r"); + if (off < len && data->tablename != NULL) + off += cmd_prarg(buf + off, len - off, " -t ", data->tablename); if (off < len) { - skey = key_string_lookup_key(data->key); + skey = key_string_lookup_key(data->key & ~KEYC_PREFIX); off += xsnprintf(buf + off, len - off, " %s ", skey); } if (off < len) diff --git a/usr.bin/tmux/cmd-list.c b/usr.bin/tmux/cmd-list.c index f876cdb089b..f3de5d77730 100644 --- a/usr.bin/tmux/cmd-list.c +++ b/usr.bin/tmux/cmd-list.c @@ -1,4 +1,4 @@ -/* $OpenBSD: cmd-list.c,v 1.4 2010/03/22 19:14:55 nicm Exp $ */ +/* $OpenBSD: cmd-list.c,v 1.5 2010/06/26 18:20:53 nicm Exp $ */ /* * Copyright (c) 2009 Nicholas Marriott <nicm@users.sourceforge.net> @@ -32,7 +32,8 @@ cmd_list_parse(int argc, char **argv, char **cause) char **new_argv; cmdlist = xmalloc(sizeof *cmdlist); - TAILQ_INIT(cmdlist); + cmdlist->references = 1; + TAILQ_INIT(&cmdlist->list); lastsplit = 0; for (i = 0; i < argc; i++) { @@ -54,7 +55,7 @@ cmd_list_parse(int argc, char **argv, char **cause) cmd = cmd_parse(new_argc, new_argv, cause); if (cmd == NULL) goto bad; - TAILQ_INSERT_TAIL(cmdlist, cmd, qentry); + TAILQ_INSERT_TAIL(&cmdlist->list, cmd, qentry); lastsplit = i + 1; } @@ -63,7 +64,7 @@ cmd_list_parse(int argc, char **argv, char **cause) cmd = cmd_parse(argc - lastsplit, argv + lastsplit, cause); if (cmd == NULL) goto bad; - TAILQ_INSERT_TAIL(cmdlist, cmd, qentry); + TAILQ_INSERT_TAIL(&cmdlist->list, cmd, qentry); } return (cmdlist); @@ -80,7 +81,7 @@ cmd_list_exec(struct cmd_list *cmdlist, struct cmd_ctx *ctx) int n, retval; retval = 0; - TAILQ_FOREACH(cmd, cmdlist, qentry) { + TAILQ_FOREACH(cmd, &cmdlist->list, qentry) { if ((n = cmd_exec(cmd, ctx)) == -1) return (-1); @@ -114,9 +115,12 @@ cmd_list_free(struct cmd_list *cmdlist) { struct cmd *cmd; - while (!TAILQ_EMPTY(cmdlist)) { - cmd = TAILQ_FIRST(cmdlist); - TAILQ_REMOVE(cmdlist, cmd, qentry); + if (--cmdlist->references != 0) + return; + + while (!TAILQ_EMPTY(&cmdlist->list)) { + cmd = TAILQ_FIRST(&cmdlist->list); + TAILQ_REMOVE(&cmdlist->list, cmd, qentry); cmd_free(cmd); } xfree(cmdlist); @@ -129,7 +133,7 @@ cmd_list_print(struct cmd_list *cmdlist, char *buf, size_t len) size_t off; off = 0; - TAILQ_FOREACH(cmd, cmdlist, qentry) { + TAILQ_FOREACH(cmd, &cmdlist->list, qentry) { if (off >= len) break; off += cmd_print(cmd, buf + off, len - off); diff --git a/usr.bin/tmux/key-bindings.c b/usr.bin/tmux/key-bindings.c index b683d025a5e..e345b6ab5c5 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.22 2010/06/21 21:44:09 nicm Exp $ */ +/* $OpenBSD: key-bindings.c,v 1.23 2010/06/26 18:20:53 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net> @@ -178,14 +178,15 @@ key_bindings_init(void) for (i = 0; i < nitems(table); i++) { cmdlist = xmalloc(sizeof *cmdlist); - TAILQ_INIT(cmdlist); + TAILQ_INIT(&cmdlist->list); + cmdlist->references = 1; cmd = xmalloc(sizeof *cmd); cmd->entry = table[i].entry; cmd->data = NULL; if (cmd->entry->init != NULL) cmd->entry->init(cmd, table[i].key); - TAILQ_INSERT_HEAD(cmdlist, cmd, qentry); + TAILQ_INSERT_HEAD(&cmdlist->list, cmd, qentry); key_bindings_add( table[i].key | KEYC_PREFIX, table[i].can_repeat, cmdlist); @@ -259,7 +260,7 @@ key_bindings_dispatch(struct key_binding *bd, struct client *c) ctx.cmdclient = NULL; readonly = 1; - TAILQ_FOREACH(cmd, bd->cmdlist, qentry) { + TAILQ_FOREACH(cmd, &bd->cmdlist->list, qentry) { if (!(cmd->entry->flags & CMD_READONLY)) readonly = 0; } diff --git a/usr.bin/tmux/tmux.c b/usr.bin/tmux/tmux.c index d53f819b2c2..56fc91743db 100644 --- a/usr.bin/tmux/tmux.c +++ b/usr.bin/tmux/tmux.c @@ -1,4 +1,4 @@ -/* $OpenBSD: tmux.c,v 1.79 2010/05/14 19:03:09 nicm Exp $ */ +/* $OpenBSD: tmux.c,v 1.80 2010/06/26 18:20:53 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net> @@ -517,7 +517,7 @@ main(int argc, char **argv) exit(1); } cmdflags &= ~CMD_STARTSERVER; - TAILQ_FOREACH(cmd, cmdlist, qentry) { + TAILQ_FOREACH(cmd, &cmdlist->list, qentry) { if (cmd->entry->flags & CMD_STARTSERVER) cmdflags |= CMD_STARTSERVER; if (cmd->entry->flags & CMD_SENDENVIRON) diff --git a/usr.bin/tmux/tmux.h b/usr.bin/tmux/tmux.h index 7ddb6e0c9d0..d10baf752f9 100644 --- a/usr.bin/tmux/tmux.h +++ b/usr.bin/tmux/tmux.h @@ -1,4 +1,4 @@ -/* $OpenBSD: tmux.h,v 1.228 2010/06/21 21:44:09 nicm Exp $ */ +/* $OpenBSD: tmux.h,v 1.229 2010/06/26 18:20:53 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net> @@ -1161,7 +1161,10 @@ struct cmd { TAILQ_ENTRY(cmd) qentry; }; -TAILQ_HEAD(cmd_list, cmd); +struct cmd_list { + int references; + TAILQ_HEAD(, cmd) list; +}; struct cmd_entry { const char *name; |