summaryrefslogtreecommitdiff
path: root/usr.bin
diff options
context:
space:
mode:
authorNicholas Marriott <nicm@cvs.openbsd.org>2009-07-29 17:03:17 +0000
committerNicholas Marriott <nicm@cvs.openbsd.org>2009-07-29 17:03:17 +0000
commit1276d43f91ce6685cf1ae5e8d0ffb06f86c5e098 (patch)
treefd0872bdcf8fa382450c09317eae488717949ccb /usr.bin
parent9297d1c2ad086d16e7afa160d80765c36d6264f4 (diff)
Add an additional heuristic to work out the current session when run from the
command line. The name of all slave ptys in the server is known, so if the client was run on a tty, look for any sessions containing that tty and use the most recently created. This is more reliable than looking at $TMUX if windows have been moved or linked between sessions.
Diffstat (limited to 'usr.bin')
-rw-r--r--usr.bin/tmux/cmd.c55
1 files changed, 47 insertions, 8 deletions
diff --git a/usr.bin/tmux/cmd.c b/usr.bin/tmux/cmd.c
index c63d2e01a19..34de0f97ab6 100644
--- a/usr.bin/tmux/cmd.c
+++ b/usr.bin/tmux/cmd.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: cmd.c,v 1.10 2009/07/26 12:58:44 nicm Exp $ */
+/* $OpenBSD: cmd.c,v 1.11 2009/07/29 17:03:16 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@@ -103,7 +103,7 @@ const struct cmd_entry *cmd_table[] = {
NULL
};
-struct session *cmd_newest_session(void);
+struct session *cmd_newest_session(struct sessions *);
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 *);
@@ -281,18 +281,57 @@ cmd_print(struct cmd *cmd, char *buf, size_t len)
/*
* Figure out the current session. Use: 1) the current session, if the command
- * context has one; 2) the session specified in the TMUX variable from the
- * environment (as passed from the client); 3) the newest session.
+ * context has one; 2) the session containing the pty of the calling client, if
+ * any 3) the session specified in the TMUX variable from the environment (as
+ * passed from the client); 3) the newest session.
*/
struct session *
cmd_current_session(struct cmd_ctx *ctx)
{
struct msg_command_data *data = ctx->msgdata;
+ struct client *c = ctx->cmdclient;
struct session *s;
+ struct sessions ss;
+ struct winlink *wl;
+ struct window_pane *wp;
+ u_int i;
+ int found;
if (ctx->cursession != NULL)
return (ctx->cursession);
+ /*
+ * If the name of the calling client's pty is know, build a list of the
+ * sessions that contain it and if any choose either the first or the
+ * newest.
+ */
+ if (c != NULL && c->tty.path != NULL) {
+ ARRAY_INIT(&ss);
+ for (i = 0; i < ARRAY_LENGTH(&sessions); i++) {
+ if ((s = ARRAY_ITEM(&sessions, i)) == NULL)
+ continue;
+ found = 0;
+ RB_FOREACH(wl, winlinks, &s->windows) {
+ TAILQ_FOREACH(wp, &wl->window->panes, entry) {
+ if (strcmp(wp->tty, c->tty.path) == 0) {
+ found = 1;
+ break;
+ }
+ }
+ if (found)
+ break;
+ }
+ if (found)
+ ARRAY_ADD(&ss, s);
+ }
+
+ s = cmd_newest_session(&ss);
+ ARRAY_FREE(&ss);
+ if (s != NULL)
+ return (s);
+ }
+
+ /* Use the session from the TMUX environment variable. */
if (data != NULL && data->pid != -1) {
if (data->pid != getpid())
return (NULL);
@@ -303,20 +342,20 @@ cmd_current_session(struct cmd_ctx *ctx)
return (s);
}
- return (cmd_newest_session());
+ return (cmd_newest_session(&sessions));
}
/* Find the newest session. */
struct session *
-cmd_newest_session(void)
+cmd_newest_session(struct sessions *ss)
{
struct session *s, *snewest;
struct timeval *tv = NULL;
u_int i;
snewest = NULL;
- for (i = 0; i < ARRAY_LENGTH(&sessions); i++) {
- if ((s = ARRAY_ITEM(&sessions, i)) == NULL)
+ for (i = 0; i < ARRAY_LENGTH(ss); i++) {
+ if ((s = ARRAY_ITEM(ss, i)) == NULL)
continue;
if (tv == NULL || timercmp(&s->tv, tv, >)) {