xref: /minix3/external/bsd/tmux/dist/control.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /* Id */
2*0a6a1f1dSLionel Sambuc 
3*0a6a1f1dSLionel Sambuc /*
4*0a6a1f1dSLionel Sambuc  * Copyright (c) 2012 Nicholas Marriott <nicm@users.sourceforge.net>
5*0a6a1f1dSLionel Sambuc  * Copyright (c) 2012 George Nachman <tmux@georgester.com>
6*0a6a1f1dSLionel Sambuc  *
7*0a6a1f1dSLionel Sambuc  * Permission to use, copy, modify, and distribute this software for any
8*0a6a1f1dSLionel Sambuc  * purpose with or without fee is hereby granted, provided that the above
9*0a6a1f1dSLionel Sambuc  * copyright notice and this permission notice appear in all copies.
10*0a6a1f1dSLionel Sambuc  *
11*0a6a1f1dSLionel Sambuc  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12*0a6a1f1dSLionel Sambuc  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13*0a6a1f1dSLionel Sambuc  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14*0a6a1f1dSLionel Sambuc  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15*0a6a1f1dSLionel Sambuc  * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
16*0a6a1f1dSLionel Sambuc  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
17*0a6a1f1dSLionel Sambuc  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18*0a6a1f1dSLionel Sambuc  */
19*0a6a1f1dSLionel Sambuc 
20*0a6a1f1dSLionel Sambuc #include <sys/types.h>
21*0a6a1f1dSLionel Sambuc 
22*0a6a1f1dSLionel Sambuc #include <event.h>
23*0a6a1f1dSLionel Sambuc #include <stdlib.h>
24*0a6a1f1dSLionel Sambuc #include <string.h>
25*0a6a1f1dSLionel Sambuc #include <time.h>
26*0a6a1f1dSLionel Sambuc 
27*0a6a1f1dSLionel Sambuc #include "tmux.h"
28*0a6a1f1dSLionel Sambuc 
29*0a6a1f1dSLionel Sambuc /* Write a line. */
30*0a6a1f1dSLionel Sambuc void printflike2
control_write(struct client * c,const char * fmt,...)31*0a6a1f1dSLionel Sambuc control_write(struct client *c, const char *fmt, ...)
32*0a6a1f1dSLionel Sambuc {
33*0a6a1f1dSLionel Sambuc 	va_list		 ap;
34*0a6a1f1dSLionel Sambuc 
35*0a6a1f1dSLionel Sambuc 	va_start(ap, fmt);
36*0a6a1f1dSLionel Sambuc 	evbuffer_add_vprintf(c->stdout_data, fmt, ap);
37*0a6a1f1dSLionel Sambuc 	va_end(ap);
38*0a6a1f1dSLionel Sambuc 
39*0a6a1f1dSLionel Sambuc 	evbuffer_add(c->stdout_data, "\n", 1);
40*0a6a1f1dSLionel Sambuc 	server_push_stdout(c);
41*0a6a1f1dSLionel Sambuc }
42*0a6a1f1dSLionel Sambuc 
43*0a6a1f1dSLionel Sambuc /* Write a buffer, adding a terminal newline. Empties buffer. */
44*0a6a1f1dSLionel Sambuc void
control_write_buffer(struct client * c,struct evbuffer * buffer)45*0a6a1f1dSLionel Sambuc control_write_buffer(struct client *c, struct evbuffer *buffer)
46*0a6a1f1dSLionel Sambuc {
47*0a6a1f1dSLionel Sambuc 	evbuffer_add_buffer(c->stdout_data, buffer);
48*0a6a1f1dSLionel Sambuc 	evbuffer_add(c->stdout_data, "\n", 1);
49*0a6a1f1dSLionel Sambuc 	server_push_stdout(c);
50*0a6a1f1dSLionel Sambuc }
51*0a6a1f1dSLionel Sambuc 
52*0a6a1f1dSLionel Sambuc /* Control input callback. Read lines and fire commands. */
53*0a6a1f1dSLionel Sambuc void
control_callback(struct client * c,int closed,unused void * data)54*0a6a1f1dSLionel Sambuc control_callback(struct client *c, int closed, unused void *data)
55*0a6a1f1dSLionel Sambuc {
56*0a6a1f1dSLionel Sambuc 	char		*line, *cause;
57*0a6a1f1dSLionel Sambuc 	struct cmd_list	*cmdlist;
58*0a6a1f1dSLionel Sambuc 	struct cmd	*cmd;
59*0a6a1f1dSLionel Sambuc 
60*0a6a1f1dSLionel Sambuc 	if (closed)
61*0a6a1f1dSLionel Sambuc 		c->flags |= CLIENT_EXIT;
62*0a6a1f1dSLionel Sambuc 
63*0a6a1f1dSLionel Sambuc 	for (;;) {
64*0a6a1f1dSLionel Sambuc 		line = evbuffer_readln(c->stdin_data, NULL, EVBUFFER_EOL_LF);
65*0a6a1f1dSLionel Sambuc 		if (line == NULL)
66*0a6a1f1dSLionel Sambuc 			break;
67*0a6a1f1dSLionel Sambuc 		if (*line == '\0') { /* empty line exit */
68*0a6a1f1dSLionel Sambuc 			c->flags |= CLIENT_EXIT;
69*0a6a1f1dSLionel Sambuc 			break;
70*0a6a1f1dSLionel Sambuc 		}
71*0a6a1f1dSLionel Sambuc 
72*0a6a1f1dSLionel Sambuc 		if (cmd_string_parse(line, &cmdlist, NULL, 0, &cause) != 0) {
73*0a6a1f1dSLionel Sambuc 			c->cmdq->time = time(NULL);
74*0a6a1f1dSLionel Sambuc 			c->cmdq->number++;
75*0a6a1f1dSLionel Sambuc 
76*0a6a1f1dSLionel Sambuc 			cmdq_guard(c->cmdq, "begin", 1);
77*0a6a1f1dSLionel Sambuc 			control_write(c, "parse error: %s", cause);
78*0a6a1f1dSLionel Sambuc 			cmdq_guard(c->cmdq, "error", 1);
79*0a6a1f1dSLionel Sambuc 
80*0a6a1f1dSLionel Sambuc 			free(cause);
81*0a6a1f1dSLionel Sambuc 		} else {
82*0a6a1f1dSLionel Sambuc 			TAILQ_FOREACH(cmd, &cmdlist->list, qentry)
83*0a6a1f1dSLionel Sambuc 				cmd->flags |= CMD_CONTROL;
84*0a6a1f1dSLionel Sambuc 			cmdq_run(c->cmdq, cmdlist);
85*0a6a1f1dSLionel Sambuc 			cmd_list_free(cmdlist);
86*0a6a1f1dSLionel Sambuc 		}
87*0a6a1f1dSLionel Sambuc 
88*0a6a1f1dSLionel Sambuc 		free(line);
89*0a6a1f1dSLionel Sambuc 	}
90*0a6a1f1dSLionel Sambuc }
91