Annotation of src/usr.bin/tmux/server-msg.c, Revision 1.19
1.19 ! nicm 1: /* $OpenBSD: server-msg.c,v 1.18 2009/09/23 06:05:02 nicm Exp $ */
1.1 nicm 2:
3: /*
4: * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
5: *
6: * Permission to use, copy, modify, and distribute this software for any
7: * purpose with or without fee is hereby granted, provided that the above
8: * copyright notice and this permission notice appear in all copies.
9: *
10: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14: * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15: * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16: * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17: */
18:
19: #include <sys/types.h>
1.19 ! nicm 20: #include <sys/ioctl.h>
1.1 nicm 21:
22: #include <errno.h>
23: #include <stdlib.h>
24: #include <string.h>
25: #include <time.h>
26: #include <unistd.h>
27:
28: #include "tmux.h"
29:
1.9 nicm 30: void server_msg_command(struct client *, struct msg_command_data *);
1.13 nicm 31: void server_msg_identify(struct client *, struct msg_identify_data *, int);
1.9 nicm 32:
33: void printflike2 server_msg_command_error(struct cmd_ctx *, const char *, ...);
34: void printflike2 server_msg_command_print(struct cmd_ctx *, const char *, ...);
35: void printflike2 server_msg_command_info(struct cmd_ctx *, const char *, ...);
1.1 nicm 36:
37: int
38: server_msg_dispatch(struct client *c)
39: {
1.11 nicm 40: struct imsg imsg;
1.9 nicm 41: struct msg_command_data commanddata;
42: struct msg_identify_data identifydata;
43: struct msg_unlock_data unlockdata;
1.10 nicm 44: struct msg_environ_data environdata;
1.11 nicm 45: ssize_t n, datalen;
46:
47: if ((n = imsg_read(&c->ibuf)) == -1 || n == 0)
48: return (-1);
1.1 nicm 49:
50: for (;;) {
1.11 nicm 51: if ((n = imsg_get(&c->ibuf, &imsg)) == -1)
52: return (-1);
53: if (n == 0)
1.1 nicm 54: return (0);
1.11 nicm 55: datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
56:
57: if (imsg.hdr.peerid != PROTOCOL_VERSION) {
58: server_write_client(c, MSG_VERSION, NULL, 0);
59: c->flags |= CLIENT_BAD;
60: imsg_free(&imsg);
61: continue;
62: }
1.1 nicm 63:
1.11 nicm 64: log_debug("got %d from client %d", imsg.hdr.type, c->ibuf.fd);
65: switch (imsg.hdr.type) {
1.9 nicm 66: case MSG_COMMAND:
1.11 nicm 67: if (datalen != sizeof commanddata)
1.9 nicm 68: fatalx("bad MSG_COMMAND size");
1.11 nicm 69: memcpy(&commanddata, imsg.data, sizeof commanddata);
1.9 nicm 70:
71: server_msg_command(c, &commanddata);
72: break;
73: case MSG_IDENTIFY:
1.11 nicm 74: if (datalen != sizeof identifydata)
1.9 nicm 75: fatalx("bad MSG_IDENTIFY size");
1.18 nicm 76: if (imsg.fd == -1)
77: fatalx("MSG_IDENTIFY missing fd");
1.11 nicm 78: memcpy(&identifydata, imsg.data, sizeof identifydata);
1.9 nicm 79:
1.13 nicm 80: server_msg_identify(c, &identifydata, imsg.fd);
1.9 nicm 81: break;
82: case MSG_RESIZE:
1.19 ! nicm 83: if (datalen != 0)
1.9 nicm 84: fatalx("bad MSG_RESIZE size");
85:
1.19 ! nicm 86: tty_resize(&c->tty);
! 87: recalculate_sizes();
! 88: server_redraw_client(c);
1.9 nicm 89: break;
90: case MSG_EXITING:
1.11 nicm 91: if (datalen != 0)
1.9 nicm 92: fatalx("bad MSG_EXITING size");
93:
94: c->session = NULL;
1.12 nicm 95: tty_close(&c->tty);
1.9 nicm 96: server_write_client(c, MSG_EXITED, NULL, 0);
97: break;
98: case MSG_UNLOCK:
1.11 nicm 99: if (datalen != sizeof unlockdata)
1.9 nicm 100: fatalx("bad MSG_UNLOCK size");
1.11 nicm 101: memcpy(&unlockdata, imsg.data, sizeof unlockdata);
1.9 nicm 102:
103: unlockdata.pass[(sizeof unlockdata.pass) - 1] = '\0';
1.16 nicm 104: switch (server_unlock(unlockdata.pass)) {
105: case -1:
1.9 nicm 106: server_write_error(c, "bad password");
1.16 nicm 107: break;
108: case -2:
109: server_write_error(c,
110: "too many bad passwords, sleeping");
111: break;
112: }
1.9 nicm 113: memset(&unlockdata, 0, sizeof unlockdata);
114: server_write_client(c, MSG_EXIT, NULL, 0);
115: break;
116: case MSG_WAKEUP:
1.11 nicm 117: if (datalen != 0)
1.9 nicm 118: fatalx("bad MSG_WAKEUP size");
119:
120: c->flags &= ~CLIENT_SUSPENDED;
121: tty_start_tty(&c->tty);
122: server_redraw_client(c);
1.10 nicm 123: break;
124: case MSG_ENVIRON:
1.11 nicm 125: if (datalen != sizeof environdata)
1.10 nicm 126: fatalx("bad MSG_ENVIRON size");
1.11 nicm 127: memcpy(&environdata, imsg.data, sizeof environdata);
1.10 nicm 128:
129: environdata.var[(sizeof environdata.var) - 1] = '\0';
130: if (strchr(environdata.var, '=') != NULL)
131: environ_put(&c->environ, environdata.var);
1.9 nicm 132: break;
133: default:
134: fatalx("unexpected message");
1.1 nicm 135: }
1.11 nicm 136:
137: imsg_free(&imsg);
1.1 nicm 138: }
139: }
140:
141: void printflike2
1.9 nicm 142: server_msg_command_error(struct cmd_ctx *ctx, const char *fmt, ...)
1.1 nicm 143: {
1.7 nicm 144: struct msg_print_data data;
145: va_list ap;
1.1 nicm 146:
147: va_start(ap, fmt);
1.7 nicm 148: xvsnprintf(data.msg, sizeof data.msg, fmt, ap);
1.1 nicm 149: va_end(ap);
150:
1.7 nicm 151: server_write_client(ctx->cmdclient, MSG_ERROR, &data, sizeof data);
1.1 nicm 152: }
153:
154: void printflike2
1.9 nicm 155: server_msg_command_print(struct cmd_ctx *ctx, const char *fmt, ...)
1.1 nicm 156: {
1.7 nicm 157: struct msg_print_data data;
158: va_list ap;
1.1 nicm 159:
160: va_start(ap, fmt);
1.7 nicm 161: xvsnprintf(data.msg, sizeof data.msg, fmt, ap);
1.1 nicm 162: va_end(ap);
163:
1.7 nicm 164: server_write_client(ctx->cmdclient, MSG_PRINT, &data, sizeof data);
1.1 nicm 165: }
166:
167: void printflike2
1.9 nicm 168: server_msg_command_info(struct cmd_ctx *ctx, const char *fmt, ...)
1.1 nicm 169: {
1.7 nicm 170: struct msg_print_data data;
171: va_list ap;
1.1 nicm 172:
173: if (be_quiet)
174: return;
175:
176: va_start(ap, fmt);
1.7 nicm 177: xvsnprintf(data.msg, sizeof data.msg, fmt, ap);
1.1 nicm 178: va_end(ap);
179:
1.7 nicm 180: server_write_client(ctx->cmdclient, MSG_PRINT, &data, sizeof data);
1.1 nicm 181: }
182:
1.6 nicm 183: void
1.9 nicm 184: server_msg_command(struct client *c, struct msg_command_data *data)
1.1 nicm 185: {
1.9 nicm 186: struct cmd_ctx ctx;
187: struct cmd_list *cmdlist = NULL;
188: struct cmd *cmd;
189: int argc;
190: char **argv, *cause;
1.1 nicm 191:
192: server_activity = time(NULL);
193:
1.9 nicm 194: ctx.error = server_msg_command_error;
195: ctx.print = server_msg_command_print;
196: ctx.info = server_msg_command_info;
1.1 nicm 197:
1.9 nicm 198: ctx.msgdata = data;
1.1 nicm 199: ctx.curclient = NULL;
200:
201: ctx.cmdclient = c;
202:
1.9 nicm 203: argc = data->argc;
204: data->argv[(sizeof data->argv) - 1] = '\0';
205: if (cmd_unpack_argv(data->argv, sizeof data->argv, argc, &argv) != 0) {
206: server_msg_command_error(&ctx, "command too long");
1.7 nicm 207: goto error;
208: }
209:
210: if (argc == 0) {
211: argc = 1;
212: argv = xcalloc(1, sizeof *argv);
213: *argv = xstrdup("new-session");
214: }
215:
216: if ((cmdlist = cmd_list_parse(argc, argv, &cause)) == NULL) {
1.9 nicm 217: server_msg_command_error(&ctx, "%s", cause);
1.7 nicm 218: cmd_free_argv(argc, argv);
219: goto error;
220: }
221: cmd_free_argv(argc, argv);
222:
1.9 nicm 223: if (data->pid != -1) {
1.1 nicm 224: TAILQ_FOREACH(cmd, cmdlist, qentry) {
225: if (cmd->entry->flags & CMD_CANTNEST) {
1.9 nicm 226: server_msg_command_error(&ctx,
1.1 nicm 227: "sessions should be nested with care. "
228: "unset $TMUX to force");
1.7 nicm 229: goto error;
1.1 nicm 230: }
231: }
232: }
233:
234: if (cmd_list_exec(cmdlist, &ctx) != 1)
235: server_write_client(c, MSG_EXIT, NULL, 0);
236: cmd_list_free(cmdlist);
1.7 nicm 237: return;
238:
239: error:
240: if (cmdlist != NULL)
241: cmd_list_free(cmdlist);
242: server_write_client(c, MSG_EXIT, NULL, 0);
1.1 nicm 243: }
244:
1.6 nicm 245: void
1.13 nicm 246: server_msg_identify(struct client *c, struct msg_identify_data *data, int fd)
1.1 nicm 247: {
248: c->cwd = NULL;
1.9 nicm 249: data->cwd[(sizeof data->cwd) - 1] = '\0';
250: if (*data->cwd != '\0')
251: c->cwd = xstrdup(data->cwd);
252:
253: data->term[(sizeof data->term) - 1] = '\0';
1.18 nicm 254: tty_init(&c->tty, fd, data->term);
1.9 nicm 255: if (data->flags & IDENTIFY_UTF8)
1.1 nicm 256: c->tty.flags |= TTY_UTF8;
1.9 nicm 257: if (data->flags & IDENTIFY_256COLOURS)
1.1 nicm 258: c->tty.term_flags |= TERM_256COLOURS;
1.9 nicm 259: else if (data->flags & IDENTIFY_88COLOURS)
1.1 nicm 260: c->tty.term_flags |= TERM_88COLOURS;
1.9 nicm 261: if (data->flags & IDENTIFY_HASDEFAULTS)
1.1 nicm 262: c->tty.term_flags |= TERM_HASDEFAULTS;
1.18 nicm 263:
1.19 ! nicm 264: tty_resize(&c->tty);
1.5 nicm 265:
1.1 nicm 266: c->flags |= CLIENT_TERMINAL;
267: }