summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNicholas Marriott <nicm@openbsd.org>2009-07-30 16:32:12 +0000
committerNicholas Marriott <nicm@openbsd.org>2009-07-30 16:32:12 +0000
commit5f13bb0c3ade513ec3f88af8b6c0f575fa00cc4b (patch)
treec5951ede730fd867a6b658479574472d8eb1bcfa
parent479d614884f66e9fa11f1292a2ef36991da46c1d (diff)
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@.
-rw-r--r--Makefile2
-rw-r--r--client-fn.c21
-rw-r--r--client-msg.c154
-rw-r--r--client.c81
-rw-r--r--tmux.h3
5 files changed, 88 insertions, 173 deletions
diff --git a/Makefile b/Makefile
index b565c44b..ccd472cf 100644
--- a/Makefile
+++ b/Makefile
@@ -2,7 +2,7 @@
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/client-fn.c b/client-fn.c
index 8e5e3722..129c6871 100644
--- a/client-fn.c
+++ b/client-fn.c
@@ -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/client-msg.c b/client-msg.c
deleted file mode 100644
index f282478b..00000000
--- a/client-msg.c
+++ /dev/null
@@ -1,154 +0,0 @@
-/* $OpenBSD$ */
-
-/*
- * 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/client.c b/client.c
index e8785359..cb2207dd 100644
--- a/client.c
+++ b/client.c
@@ -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/tmux.h b/tmux.h
index 900aa200..62a2dac0 100644
--- a/tmux.h
+++ b/tmux.h
@@ -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;