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