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