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 *
cmd_find_inside_pane(struct client * c)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
cmd_find_client_better(struct client * c,struct client * than)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 *
cmd_find_best_client(struct session * s)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
cmd_find_session_better(struct session * s,struct session * than,int flags)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 *
cmd_find_best_session(struct session ** slist,u_int ssize,int flags)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
cmd_find_best_session_with_window(struct cmd_find_state * fs)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
cmd_find_best_winlink_with_window(struct cmd_find_state * fs)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 *
cmd_find_map_table(const char * table[][2],const char * s)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
cmd_find_get_session(struct cmd_find_state * fs,const char * session)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
cmd_find_get_window(struct cmd_find_state * fs,const char * window,int only)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
cmd_find_get_window_with_session(struct cmd_find_state * fs,const char * window)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
cmd_find_get_pane(struct cmd_find_state * fs,const char * pane,int only)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
cmd_find_get_pane_with_session(struct cmd_find_state * fs,const char * pane)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
cmd_find_get_pane_with_window(struct cmd_find_state * fs,const char * pane)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->current->wp);
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->current->wp);
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->current->wp);
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->current->wp);
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->current->wp;
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
cmd_find_clear_state(struct cmd_find_state * fs,int flags)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
cmd_find_empty_state(struct cmd_find_state * fs)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
cmd_find_valid_state(struct cmd_find_state * fs)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
cmd_find_copy_state(struct cmd_find_state * dst,struct cmd_find_state * src)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
cmd_find_log_state(const char * prefix,struct cmd_find_state * fs)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
cmd_find_from_session(struct cmd_find_state * fs,struct session * s,int flags)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
cmd_find_from_winlink(struct cmd_find_state * fs,struct winlink * wl,int flags)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
cmd_find_from_session_window(struct cmd_find_state * fs,struct session * s,struct window * w,int flags)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
cmd_find_from_window(struct cmd_find_state * fs,struct window * w,int flags)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
cmd_find_from_winlink_pane(struct cmd_find_state * fs,struct winlink * wl,struct window_pane * wp,int flags)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
cmd_find_from_pane(struct cmd_find_state * fs,struct window_pane * wp,int flags)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
cmd_find_from_nothing(struct cmd_find_state * fs,int flags)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
cmd_find_from_mouse(struct cmd_find_state * fs,struct mouse_event * m,int flags)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
cmd_find_from_client(struct cmd_find_state * fs,struct client * c,int flags)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_clear_state(fs, flags);
870 
871 		fs->wp = server_client_get_pane(c);
872 		if (fs->wp == NULL) {
873 			cmd_find_from_session(fs, c->session, flags);
874 			return (0);
875 		}
876 		fs->s = c->session;
877 		fs->wl = fs->s->curw;
878 		fs->w = fs->wl->window;
879 
880 		cmd_find_log_state(__func__, fs);
881 		return (0);
882 	}
883 	cmd_find_clear_state(fs, flags);
884 
885 	/*
886 	 * If this is an unattached client running in a pane, we can use that
887 	 * to limit the list of sessions to those containing that pane.
888 	 */
889 	wp = cmd_find_inside_pane(c);
890 	if (wp == NULL)
891 		goto unknown_pane;
892 
893 	/*
894 	 * Don't have a session, or it doesn't have this pane. Try all
895 	 * sessions.
896 	 */
897 	fs->w = wp->window;
898 	if (cmd_find_best_session_with_window(fs) != 0) {
899 		/*
900 		 * The window may have been destroyed but the pane
901 		 * still on all_window_panes due to something else
902 		 * holding a reference.
903 		 */
904 		goto unknown_pane;
905 	}
906 	fs->wl = fs->s->curw;
907 	fs->w = fs->wl->window;
908 	fs->wp = fs->w->active; /* use active pane */
909 
910 	cmd_find_log_state(__func__, fs);
911 	return (0);
912 
913 unknown_pane:
914 	/* We can't find the pane so need to guess. */
915 	return (cmd_find_from_nothing(fs, flags));
916 }
917 
918 /*
919  * Split target into pieces and resolve for the given type. Fills in the given
920  * state. Returns 0 on success or -1 on error.
921  */
922 int
cmd_find_target(struct cmd_find_state * fs,struct cmdq_item * item,const char * target,enum cmd_find_type type,int flags)923 cmd_find_target(struct cmd_find_state *fs, struct cmdq_item *item,
924     const char *target, enum cmd_find_type type, int flags)
925 {
926 	struct mouse_event	*m;
927 	struct cmd_find_state	 current;
928 	char			*colon, *period, *copy = NULL, tmp[256];
929 	const char		*session, *window, *pane, *s;
930 	int			 window_only = 0, pane_only = 0;
931 
932 	/* Can fail flag implies quiet. */
933 	if (flags & CMD_FIND_CANFAIL)
934 		flags |= CMD_FIND_QUIET;
935 
936 	/* Log the arguments. */
937 	if (type == CMD_FIND_PANE)
938 		s = "pane";
939 	else if (type == CMD_FIND_WINDOW)
940 		s = "window";
941 	else if (type == CMD_FIND_SESSION)
942 		s = "session";
943 	else
944 		s = "unknown";
945 	*tmp = '\0';
946 	if (flags & CMD_FIND_PREFER_UNATTACHED)
947 		strlcat(tmp, "PREFER_UNATTACHED,", sizeof tmp);
948 	if (flags & CMD_FIND_QUIET)
949 		strlcat(tmp, "QUIET,", sizeof tmp);
950 	if (flags & CMD_FIND_WINDOW_INDEX)
951 		strlcat(tmp, "WINDOW_INDEX,", sizeof tmp);
952 	if (flags & CMD_FIND_DEFAULT_MARKED)
953 		strlcat(tmp, "DEFAULT_MARKED,", sizeof tmp);
954 	if (flags & CMD_FIND_EXACT_SESSION)
955 		strlcat(tmp, "EXACT_SESSION,", sizeof tmp);
956 	if (flags & CMD_FIND_EXACT_WINDOW)
957 		strlcat(tmp, "EXACT_WINDOW,", sizeof tmp);
958 	if (flags & CMD_FIND_CANFAIL)
959 		strlcat(tmp, "CANFAIL,", sizeof tmp);
960 	if (*tmp != '\0')
961 		tmp[strlen(tmp) - 1] = '\0';
962 	else
963 		strlcat(tmp, "NONE", sizeof tmp);
964 	log_debug("%s: target %s, type %s, item %p, flags %s", __func__,
965 	    target == NULL ? "none" : target, s, item, tmp);
966 
967 	/* Clear new state. */
968 	cmd_find_clear_state(fs, flags);
969 
970 	/* Find current state. */
971 	if (server_check_marked() && (flags & CMD_FIND_DEFAULT_MARKED)) {
972 		fs->current = &marked_pane;
973 		log_debug("%s: current is marked pane", __func__);
974 	} else if (cmd_find_valid_state(cmdq_get_current(item))) {
975 		fs->current = cmdq_get_current(item);
976 		log_debug("%s: current is from queue", __func__);
977 	} else if (cmd_find_from_client(&current, cmdq_get_client(item),
978 	    flags) == 0) {
979 		fs->current = &current;
980 		log_debug("%s: current is from client", __func__);
981 	} else {
982 		if (~flags & CMD_FIND_QUIET)
983 			cmdq_error(item, "no current target");
984 		goto error;
985 	}
986 	if (!cmd_find_valid_state(fs->current))
987 		fatalx("invalid current find state");
988 
989 	/* An empty or NULL target is the current. */
990 	if (target == NULL || *target == '\0')
991 		goto current;
992 
993 	/* Mouse target is a plain = or {mouse}. */
994 	if (strcmp(target, "=") == 0 || strcmp(target, "{mouse}") == 0) {
995 		m = &cmdq_get_event(item)->m;
996 		switch (type) {
997 		case CMD_FIND_PANE:
998 			fs->wp = cmd_mouse_pane(m, &fs->s, &fs->wl);
999 			if (fs->wp != NULL) {
1000 				fs->w = fs->wl->window;
1001 				break;
1002 			}
1003 			/* FALLTHROUGH */
1004 		case CMD_FIND_WINDOW:
1005 		case CMD_FIND_SESSION:
1006 			fs->wl = cmd_mouse_window(m, &fs->s);
1007 			if (fs->wl == NULL && fs->s != NULL)
1008 				fs->wl = fs->s->curw;
1009 			if (fs->wl != NULL) {
1010 				fs->w = fs->wl->window;
1011 				fs->wp = fs->w->active;
1012 			}
1013 			break;
1014 		}
1015 		if (fs->wp == NULL) {
1016 			if (~flags & CMD_FIND_QUIET)
1017 				cmdq_error(item, "no mouse target");
1018 			goto error;
1019 		}
1020 		goto found;
1021 	}
1022 
1023 	/* Marked target is a plain ~ or {marked}. */
1024 	if (strcmp(target, "~") == 0 || strcmp(target, "{marked}") == 0) {
1025 		if (!server_check_marked()) {
1026 			if (~flags & CMD_FIND_QUIET)
1027 				cmdq_error(item, "no marked target");
1028 			goto error;
1029 		}
1030 		cmd_find_copy_state(fs, &marked_pane);
1031 		goto found;
1032 	}
1033 
1034 	/* Find separators if they exist. */
1035 	copy = xstrdup(target);
1036 	colon = strchr(copy, ':');
1037 	if (colon != NULL)
1038 		*colon++ = '\0';
1039 	if (colon == NULL)
1040 		period = strchr(copy, '.');
1041 	else
1042 		period = strchr(colon, '.');
1043 	if (period != NULL)
1044 		*period++ = '\0';
1045 
1046 	/* Set session, window and pane parts. */
1047 	session = window = pane = NULL;
1048 	if (colon != NULL && period != NULL) {
1049 		session = copy;
1050 		window = colon;
1051 		window_only = 1;
1052 		pane = period;
1053 		pane_only = 1;
1054 	} else if (colon != NULL && period == NULL) {
1055 		session = copy;
1056 		window = colon;
1057 		window_only = 1;
1058 	} else if (colon == NULL && period != NULL) {
1059 		window = copy;
1060 		pane = period;
1061 		pane_only = 1;
1062 	} else {
1063 		if (*copy == '$')
1064 			session = copy;
1065 		else if (*copy == '@')
1066 			window = copy;
1067 		else if (*copy == '%')
1068 			pane = copy;
1069 		else {
1070 			switch (type) {
1071 			case CMD_FIND_SESSION:
1072 				session = copy;
1073 				break;
1074 			case CMD_FIND_WINDOW:
1075 				window = copy;
1076 				break;
1077 			case CMD_FIND_PANE:
1078 				pane = copy;
1079 				break;
1080 			}
1081 		}
1082 	}
1083 
1084 	/* Set exact match flags. */
1085 	if (session != NULL && *session == '=') {
1086 		session++;
1087 		fs->flags |= CMD_FIND_EXACT_SESSION;
1088 	}
1089 	if (window != NULL && *window == '=') {
1090 		window++;
1091 		fs->flags |= CMD_FIND_EXACT_WINDOW;
1092 	}
1093 
1094 	/* Empty is the same as NULL. */
1095 	if (session != NULL && *session == '\0')
1096 		session = NULL;
1097 	if (window != NULL && *window == '\0')
1098 		window = NULL;
1099 	if (pane != NULL && *pane == '\0')
1100 		pane = NULL;
1101 
1102 	/* Map though conversion table. */
1103 	if (session != NULL)
1104 		session = cmd_find_map_table(cmd_find_session_table, session);
1105 	if (window != NULL)
1106 		window = cmd_find_map_table(cmd_find_window_table, window);
1107 	if (pane != NULL)
1108 		pane = cmd_find_map_table(cmd_find_pane_table, pane);
1109 
1110 	if (session != NULL || window != NULL || pane != NULL) {
1111 		log_debug("%s: target %s is %s%s%s%s%s%s",
1112 		    __func__, target,
1113 		    session == NULL ? "" : "session ",
1114 		    session == NULL ? "" : session,
1115 		    window == NULL ? "" : "window ",
1116 		    window == NULL ? "" : window,
1117 		    pane == NULL ? "" : "pane ",
1118 		    pane == NULL ? "" : pane);
1119 	}
1120 
1121 	/* No pane is allowed if want an index. */
1122 	if (pane != NULL && (flags & CMD_FIND_WINDOW_INDEX)) {
1123 		if (~flags & CMD_FIND_QUIET)
1124 			cmdq_error(item, "can't specify pane here");
1125 		goto error;
1126 	}
1127 
1128 	/* If the session isn't NULL, look it up. */
1129 	if (session != NULL) {
1130 		/* This will fill in session. */
1131 		if (cmd_find_get_session(fs, session) != 0)
1132 			goto no_session;
1133 
1134 		/* If window and pane are NULL, use that session's current. */
1135 		if (window == NULL && pane == NULL) {
1136 			fs->wl = fs->s->curw;
1137 			fs->idx = -1;
1138 			fs->w = fs->wl->window;
1139 			fs->wp = fs->w->active;
1140 			goto found;
1141 		}
1142 
1143 		/* If window is present but pane not, find window in session. */
1144 		if (window != NULL && pane == NULL) {
1145 			/* This will fill in winlink and window. */
1146 			if (cmd_find_get_window_with_session(fs, window) != 0)
1147 				goto no_window;
1148 			if (fs->wl != NULL) /* can be NULL if index only */
1149 				fs->wp = fs->wl->window->active;
1150 			goto found;
1151 		}
1152 
1153 		/* If pane is present but window not, find pane. */
1154 		if (window == NULL && pane != NULL) {
1155 			/* This will fill in winlink and window and pane. */
1156 			if (cmd_find_get_pane_with_session(fs, pane) != 0)
1157 				goto no_pane;
1158 			goto found;
1159 		}
1160 
1161 		/*
1162 		 * If window and pane are present, find both in session. This
1163 		 * will fill in winlink and window.
1164 		 */
1165 		if (cmd_find_get_window_with_session(fs, window) != 0)
1166 			goto no_window;
1167 		/* This will fill in pane. */
1168 		if (cmd_find_get_pane_with_window(fs, pane) != 0)
1169 			goto no_pane;
1170 		goto found;
1171 	}
1172 
1173 	/* No session. If window and pane, try them. */
1174 	if (window != NULL && pane != NULL) {
1175 		/* This will fill in session, winlink and window. */
1176 		if (cmd_find_get_window(fs, window, window_only) != 0)
1177 			goto no_window;
1178 		/* This will fill in pane. */
1179 		if (cmd_find_get_pane_with_window(fs, pane) != 0)
1180 			goto no_pane;
1181 		goto found;
1182 	}
1183 
1184 	/* If just window is present, try it. */
1185 	if (window != NULL && pane == NULL) {
1186 		/* This will fill in session, winlink and window. */
1187 		if (cmd_find_get_window(fs, window, window_only) != 0)
1188 			goto no_window;
1189 		if (fs->wl != NULL) /* can be NULL if index only */
1190 			fs->wp = fs->wl->window->active;
1191 		goto found;
1192 	}
1193 
1194 	/* If just pane is present, try it. */
1195 	if (window == NULL && pane != NULL) {
1196 		/* This will fill in session, winlink, window and pane. */
1197 		if (cmd_find_get_pane(fs, pane, pane_only) != 0)
1198 			goto no_pane;
1199 		goto found;
1200 	}
1201 
1202 current:
1203 	/* Use the current session. */
1204 	cmd_find_copy_state(fs, fs->current);
1205 	if (flags & CMD_FIND_WINDOW_INDEX)
1206 		fs->idx = -1;
1207 	goto found;
1208 
1209 error:
1210 	fs->current = NULL;
1211 	log_debug("%s: error", __func__);
1212 
1213 	free(copy);
1214 	if (flags & CMD_FIND_CANFAIL)
1215 		return (0);
1216 	return (-1);
1217 
1218 found:
1219 	fs->current = NULL;
1220 	cmd_find_log_state(__func__, fs);
1221 
1222 	free(copy);
1223 	return (0);
1224 
1225 no_session:
1226 	if (~flags & CMD_FIND_QUIET)
1227 		cmdq_error(item, "can't find session: %s", session);
1228 	goto error;
1229 
1230 no_window:
1231 	if (~flags & CMD_FIND_QUIET)
1232 		cmdq_error(item, "can't find window: %s", window);
1233 	goto error;
1234 
1235 no_pane:
1236 	if (~flags & CMD_FIND_QUIET)
1237 		cmdq_error(item, "can't find pane: %s", pane);
1238 	goto error;
1239 }
1240 
1241 /* Find the current client. */
1242 static struct client *
cmd_find_current_client(struct cmdq_item * item,int quiet)1243 cmd_find_current_client(struct cmdq_item *item, int quiet)
1244 {
1245 	struct client		*c = NULL, *found;
1246 	struct session		*s;
1247 	struct window_pane	*wp;
1248 	struct cmd_find_state	 fs;
1249 
1250 	if (item != NULL)
1251 		c = cmdq_get_client(item);
1252 	if (c != NULL && c->session != NULL)
1253 		return (c);
1254 
1255 	found = NULL;
1256 	if (c != NULL && (wp = cmd_find_inside_pane(c)) != NULL) {
1257 		cmd_find_clear_state(&fs, CMD_FIND_QUIET);
1258 		fs.w = wp->window;
1259 		if (cmd_find_best_session_with_window(&fs) == 0)
1260 			found = cmd_find_best_client(fs.s);
1261 	} else {
1262 		s = cmd_find_best_session(NULL, 0, CMD_FIND_QUIET);
1263 		if (s != NULL)
1264 			found = cmd_find_best_client(s);
1265 	}
1266 	if (found == NULL && item != NULL && !quiet)
1267 		cmdq_error(item, "no current client");
1268 	log_debug("%s: no target, return %p", __func__, found);
1269 	return (found);
1270 }
1271 
1272 /* Find the target client or report an error and return NULL. */
1273 struct client *
cmd_find_client(struct cmdq_item * item,const char * target,int quiet)1274 cmd_find_client(struct cmdq_item *item, const char *target, int quiet)
1275 {
1276 	struct client	*c;
1277 	char		*copy;
1278 	size_t		 size;
1279 
1280 	/* A NULL argument means the current client. */
1281 	if (target == NULL)
1282 		return (cmd_find_current_client(item, quiet));
1283 	copy = xstrdup(target);
1284 
1285 	/* Trim a single trailing colon if any. */
1286 	size = strlen(copy);
1287 	if (size != 0 && copy[size - 1] == ':')
1288 		copy[size - 1] = '\0';
1289 
1290 	/* Check name and path of each client. */
1291 	TAILQ_FOREACH(c, &clients, entry) {
1292 		if (c->session == NULL)
1293 			continue;
1294 		if (strcmp(copy, c->name) == 0)
1295 			break;
1296 
1297 		if (*c->ttyname == '\0')
1298 			continue;
1299 		if (strcmp(copy, c->ttyname) == 0)
1300 			break;
1301 		if (strncmp(c->ttyname, _PATH_DEV, (sizeof _PATH_DEV) - 1) != 0)
1302 			continue;
1303 		if (strcmp(copy, c->ttyname + (sizeof _PATH_DEV) - 1) == 0)
1304 			break;
1305 	}
1306 
1307 	/* If no client found, report an error. */
1308 	if (c == NULL && !quiet)
1309 		cmdq_error(item, "can't find client: %s", copy);
1310 
1311 	free(copy);
1312 	log_debug("%s: target %s, return %p", __func__, target, c);
1313 	return (c);
1314 }
1315