mirror of
https://github.com/tmate-io/tmate.git
synced 2025-08-15 16:22:49 +02:00
Move -s and -c down a level so handling them is the responsibility of the command (with some helper functions), rather than the top-level. This changes the action command syntax so that -s and -c must be after the command rather than before.
This commit is contained in:
@ -1,4 +1,4 @@
|
||||
/* $Id: cmd-switch-client.c,v 1.2 2007-12-06 09:46:22 nicm Exp $ */
|
||||
/* $Id: cmd-switch-client.c,v 1.3 2008-06-02 18:08:16 nicm Exp $ */
|
||||
|
||||
/*
|
||||
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
|
||||
@ -28,19 +28,21 @@
|
||||
* Switch client to a different session.
|
||||
*/
|
||||
|
||||
int cmd_switch_client_parse(void **, int, char **, char **);
|
||||
int cmd_switch_client_parse(struct cmd *, void **, int, char **, char **);
|
||||
void cmd_switch_client_exec(void *, struct cmd_ctx *);
|
||||
void cmd_switch_client_send(void *, struct buffer *);
|
||||
void cmd_switch_client_recv(void **, struct buffer *);
|
||||
void cmd_switch_client_free(void *);
|
||||
|
||||
struct cmd_switch_client_data {
|
||||
char *cname;
|
||||
char *name;
|
||||
};
|
||||
|
||||
const struct cmd_entry cmd_switch_client_entry = {
|
||||
"switch-client", "switchc", "session-name",
|
||||
CMD_NOSESSION,
|
||||
"switch-client", "switchc",
|
||||
"session-name",
|
||||
0,
|
||||
cmd_switch_client_parse,
|
||||
cmd_switch_client_exec,
|
||||
cmd_switch_client_send,
|
||||
@ -49,16 +51,21 @@ const struct cmd_entry cmd_switch_client_entry = {
|
||||
};
|
||||
|
||||
int
|
||||
cmd_switch_client_parse(void **ptr, int argc, char **argv, char **cause)
|
||||
cmd_switch_client_parse(
|
||||
struct cmd *self, void **ptr, int argc, char **argv, char **cause)
|
||||
{
|
||||
struct cmd_switch_client_data *data;
|
||||
int opt;
|
||||
|
||||
*ptr = data = xmalloc(sizeof *data);
|
||||
data->cname = NULL;
|
||||
data->name = NULL;
|
||||
|
||||
while ((opt = getopt(argc, argv, "")) != EOF) {
|
||||
while ((opt = getopt(argc, argv, "c:")) != EOF) {
|
||||
switch (opt) {
|
||||
case 'c':
|
||||
data->cname = xstrdup(optarg);
|
||||
break;
|
||||
default:
|
||||
goto usage;
|
||||
}
|
||||
@ -73,8 +80,7 @@ cmd_switch_client_parse(void **ptr, int argc, char **argv, char **cause)
|
||||
return (0);
|
||||
|
||||
usage:
|
||||
usage(cause, "%s %s",
|
||||
cmd_switch_client_entry.name, cmd_switch_client_entry.usage);
|
||||
usage(cause, "%s %s", self->entry->name, self->entry->usage);
|
||||
|
||||
cmd_switch_client_free(data);
|
||||
return (-1);
|
||||
@ -84,20 +90,23 @@ void
|
||||
cmd_switch_client_exec(void *ptr, struct cmd_ctx *ctx)
|
||||
{
|
||||
struct cmd_switch_client_data *data = ptr;
|
||||
struct client *c;
|
||||
struct session *s;
|
||||
|
||||
if (data == NULL)
|
||||
return;
|
||||
|
||||
if ((c = cmd_find_client(ctx, data->cname)) == NULL)
|
||||
return;
|
||||
|
||||
if ((s = session_find(data->name)) == NULL) {
|
||||
ctx->error(ctx, "session not found: %s", data->name);
|
||||
return;
|
||||
}
|
||||
|
||||
ctx->client->session = s;
|
||||
c->session = s;
|
||||
|
||||
recalculate_sizes();
|
||||
server_redraw_client(ctx->client);
|
||||
server_redraw_client(c);
|
||||
|
||||
if (ctx->cmdclient != NULL)
|
||||
server_write_client(ctx->cmdclient, MSG_EXIT, NULL, 0);
|
||||
@ -109,6 +118,7 @@ cmd_switch_client_send(void *ptr, struct buffer *b)
|
||||
struct cmd_switch_client_data *data = ptr;
|
||||
|
||||
buffer_write(b, data, sizeof *data);
|
||||
cmd_send_string(b, data->cname);
|
||||
cmd_send_string(b, data->name);
|
||||
}
|
||||
|
||||
@ -119,6 +129,7 @@ cmd_switch_client_recv(void **ptr, struct buffer *b)
|
||||
|
||||
*ptr = data = xmalloc(sizeof *data);
|
||||
buffer_read(b, data, sizeof *data);
|
||||
data->cname = cmd_recv_string(b);
|
||||
data->name = cmd_recv_string(b);
|
||||
}
|
||||
|
||||
@ -127,6 +138,8 @@ cmd_switch_client_free(void *ptr)
|
||||
{
|
||||
struct cmd_switch_client_data *data = ptr;
|
||||
|
||||
if (data->cname != NULL)
|
||||
xfree(data->cname);
|
||||
if (data->name != NULL)
|
||||
xfree(data->name);
|
||||
xfree(data);
|
||||
|
Reference in New Issue
Block a user