From 42e24139788d76f00896005df0f745d9e022709a Mon Sep 17 00:00:00 2001 From: Nicholas Marriott Date: Sat, 26 Jun 2010 18:20:53 +0000 Subject: 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@. --- cmd-list.c | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) (limited to 'cmd-list.c') diff --git a/cmd-list.c b/cmd-list.c index 55550c21..181127ae 100644 --- a/cmd-list.c +++ b/cmd-list.c @@ -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); -- cgit