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