Annotation of src/usr.bin/tmux/cmd-find.c, Revision 1.23
1.23 ! nicm 1: /* $OpenBSD: cmd-find.c,v 1.22 2015/12/14 23:30:58 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.15 nicm 193: if (fs->cmdq->client != NULL) {
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:
252: /* If this is running in a pane, that's great. */
1.5 nicm 253: if (fs->cmdq->client->tty.path != NULL) {
254: RB_FOREACH(wp, window_pane_tree, &all_window_panes) {
255: if (strcmp(wp->tty, fs->cmdq->client->tty.path) == 0)
256: break;
257: }
258: } else
259: wp = NULL;
1.1 nicm 260:
261: /* Not running in a pane. We know nothing. Find the best session. */
1.14 nicm 262: if (wp == NULL)
263: goto unknown_pane;
1.1 nicm 264:
265: /* We now know the window and pane. */
266: fs->w = wp->window;
267: fs->wp = wp;
268:
269: /* Find the best session and winlink. */
1.14 nicm 270: if (cmd_find_best_session_with_window(fs) != 0) {
271: if (wp != NULL) {
272: /*
273: * The window may have been destroyed but the pane
274: * still on all_window_panes due to something else
275: * holding a reference.
276: */
277: goto unknown_pane;
278: }
279: return (-1);
280: }
281: return (0);
282:
283: unknown_pane:
1.15 nicm 284: fs->s = cmd_find_try_TMUX(fs->cmdq->client, NULL);
285: if (fs->s == NULL)
286: fs->s = cmd_find_best_session(NULL, 0, fs->flags);
1.14 nicm 287: if (fs->s == NULL)
1.1 nicm 288: return (-1);
1.14 nicm 289: fs->wl = fs->s->curw;
290: fs->idx = fs->wl->idx;
291: fs->w = fs->wl->window;
292: fs->wp = fs->w->active;
1.1 nicm 293: return (0);
294: }
295:
296: /*
297: * Work out the best current state. If this function succeeds, the state is
298: * guaranteed to be completely filled in.
299: */
300: int
301: cmd_find_current_session(struct cmd_find_state *fs)
302: {
303: /* If we know the current client, use it. */
304: if (fs->cmdq->client != NULL) {
1.16 nicm 305: log_debug("%s: have client %p%s", __func__, fs->cmdq->client,
306: fs->cmdq->client->session == NULL ? "" : " (with session)");
1.1 nicm 307: if (fs->cmdq->client->session == NULL)
308: return (cmd_find_current_session_with_client(fs));
309: fs->s = fs->cmdq->client->session;
310: fs->wl = fs->s->curw;
311: fs->idx = fs->wl->idx;
312: fs->w = fs->wl->window;
313: fs->wp = fs->w->active;
314: return (0);
315: }
316:
317: /* We know nothing, find the best session and client. */
318: fs->s = cmd_find_best_session(NULL, 0, fs->flags);
319: if (fs->s == NULL)
320: return (-1);
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:
326: return (0);
327: }
328:
329: /* Work out the best current client. */
330: struct client *
331: cmd_find_current_client(struct cmd_q *cmdq)
332: {
333: struct cmd_find_state current;
334: struct session *s;
335: struct client *c, **clist = NULL;
336: u_int csize;
337:
338: /* If the queue client has a session, use it. */
1.16 nicm 339: if (cmdq->client != NULL && cmdq->client->session != NULL) {
340: log_debug("%s: using cmdq %p client %p", __func__, cmdq,
341: cmdq->client);
1.1 nicm 342: return (cmdq->client);
1.16 nicm 343: }
1.1 nicm 344:
345: /* Otherwise find the current session. */
346: cmd_find_clear_state(¤t, cmdq, 0);
347: if (cmd_find_current_session(¤t) != 0)
348: return (NULL);
349:
350: /* If it is attached, find the best of it's clients. */
351: s = current.s;
1.16 nicm 352: log_debug("%s: current session $%u %s", __func__, s->id, s->name);
1.1 nicm 353: if (~s->flags & SESSION_UNATTACHED) {
354: csize = 0;
355: TAILQ_FOREACH(c, &clients, entry) {
356: if (c->session != s)
357: continue;
1.7 nicm 358: clist = xreallocarray(clist, csize + 1, sizeof *clist);
1.1 nicm 359: clist[csize++] = c;
360: }
361: if (csize != 0) {
362: c = cmd_find_best_client(clist, csize);
363: if (c != NULL) {
364: free(clist);
365: return (c);
366: }
367: }
368: free(clist);
369: }
370:
371: /* Otherwise pick best of all clients. */
372: return (cmd_find_best_client(NULL, 0));
373: }
374:
375: /* Maps string in table. */
376: const char *
377: cmd_find_map_table(const char *table[][2], const char *s)
378: {
379: u_int i;
380:
381: for (i = 0; table[i][0] != NULL; i++) {
382: if (strcmp(s, table[i][0]) == 0)
383: return (table[i][1]);
384: }
385: return (s);
386: }
387:
388: /* Find session from string. Fills in s. */
389: int
390: cmd_find_get_session(struct cmd_find_state *fs, const char *session)
391: {
392: struct session *s, *s_loop;
393:
394: log_debug("%s: %s", __func__, session);
395:
396: /* Check for session ids starting with $. */
397: if (*session == '$') {
398: fs->s = session_find_by_id_str(session);
399: if (fs->s == NULL)
400: return (-1);
401: return (0);
402: }
403:
404: /* Look for exactly this session. */
405: fs->s = session_find(session);
406: if (fs->s != NULL)
407: return (0);
408:
1.9 nicm 409: /* Stop now if exact only. */
410: if (fs->flags & CMD_FIND_EXACT_SESSION)
411: return (-1);
412:
1.1 nicm 413: /* Otherwise look for prefix. */
414: s = NULL;
415: RB_FOREACH(s_loop, sessions, &sessions) {
416: if (strncmp(session, s_loop->name, strlen(session)) == 0) {
417: if (s != NULL)
418: return (-1);
419: s = s_loop;
420: }
421: }
422: if (s != NULL) {
423: fs->s = s;
424: return (0);
425: }
426:
427: /* Then as a pattern. */
428: s = NULL;
429: RB_FOREACH(s_loop, sessions, &sessions) {
430: if (fnmatch(session, s_loop->name, 0) == 0) {
431: if (s != NULL)
432: return (-1);
433: s = s_loop;
434: }
435: }
436: if (s != NULL) {
437: fs->s = s;
438: return (0);
439: }
440:
441: return (-1);
442: }
443:
444: /* Find window from string. Fills in s, wl, w. */
445: int
446: cmd_find_get_window(struct cmd_find_state *fs, const char *window)
447: {
448: log_debug("%s: %s", __func__, window);
449:
450: /* Check for window ids starting with @. */
451: if (*window == '@') {
452: fs->w = window_find_by_id_str(window);
453: if (fs->w == NULL)
454: return (-1);
455: return (cmd_find_best_session_with_window(fs));
456: }
457:
458: /* Not a window id, so use the current session. */
459: fs->s = fs->current->s;
460:
461: /* We now only need to find the winlink in this session. */
1.4 nicm 462: if (cmd_find_get_window_with_session(fs, window) == 0)
463: return (0);
464:
465: /* Otherwise try as a session itself. */
466: if (cmd_find_get_session(fs, window) == 0) {
1.21 nicm 467: fs->wl = fs->s->curw;
468: fs->w = fs->wl->window;
469: if (~fs->flags & CMD_FIND_WINDOW_INDEX)
1.6 nicm 470: fs->idx = fs->wl->idx;
1.4 nicm 471: return (0);
472: }
473:
474: return (-1);
1.1 nicm 475: }
476:
477: /*
478: * Find window from string, assuming it is in given session. Needs s, fills in
479: * wl and w.
480: */
481: int
482: cmd_find_get_window_with_session(struct cmd_find_state *fs, const char *window)
483: {
484: struct winlink *wl;
485: const char *errstr;
1.9 nicm 486: int idx, n, exact;
1.1 nicm 487: struct session *s;
488:
489: log_debug("%s: %s", __func__, window);
1.9 nicm 490: exact = (fs->flags & CMD_FIND_EXACT_WINDOW);
1.1 nicm 491:
1.21 nicm 492: /*
493: * Start with the current window as the default. So if only an index is
494: * found, the window will be the current.
495: */
496: fs->wl = fs->s->curw;
497: fs->w = fs->wl->window;
498:
1.1 nicm 499: /* Check for window ids starting with @. */
500: if (*window == '@') {
501: fs->w = window_find_by_id_str(window);
502: if (fs->w == NULL || !session_has(fs->s, fs->w))
503: return (-1);
504: return (cmd_find_best_winlink_with_window(fs));
505: }
506:
507: /* Try as an offset. */
1.10 nicm 508: if (!exact && (window[0] == '+' || window[0] == '-')) {
1.1 nicm 509: if (window[1] != '\0')
510: n = strtonum(window + 1, 1, INT_MAX, NULL);
511: else
512: n = 1;
513: s = fs->s;
514: if (fs->flags & CMD_FIND_WINDOW_INDEX) {
515: if (window[0] == '+') {
516: if (INT_MAX - s->curw->idx < n)
517: return (-1);
518: fs->idx = s->curw->idx + n;
519: } else {
520: if (n < s->curw->idx)
521: return (-1);
522: fs->idx = s->curw->idx - n;
523: }
524: return (0);
525: }
526: if (window[0] == '+')
527: fs->wl = winlink_next_by_number(s->curw, s, n);
528: else
529: fs->wl = winlink_previous_by_number(s->curw, s, n);
530: if (fs->wl != NULL) {
531: fs->idx = fs->wl->idx;
532: fs->w = fs->wl->window;
533: return (0);
534: }
535: }
536:
537: /* Try special characters. */
1.9 nicm 538: if (!exact) {
539: if (strcmp(window, "!") == 0) {
540: fs->wl = TAILQ_FIRST(&fs->s->lastw);
541: if (fs->wl == NULL)
542: return (-1);
543: fs->idx = fs->wl->idx;
544: fs->w = fs->wl->window;
545: return (0);
546: } else if (strcmp(window, "^") == 0) {
547: fs->wl = RB_MIN(winlinks, &fs->s->windows);
548: if (fs->wl == NULL)
549: return (-1);
550: fs->idx = fs->wl->idx;
551: fs->w = fs->wl->window;
552: return (0);
553: } else if (strcmp(window, "$") == 0) {
554: fs->wl = RB_MAX(winlinks, &fs->s->windows);
555: if (fs->wl == NULL)
556: return (-1);
557: fs->idx = fs->wl->idx;
558: fs->w = fs->wl->window;
559: return (0);
560: }
1.1 nicm 561: }
562:
563: /* First see if this is a valid window index in this session. */
1.9 nicm 564: if (window[0] != '+' && window[0] != '-') {
565: idx = strtonum(window, 0, INT_MAX, &errstr);
566: if (errstr == NULL) {
567: if (fs->flags & CMD_FIND_WINDOW_INDEX) {
568: fs->idx = idx;
569: return (0);
570: }
571: fs->wl = winlink_find_by_index(&fs->s->windows, idx);
572: if (fs->wl != NULL) {
573: fs->w = fs->wl->window;
574: return (0);
575: }
1.1 nicm 576: }
577: }
578:
579: /* Look for exact matches, error if more than one. */
580: fs->wl = NULL;
581: RB_FOREACH(wl, winlinks, &fs->s->windows) {
582: if (strcmp(window, wl->window->name) == 0) {
583: if (fs->wl != NULL)
584: return (-1);
585: fs->wl = wl;
586: }
587: }
588: if (fs->wl != NULL) {
589: fs->idx = fs->wl->idx;
590: fs->w = fs->wl->window;
591: return (0);
592: }
1.9 nicm 593:
594: /* Stop now if exact only. */
595: if (exact)
596: return (-1);
597:
1.1 nicm 598: /* Try as the start of a window name, error if multiple. */
599: fs->wl = NULL;
600: RB_FOREACH(wl, winlinks, &fs->s->windows) {
601: if (strncmp(window, wl->window->name, strlen(window)) == 0) {
602: if (fs->wl != NULL)
603: return (-1);
604: fs->wl = wl;
605: }
606: }
607: if (fs->wl != NULL) {
608: fs->idx = fs->wl->idx;
609: fs->w = fs->wl->window;
610: return (0);
611: }
612:
613: /* Now look for pattern matches, again error if multiple. */
614: fs->wl = NULL;
615: RB_FOREACH(wl, winlinks, &fs->s->windows) {
616: if (fnmatch(window, wl->window->name, 0) == 0) {
617: if (fs->wl != NULL)
618: return (-1);
619: fs->wl = wl;
620: }
621: }
622: if (fs->wl != NULL) {
623: fs->idx = fs->wl->idx;
624: fs->w = fs->wl->window;
625: return (0);
626: }
627:
628: return (-1);
629: }
630:
631: /* Find window from given pane. Needs wp, fills in s and wl and w. */
632: int
633: cmd_find_get_window_with_pane(struct cmd_find_state *fs)
634: {
635: log_debug("%s", __func__);
636:
637: fs->w = fs->wp->window;
638: return (cmd_find_best_session_with_window(fs));
639: }
640:
641: /* Find pane from string. Fills in s, wl, w, wp. */
642: int
643: cmd_find_get_pane(struct cmd_find_state *fs, const char *pane)
644: {
645: log_debug("%s: %s", __func__, pane);
646:
647: /* Check for pane ids starting with %. */
648: if (*pane == '%') {
649: fs->wp = window_pane_find_by_id_str(pane);
650: if (fs->wp == NULL)
651: return (-1);
652: fs->w = fs->wp->window;
653: return (cmd_find_best_session_with_window(fs));
654: }
655:
1.4 nicm 656: /* Not a pane id, so try the current session and window. */
1.1 nicm 657: fs->s = fs->current->s;
658: fs->wl = fs->current->wl;
659: fs->idx = fs->current->idx;
660: fs->w = fs->current->w;
661:
662: /* We now only need to find the pane in this window. */
1.4 nicm 663: if (cmd_find_get_pane_with_window(fs, pane) == 0)
664: return (0);
665:
666: /* Otherwise try as a window itself (this will also try as session). */
667: if (cmd_find_get_window(fs, pane) == 0) {
668: fs->wp = fs->w->active;
669: return (0);
670: }
671:
672: return (-1);
1.1 nicm 673: }
674:
675: /*
676: * Find pane from string, assuming it is in given session. Needs s, fills in wl
677: * and w and wp.
678: */
679: int
680: cmd_find_get_pane_with_session(struct cmd_find_state *fs, const char *pane)
681: {
682: log_debug("%s: %s", __func__, pane);
683:
684: /* Check for pane ids starting with %. */
685: if (*pane == '%') {
686: fs->wp = window_pane_find_by_id_str(pane);
687: if (fs->wp == NULL)
688: return (-1);
689: fs->w = fs->wp->window;
690: return (cmd_find_best_winlink_with_window(fs));
691: }
692:
693: /* Otherwise use the current window. */
694: fs->wl = fs->s->curw;
695: fs->idx = fs->wl->idx;
696: fs->w = fs->wl->window;
697:
698: /* Now we just need to look up the pane. */
699: return (cmd_find_get_pane_with_window(fs, pane));
700: }
701:
702: /*
703: * Find pane from string, assuming it is in the given window. Needs w, fills in
704: * wp.
705: */
706: int
707: cmd_find_get_pane_with_window(struct cmd_find_state *fs, const char *pane)
708: {
709: const char *errstr;
710: int idx;
711: struct window_pane *wp;
712: u_int n;
713:
714: log_debug("%s: %s", __func__, pane);
715:
716: /* Check for pane ids starting with %. */
717: if (*pane == '%') {
718: fs->wp = window_pane_find_by_id_str(pane);
719: if (fs->wp == NULL || fs->wp->window != fs->w)
720: return (-1);
721: return (0);
722: }
723:
724: /* Try special characters. */
725: if (strcmp(pane, "!") == 0) {
726: if (fs->w->last == NULL)
727: return (-1);
728: fs->wp = fs->w->last;
729: return (0);
1.12 nicm 730: } else if (strcmp(pane, "{up-of}") == 0) {
1.1 nicm 731: fs->wp = window_pane_find_up(fs->w->active);
732: if (fs->wp == NULL)
733: return (-1);
734: return (0);
1.12 nicm 735: } else if (strcmp(pane, "{down-of}") == 0) {
1.1 nicm 736: fs->wp = window_pane_find_down(fs->w->active);
737: if (fs->wp == NULL)
738: return (-1);
739: return (0);
1.12 nicm 740: } else if (strcmp(pane, "{left-of}") == 0) {
1.1 nicm 741: fs->wp = window_pane_find_left(fs->w->active);
742: if (fs->wp == NULL)
743: return (-1);
744: return (0);
1.12 nicm 745: } else if (strcmp(pane, "{right-of}") == 0) {
1.1 nicm 746: fs->wp = window_pane_find_right(fs->w->active);
747: if (fs->wp == NULL)
748: return (-1);
749: return (0);
750: }
751:
752: /* Try as an offset. */
753: if (pane[0] == '+' || pane[0] == '-') {
754: if (pane[1] != '\0')
755: n = strtonum(pane + 1, 1, INT_MAX, NULL);
756: else
757: n = 1;
758: wp = fs->w->active;
759: if (pane[0] == '+')
760: fs->wp = window_pane_next_by_number(fs->w, wp, n);
761: else
762: fs->wp = window_pane_previous_by_number(fs->w, wp, n);
763: if (fs->wp != NULL)
764: return (0);
765: }
766:
767: /* Get pane by index. */
768: idx = strtonum(pane, 0, INT_MAX, &errstr);
769: if (errstr == NULL) {
770: fs->wp = window_pane_at_index(fs->w, idx);
771: if (fs->wp != NULL)
772: return (0);
773: }
774:
775: /* Try as a description. */
776: fs->wp = window_find_string(fs->w, pane);
777: if (fs->wp != NULL)
778: return (0);
779:
780: return (-1);
781: }
782:
783: /* Clear state. */
784: void
785: cmd_find_clear_state(struct cmd_find_state *fs, struct cmd_q *cmdq, int flags)
786: {
1.7 nicm 787: memset(fs, 0, sizeof *fs);
1.1 nicm 788:
789: fs->cmdq = cmdq;
790: fs->flags = flags;
791:
792: fs->idx = -1;
793: }
794:
1.23 ! nicm 795: /* Check if a state if valid. */
! 796: int
! 797: cmd_find_valid_state(struct cmd_find_state *fs)
! 798: {
! 799: struct winlink *wl;
! 800:
! 801: if (fs->s == NULL || fs->wl == NULL || fs->w == NULL || fs->wp == NULL)
! 802: return (0);
! 803:
! 804: if (!session_alive(fs->s))
! 805: return (0);
! 806:
! 807: RB_FOREACH(wl, winlinks, &fs->s->windows) {
! 808: if (wl->window == fs->w && wl == fs->wl)
! 809: break;
! 810: }
! 811: if (wl == NULL)
! 812: return (0);
! 813:
! 814: if (fs->w != fs->wl->window)
! 815: return (0);
! 816:
! 817: if (!window_has_pane(fs->w, fs->wp))
! 818: return (0);
! 819: return (window_pane_visible(fs->wp));
! 820: }
! 821:
! 822: /* Copy a state. */
! 823: void
! 824: cmd_find_copy_state(struct cmd_find_state *dst, struct cmd_find_state *src)
! 825: {
! 826: dst->s = src->s;
! 827: dst->wl = src->wl;
! 828: dst->idx = dst->wl->idx;
! 829: dst->w = dst->wl->window;
! 830: dst->wp = src->wp;
! 831: }
! 832:
! 833: /* Log the result. */
! 834: void
! 835: cmd_find_log_state(const char *prefix, struct cmd_find_state *fs)
! 836: {
! 837: if (fs->s != NULL)
! 838: log_debug("%s: s=$%u", prefix, fs->s->id);
! 839: else
! 840: log_debug("%s: s=none", prefix);
! 841: if (fs->wl != NULL) {
! 842: log_debug("%s: wl=%u %d w=@%u %s", prefix, fs->wl->idx,
! 843: fs->wl->window == fs->w, fs->w->id, fs->w->name);
! 844: } else
! 845: log_debug("%s: wl=none", prefix);
! 846: if (fs->wp != NULL)
! 847: log_debug("%s: wp=%%%u", prefix, fs->wp->id);
! 848: else
! 849: log_debug("%s: wp=none", prefix);
! 850: if (fs->idx != -1)
! 851: log_debug("%s: idx=%d", prefix, fs->idx);
! 852: else
! 853: log_debug("%s: idx=none", prefix);
! 854: }
! 855:
1.20 nicm 856: /*
857: * Split target into pieces and resolve for the given type. Fills in the given
858: * state. Returns 0 on success or -1 on error.
859: */
860: int
861: cmd_find_target(struct cmd_find_state *fs, struct cmd_q *cmdq,
862: const char *target, enum cmd_find_type type, int flags)
1.1 nicm 863: {
1.20 nicm 864: struct cmd_find_state current;
865: struct mouse_event *m;
866: char *colon, *period, *copy = NULL;
867: const char *session, *window, *pane;
1.1 nicm 868:
1.18 nicm 869: /* Log the arguments. */
870: if (target == NULL)
871: log_debug("%s: target none, type %d", __func__, type);
872: else
873: log_debug("%s: target %s, type %d", __func__, target, type);
874: log_debug("%s: cmdq %p, flags %#x", __func__, cmdq, flags);
875:
1.1 nicm 876: /* Find current state. */
877: cmd_find_clear_state(¤t, cmdq, flags);
1.23 ! nicm 878: if (server_check_marked() && (flags & CMD_FIND_DEFAULT_MARKED))
! 879: cmd_find_copy_state(¤t, &marked_pane);
! 880: else {
! 881: if (cmd_find_current_session(¤t) != 0) {
! 882: if (~flags & CMD_FIND_QUIET)
! 883: cmdq_error(cmdq, "no current session");
! 884: goto error;
! 885: }
1.1 nicm 886: }
887:
888: /* Clear new state. */
1.20 nicm 889: cmd_find_clear_state(fs, cmdq, flags);
890: fs->current = ¤t;
1.1 nicm 891:
892: /* An empty or NULL target is the current. */
893: if (target == NULL || *target == '\0')
894: goto current;
895:
896: /* Mouse target is a plain = or {mouse}. */
897: if (strcmp(target, "=") == 0 || strcmp(target, "{mouse}") == 0) {
898: m = &cmdq->item->mouse;
899: switch (type) {
900: case CMD_FIND_PANE:
1.20 nicm 901: fs->wp = cmd_mouse_pane(m, &fs->s, &fs->wl);
902: if (fs->wp != NULL)
903: fs->w = fs->wl->window;
1.1 nicm 904: break;
905: case CMD_FIND_WINDOW:
906: case CMD_FIND_SESSION:
1.20 nicm 907: fs->wl = cmd_mouse_window(m, &fs->s);
908: if (fs->wl != NULL) {
909: fs->w = fs->wl->window;
910: fs->wp = fs->w->active;
1.1 nicm 911: }
912: break;
913: }
1.20 nicm 914: if (fs->wp == NULL) {
1.1 nicm 915: if (~flags & CMD_FIND_QUIET)
916: cmdq_error(cmdq, "no mouse target");
917: goto error;
918: }
1.18 nicm 919: goto found;
1.1 nicm 920: }
1.8 nicm 921:
922: /* Marked target is a plain ~ or {marked}. */
923: if (strcmp(target, "~") == 0 || strcmp(target, "{marked}") == 0) {
924: if (!server_check_marked()) {
925: if (~flags & CMD_FIND_QUIET)
926: cmdq_error(cmdq, "no marked target");
927: goto error;
928: }
1.23 ! nicm 929: cmd_find_copy_state(fs, &marked_pane);
1.18 nicm 930: goto found;
1.8 nicm 931: }
1.1 nicm 932:
933: /* Find separators if they exist. */
1.8 nicm 934: copy = xstrdup(target);
1.1 nicm 935: colon = strchr(copy, ':');
936: if (colon != NULL)
937: *colon++ = '\0';
938: if (colon == NULL)
939: period = strchr(copy, '.');
940: else
941: period = strchr(colon, '.');
942: if (period != NULL)
943: *period++ = '\0';
944:
945: /* Set session, window and pane parts. */
946: session = window = pane = NULL;
947: if (colon != NULL && period != NULL) {
948: session = copy;
949: window = colon;
950: pane = period;
951: } else if (colon != NULL && period == NULL) {
952: session = copy;
953: window = colon;
954: } else if (colon == NULL && period != NULL) {
955: window = copy;
956: pane = period;
957: } else {
958: if (*copy == '$')
959: session = copy;
960: else if (*copy == '@')
961: window = copy;
962: else if (*copy == '%')
963: pane = copy;
964: else {
965: switch (type) {
966: case CMD_FIND_SESSION:
967: session = copy;
968: break;
969: case CMD_FIND_WINDOW:
970: window = copy;
971: break;
972: case CMD_FIND_PANE:
973: pane = copy;
974: break;
975: }
976: }
1.9 nicm 977: }
978:
979: /* Set exact match flags. */
980: if (session != NULL && *session == '=') {
981: session++;
1.20 nicm 982: fs->flags |= CMD_FIND_EXACT_SESSION;
1.9 nicm 983: }
984: if (window != NULL && *window == '=') {
985: window++;
1.20 nicm 986: fs->flags |= CMD_FIND_EXACT_WINDOW;
1.1 nicm 987: }
988:
989: /* Empty is the same as NULL. */
990: if (session != NULL && *session == '\0')
991: session = NULL;
992: if (window != NULL && *window == '\0')
993: window = NULL;
994: if (pane != NULL && *pane == '\0')
995: pane = NULL;
996:
997: /* Map though conversion table. */
998: if (session != NULL)
999: session = cmd_find_map_table(cmd_find_session_table, session);
1000: if (window != NULL)
1001: window = cmd_find_map_table(cmd_find_window_table, window);
1002: if (pane != NULL)
1003: pane = cmd_find_map_table(cmd_find_pane_table, pane);
1004:
1005: log_debug("target %s (flags %#x): session=%s, window=%s, pane=%s",
1006: target, flags, session == NULL ? "none" : session,
1007: window == NULL ? "none" : window, pane == NULL ? "none" : pane);
1008:
1009: /* No pane is allowed if want an index. */
1010: if (pane != NULL && (flags & CMD_FIND_WINDOW_INDEX)) {
1011: if (~flags & CMD_FIND_QUIET)
1012: cmdq_error(cmdq, "can't specify pane here");
1013: goto error;
1014: }
1015:
1016: /* If the session isn't NULL, look it up. */
1017: if (session != NULL) {
1018: /* This will fill in session. */
1.20 nicm 1019: if (cmd_find_get_session(fs, session) != 0)
1.1 nicm 1020: goto no_session;
1021:
1022: /* If window and pane are NULL, use that session's current. */
1023: if (window == NULL && pane == NULL) {
1.20 nicm 1024: fs->wl = fs->s->curw;
1025: fs->idx = -1;
1026: fs->w = fs->wl->window;
1027: fs->wp = fs->w->active;
1.1 nicm 1028: goto found;
1029: }
1030:
1031: /* If window is present but pane not, find window in session. */
1032: if (window != NULL && pane == NULL) {
1033: /* This will fill in winlink and window. */
1.20 nicm 1034: if (cmd_find_get_window_with_session(fs, window) != 0)
1.1 nicm 1035: goto no_window;
1.21 nicm 1036: fs->wp = fs->wl->window->active;
1.1 nicm 1037: goto found;
1038: }
1039:
1040: /* If pane is present but window not, find pane. */
1041: if (window == NULL && pane != NULL) {
1042: /* This will fill in winlink and window and pane. */
1.20 nicm 1043: if (cmd_find_get_pane_with_session(fs, pane) != 0)
1.1 nicm 1044: goto no_pane;
1045: goto found;
1046: }
1047:
1048: /*
1049: * If window and pane are present, find both in session. This
1050: * will fill in winlink and window.
1051: */
1.20 nicm 1052: if (cmd_find_get_window_with_session(fs, window) != 0)
1.1 nicm 1053: goto no_window;
1054: /* This will fill in pane. */
1.20 nicm 1055: if (cmd_find_get_pane_with_window(fs, pane) != 0)
1.1 nicm 1056: goto no_pane;
1057: goto found;
1058: }
1059:
1060: /* No session. If window and pane, try them. */
1061: if (window != NULL && pane != NULL) {
1062: /* This will fill in session, winlink and window. */
1.20 nicm 1063: if (cmd_find_get_window(fs, window) != 0)
1.1 nicm 1064: goto no_window;
1065: /* This will fill in pane. */
1.20 nicm 1066: if (cmd_find_get_pane_with_window(fs, pane) != 0)
1.1 nicm 1067: goto no_pane;
1068: goto found;
1069: }
1070:
1071: /* If just window is present, try it. */
1072: if (window != NULL && pane == NULL) {
1073: /* This will fill in session, winlink and window. */
1.20 nicm 1074: if (cmd_find_get_window(fs, window) != 0)
1.1 nicm 1075: goto no_window;
1.21 nicm 1076: fs->wp = fs->wl->window->active;
1.1 nicm 1077: goto found;
1078: }
1079:
1080: /* If just pane is present, try it. */
1081: if (window == NULL && pane != NULL) {
1082: /* This will fill in session, winlink, window and pane. */
1.20 nicm 1083: if (cmd_find_get_pane(fs, pane) != 0)
1.1 nicm 1084: goto no_pane;
1085: goto found;
1086: }
1087:
1088: current:
1.20 nicm 1089: /* Use the current session. */
1.1 nicm 1090: if (flags & CMD_FIND_WINDOW_INDEX)
1091: current.idx = -1;
1.20 nicm 1092: memcpy(fs, ¤t, sizeof *fs);
1093: goto found;
1.1 nicm 1094:
1095: error:
1.20 nicm 1096: fs->current = NULL;
1097: log_debug(" error");
1098:
1.1 nicm 1099: free(copy);
1.20 nicm 1100: return (-1);
1.1 nicm 1101:
1102: found:
1.20 nicm 1103: fs->current = NULL;
1104: cmd_find_log_state(__func__, fs);
1105:
1.1 nicm 1106: free(copy);
1.20 nicm 1107: return (0);
1.1 nicm 1108:
1109: no_session:
1110: if (~flags & CMD_FIND_QUIET)
1111: cmdq_error(cmdq, "can't find session %s", session);
1112: goto error;
1113:
1114: no_window:
1115: if (~flags & CMD_FIND_QUIET)
1116: cmdq_error(cmdq, "can't find window %s", window);
1117: goto error;
1118:
1119: no_pane:
1120: if (~flags & CMD_FIND_QUIET)
1121: cmdq_error(cmdq, "can't find pane %s", pane);
1122: goto error;
1123: }
1124:
1125: /* Find the target client or report an error and return NULL. */
1126: struct client *
1127: cmd_find_client(struct cmd_q *cmdq, const char *target, int quiet)
1128: {
1129: struct client *c;
1130: char *copy;
1131: size_t size;
1132: const char *path;
1133:
1134: /* A NULL argument means the current client. */
1135: if (target == NULL) {
1136: c = cmd_find_current_client(cmdq);
1137: if (c == NULL && !quiet)
1138: cmdq_error(cmdq, "no current client");
1.16 nicm 1139: log_debug("%s: no target, return %p", __func__, c);
1.1 nicm 1140: return (c);
1141: }
1142: copy = xstrdup(target);
1143:
1144: /* Trim a single trailing colon if any. */
1145: size = strlen(copy);
1146: if (size != 0 && copy[size - 1] == ':')
1147: copy[size - 1] = '\0';
1148:
1149: /* Check path of each client. */
1150: TAILQ_FOREACH(c, &clients, entry) {
1151: if (c->session == NULL || c->tty.path == NULL)
1152: continue;
1153: path = c->tty.path;
1154:
1155: /* Try for exact match. */
1156: if (strcmp(copy, path) == 0)
1157: break;
1158:
1159: /* Try without leading /dev. */
1160: if (strncmp(path, _PATH_DEV, (sizeof _PATH_DEV) - 1) != 0)
1161: continue;
1162: if (strcmp(copy, path + (sizeof _PATH_DEV) - 1) == 0)
1163: break;
1164: }
1165:
1166: /* If no client found, report an error. */
1167: if (c == NULL && !quiet)
1168: cmdq_error(cmdq, "can't find client %s", copy);
1169:
1170: free(copy);
1.16 nicm 1171: log_debug("%s: target %s, return %p", __func__, target, c);
1.1 nicm 1172: return (c);
1173: }