2013-03-24 09:54:10 +00:00
|
|
|
/* $OpenBSD$ */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2013 Nicholas Marriott <nicm@users.sourceforge.net>
|
|
|
|
*
|
|
|
|
* 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"
|
|
|
|
|
2015-06-17 17:02:15 +00:00
|
|
|
enum cmd_retval cmdq_continue_one(struct cmd_q *);
|
|
|
|
|
2013-03-24 09:54:10 +00:00
|
|
|
/* Create new command queue. */
|
|
|
|
struct cmd_q *
|
|
|
|
cmdq_new(struct client *c)
|
|
|
|
{
|
|
|
|
struct cmd_q *cmdq;
|
|
|
|
|
|
|
|
cmdq = xcalloc(1, sizeof *cmdq);
|
|
|
|
cmdq->references = 1;
|
2015-09-16 22:24:54 +00:00
|
|
|
cmdq->flags = 0;
|
2013-03-24 09:54:10 +00:00
|
|
|
|
|
|
|
cmdq->client = c;
|
2013-10-10 12:07:36 +00:00
|
|
|
cmdq->client_exit = -1;
|
2013-03-24 09:54:10 +00:00
|
|
|
|
|
|
|
TAILQ_INIT(&cmdq->queue);
|
|
|
|
cmdq->item = NULL;
|
|
|
|
cmdq->cmd = NULL;
|
|
|
|
|
|
|
|
return (cmdq);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Free command queue */
|
|
|
|
int
|
|
|
|
cmdq_free(struct cmd_q *cmdq)
|
|
|
|
{
|
2015-09-16 22:24:54 +00:00
|
|
|
if (--cmdq->references != 0) {
|
|
|
|
if (cmdq->flags & CMD_Q_DEAD)
|
|
|
|
return (1);
|
|
|
|
return (0);
|
|
|
|
}
|
2013-03-24 09:54:10 +00:00
|
|
|
|
|
|
|
cmdq_flush(cmdq);
|
|
|
|
free(cmdq);
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Show message from command. */
|
2014-10-20 23:57:13 +00:00
|
|
|
void
|
2013-03-24 09:54:10 +00:00
|
|
|
cmdq_print(struct cmd_q *cmdq, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
struct client *c = cmdq->client;
|
|
|
|
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) {
|
|
|
|
vasprintf(&tmp, fmt, ap);
|
|
|
|
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);
|
|
|
|
server_push_stdout(c);
|
|
|
|
} 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
|
2013-03-24 09:54:10 +00:00
|
|
|
cmdq_error(struct cmd_q *cmdq, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
struct client *c = cmdq->client;
|
|
|
|
struct cmd *cmd = cmdq->cmd;
|
|
|
|
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);
|
|
|
|
server_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);
|
|
|
|
}
|
|
|
|
|
2013-03-25 11:41:16 +00:00
|
|
|
/* Print a guard line. */
|
2015-02-05 10:26:29 +00:00
|
|
|
void
|
2013-10-10 11:45:28 +00:00
|
|
|
cmdq_guard(struct cmd_q *cmdq, const char *guard, int flags)
|
2013-03-25 11:41:16 +00:00
|
|
|
{
|
|
|
|
struct client *c = cmdq->client;
|
|
|
|
|
2015-02-05 10:26:29 +00:00
|
|
|
if (c == NULL || !(c->flags & CLIENT_CONTROL))
|
|
|
|
return;
|
2013-03-25 11:41:16 +00:00
|
|
|
|
2013-06-23 12:41:54 +00:00
|
|
|
evbuffer_add_printf(c->stdout_data, "%%%s %ld %u %d\n", guard,
|
|
|
|
(long) cmdq->time, cmdq->number, flags);
|
2013-03-25 11:41:16 +00:00
|
|
|
server_push_stdout(c);
|
|
|
|
}
|
|
|
|
|
2013-03-24 09:54:10 +00:00
|
|
|
/* Add command list to queue and begin processing if needed. */
|
|
|
|
void
|
2015-04-19 21:34:21 +00:00
|
|
|
cmdq_run(struct cmd_q *cmdq, struct cmd_list *cmdlist, struct mouse_event *m)
|
2013-03-24 09:54:10 +00:00
|
|
|
{
|
2015-04-19 21:34:21 +00:00
|
|
|
cmdq_append(cmdq, cmdlist, m);
|
2013-03-24 09:54:10 +00:00
|
|
|
|
|
|
|
if (cmdq->item == NULL) {
|
|
|
|
cmdq->cmd = NULL;
|
|
|
|
cmdq_continue(cmdq);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add command list to queue. */
|
|
|
|
void
|
2015-04-19 21:34:21 +00:00
|
|
|
cmdq_append(struct cmd_q *cmdq, struct cmd_list *cmdlist, struct mouse_event *m)
|
2013-03-24 09:54:10 +00:00
|
|
|
{
|
|
|
|
struct cmd_q_item *item;
|
|
|
|
|
|
|
|
item = xcalloc(1, sizeof *item);
|
|
|
|
item->cmdlist = cmdlist;
|
|
|
|
TAILQ_INSERT_TAIL(&cmdq->queue, item, qentry);
|
|
|
|
cmdlist->references++;
|
2015-04-19 21:34:21 +00:00
|
|
|
|
|
|
|
if (m != NULL)
|
|
|
|
memcpy(&item->mouse, m, sizeof item->mouse);
|
|
|
|
else
|
|
|
|
item->mouse.valid = 0;
|
2013-03-24 09:54:10 +00:00
|
|
|
}
|
|
|
|
|
2015-06-17 17:02:15 +00:00
|
|
|
/* Process one command. */
|
|
|
|
enum cmd_retval
|
|
|
|
cmdq_continue_one(struct cmd_q *cmdq)
|
|
|
|
{
|
|
|
|
struct cmd *cmd = cmdq->cmd;
|
|
|
|
enum cmd_retval retval;
|
|
|
|
char tmp[1024];
|
|
|
|
int flags = !!(cmd->flags & CMD_CONTROL);
|
|
|
|
|
|
|
|
cmd_print(cmd, tmp, sizeof tmp);
|
|
|
|
log_debug("cmdq %p: %s", cmdq, tmp);
|
|
|
|
|
|
|
|
cmdq->time = time(NULL);
|
|
|
|
cmdq->number++;
|
|
|
|
|
|
|
|
cmdq_guard(cmdq, "begin", flags);
|
|
|
|
|
|
|
|
retval = cmd->entry->exec(cmd, cmdq);
|
|
|
|
|
|
|
|
if (retval == CMD_RETURN_ERROR)
|
|
|
|
cmdq_guard(cmdq, "error", flags);
|
|
|
|
else
|
|
|
|
cmdq_guard(cmdq, "end", flags);
|
|
|
|
return (retval);
|
|
|
|
}
|
|
|
|
|
2013-03-24 09:54:10 +00:00
|
|
|
/* Continue processing command queue. Returns 1 if finishes empty. */
|
|
|
|
int
|
|
|
|
cmdq_continue(struct cmd_q *cmdq)
|
|
|
|
{
|
2015-09-16 22:41:00 +00:00
|
|
|
struct client *c = cmdq->client;
|
2013-03-24 09:54:10 +00:00
|
|
|
struct cmd_q_item *next;
|
|
|
|
enum cmd_retval retval;
|
2015-06-17 17:02:15 +00:00
|
|
|
int empty;
|
2013-03-24 09:54:10 +00:00
|
|
|
|
2015-02-12 09:56:19 +00:00
|
|
|
cmdq->references++;
|
2013-03-24 09:54:10 +00:00
|
|
|
notify_disable();
|
|
|
|
|
2015-10-20 21:12:08 +00:00
|
|
|
log_debug("continuing cmdq %p: flags %#x, client %p", cmdq, cmdq->flags,
|
|
|
|
c);
|
2015-09-16 22:41:00 +00:00
|
|
|
|
2013-03-24 09:54:10 +00:00
|
|
|
empty = TAILQ_EMPTY(&cmdq->queue);
|
|
|
|
if (empty)
|
|
|
|
goto empty;
|
|
|
|
|
|
|
|
if (cmdq->item == NULL) {
|
|
|
|
cmdq->item = TAILQ_FIRST(&cmdq->queue);
|
|
|
|
cmdq->cmd = TAILQ_FIRST(&cmdq->item->cmdlist->list);
|
|
|
|
} else
|
|
|
|
cmdq->cmd = TAILQ_NEXT(cmdq->cmd, qentry);
|
|
|
|
|
|
|
|
do {
|
|
|
|
while (cmdq->cmd != NULL) {
|
2015-06-17 17:02:15 +00:00
|
|
|
retval = cmdq_continue_one(cmdq);
|
2013-03-24 09:54:10 +00:00
|
|
|
if (retval == CMD_RETURN_ERROR)
|
|
|
|
break;
|
|
|
|
if (retval == CMD_RETURN_WAIT)
|
|
|
|
goto out;
|
|
|
|
if (retval == CMD_RETURN_STOP) {
|
|
|
|
cmdq_flush(cmdq);
|
|
|
|
goto empty;
|
|
|
|
}
|
|
|
|
cmdq->cmd = TAILQ_NEXT(cmdq->cmd, qentry);
|
|
|
|
}
|
2014-10-21 22:06:46 +00:00
|
|
|
next = TAILQ_NEXT(cmdq->item, qentry);
|
2013-03-24 09:54:10 +00:00
|
|
|
|
|
|
|
TAILQ_REMOVE(&cmdq->queue, cmdq->item, qentry);
|
|
|
|
cmd_list_free(cmdq->item->cmdlist);
|
|
|
|
free(cmdq->item);
|
|
|
|
|
|
|
|
cmdq->item = next;
|
|
|
|
if (cmdq->item != NULL)
|
|
|
|
cmdq->cmd = TAILQ_FIRST(&cmdq->item->cmdlist->list);
|
|
|
|
} while (cmdq->item != NULL);
|
|
|
|
|
|
|
|
empty:
|
2013-10-10 12:07:36 +00:00
|
|
|
if (cmdq->client_exit > 0)
|
2013-03-24 09:54:10 +00:00
|
|
|
cmdq->client->flags |= CLIENT_EXIT;
|
|
|
|
if (cmdq->emptyfn != NULL)
|
2015-02-12 09:56:19 +00:00
|
|
|
cmdq->emptyfn(cmdq);
|
2013-03-24 09:54:10 +00:00
|
|
|
empty = 1;
|
|
|
|
|
|
|
|
out:
|
|
|
|
notify_enable();
|
2015-02-12 09:56:19 +00:00
|
|
|
cmdq_free(cmdq);
|
|
|
|
|
2013-03-24 09:54:10 +00:00
|
|
|
return (empty);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Flush command queue. */
|
|
|
|
void
|
|
|
|
cmdq_flush(struct cmd_q *cmdq)
|
|
|
|
{
|
|
|
|
struct cmd_q_item *item, *item1;
|
|
|
|
|
|
|
|
TAILQ_FOREACH_SAFE(item, &cmdq->queue, qentry, item1) {
|
|
|
|
TAILQ_REMOVE(&cmdq->queue, item, qentry);
|
|
|
|
cmd_list_free(item->cmdlist);
|
|
|
|
free(item);
|
|
|
|
}
|
|
|
|
cmdq->item = NULL;
|
|
|
|
}
|