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