Join oldest session if non specified. Fix errors.

This commit is contained in:
Nicholas Marriott 2007-09-26 18:50:49 +00:00
parent 8d01984182
commit 5ef6d077c6
5 changed files with 73 additions and 43 deletions

View File

@ -1,4 +1,4 @@
/* $Id: client-cmd.c,v 1.2 2007-09-26 18:09:23 nicm Exp $ */ /* $Id: client-cmd.c,v 1.3 2007-09-26 18:50:49 nicm Exp $ */
/* /*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net> * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@ -22,13 +22,13 @@
int client_cmd_prefix = META; int client_cmd_prefix = META;
int client_cmd_fn_select(int, struct client_ctx *, const char **); int client_cmd_fn_select(int, struct client_ctx *, char **);
int client_cmd_fn_detach(int, struct client_ctx *, const char **); int client_cmd_fn_detach(int, struct client_ctx *, char **);
int client_cmd_fn_msg(int, struct client_ctx *, const char **); int client_cmd_fn_msg(int, struct client_ctx *, char **);
struct cmd { struct cmd {
int key; int key;
int (*fn)(int, struct client_ctx *, const char **); int (*fn)(int, struct client_ctx *, char **);
int arg; int arg;
}; };
@ -63,7 +63,7 @@ struct cmd client_cmd_table[] = {
#define NCLIENTCMD (sizeof client_cmd_table / sizeof client_cmd_table[0]) #define NCLIENTCMD (sizeof client_cmd_table / sizeof client_cmd_table[0])
int int
client_cmd_dispatch(int key, struct client_ctx *cctx, const char **error) client_cmd_dispatch(int key, struct client_ctx *cctx, char **error)
{ {
struct cmd *cmd; struct cmd *cmd;
u_int i; u_int i;
@ -78,7 +78,7 @@ client_cmd_dispatch(int key, struct client_ctx *cctx, const char **error)
/* Handle generic command. */ /* Handle generic command. */
int int
client_cmd_fn_msg(int arg, struct client_ctx *cctx, unused const char **error) client_cmd_fn_msg(int arg, struct client_ctx *cctx, unused char **error)
{ {
client_write_server(cctx, arg, NULL, 0); client_write_server(cctx, arg, NULL, 0);
@ -87,8 +87,7 @@ client_cmd_fn_msg(int arg, struct client_ctx *cctx, unused const char **error)
/* Handle select command. */ /* Handle select command. */
int int
client_cmd_fn_select( client_cmd_fn_select(int arg, struct client_ctx *cctx, unused char **error)
int arg, struct client_ctx *cctx, unused const char **error)
{ {
struct select_data data; struct select_data data;
@ -101,7 +100,7 @@ client_cmd_fn_select(
/* Handle detach command. */ /* Handle detach command. */
int int
client_cmd_fn_detach( client_cmd_fn_detach(
unused int arg, unused struct client_ctx *cctx, unused const char **error) unused int arg, unused struct client_ctx *cctx, unused char **error)
{ {
return (-1); return (-1);
} }

View File

@ -1,4 +1,4 @@
/* $Id: client-msg.c,v 1.2 2007-09-26 18:09:23 nicm Exp $ */ /* $Id: client-msg.c,v 1.3 2007-09-26 18:50:49 nicm Exp $ */
/* /*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net> * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@ -24,24 +24,26 @@
#include "tmux.h" #include "tmux.h"
int client_msg_fn_output(struct hdr *, struct client_ctx *, const char **); int client_msg_fn_output(struct hdr *, struct client_ctx *, char **);
int client_msg_fn_pause(struct hdr *, struct client_ctx *, const char **); int client_msg_fn_pause(struct hdr *, struct client_ctx *, char **);
int client_msg_fn_exit(struct hdr *, struct client_ctx *, const char **); int client_msg_fn_exit(struct hdr *, struct client_ctx *, char **);
int client_msg_fn_error(struct hdr *, struct client_ctx *, char **);
struct client_msg { struct client_msg {
enum hdrtype type; enum hdrtype type;
int (*fn)(struct hdr *, struct client_ctx *, const char **); int (*fn)(struct hdr *, struct client_ctx *, char **);
}; };
struct client_msg client_msg_table[] = { struct client_msg client_msg_table[] = {
{ MSG_OUTPUT, client_msg_fn_output }, { MSG_OUTPUT, client_msg_fn_output },
{ MSG_PAUSE, client_msg_fn_pause }, { MSG_PAUSE, client_msg_fn_pause },
{ MSG_EXIT, client_msg_fn_exit }, { MSG_EXIT, client_msg_fn_exit },
{ MSG_ERROR, client_msg_fn_error },
}; };
#define NCLIENTMSG (sizeof client_msg_table / sizeof client_msg_table[0]) #define NCLIENTMSG (sizeof client_msg_table / sizeof client_msg_table[0])
int int
client_msg_dispatch(struct client_ctx *cctx, const char **error) client_msg_dispatch(struct client_ctx *cctx, char **error)
{ {
struct hdr hdr; struct hdr hdr;
struct client_msg *msg; struct client_msg *msg;
@ -72,7 +74,7 @@ client_msg_dispatch(struct client_ctx *cctx, const char **error)
/* Output message from server. */ /* Output message from server. */
int int
client_msg_fn_output( client_msg_fn_output(
struct hdr *hdr, struct client_ctx *cctx, unused const char **error) struct hdr *hdr, struct client_ctx *cctx, unused char **error)
{ {
local_output(cctx->srv_in, hdr->size); local_output(cctx->srv_in, hdr->size);
return (0); return (0);
@ -81,7 +83,7 @@ client_msg_fn_output(
/* Pause message from server. */ /* Pause message from server. */
int int
client_msg_fn_pause( client_msg_fn_pause(
struct hdr *hdr, unused struct client_ctx *cctx, unused const char **error) struct hdr *hdr, unused struct client_ctx *cctx, unused char **error)
{ {
if (hdr->size != 0) if (hdr->size != 0)
fatalx("bad MSG_PAUSE size"); fatalx("bad MSG_PAUSE size");
@ -91,9 +93,23 @@ client_msg_fn_pause(
/* Exit message from server. */ /* Exit message from server. */
int int
client_msg_fn_exit( client_msg_fn_exit(
struct hdr *hdr, unused struct client_ctx *cctx, unused const 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");
return (-1); return (-1);
} }
/* Error message from server. */
int
client_msg_fn_error(struct hdr *hdr, struct client_ctx *cctx, char **error)
{
if (hdr->size > SIZE_MAX - 1)
fatalx("bad MSG_ERROR size");
*error = xmalloc(hdr->size + 1);
buffer_read(cctx->srv_in, *error, hdr->size);
(*error)[hdr->size] = '\0';
return (-1);
}

View File

@ -1,4 +1,4 @@
/* $Id: client.c,v 1.3 2007-09-26 18:32:16 nicm Exp $ */ /* $Id: client.c,v 1.4 2007-09-26 18:50:49 nicm Exp $ */
/* /*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net> * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@ -33,7 +33,7 @@
#include "tmux.h" #include "tmux.h"
void client_handle_winch(struct client_ctx *); void client_handle_winch(struct client_ctx *);
int client_process_local(struct client_ctx *, const char **); int client_process_local(struct client_ctx *, char **);
int int
client_init(char *path, struct client_ctx *cctx, int start_server) client_init(char *path, struct client_ctx *cctx, int start_server)
@ -126,7 +126,7 @@ int
client_main(struct client_ctx *cctx) client_main(struct client_ctx *cctx)
{ {
struct pollfd pfds[2]; struct pollfd pfds[2];
const char *error; char *error;
int n; int n;
logfile("client"); logfile("client");
@ -174,11 +174,13 @@ client_main(struct client_ctx *cctx)
local_done(); local_done();
if (sigterm)
error = "received SIGTERM";
if (error != NULL) { if (error != NULL) {
printf("[terminated: %s]\n", error); printf("[error: %s]\n", error);
return (0); return (1);
}
if (sigterm) {
printf("[terminated]\n");
return (1);
} }
printf("[detached]\n"); printf("[detached]\n");
return (0); return (0);
@ -223,7 +225,7 @@ client_handle_winch(struct client_ctx *cctx)
} }
int int
client_process_local(struct client_ctx *cctx, const char **error) client_process_local(struct client_ctx *cctx, char **error)
{ {
struct buffer *b; struct buffer *b;
size_t size; size_t size;

View File

@ -1,4 +1,4 @@
/* $Id: server-msg.c,v 1.3 2007-09-26 18:09:23 nicm Exp $ */ /* $Id: server-msg.c,v 1.4 2007-09-26 18:50:49 nicm Exp $ */
/* /*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net> * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@ -139,6 +139,8 @@ server_msg_fn_attach(struct hdr *hdr, struct client *c)
{ {
struct attach_data data; struct attach_data data;
char *msg; char *msg;
struct session *s;
u_int i;
if (c->session != NULL) if (c->session != NULL)
return (0); return (0);
@ -153,10 +155,21 @@ server_msg_fn_attach(struct hdr *hdr, struct client *c)
if (c->sy == 0) if (c->sy == 0)
c->sy = 25; c->sy = 25;
if (*data.name != '\0') if (*data.name != '\0') {
c->session = session_find(data.name); if ((c->session = session_find(data.name)) == NULL)
xasprintf(&msg, "session not found: %s", data.name);
} else {
/* Find the oldest session. */
for (i = 0; i < ARRAY_LENGTH(&sessions); i++) {
if ((s = ARRAY_ITEM(&sessions, i)) == NULL)
continue;
if (c->session == NULL || s->tim < c->session->tim)
c->session = s;
}
if (c->session == NULL)
xasprintf(&msg, "no sessions found");
}
if (c->session == NULL) { if (c->session == NULL) {
xasprintf(&msg, "session not found: %s", data.name);
server_write_client(c, MSG_ERROR, msg, strlen(msg)); server_write_client(c, MSG_ERROR, msg, strlen(msg));
xfree(msg); xfree(msg);
return (0); return (0);

24
tmux.h
View File

@ -1,4 +1,4 @@
/* $Id: tmux.h,v 1.14 2007-09-26 18:32:17 nicm Exp $ */ /* $Id: tmux.h,v 1.15 2007-09-26 18:50:49 nicm Exp $ */
/* /*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net> * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@ -259,24 +259,24 @@ struct buffer {
/* Message codes. */ /* Message codes. */
enum hdrtype { enum hdrtype {
MSG_NEW = 0,
MSG_ATTACH, MSG_ATTACH,
MSG_ERROR,
MSG_CREATE, MSG_CREATE,
MSG_ERROR,
MSG_EXIT, MSG_EXIT,
MSG_SIZE,
MSG_NEXT,
MSG_PREVIOUS,
MSG_INPUT, MSG_INPUT,
MSG_LAST,
MSG_NEW,
MSG_NEXT,
MSG_OUTPUT, MSG_OUTPUT,
MSG_PAUSE,
MSG_PREVIOUS,
MSG_REFRESH, MSG_REFRESH,
MSG_RENAME,
MSG_SELECT, MSG_SELECT,
MSG_SESSIONS, MSG_SESSIONS,
MSG_SIZE,
MSG_WINDOWLIST,
MSG_WINDOWS, MSG_WINDOWS,
MSG_PAUSE,
MSG_RENAME,
MSG_LAST,
MSG_WINDOWLIST
}; };
/* Message header structure. */ /* Message header structure. */
@ -451,11 +451,11 @@ int client_main(struct client_ctx *);
void client_write_server(struct client_ctx *, enum hdrtype, void *, size_t); void client_write_server(struct client_ctx *, enum hdrtype, void *, size_t);
/* client-msg.c */ /* client-msg.c */
int client_msg_dispatch(struct client_ctx *, const char **); int client_msg_dispatch(struct client_ctx *, char **);
/* command.c */ /* command.c */
extern int client_cmd_prefix; extern int client_cmd_prefix;
int client_cmd_dispatch(int, struct client_ctx *, const char **); int client_cmd_dispatch(int, struct client_ctx *, char **);
/* server.c */ /* server.c */
extern struct clients clients; extern struct clients clients;