Sync OpenBSD patchset 172:

Tidy client message return slightly: convert flags into an enum, and merge
error string into struct client_ctx as well.
pull/1/head
Tiago Cunha 2009-07-23 23:42:59 +00:00
parent 2e4df706f6
commit 1870b96578
3 changed files with 51 additions and 55 deletions

View File

@ -1,4 +1,4 @@
/* $Id: client-msg.c,v 1.20 2009-07-22 17:38:11 tcunha Exp $ */ /* $Id: client-msg.c,v 1.21 2009-07-23 23:42:59 tcunha Exp $ */
/* /*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net> * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@ -25,16 +25,16 @@
#include "tmux.h" #include "tmux.h"
int client_msg_fn_detach(struct hdr *, struct client_ctx *, char **); int client_msg_fn_detach(struct hdr *, struct client_ctx *);
int client_msg_fn_error(struct hdr *, struct client_ctx *, char **); int client_msg_fn_error(struct hdr *, struct client_ctx *);
int client_msg_fn_shutdown(struct hdr *, struct client_ctx *, char **); int client_msg_fn_shutdown(struct hdr *, struct client_ctx *);
int client_msg_fn_exit(struct hdr *, struct client_ctx *, char **); int client_msg_fn_exit(struct hdr *, struct client_ctx *);
int client_msg_fn_exited(struct hdr *, struct client_ctx *, char **); int client_msg_fn_exited(struct hdr *, struct client_ctx *);
int client_msg_fn_suspend(struct hdr *, struct client_ctx *, char **); int client_msg_fn_suspend(struct hdr *, struct client_ctx *);
struct client_msg { struct client_msg {
enum hdrtype type; enum hdrtype type;
int (*fn)(struct hdr *, struct client_ctx *, char **); int (*fn)(struct hdr *, struct client_ctx *);
}; };
struct client_msg client_msg_table[] = { struct client_msg client_msg_table[] = {
{ MSG_DETACH, client_msg_fn_detach }, { MSG_DETACH, client_msg_fn_detach },
@ -46,7 +46,7 @@ struct client_msg client_msg_table[] = {
}; };
int int
client_msg_dispatch(struct client_ctx *cctx, char **error) client_msg_dispatch(struct client_ctx *cctx)
{ {
struct hdr hdr; struct hdr hdr;
struct client_msg *msg; struct client_msg *msg;
@ -61,70 +61,67 @@ client_msg_dispatch(struct client_ctx *cctx, char **error)
for (i = 0; i < nitems(client_msg_table); i++) { for (i = 0; i < nitems(client_msg_table); i++) {
msg = client_msg_table + i; msg = client_msg_table + i;
if (msg->type == hdr.type) { if (msg->type == hdr.type)
if (msg->fn(&hdr, cctx, error) != 0) return (msg->fn(&hdr, cctx));
return (-1);
return (0);
}
} }
fatalx("unexpected message"); fatalx("unexpected message");
} }
int int
client_msg_fn_error(struct hdr *hdr, struct client_ctx *cctx, char **error) client_msg_fn_error(struct hdr *hdr, struct client_ctx *cctx)
{ {
char *errstr;
if (hdr->size == SIZE_MAX) if (hdr->size == SIZE_MAX)
fatalx("bad MSG_ERROR size"); fatalx("bad MSG_ERROR size");
*error = xmalloc(hdr->size + 1); errstr = xmalloc(hdr->size + 1);
buffer_read(cctx->srv_in, *error, hdr->size); buffer_read(cctx->srv_in, errstr, hdr->size);
(*error)[hdr->size] = '\0'; errstr[hdr->size] = '\0';
cctx->errstr = errstr;
return (-1); return (-1);
} }
int int
client_msg_fn_detach( client_msg_fn_detach(struct hdr *hdr, struct client_ctx *cctx)
struct hdr *hdr, 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");
client_write_server(cctx, MSG_EXITING, NULL, 0); client_write_server(cctx, MSG_EXITING, NULL, 0);
cctx->flags |= CCTX_DETACH; cctx->exittype = CCTX_DETACH;
return (0); return (0);
} }
int int
client_msg_fn_shutdown( client_msg_fn_shutdown(
struct hdr *hdr, struct client_ctx *cctx, unused char **error) struct hdr *hdr, struct client_ctx *cctx)
{ {
if (hdr->size != 0) if (hdr->size != 0)
fatalx("bad MSG_SHUTDOWN size"); fatalx("bad MSG_SHUTDOWN size");
client_write_server(cctx, MSG_EXITING, NULL, 0); client_write_server(cctx, MSG_EXITING, NULL, 0);
cctx->flags |= CCTX_SHUTDOWN; cctx->exittype = CCTX_SHUTDOWN;
return (0); return (0);
} }
int int
client_msg_fn_exit( client_msg_fn_exit(struct hdr *hdr, struct client_ctx *cctx)
struct hdr *hdr, 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");
client_write_server(cctx, MSG_EXITING, NULL, 0); client_write_server(cctx, MSG_EXITING, NULL, 0);
cctx->flags |= CCTX_EXIT; cctx->exittype = CCTX_EXIT;
return (0); return (0);
} }
int int
client_msg_fn_exited( client_msg_fn_exited(struct hdr *hdr, unused struct client_ctx *cctx)
struct hdr *hdr, unused struct client_ctx *cctx, unused char **error)
{ {
if (hdr->size != 0) if (hdr->size != 0)
fatalx("bad MSG_EXITED size"); fatalx("bad MSG_EXITED size");
@ -133,8 +130,7 @@ client_msg_fn_exited(
} }
int int
client_msg_fn_suspend( client_msg_fn_suspend(struct hdr *hdr, unused struct client_ctx *cctx)
struct hdr *hdr, unused struct client_ctx *cctx, unused char **error)
{ {
struct sigaction act; struct sigaction act;

View File

@ -1,4 +1,4 @@
/* $Id: client.c,v 1.54 2009-07-23 13:15:41 tcunha Exp $ */ /* $Id: client.c,v 1.55 2009-07-23 23:42:59 tcunha Exp $ */
/* /*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net> * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@ -159,7 +159,7 @@ client_main(struct client_ctx *cctx)
sigcont = 0; sigcont = 0;
} }
switch (client_msg_dispatch(cctx, &error)) { switch (client_msg_dispatch(cctx)) {
case -1: case -1:
goto out; goto out;
case 0: case 0:
@ -183,8 +183,10 @@ client_main(struct client_ctx *cctx)
fatal("poll failed"); fatal("poll failed");
} }
if (buffer_poll(&pfd, cctx->srv_in, cctx->srv_out) != 0) if (buffer_poll(&pfd, cctx->srv_in, cctx->srv_out) != 0) {
goto server_dead; cctx->exittype = CCTX_DIED;
break;
}
} }
out: out:
@ -192,28 +194,23 @@ out:
printf("[terminated]\n"); printf("[terminated]\n");
return (1); return (1);
} }
switch (cctx->exittype) {
if (cctx->flags & CCTX_SHUTDOWN) { case CCTX_DIED:
printf("[lost server]\n");
return (0);
case CCTX_SHUTDOWN:
printf("[server exited]\n"); printf("[server exited]\n");
return (0); return (0);
} case CCTX_EXIT:
if (cctx->flags & CCTX_EXIT) {
printf("[exited]\n"); printf("[exited]\n");
return (0); return (0);
} case CCTX_DETACH:
if (cctx->flags & CCTX_DETACH) {
printf("[detached]\n"); printf("[detached]\n");
return (0); return (0);
default:
printf("[error: %s]\n", cctx->errstr);
return (1);
} }
printf("[error: %s]\n", error);
return (1);
server_dead:
printf("[lost server]\n");
return (0);
} }
void void

15
tmux.h
View File

@ -1,4 +1,4 @@
/* $Id: tmux.h,v 1.387 2009-07-23 13:25:27 tcunha Exp $ */ /* $Id: tmux.h,v 1.388 2009-07-23 23:42:59 tcunha Exp $ */
/* /*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net> * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@ -844,10 +844,13 @@ struct client_ctx {
struct buffer *srv_in; struct buffer *srv_in;
struct buffer *srv_out; struct buffer *srv_out;
#define CCTX_DETACH 0x1 enum {
#define CCTX_EXIT 0x2 CCTX_DETACH,
#define CCTX_SHUTDOWN 0x4 CCTX_EXIT,
int flags; CCTX_DIED,
CCTX_SHUTDOWN,
} exittype;
const char *errstr;
}; };
/* Key/command line command. */ /* Key/command line command. */
@ -1260,7 +1263,7 @@ int client_init(char *, struct client_ctx *, int, int);
int client_main(struct client_ctx *); int client_main(struct client_ctx *);
/* client-msg.c */ /* client-msg.c */
int client_msg_dispatch(struct client_ctx *, char **); int client_msg_dispatch(struct client_ctx *);
/* client-fn.c */ /* client-fn.c */
void client_write_server(struct client_ctx *, enum hdrtype, void *, size_t); void client_write_server(struct client_ctx *, enum hdrtype, void *, size_t);