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.c | |
parent | 11ee55e755af67dc9155e956b4569c8fdeb11848 (diff) | |
download | rtmux-c7243b73cb3baaf6993d8a9dfb16c054c3978040.tar.gz rtmux-c7243b73cb3baaf6993d8a9dfb16c054c3978040.tar.bz2 rtmux-c7243b73cb3baaf6993d8a9dfb16c054c3978040.zip |
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.c')
-rw-r--r-- | cmd.c | 91 |
1 files changed, 89 insertions, 2 deletions
@@ -1,4 +1,4 @@ -/* $Id: cmd.c,v 1.34 2008-06-01 20:20:25 nicm Exp $ */ +/* $Id: cmd.c,v 1.35 2008-06-02 18:08:16 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net> @@ -20,6 +20,7 @@ #include <getopt.h> #include <string.h> +#include <unistd.h> #include "tmux.h" @@ -105,7 +106,7 @@ cmd_parse(int argc, char **argv, char **cause) cmd = xmalloc(sizeof *cmd); cmd->entry = entry; if (entry->parse != NULL) { - if (entry->parse(&cmd->data, argc, argv, cause) != 0) { + if (entry->parse(cmd, &cmd->data, argc, argv, cause) != 0) { xfree(cmd); return (NULL); } @@ -226,3 +227,89 @@ cmd_recv_string(struct buffer *b) return (s); } + +/* + * Attempt to establish session. This looks first at the command-line argument + * if any, then sees if there is a session in the context, then finally tries + * the session data passed up from the client $TMUX variable. + */ +struct session * +cmd_find_session(struct cmd_ctx *ctx, const char *arg) +{ + struct session *s; + struct msg_command_data *data = ctx->msgdata; + u_int i, n; + + if (arg != NULL) { + if ((s = session_find(arg)) == NULL) { + ctx->error(ctx, "session not found: %s", arg); + return (NULL); + } + return (s); + } + + if (ctx->cursession != NULL) + return (ctx->cursession); + + if (data != NULL && data->pid != -1) { + if (data->pid != getpid()) { + ctx->error(ctx, "wrong server: %lld", data->pid); + return (NULL); + } + if (data->idx > ARRAY_LENGTH(&sessions)) { + ctx->error(ctx, "index out of range: %d", data->idx); + return (NULL); + } + if ((s = ARRAY_ITEM(&sessions, data->idx)) == NULL) { + ctx->error(ctx, "session doesn't exist: %u", data->idx); + return (NULL); + } + return (s); + } + + s = NULL; + n = 0; + for (i = 0; i < ARRAY_LENGTH(&sessions); i++) { + if (ARRAY_ITEM(&sessions, i) != NULL) { + s = ARRAY_ITEM(&sessions, i); + n++; + } + } + if (s == NULL) { + ctx->error(ctx, "no sessions found"); + return (NULL); + } + if (n != 1) { + ctx->error(ctx, "multiple sessions and session not specified"); + return (NULL); + } + return (s); +} + +/* + * Figure out the client. Try the current client (if any) first, then try to + * figure it out from the argument. + */ +struct client * +cmd_find_client(unused struct cmd_ctx *ctx, const char *arg) +{ + struct client *c; + u_int i; + + if (ctx->curclient != NULL) + return (ctx->curclient); + + if (arg == NULL) { + ctx->error(ctx, "must specify a client"); + return (NULL); + } + + for (i = 0; i < ARRAY_LENGTH(&clients); i++) { + c = ARRAY_ITEM(&clients, i); + if (c != NULL && strcmp(arg, c->tty.path) == 0) + return (c); + } + + ctx->error(ctx, "client not found: %s", arg); + return (NULL); +} |