diff options
-rw-r--r-- | usr.bin/tmux/Makefile | 4 | ||||
-rw-r--r-- | usr.bin/tmux/client-fn.c | 23 | ||||
-rw-r--r-- | usr.bin/tmux/client-msg.c | 154 | ||||
-rw-r--r-- | usr.bin/tmux/client.c | 83 | ||||
-rw-r--r-- | usr.bin/tmux/tmux.h | 5 |
5 files changed, 92 insertions, 177 deletions
diff --git a/usr.bin/tmux/Makefile b/usr.bin/tmux/Makefile index 9f430f13a03..f6712aab1ae 100644 --- a/usr.bin/tmux/Makefile +++ b/usr.bin/tmux/Makefile @@ -1,8 +1,8 @@ -# $OpenBSD: Makefile,v 1.10 2009/07/22 20:56:58 nicm Exp $ +# $OpenBSD: Makefile,v 1.11 2009/07/30 16:32:12 nicm Exp $ PROG= tmux SRCS= attributes.c buffer-poll.c buffer.c cfg.c client-fn.c \ - client-msg.c client.c clock.c cmd-attach-session.c cmd-bind-key.c \ + client.c clock.c cmd-attach-session.c cmd-bind-key.c \ cmd-break-pane.c cmd-choose-session.c cmd-choose-window.c \ cmd-clear-history.c cmd-clock-mode.c cmd-command-prompt.c \ cmd-confirm-before.c cmd-copy-buffer.c cmd-copy-mode.c \ diff --git a/usr.bin/tmux/client-fn.c b/usr.bin/tmux/client-fn.c index b986bc75a6f..eeb83421cbc 100644 --- a/usr.bin/tmux/client-fn.c +++ b/usr.bin/tmux/client-fn.c @@ -1,4 +1,4 @@ -/* $OpenBSD: client-fn.c,v 1.3 2009/07/29 14:17:26 nicm Exp $ */ +/* $OpenBSD: client-fn.c,v 1.4 2009/07/30 16:32:12 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net> @@ -20,6 +20,7 @@ #include <stdlib.h> #include <string.h> +#include <unistd.h> #include "tmux.h" @@ -74,3 +75,23 @@ client_write_server( if (buf != NULL && len > 0) buffer_write(cctx->srv_out, buf, len); } + +void +client_suspend(void) +{ + struct sigaction act; + + memset(&act, 0, sizeof act); + sigemptyset(&act.sa_mask); + act.sa_flags = SA_RESTART; + + act.sa_handler = SIG_DFL; + if (sigaction(SIGTSTP, &act, NULL) != 0) + fatal("sigaction failed"); + + act.sa_handler = sighandler; + if (sigaction(SIGCONT, &act, NULL) != 0) + fatal("sigaction failed"); + + kill(getpid(), SIGTSTP); +} diff --git a/usr.bin/tmux/client-msg.c b/usr.bin/tmux/client-msg.c deleted file mode 100644 index a0c6fdd8ec8..00000000000 --- a/usr.bin/tmux/client-msg.c +++ /dev/null @@ -1,154 +0,0 @@ -/* $OpenBSD: client-msg.c,v 1.6 2009/07/29 14:17:26 nicm Exp $ */ - -/* - * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net> - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER - * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING - * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#include <sys/types.h> - -#include <stdint.h> -#include <stdlib.h> -#include <string.h> -#include <unistd.h> - -#include "tmux.h" - -int client_msg_fn_detach(struct hdr *, struct client_ctx *); -int client_msg_fn_error(struct hdr *, struct client_ctx *); -int client_msg_fn_shutdown(struct hdr *, struct client_ctx *); -int client_msg_fn_exit(struct hdr *, struct client_ctx *); -int client_msg_fn_exited(struct hdr *, struct client_ctx *); -int client_msg_fn_suspend(struct hdr *, struct client_ctx *); - -struct client_msg { - enum msgtype type; - int (*fn)(struct hdr *, struct client_ctx *); -}; -struct client_msg client_msg_table[] = { - { MSG_DETACH, client_msg_fn_detach }, - { MSG_ERROR, client_msg_fn_error }, - { MSG_EXIT, client_msg_fn_exit }, - { MSG_EXITED, client_msg_fn_exited }, - { MSG_SHUTDOWN, client_msg_fn_shutdown }, - { MSG_SUSPEND, client_msg_fn_suspend }, -}; - -int -client_msg_dispatch(struct client_ctx *cctx) -{ - struct hdr hdr; - struct client_msg *msg; - u_int i; - - if (BUFFER_USED(cctx->srv_in) < sizeof hdr) - return (1); - memcpy(&hdr, BUFFER_OUT(cctx->srv_in), sizeof hdr); - if (BUFFER_USED(cctx->srv_in) < (sizeof hdr) + hdr.size) - return (1); - buffer_remove(cctx->srv_in, sizeof hdr); - - for (i = 0; i < nitems(client_msg_table); i++) { - msg = client_msg_table + i; - if (msg->type == hdr.type) - return (msg->fn(&hdr, cctx)); - } - fatalx("unexpected message"); -} - -int -client_msg_fn_error(struct hdr *hdr, struct client_ctx *cctx) -{ - struct msg_print_data data; - - if (hdr->size < sizeof data) - fatalx("bad MSG_PRINT size"); - buffer_read(cctx->srv_in, &data, sizeof data); - - data.msg[(sizeof data.msg) - 1] = '\0'; - cctx->errstr = xstrdup(data.msg); - - return (-1); -} - -int -client_msg_fn_detach(struct hdr *hdr, struct client_ctx *cctx) -{ - if (hdr->size != 0) - fatalx("bad MSG_DETACH size"); - - client_write_server(cctx, MSG_EXITING, NULL, 0); - cctx->exittype = CCTX_DETACH; - - return (0); -} - -int -client_msg_fn_shutdown( - struct hdr *hdr, struct client_ctx *cctx) -{ - if (hdr->size != 0) - fatalx("bad MSG_SHUTDOWN size"); - - client_write_server(cctx, MSG_EXITING, NULL, 0); - cctx->exittype = CCTX_SHUTDOWN; - - return (0); -} - -int -client_msg_fn_exit(struct hdr *hdr, struct client_ctx *cctx) -{ - if (hdr->size != 0) - fatalx("bad MSG_EXIT size"); - - client_write_server(cctx, MSG_EXITING, NULL, 0); - cctx->exittype = CCTX_EXIT; - - return (0); -} - -int -client_msg_fn_exited(struct hdr *hdr, unused struct client_ctx *cctx) -{ - if (hdr->size != 0) - fatalx("bad MSG_EXITED size"); - - return (-1); -} - -int -client_msg_fn_suspend(struct hdr *hdr, unused struct client_ctx *cctx) -{ - struct sigaction act; - - if (hdr->size != 0) - fatalx("bad MSG_SUSPEND size"); - - memset(&act, 0, sizeof act); - sigemptyset(&act.sa_mask); - act.sa_flags = SA_RESTART; - - act.sa_handler = SIG_DFL; - if (sigaction(SIGTSTP, &act, NULL) != 0) - fatal("sigaction failed"); - - act.sa_handler = sighandler; - if (sigaction(SIGCONT, &act, NULL) != 0) - fatal("sigaction failed"); - - kill(getpid(), SIGTSTP); - - return (0); -} 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"); + } + } +} diff --git a/usr.bin/tmux/tmux.h b/usr.bin/tmux/tmux.h index 17c48dc7677..eecb88fa4d5 100644 --- a/usr.bin/tmux/tmux.h +++ b/usr.bin/tmux/tmux.h @@ -1,4 +1,4 @@ -/* $OpenBSD: tmux.h,v 1.65 2009/07/30 13:45:56 nicm Exp $ */ +/* $OpenBSD: tmux.h,v 1.66 2009/07/30 16:32:12 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net> @@ -1338,13 +1338,12 @@ size_t cmd_option_print(struct cmd *, char *, size_t); /* client.c */ int client_init(char *, struct client_ctx *, int, int); int client_main(struct client_ctx *); - -/* client-msg.c */ int client_msg_dispatch(struct client_ctx *); /* client-fn.c */ void client_write_server(struct client_ctx *, enum msgtype, void *, size_t); void client_fill_session(struct msg_command_data *); +void client_suspend(void); /* key-bindings.c */ extern struct key_bindings key_bindings; |