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)
|
|
|
|
{
|
|
|
|
static char s[32];
|
|
|
|
|
|
|
|
if (c == NULL)
|
|
|
|
return ("<global>");
|
|
|
|
xsnprintf(s, sizeof s, "<%p>", c);
|
|
|
|
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);
|
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;
|
|
|
|
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;
|
2016-10-16 17:55:14 +00:00
|
|
|
if (after->next != NULL)
|
2016-10-16 19:04:05 +00:00
|
|
|
TAILQ_INSERT_AFTER(queue, after->next, item, entry);
|
2016-10-16 17:55:14 +00:00
|
|
|
else
|
2016-10-16 19:04:05 +00:00
|
|
|
TAILQ_INSERT_AFTER(queue, after, item, entry);
|
|
|
|
after->next = item;
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/* 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
|
|
|
{
|
2016-10-16 19:36:37 +00:00
|
|
|
if (item->formats != NULL)
|
|
|
|
format_free(item->formats);
|
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
|
|
|
|
2016-10-16 19:04:05 +00:00
|
|
|
if (item->type == CMDQ_COMMAND)
|
|
|
|
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
|
|
|
|
|
|
|
free((void *)item->name);
|
2016-10-16 19:04:05 +00:00
|
|
|
free(item);
|
2016-10-16 17:55:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Set command group. */
|
|
|
|
static u_int
|
|
|
|
cmdq_next_group(void)
|
|
|
|
{
|
|
|
|
static u_int group;
|
|
|
|
|
|
|
|
return (++group);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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
|
|
|
|
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;
|
|
|
|
u_int group = cmdq_next_group();
|
2016-10-18 08:46:43 +00:00
|
|
|
char *tmp;
|
2016-10-16 17:55:14 +00:00
|
|
|
|
|
|
|
TAILQ_FOREACH(cmd, &cmdlist->list, qentry) {
|
2016-10-18 08:46:43 +00:00
|
|
|
xasprintf(&tmp, "command[%s]", cmd->entry->name);
|
|
|
|
|
2016-10-16 19:04:05 +00:00
|
|
|
item = xcalloc(1, sizeof *item);
|
2016-10-18 08:46:43 +00:00
|
|
|
item->name = tmp;
|
2016-10-16 19:04:05 +00:00
|
|
|
item->type = CMDQ_COMMAND;
|
2016-10-18 08:46:43 +00:00
|
|
|
|
2016-10-16 19:04:05 +00:00
|
|
|
item->group = group;
|
|
|
|
item->flags = flags;
|
2016-10-16 17:55:14 +00:00
|
|
|
|
2016-10-16 19:04:05 +00:00
|
|
|
item->cmdlist = cmdlist;
|
|
|
|
item->cmd = cmd;
|
2016-10-16 17:55:14 +00:00
|
|
|
|
|
|
|
if (current != NULL)
|
2016-10-16 19:04:05 +00:00
|
|
|
cmd_find_copy_state(&item->current, current);
|
2016-10-16 17:55:14 +00:00
|
|
|
if (m != NULL)
|
2016-10-16 19:04:05 +00:00
|
|
|
memcpy(&item->mouse, m, sizeof item->mouse);
|
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
|
|
|
|
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;
|
|
|
|
struct cmd *cmd = item->cmd;
|
2016-10-16 17:55:14 +00:00
|
|
|
enum cmd_retval retval;
|
|
|
|
const char *name;
|
|
|
|
struct cmd_find_state *fsp, fs;
|
|
|
|
int flags;
|
|
|
|
|
|
|
|
flags = !!(cmd->flags & CMD_CONTROL);
|
2016-10-16 19:04:05 +00:00
|
|
|
cmdq_guard(item, "begin", flags);
|
2016-10-16 17:55:14 +00:00
|
|
|
|
2016-10-16 19:04:05 +00:00
|
|
|
if (cmd_prepare_state(cmd, item) != 0) {
|
2016-10-16 17:55:14 +00:00
|
|
|
retval = CMD_RETURN_ERROR;
|
|
|
|
goto out;
|
|
|
|
}
|
2016-10-16 19:04:05 +00:00
|
|
|
if (item->client == NULL)
|
|
|
|
item->client = cmd_find_client(item, NULL, CMD_FIND_QUIET);
|
2013-03-24 09:54:10 +00:00
|
|
|
|
2016-10-16 19:04:05 +00:00
|
|
|
retval = cmd->entry->exec(cmd, item);
|
2016-10-16 17:55:14 +00:00
|
|
|
if (retval == CMD_RETURN_ERROR)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
if (cmd->entry->flags & CMD_AFTERHOOK) {
|
|
|
|
name = cmd->entry->name;
|
2016-10-16 19:04:05 +00:00
|
|
|
if (cmd_find_valid_state(&item->state.tflag))
|
|
|
|
fsp = &item->state.tflag;
|
2016-10-16 17:55:14 +00:00
|
|
|
else {
|
2016-10-16 19:04:05 +00:00
|
|
|
if (cmd_find_current(&fs, item, CMD_FIND_QUIET) != 0)
|
2016-10-16 17:55:14 +00:00
|
|
|
goto out;
|
|
|
|
fsp = &fs;
|
|
|
|
}
|
2016-10-16 19:04:05 +00:00
|
|
|
hooks_insert(fsp->s->hooks, item, fsp, "after-%s", 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-18 08:46:43 +00:00
|
|
|
char *tmp;
|
|
|
|
|
|
|
|
xasprintf(&tmp, "callback[%s]", name);
|
2016-10-16 17:55:14 +00:00
|
|
|
|
2016-10-16 19:04:05 +00:00
|
|
|
item = xcalloc(1, sizeof *item);
|
2016-10-18 08:46:43 +00:00
|
|
|
item->name = tmp;
|
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
|
|
|
}
|
|
|
|
|
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, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
struct cmdq_item *loop;
|
|
|
|
char *value;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
xvasprintf(&value, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
for (loop = item; loop != NULL; loop = item->next) {
|
|
|
|
if (loop->formats == NULL)
|
2017-02-03 11:57:27 +00:00
|
|
|
loop->formats = format_create(NULL, FORMAT_NONE, 0);
|
2016-10-16 19:36:37 +00:00
|
|
|
format_add(loop->formats, key, "%s", value);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2016-10-16 19:04:05 +00:00
|
|
|
switch (item->type)
|
2016-10-16 17:55:14 +00:00
|
|
|
{
|
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
|
|
|
{
|
2016-10-16 19:04:05 +00:00
|
|
|
struct client *c = item->client;
|
2013-03-24 09:54:10 +00:00
|
|
|
struct window *w;
|
|
|
|
va_list ap;
|
2015-11-12 11:10:50 +00:00
|
|
|
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 {
|
|
|
|
w = c->session->curw->window;
|
|
|
|
if (w->active->mode != &window_copy_mode) {
|
|
|
|
window_pane_reset_mode(w->active);
|
|
|
|
window_pane_set_mode(w->active, &window_copy_mode);
|
|
|
|
window_copy_init_for_output(w->active);
|
|
|
|
}
|
|
|
|
window_copy_vadd(w->active, fmt, ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
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);
|
|
|
|
}
|