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