2012-06-18 13:16:42 +00:00
|
|
|
/* $OpenBSD$ */
|
|
|
|
|
|
|
|
/*
|
2016-01-19 15:59:12 +00:00
|
|
|
* Copyright (c) 2012 Nicholas Marriott <nicholas.marriott@gmail.com>
|
2012-06-18 13:16:42 +00:00
|
|
|
* Copyright (c) 2012 George Nachman <tmux@georgester.com>
|
|
|
|
*
|
|
|
|
* 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 <event.h>
|
2012-07-10 11:53:01 +00:00
|
|
|
#include <stdlib.h>
|
2012-06-18 13:16:42 +00:00
|
|
|
#include <string.h>
|
2013-03-26 10:54:48 +00:00
|
|
|
#include <time.h>
|
2012-06-18 13:16:42 +00:00
|
|
|
|
|
|
|
#include "tmux.h"
|
|
|
|
|
|
|
|
/* Write a line. */
|
2014-10-20 23:57:13 +00:00
|
|
|
void
|
2012-06-18 13:16:42 +00:00
|
|
|
control_write(struct client *c, const char *fmt, ...)
|
|
|
|
{
|
2019-12-12 11:39:56 +00:00
|
|
|
va_list ap;
|
2012-06-18 13:16:42 +00:00
|
|
|
|
|
|
|
va_start(ap, fmt);
|
2019-12-12 11:39:56 +00:00
|
|
|
file_vprint(c, fmt, ap);
|
|
|
|
file_print(c, "\n");
|
2012-06-18 13:16:42 +00:00
|
|
|
va_end(ap);
|
2012-09-24 13:39:10 +00:00
|
|
|
}
|
|
|
|
|
2016-10-16 17:55:14 +00:00
|
|
|
/* Control error callback. */
|
|
|
|
static enum cmd_retval
|
2016-10-16 19:04:05 +00:00
|
|
|
control_error(struct cmdq_item *item, void *data)
|
2016-10-16 17:55:14 +00:00
|
|
|
{
|
2020-04-13 10:59:58 +00:00
|
|
|
struct client *c = cmdq_get_client(item);
|
2016-10-16 17:55:14 +00:00
|
|
|
char *error = data;
|
|
|
|
|
2016-10-16 19:04:05 +00:00
|
|
|
cmdq_guard(item, "begin", 1);
|
2016-10-16 17:55:14 +00:00
|
|
|
control_write(c, "parse error: %s", error);
|
2016-10-16 19:04:05 +00:00
|
|
|
cmdq_guard(item, "error", 1);
|
2016-10-16 17:55:14 +00:00
|
|
|
|
|
|
|
free(error);
|
|
|
|
return (CMD_RETURN_NORMAL);
|
|
|
|
}
|
|
|
|
|
2012-06-18 13:16:42 +00:00
|
|
|
/* Control input callback. Read lines and fire commands. */
|
2019-12-12 11:39:56 +00:00
|
|
|
static void
|
|
|
|
control_callback(__unused struct client *c, __unused const char *path,
|
|
|
|
int error, int closed, struct evbuffer *buffer, __unused void *data)
|
2012-06-18 13:16:42 +00:00
|
|
|
{
|
2019-05-23 11:13:30 +00:00
|
|
|
char *line;
|
2016-10-16 19:04:05 +00:00
|
|
|
struct cmdq_item *item;
|
2020-04-13 15:55:51 +00:00
|
|
|
struct cmdq_state *state;
|
2019-05-23 11:13:30 +00:00
|
|
|
struct cmd_parse_result *pr;
|
2012-06-18 13:16:42 +00:00
|
|
|
|
2019-12-12 11:39:56 +00:00
|
|
|
if (closed || error != 0)
|
2012-06-18 13:16:42 +00:00
|
|
|
c->flags |= CLIENT_EXIT;
|
|
|
|
|
|
|
|
for (;;) {
|
2019-12-12 11:39:56 +00:00
|
|
|
line = evbuffer_readln(buffer, NULL, EVBUFFER_EOL_LF);
|
2012-06-18 13:16:42 +00:00
|
|
|
if (line == NULL)
|
|
|
|
break;
|
2019-12-12 11:39:56 +00:00
|
|
|
log_debug("%s: %s", __func__, line);
|
2012-06-18 13:16:42 +00:00
|
|
|
if (*line == '\0') { /* empty line exit */
|
2019-07-09 13:19:36 +00:00
|
|
|
free(line);
|
2012-06-18 13:16:42 +00:00
|
|
|
c->flags |= CLIENT_EXIT;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-05-23 11:13:30 +00:00
|
|
|
pr = cmd_parse_from_string(line, NULL);
|
|
|
|
switch (pr->status) {
|
|
|
|
case CMD_PARSE_EMPTY:
|
|
|
|
break;
|
|
|
|
case CMD_PARSE_ERROR:
|
|
|
|
item = cmdq_get_callback(control_error, pr->error);
|
2016-10-16 19:04:05 +00:00
|
|
|
cmdq_append(c, item);
|
2019-05-23 11:13:30 +00:00
|
|
|
break;
|
|
|
|
case CMD_PARSE_SUCCESS:
|
2020-04-13 15:55:51 +00:00
|
|
|
state = cmdq_new_state(NULL, NULL, CMDQ_STATE_CONTROL);
|
|
|
|
item = cmdq_get_command(pr->cmdlist, state);
|
2016-10-16 19:04:05 +00:00
|
|
|
cmdq_append(c, item);
|
2020-04-13 15:55:51 +00:00
|
|
|
cmdq_free_state(state);
|
2019-05-23 11:13:30 +00:00
|
|
|
cmd_list_free(pr->cmdlist);
|
|
|
|
break;
|
2012-06-18 13:16:42 +00:00
|
|
|
}
|
|
|
|
|
2012-07-10 11:53:01 +00:00
|
|
|
free(line);
|
2012-06-18 13:16:42 +00:00
|
|
|
}
|
|
|
|
}
|
2019-12-12 11:39:56 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
control_start(struct client *c)
|
|
|
|
{
|
|
|
|
file_read(c, "-", control_callback, c);
|
|
|
|
|
|
|
|
if (c->flags & CLIENT_CONTROLCONTROL)
|
|
|
|
file_print(c, "\033P1000p");
|
|
|
|
}
|