Annotation of src/usr.bin/tmux/server-msg.c, Revision 1.15
1.15 ! nicm 1: /* $OpenBSD: server-msg.c,v 1.14 2009/08/13 12:15:45 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>
20:
21: #include <errno.h>
22: #include <stdlib.h>
23: #include <string.h>
24: #include <time.h>
25: #include <unistd.h>
26:
27: #include "tmux.h"
28:
1.9 nicm 29: void server_msg_command(struct client *, struct msg_command_data *);
1.13 nicm 30: void server_msg_identify(struct client *, struct msg_identify_data *, int);
1.9 nicm 31: void server_msg_resize(struct client *, struct msg_resize_data *);
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_resize_data resizedata;
44: struct msg_unlock_data unlockdata;
1.10 nicm 45: struct msg_environ_data environdata;
1.11 nicm 46: ssize_t n, datalen;
47:
48: if ((n = imsg_read(&c->ibuf)) == -1 || n == 0)
49: return (-1);
1.1 nicm 50:
51: for (;;) {
1.11 nicm 52: if ((n = imsg_get(&c->ibuf, &imsg)) == -1)
53: return (-1);
54: if (n == 0)
1.1 nicm 55: return (0);
1.11 nicm 56: datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
57:
58: if (imsg.hdr.peerid != PROTOCOL_VERSION) {
59: server_write_client(c, MSG_VERSION, NULL, 0);
60: c->flags |= CLIENT_BAD;
61: imsg_free(&imsg);
62: continue;
63: }
1.1 nicm 64:
1.11 nicm 65: log_debug("got %d from client %d", imsg.hdr.type, c->ibuf.fd);
66: switch (imsg.hdr.type) {
1.9 nicm 67: case MSG_COMMAND:
1.11 nicm 68: if (datalen != sizeof commanddata)
1.9 nicm 69: fatalx("bad MSG_COMMAND size");
1.11 nicm 70: memcpy(&commanddata, imsg.data, sizeof commanddata);
1.9 nicm 71:
72: server_msg_command(c, &commanddata);
73: break;
74: case MSG_IDENTIFY:
1.11 nicm 75: if (datalen != sizeof identifydata)
1.9 nicm 76: fatalx("bad MSG_IDENTIFY size");
1.11 nicm 77: memcpy(&identifydata, imsg.data, sizeof identifydata);
1.9 nicm 78:
1.13 nicm 79: server_msg_identify(c, &identifydata, imsg.fd);
1.9 nicm 80: break;
81: case MSG_RESIZE:
1.11 nicm 82: if (datalen != sizeof resizedata)
1.9 nicm 83: fatalx("bad MSG_RESIZE size");
1.11 nicm 84: memcpy(&resizedata, imsg.data, sizeof resizedata);
1.9 nicm 85:
86: server_msg_resize(c, &resizedata);
87: break;
88: case MSG_EXITING:
1.11 nicm 89: if (datalen != 0)
1.9 nicm 90: fatalx("bad MSG_EXITING size");
91:
92: c->session = NULL;
1.12 nicm 93: tty_close(&c->tty);
1.9 nicm 94: server_write_client(c, MSG_EXITED, NULL, 0);
95: break;
96: case MSG_UNLOCK:
1.11 nicm 97: if (datalen != sizeof unlockdata)
1.9 nicm 98: fatalx("bad MSG_UNLOCK size");
1.11 nicm 99: memcpy(&unlockdata, imsg.data, sizeof unlockdata);
1.9 nicm 100:
101: unlockdata.pass[(sizeof unlockdata.pass) - 1] = '\0';
102: if (server_unlock(unlockdata.pass) != 0)
103: server_write_error(c, "bad password");
104: memset(&unlockdata, 0, sizeof unlockdata);
105: server_write_client(c, MSG_EXIT, NULL, 0);
106: break;
107: case MSG_WAKEUP:
1.11 nicm 108: if (datalen != 0)
1.9 nicm 109: fatalx("bad MSG_WAKEUP size");
110:
111: c->flags &= ~CLIENT_SUSPENDED;
112: tty_start_tty(&c->tty);
113: server_redraw_client(c);
1.10 nicm 114: break;
115: case MSG_ENVIRON:
1.11 nicm 116: if (datalen != sizeof environdata)
1.10 nicm 117: fatalx("bad MSG_ENVIRON size");
1.11 nicm 118: memcpy(&environdata, imsg.data, sizeof environdata);
1.10 nicm 119:
120: environdata.var[(sizeof environdata.var) - 1] = '\0';
121: if (strchr(environdata.var, '=') != NULL)
122: environ_put(&c->environ, environdata.var);
1.9 nicm 123: break;
124: default:
125: fatalx("unexpected message");
1.1 nicm 126: }
1.11 nicm 127:
128: imsg_free(&imsg);
1.1 nicm 129: }
130: }
131:
132: void printflike2
1.9 nicm 133: server_msg_command_error(struct cmd_ctx *ctx, const char *fmt, ...)
1.1 nicm 134: {
1.7 nicm 135: struct msg_print_data data;
136: va_list ap;
1.1 nicm 137:
138: va_start(ap, fmt);
1.7 nicm 139: xvsnprintf(data.msg, sizeof data.msg, fmt, ap);
1.1 nicm 140: va_end(ap);
141:
1.7 nicm 142: server_write_client(ctx->cmdclient, MSG_ERROR, &data, sizeof data);
1.1 nicm 143: }
144:
145: void printflike2
1.9 nicm 146: server_msg_command_print(struct cmd_ctx *ctx, const char *fmt, ...)
1.1 nicm 147: {
1.7 nicm 148: struct msg_print_data data;
149: va_list ap;
1.1 nicm 150:
151: va_start(ap, fmt);
1.7 nicm 152: xvsnprintf(data.msg, sizeof data.msg, fmt, ap);
1.1 nicm 153: va_end(ap);
154:
1.7 nicm 155: server_write_client(ctx->cmdclient, MSG_PRINT, &data, sizeof data);
1.1 nicm 156: }
157:
158: void printflike2
1.9 nicm 159: server_msg_command_info(struct cmd_ctx *ctx, const char *fmt, ...)
1.1 nicm 160: {
1.7 nicm 161: struct msg_print_data data;
162: va_list ap;
1.1 nicm 163:
164: if (be_quiet)
165: return;
166:
167: va_start(ap, fmt);
1.7 nicm 168: xvsnprintf(data.msg, sizeof data.msg, fmt, ap);
1.1 nicm 169: va_end(ap);
170:
1.7 nicm 171: server_write_client(ctx->cmdclient, MSG_PRINT, &data, sizeof data);
1.1 nicm 172: }
173:
1.6 nicm 174: void
1.9 nicm 175: server_msg_command(struct client *c, struct msg_command_data *data)
1.1 nicm 176: {
1.9 nicm 177: struct cmd_ctx ctx;
178: struct cmd_list *cmdlist = NULL;
179: struct cmd *cmd;
180: int argc;
181: char **argv, *cause;
1.1 nicm 182:
183: server_activity = time(NULL);
184:
1.9 nicm 185: ctx.error = server_msg_command_error;
186: ctx.print = server_msg_command_print;
187: ctx.info = server_msg_command_info;
1.1 nicm 188:
1.9 nicm 189: ctx.msgdata = data;
1.1 nicm 190: ctx.curclient = NULL;
191:
192: ctx.cmdclient = c;
193:
1.9 nicm 194: argc = data->argc;
195: data->argv[(sizeof data->argv) - 1] = '\0';
196: if (cmd_unpack_argv(data->argv, sizeof data->argv, argc, &argv) != 0) {
197: server_msg_command_error(&ctx, "command too long");
1.7 nicm 198: goto error;
199: }
200:
201: if (argc == 0) {
202: argc = 1;
203: argv = xcalloc(1, sizeof *argv);
204: *argv = xstrdup("new-session");
205: }
206:
207: if ((cmdlist = cmd_list_parse(argc, argv, &cause)) == NULL) {
1.9 nicm 208: server_msg_command_error(&ctx, "%s", cause);
1.7 nicm 209: cmd_free_argv(argc, argv);
210: goto error;
211: }
212: cmd_free_argv(argc, argv);
213:
1.9 nicm 214: if (data->pid != -1) {
1.1 nicm 215: TAILQ_FOREACH(cmd, cmdlist, qentry) {
216: if (cmd->entry->flags & CMD_CANTNEST) {
1.9 nicm 217: server_msg_command_error(&ctx,
1.1 nicm 218: "sessions should be nested with care. "
219: "unset $TMUX to force");
1.7 nicm 220: goto error;
1.1 nicm 221: }
222: }
223: }
224:
225: if (cmd_list_exec(cmdlist, &ctx) != 1)
226: server_write_client(c, MSG_EXIT, NULL, 0);
227: cmd_list_free(cmdlist);
1.7 nicm 228: return;
229:
230: error:
231: if (cmdlist != NULL)
232: cmd_list_free(cmdlist);
233: server_write_client(c, MSG_EXIT, NULL, 0);
1.1 nicm 234: }
235:
1.6 nicm 236: void
1.13 nicm 237: server_msg_identify(struct client *c, struct msg_identify_data *data, int fd)
1.1 nicm 238: {
1.9 nicm 239: c->tty.sx = data->sx;
1.14 nicm 240: if (c->tty.sx == 0)
241: c->tty.sx = 80;
1.9 nicm 242: c->tty.sy = data->sy;
1.14 nicm 243: if (c->tty.sy == 0)
244: c->tty.sy = 25;
1.1 nicm 245:
246: c->cwd = NULL;
1.9 nicm 247: data->cwd[(sizeof data->cwd) - 1] = '\0';
248: if (*data->cwd != '\0')
249: c->cwd = xstrdup(data->cwd);
250:
251: data->tty[(sizeof data->tty) - 1] = '\0';
252: data->term[(sizeof data->term) - 1] = '\0';
1.13 nicm 253: tty_init(&c->tty, fd, data->tty, data->term);
1.9 nicm 254: if (data->flags & IDENTIFY_UTF8)
1.1 nicm 255: c->tty.flags |= TTY_UTF8;
1.9 nicm 256: if (data->flags & IDENTIFY_256COLOURS)
1.1 nicm 257: c->tty.term_flags |= TERM_256COLOURS;
1.9 nicm 258: else if (data->flags & IDENTIFY_88COLOURS)
1.1 nicm 259: c->tty.term_flags |= TERM_88COLOURS;
1.9 nicm 260: if (data->flags & IDENTIFY_HASDEFAULTS)
1.1 nicm 261: c->tty.term_flags |= TERM_HASDEFAULTS;
1.5 nicm 262:
1.1 nicm 263: c->flags |= CLIENT_TERMINAL;
264: }
265:
1.6 nicm 266: void
1.9 nicm 267: server_msg_resize(struct client *c, struct msg_resize_data *data)
1.1 nicm 268: {
1.9 nicm 269: c->tty.sx = data->sx;
1.1 nicm 270: if (c->tty.sx == 0)
271: c->tty.sx = 80;
1.9 nicm 272: c->tty.sy = data->sy;
1.1 nicm 273: if (c->tty.sy == 0)
274: c->tty.sy = 25;
275:
276: c->tty.cx = UINT_MAX;
277: c->tty.cy = UINT_MAX;
278: c->tty.rupper = UINT_MAX;
279: c->tty.rlower = UINT_MAX;
280:
281: recalculate_sizes();
282:
283: /* Always redraw this client. */
284: server_redraw_client(c);
285: }