1 |
|
|
/* $OpenBSD: control.c,v 1.17 2016/01/19 15:59:12 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. */ |
30 |
|
|
void |
31 |
|
|
control_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. */ |
44 |
|
|
void |
45 |
|
|
control_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 input callback. Read lines and fire commands. */ |
53 |
|
|
void |
54 |
|
|
control_callback(struct client *c, int closed, __unused void *data) |
55 |
|
|
{ |
56 |
|
|
char *line, *cause; |
57 |
|
|
struct cmd_list *cmdlist; |
58 |
|
|
struct cmd *cmd; |
59 |
|
|
|
60 |
|
|
if (closed) |
61 |
|
|
c->flags |= CLIENT_EXIT; |
62 |
|
|
|
63 |
|
|
for (;;) { |
64 |
|
|
line = evbuffer_readln(c->stdin_data, NULL, EVBUFFER_EOL_LF); |
65 |
|
|
if (line == NULL) |
66 |
|
|
break; |
67 |
|
|
if (*line == '\0') { /* empty line exit */ |
68 |
|
|
c->flags |= CLIENT_EXIT; |
69 |
|
|
break; |
70 |
|
|
} |
71 |
|
|
|
72 |
|
|
if (cmd_string_parse(line, &cmdlist, NULL, 0, &cause) != 0) { |
73 |
|
|
c->cmdq->time = time(NULL); |
74 |
|
|
c->cmdq->number++; |
75 |
|
|
|
76 |
|
|
cmdq_guard(c->cmdq, "begin", 1); |
77 |
|
|
control_write(c, "parse error: %s", cause); |
78 |
|
|
cmdq_guard(c->cmdq, "error", 1); |
79 |
|
|
|
80 |
|
|
free(cause); |
81 |
|
|
} else { |
82 |
|
|
TAILQ_FOREACH(cmd, &cmdlist->list, qentry) |
83 |
|
|
cmd->flags |= CMD_CONTROL; |
84 |
|
|
cmdq_run(c->cmdq, cmdlist, NULL); |
85 |
|
|
cmd_list_free(cmdlist); |
86 |
|
|
} |
87 |
|
|
|
88 |
|
|
free(line); |
89 |
|
|
} |
90 |
|
|
} |