control.c revision 1.24
1/* $OpenBSD: control.c,v 1.24 2019/07/09 13:19:36 nicm Exp $ */
2
3/*
4 * Copyright (c) 2012 Nicholas Marriott <nicholas.marriott@gmail.com>
5 * Copyright (c) 2012 George Nachman <tmux@georgester.com>
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
16 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
17 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */
19
20#include <sys/types.h>
21
22#include <event.h>
23#include <stdlib.h>
24#include <string.h>
25#include <time.h>
26
27#include "tmux.h"
28
29/* Write a line. */
30void
31control_write(struct client *c, const char *fmt, ...)
32{
33	va_list		 ap;
34
35	va_start(ap, fmt);
36	evbuffer_add_vprintf(c->stdout_data, fmt, ap);
37	va_end(ap);
38
39	evbuffer_add(c->stdout_data, "\n", 1);
40	server_client_push_stdout(c);
41}
42
43/* Write a buffer, adding a terminal newline. Empties buffer. */
44void
45control_write_buffer(struct client *c, struct evbuffer *buffer)
46{
47	evbuffer_add_buffer(c->stdout_data, buffer);
48	evbuffer_add(c->stdout_data, "\n", 1);
49	server_client_push_stdout(c);
50}
51
52/* Control error callback. */
53static enum cmd_retval
54control_error(struct cmdq_item *item, void *data)
55{
56	struct client	*c = item->client;
57	char		*error = data;
58
59	cmdq_guard(item, "begin", 1);
60	control_write(c, "parse error: %s", error);
61	cmdq_guard(item, "error", 1);
62
63	free(error);
64	return (CMD_RETURN_NORMAL);
65}
66
67/* Control input callback. Read lines and fire commands. */
68void
69control_callback(struct client *c, int closed, __unused void *data)
70{
71	char			*line;
72	struct cmdq_item	*item;
73	struct cmd_parse_result	*pr;
74
75	if (closed)
76		c->flags |= CLIENT_EXIT;
77
78	for (;;) {
79		line = evbuffer_readln(c->stdin_data, NULL, EVBUFFER_EOL_LF);
80		if (line == NULL)
81			break;
82		if (*line == '\0') { /* empty line exit */
83			free(line);
84			c->flags |= CLIENT_EXIT;
85			break;
86		}
87
88		pr = cmd_parse_from_string(line, NULL);
89		switch (pr->status) {
90		case CMD_PARSE_EMPTY:
91			break;
92		case CMD_PARSE_ERROR:
93			item = cmdq_get_callback(control_error, pr->error);
94			cmdq_append(c, item);
95			break;
96		case CMD_PARSE_SUCCESS:
97			item = cmdq_get_command(pr->cmdlist, NULL, NULL, 0);
98			item->shared->flags |= CMDQ_SHARED_CONTROL;
99			cmdq_append(c, item);
100			cmd_list_free(pr->cmdlist);
101			break;
102		}
103
104		free(line);
105	}
106}
107