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