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