Rewrite command queue handling. Each client still has a command queue,

but there is also now a global command queue. Instead of command queues
being dispatched on demand from wherever the command happens to be
added, they are now all dispatched from the top level server
loop. Command queues may now also include callbacks as well as commands,
and items may be inserted after the current command as well as at the end.

This all makes command queues significantly more predictable and easier
to use, and avoids the complex multiple nested command queues used by
source-file, if-shell and friends.

A mass rename of struct cmdq to a better name (cmdq_item probably) is
coming.
This commit is contained in:
nicm
2016-10-16 17:55:14 +00:00
parent bfe14b5312
commit ddc4512d2e
29 changed files with 817 additions and 702 deletions

68
hooks.c
View File

@ -33,7 +33,6 @@ RB_GENERATE_STATIC(hooks_tree, hook, entry, hooks_cmp);
static struct hook *hooks_find1(struct hooks *, const char *);
static void hooks_free1(struct hooks *, struct hook *);
static void hooks_emptyfn(struct cmd_q *);
static int
hooks_cmp(struct hook *hook1, struct hook *hook2)
@ -140,28 +139,14 @@ hooks_find(struct hooks *hooks, const char *name)
return (hook);
}
static void
hooks_emptyfn(struct cmd_q *hooks_cmdq)
{
struct cmd_q *cmdq = hooks_cmdq->data;
if (cmdq != NULL) {
if (hooks_cmdq->client_exit >= 0)
cmdq->client_exit = hooks_cmdq->client_exit;
if (!cmdq_free(cmdq))
cmdq_continue(cmdq);
}
cmdq_free(hooks_cmdq);
}
int
void
hooks_run(struct hooks *hooks, struct client *c, struct cmd_find_state *fs,
const char *fmt, ...)
{
struct hook *hook;
struct cmd_q *hooks_cmdq;
va_list ap;
char *name;
struct cmd_q *new_cmdq, *loop;
va_start(ap, fmt);
xvasprintf(&name, fmt, ap);
@ -170,34 +155,30 @@ hooks_run(struct hooks *hooks, struct client *c, struct cmd_find_state *fs,
hook = hooks_find(hooks, name);
if (hook == NULL) {
free(name);
return (-1);
return;
}
log_debug("running hook %s", name);
new_cmdq = cmdq_get_command(hook->cmdlist, fs, NULL, CMD_Q_NOHOOKS);
for (loop = new_cmdq; loop != NULL; loop = loop->next)
loop->hook = xstrdup(name);
free(name);
hooks_cmdq = cmdq_new(c);
hooks_cmdq->flags |= CMD_Q_NOHOOKS;
if (fs != NULL)
cmd_find_copy_state(&hooks_cmdq->current, fs);
hooks_cmdq->parent = NULL;
cmdq_run(hooks_cmdq, hook->cmdlist, NULL);
cmdq_free(hooks_cmdq);
return (0);
cmdq_append(c, new_cmdq);
}
int
hooks_wait(struct hooks *hooks, struct cmd_q *cmdq, struct cmd_find_state *fs,
void
hooks_insert(struct hooks *hooks, struct cmd_q *cmdq, struct cmd_find_state *fs,
const char *fmt, ...)
{
struct hook *hook;
struct cmd_q *hooks_cmdq;
va_list ap;
char *name;
struct cmd_q *new_cmdq, *loop;
if (cmdq->flags & CMD_Q_NOHOOKS)
return (-1);
return;
va_start(ap, fmt);
xvasprintf(&name, fmt, ap);
@ -206,23 +187,18 @@ hooks_wait(struct hooks *hooks, struct cmd_q *cmdq, struct cmd_find_state *fs,
hook = hooks_find(hooks, name);
if (hook == NULL) {
free(name);
return (-1);
return;
}
log_debug("running hook %s (parent %p)", name, cmdq);
new_cmdq = cmdq_get_command(hook->cmdlist, fs, NULL, CMD_Q_NOHOOKS);
for (loop = new_cmdq; loop != NULL; loop = loop->next)
loop->hook = xstrdup(name);
free(name);
hooks_cmdq = cmdq_new(cmdq->client);
hooks_cmdq->flags |= CMD_Q_NOHOOKS;
if (fs != NULL)
cmd_find_copy_state(&hooks_cmdq->current, fs);
hooks_cmdq->parent = cmdq;
hooks_cmdq->emptyfn = hooks_emptyfn;
hooks_cmdq->data = cmdq;
if (cmdq != NULL)
cmdq->references++;
cmdq_run(hooks_cmdq, hook->cmdlist, NULL);
return (0);
cmdq_insert_after(cmdq, new_cmdq);
else
cmdq_append(NULL, new_cmdq);
}