Provide an accessor for the running queue item and use it to not let

hooks recurse.
This commit is contained in:
nicm
2020-04-14 06:00:52 +00:00
parent fc83517913
commit 63ec791854
3 changed files with 33 additions and 13 deletions

View File

@ -64,7 +64,7 @@ struct cmdq_item {
TAILQ_ENTRY(cmdq_item) entry;
};
TAILQ_HEAD(cmdq_list, cmdq_item);
TAILQ_HEAD(cmdq_item_list, cmdq_item);
/*
* Command queue state. This is the context for commands on the command queue.
@ -83,6 +83,12 @@ struct cmdq_state {
struct cmd_find_state current;
};
/* Command queue. */
struct cmdq_list {
struct cmdq_item *item;
struct cmdq_item_list list;
};
/* Get command queue name. */
static const char *
cmdq_name(struct client *c)
@ -119,7 +125,7 @@ cmdq_new(void)
struct cmdq_list *queue;
queue = xcalloc (1, sizeof *queue);
TAILQ_INIT (queue);
TAILQ_INIT (&queue->list);
return (queue);
}
@ -127,7 +133,7 @@ cmdq_new(void)
void
cmdq_free(struct cmdq_list *queue)
{
if (!TAILQ_EMPTY(queue))
if (!TAILQ_EMPTY(&queue->list))
fatalx("queue not empty");
free(queue);
}
@ -289,12 +295,12 @@ cmdq_append(struct client *c, struct cmdq_item *item)
item->client = c;
item->queue = queue;
TAILQ_INSERT_TAIL(queue, item, entry);
TAILQ_INSERT_TAIL(&queue->list, item, entry);
log_debug("%s %s: %s", __func__, cmdq_name(c), item->name);
item = next;
} while (item != NULL);
return (TAILQ_LAST(queue, cmdq_list));
return (TAILQ_LAST(&queue->list, cmdq_item_list));
}
/* Insert an item. */
@ -315,7 +321,7 @@ cmdq_insert_after(struct cmdq_item *after, struct cmdq_item *item)
item->client = c;
item->queue = queue;
TAILQ_INSERT_AFTER(queue, after, item, entry);
TAILQ_INSERT_AFTER(&queue->list, after, item, entry);
log_debug("%s %s: %s after %s", __func__, cmdq_name(c),
item->name, after->name);
@ -399,7 +405,7 @@ cmdq_remove(struct cmdq_item *item)
cmd_list_free(item->cmdlist);
cmdq_free_state(item->state);
TAILQ_REMOVE(item->queue, item, entry);
TAILQ_REMOVE(&item->queue->list, item, entry);
free(item->name);
free(item);
@ -621,18 +627,18 @@ cmdq_next(struct client *c)
u_int items = 0;
static u_int number;
if (TAILQ_EMPTY(queue)) {
if (TAILQ_EMPTY(&queue->list)) {
log_debug("%s %s: empty", __func__, name);
return (0);
}
if (TAILQ_FIRST(queue)->flags & CMDQ_WAITING) {
if (TAILQ_FIRST(&queue->list)->flags & CMDQ_WAITING) {
log_debug("%s %s: waiting", __func__, name);
return (0);
}
log_debug("%s %s: enter", __func__, name);
for (;;) {
item = TAILQ_FIRST(queue);
item = queue->item = TAILQ_FIRST(&queue->list);
if (item == NULL)
break;
log_debug("%s %s: %s (%d), flags %x", __func__, name,
@ -682,6 +688,7 @@ cmdq_next(struct client *c)
}
cmdq_remove(item);
}
queue->item = NULL;
log_debug("%s %s: exit (empty)", __func__, name);
return (items);
@ -691,6 +698,15 @@ waiting:
return (items);
}
/* Get running item if any. */
struct cmdq_item *
cmdq_running(struct client *c)
{
struct cmdq_list *queue = cmdq_get(c);
return (queue->item);
}
/* Print a guard line. */
void
cmdq_guard(struct cmdq_item *item, const char *guard, int flags)