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