Clean up pause/exit mess with flags.

This commit is contained in:
Nicholas Marriott 2007-10-05 14:23:28 +00:00
parent fa65aec2ef
commit 91e24af089
4 changed files with 74 additions and 52 deletions

3
TODO
View File

@ -44,6 +44,8 @@
-- For 0.1 -------------------------------------------------------------------- -- For 0.1 --------------------------------------------------------------------
- man page - man page
- sort out bell: passing through should be optional, and toolbar should change
window colour or something
- commands: - commands:
refresh client refresh client
rename sessions rename sessions
@ -56,4 +58,3 @@
set shell set shell
- handle tmux in tmux (check $TMUX and abort) - handle tmux in tmux (check $TMUX and abort)
- check for some reqd terminfo caps on startup - check for some reqd terminfo caps on startup
- sort out the pause mess

View File

@ -1,4 +1,4 @@
/* $Id: client-msg.c,v 1.7 2007-10-04 11:52:02 nicm Exp $ */ /* $Id: client-msg.c,v 1.8 2007-10-05 14:23:28 nicm Exp $ */
/* /*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net> * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@ -51,27 +51,24 @@ client_msg_dispatch(struct client_ctx *cctx, char **error)
struct hdr hdr; struct hdr hdr;
struct client_msg *msg; struct client_msg *msg;
u_int i; u_int i;
int n;
for (;;) { if (BUFFER_USED(cctx->srv_in) < sizeof hdr)
if (BUFFER_USED(cctx->srv_in) < sizeof hdr) return (1);
return (0); memcpy(&hdr, BUFFER_OUT(cctx->srv_in), sizeof hdr);
memcpy(&hdr, BUFFER_OUT(cctx->srv_in), sizeof hdr); if (BUFFER_USED(cctx->srv_in) < (sizeof hdr) + hdr.size)
if (BUFFER_USED(cctx->srv_in) < (sizeof hdr) + hdr.size) return (1);
return (0); buffer_remove(cctx->srv_in, sizeof hdr);
buffer_remove(cctx->srv_in, sizeof hdr);
for (i = 0; i < NCLIENTMSG; i++) { for (i = 0; i < NCLIENTMSG; i++) {
msg = client_msg_table + i; msg = client_msg_table + i;
if (msg->type == hdr.type) { if (msg->type == hdr.type) {
if ((n = msg->fn(&hdr, cctx, error)) != 0) if (msg->fn(&hdr, cctx, error) != 0)
return (n); return (-1);
break; return (0);
}
} }
if (i == NCLIENTMSG)
fatalx("unexpected message");
} }
if (i == NCLIENTMSG)
fatalx("unexpected message");
} }
int int
@ -88,7 +85,10 @@ client_msg_fn_pause(
{ {
if (hdr->size != 0) if (hdr->size != 0)
fatalx("bad MSG_PAUSE size"); fatalx("bad MSG_PAUSE size");
return (1);
cctx->flags |= CCTX_PAUSE;
return (0);
} }
int int
@ -106,24 +106,24 @@ client_msg_fn_error(struct hdr *hdr, struct client_ctx *cctx, char **error)
int int
client_msg_fn_exit( client_msg_fn_exit(
struct hdr *hdr, unused struct client_ctx *cctx, char **error) struct hdr *hdr, unused struct client_ctx *cctx, unused char **error)
{ {
if (hdr->size != 0) if (hdr->size != 0)
fatalx("bad MSG_EXIT size"); fatalx("bad MSG_EXIT size");
*error = xstrdup(""); cctx->flags |= CCTX_EXIT;
return (-1); return (-1);
} }
int int
client_msg_fn_detach( client_msg_fn_detach(
struct hdr *hdr, unused struct client_ctx *cctx, char **error) struct hdr *hdr, unused struct client_ctx *cctx, unused char **error)
{ {
if (hdr->size != 0) if (hdr->size != 0)
fatalx("bad MSG_DETACH size"); fatalx("bad MSG_DETACH size");
*error = NULL; cctx->flags |= CCTX_DETACH;
return (-1); return (-1);
} }

View File

@ -1,4 +1,4 @@
/* $Id: client.c,v 1.13 2007-10-04 19:03:51 nicm Exp $ */ /* $Id: client.c,v 1.14 2007-10-05 14:23:28 nicm Exp $ */
/* /*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net> * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@ -126,7 +126,7 @@ client_main(struct client_ctx *cctx)
{ {
struct pollfd pfds[2]; struct pollfd pfds[2];
char *error; char *error;
int n; int timeout;
logfile("client"); logfile("client");
setproctitle("client"); setproctitle("client");
@ -135,8 +135,8 @@ client_main(struct client_ctx *cctx)
if ((cctx->loc_fd = local_init(&cctx->loc_in, &cctx->loc_out)) == -1) if ((cctx->loc_fd = local_init(&cctx->loc_in, &cctx->loc_out)) == -1)
return (1); return (1);
n = 0;
error = NULL; error = NULL;
timeout = INFTIM;
while (!sigterm) { while (!sigterm) {
if (sigwinch) if (sigwinch)
client_handle_winch(cctx); client_handle_winch(cctx);
@ -150,7 +150,7 @@ client_main(struct client_ctx *cctx)
if (BUFFER_USED(cctx->loc_out) > 0) if (BUFFER_USED(cctx->loc_out) > 0)
pfds[1].events |= POLLOUT; pfds[1].events |= POLLOUT;
if (poll(pfds, 2, INFTIM) == -1) { if (poll(pfds, 2, timeout) == -1) {
if (errno == EAGAIN || errno == EINTR) if (errno == EAGAIN || errno == EINTR)
continue; continue;
fatal("poll failed"); fatal("poll failed");
@ -161,32 +161,48 @@ client_main(struct client_ctx *cctx)
if (buffer_poll(&pfds[1], cctx->loc_in, cctx->loc_out) != 0) if (buffer_poll(&pfds[1], cctx->loc_in, cctx->loc_out) != 0)
goto local_dead; goto local_dead;
/* XXX Output flushed; pause if required. */ if (cctx->flags & CCTX_PAUSE) {
if (n)
usleep(750000); usleep(750000);
/* XXX XXX special return code for pause? or flag in cctx? */ cctx->flags = 0;
if ((n = client_process_local(cctx, &error)) == -1) }
break;
if ((n = client_msg_dispatch(cctx, &error)) == -1) if (client_process_local(cctx, &error) == -1)
break; goto out;
}
switch (client_msg_dispatch(cctx, &error)) {
local_done(); case -1:
goto out;
if (error != NULL) { case 0:
if (*error == '\0') { /* May be more in buffer, don't let poll block. */
printf("[exited]\n"); timeout = 0;
return (0); break;
default:
/* Out of data, poll may block. */
timeout = INFTIM;
break;
} }
printf("[error: %s]\n", error);
return (1);
} }
if (sigterm) {
printf("[terminated]\n"); out:
return (1); local_done();
if (sigterm) {
printf("[terminated]\n");
return (1);
}
if (cctx->flags & CCTX_EXIT) {
printf("[exited]\n");
return (0);
} }
printf("[detached]\n");
return (0); if (cctx->flags & CCTX_DETACH) {
printf("[detached]\n");
return (0);
}
printf("[error: %s]\n", error);
return (1);
server_dead: server_dead:
local_done(); local_done();

7
tmux.h
View File

@ -1,4 +1,4 @@
/* $Id: tmux.h,v 1.50 2007-10-04 22:04:01 nicm Exp $ */ /* $Id: tmux.h,v 1.51 2007-10-05 14:23:28 nicm Exp $ */
/* /*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net> * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@ -453,6 +453,11 @@ struct client_ctx {
int loc_fd; int loc_fd;
struct buffer *loc_in; struct buffer *loc_in;
struct buffer *loc_out; struct buffer *loc_out;
#define CCTX_PAUSE 0x1
#define CCTX_DETACH 0x2
#define CCTX_EXIT 0x4
int flags;
}; };
/* Key/command line command. */ /* Key/command line command. */