File:  [ELWIX - Embedded LightWeight unIX -] / embedaddon / tmux / cmd-find.c
Revision 1.1.1.1 (vendor branch): download - view: text, annotated - select for diffs - revision graph
Wed Jun 14 12:22:44 2017 UTC (6 years, 11 months ago) by misho
Branches: tmux, MAIN
CVS tags: v2_4p0, v2_4, HEAD
tmux 2.4

    1: /* $OpenBSD$ */
    2: 
    3: /*
    4:  * Copyright (c) 2015 Nicholas Marriott <nicholas.marriott@gmail.com>
    5:  *
    6:  * Permission to use, copy, modify, and distribute this software for any
    7:  * purpose with or without fee is hereby granted, provided that the above
    8:  * copyright notice and this permission notice appear in all copies.
    9:  *
   10:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
   11:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
   12:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
   13:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
   14:  * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
   15:  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
   16:  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
   17:  */
   18: 
   19: #include <sys/types.h>
   20: 
   21: #include <fnmatch.h>
   22: #include <limits.h>
   23: #include <stdlib.h>
   24: #include <string.h>
   25: #include <unistd.h>
   26: 
   27: #include "tmux.h"
   28: 
   29: static struct session *cmd_find_try_TMUX(struct client *, struct window *);
   30: static int	cmd_find_client_better(struct client *, struct client *);
   31: static struct client *cmd_find_best_client(struct client **, u_int);
   32: static int	cmd_find_session_better(struct session *, struct session *,
   33: 		    int);
   34: static struct session *cmd_find_best_session(struct session **, u_int, int);
   35: static int	cmd_find_best_session_with_window(struct cmd_find_state *);
   36: static int	cmd_find_best_winlink_with_window(struct cmd_find_state *);
   37: 
   38: static int	cmd_find_current_session_with_client(struct cmd_find_state *);
   39: static int	cmd_find_current_session(struct cmd_find_state *);
   40: static struct client *cmd_find_current_client(struct cmdq_item *);
   41: 
   42: static const char *cmd_find_map_table(const char *[][2], const char *);
   43: 
   44: static int	cmd_find_get_session(struct cmd_find_state *, const char *);
   45: static int	cmd_find_get_window(struct cmd_find_state *, const char *, int);
   46: static int	cmd_find_get_window_with_session(struct cmd_find_state *,
   47: 		    const char *);
   48: static int	cmd_find_get_pane(struct cmd_find_state *, const char *, int);
   49: static int	cmd_find_get_pane_with_session(struct cmd_find_state *,
   50: 		    const char *);
   51: static int	cmd_find_get_pane_with_window(struct cmd_find_state *,
   52: 		    const char *);
   53: 
   54: static const char *cmd_find_session_table[][2] = {
   55: 	{ NULL, NULL }
   56: };
   57: static const char *cmd_find_window_table[][2] = {
   58: 	{ "{start}", "^" },
   59: 	{ "{last}", "!" },
   60: 	{ "{end}", "$" },
   61: 	{ "{next}", "+" },
   62: 	{ "{previous}", "-" },
   63: 	{ NULL, NULL }
   64: };
   65: static const char *cmd_find_pane_table[][2] = {
   66: 	{ "{last}", "!" },
   67: 	{ "{next}", "+" },
   68: 	{ "{previous}", "-" },
   69: 	{ "{top}", "top" },
   70: 	{ "{bottom}", "bottom" },
   71: 	{ "{left}", "left" },
   72: 	{ "{right}", "right" },
   73: 	{ "{top-left}", "top-left" },
   74: 	{ "{top-right}", "top-right" },
   75: 	{ "{bottom-left}", "bottom-left" },
   76: 	{ "{bottom-right}", "bottom-right" },
   77: 	{ "{up-of}", "{up-of}" },
   78: 	{ "{down-of}", "{down-of}" },
   79: 	{ "{left-of}", "{left-of}" },
   80: 	{ "{right-of}", "{right-of}" },
   81: 	{ NULL, NULL }
   82: };
   83: 
   84: /* Get session from TMUX if present. */
   85: static struct session *
   86: cmd_find_try_TMUX(struct client *c, struct window *w)
   87: {
   88: 	struct environ_entry	*envent;
   89: 	char			 tmp[256];
   90: 	long long		 pid;
   91: 	u_int			 session;
   92: 	struct session		*s;
   93: 
   94: 	envent = environ_find(c->environ, "TMUX");
   95: 	if (envent == NULL)
   96: 		return (NULL);
   97: 
   98: 	if (sscanf(envent->value, "%255[^,],%lld,%d", tmp, &pid, &session) != 3)
   99: 		return (NULL);
  100: 	if (pid != getpid())
  101: 		return (NULL);
  102: 	log_debug("client %p TMUX is %s (session @%u)", c, envent->value,
  103: 	    session);
  104: 
  105: 	s = session_find_by_id(session);
  106: 	if (s == NULL || (w != NULL && !session_has(s, w)))
  107: 		return (NULL);
  108: 	return (s);
  109: }
  110: 
  111: /* Is this client better? */
  112: static int
  113: cmd_find_client_better(struct client *c, struct client *than)
  114: {
  115: 	if (than == NULL)
  116: 		return (1);
  117: 	return (timercmp(&c->activity_time, &than->activity_time, >));
  118: }
  119: 
  120: /* Find best client from a list, or all if list is NULL. */
  121: static struct client *
  122: cmd_find_best_client(struct client **clist, u_int csize)
  123: {
  124: 	struct client	*c_loop, *c;
  125: 	u_int		 i;
  126: 
  127: 	c = NULL;
  128: 	if (clist != NULL) {
  129: 		for (i = 0; i < csize; i++) {
  130: 			if (clist[i]->session == NULL)
  131: 				continue;
  132: 			if (cmd_find_client_better(clist[i], c))
  133: 				c = clist[i];
  134: 		}
  135: 	} else {
  136: 		TAILQ_FOREACH(c_loop, &clients, entry) {
  137: 			if (c_loop->session == NULL)
  138: 				continue;
  139: 			if (cmd_find_client_better(c_loop, c))
  140: 				c = c_loop;
  141: 		}
  142: 	}
  143: 	return (c);
  144: }
  145: 
  146: /* Is this session better? */
  147: static int
  148: cmd_find_session_better(struct session *s, struct session *than, int flags)
  149: {
  150: 	int	attached;
  151: 
  152: 	if (than == NULL)
  153: 		return (1);
  154: 	if (flags & CMD_FIND_PREFER_UNATTACHED) {
  155: 		attached = (~than->flags & SESSION_UNATTACHED);
  156: 		if (attached && (s->flags & SESSION_UNATTACHED))
  157: 			return (1);
  158: 		else if (!attached && (~s->flags & SESSION_UNATTACHED))
  159: 			return (0);
  160: 	}
  161: 	return (timercmp(&s->activity_time, &than->activity_time, >));
  162: }
  163: 
  164: /* Find best session from a list, or all if list is NULL. */
  165: static struct session *
  166: cmd_find_best_session(struct session **slist, u_int ssize, int flags)
  167: {
  168: 	struct session	 *s_loop, *s;
  169: 	u_int		  i;
  170: 
  171: 	s = NULL;
  172: 	if (slist != NULL) {
  173: 		for (i = 0; i < ssize; i++) {
  174: 			if (cmd_find_session_better(slist[i], s, flags))
  175: 				s = slist[i];
  176: 		}
  177: 	} else {
  178: 		RB_FOREACH(s_loop, sessions, &sessions) {
  179: 			if (cmd_find_session_better(s_loop, s, flags))
  180: 				s = s_loop;
  181: 		}
  182: 	}
  183: 	return (s);
  184: }
  185: 
  186: /* Find best session and winlink for window. */
  187: static int
  188: cmd_find_best_session_with_window(struct cmd_find_state *fs)
  189: {
  190: 	struct session	**slist = NULL;
  191: 	u_int		  ssize;
  192: 	struct session	 *s;
  193: 
  194: 	if (fs->item != NULL && fs->item->client != NULL) {
  195: 		fs->s = cmd_find_try_TMUX(fs->item->client, fs->w);
  196: 		if (fs->s != NULL)
  197: 			return (cmd_find_best_winlink_with_window(fs));
  198: 	}
  199: 
  200: 	ssize = 0;
  201: 	RB_FOREACH(s, sessions, &sessions) {
  202: 		if (!session_has(s, fs->w))
  203: 			continue;
  204: 		slist = xreallocarray(slist, ssize + 1, sizeof *slist);
  205: 		slist[ssize++] = s;
  206: 	}
  207: 	if (ssize == 0)
  208: 		goto fail;
  209: 	fs->s = cmd_find_best_session(slist, ssize, fs->flags);
  210: 	if (fs->s == NULL)
  211: 		goto fail;
  212: 	free(slist);
  213: 	return (cmd_find_best_winlink_with_window(fs));
  214: 
  215: fail:
  216: 	free(slist);
  217: 	return (-1);
  218: }
  219: 
  220: /*
  221:  * Find the best winlink for a window (the current if it contains the pane,
  222:  * otherwise the first).
  223:  */
  224: static int
  225: cmd_find_best_winlink_with_window(struct cmd_find_state *fs)
  226: {
  227: 	struct winlink	 *wl, *wl_loop;
  228: 
  229: 	wl = NULL;
  230: 	if (fs->s->curw != NULL && fs->s->curw->window == fs->w)
  231: 		wl = fs->s->curw;
  232: 	else {
  233: 		RB_FOREACH(wl_loop, winlinks, &fs->s->windows) {
  234: 			if (wl_loop->window == fs->w) {
  235: 				wl = wl_loop;
  236: 				break;
  237: 			}
  238: 		}
  239: 	}
  240: 	if (wl == NULL)
  241: 		return (-1);
  242: 	fs->wl = wl;
  243: 	fs->idx = fs->wl->idx;
  244: 	return (0);
  245: }
  246: 
  247: /* Find current session when we have an unattached client. */
  248: static int
  249: cmd_find_current_session_with_client(struct cmd_find_state *fs)
  250: {
  251: 	struct window_pane	*wp;
  252: 
  253: 	/*
  254: 	 * If this is running in a pane, we can use that to limit the list of
  255: 	 * sessions to those containing that pane (we still use the current
  256: 	 * window in the best session).
  257: 	 */
  258: 	if (fs->item != NULL) {
  259: 		RB_FOREACH(wp, window_pane_tree, &all_window_panes) {
  260: 			if (strcmp(wp->tty, fs->item->client->ttyname) == 0)
  261: 				break;
  262: 		}
  263: 	} else
  264: 		wp = NULL;
  265: 
  266: 	/* Not running in a pane. We know nothing. Find the best session. */
  267: 	if (wp == NULL)
  268: 		goto unknown_pane;
  269: 
  270: 	/* Find the best session and winlink containing this pane. */
  271: 	fs->w = wp->window;
  272: 	if (cmd_find_best_session_with_window(fs) != 0) {
  273: 		if (wp != NULL) {
  274: 			/*
  275: 			 * The window may have been destroyed but the pane
  276: 			 * still on all_window_panes due to something else
  277: 			 * holding a reference.
  278: 			 */
  279: 			goto unknown_pane;
  280: 		}
  281: 		return (-1);
  282: 	}
  283: 
  284: 	/* Use the current window and pane from this session. */
  285: 	fs->wl = fs->s->curw;
  286: 	fs->idx = fs->wl->idx;
  287: 	fs->w = fs->wl->window;
  288: 	fs->wp = fs->w->active;
  289: 
  290: 	return (0);
  291: 
  292: unknown_pane:
  293: 	fs->s = NULL;
  294: 	if (fs->item != NULL)
  295: 		fs->s = cmd_find_try_TMUX(fs->item->client, NULL);
  296: 	if (fs->s == NULL)
  297: 		fs->s = cmd_find_best_session(NULL, 0, fs->flags);
  298: 	if (fs->s == NULL)
  299: 		return (-1);
  300: 	fs->wl = fs->s->curw;
  301: 	fs->idx = fs->wl->idx;
  302: 	fs->w = fs->wl->window;
  303: 	fs->wp = fs->w->active;
  304: 
  305: 	return (0);
  306: }
  307: 
  308: /*
  309:  * Work out the best current state. If this function succeeds, the state is
  310:  * guaranteed to be completely filled in.
  311:  */
  312: static int
  313: cmd_find_current_session(struct cmd_find_state *fs)
  314: {
  315: 	/* If we know the current client, use it. */
  316: 	if (fs->item != NULL && fs->item->client != NULL) {
  317: 		log_debug("%s: have client %p%s", __func__, fs->item->client,
  318: 		    fs->item->client->session == NULL ? "" : " (with session)");
  319: 		if (fs->item->client->session == NULL)
  320: 			return (cmd_find_current_session_with_client(fs));
  321: 		fs->s = fs->item->client->session;
  322: 		fs->wl = fs->s->curw;
  323: 		fs->idx = fs->wl->idx;
  324: 		fs->w = fs->wl->window;
  325: 		fs->wp = fs->w->active;
  326: 		return (0);
  327: 	}
  328: 
  329: 	/* We know nothing, find the best session and client. */
  330: 	fs->s = cmd_find_best_session(NULL, 0, fs->flags);
  331: 	if (fs->s == NULL)
  332: 		return (-1);
  333: 	fs->wl = fs->s->curw;
  334: 	fs->idx = fs->wl->idx;
  335: 	fs->w = fs->wl->window;
  336: 	fs->wp = fs->w->active;
  337: 
  338: 	return (0);
  339: }
  340: 
  341: /* Work out the best current client. */
  342: static struct client *
  343: cmd_find_current_client(struct cmdq_item *item)
  344: {
  345: 	struct cmd_find_state	 current;
  346: 	struct session		*s;
  347: 	struct client		*c, **clist = NULL;
  348: 	u_int		 	 csize;
  349: 
  350: 	/* If the queue client has a session, use it. */
  351: 	if (item->client != NULL && item->client->session != NULL) {
  352: 		log_debug("%s: using item %p client %p", __func__, item,
  353: 		    item->client);
  354: 		return (item->client);
  355: 	}
  356: 
  357: 	/* Otherwise find the current session. */
  358: 	cmd_find_clear_state(&current, item, 0);
  359: 	if (cmd_find_current_session(&current) != 0)
  360: 		return (NULL);
  361: 
  362: 	/* If it is attached, find the best of it's clients. */
  363: 	s = current.s;
  364: 	log_debug("%s: current session $%u %s", __func__, s->id, s->name);
  365: 	if (~s->flags & SESSION_UNATTACHED) {
  366: 		csize = 0;
  367: 		TAILQ_FOREACH(c, &clients, entry) {
  368: 			if (c->session != s)
  369: 				continue;
  370: 			clist = xreallocarray(clist, csize + 1, sizeof *clist);
  371: 			clist[csize++] = c;
  372: 		}
  373: 		if (csize != 0) {
  374: 			c = cmd_find_best_client(clist, csize);
  375: 			if (c != NULL) {
  376: 				free(clist);
  377: 				return (c);
  378: 			}
  379: 		}
  380: 		free(clist);
  381: 	}
  382: 
  383: 	/* Otherwise pick best of all clients. */
  384: 	return (cmd_find_best_client(NULL, 0));
  385: }
  386: 
  387: /* Maps string in table. */
  388: static const char *
  389: cmd_find_map_table(const char *table[][2], const char *s)
  390: {
  391: 	u_int	i;
  392: 
  393: 	for (i = 0; table[i][0] != NULL; i++) {
  394: 		if (strcmp(s, table[i][0]) == 0)
  395: 			return (table[i][1]);
  396: 	}
  397: 	return (s);
  398: }
  399: 
  400: /* Find session from string. Fills in s. */
  401: static int
  402: cmd_find_get_session(struct cmd_find_state *fs, const char *session)
  403: {
  404: 	struct session	*s, *s_loop;
  405: 	struct client	*c;
  406: 
  407: 	log_debug("%s: %s", __func__, session);
  408: 
  409: 	/* Check for session ids starting with $. */
  410: 	if (*session == '$') {
  411: 		fs->s = session_find_by_id_str(session);
  412: 		if (fs->s == NULL)
  413: 			return (-1);
  414: 		return (0);
  415: 	}
  416: 
  417: 	/* Look for exactly this session. */
  418: 	fs->s = session_find(session);
  419: 	if (fs->s != NULL)
  420: 		return (0);
  421: 
  422: 	/* Look for as a client. */
  423: 	c = cmd_find_client(NULL, session, 1);
  424: 	if (c != NULL && c->session != NULL) {
  425: 		fs->s = c->session;
  426: 		return (0);
  427: 	}
  428: 
  429: 	/* Stop now if exact only. */
  430: 	if (fs->flags & CMD_FIND_EXACT_SESSION)
  431: 		return (-1);
  432: 
  433: 	/* Otherwise look for prefix. */
  434: 	s = NULL;
  435: 	RB_FOREACH(s_loop, sessions, &sessions) {
  436: 		if (strncmp(session, s_loop->name, strlen(session)) == 0) {
  437: 			if (s != NULL)
  438: 				return (-1);
  439: 			s = s_loop;
  440: 		}
  441: 	}
  442: 	if (s != NULL) {
  443: 		fs->s = s;
  444: 		return (0);
  445: 	}
  446: 
  447: 	/* Then as a pattern. */
  448: 	s = NULL;
  449: 	RB_FOREACH(s_loop, sessions, &sessions) {
  450: 		if (fnmatch(session, s_loop->name, 0) == 0) {
  451: 			if (s != NULL)
  452: 				return (-1);
  453: 			s = s_loop;
  454: 		}
  455: 	}
  456: 	if (s != NULL) {
  457: 		fs->s = s;
  458: 		return (0);
  459: 	}
  460: 
  461: 	return (-1);
  462: }
  463: 
  464: /* Find window from string. Fills in s, wl, w. */
  465: static int
  466: cmd_find_get_window(struct cmd_find_state *fs, const char *window, int only)
  467: {
  468: 	log_debug("%s: %s", __func__, window);
  469: 
  470: 	/* Check for window ids starting with @. */
  471: 	if (*window == '@') {
  472: 		fs->w = window_find_by_id_str(window);
  473: 		if (fs->w == NULL)
  474: 			return (-1);
  475: 		return (cmd_find_best_session_with_window(fs));
  476: 	}
  477: 
  478: 	/* Not a window id, so use the current session. */
  479: 	fs->s = fs->current->s;
  480: 
  481: 	/* We now only need to find the winlink in this session. */
  482: 	if (cmd_find_get_window_with_session(fs, window) == 0)
  483: 		return (0);
  484: 
  485: 	/* Otherwise try as a session itself. */
  486: 	if (!only && cmd_find_get_session(fs, window) == 0) {
  487: 		fs->wl = fs->s->curw;
  488: 		fs->w = fs->wl->window;
  489: 		if (~fs->flags & CMD_FIND_WINDOW_INDEX)
  490: 			fs->idx = fs->wl->idx;
  491: 		return (0);
  492: 	}
  493: 
  494: 	return (-1);
  495: }
  496: 
  497: /*
  498:  * Find window from string, assuming it is in given session. Needs s, fills in
  499:  * wl and w.
  500:  */
  501: static int
  502: cmd_find_get_window_with_session(struct cmd_find_state *fs, const char *window)
  503: {
  504: 	struct winlink	*wl;
  505: 	const char	*errstr;
  506: 	int		 idx, n, exact;
  507: 	struct session	*s;
  508: 
  509: 	log_debug("%s: %s", __func__, window);
  510: 	exact = (fs->flags & CMD_FIND_EXACT_WINDOW);
  511: 
  512: 	/*
  513: 	 * Start with the current window as the default. So if only an index is
  514: 	 * found, the window will be the current.
  515: 	 */
  516: 	fs->wl = fs->s->curw;
  517: 	fs->w = fs->wl->window;
  518: 
  519: 	/* Check for window ids starting with @. */
  520: 	if (*window == '@') {
  521: 		fs->w = window_find_by_id_str(window);
  522: 		if (fs->w == NULL || !session_has(fs->s, fs->w))
  523: 			return (-1);
  524: 		return (cmd_find_best_winlink_with_window(fs));
  525: 	}
  526: 
  527: 	/* Try as an offset. */
  528: 	if (!exact && (window[0] == '+' || window[0] == '-')) {
  529: 		if (window[1] != '\0')
  530: 			n = strtonum(window + 1, 1, INT_MAX, NULL);
  531: 		else
  532: 			n = 1;
  533: 		s = fs->s;
  534: 		if (fs->flags & CMD_FIND_WINDOW_INDEX) {
  535: 			if (window[0] == '+') {
  536: 				if (INT_MAX - s->curw->idx < n)
  537: 					return (-1);
  538: 				fs->idx = s->curw->idx + n;
  539: 			} else {
  540: 				if (n < s->curw->idx)
  541: 					return (-1);
  542: 				fs->idx = s->curw->idx - n;
  543: 			}
  544: 			return (0);
  545: 		}
  546: 		if (window[0] == '+')
  547: 			fs->wl = winlink_next_by_number(s->curw, s, n);
  548: 		else
  549: 			fs->wl = winlink_previous_by_number(s->curw, s, n);
  550: 		if (fs->wl != NULL) {
  551: 			fs->idx = fs->wl->idx;
  552: 			fs->w = fs->wl->window;
  553: 			return (0);
  554: 		}
  555: 	}
  556: 
  557: 	/* Try special characters. */
  558: 	if (!exact) {
  559: 		if (strcmp(window, "!") == 0) {
  560: 			fs->wl = TAILQ_FIRST(&fs->s->lastw);
  561: 			if (fs->wl == NULL)
  562: 				return (-1);
  563: 			fs->idx = fs->wl->idx;
  564: 			fs->w = fs->wl->window;
  565: 			return (0);
  566: 		} else if (strcmp(window, "^") == 0) {
  567: 			fs->wl = RB_MIN(winlinks, &fs->s->windows);
  568: 			if (fs->wl == NULL)
  569: 				return (-1);
  570: 			fs->idx = fs->wl->idx;
  571: 			fs->w = fs->wl->window;
  572: 			return (0);
  573: 		} else if (strcmp(window, "$") == 0) {
  574: 			fs->wl = RB_MAX(winlinks, &fs->s->windows);
  575: 			if (fs->wl == NULL)
  576: 				return (-1);
  577: 			fs->idx = fs->wl->idx;
  578: 			fs->w = fs->wl->window;
  579: 			return (0);
  580: 		}
  581: 	}
  582: 
  583: 	/* First see if this is a valid window index in this session. */
  584: 	if (window[0] != '+' && window[0] != '-') {
  585: 		idx = strtonum(window, 0, INT_MAX, &errstr);
  586: 		if (errstr == NULL) {
  587: 			if (fs->flags & CMD_FIND_WINDOW_INDEX) {
  588: 				fs->idx = idx;
  589: 				return (0);
  590: 			}
  591: 			fs->wl = winlink_find_by_index(&fs->s->windows, idx);
  592: 			if (fs->wl != NULL) {
  593: 				fs->w = fs->wl->window;
  594: 				return (0);
  595: 			}
  596: 		}
  597: 	}
  598: 
  599: 	/* Look for exact matches, error if more than one. */
  600: 	fs->wl = NULL;
  601: 	RB_FOREACH(wl, winlinks, &fs->s->windows) {
  602: 		if (strcmp(window, wl->window->name) == 0) {
  603: 			if (fs->wl != NULL)
  604: 				return (-1);
  605: 			fs->wl = wl;
  606: 		}
  607: 	}
  608: 	if (fs->wl != NULL) {
  609: 		fs->idx = fs->wl->idx;
  610: 		fs->w = fs->wl->window;
  611: 		return (0);
  612: 	}
  613: 
  614: 	/* Stop now if exact only. */
  615: 	if (exact)
  616: 		return (-1);
  617: 
  618: 	/* Try as the start of a window name, error if multiple. */
  619: 	fs->wl = NULL;
  620: 	RB_FOREACH(wl, winlinks, &fs->s->windows) {
  621: 		if (strncmp(window, wl->window->name, strlen(window)) == 0) {
  622: 			if (fs->wl != NULL)
  623: 				return (-1);
  624: 			fs->wl = wl;
  625: 		}
  626: 	}
  627: 	if (fs->wl != NULL) {
  628: 		fs->idx = fs->wl->idx;
  629: 		fs->w = fs->wl->window;
  630: 		return (0);
  631: 	}
  632: 
  633: 	/* Now look for pattern matches, again error if multiple. */
  634: 	fs->wl = NULL;
  635: 	RB_FOREACH(wl, winlinks, &fs->s->windows) {
  636: 		if (fnmatch(window, wl->window->name, 0) == 0) {
  637: 			if (fs->wl != NULL)
  638: 				return (-1);
  639: 			fs->wl = wl;
  640: 		}
  641: 	}
  642: 	if (fs->wl != NULL) {
  643: 		fs->idx = fs->wl->idx;
  644: 		fs->w = fs->wl->window;
  645: 		return (0);
  646: 	}
  647: 
  648: 	return (-1);
  649: }
  650: 
  651: /* Find pane from string. Fills in s, wl, w, wp. */
  652: static int
  653: cmd_find_get_pane(struct cmd_find_state *fs, const char *pane, int only)
  654: {
  655: 	log_debug("%s: %s", __func__, pane);
  656: 
  657: 	/* Check for pane ids starting with %. */
  658: 	if (*pane == '%') {
  659: 		fs->wp = window_pane_find_by_id_str(pane);
  660: 		if (fs->wp == NULL || window_pane_outside(fs->wp))
  661: 			return (-1);
  662: 		fs->w = fs->wp->window;
  663: 		return (cmd_find_best_session_with_window(fs));
  664: 	}
  665: 
  666: 	/* Not a pane id, so try the current session and window. */
  667: 	fs->s = fs->current->s;
  668: 	fs->wl = fs->current->wl;
  669: 	fs->idx = fs->current->idx;
  670: 	fs->w = fs->current->w;
  671: 
  672: 	/* We now only need to find the pane in this window. */
  673: 	if (cmd_find_get_pane_with_window(fs, pane) == 0)
  674: 		return (0);
  675: 
  676: 	/* Otherwise try as a window itself (this will also try as session). */
  677: 	if (!only && cmd_find_get_window(fs, pane, 0) == 0) {
  678: 		fs->wp = fs->w->active;
  679: 		return (0);
  680: 	}
  681: 
  682: 	return (-1);
  683: }
  684: 
  685: /*
  686:  * Find pane from string, assuming it is in given session. Needs s, fills in wl
  687:  * and w and wp.
  688:  */
  689: static int
  690: cmd_find_get_pane_with_session(struct cmd_find_state *fs, const char *pane)
  691: {
  692: 	log_debug("%s: %s", __func__, pane);
  693: 
  694: 	/* Check for pane ids starting with %. */
  695: 	if (*pane == '%') {
  696: 		fs->wp = window_pane_find_by_id_str(pane);
  697: 		if (fs->wp == NULL || window_pane_outside(fs->wp))
  698: 			return (-1);
  699: 		fs->w = fs->wp->window;
  700: 		return (cmd_find_best_winlink_with_window(fs));
  701: 	}
  702: 
  703: 	/* Otherwise use the current window. */
  704: 	fs->wl = fs->s->curw;
  705: 	fs->idx = fs->wl->idx;
  706: 	fs->w = fs->wl->window;
  707: 
  708: 	/* Now we just need to look up the pane. */
  709: 	return (cmd_find_get_pane_with_window(fs, pane));
  710: }
  711: 
  712: /*
  713:  * Find pane from string, assuming it is in the given window. Needs w, fills in
  714:  * wp.
  715:  */
  716: static int
  717: cmd_find_get_pane_with_window(struct cmd_find_state *fs, const char *pane)
  718: {
  719: 	const char		*errstr;
  720: 	int			 idx;
  721: 	struct window_pane	*wp;
  722: 	u_int			 n;
  723: 
  724: 	log_debug("%s: %s", __func__, pane);
  725: 
  726: 	/* Check for pane ids starting with %. */
  727: 	if (*pane == '%') {
  728: 		fs->wp = window_pane_find_by_id_str(pane);
  729: 		if (fs->wp == NULL || window_pane_outside(fs->wp))
  730: 			return (-1);
  731: 		if (fs->wp->window != fs->w)
  732: 			return (-1);
  733: 		return (0);
  734: 	}
  735: 
  736: 	/* Try special characters. */
  737: 	if (strcmp(pane, "!") == 0) {
  738: 		if (fs->w->last == NULL)
  739: 			return (-1);
  740: 		fs->wp = fs->w->last;
  741: 		if (fs->wp == NULL || window_pane_outside(fs->wp))
  742: 			return (-1);
  743: 		return (0);
  744: 	} else if (strcmp(pane, "{up-of}") == 0) {
  745: 		fs->wp = window_pane_find_up(fs->w->active);
  746: 		if (fs->wp == NULL || window_pane_outside(fs->wp))
  747: 			return (-1);
  748: 		return (0);
  749: 	} else if (strcmp(pane, "{down-of}") == 0) {
  750: 		fs->wp = window_pane_find_down(fs->w->active);
  751: 		if (fs->wp == NULL || window_pane_outside(fs->wp))
  752: 			return (-1);
  753: 		return (0);
  754: 	} else if (strcmp(pane, "{left-of}") == 0) {
  755: 		fs->wp = window_pane_find_left(fs->w->active);
  756: 		if (fs->wp == NULL || window_pane_outside(fs->wp))
  757: 			return (-1);
  758: 		return (0);
  759: 	} else if (strcmp(pane, "{right-of}") == 0) {
  760: 		fs->wp = window_pane_find_right(fs->w->active);
  761: 		if (fs->wp == NULL || window_pane_outside(fs->wp))
  762: 			return (-1);
  763: 		return (0);
  764: 	}
  765: 
  766: 	/* Try as an offset. */
  767: 	if (pane[0] == '+' || pane[0] == '-') {
  768: 		if (pane[1] != '\0')
  769: 			n = strtonum(pane + 1, 1, INT_MAX, NULL);
  770: 		else
  771: 			n = 1;
  772: 		wp = fs->w->active;
  773: 		if (pane[0] == '+')
  774: 			fs->wp = window_pane_next_by_number(fs->w, wp, n);
  775: 		else
  776: 			fs->wp = window_pane_previous_by_number(fs->w, wp, n);
  777: 		if (fs->wp != NULL && !window_pane_outside(fs->wp))
  778: 			return (0);
  779: 	}
  780: 
  781: 	/* Get pane by index. */
  782: 	idx = strtonum(pane, 0, INT_MAX, &errstr);
  783: 	if (errstr == NULL) {
  784: 		fs->wp = window_pane_at_index(fs->w, idx);
  785: 		if (fs->wp != NULL && !window_pane_outside(fs->wp))
  786: 			return (0);
  787: 	}
  788: 
  789: 	/* Try as a description. */
  790: 	fs->wp = window_find_string(fs->w, pane);
  791: 	if (fs->wp != NULL && !window_pane_outside(fs->wp))
  792: 		return (0);
  793: 
  794: 	return (-1);
  795: }
  796: 
  797: /* Clear state. */
  798: void
  799: cmd_find_clear_state(struct cmd_find_state *fs, struct cmdq_item *item,
  800:     int flags)
  801: {
  802: 	memset(fs, 0, sizeof *fs);
  803: 
  804: 	fs->item = item;
  805: 	fs->flags = flags;
  806: 
  807: 	fs->idx = -1;
  808: }
  809: 
  810: /* Check if state is empty/ */
  811: int
  812: cmd_find_empty_state(struct cmd_find_state *fs)
  813: {
  814: 	if (fs->s == NULL && fs->wl == NULL && fs->w == NULL && fs->wp == NULL)
  815: 		return (1);
  816: 	return (0);
  817: }
  818: 
  819: /* Check if a state if valid. */
  820: int
  821: cmd_find_valid_state(struct cmd_find_state *fs)
  822: {
  823: 	struct winlink	*wl;
  824: 
  825: 	if (fs->s == NULL || fs->wl == NULL || fs->w == NULL || fs->wp == NULL)
  826: 		return (0);
  827: 
  828: 	if (!session_alive(fs->s))
  829: 		return (0);
  830: 
  831: 	RB_FOREACH(wl, winlinks, &fs->s->windows) {
  832: 		if (wl->window == fs->w && wl == fs->wl)
  833: 			break;
  834: 	}
  835: 	if (wl == NULL)
  836: 		return (0);
  837: 
  838: 	if (fs->w != fs->wl->window)
  839: 		return (0);
  840: 
  841: 	if (!window_has_pane(fs->w, fs->wp))
  842: 		return (0);
  843: 	return (!window_pane_outside(fs->wp));
  844: }
  845: 
  846: /* Copy a state. */
  847: void
  848: cmd_find_copy_state(struct cmd_find_state *dst, struct cmd_find_state *src)
  849: {
  850: 	dst->s = src->s;
  851: 	dst->wl = src->wl;
  852: 	dst->idx = src->idx;
  853: 	dst->w = src->w;
  854: 	dst->wp = src->wp;
  855: }
  856: 
  857: /* Log the result. */
  858: void
  859: cmd_find_log_state(const char *prefix, struct cmd_find_state *fs)
  860: {
  861: 	if (fs->s != NULL)
  862: 		log_debug("%s: s=$%u", prefix, fs->s->id);
  863: 	else
  864: 		log_debug("%s: s=none", prefix);
  865: 	if (fs->wl != NULL) {
  866: 		log_debug("%s: wl=%u %d w=@%u %s", prefix, fs->wl->idx,
  867: 		    fs->wl->window == fs->w, fs->w->id, fs->w->name);
  868: 	} else
  869: 		log_debug("%s: wl=none", prefix);
  870: 	if (fs->wp != NULL)
  871: 		log_debug("%s: wp=%%%u", prefix, fs->wp->id);
  872: 	else
  873: 		log_debug("%s: wp=none", prefix);
  874: 	if (fs->idx != -1)
  875: 		log_debug("%s: idx=%d", prefix, fs->idx);
  876: 	else
  877: 		log_debug("%s: idx=none", prefix);
  878: }
  879: 
  880: /* Find state from a session. */
  881: int
  882: cmd_find_from_session(struct cmd_find_state *fs, struct session *s)
  883: {
  884: 	cmd_find_clear_state(fs, NULL, 0);
  885: 
  886: 	fs->s = s;
  887: 	fs->wl = fs->s->curw;
  888: 	fs->w = fs->wl->window;
  889: 	fs->wp = fs->w->active;
  890: 
  891: 	cmd_find_log_state(__func__, fs);
  892: 	return (0);
  893: }
  894: 
  895: /* Find state from a winlink. */
  896: int
  897: cmd_find_from_winlink(struct cmd_find_state *fs, struct session *s,
  898:     struct winlink *wl)
  899: {
  900: 	cmd_find_clear_state(fs, NULL, 0);
  901: 
  902: 	fs->s = s;
  903: 	fs->wl = wl;
  904: 	fs->w = wl->window;
  905: 	fs->wp = wl->window->active;
  906: 
  907: 	cmd_find_log_state(__func__, fs);
  908: 	return (0);
  909: }
  910: 
  911: /* Find state from a session and window. */
  912: int
  913: cmd_find_from_session_window(struct cmd_find_state *fs, struct session *s,
  914:     struct window *w)
  915: {
  916: 	cmd_find_clear_state(fs, NULL, 0);
  917: 
  918: 	fs->s = s;
  919: 	fs->w = w;
  920: 	if (cmd_find_best_winlink_with_window(fs) != 0)
  921: 		return (-1);
  922: 	fs->wp = fs->w->active;
  923: 
  924: 	cmd_find_log_state(__func__, fs);
  925: 	return (0);
  926: }
  927: 
  928: /* Find state from a window. */
  929: int
  930: cmd_find_from_window(struct cmd_find_state *fs, struct window *w)
  931: {
  932: 	cmd_find_clear_state(fs, NULL, 0);
  933: 
  934: 	fs->w = w;
  935: 	if (cmd_find_best_session_with_window(fs) != 0)
  936: 		return (-1);
  937: 	if (cmd_find_best_winlink_with_window(fs) != 0)
  938: 		return (-1);
  939: 	fs->wp = fs->w->active;
  940: 
  941: 	cmd_find_log_state(__func__, fs);
  942: 	return (0);
  943: }
  944: 
  945: /* Find state from a pane. */
  946: int
  947: cmd_find_from_pane(struct cmd_find_state *fs, struct window_pane *wp)
  948: {
  949: 	if (cmd_find_from_window(fs, wp->window) != 0)
  950: 		return (-1);
  951: 	if (window_pane_outside(wp))
  952: 		return (-1);
  953: 	fs->wp = wp;
  954: 
  955: 	cmd_find_log_state(__func__, fs);
  956: 	return (0);
  957: }
  958: 
  959: /* Find current state. */
  960: int
  961: cmd_find_current(struct cmd_find_state *fs, struct cmdq_item *item, int flags)
  962: {
  963: 	cmd_find_clear_state(fs, item, flags);
  964: 	if (cmd_find_current_session(fs) != 0) {
  965: 		if (~flags & CMD_FIND_QUIET)
  966: 			cmdq_error(item, "no current session");
  967: 		return (-1);
  968: 	}
  969: 	return (0);
  970: }
  971: 
  972: /*
  973:  * Split target into pieces and resolve for the given type. Fills in the given
  974:  * state. Returns 0 on success or -1 on error.
  975:  */
  976: int
  977: cmd_find_target(struct cmd_find_state *fs, struct cmd_find_state *current,
  978:     struct cmdq_item *item, const char *target, enum cmd_find_type type,
  979:     int flags)
  980: {
  981: 	struct mouse_event	*m;
  982: 	char			*colon, *period, *copy = NULL;
  983: 	const char		*session, *window, *pane;
  984: 	int			 window_only = 0, pane_only = 0;
  985: 
  986: 	/* Log the arguments. */
  987: 	if (target == NULL)
  988: 		log_debug("%s: target none, type %d", __func__, type);
  989: 	else
  990: 		log_debug("%s: target %s, type %d", __func__, target, type);
  991: 	log_debug("%s: item %p, flags %#x", __func__, item, flags);
  992: 
  993: 	/* Clear new state. */
  994: 	cmd_find_clear_state(fs, item, flags);
  995: 
  996: 	/* Find current state. */
  997: 	if (server_check_marked() && (flags & CMD_FIND_DEFAULT_MARKED)) {
  998: 		fs->current = &marked_pane;
  999: 		log_debug("%s: current is marked pane", __func__);
 1000: 	} else if (cmd_find_valid_state(&item->current)) {
 1001: 		fs->current = &item->current;
 1002: 		log_debug("%s: current is from queue", __func__);
 1003: 	} else {
 1004: 		fs->current = current;
 1005: 		log_debug("%s: current is from argument", __func__);
 1006: 	}
 1007: 	if (!cmd_find_empty_state(fs->current) &&
 1008: 	    !cmd_find_valid_state(fs->current))
 1009: 		fatalx("invalid current find state");
 1010: 
 1011: 	/* An empty or NULL target is the current. */
 1012: 	if (target == NULL || *target == '\0')
 1013: 		goto current;
 1014: 
 1015: 	/* Mouse target is a plain = or {mouse}. */
 1016: 	if (strcmp(target, "=") == 0 || strcmp(target, "{mouse}") == 0) {
 1017: 		m = &item->mouse;
 1018: 		switch (type) {
 1019: 		case CMD_FIND_PANE:
 1020: 			fs->wp = cmd_mouse_pane(m, &fs->s, &fs->wl);
 1021: 			if (fs->wp != NULL && !window_pane_outside(fs->wp))
 1022: 				fs->w = fs->wl->window;
 1023: 			break;
 1024: 		case CMD_FIND_WINDOW:
 1025: 		case CMD_FIND_SESSION:
 1026: 			fs->wl = cmd_mouse_window(m, &fs->s);
 1027: 			if (fs->wl != NULL) {
 1028: 				fs->w = fs->wl->window;
 1029: 				fs->wp = fs->w->active;
 1030: 			}
 1031: 			break;
 1032: 		}
 1033: 		if (fs->wp == NULL) {
 1034: 			if (~flags & CMD_FIND_QUIET)
 1035: 				cmdq_error(item, "no mouse target");
 1036: 			goto error;
 1037: 		}
 1038: 		goto found;
 1039: 	}
 1040: 
 1041: 	/* Marked target is a plain ~ or {marked}. */
 1042: 	if (strcmp(target, "~") == 0 || strcmp(target, "{marked}") == 0) {
 1043: 		if (!server_check_marked()) {
 1044: 			if (~flags & CMD_FIND_QUIET)
 1045: 				cmdq_error(item, "no marked target");
 1046: 			goto error;
 1047: 		}
 1048: 		cmd_find_copy_state(fs, &marked_pane);
 1049: 		goto found;
 1050: 	}
 1051: 
 1052: 	/* Find separators if they exist. */
 1053: 	copy = xstrdup(target);
 1054: 	colon = strchr(copy, ':');
 1055: 	if (colon != NULL)
 1056: 		*colon++ = '\0';
 1057: 	if (colon == NULL)
 1058: 		period = strchr(copy, '.');
 1059: 	else
 1060: 		period = strchr(colon, '.');
 1061: 	if (period != NULL)
 1062: 		*period++ = '\0';
 1063: 
 1064: 	/* Set session, window and pane parts. */
 1065: 	session = window = pane = NULL;
 1066: 	if (colon != NULL && period != NULL) {
 1067: 		session = copy;
 1068: 		window = colon;
 1069: 		window_only = 1;
 1070: 		pane = period;
 1071: 		pane_only = 1;
 1072: 	} else if (colon != NULL && period == NULL) {
 1073: 		session = copy;
 1074: 		window = colon;
 1075: 		window_only = 1;
 1076: 	} else if (colon == NULL && period != NULL) {
 1077: 		window = copy;
 1078: 		pane = period;
 1079: 		pane_only = 1;
 1080: 	} else {
 1081: 		if (*copy == '$')
 1082: 			session = copy;
 1083: 		else if (*copy == '@')
 1084: 			window = copy;
 1085: 		else if (*copy == '%')
 1086: 			pane = copy;
 1087: 		else {
 1088: 			switch (type) {
 1089: 			case CMD_FIND_SESSION:
 1090: 				session = copy;
 1091: 				break;
 1092: 			case CMD_FIND_WINDOW:
 1093: 				window = copy;
 1094: 				break;
 1095: 			case CMD_FIND_PANE:
 1096: 				pane = copy;
 1097: 				break;
 1098: 			}
 1099: 		}
 1100: 	}
 1101: 
 1102: 	/* Set exact match flags. */
 1103: 	if (session != NULL && *session == '=') {
 1104: 		session++;
 1105: 		fs->flags |= CMD_FIND_EXACT_SESSION;
 1106: 	}
 1107: 	if (window != NULL && *window == '=') {
 1108: 		window++;
 1109: 		fs->flags |= CMD_FIND_EXACT_WINDOW;
 1110: 	}
 1111: 
 1112: 	/* Empty is the same as NULL. */
 1113: 	if (session != NULL && *session == '\0')
 1114: 		session = NULL;
 1115: 	if (window != NULL && *window == '\0')
 1116: 		window = NULL;
 1117: 	if (pane != NULL && *pane == '\0')
 1118: 		pane = NULL;
 1119: 
 1120: 	/* Map though conversion table. */
 1121: 	if (session != NULL)
 1122: 		session = cmd_find_map_table(cmd_find_session_table, session);
 1123: 	if (window != NULL)
 1124: 		window = cmd_find_map_table(cmd_find_window_table, window);
 1125: 	if (pane != NULL)
 1126: 		pane = cmd_find_map_table(cmd_find_pane_table, pane);
 1127: 
 1128: 	log_debug("target %s (flags %#x): session=%s, window=%s, pane=%s",
 1129: 	    target, flags, session == NULL ? "none" : session,
 1130: 	    window == NULL ? "none" : window, pane == NULL ? "none" : pane);
 1131: 
 1132: 	/* No pane is allowed if want an index. */
 1133: 	if (pane != NULL && (flags & CMD_FIND_WINDOW_INDEX)) {
 1134: 		if (~flags & CMD_FIND_QUIET)
 1135: 			cmdq_error(item, "can't specify pane here");
 1136: 		goto error;
 1137: 	}
 1138: 
 1139: 	/* If the session isn't NULL, look it up. */
 1140: 	if (session != NULL) {
 1141: 		/* This will fill in session. */
 1142: 		if (cmd_find_get_session(fs, session) != 0)
 1143: 			goto no_session;
 1144: 
 1145: 		/* If window and pane are NULL, use that session's current. */
 1146: 		if (window == NULL && pane == NULL) {
 1147: 			fs->wl = fs->s->curw;
 1148: 			fs->idx = -1;
 1149: 			fs->w = fs->wl->window;
 1150: 			fs->wp = fs->w->active;
 1151: 			goto found;
 1152: 		}
 1153: 
 1154: 		/* If window is present but pane not, find window in session. */
 1155: 		if (window != NULL && pane == NULL) {
 1156: 			/* This will fill in winlink and window. */
 1157: 			if (cmd_find_get_window_with_session(fs, window) != 0)
 1158: 				goto no_window;
 1159: 			fs->wp = fs->wl->window->active;
 1160: 			goto found;
 1161: 		}
 1162: 
 1163: 		/* If pane is present but window not, find pane. */
 1164: 		if (window == NULL && pane != NULL) {
 1165: 			/* This will fill in winlink and window and pane. */
 1166: 			if (cmd_find_get_pane_with_session(fs, pane) != 0)
 1167: 				goto no_pane;
 1168: 			goto found;
 1169: 		}
 1170: 
 1171: 		/*
 1172: 		 * If window and pane are present, find both in session. This
 1173: 		 * will fill in winlink and window.
 1174: 		 */
 1175: 		if (cmd_find_get_window_with_session(fs, window) != 0)
 1176: 			goto no_window;
 1177: 		/* This will fill in pane. */
 1178: 		if (cmd_find_get_pane_with_window(fs, pane) != 0)
 1179: 			goto no_pane;
 1180: 		goto found;
 1181: 	}
 1182: 
 1183: 	/* No session. If window and pane, try them. */
 1184: 	if (window != NULL && pane != NULL) {
 1185: 		/* This will fill in session, winlink and window. */
 1186: 		if (cmd_find_get_window(fs, window, window_only) != 0)
 1187: 			goto no_window;
 1188: 		/* This will fill in pane. */
 1189: 		if (cmd_find_get_pane_with_window(fs, pane) != 0)
 1190: 			goto no_pane;
 1191: 		goto found;
 1192: 	}
 1193: 
 1194: 	/* If just window is present, try it. */
 1195: 	if (window != NULL && pane == NULL) {
 1196: 		/* This will fill in session, winlink and window. */
 1197: 		if (cmd_find_get_window(fs, window, window_only) != 0)
 1198: 			goto no_window;
 1199: 		fs->wp = fs->wl->window->active;
 1200: 		goto found;
 1201: 	}
 1202: 
 1203: 	/* If just pane is present, try it. */
 1204: 	if (window == NULL && pane != NULL) {
 1205: 		/* This will fill in session, winlink, window and pane. */
 1206: 		if (cmd_find_get_pane(fs, pane, pane_only) != 0)
 1207: 			goto no_pane;
 1208: 		goto found;
 1209: 	}
 1210: 
 1211: current:
 1212: 	/* Use the current session. */
 1213: 	cmd_find_copy_state(fs, fs->current);
 1214: 	if (flags & CMD_FIND_WINDOW_INDEX)
 1215: 		fs->idx = -1;
 1216: 	goto found;
 1217: 
 1218: error:
 1219: 	fs->current = NULL;
 1220: 	log_debug("%s: error", __func__);
 1221: 
 1222: 	free(copy);
 1223: 	return (-1);
 1224: 
 1225: found:
 1226: 	fs->current = NULL;
 1227: 	cmd_find_log_state(__func__, fs);
 1228: 
 1229: 	free(copy);
 1230: 	return (0);
 1231: 
 1232: no_session:
 1233: 	if (~flags & CMD_FIND_QUIET)
 1234: 		cmdq_error(item, "can't find session %s", session);
 1235: 	goto error;
 1236: 
 1237: no_window:
 1238: 	if (~flags & CMD_FIND_QUIET)
 1239: 		cmdq_error(item, "can't find window %s", window);
 1240: 	goto error;
 1241: 
 1242: no_pane:
 1243: 	if (~flags & CMD_FIND_QUIET)
 1244: 		cmdq_error(item, "can't find pane %s", pane);
 1245: 	goto error;
 1246: }
 1247: 
 1248: /* Find the target client or report an error and return NULL. */
 1249: struct client *
 1250: cmd_find_client(struct cmdq_item *item, const char *target, int quiet)
 1251: {
 1252: 	struct client	*c;
 1253: 	char		*copy;
 1254: 	size_t		 size;
 1255: 
 1256: 	/* A NULL argument means the current client. */
 1257: 	if (item != NULL && target == NULL) {
 1258: 		c = cmd_find_current_client(item);
 1259: 		if (c == NULL && !quiet)
 1260: 			cmdq_error(item, "no current client");
 1261: 		log_debug("%s: no target, return %p", __func__, c);
 1262: 		return (c);
 1263: 	}
 1264: 	copy = xstrdup(target);
 1265: 
 1266: 	/* Trim a single trailing colon if any. */
 1267: 	size = strlen(copy);
 1268: 	if (size != 0 && copy[size - 1] == ':')
 1269: 		copy[size - 1] = '\0';
 1270: 
 1271: 	/* Check name and path of each client. */
 1272: 	TAILQ_FOREACH(c, &clients, entry) {
 1273: 		if (c->session == NULL)
 1274: 			continue;
 1275: 		if (strcmp(copy, c->name) == 0)
 1276: 			break;
 1277: 
 1278: 		if (*c->ttyname == '\0')
 1279: 			continue;
 1280: 		if (strcmp(copy, c->ttyname) == 0)
 1281: 			break;
 1282: 		if (strncmp(c->ttyname, _PATH_DEV, (sizeof _PATH_DEV) - 1) != 0)
 1283: 			continue;
 1284: 		if (strcmp(copy, c->ttyname + (sizeof _PATH_DEV) - 1) == 0)
 1285: 			break;
 1286: 	}
 1287: 
 1288: 	/* If no client found, report an error. */
 1289: 	if (c == NULL && !quiet)
 1290: 		cmdq_error(item, "can't find client %s", copy);
 1291: 
 1292: 	free(copy);
 1293: 	log_debug("%s: target %s, return %p", __func__, target, c);
 1294: 	return (c);
 1295: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>