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