summaryrefslogtreecommitdiffstats
path: root/cmd-list.c
diff options
context:
space:
mode:
authorNicholas Marriott <nicm@openbsd.org>2010-06-26 18:20:53 +0000
committerNicholas Marriott <nicm@openbsd.org>2010-06-26 18:20:53 +0000
commit42e24139788d76f00896005df0f745d9e022709a (patch)
treeccc08279c4c2f4162b42b07112ae3fe340c81358 /cmd-list.c
parentef7293379f00b85cb96dd0dff128bb503e87612b (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 'cmd-list.c')
-rw-r--r--cmd-list.c20
1 files changed, 12 insertions, 8 deletions
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);