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