Annotation of src/usr.bin/tmux/control.c, Revision 1.22
1.22 ! nicm 1: /* $OpenBSD: control.c,v 1.21 2019/05/18 21:14:10 nicm Exp $ */
1.1 nicm 2:
3: /*
1.17 nicm 4: * Copyright (c) 2012 Nicholas Marriott <nicholas.marriott@gmail.com>
1.1 nicm 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>
1.2 nicm 23: #include <stdlib.h>
1.1 nicm 24: #include <string.h>
1.10 nicm 25: #include <time.h>
1.1 nicm 26:
27: #include "tmux.h"
28:
29: /* Write a line. */
1.13 nicm 30: void
1.1 nicm 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:
1.4 nicm 39: evbuffer_add(c->stdout_data, "\n", 1);
1.15 nicm 40: server_client_push_stdout(c);
1.4 nicm 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);
1.1 nicm 48: evbuffer_add(c->stdout_data, "\n", 1);
1.15 nicm 49: server_client_push_stdout(c);
1.1 nicm 50: }
51:
1.18 nicm 52: /* Control error callback. */
53: static enum cmd_retval
1.19 nicm 54: control_error(struct cmdq_item *item, void *data)
1.18 nicm 55: {
1.19 nicm 56: struct client *c = item->client;
1.18 nicm 57: char *error = data;
58:
1.19 nicm 59: cmdq_guard(item, "begin", 1);
1.18 nicm 60: control_write(c, "parse error: %s", error);
1.19 nicm 61: cmdq_guard(item, "error", 1);
1.18 nicm 62:
63: free(error);
64: return (CMD_RETURN_NORMAL);
65: }
66:
1.1 nicm 67: /* Control input callback. Read lines and fire commands. */
68: void
1.16 nicm 69: control_callback(struct client *c, int closed, __unused void *data)
1.1 nicm 70: {
1.22 ! nicm 71: char *line;
1.19 nicm 72: struct cmdq_item *item;
1.22 ! nicm 73: struct cmd_parse_result *pr;
1.1 nicm 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: c->flags |= CLIENT_EXIT;
84: break;
85: }
86:
1.22 ! nicm 87: pr = cmd_parse_from_string(line, NULL);
! 88: switch (pr->status) {
! 89: case CMD_PARSE_EMPTY:
! 90: break;
! 91: case CMD_PARSE_ERROR:
! 92: item = cmdq_get_callback(control_error, pr->error);
1.19 nicm 93: cmdq_append(c, item);
1.22 ! nicm 94: free(pr->error);
! 95: break;
! 96: case CMD_PARSE_SUCCESS:
! 97: item = cmdq_get_command(pr->cmdlist, NULL, NULL, 0);
1.21 nicm 98: item->shared->flags |= CMDQ_SHARED_CONTROL;
1.19 nicm 99: cmdq_append(c, item);
1.22 ! nicm 100: cmd_list_free(pr->cmdlist);
! 101: break;
1.1 nicm 102: }
103:
1.2 nicm 104: free(line);
1.1 nicm 105: }
106: }