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:
Nicholas Marriott
2008-06-02 18:08:17 +00:00
parent 11ee55e755
commit c7243b73cb
42 changed files with 1086 additions and 437 deletions

View File

@ -1,4 +1,4 @@
/* $Id: cmd-swap-window.c,v 1.4 2007-12-06 09:46:22 nicm Exp $ */
/* $Id: cmd-swap-window.c,v 1.5 2008-06-02 18:08:16 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@ -27,13 +27,14 @@
* Swap one window with another.
*/
int cmd_swap_window_parse(void **, int, char **, char **);
int cmd_swap_window_parse(struct cmd *, void **, int, char **, char **);
void cmd_swap_window_exec(void *, struct cmd_ctx *);
void cmd_swap_window_send(void *, struct buffer *);
void cmd_swap_window_recv(void **, struct buffer *);
void cmd_swap_window_free(void *);
struct cmd_swap_window_data {
char *sname;
int dstidx;
int srcidx;
char *srcname;
@ -41,8 +42,9 @@ struct cmd_swap_window_data {
};
const struct cmd_entry cmd_swap_window_entry = {
"swap-window", "swapw", "[-i index] name index",
CMD_NOCLIENT,
"swap-window", "swapw",
"[-i index] [-s session-name] session-name index",
0,
cmd_swap_window_parse,
cmd_swap_window_exec,
cmd_swap_window_send,
@ -51,20 +53,25 @@ const struct cmd_entry cmd_swap_window_entry = {
};
int
cmd_swap_window_parse(void **ptr, int argc, char **argv, char **cause)
cmd_swap_window_parse(
struct cmd *self, void **ptr, int argc, char **argv, char **cause)
{
struct cmd_swap_window_data *data;
const char *errstr;
int opt;
*ptr = data = xmalloc(sizeof *data);
data->sname = NULL;
data->flag_detached = 0;
data->dstidx = -1;
data->srcidx = -1;
data->srcname = NULL;
while ((opt = getopt(argc, argv, "di:")) != EOF) {
while ((opt = getopt(argc, argv, "di:s:")) != EOF) {
switch (opt) {
case 'd':
data->flag_detached = 1;
break;
case 'i':
data->dstidx = strtonum(optarg, 0, INT_MAX, &errstr);
if (errstr != NULL) {
@ -72,8 +79,8 @@ cmd_swap_window_parse(void **ptr, int argc, char **argv, char **cause)
goto error;
}
break;
case 'd':
data->flag_detached = 1;
case 's':
data->sname = xstrdup(optarg);
break;
default:
goto usage;
@ -94,8 +101,7 @@ cmd_swap_window_parse(void **ptr, int argc, char **argv, char **cause)
return (0);
usage:
usage(cause, "%s %s",
cmd_swap_window_entry.name, cmd_swap_window_entry.usage);
usage(cause, "%s %s", self->entry->name, self->entry->usage);
error:
cmd_swap_window_free(data);
@ -106,13 +112,16 @@ void
cmd_swap_window_exec(void *ptr, struct cmd_ctx *ctx)
{
struct cmd_swap_window_data *data = ptr;
struct session *dst = ctx->session, *src;
struct session *s, *src;
struct winlink *srcwl, *dstwl;
struct window *w;
if (data == NULL)
return;
if ((s = cmd_find_session(ctx, data->sname)) == NULL)
return;
if ((src = session_find(data->srcname)) == NULL) {
ctx->error(ctx, "session not found: %s", data->srcname);
return;
@ -133,9 +142,9 @@ cmd_swap_window_exec(void *ptr, struct cmd_ctx *ctx)
if (data->dstidx < 0)
data->dstidx = -1;
if (data->dstidx == -1)
dstwl = dst->curw;
dstwl = s->curw;
else {
dstwl = winlink_find_by_index(&dst->windows, data->dstidx);
dstwl = winlink_find_by_index(&s->windows, data->dstidx);
if (dstwl == NULL) {
ctx->error(ctx, "no window %d", data->dstidx);
return;
@ -147,13 +156,13 @@ cmd_swap_window_exec(void *ptr, struct cmd_ctx *ctx)
srcwl->window = w;
if (!data->flag_detached) {
session_select(dst, dstwl->idx);
if (src != dst)
session_select(s, dstwl->idx);
if (src != s)
session_select(src, srcwl->idx);
}
server_redraw_session(src);
if (src != dst)
server_redraw_session(dst);
if (src != s)
server_redraw_session(s);
if (ctx->cmdclient != NULL)
server_write_client(ctx->cmdclient, MSG_EXIT, NULL, 0);
@ -165,6 +174,7 @@ cmd_swap_window_send(void *ptr, struct buffer *b)
struct cmd_swap_window_data *data = ptr;
buffer_write(b, data, sizeof *data);
cmd_send_string(b, data->sname);
cmd_send_string(b, data->srcname);
}
@ -175,6 +185,7 @@ cmd_swap_window_recv(void **ptr, struct buffer *b)
*ptr = data = xmalloc(sizeof *data);
buffer_read(b, data, sizeof *data);
data->sname = cmd_recv_string(b);
data->srcname = cmd_recv_string(b);
}
@ -183,6 +194,8 @@ cmd_swap_window_free(void *ptr)
{
struct cmd_swap_window_data *data = ptr;
if (data->sname != NULL)
xfree(data->sname);
if (data->srcname != NULL)
xfree(data->srcname);
xfree(data);