Annotation of src/usr.bin/tmux/cmd-find.c, Revision 1.53
1.53 ! nicm 1: /* $OpenBSD: cmd-find.c,v 1.52 2017/04/22 12:08:41 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;
1.49 nicm 752: if (cmd_find_best_winlink_with_window(fs) != 0) {
753: cmd_find_clear_state(fs, 0);
1.35 nicm 754: return (-1);
1.49 nicm 755: }
1.35 nicm 756: fs->wp = fs->w->active;
757:
758: cmd_find_log_state(__func__, fs);
759: return (0);
760: }
761:
1.27 nicm 762: /* Find state from a window. */
763: int
764: cmd_find_from_window(struct cmd_find_state *fs, struct window *w)
765: {
1.48 nicm 766: cmd_find_clear_state(fs, 0);
1.27 nicm 767:
768: fs->w = w;
1.49 nicm 769: if (cmd_find_best_session_with_window(fs) != 0) {
770: cmd_find_clear_state(fs, 0);
1.27 nicm 771: return (-1);
1.49 nicm 772: }
773: if (cmd_find_best_winlink_with_window(fs) != 0) {
774: cmd_find_clear_state(fs, 0);
1.27 nicm 775: return (-1);
1.49 nicm 776: }
1.35 nicm 777: fs->wp = fs->w->active;
1.27 nicm 778:
779: cmd_find_log_state(__func__, fs);
780: return (0);
1.47 nicm 781: }
782:
783: /* Find state from a winlink and pane. */
784: void
785: cmd_find_from_winlink_pane(struct cmd_find_state *fs, struct winlink *wl,
786: struct window_pane *wp)
787: {
1.48 nicm 788: cmd_find_clear_state(fs, 0);
1.47 nicm 789:
790: fs->s = wl->session;
791: fs->wl = wl;
792: fs->idx = fs->wl->idx;
793: fs->w = fs->wl->window;
794: fs->wp = wp;
795:
796: cmd_find_log_state(__func__, fs);
1.27 nicm 797: }
798:
799: /* Find state from a pane. */
800: int
801: cmd_find_from_pane(struct cmd_find_state *fs, struct window_pane *wp)
802: {
803: if (cmd_find_from_window(fs, wp->window) != 0)
804: return (-1);
1.49 nicm 805: if (window_pane_outside(wp)) {
806: cmd_find_clear_state(fs, 0);
1.40 nicm 807: return (-1);
1.49 nicm 808: }
1.27 nicm 809: fs->wp = wp;
810:
811: cmd_find_log_state(__func__, fs);
812: return (0);
1.23 nicm 813: }
814:
1.48 nicm 815: /* Find state from nothing. */
1.31 nicm 816: int
1.48 nicm 817: cmd_find_from_nothing(struct cmd_find_state *fs)
1.31 nicm 818: {
1.48 nicm 819: cmd_find_clear_state(fs, 0);
820:
821: fs->s = cmd_find_best_session(NULL, 0, fs->flags);
1.49 nicm 822: if (fs->s == NULL) {
823: cmd_find_clear_state(fs, 0);
1.48 nicm 824: return (-1);
1.49 nicm 825: }
1.48 nicm 826: fs->wl = fs->s->curw;
827: fs->idx = fs->wl->idx;
828: fs->w = fs->wl->window;
829: fs->wp = fs->w->active;
830:
831: cmd_find_log_state(__func__, fs);
832: return (0);
833: }
834:
835: /* Find state from mouse. */
836: int
837: cmd_find_from_mouse(struct cmd_find_state *fs, struct mouse_event *m)
838: {
839: cmd_find_clear_state(fs, 0);
840:
841: if (!m->valid)
842: return (-1);
843:
844: fs->wp = cmd_mouse_pane(m, &fs->s, &fs->wl);
1.49 nicm 845: if (fs->wp == NULL) {
846: cmd_find_clear_state(fs, 0);
1.48 nicm 847: return (-1);
1.49 nicm 848: }
1.48 nicm 849: fs->w = fs->wl->window;
850:
851: cmd_find_log_state(__func__, fs);
852: return (0);
853: }
854:
855: /* Find state from client. */
856: int
857: cmd_find_from_client(struct cmd_find_state *fs, struct client *c)
858: {
859: struct session *s;
860: struct winlink *wl;
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));
866:
867: /* If this is an attached client, all done. */
868: if (c->session != NULL) {
869: cmd_find_from_session(fs, c->session);
870: return (0);
871: }
872: cmd_find_clear_state(fs, 0);
873:
874: /*
875: * If this is an unattached client running in a pane, we can use that
876: * to limit the list of sessions to those containing that pane.
877: */
878: RB_FOREACH(wp, window_pane_tree, &all_window_panes) {
879: if (strcmp(wp->tty, c->ttyname) == 0)
880: break;
881: }
882: if (wp == NULL)
883: goto unknown_pane;
884:
885: /* If we have a session in TMUX, see if it has this pane. */
886: s = cmd_find_try_TMUX(c);
887: if (s != NULL) {
888: RB_FOREACH(wl, winlinks, &s->windows) {
889: if (window_has_pane(wl->window, wp))
890: break;
891: }
892: if (wl != NULL) {
893: fs->s = s;
1.52 nicm 894: fs->wl = s->curw; /* use current session */
1.48 nicm 895: fs->w = fs->wl->window;
896: fs->wp = fs->w->active; /* use active pane */
897:
898: cmd_find_log_state(__func__, fs);
899: return (0);
900: }
901: }
902:
903: /*
904: * Don't have a session, or it doesn't have this pane. Try all
905: * sessions.
906: */
907: fs->w = wp->window;
908: if (cmd_find_best_session_with_window(fs) != 0) {
909: if (wp != NULL) {
910: /*
911: * The window may have been destroyed but the pane
912: * still on all_window_panes due to something else
913: * holding a reference.
914: */
915: goto unknown_pane;
916: }
1.49 nicm 917: cmd_find_clear_state(fs, 0);
1.31 nicm 918: return (-1);
919: }
1.48 nicm 920: fs->wl = fs->s->curw;
921: fs->w = fs->wl->window;
922: fs->wp = fs->w->active; /* use active pane */
923:
924: cmd_find_log_state(__func__, fs);
1.31 nicm 925: return (0);
1.48 nicm 926:
927: unknown_pane:
928: /*
929: * We're not running in a known pane, but maybe this client has TMUX
930: * in the environment. That'd give us a session.
931: */
932: s = cmd_find_try_TMUX(c);
933: if (s != NULL) {
934: cmd_find_from_session(fs, s);
935: return (0);
936: }
937:
938: /* Otherwise we need to guess. */
939: return (cmd_find_from_nothing(fs));
1.31 nicm 940: }
941:
1.20 nicm 942: /*
943: * Split target into pieces and resolve for the given type. Fills in the given
944: * state. Returns 0 on success or -1 on error.
945: */
946: int
1.48 nicm 947: cmd_find_target(struct cmd_find_state *fs, struct cmdq_item *item,
948: const char *target, enum cmd_find_type type, int flags)
1.1 nicm 949: {
1.20 nicm 950: struct mouse_event *m;
1.48 nicm 951: struct cmd_find_state current;
1.20 nicm 952: char *colon, *period, *copy = NULL;
953: const char *session, *window, *pane;
1.44 nicm 954: int window_only = 0, pane_only = 0;
1.1 nicm 955:
1.51 nicm 956: /* Can fail flag implies quiet. */
957: if (flags & CMD_FIND_CANFAIL)
958: flags |= CMD_FIND_QUIET;
959:
1.18 nicm 960: /* Log the arguments. */
961: if (target == NULL)
962: log_debug("%s: target none, type %d", __func__, type);
963: else
964: log_debug("%s: target %s, type %d", __func__, target, type);
1.37 nicm 965: log_debug("%s: item %p, flags %#x", __func__, item, flags);
1.18 nicm 966:
1.24 nicm 967: /* Clear new state. */
1.48 nicm 968: cmd_find_clear_state(fs, flags);
1.24 nicm 969:
1.1 nicm 970: /* Find current state. */
1.34 nicm 971: if (server_check_marked() && (flags & CMD_FIND_DEFAULT_MARKED)) {
1.24 nicm 972: fs->current = &marked_pane;
1.39 nicm 973: log_debug("%s: current is marked pane", __func__);
1.45 nicm 974: } else if (cmd_find_valid_state(&item->shared->current)) {
975: fs->current = &item->shared->current;
1.39 nicm 976: log_debug("%s: current is from queue", __func__);
1.48 nicm 977: } else if (cmd_find_from_client(¤t, item->client) == 0) {
978: fs->current = ¤t;
979: log_debug("%s: current is from client", __func__);
1.53 ! nicm 980: } else {
! 981: if (~flags & CMD_FIND_QUIET)
! 982: cmdq_error(item, "no current target");
1.50 nicm 983: goto error;
1.53 ! nicm 984: }
1.50 nicm 985: if (!cmd_find_valid_state(fs->current))
1.34 nicm 986: fatalx("invalid current find state");
1.1 nicm 987:
988: /* An empty or NULL target is the current. */
989: if (target == NULL || *target == '\0')
990: goto current;
991:
992: /* Mouse target is a plain = or {mouse}. */
993: if (strcmp(target, "=") == 0 || strcmp(target, "{mouse}") == 0) {
1.45 nicm 994: m = &item->shared->mouse;
1.1 nicm 995: switch (type) {
996: case CMD_FIND_PANE:
1.20 nicm 997: fs->wp = cmd_mouse_pane(m, &fs->s, &fs->wl);
1.40 nicm 998: if (fs->wp != NULL && !window_pane_outside(fs->wp))
1.20 nicm 999: fs->w = fs->wl->window;
1.1 nicm 1000: break;
1001: case CMD_FIND_WINDOW:
1002: case CMD_FIND_SESSION:
1.20 nicm 1003: fs->wl = cmd_mouse_window(m, &fs->s);
1004: if (fs->wl != NULL) {
1005: fs->w = fs->wl->window;
1006: fs->wp = fs->w->active;
1.1 nicm 1007: }
1008: break;
1009: }
1.20 nicm 1010: if (fs->wp == NULL) {
1.1 nicm 1011: if (~flags & CMD_FIND_QUIET)
1.37 nicm 1012: cmdq_error(item, "no mouse target");
1.1 nicm 1013: goto error;
1014: }
1.18 nicm 1015: goto found;
1.1 nicm 1016: }
1.8 nicm 1017:
1018: /* Marked target is a plain ~ or {marked}. */
1019: if (strcmp(target, "~") == 0 || strcmp(target, "{marked}") == 0) {
1020: if (!server_check_marked()) {
1021: if (~flags & CMD_FIND_QUIET)
1.37 nicm 1022: cmdq_error(item, "no marked target");
1.8 nicm 1023: goto error;
1024: }
1.23 nicm 1025: cmd_find_copy_state(fs, &marked_pane);
1.18 nicm 1026: goto found;
1.8 nicm 1027: }
1.1 nicm 1028:
1029: /* Find separators if they exist. */
1.8 nicm 1030: copy = xstrdup(target);
1.1 nicm 1031: colon = strchr(copy, ':');
1032: if (colon != NULL)
1033: *colon++ = '\0';
1034: if (colon == NULL)
1035: period = strchr(copy, '.');
1036: else
1037: period = strchr(colon, '.');
1038: if (period != NULL)
1039: *period++ = '\0';
1040:
1041: /* Set session, window and pane parts. */
1042: session = window = pane = NULL;
1043: if (colon != NULL && period != NULL) {
1044: session = copy;
1045: window = colon;
1.44 nicm 1046: window_only = 1;
1.1 nicm 1047: pane = period;
1.44 nicm 1048: pane_only = 1;
1.1 nicm 1049: } else if (colon != NULL && period == NULL) {
1050: session = copy;
1051: window = colon;
1.44 nicm 1052: window_only = 1;
1.1 nicm 1053: } else if (colon == NULL && period != NULL) {
1054: window = copy;
1055: pane = period;
1.44 nicm 1056: pane_only = 1;
1.1 nicm 1057: } else {
1058: if (*copy == '$')
1059: session = copy;
1060: else if (*copy == '@')
1061: window = copy;
1062: else if (*copy == '%')
1063: pane = copy;
1064: else {
1065: switch (type) {
1066: case CMD_FIND_SESSION:
1067: session = copy;
1068: break;
1069: case CMD_FIND_WINDOW:
1070: window = copy;
1071: break;
1072: case CMD_FIND_PANE:
1073: pane = copy;
1074: break;
1075: }
1076: }
1.9 nicm 1077: }
1078:
1079: /* Set exact match flags. */
1080: if (session != NULL && *session == '=') {
1081: session++;
1.20 nicm 1082: fs->flags |= CMD_FIND_EXACT_SESSION;
1.9 nicm 1083: }
1084: if (window != NULL && *window == '=') {
1085: window++;
1.20 nicm 1086: fs->flags |= CMD_FIND_EXACT_WINDOW;
1.1 nicm 1087: }
1088:
1089: /* Empty is the same as NULL. */
1090: if (session != NULL && *session == '\0')
1091: session = NULL;
1092: if (window != NULL && *window == '\0')
1093: window = NULL;
1094: if (pane != NULL && *pane == '\0')
1095: pane = NULL;
1096:
1097: /* Map though conversion table. */
1098: if (session != NULL)
1099: session = cmd_find_map_table(cmd_find_session_table, session);
1100: if (window != NULL)
1101: window = cmd_find_map_table(cmd_find_window_table, window);
1102: if (pane != NULL)
1103: pane = cmd_find_map_table(cmd_find_pane_table, pane);
1104:
1.49 nicm 1105: log_debug("%s: target %s (flags %#x): session=%s, window=%s, pane=%s",
1106: __func__, target, flags, session == NULL ? "none" : session,
1.1 nicm 1107: window == NULL ? "none" : window, pane == NULL ? "none" : pane);
1108:
1109: /* No pane is allowed if want an index. */
1110: if (pane != NULL && (flags & CMD_FIND_WINDOW_INDEX)) {
1111: if (~flags & CMD_FIND_QUIET)
1.37 nicm 1112: cmdq_error(item, "can't specify pane here");
1.1 nicm 1113: goto error;
1114: }
1115:
1116: /* If the session isn't NULL, look it up. */
1117: if (session != NULL) {
1118: /* This will fill in session. */
1.20 nicm 1119: if (cmd_find_get_session(fs, session) != 0)
1.1 nicm 1120: goto no_session;
1121:
1122: /* If window and pane are NULL, use that session's current. */
1123: if (window == NULL && pane == NULL) {
1.20 nicm 1124: fs->wl = fs->s->curw;
1125: fs->idx = -1;
1126: fs->w = fs->wl->window;
1127: fs->wp = fs->w->active;
1.1 nicm 1128: goto found;
1129: }
1130:
1131: /* If window is present but pane not, find window in session. */
1132: if (window != NULL && pane == NULL) {
1133: /* This will fill in winlink and window. */
1.20 nicm 1134: if (cmd_find_get_window_with_session(fs, window) != 0)
1.1 nicm 1135: goto no_window;
1.21 nicm 1136: fs->wp = fs->wl->window->active;
1.1 nicm 1137: goto found;
1138: }
1139:
1140: /* If pane is present but window not, find pane. */
1141: if (window == NULL && pane != NULL) {
1142: /* This will fill in winlink and window and pane. */
1.20 nicm 1143: if (cmd_find_get_pane_with_session(fs, pane) != 0)
1.1 nicm 1144: goto no_pane;
1145: goto found;
1146: }
1147:
1148: /*
1149: * If window and pane are present, find both in session. This
1150: * will fill in winlink and window.
1151: */
1.20 nicm 1152: if (cmd_find_get_window_with_session(fs, window) != 0)
1.1 nicm 1153: goto no_window;
1154: /* This will fill in pane. */
1.20 nicm 1155: if (cmd_find_get_pane_with_window(fs, pane) != 0)
1.1 nicm 1156: goto no_pane;
1157: goto found;
1158: }
1159:
1160: /* No session. If window and pane, try them. */
1161: if (window != NULL && pane != NULL) {
1162: /* This will fill in session, winlink and window. */
1.44 nicm 1163: if (cmd_find_get_window(fs, window, window_only) != 0)
1.1 nicm 1164: goto no_window;
1165: /* This will fill in pane. */
1.20 nicm 1166: if (cmd_find_get_pane_with_window(fs, pane) != 0)
1.1 nicm 1167: goto no_pane;
1168: goto found;
1169: }
1170:
1171: /* If just window is present, try it. */
1172: if (window != NULL && pane == NULL) {
1173: /* This will fill in session, winlink and window. */
1.44 nicm 1174: if (cmd_find_get_window(fs, window, window_only) != 0)
1.1 nicm 1175: goto no_window;
1.21 nicm 1176: fs->wp = fs->wl->window->active;
1.1 nicm 1177: goto found;
1178: }
1179:
1180: /* If just pane is present, try it. */
1181: if (window == NULL && pane != NULL) {
1182: /* This will fill in session, winlink, window and pane. */
1.44 nicm 1183: if (cmd_find_get_pane(fs, pane, pane_only) != 0)
1.1 nicm 1184: goto no_pane;
1185: goto found;
1186: }
1187:
1188: current:
1.20 nicm 1189: /* Use the current session. */
1.24 nicm 1190: cmd_find_copy_state(fs, fs->current);
1.1 nicm 1191: if (flags & CMD_FIND_WINDOW_INDEX)
1.24 nicm 1192: fs->idx = -1;
1.20 nicm 1193: goto found;
1.1 nicm 1194:
1195: error:
1.20 nicm 1196: fs->current = NULL;
1.39 nicm 1197: log_debug("%s: error", __func__);
1.20 nicm 1198:
1.1 nicm 1199: free(copy);
1.51 nicm 1200: if (flags & CMD_FIND_CANFAIL)
1201: return (0);
1.20 nicm 1202: return (-1);
1.1 nicm 1203:
1204: found:
1.20 nicm 1205: fs->current = NULL;
1206: cmd_find_log_state(__func__, fs);
1207:
1.1 nicm 1208: free(copy);
1.20 nicm 1209: return (0);
1.1 nicm 1210:
1211: no_session:
1212: if (~flags & CMD_FIND_QUIET)
1.37 nicm 1213: cmdq_error(item, "can't find session %s", session);
1.1 nicm 1214: goto error;
1215:
1216: no_window:
1217: if (~flags & CMD_FIND_QUIET)
1.37 nicm 1218: cmdq_error(item, "can't find window %s", window);
1.1 nicm 1219: goto error;
1220:
1221: no_pane:
1222: if (~flags & CMD_FIND_QUIET)
1.37 nicm 1223: cmdq_error(item, "can't find pane %s", pane);
1.1 nicm 1224: goto error;
1225: }
1226:
1227: /* Find the target client or report an error and return NULL. */
1228: struct client *
1.37 nicm 1229: cmd_find_client(struct cmdq_item *item, const char *target, int quiet)
1.1 nicm 1230: {
1231: struct client *c;
1.48 nicm 1232: struct session *s;
1.1 nicm 1233: char *copy;
1234: size_t size;
1235:
1236: /* A NULL argument means the current client. */
1.48 nicm 1237: if (target == NULL) {
1238: c = NULL;
1239: if (item->client != NULL && item->client->session != NULL)
1240: c = item->client;
1241: else {
1242: s = cmd_find_best_session(NULL, 0, CMD_FIND_QUIET);
1243: if (s != NULL)
1244: c = cmd_find_best_client(s);
1245: }
1.1 nicm 1246: if (c == NULL && !quiet)
1.37 nicm 1247: cmdq_error(item, "no current client");
1.16 nicm 1248: log_debug("%s: no target, return %p", __func__, c);
1.1 nicm 1249: return (c);
1250: }
1251: copy = xstrdup(target);
1252:
1253: /* Trim a single trailing colon if any. */
1254: size = strlen(copy);
1255: if (size != 0 && copy[size - 1] == ':')
1256: copy[size - 1] = '\0';
1257:
1.43 nicm 1258: /* Check name and path of each client. */
1.1 nicm 1259: TAILQ_FOREACH(c, &clients, entry) {
1.43 nicm 1260: if (c->session == NULL)
1.1 nicm 1261: continue;
1.43 nicm 1262: if (strcmp(copy, c->name) == 0)
1263: break;
1.1 nicm 1264:
1.43 nicm 1265: if (*c->ttyname == '\0')
1266: continue;
1267: if (strcmp(copy, c->ttyname) == 0)
1.1 nicm 1268: break;
1.43 nicm 1269: if (strncmp(c->ttyname, _PATH_DEV, (sizeof _PATH_DEV) - 1) != 0)
1.1 nicm 1270: continue;
1.43 nicm 1271: if (strcmp(copy, c->ttyname + (sizeof _PATH_DEV) - 1) == 0)
1.1 nicm 1272: break;
1273: }
1274:
1275: /* If no client found, report an error. */
1276: if (c == NULL && !quiet)
1.37 nicm 1277: cmdq_error(item, "can't find client %s", copy);
1.1 nicm 1278:
1279: free(copy);
1.16 nicm 1280: log_debug("%s: target %s, return %p", __func__, target, c);
1.1 nicm 1281: return (c);
1282: }