diff options
author | Nicholas Marriott <nicholas.marriott@gmail.com> | 2008-06-02 18:08:17 +0000 |
---|---|---|
committer | Nicholas Marriott <nicholas.marriott@gmail.com> | 2008-06-02 18:08:17 +0000 |
commit | c7243b73cb3baaf6993d8a9dfb16c054c3978040 (patch) | |
tree | 1d67ee4c5cf764dc83245c79d27da52857b4a89d /cmd-link-window.c | |
parent | 11ee55e755af67dc9155e956b4569c8fdeb11848 (diff) |
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.
Diffstat (limited to 'cmd-link-window.c')
-rw-r--r-- | cmd-link-window.c | 59 |
1 files changed, 36 insertions, 23 deletions
diff --git a/cmd-link-window.c b/cmd-link-window.c index 45cb4876..72b2e03b 100644 --- a/cmd-link-window.c +++ b/cmd-link-window.c @@ -1,4 +1,4 @@ -/* $Id: cmd-link-window.c,v 1.10 2008-06-01 20:32:41 nicm Exp $ */ +/* $Id: cmd-link-window.c,v 1.11 2008-06-02 18:08:16 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net> @@ -27,13 +27,14 @@ * Link a window into another session. */ -int cmd_link_window_parse(void **, int, char **, char **); +int cmd_link_window_parse(struct cmd *, void **, int, char **, char **); void cmd_link_window_exec(void *, struct cmd_ctx *); void cmd_link_window_send(void *, struct buffer *); void cmd_link_window_recv(void **, struct buffer *); void cmd_link_window_free(void *); struct cmd_link_window_data { + char *sname; int flag_detached; int flag_kill; int dstidx; @@ -42,8 +43,9 @@ struct cmd_link_window_data { }; const struct cmd_entry cmd_link_window_entry = { - "link-window", "linkw", "[-dk] [-i index] name index", - CMD_NOCLIENT, + "link-window", "linkw", + "[-dk] [-s session-name] [-i index] session-name index", + 0, cmd_link_window_parse, cmd_link_window_exec, cmd_link_window_send, @@ -52,21 +54,26 @@ const struct cmd_entry cmd_link_window_entry = { }; int -cmd_link_window_parse(void **ptr, int argc, char **argv, char **cause) +cmd_link_window_parse( + struct cmd *self, void **ptr, int argc, char **argv, char **cause) { struct cmd_link_window_data *data; const char *errstr; int opt; *ptr = data = xmalloc(sizeof *data); + data->sname = NULL; data->flag_detached = 0; data->flag_kill = 0; data->dstidx = -1; data->srcidx = -1; data->srcname = NULL; - while ((opt = getopt(argc, argv, "dki:")) != EOF) { + while ((opt = getopt(argc, argv, "di:ks:")) != EOF) { switch (opt) { + case 'd': + data->flag_detached = 1; + break; case 'i': data->dstidx = strtonum(optarg, 0, INT_MAX, &errstr); if (errstr != NULL) { @@ -74,12 +81,12 @@ cmd_link_window_parse(void **ptr, int argc, char **argv, char **cause) goto error; } break; - case 'd': - data->flag_detached = 1; - break; case 'k': data->flag_kill = 1; break; + case 's': + data->sname = xstrdup(optarg); + break; default: goto usage; } @@ -99,8 +106,7 @@ cmd_link_window_parse(void **ptr, int argc, char **argv, char **cause) return (0); usage: - usage(cause, "%s %s", - cmd_link_window_entry.name, cmd_link_window_entry.usage); + usage(cause, "%s %s", self->entry->name, self->entry->usage); error: cmd_link_window_free(data); @@ -111,12 +117,15 @@ void cmd_link_window_exec(void *ptr, struct cmd_ctx *ctx) { struct cmd_link_window_data *data = ptr; - struct session *dst = ctx->session, *src; + struct session *s, *src; struct winlink *wl, *wl2; 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; @@ -137,7 +146,7 @@ cmd_link_window_exec(void *ptr, struct cmd_ctx *ctx) if (data->dstidx < 0) data->dstidx = -1; if (data->flag_kill && data->dstidx != -1) { - wl2 = winlink_find_by_index(&dst->windows, data->dstidx); + wl2 = winlink_find_by_index(&s->windows, data->dstidx); if (wl2 == NULL) { ctx->error(ctx, "no window %d", data->dstidx); return; @@ -147,16 +156,16 @@ cmd_link_window_exec(void *ptr, struct cmd_ctx *ctx) * Can't use session_detach as it will destroy session if this * makes it empty. */ - session_cancelbell(dst, wl2); - winlink_remove(&dst->windows, wl2); + session_cancelbell(s, wl2); + winlink_remove(&s->windows, wl2); /* Force select/redraw if current. */ - if (wl2 == dst->curw) { + if (wl2 == s->curw) { data->flag_detached = 0; - dst->curw = NULL; + s->curw = NULL; } - if (wl2 == dst->lastw) - dst->lastw = NULL; + if (wl2 == s->lastw) + s->lastw = NULL; /* * Can't error out after this or there could be an empty @@ -164,17 +173,17 @@ cmd_link_window_exec(void *ptr, struct cmd_ctx *ctx) */ } - wl = session_attach(dst, wl->window, data->dstidx); + wl = session_attach(s, wl->window, data->dstidx); if (wl == NULL) { ctx->error(ctx, "index in use: %d", data->dstidx); return; } if (!data->flag_detached) { - session_select(dst, wl->idx); - server_redraw_session(dst); + session_select(s, wl->idx); + server_redraw_session(s); } else - server_status_session(dst); + server_status_session(s); if (ctx->cmdclient != NULL) server_write_client(ctx->cmdclient, MSG_EXIT, NULL, 0); @@ -186,6 +195,7 @@ cmd_link_window_send(void *ptr, struct buffer *b) struct cmd_link_window_data *data = ptr; buffer_write(b, data, sizeof *data); + cmd_send_string(b, data->sname); cmd_send_string(b, data->srcname); } @@ -196,6 +206,7 @@ cmd_link_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); } @@ -204,6 +215,8 @@ cmd_link_window_free(void *ptr) { struct cmd_link_window_data *data = ptr; + if (data->sname != NULL) + xfree(data->sname); if (data->srcname != NULL) xfree(data->srcname); xfree(data); |