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