mirror of
				https://github.com/tmux/tmux.git
				synced 2025-11-04 09:07:38 +00:00 
			
		
		
		
	Sync OpenBSD patchset 484:
If it isn't available explicitly, work out the current client in a similar way to the current session - build a list of the possibilities then pick the newest.
This commit is contained in:
		
							
								
								
									
										78
									
								
								cmd.c
									
									
									
									
									
								
							
							
						
						
									
										78
									
								
								cmd.c
									
									
									
									
									
								
							@@ -1,4 +1,4 @@
 | 
			
		||||
/* $Id: cmd.c,v 1.128 2009-11-04 22:42:31 tcunha Exp $ */
 | 
			
		||||
/* $Id: cmd.c,v 1.129 2009-11-04 22:44:01 tcunha Exp $ */
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
 * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
 | 
			
		||||
@@ -110,7 +110,7 @@ const struct cmd_entry *cmd_table[] = {
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
struct session	*cmd_newest_session(struct sessions *);
 | 
			
		||||
struct client	*cmd_newest_client(void);
 | 
			
		||||
struct client	*cmd_newest_client(struct clients *);
 | 
			
		||||
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 *);
 | 
			
		||||
@@ -370,17 +370,56 @@ cmd_newest_session(struct sessions *ss)
 | 
			
		||||
	return (snewest);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
 * Find the current client. First try the current client if set, then pick the
 | 
			
		||||
 * newest of the clients attached to the current session if any, then the
 | 
			
		||||
 * newest client.
 | 
			
		||||
 */
 | 
			
		||||
struct client *
 | 
			
		||||
cmd_current_client(struct cmd_ctx *ctx)
 | 
			
		||||
{
 | 
			
		||||
	struct session		*s;
 | 
			
		||||
	struct client		*c;
 | 
			
		||||
	struct clients		 cc;
 | 
			
		||||
	u_int			 i;
 | 
			
		||||
 | 
			
		||||
	if (ctx->curclient != NULL)
 | 
			
		||||
		return (ctx->curclient);
 | 
			
		||||
 | 
			
		||||
	/*
 | 
			
		||||
	 * No current client set. Find the current session and return the
 | 
			
		||||
	 * newest of its clients.
 | 
			
		||||
	 */
 | 
			
		||||
	s = cmd_current_session(ctx);
 | 
			
		||||
	if (s != NULL && !(s->flags & SESSION_UNATTACHED)) {
 | 
			
		||||
		ARRAY_INIT(&cc);
 | 
			
		||||
		for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
 | 
			
		||||
			if ((c = ARRAY_ITEM(&clients, i)) == NULL)
 | 
			
		||||
				continue;
 | 
			
		||||
			if (s == c->session)
 | 
			
		||||
				ARRAY_ADD(&cc, c);
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		c = cmd_newest_client(&cc);
 | 
			
		||||
		ARRAY_FREE(&cc);
 | 
			
		||||
		if (c != NULL)
 | 
			
		||||
			return (c);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return (cmd_newest_client(&clients));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* Find the newest client. */
 | 
			
		||||
struct client *
 | 
			
		||||
cmd_newest_client(void)
 | 
			
		||||
cmd_newest_client(struct clients *cc)
 | 
			
		||||
{
 | 
			
		||||
	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)
 | 
			
		||||
	for (i = 0; i < ARRAY_LENGTH(cc); i++) {
 | 
			
		||||
		if ((c = ARRAY_ITEM(cc, i)) == NULL)
 | 
			
		||||
			continue;
 | 
			
		||||
		if (c->session == NULL)
 | 
			
		||||
			continue;
 | 
			
		||||
@@ -398,36 +437,13 @@ cmd_newest_client(void)
 | 
			
		||||
struct client *
 | 
			
		||||
cmd_find_client(struct cmd_ctx *ctx, const char *arg)
 | 
			
		||||
{
 | 
			
		||||
	struct client	*c, *lastc;
 | 
			
		||||
	struct session	*s;
 | 
			
		||||
	struct client	*c;
 | 
			
		||||
	char		*tmparg;
 | 
			
		||||
	size_t		 arglen;
 | 
			
		||||
	u_int		 i;
 | 
			
		||||
 | 
			
		||||
	/* A NULL argument means the current client. */
 | 
			
		||||
	if (arg == NULL) {
 | 
			
		||||
		if (ctx->curclient != NULL)
 | 
			
		||||
			return (ctx->curclient);
 | 
			
		||||
		/*
 | 
			
		||||
		 * No current client set. Find the current session and see if
 | 
			
		||||
		 * it has only one client.
 | 
			
		||||
		 */
 | 
			
		||||
		s = cmd_current_session(ctx);
 | 
			
		||||
		if (s != NULL) {
 | 
			
		||||
			lastc = NULL;
 | 
			
		||||
			for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
 | 
			
		||||
				c = ARRAY_ITEM(&clients, i);
 | 
			
		||||
				if (c != NULL && c->session == s) {
 | 
			
		||||
					if (lastc != NULL)
 | 
			
		||||
						break;
 | 
			
		||||
					lastc = c;
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
			if (i == ARRAY_LENGTH(&clients) && lastc != NULL)
 | 
			
		||||
				return (lastc);
 | 
			
		||||
		}
 | 
			
		||||
		return (cmd_newest_client());
 | 
			
		||||
	}
 | 
			
		||||
	if (arg == NULL)
 | 
			
		||||
		return (cmd_current_client(ctx));
 | 
			
		||||
	tmparg = xstrdup(arg);
 | 
			
		||||
 | 
			
		||||
	/* Trim a single trailing colon if any. */
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										3
									
								
								tmux.h
									
									
									
									
									
								
							
							
						
						
									
										3
									
								
								tmux.h
									
									
									
									
									
								
							@@ -1,4 +1,4 @@
 | 
			
		||||
/* $Id: tmux.h,v 1.494 2009-11-04 22:42:31 tcunha Exp $ */
 | 
			
		||||
/* $Id: tmux.h,v 1.495 2009-11-04 22:44:01 tcunha Exp $ */
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
 * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
 | 
			
		||||
@@ -1425,6 +1425,7 @@ int		 cmd_exec(struct cmd *, struct cmd_ctx *);
 | 
			
		||||
void		 cmd_free(struct cmd *);
 | 
			
		||||
size_t		 cmd_print(struct cmd *, char *, size_t);
 | 
			
		||||
struct session	*cmd_current_session(struct cmd_ctx *);
 | 
			
		||||
struct client	*cmd_current_client(struct cmd_ctx *);
 | 
			
		||||
struct client	*cmd_find_client(struct cmd_ctx *, const char *);
 | 
			
		||||
struct session	*cmd_find_session(struct cmd_ctx *, const char *);
 | 
			
		||||
struct winlink	*cmd_find_window(
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user