Fix session choice so that preferring unattached sessions actually

works, reported by Drew Frank.
This commit is contained in:
Nicholas Marriott 2012-11-27 09:20:03 +00:00
parent d762ced298
commit 991bfcf443
2 changed files with 25 additions and 13 deletions

36
cmd.c
View File

@ -116,6 +116,7 @@ const struct cmd_entry *cmd_table[] = {
NULL NULL
}; };
int cmd_session_better(struct session *, struct session *, int);
struct session *cmd_choose_session_list(struct sessionslist *); struct session *cmd_choose_session_list(struct sessionslist *);
struct session *cmd_choose_session(int); struct session *cmd_choose_session(int);
struct client *cmd_choose_client(struct clients *); struct client *cmd_choose_client(struct clients *);
@ -371,6 +372,24 @@ cmd_current_session(struct cmd_ctx *ctx, int prefer_unattached)
return (cmd_choose_session(prefer_unattached)); return (cmd_choose_session(prefer_unattached));
} }
/* Is this session better? */
int
cmd_session_better(struct session *s, struct session *best,
int prefer_unattached)
{
if (best == NULL)
return 1;
if (prefer_unattached) {
if (!(best->flags & SESSION_UNATTACHED) &&
(s->flags & SESSION_UNATTACHED))
return 1;
else if ((best->flags & SESSION_UNATTACHED) &&
!(s->flags & SESSION_UNATTACHED))
return 0;
}
return (timercmp(&s->activity_time, &best->activity_time, >));
}
/* /*
* Find the most recently used session, preferring unattached if the flag is * Find the most recently used session, preferring unattached if the flag is
* set. * set.
@ -378,21 +397,14 @@ cmd_current_session(struct cmd_ctx *ctx, int prefer_unattached)
struct session * struct session *
cmd_choose_session(int prefer_unattached) cmd_choose_session(int prefer_unattached)
{ {
struct session *s, *sbest; struct session *s, *best;
struct timeval *tv = NULL;
sbest = NULL; best = NULL;
RB_FOREACH(s, sessions, &sessions) { RB_FOREACH(s, sessions, &sessions) {
if (tv == NULL || timercmp(&s->activity_time, tv, >) || if (cmd_session_better(s, best, prefer_unattached))
(prefer_unattached && best = s;
!(sbest->flags & SESSION_UNATTACHED) &&
(s->flags & SESSION_UNATTACHED))) {
sbest = s;
tv = &s->activity_time;
} }
} return (best);
return (sbest);
} }
/* Find the most recently used session from a list. */ /* Find the most recently used session from a list. */