summaryrefslogtreecommitdiffstats
path: root/cmd.c
diff options
context:
space:
mode:
authorNicholas Marriott <nicm@openbsd.org>2009-10-05 18:30:54 +0000
committerNicholas Marriott <nicm@openbsd.org>2009-10-05 18:30:54 +0000
commit4ca2200d838732c129c6dd5c11f92245346a1e28 (patch)
tree44096997d5b3b2d20ef12f24f37a627a99a5e747 /cmd.c
parent45043ebf3ecabb8847d404baa7b3e9d66696f667 (diff)
If no target client is specified to commands which accept one, try to guess the
current client, in a similar manner to how sessions already work: if the current session can be established and has only one client, use that; otherwise use the most recently created client.
Diffstat (limited to 'cmd.c')
-rw-r--r--cmd.c51
1 files changed, 49 insertions, 2 deletions
diff --git a/cmd.c b/cmd.c
index 05294be1..17cf0c16 100644
--- a/cmd.c
+++ b/cmd.c
@@ -110,6 +110,7 @@ const struct cmd_entry *cmd_table[] = {
};
struct session *cmd_newest_session(struct sessions *);
+struct client *cmd_newest_client(void);
struct client *cmd_lookup_client(const char *);
struct session *cmd_lookup_session(const char *, int *);
struct winlink *cmd_lookup_window(struct session *, const char *, int *);
@@ -369,17 +370,63 @@ cmd_newest_session(struct sessions *ss)
return (snewest);
}
+/* Find the newest client. */
+struct client *
+cmd_newest_client(void)
+{
+ struct client *c, *cnewest;
+ struct timeval *tv = NULL;
+ u_int i;
+
+ cnewest = NULL;
+ for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
+ if ((c = ARRAY_ITEM(&clients, i)) == NULL)
+ continue;
+ if (c->session == NULL)
+ continue;
+
+ if (tv == NULL || timercmp(&c->tv, tv, >)) {
+ cnewest = c;
+ tv = &c->tv;
+ }
+ }
+
+ return (cnewest);
+}
+
/* Find the target client or report an error and return NULL. */
struct client *
cmd_find_client(struct cmd_ctx *ctx, const char *arg)
{
struct client *c;
+ struct session *s;
char *tmparg;
size_t arglen;
+ u_int i;
/* A NULL argument means the current client. */
- if (arg == NULL)
- return (ctx->curclient);
+ if (arg == NULL) {
+ if (ctx->curclient != NULL)
+ return (ctx->curclient);
+ /*
+ * No current client set. Find the current session and see if
+ * it has only has one client.
+ */
+ s = cmd_current_session(ctx);
+ if (s != NULL) {
+ c = NULL;
+ for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
+ if (ARRAY_ITEM(&clients, i)->session == s) {
+ if (c != NULL)
+ break;
+ c = ARRAY_ITEM(&clients, i);
+ }
+ }
+ if (i == ARRAY_LENGTH(&clients) && c != NULL)
+ return (c);
+ }
+ return (cmd_newest_client());
+ }
tmparg = xstrdup(arg);
/* Trim a single trailing colon if any. */