2013-03-24 09:54:10 +00:00
|
|
|
/* $OpenBSD$ */
|
|
|
|
|
|
|
|
/*
|
2016-01-19 15:59:12 +00:00
|
|
|
* Copyright (c) 2013 Nicholas Marriott <nicholas.marriott@gmail.com>
|
2013-03-24 09:54:10 +00:00
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
|
|
|
|
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <stdlib.h>
|
2015-04-19 21:34:21 +00:00
|
|
|
#include <string.h>
|
2013-03-26 10:54:48 +00:00
|
|
|
#include <time.h>
|
2013-03-24 09:54:10 +00:00
|
|
|
|
|
|
|
#include "tmux.h"
|
|
|
|
|
2016-10-16 17:55:14 +00:00
|
|
|
/* Global command queue. */
|
2016-10-16 19:04:05 +00:00
|
|
|
static struct cmdq_list global_queue = TAILQ_HEAD_INITIALIZER(global_queue);
|
2015-06-17 17:02:15 +00:00
|
|
|
|
2016-10-16 17:55:14 +00:00
|
|
|
/* Get command queue name. */
|
|
|
|
static const char *
|
|
|
|
cmdq_name(struct client *c)
|
|
|
|
{
|
2019-05-25 10:44:09 +00:00
|
|
|
static char s[256];
|
2016-10-16 17:55:14 +00:00
|
|
|
|
|
|
|
if (c == NULL)
|
|
|
|
return ("<global>");
|
2019-05-25 10:46:55 +00:00
|
|
|
if (c->name != NULL)
|
|
|
|
xsnprintf(s, sizeof s, "<%s>", c->name);
|
|
|
|
else
|
|
|
|
xsnprintf(s, sizeof s, "<%p>", c);
|
2016-10-16 17:55:14 +00:00
|
|
|
return (s);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get command queue from client. */
|
2016-10-16 19:04:05 +00:00
|
|
|
static struct cmdq_list *
|
2016-10-16 17:55:14 +00:00
|
|
|
cmdq_get(struct client *c)
|
|
|
|
{
|
|
|
|
if (c == NULL)
|
|
|
|
return (&global_queue);
|
|
|
|
return (&c->queue);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Append an item. */
|
|
|
|
void
|
2016-10-16 19:04:05 +00:00
|
|
|
cmdq_append(struct client *c, struct cmdq_item *item)
|
2016-10-16 17:55:14 +00:00
|
|
|
{
|
2016-10-16 19:04:05 +00:00
|
|
|
struct cmdq_list *queue = cmdq_get(c);
|
|
|
|
struct cmdq_item *next;
|
2016-10-16 17:55:14 +00:00
|
|
|
|
|
|
|
do {
|
2016-10-16 19:04:05 +00:00
|
|
|
next = item->next;
|
|
|
|
item->next = NULL;
|
2016-10-16 17:55:14 +00:00
|
|
|
|
|
|
|
if (c != NULL)
|
|
|
|
c->references++;
|
2016-10-16 19:04:05 +00:00
|
|
|
item->client = c;
|
2016-10-16 17:55:14 +00:00
|
|
|
|
2016-10-16 19:04:05 +00:00
|
|
|
item->queue = queue;
|
|
|
|
TAILQ_INSERT_TAIL(queue, item, entry);
|
2019-05-03 15:43:01 +00:00
|
|
|
log_debug("%s %s: %s", __func__, cmdq_name(c), item->name);
|
2016-10-16 17:55:14 +00:00
|
|
|
|
2016-10-16 19:04:05 +00:00
|
|
|
item = next;
|
|
|
|
} while (item != NULL);
|
2016-10-16 17:55:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Insert an item. */
|
|
|
|
void
|
2016-10-16 19:04:05 +00:00
|
|
|
cmdq_insert_after(struct cmdq_item *after, struct cmdq_item *item)
|
2016-10-16 17:55:14 +00:00
|
|
|
{
|
|
|
|
struct client *c = after->client;
|
2016-10-16 19:04:05 +00:00
|
|
|
struct cmdq_list *queue = after->queue;
|
|
|
|
struct cmdq_item *next;
|
2016-10-16 17:55:14 +00:00
|
|
|
|
|
|
|
do {
|
2016-10-16 19:04:05 +00:00
|
|
|
next = item->next;
|
2019-05-03 15:43:01 +00:00
|
|
|
item->next = after->next;
|
|
|
|
after->next = item;
|
2016-10-16 17:55:14 +00:00
|
|
|
|
|
|
|
if (c != NULL)
|
|
|
|
c->references++;
|
2016-10-16 19:04:05 +00:00
|
|
|
item->client = c;
|
2016-10-16 17:55:14 +00:00
|
|
|
|
2016-10-16 19:04:05 +00:00
|
|
|
item->queue = queue;
|
2019-05-03 15:43:01 +00:00
|
|
|
TAILQ_INSERT_AFTER(queue, after, item, entry);
|
|
|
|
log_debug("%s %s: %s after %s", __func__, cmdq_name(c),
|
|
|
|
item->name, after->name);
|
2016-10-16 17:55:14 +00:00
|
|
|
|
2019-05-03 18:59:58 +00:00
|
|
|
after = item;
|
2016-10-16 19:04:05 +00:00
|
|
|
item = next;
|
|
|
|
} while (item != NULL);
|
2016-10-16 17:55:14 +00:00
|
|
|
}
|
|
|
|
|
2019-04-26 11:38:51 +00:00
|
|
|
|
|
|
|
/* Insert a hook. */
|
|
|
|
void
|
|
|
|
cmdq_insert_hook(struct session *s, struct cmdq_item *item,
|
|
|
|
struct cmd_find_state *fs, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
struct options *oo;
|
|
|
|
va_list ap;
|
|
|
|
char *name;
|
|
|
|
struct cmdq_item *new_item;
|
|
|
|
struct options_entry *o;
|
|
|
|
struct options_array_item *a;
|
|
|
|
struct cmd_list *cmdlist;
|
|
|
|
|
|
|
|
if (item->flags & CMDQ_NOHOOKS)
|
|
|
|
return;
|
|
|
|
if (s == NULL)
|
|
|
|
oo = global_s_options;
|
|
|
|
else
|
|
|
|
oo = s->options;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
xvasprintf(&name, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
o = options_get(oo, name);
|
|
|
|
if (o == NULL) {
|
|
|
|
free(name);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
log_debug("running hook %s (parent %p)", name, item);
|
|
|
|
|
|
|
|
a = options_array_first(o);
|
|
|
|
while (a != NULL) {
|
|
|
|
cmdlist = options_array_item_value(a)->cmdlist;
|
|
|
|
if (cmdlist == NULL) {
|
|
|
|
a = options_array_next(a);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
new_item = cmdq_get_command(cmdlist, fs, NULL, CMDQ_NOHOOKS);
|
|
|
|
cmdq_format(new_item, "hook", "%s", name);
|
|
|
|
if (item != NULL) {
|
|
|
|
cmdq_insert_after(item, new_item);
|
|
|
|
item = new_item;
|
|
|
|
} else
|
|
|
|
cmdq_append(NULL, new_item);
|
|
|
|
|
|
|
|
a = options_array_next(a);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(name);
|
|
|
|
}
|
|
|
|
|
2016-10-16 17:55:14 +00:00
|
|
|
/* Remove an item. */
|
|
|
|
static void
|
2016-10-16 19:04:05 +00:00
|
|
|
cmdq_remove(struct cmdq_item *item)
|
2016-10-16 17:55:14 +00:00
|
|
|
{
|
2017-04-21 14:01:19 +00:00
|
|
|
if (item->shared != NULL && --item->shared->references == 0) {
|
|
|
|
if (item->shared->formats != NULL)
|
|
|
|
format_free(item->shared->formats);
|
|
|
|
free(item->shared);
|
|
|
|
}
|
2016-10-16 17:55:14 +00:00
|
|
|
|
2016-10-16 19:04:05 +00:00
|
|
|
if (item->client != NULL)
|
|
|
|
server_client_unref(item->client);
|
2016-10-16 17:55:14 +00:00
|
|
|
|
2019-04-17 14:37:48 +00:00
|
|
|
if (item->cmdlist != NULL)
|
2016-10-16 19:04:05 +00:00
|
|
|
cmd_list_free(item->cmdlist);
|
2016-10-16 17:55:14 +00:00
|
|
|
|
2016-10-16 19:04:05 +00:00
|
|
|
TAILQ_REMOVE(item->queue, item, entry);
|
2016-10-18 08:46:43 +00:00
|
|
|
|
2019-05-03 15:43:01 +00:00
|
|
|
free(item->name);
|
2016-10-16 19:04:05 +00:00
|
|
|
free(item);
|
2016-10-16 17:55:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Remove all subsequent items that match this item's group. */
|
|
|
|
static void
|
2016-10-16 19:04:05 +00:00
|
|
|
cmdq_remove_group(struct cmdq_item *item)
|
2016-10-16 17:55:14 +00:00
|
|
|
{
|
2016-10-16 19:04:05 +00:00
|
|
|
struct cmdq_item *this, *next;
|
2016-10-16 17:55:14 +00:00
|
|
|
|
2019-05-23 13:08:43 +00:00
|
|
|
if (item->group == 0)
|
|
|
|
return;
|
2016-10-16 19:04:05 +00:00
|
|
|
this = TAILQ_NEXT(item, entry);
|
2016-10-16 17:55:14 +00:00
|
|
|
while (this != NULL) {
|
|
|
|
next = TAILQ_NEXT(this, entry);
|
2016-10-16 19:04:05 +00:00
|
|
|
if (this->group == item->group)
|
2016-10-16 17:55:14 +00:00
|
|
|
cmdq_remove(this);
|
|
|
|
this = next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get a command for the command queue. */
|
2016-10-16 19:04:05 +00:00
|
|
|
struct cmdq_item *
|
2016-10-16 17:55:14 +00:00
|
|
|
cmdq_get_command(struct cmd_list *cmdlist, struct cmd_find_state *current,
|
|
|
|
struct mouse_event *m, int flags)
|
2013-03-24 09:54:10 +00:00
|
|
|
{
|
2016-10-16 19:04:05 +00:00
|
|
|
struct cmdq_item *item, *first = NULL, *last = NULL;
|
|
|
|
struct cmd *cmd;
|
2017-04-21 14:01:19 +00:00
|
|
|
struct cmdq_shared *shared;
|
2019-05-29 19:34:42 +00:00
|
|
|
u_int group = 0;
|
2016-10-16 17:55:14 +00:00
|
|
|
|
|
|
|
TAILQ_FOREACH(cmd, &cmdlist->list, qentry) {
|
2019-05-29 19:34:42 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-10-16 19:04:05 +00:00
|
|
|
item = xcalloc(1, sizeof *item);
|
2019-05-03 15:43:01 +00:00
|
|
|
xasprintf(&item->name, "[%s/%p]", cmd->entry->name, item);
|
2016-10-16 19:04:05 +00:00
|
|
|
item->type = CMDQ_COMMAND;
|
2016-10-18 08:46:43 +00:00
|
|
|
|
2019-05-23 11:13:30 +00:00
|
|
|
item->group = cmd->group;
|
2016-10-16 19:04:05 +00:00
|
|
|
item->flags = flags;
|
2016-10-16 17:55:14 +00:00
|
|
|
|
2017-04-21 14:01:19 +00:00
|
|
|
item->shared = shared;
|
2016-10-16 19:04:05 +00:00
|
|
|
item->cmdlist = cmdlist;
|
|
|
|
item->cmd = cmd;
|
2016-10-16 17:55:14 +00:00
|
|
|
|
2019-05-23 11:13:30 +00:00
|
|
|
log_debug("%s: %s group %u", __func__, item->name, item->group);
|
|
|
|
|
2017-04-21 14:01:19 +00:00
|
|
|
shared->references++;
|
2016-10-16 17:55:14 +00:00
|
|
|
cmdlist->references++;
|
|
|
|
|
|
|
|
if (first == NULL)
|
2016-10-16 19:04:05 +00:00
|
|
|
first = item;
|
2016-10-16 17:55:14 +00:00
|
|
|
if (last != NULL)
|
2016-10-16 19:04:05 +00:00
|
|
|
last->next = item;
|
|
|
|
last = item;
|
2016-10-16 17:55:14 +00:00
|
|
|
}
|
|
|
|
return (first);
|
|
|
|
}
|
2013-03-24 09:54:10 +00:00
|
|
|
|
2017-04-22 10:22:39 +00:00
|
|
|
/* Fill in flag for a command. */
|
|
|
|
static enum cmd_retval
|
|
|
|
cmdq_find_flag(struct cmdq_item *item, struct cmd_find_state *fs,
|
|
|
|
const struct cmd_entry_flag *flag)
|
|
|
|
{
|
|
|
|
const char *value;
|
|
|
|
|
|
|
|
if (flag->flag == 0) {
|
|
|
|
cmd_find_clear_state(fs, 0);
|
|
|
|
return (CMD_RETURN_NORMAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
value = args_get(item->cmd->args, flag->flag);
|
|
|
|
if (cmd_find_target(fs, item, value, flag->type, flag->flags) != 0) {
|
|
|
|
cmd_find_clear_state(fs, 0);
|
|
|
|
return (CMD_RETURN_ERROR);
|
|
|
|
}
|
|
|
|
return (CMD_RETURN_NORMAL);
|
|
|
|
}
|
|
|
|
|
2016-10-16 17:55:14 +00:00
|
|
|
/* Fire command on command queue. */
|
|
|
|
static enum cmd_retval
|
2016-10-16 19:04:05 +00:00
|
|
|
cmdq_fire_command(struct cmdq_item *item)
|
2016-10-16 17:55:14 +00:00
|
|
|
{
|
2016-10-16 19:04:05 +00:00
|
|
|
struct client *c = item->client;
|
2019-05-29 19:34:42 +00:00
|
|
|
const char *name = cmdq_name(c);
|
2019-05-18 21:14:10 +00:00
|
|
|
struct cmdq_shared *shared = item->shared;
|
2016-10-16 19:04:05 +00:00
|
|
|
struct cmd *cmd = item->cmd;
|
2017-04-22 10:22:39 +00:00
|
|
|
const struct cmd_entry *entry = cmd->entry;
|
2016-10-16 17:55:14 +00:00
|
|
|
enum cmd_retval retval;
|
|
|
|
struct cmd_find_state *fsp, fs;
|
|
|
|
int flags;
|
2019-05-29 19:34:42 +00:00
|
|
|
char *tmp;
|
|
|
|
|
|
|
|
if (log_get_level() > 1) {
|
|
|
|
tmp = cmd_print(cmd);
|
|
|
|
log_debug("%s %s: (%u) %s", __func__, name, item->group, tmp);
|
|
|
|
free(tmp);
|
|
|
|
}
|
2016-10-16 17:55:14 +00:00
|
|
|
|
2019-05-18 21:14:10 +00:00
|
|
|
flags = !!(shared->flags & CMDQ_SHARED_CONTROL);
|
2016-10-16 19:04:05 +00:00
|
|
|
cmdq_guard(item, "begin", flags);
|
2016-10-16 17:55:14 +00:00
|
|
|
|
2017-04-21 22:23:24 +00:00
|
|
|
if (item->client == NULL)
|
|
|
|
item->client = cmd_find_client(item, NULL, 1);
|
2017-04-22 10:22:39 +00:00
|
|
|
retval = cmdq_find_flag(item, &item->source, &entry->source);
|
|
|
|
if (retval == CMD_RETURN_ERROR)
|
|
|
|
goto out;
|
|
|
|
retval = cmdq_find_flag(item, &item->target, &entry->target);
|
|
|
|
if (retval == CMD_RETURN_ERROR)
|
2016-10-16 17:55:14 +00:00
|
|
|
goto out;
|
2017-04-21 20:34:05 +00:00
|
|
|
|
2017-04-22 10:22:39 +00:00
|
|
|
retval = entry->exec(cmd, item);
|
2016-10-16 17:55:14 +00:00
|
|
|
if (retval == CMD_RETURN_ERROR)
|
|
|
|
goto out;
|
|
|
|
|
2017-04-22 10:22:39 +00:00
|
|
|
if (entry->flags & CMD_AFTERHOOK) {
|
|
|
|
if (cmd_find_valid_state(&item->target))
|
|
|
|
fsp = &item->target;
|
2017-04-21 20:26:34 +00:00
|
|
|
else if (cmd_find_valid_state(&item->shared->current))
|
|
|
|
fsp = &item->shared->current;
|
2017-08-30 10:33:57 +00:00
|
|
|
else if (cmd_find_from_client(&fs, item->client, 0) == 0)
|
2016-10-16 17:55:14 +00:00
|
|
|
fsp = &fs;
|
2017-04-21 20:26:34 +00:00
|
|
|
else
|
|
|
|
goto out;
|
2019-04-26 11:38:51 +00:00
|
|
|
cmdq_insert_hook(fsp->s, item, fsp, "after-%s", entry->name);
|
2016-10-16 17:55:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
2016-10-16 19:04:05 +00:00
|
|
|
item->client = c;
|
2016-10-16 17:55:14 +00:00
|
|
|
if (retval == CMD_RETURN_ERROR)
|
2016-10-16 19:04:05 +00:00
|
|
|
cmdq_guard(item, "error", flags);
|
2016-10-16 17:55:14 +00:00
|
|
|
else
|
2016-10-16 19:04:05 +00:00
|
|
|
cmdq_guard(item, "end", flags);
|
2016-10-16 17:55:14 +00:00
|
|
|
return (retval);
|
|
|
|
}
|
2013-03-24 09:54:10 +00:00
|
|
|
|
2016-10-16 17:55:14 +00:00
|
|
|
/* Get a callback for the command queue. */
|
2016-10-16 19:04:05 +00:00
|
|
|
struct cmdq_item *
|
2016-10-18 08:46:43 +00:00
|
|
|
cmdq_get_callback1(const char *name, cmdq_cb cb, void *data)
|
2016-10-16 17:55:14 +00:00
|
|
|
{
|
2016-10-16 19:04:05 +00:00
|
|
|
struct cmdq_item *item;
|
2016-10-16 17:55:14 +00:00
|
|
|
|
2016-10-16 19:04:05 +00:00
|
|
|
item = xcalloc(1, sizeof *item);
|
2019-05-03 15:43:01 +00:00
|
|
|
xasprintf(&item->name, "[%s/%p]", name, item);
|
2016-10-16 19:04:05 +00:00
|
|
|
item->type = CMDQ_CALLBACK;
|
2016-10-18 08:46:43 +00:00
|
|
|
|
2016-10-16 19:04:05 +00:00
|
|
|
item->group = 0;
|
|
|
|
item->flags = 0;
|
2013-03-24 09:54:10 +00:00
|
|
|
|
2016-10-16 19:04:05 +00:00
|
|
|
item->cb = cb;
|
|
|
|
item->data = data;
|
2015-12-16 21:50:37 +00:00
|
|
|
|
2016-10-16 19:04:05 +00:00
|
|
|
return (item);
|
2013-03-24 09:54:10 +00:00
|
|
|
}
|
|
|
|
|
2019-05-20 11:46:06 +00:00
|
|
|
/* Generic error callback. */
|
|
|
|
static enum cmd_retval
|
|
|
|
cmdq_error_callback(struct cmdq_item *item, void *data)
|
|
|
|
{
|
|
|
|
char *error = data;
|
|
|
|
|
|
|
|
cmdq_error(item, "%s", error);
|
|
|
|
free(error);
|
|
|
|
|
|
|
|
return (CMD_RETURN_NORMAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get an error callback for the command queue. */
|
|
|
|
struct cmdq_item *
|
|
|
|
cmdq_get_error(const char *error)
|
|
|
|
{
|
|
|
|
return (cmdq_get_callback(cmdq_error_callback, xstrdup(error)));
|
|
|
|
}
|
|
|
|
|
2016-10-16 17:55:14 +00:00
|
|
|
/* Fire callback on callback queue. */
|
|
|
|
static enum cmd_retval
|
2016-10-16 19:04:05 +00:00
|
|
|
cmdq_fire_callback(struct cmdq_item *item)
|
2013-03-24 09:54:10 +00:00
|
|
|
{
|
2016-10-16 19:04:05 +00:00
|
|
|
return (item->cb(item, item->data));
|
2016-10-16 17:55:14 +00:00
|
|
|
}
|
|
|
|
|
2016-10-16 19:36:37 +00:00
|
|
|
/* Add a format to command queue. */
|
|
|
|
void
|
|
|
|
cmdq_format(struct cmdq_item *item, const char *key, const char *fmt, ...)
|
|
|
|
{
|
2017-04-21 14:01:19 +00:00
|
|
|
struct cmdq_shared *shared = item->shared;
|
2016-10-16 19:36:37 +00:00
|
|
|
va_list ap;
|
|
|
|
char *value;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
xvasprintf(&value, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
2017-04-21 14:01:19 +00:00
|
|
|
if (shared->formats == NULL)
|
2017-05-01 12:20:55 +00:00
|
|
|
shared->formats = format_create(NULL, NULL, FORMAT_NONE, 0);
|
2017-04-21 14:01:19 +00:00
|
|
|
format_add(shared->formats, key, "%s", value);
|
2016-10-16 19:36:37 +00:00
|
|
|
|
|
|
|
free(value);
|
|
|
|
}
|
|
|
|
|
2016-10-16 17:55:14 +00:00
|
|
|
/* Process next item on command queue. */
|
|
|
|
u_int
|
|
|
|
cmdq_next(struct client *c)
|
|
|
|
{
|
2016-10-16 19:04:05 +00:00
|
|
|
struct cmdq_list *queue = cmdq_get(c);
|
2016-10-16 17:55:14 +00:00
|
|
|
const char *name = cmdq_name(c);
|
2016-10-16 19:04:05 +00:00
|
|
|
struct cmdq_item *item;
|
2016-10-16 17:55:14 +00:00
|
|
|
enum cmd_retval retval;
|
|
|
|
u_int items = 0;
|
|
|
|
static u_int number;
|
2016-10-14 18:41:53 +00:00
|
|
|
|
2016-10-16 17:55:14 +00:00
|
|
|
if (TAILQ_EMPTY(queue)) {
|
|
|
|
log_debug("%s %s: empty", __func__, name);
|
|
|
|
return (0);
|
|
|
|
}
|
2016-10-16 19:04:05 +00:00
|
|
|
if (TAILQ_FIRST(queue)->flags & CMDQ_WAITING) {
|
2016-10-16 17:55:14 +00:00
|
|
|
log_debug("%s %s: waiting", __func__, name);
|
2015-09-16 22:24:54 +00:00
|
|
|
return (0);
|
|
|
|
}
|
2013-03-24 09:54:10 +00:00
|
|
|
|
2016-10-16 17:55:14 +00:00
|
|
|
log_debug("%s %s: enter", __func__, name);
|
|
|
|
for (;;) {
|
2016-10-16 19:04:05 +00:00
|
|
|
item = TAILQ_FIRST(queue);
|
|
|
|
if (item == NULL)
|
2016-10-16 17:55:14 +00:00
|
|
|
break;
|
2016-10-18 08:46:43 +00:00
|
|
|
log_debug("%s %s: %s (%d), flags %x", __func__, name,
|
|
|
|
item->name, item->type, item->flags);
|
2016-10-16 17:55:14 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Any item with the waiting flag set waits until an external
|
|
|
|
* event clears the flag (for example, a job - look at
|
|
|
|
* run-shell).
|
|
|
|
*/
|
2016-10-16 19:04:05 +00:00
|
|
|
if (item->flags & CMDQ_WAITING)
|
2016-10-16 17:55:14 +00:00
|
|
|
goto waiting;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Items are only fired once, once the fired flag is set, a
|
|
|
|
* waiting flag can only be cleared by an external event.
|
|
|
|
*/
|
2016-10-16 19:04:05 +00:00
|
|
|
if (~item->flags & CMDQ_FIRED) {
|
|
|
|
item->time = time(NULL);
|
|
|
|
item->number = ++number;
|
2016-10-16 17:55:14 +00:00
|
|
|
|
2017-04-21 18:18:17 +00:00
|
|
|
switch (item->type) {
|
2016-10-16 19:04:05 +00:00
|
|
|
case CMDQ_COMMAND:
|
|
|
|
retval = cmdq_fire_command(item);
|
2016-10-16 17:55:14 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If a command returns an error, remove any
|
|
|
|
* subsequent commands in the same group.
|
|
|
|
*/
|
|
|
|
if (retval == CMD_RETURN_ERROR)
|
2016-10-16 19:04:05 +00:00
|
|
|
cmdq_remove_group(item);
|
2016-10-16 17:55:14 +00:00
|
|
|
break;
|
2016-10-16 19:04:05 +00:00
|
|
|
case CMDQ_CALLBACK:
|
|
|
|
retval = cmdq_fire_callback(item);
|
2016-10-16 17:55:14 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
retval = CMD_RETURN_ERROR;
|
|
|
|
break;
|
|
|
|
}
|
2016-10-16 19:04:05 +00:00
|
|
|
item->flags |= CMDQ_FIRED;
|
2016-10-16 17:55:14 +00:00
|
|
|
|
|
|
|
if (retval == CMD_RETURN_WAIT) {
|
2016-10-16 19:04:05 +00:00
|
|
|
item->flags |= CMDQ_WAITING;
|
2016-10-16 17:55:14 +00:00
|
|
|
goto waiting;
|
|
|
|
}
|
|
|
|
items++;
|
|
|
|
}
|
2016-10-16 19:04:05 +00:00
|
|
|
cmdq_remove(item);
|
2016-10-16 17:55:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
log_debug("%s %s: exit (empty)", __func__, name);
|
|
|
|
return (items);
|
|
|
|
|
|
|
|
waiting:
|
|
|
|
log_debug("%s %s: exit (wait)", __func__, name);
|
|
|
|
return (items);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Print a guard line. */
|
|
|
|
void
|
2016-10-16 19:04:05 +00:00
|
|
|
cmdq_guard(struct cmdq_item *item, const char *guard, int flags)
|
2016-10-16 17:55:14 +00:00
|
|
|
{
|
2016-10-16 19:04:05 +00:00
|
|
|
struct client *c = item->client;
|
2016-10-16 17:55:14 +00:00
|
|
|
|
|
|
|
if (c == NULL || !(c->flags & CLIENT_CONTROL))
|
|
|
|
return;
|
|
|
|
|
|
|
|
evbuffer_add_printf(c->stdout_data, "%%%s %ld %u %d\n", guard,
|
2016-10-16 19:04:05 +00:00
|
|
|
(long)item->time, item->number, flags);
|
2016-10-16 17:55:14 +00:00
|
|
|
server_client_push_stdout(c);
|
2013-03-24 09:54:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Show message from command. */
|
2014-10-20 23:57:13 +00:00
|
|
|
void
|
2016-10-16 19:04:05 +00:00
|
|
|
cmdq_print(struct cmdq_item *item, const char *fmt, ...)
|
2013-03-24 09:54:10 +00:00
|
|
|
{
|
2019-03-12 11:16:49 +00:00
|
|
|
struct client *c = item->client;
|
|
|
|
struct window_pane *wp;
|
|
|
|
struct window_mode_entry *wme;
|
|
|
|
va_list ap;
|
|
|
|
char *tmp, *msg;
|
2013-03-24 09:54:10 +00:00
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
|
|
|
|
if (c == NULL)
|
|
|
|
/* nothing */;
|
|
|
|
else if (c->session == NULL || (c->flags & CLIENT_CONTROL)) {
|
2015-11-12 11:10:50 +00:00
|
|
|
if (~c->flags & CLIENT_UTF8) {
|
2016-09-28 14:40:07 +00:00
|
|
|
xvasprintf(&tmp, fmt, ap);
|
2015-11-12 11:10:50 +00:00
|
|
|
msg = utf8_sanitize(tmp);
|
|
|
|
free(tmp);
|
|
|
|
evbuffer_add(c->stdout_data, msg, strlen(msg));
|
|
|
|
free(msg);
|
|
|
|
} else
|
|
|
|
evbuffer_add_vprintf(c->stdout_data, fmt, ap);
|
2013-03-24 09:54:10 +00:00
|
|
|
evbuffer_add(c->stdout_data, "\n", 1);
|
2015-11-14 09:41:06 +00:00
|
|
|
server_client_push_stdout(c);
|
2013-03-24 09:54:10 +00:00
|
|
|
} else {
|
2019-03-08 10:34:20 +00:00
|
|
|
wp = c->session->curw->window->active;
|
2019-03-12 11:16:49 +00:00
|
|
|
wme = TAILQ_FIRST(&wp->modes);
|
|
|
|
if (wme == NULL || wme->mode != &window_view_mode)
|
2019-03-08 10:34:20 +00:00
|
|
|
window_pane_set_mode(wp, &window_view_mode, NULL, NULL);
|
|
|
|
window_copy_vadd(wp, fmt, ap);
|
2013-03-24 09:54:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Show error from command. */
|
2014-10-20 23:57:13 +00:00
|
|
|
void
|
2016-10-16 19:04:05 +00:00
|
|
|
cmdq_error(struct cmdq_item *item, const char *fmt, ...)
|
2013-03-24 09:54:10 +00:00
|
|
|
{
|
2016-10-16 19:04:05 +00:00
|
|
|
struct client *c = item->client;
|
|
|
|
struct cmd *cmd = item->cmd;
|
2013-03-24 09:54:10 +00:00
|
|
|
va_list ap;
|
2014-10-27 22:23:47 +00:00
|
|
|
char *msg;
|
2013-03-24 09:54:10 +00:00
|
|
|
size_t msglen;
|
2015-11-12 11:10:50 +00:00
|
|
|
char *tmp;
|
2013-03-24 09:54:10 +00:00
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
msglen = xvasprintf(&msg, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
2017-06-16 15:12:38 +00:00
|
|
|
log_debug("%s: %s", __func__, msg);
|
|
|
|
|
2014-10-27 22:23:47 +00:00
|
|
|
if (c == NULL)
|
|
|
|
cfg_add_cause("%s:%u: %s", cmd->file, cmd->line, msg);
|
|
|
|
else if (c->session == NULL || (c->flags & CLIENT_CONTROL)) {
|
2015-11-12 11:10:50 +00:00
|
|
|
if (~c->flags & CLIENT_UTF8) {
|
|
|
|
tmp = msg;
|
|
|
|
msg = utf8_sanitize(tmp);
|
|
|
|
free(tmp);
|
|
|
|
msglen = strlen(msg);
|
|
|
|
}
|
2013-03-24 09:54:10 +00:00
|
|
|
evbuffer_add(c->stderr_data, msg, msglen);
|
|
|
|
evbuffer_add(c->stderr_data, "\n", 1);
|
2015-11-14 09:41:06 +00:00
|
|
|
server_client_push_stderr(c);
|
2013-10-10 12:12:54 +00:00
|
|
|
c->retval = 1;
|
2013-03-24 09:54:10 +00:00
|
|
|
} else {
|
|
|
|
*msg = toupper((u_char) *msg);
|
|
|
|
status_message_set(c, "%s", msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(msg);
|
|
|
|
}
|