diff options
Diffstat (limited to 'cmd-queue.c')
-rw-r--r-- | cmd-queue.c | 428 |
1 files changed, 336 insertions, 92 deletions
diff --git a/cmd-queue.c b/cmd-queue.c index a9e1dd3a..59f86c64 100644 --- a/cmd-queue.c +++ b/cmd-queue.c @@ -25,8 +25,69 @@ #include "tmux.h" -/* Global command queue. */ -static struct cmdq_list global_queue = TAILQ_HEAD_INITIALIZER(global_queue); +/* Command queue flags. */ +#define CMDQ_FIRED 0x1 +#define CMDQ_WAITING 0x2 + +/* Command queue item type. */ +enum cmdq_type { + CMDQ_COMMAND, + CMDQ_CALLBACK, +}; + +/* Command queue item. */ +struct cmdq_item { + char *name; + struct cmdq_list *queue; + struct cmdq_item *next; + + struct client *client; + struct client *target_client; + + enum cmdq_type type; + u_int group; + + u_int number; + time_t time; + + int flags; + + struct cmdq_state *state; + struct cmd_find_state source; + struct cmd_find_state target; + + struct cmd_list *cmdlist; + struct cmd *cmd; + + cmdq_cb cb; + void *data; + + TAILQ_ENTRY(cmdq_item) entry; +}; +TAILQ_HEAD(cmdq_item_list, cmdq_item); + +/* + * Command queue state. This is the context for commands on the command queue. + * It holds information about how the commands were fired (the key and flags), + * any additional formats for the commands, and the current default target. + * Multiple commands can share the same state and a command may update the + * default target. + */ +struct cmdq_state { + int references; + int flags; + + struct format_tree *formats; + + struct key_event event; + 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 * @@ -47,9 +108,179 @@ cmdq_name(struct client *c) static struct cmdq_list * cmdq_get(struct client *c) { - if (c == NULL) - return (&global_queue); - return (&c->queue); + static struct cmdq_list *global_queue; + + if (c == NULL) { + if (global_queue == NULL) + global_queue = cmdq_new(); + return (global_queue); + } + return (c->queue); +} + +/* Create a queue. */ +struct cmdq_list * +cmdq_new(void) +{ + struct cmdq_list *queue; + + queue = xcalloc (1, sizeof *queue); + TAILQ_INIT (&queue->list); + return (queue); +} + +/* Free a queue. */ +void +cmdq_free(struct cmdq_list *queue) +{ + if (!TAILQ_EMPTY(&queue->list)) + fatalx("queue not empty"); + free(queue); +} + +/* Get item name. */ +const char * +cmdq_get_name(struct cmdq_item *item) +{ + return (item->name); +} + +/* Get item client. */ +struct client * +cmdq_get_client(struct cmdq_item *item) +{ + return (item->client); +} + +/* Get item target client. */ +struct client * +cmdq_get_target_client(struct cmdq_item *item) +{ + return (item->target_client); +} + +/* Get item state. */ +struct cmdq_state * +cmdq_get_state(struct cmdq_item *item) +{ + return (item->state); +} + +/* Get item target. */ +struct cmd_find_state * +cmdq_get_target(struct cmdq_item *item) +{ + return (&item->target); +} + +/* Get item source. */ +struct cmd_find_state * +cmdq_get_source(struct cmdq_item *item) +{ + return (&item->source); +} + +/* Get state event. */ +struct key_event * +cmdq_get_event(struct cmdq_item *item) +{ + return (&item->state->event); +} + +/* Get state current target. */ +struct cmd_find_state * +cmdq_get_current(struct cmdq_item *item) +{ + return (&item->state->current); +} + +/* Get state flags. */ +int +cmdq_get_flags(struct cmdq_item *item) +{ + return (item->state->flags); +} + +/* Create a new state. */ +struct cmdq_state * +cmdq_new_state(struct cmd_find_state *current, struct key_event *event, + int flags) +{ + struct cmdq_state *state; + + state = xcalloc(1, sizeof *state); + state->references = 1; + state->flags = flags; + + if (event != NULL) + memcpy(&state->event, event, sizeof state->event); + else + state->event.key = KEYC_NONE; + if (current != NULL && cmd_find_valid_state(current)) + cmd_find_copy_state(&state->current, current); + else + cmd_find_clear_state(&state->current, 0); + + return (state); +} + +/* Add a reference to a state. */ +struct cmdq_state * +cmdq_link_state(struct cmdq_state *state) +{ + state->references++; + return (state); +} + +/* Make a copy of a state. */ +struct cmdq_state * +cmdq_copy_state(struct cmdq_state *state) +{ + return (cmdq_new_state(&state->current, &state->event, state->flags)); +} + +/* Free a state. */ +void +cmdq_free_state(struct cmdq_state *state) +{ + if (--state->references != 0) + return; + + if (state->formats != NULL) + format_free(state->formats); + free(state); +} + +/* Add a format to command queue. */ +void +cmdq_add_format(struct cmdq_state *state, const char *key, const char *fmt, ...) +{ + va_list ap; + char *value; + + va_start(ap, fmt); + xvasprintf(&value, fmt, ap); + va_end(ap); + + if (state->formats == NULL) + state->formats = format_create(NULL, NULL, FORMAT_NONE, 0); + format_add(state->formats, key, "%s", value); + + free(value); +} + +/* Merge formats from item. */ +void +cmdq_merge_formats(struct cmdq_item *item, struct format_tree *ft) +{ + const struct cmd_entry *entry; + + if (item->cmd != NULL) { + entry = cmd_get_entry (item->cmd); + format_add(ft, "command", "%s", entry->name); + } + if (item->state->formats != NULL) + format_merge(ft, item->state->formats); } /* Append an item. */ @@ -68,12 +299,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. */ @@ -94,7 +325,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); @@ -107,17 +338,19 @@ cmdq_insert_after(struct cmdq_item *after, struct cmdq_item *item) /* Insert a hook. */ void cmdq_insert_hook(struct session *s, struct cmdq_item *item, - struct cmd_find_state *fs, const char *fmt, ...) + struct cmd_find_state *current, const char *fmt, ...) { + struct cmdq_state *state = item->state; struct options *oo; va_list ap; char *name; struct cmdq_item *new_item; + struct cmdq_state *new_state; struct options_entry *o; struct options_array_item *a; struct cmd_list *cmdlist; - if (item->flags & CMDQ_NOHOOKS) + if (item->state->flags & CMDQ_STATE_NOHOOKS) return; if (s == NULL) oo = global_s_options; @@ -135,24 +368,27 @@ cmdq_insert_hook(struct session *s, struct cmdq_item *item, } log_debug("running hook %s (parent %p)", name, item); + /* + * The hooks get a new state because they should not update the current + * target or formats for any subsequent commands. + */ + new_state = cmdq_new_state(current, &state->event, CMDQ_STATE_NOHOOKS); + cmdq_add_format(new_state, "hook", "%s", name); + a = options_array_first(o); while (a != NULL) { cmdlist = options_array_item_value(a)->cmdlist; - if (cmdlist == NULL) { - a = options_array_next(a); - continue; + if (cmdlist != NULL) { + new_item = cmdq_get_command(cmdlist, new_state); + if (item != NULL) + item = cmdq_insert_after(item, new_item); + else + item = cmdq_append(NULL, new_item); } - - new_item = cmdq_get_command(cmdlist, fs, NULL, CMDQ_NOHOOKS); - cmdq_format(new_item, "hook", "%s", name); - if (item != NULL) - item = cmdq_insert_after(item, new_item); - else - item = cmdq_append(NULL, new_item); - a = options_array_next(a); } + cmdq_free_state(new_state); free(name); } @@ -167,19 +403,13 @@ cmdq_continue(struct cmdq_item *item) static void cmdq_remove(struct cmdq_item *item) { - if (item->shared != NULL && --item->shared->references == 0) { - if (item->shared->formats != NULL) - format_free(item->shared->formats); - free(item->shared); - } - if (item->client != NULL) server_client_unref(item->client); - if (item->cmdlist != NULL) 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); @@ -204,48 +434,46 @@ cmdq_remove_group(struct cmdq_item *item) /* Get a command for the command queue. */ struct cmdq_item * -cmdq_get_command(struct cmd_list *cmdlist, struct cmd_find_state *current, - struct mouse_event *m, int flags) +cmdq_get_command(struct cmd_list *cmdlist, struct cmdq_state *state) { struct cmdq_item *item, *first = NULL, *last = NULL; struct cmd *cmd; - struct cmdq_shared *shared = NULL; - u_int group = 0; - - TAILQ_FOREACH(cmd, &cmdlist->list, qentry) { - if (cmd->group != group) { - shared = xcalloc(1, sizeof *shared); - if (current != NULL) - cmd_find_copy_state(&shared->current, current); - else - cmd_find_clear_state(&shared->current, 0); - if (m != NULL) - memcpy(&shared->mouse, m, sizeof shared->mouse); - group = cmd->group; - } + const struct cmd_entry *entry; + int created = 0; + + if (state == NULL) { + state = cmdq_new_state(NULL, NULL, 0); + created = 1; + } + + cmd = cmd_list_first(cmdlist); + while (cmd != NULL) { + entry = cmd_get_entry(cmd); item = xcalloc(1, sizeof *item); - xasprintf(&item->name, "[%s/%p]", cmd->entry->name, item); + xasprintf(&item->name, "[%s/%p]", entry->name, item); item->type = CMDQ_COMMAND; - item->group = cmd->group; - item->flags = flags; + item->group = cmd_get_group(cmd); + item->state = cmdq_link_state(state); - item->shared = shared; item->cmdlist = cmdlist; item->cmd = cmd; - log_debug("%s: %s group %u", __func__, item->name, item->group); - - shared->references++; cmdlist->references++; + log_debug("%s: %s group %u", __func__, item->name, item->group); if (first == NULL) first = item; if (last != NULL) last->next = item; last = item; + + cmd = cmd_list_next(cmd); } + + if (created) + cmdq_free_state(state); return (first); } @@ -261,7 +489,7 @@ cmdq_find_flag(struct cmdq_item *item, struct cmd_find_state *fs, return (CMD_RETURN_NORMAL); } - value = args_get(item->cmd->args, flag->flag); + value = args_get(cmd_get_args(item->cmd), flag->flag); if (cmd_find_target(fs, item, value, flag->type, flag->flags) != 0) { cmd_find_clear_state(fs, 0); return (CMD_RETURN_ERROR); @@ -273,14 +501,15 @@ cmdq_find_flag(struct cmdq_item *item, struct cmd_find_state *fs, static enum cmd_retval cmdq_fire_command(struct cmdq_item *item) { - struct client *c = item->client; - const char *name = cmdq_name(c); - struct cmdq_shared *shared = item->shared; + const char *name = cmdq_name(item->client); + struct cmdq_state *state = item->state; struct cmd *cmd = item->cmd; - const struct cmd_entry *entry = cmd->entry; + struct args *args = cmd_get_args(cmd); + const struct cmd_entry *entry = cmd_get_entry(cmd); + struct client *tc, *saved = item->client; enum cmd_retval retval; struct cmd_find_state *fsp, fs; - int flags; + int flags, quiet = 0; char *tmp; if (log_get_level() > 1) { @@ -289,11 +518,30 @@ cmdq_fire_command(struct cmdq_item *item) free(tmp); } - flags = !!(shared->flags & CMDQ_SHARED_CONTROL); + flags = !!(state->flags & CMDQ_STATE_CONTROL); cmdq_guard(item, "begin", flags); if (item->client == NULL) item->client = cmd_find_client(item, NULL, 1); + + if (entry->flags & CMD_CLIENT_CANFAIL) + quiet = 1; + if (entry->flags & CMD_CLIENT_CFLAG) { + tc = cmd_find_client(item, args_get(args, 'c'), quiet); + if (tc == NULL && !quiet) { + retval = CMD_RETURN_ERROR; + goto out; + } + } else if (entry->flags & CMD_CLIENT_TFLAG) { + tc = cmd_find_client(item, args_get(args, 't'), quiet); + if (tc == NULL && !quiet) { + retval = CMD_RETURN_ERROR; + goto out; + } + } else + tc = cmd_find_client(item, NULL, 1); + item->target_client = tc; + retval = cmdq_find_flag(item, &item->source, &entry->source); if (retval == CMD_RETURN_ERROR) goto out; @@ -301,6 +549,7 @@ cmdq_fire_command(struct cmdq_item *item) if (retval == CMD_RETURN_ERROR) goto out; + retval = entry->exec(cmd, item); if (retval == CMD_RETURN_ERROR) goto out; @@ -308,8 +557,8 @@ cmdq_fire_command(struct cmdq_item *item) if (entry->flags & CMD_AFTERHOOK) { if (cmd_find_valid_state(&item->target)) fsp = &item->target; - else if (cmd_find_valid_state(&item->shared->current)) - fsp = &item->shared->current; + else if (cmd_find_valid_state(&item->state->current)) + fsp = &item->state->current; else if (cmd_find_from_client(&fs, item->client, 0) == 0) fsp = &fs; else @@ -318,7 +567,7 @@ cmdq_fire_command(struct cmdq_item *item) } out: - item->client = c; + item->client = saved; if (retval == CMD_RETURN_ERROR) cmdq_guard(item, "error", flags); else @@ -337,7 +586,7 @@ cmdq_get_callback1(const char *name, cmdq_cb cb, void *data) item->type = CMDQ_CALLBACK; item->group = 0; - item->flags = 0; + item->state = cmdq_new_state(NULL, NULL, 0); item->cb = cb; item->data = data; @@ -371,25 +620,6 @@ cmdq_fire_callback(struct cmdq_item *item) return (item->cb(item, item->data)); } -/* Add a format to command queue. */ -void -cmdq_format(struct cmdq_item *item, const char *key, const char *fmt, ...) -{ - struct cmdq_shared *shared = item->shared; - va_list ap; - char *value; - - va_start(ap, fmt); - xvasprintf(&value, fmt, ap); - va_end(ap); - - if (shared->formats == NULL) - shared->formats = format_create(NULL, NULL, FORMAT_NONE, 0); - format_add(shared->formats, key, "%s", value); - - free(value); -} - /* Process next item on command queue. */ u_int cmdq_next(struct client *c) @@ -401,18 +631,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, @@ -462,6 +692,7 @@ cmdq_next(struct client *c) } cmdq_remove(item); } + queue->item = NULL; log_debug("%s %s: exit (empty)", __func__, name); return (items); @@ -471,6 +702,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) @@ -511,8 +751,10 @@ cmdq_print(struct cmdq_item *item, const char *fmt, ...) } else { wp = c->session->curw->window->active; wme = TAILQ_FIRST(&wp->modes); - if (wme == NULL || wme->mode != &window_view_mode) - window_pane_set_mode(wp, &window_view_mode, NULL, NULL); + if (wme == NULL || wme->mode != &window_view_mode) { + window_pane_set_mode(wp, NULL, &window_view_mode, NULL, + NULL); + } window_copy_add(wp, "%s", msg); } @@ -526,8 +768,9 @@ cmdq_error(struct cmdq_item *item, const char *fmt, ...) struct client *c = item->client; struct cmd *cmd = item->cmd; va_list ap; - char *msg; - char *tmp; + char *msg, *tmp; + const char *file; + u_int line; va_start(ap, fmt); xvasprintf(&msg, fmt, ap); @@ -535,9 +778,10 @@ cmdq_error(struct cmdq_item *item, const char *fmt, ...) log_debug("%s: %s", __func__, msg); - if (c == NULL) - cfg_add_cause("%s:%u: %s", cmd->file, cmd->line, msg); - else if (c->session == NULL || (c->flags & CLIENT_CONTROL)) { + if (c == NULL) { + cmd_get_source(cmd, &file, &line); + cfg_add_cause("%s:%u: %s", file, line, msg); + } else if (c->session == NULL || (c->flags & CLIENT_CONTROL)) { if (~c->flags & CLIENT_UTF8) { tmp = msg; msg = utf8_sanitize(tmp); |