diff options
author | 2009-07-30 16:32:12 +0000 | |
---|---|---|
committer | 2009-07-30 16:32:12 +0000 | |
commit | 5b5228379f161fe1bfc1dc914dea28fb52645ee4 (patch) | |
tree | eeed5ee42da65cb6e5859d445984cb8c1ac8303d /usr.bin/tmux/client.c | |
parent | Tell the server when the client gets SIGTERM so it can clean up the terminal (diff) | |
download | wireguard-openbsd-5b5228379f161fe1bfc1dc914dea28fb52645ee4.tar.xz wireguard-openbsd-5b5228379f161fe1bfc1dc914dea28fb52645ee4.zip |
There aren't many client message types or code to handle them so get rid of the
lookup table and use a switch, merge the tiny handler functions into it, and
move the whole lot to client.c.
Also change client_msg_dispatch to consume as many messages as possible and
move the call to it to the right place so it checks for signals afterwards.
Prompted by suggestions from eric@.
Diffstat (limited to 'usr.bin/tmux/client.c')
-rw-r--r-- | usr.bin/tmux/client.c | 83 |
1 files changed, 66 insertions, 17 deletions
diff --git a/usr.bin/tmux/client.c b/usr.bin/tmux/client.c index f8509ebb33b..1d064d45dae 100644 --- a/usr.bin/tmux/client.c +++ b/usr.bin/tmux/client.c @@ -1,4 +1,4 @@ -/* $OpenBSD: client.c,v 1.8 2009/07/30 16:16:19 nicm Exp $ */ +/* $OpenBSD: client.c,v 1.9 2009/07/30 16:32:12 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net> @@ -137,7 +137,6 @@ int client_main(struct client_ctx *cctx) { struct pollfd pfd; - int xtimeout; /* Yay for ncurses namespace! */ siginit(); @@ -158,25 +157,12 @@ client_main(struct client_ctx *cctx) sigcont = 0; } - switch (client_msg_dispatch(cctx)) { - case -1: - goto out; - case 0: - /* May be more in buffer, don't let poll block. */ - xtimeout = 0; - break; - default: - /* Out of data, poll may block. */ - xtimeout = INFTIM; - break; - } - pfd.fd = cctx->srv_fd; pfd.events = POLLIN; if (BUFFER_USED(cctx->srv_out) > 0) pfd.events |= POLLOUT; - if (poll(&pfd, 1, xtimeout) == -1) { + if (poll(&pfd, 1, INFTIM) == -1) { if (errno == EAGAIN || errno == EINTR) continue; fatal("poll failed"); @@ -186,9 +172,11 @@ client_main(struct client_ctx *cctx) cctx->exittype = CCTX_DIED; break; } + + if (client_msg_dispatch(cctx) != 0) + break; } -out: if (sigterm) { printf("[terminated]\n"); return (1); @@ -227,3 +215,64 @@ client_handle_winch(struct client_ctx *cctx) sigwinch = 0; } + +int +client_msg_dispatch(struct client_ctx *cctx) +{ + struct hdr hdr; + struct msg_print_data printdata; + + for (;;) { + if (BUFFER_USED(cctx->srv_in) < sizeof hdr) + return (0); + memcpy(&hdr, BUFFER_OUT(cctx->srv_in), sizeof hdr); + if (BUFFER_USED(cctx->srv_in) < (sizeof hdr) + hdr.size) + return (0); + buffer_remove(cctx->srv_in, sizeof hdr); + + switch (hdr.type) { + case MSG_DETACH: + if (hdr.size != 0) + fatalx("bad MSG_DETACH size"); + + client_write_server(cctx, MSG_EXITING, NULL, 0); + cctx->exittype = CCTX_DETACH; + break; + case MSG_ERROR: + if (hdr.size != sizeof printdata) + fatalx("bad MSG_PRINT size"); + buffer_read(cctx->srv_in, &printdata, sizeof printdata); + printdata.msg[(sizeof printdata.msg) - 1] = '\0'; + + cctx->errstr = xstrdup(printdata.msg); + return (-1); + case MSG_EXIT: + if (hdr.size != 0) + fatalx("bad MSG_EXIT size"); + + client_write_server(cctx, MSG_EXITING, NULL, 0); + cctx->exittype = CCTX_EXIT; + break; + case MSG_EXITED: + if (hdr.size != 0) + fatalx("bad MSG_EXITED size"); + + return (-1); + case MSG_SHUTDOWN: + if (hdr.size != 0) + fatalx("bad MSG_SHUTDOWN size"); + + client_write_server(cctx, MSG_EXITING, NULL, 0); + cctx->exittype = CCTX_SHUTDOWN; + break; + case MSG_SUSPEND: + if (hdr.size != 0) + fatalx("bad MSG_SUSPEND size"); + + client_suspend(); + break; + default: + fatalx("unexpected message"); + } + } +} |