Annotation of src/usr.bin/tmux/cmd-find.c, Revision 1.29
1.29 ! nicm 1: /* $OpenBSD: cmd-find.c,v 1.28 2015/12/17 23:08:22 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;
1.29 ! nicm 877:
! 878: cmd_find_log_state(__func__, fs);
! 879: return (0);
! 880: }
! 881:
! 882: /* Find state from a winlink. */
! 883: int
! 884: cmd_find_from_winlink(struct cmd_find_state *fs, struct session *s,
! 885: struct winlink *wl)
! 886: {
! 887: cmd_find_clear_state(fs, NULL, 0);
! 888:
! 889: fs->s = s;
! 890: fs->wl = wl;
! 891: fs->w = wl->window;
! 892: fs->wp = wl->window->active;
1.27 nicm 893:
894: cmd_find_log_state(__func__, fs);
895: return (0);
896: }
897:
898: /* Find state from a window. */
899: int
900: cmd_find_from_window(struct cmd_find_state *fs, struct window *w)
901: {
902: cmd_find_clear_state(fs, NULL, 0);
903:
904: fs->w = w;
905: if (cmd_find_best_session_with_window(fs) != 0)
906: return (-1);
907: if (cmd_find_best_winlink_with_window(fs) != 0)
908: return (-1);
909:
910: cmd_find_log_state(__func__, fs);
911: return (0);
912: }
913:
914: /* Find state from a pane. */
915: int
916: cmd_find_from_pane(struct cmd_find_state *fs, struct window_pane *wp)
917: {
918: if (cmd_find_from_window(fs, wp->window) != 0)
919: return (-1);
920: fs->wp = wp;
921:
922: cmd_find_log_state(__func__, fs);
923: return (0);
1.23 nicm 924: }
925:
1.20 nicm 926: /*
927: * Split target into pieces and resolve for the given type. Fills in the given
928: * state. Returns 0 on success or -1 on error.
929: */
930: int
931: cmd_find_target(struct cmd_find_state *fs, struct cmd_q *cmdq,
932: const char *target, enum cmd_find_type type, int flags)
1.1 nicm 933: {
1.20 nicm 934: struct cmd_find_state current;
935: struct mouse_event *m;
936: char *colon, *period, *copy = NULL;
937: const char *session, *window, *pane;
1.1 nicm 938:
1.18 nicm 939: /* Log the arguments. */
940: if (target == NULL)
941: log_debug("%s: target none, type %d", __func__, type);
942: else
943: log_debug("%s: target %s, type %d", __func__, target, type);
944: log_debug("%s: cmdq %p, flags %#x", __func__, cmdq, flags);
945:
1.24 nicm 946: /* Clear new state. */
947: cmd_find_clear_state(fs, cmdq, flags);
948:
1.1 nicm 949: /* Find current state. */
1.23 nicm 950: if (server_check_marked() && (flags & CMD_FIND_DEFAULT_MARKED))
1.24 nicm 951: fs->current = &marked_pane;
1.28 nicm 952: else if (cmd_find_valid_state(&cmdq->current))
953: fs->current = &cmdq->current;
954: else {
1.24 nicm 955: cmd_find_clear_state(¤t, cmdq, flags);
1.23 nicm 956: if (cmd_find_current_session(¤t) != 0) {
957: if (~flags & CMD_FIND_QUIET)
958: cmdq_error(cmdq, "no current session");
959: goto error;
960: }
1.24 nicm 961: fs->current = ¤t;
1.1 nicm 962: }
963:
964: /* An empty or NULL target is the current. */
965: if (target == NULL || *target == '\0')
966: goto current;
967:
968: /* Mouse target is a plain = or {mouse}. */
969: if (strcmp(target, "=") == 0 || strcmp(target, "{mouse}") == 0) {
970: m = &cmdq->item->mouse;
971: switch (type) {
972: case CMD_FIND_PANE:
1.20 nicm 973: fs->wp = cmd_mouse_pane(m, &fs->s, &fs->wl);
974: if (fs->wp != NULL)
975: fs->w = fs->wl->window;
1.1 nicm 976: break;
977: case CMD_FIND_WINDOW:
978: case CMD_FIND_SESSION:
1.20 nicm 979: fs->wl = cmd_mouse_window(m, &fs->s);
980: if (fs->wl != NULL) {
981: fs->w = fs->wl->window;
982: fs->wp = fs->w->active;
1.1 nicm 983: }
984: break;
985: }
1.20 nicm 986: if (fs->wp == NULL) {
1.1 nicm 987: if (~flags & CMD_FIND_QUIET)
988: cmdq_error(cmdq, "no mouse target");
989: goto error;
990: }
1.18 nicm 991: goto found;
1.1 nicm 992: }
1.8 nicm 993:
994: /* Marked target is a plain ~ or {marked}. */
995: if (strcmp(target, "~") == 0 || strcmp(target, "{marked}") == 0) {
996: if (!server_check_marked()) {
997: if (~flags & CMD_FIND_QUIET)
998: cmdq_error(cmdq, "no marked target");
999: goto error;
1000: }
1.23 nicm 1001: cmd_find_copy_state(fs, &marked_pane);
1.18 nicm 1002: goto found;
1.8 nicm 1003: }
1.1 nicm 1004:
1005: /* Find separators if they exist. */
1.8 nicm 1006: copy = xstrdup(target);
1.1 nicm 1007: colon = strchr(copy, ':');
1008: if (colon != NULL)
1009: *colon++ = '\0';
1010: if (colon == NULL)
1011: period = strchr(copy, '.');
1012: else
1013: period = strchr(colon, '.');
1014: if (period != NULL)
1015: *period++ = '\0';
1016:
1017: /* Set session, window and pane parts. */
1018: session = window = pane = NULL;
1019: if (colon != NULL && period != NULL) {
1020: session = copy;
1021: window = colon;
1022: pane = period;
1023: } else if (colon != NULL && period == NULL) {
1024: session = copy;
1025: window = colon;
1026: } else if (colon == NULL && period != NULL) {
1027: window = copy;
1028: pane = period;
1029: } else {
1030: if (*copy == '$')
1031: session = copy;
1032: else if (*copy == '@')
1033: window = copy;
1034: else if (*copy == '%')
1035: pane = copy;
1036: else {
1037: switch (type) {
1038: case CMD_FIND_SESSION:
1039: session = copy;
1040: break;
1041: case CMD_FIND_WINDOW:
1042: window = copy;
1043: break;
1044: case CMD_FIND_PANE:
1045: pane = copy;
1046: break;
1047: }
1048: }
1.9 nicm 1049: }
1050:
1051: /* Set exact match flags. */
1052: if (session != NULL && *session == '=') {
1053: session++;
1.20 nicm 1054: fs->flags |= CMD_FIND_EXACT_SESSION;
1.9 nicm 1055: }
1056: if (window != NULL && *window == '=') {
1057: window++;
1.20 nicm 1058: fs->flags |= CMD_FIND_EXACT_WINDOW;
1.1 nicm 1059: }
1060:
1061: /* Empty is the same as NULL. */
1062: if (session != NULL && *session == '\0')
1063: session = NULL;
1064: if (window != NULL && *window == '\0')
1065: window = NULL;
1066: if (pane != NULL && *pane == '\0')
1067: pane = NULL;
1068:
1069: /* Map though conversion table. */
1070: if (session != NULL)
1071: session = cmd_find_map_table(cmd_find_session_table, session);
1072: if (window != NULL)
1073: window = cmd_find_map_table(cmd_find_window_table, window);
1074: if (pane != NULL)
1075: pane = cmd_find_map_table(cmd_find_pane_table, pane);
1076:
1077: log_debug("target %s (flags %#x): session=%s, window=%s, pane=%s",
1078: target, flags, session == NULL ? "none" : session,
1079: window == NULL ? "none" : window, pane == NULL ? "none" : pane);
1080:
1081: /* No pane is allowed if want an index. */
1082: if (pane != NULL && (flags & CMD_FIND_WINDOW_INDEX)) {
1083: if (~flags & CMD_FIND_QUIET)
1084: cmdq_error(cmdq, "can't specify pane here");
1085: goto error;
1086: }
1087:
1088: /* If the session isn't NULL, look it up. */
1089: if (session != NULL) {
1090: /* This will fill in session. */
1.20 nicm 1091: if (cmd_find_get_session(fs, session) != 0)
1.1 nicm 1092: goto no_session;
1093:
1094: /* If window and pane are NULL, use that session's current. */
1095: if (window == NULL && pane == NULL) {
1.20 nicm 1096: fs->wl = fs->s->curw;
1097: fs->idx = -1;
1098: fs->w = fs->wl->window;
1099: fs->wp = fs->w->active;
1.1 nicm 1100: goto found;
1101: }
1102:
1103: /* If window is present but pane not, find window in session. */
1104: if (window != NULL && pane == NULL) {
1105: /* This will fill in winlink and window. */
1.20 nicm 1106: if (cmd_find_get_window_with_session(fs, window) != 0)
1.1 nicm 1107: goto no_window;
1.21 nicm 1108: fs->wp = fs->wl->window->active;
1.1 nicm 1109: goto found;
1110: }
1111:
1112: /* If pane is present but window not, find pane. */
1113: if (window == NULL && pane != NULL) {
1114: /* This will fill in winlink and window and pane. */
1.20 nicm 1115: if (cmd_find_get_pane_with_session(fs, pane) != 0)
1.1 nicm 1116: goto no_pane;
1117: goto found;
1118: }
1119:
1120: /*
1121: * If window and pane are present, find both in session. This
1122: * will fill in winlink and window.
1123: */
1.20 nicm 1124: if (cmd_find_get_window_with_session(fs, window) != 0)
1.1 nicm 1125: goto no_window;
1126: /* This will fill in pane. */
1.20 nicm 1127: if (cmd_find_get_pane_with_window(fs, pane) != 0)
1.1 nicm 1128: goto no_pane;
1129: goto found;
1130: }
1131:
1132: /* No session. If window and pane, try them. */
1133: if (window != NULL && pane != NULL) {
1134: /* This will fill in session, winlink and window. */
1.20 nicm 1135: if (cmd_find_get_window(fs, window) != 0)
1.1 nicm 1136: goto no_window;
1137: /* This will fill in pane. */
1.20 nicm 1138: if (cmd_find_get_pane_with_window(fs, pane) != 0)
1.1 nicm 1139: goto no_pane;
1140: goto found;
1141: }
1142:
1143: /* If just window is present, try it. */
1144: if (window != NULL && pane == NULL) {
1145: /* This will fill in session, winlink and window. */
1.20 nicm 1146: if (cmd_find_get_window(fs, window) != 0)
1.1 nicm 1147: goto no_window;
1.21 nicm 1148: fs->wp = fs->wl->window->active;
1.1 nicm 1149: goto found;
1150: }
1151:
1152: /* If just pane is present, try it. */
1153: if (window == NULL && pane != NULL) {
1154: /* This will fill in session, winlink, window and pane. */
1.20 nicm 1155: if (cmd_find_get_pane(fs, pane) != 0)
1.1 nicm 1156: goto no_pane;
1157: goto found;
1158: }
1159:
1160: current:
1.20 nicm 1161: /* Use the current session. */
1.24 nicm 1162: cmd_find_copy_state(fs, fs->current);
1.1 nicm 1163: if (flags & CMD_FIND_WINDOW_INDEX)
1.24 nicm 1164: fs->idx = -1;
1.20 nicm 1165: goto found;
1.1 nicm 1166:
1167: error:
1.20 nicm 1168: fs->current = NULL;
1169: log_debug(" error");
1170:
1.1 nicm 1171: free(copy);
1.20 nicm 1172: return (-1);
1.1 nicm 1173:
1174: found:
1.20 nicm 1175: fs->current = NULL;
1176: cmd_find_log_state(__func__, fs);
1177:
1.1 nicm 1178: free(copy);
1.20 nicm 1179: return (0);
1.1 nicm 1180:
1181: no_session:
1182: if (~flags & CMD_FIND_QUIET)
1183: cmdq_error(cmdq, "can't find session %s", session);
1184: goto error;
1185:
1186: no_window:
1187: if (~flags & CMD_FIND_QUIET)
1188: cmdq_error(cmdq, "can't find window %s", window);
1189: goto error;
1190:
1191: no_pane:
1192: if (~flags & CMD_FIND_QUIET)
1193: cmdq_error(cmdq, "can't find pane %s", pane);
1194: goto error;
1195: }
1196:
1197: /* Find the target client or report an error and return NULL. */
1198: struct client *
1199: cmd_find_client(struct cmd_q *cmdq, const char *target, int quiet)
1200: {
1201: struct client *c;
1202: char *copy;
1203: size_t size;
1204: const char *path;
1205:
1206: /* A NULL argument means the current client. */
1207: if (target == NULL) {
1208: c = cmd_find_current_client(cmdq);
1209: if (c == NULL && !quiet)
1210: cmdq_error(cmdq, "no current client");
1.16 nicm 1211: log_debug("%s: no target, return %p", __func__, c);
1.1 nicm 1212: return (c);
1213: }
1214: copy = xstrdup(target);
1215:
1216: /* Trim a single trailing colon if any. */
1217: size = strlen(copy);
1218: if (size != 0 && copy[size - 1] == ':')
1219: copy[size - 1] = '\0';
1220:
1221: /* Check path of each client. */
1222: TAILQ_FOREACH(c, &clients, entry) {
1223: if (c->session == NULL || c->tty.path == NULL)
1224: continue;
1225: path = c->tty.path;
1226:
1227: /* Try for exact match. */
1228: if (strcmp(copy, path) == 0)
1229: break;
1230:
1231: /* Try without leading /dev. */
1232: if (strncmp(path, _PATH_DEV, (sizeof _PATH_DEV) - 1) != 0)
1233: continue;
1234: if (strcmp(copy, path + (sizeof _PATH_DEV) - 1) == 0)
1235: break;
1236: }
1237:
1238: /* If no client found, report an error. */
1239: if (c == NULL && !quiet)
1240: cmdq_error(cmdq, "can't find client %s", copy);
1241:
1242: free(copy);
1.16 nicm 1243: log_debug("%s: target %s, return %p", __func__, target, c);
1.1 nicm 1244: return (c);
1245: }