Annotation of src/usr.bin/tmux/cmd-find.c, Revision 1.19
1.19 ! nicm 1: /* $OpenBSD: cmd-find.c,v 1.18 2015/12/13 15:00:37 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.6 nicm 470: if (~fs->flags & CMD_FIND_WINDOW_INDEX) {
471: fs->wl = fs->s->curw;
472: fs->w = fs->wl->window;
473: fs->idx = fs->wl->idx;
474: }
1.4 nicm 475: return (0);
476: }
477:
478: return (-1);
1.1 nicm 479: }
480:
481: /*
482: * Find window from string, assuming it is in given session. Needs s, fills in
483: * wl and w.
484: */
485: int
486: cmd_find_get_window_with_session(struct cmd_find_state *fs, const char *window)
487: {
488: struct winlink *wl;
489: const char *errstr;
1.9 nicm 490: int idx, n, exact;
1.1 nicm 491: struct session *s;
492:
493: log_debug("%s: %s", __func__, window);
1.9 nicm 494: exact = (fs->flags & CMD_FIND_EXACT_WINDOW);
1.1 nicm 495:
496: /* Check for window ids starting with @. */
497: if (*window == '@') {
498: fs->w = window_find_by_id_str(window);
499: if (fs->w == NULL || !session_has(fs->s, fs->w))
500: return (-1);
501: return (cmd_find_best_winlink_with_window(fs));
502: }
503:
504: /* Try as an offset. */
1.10 nicm 505: if (!exact && (window[0] == '+' || window[0] == '-')) {
1.1 nicm 506: if (window[1] != '\0')
507: n = strtonum(window + 1, 1, INT_MAX, NULL);
508: else
509: n = 1;
510: s = fs->s;
511: if (fs->flags & CMD_FIND_WINDOW_INDEX) {
512: if (window[0] == '+') {
513: if (INT_MAX - s->curw->idx < n)
514: return (-1);
515: fs->idx = s->curw->idx + n;
516: } else {
517: if (n < s->curw->idx)
518: return (-1);
519: fs->idx = s->curw->idx - n;
520: }
521: return (0);
522: }
523: if (window[0] == '+')
524: fs->wl = winlink_next_by_number(s->curw, s, n);
525: else
526: fs->wl = winlink_previous_by_number(s->curw, s, n);
527: if (fs->wl != NULL) {
528: fs->idx = fs->wl->idx;
529: fs->w = fs->wl->window;
530: return (0);
531: }
532: }
533:
534: /* Try special characters. */
1.9 nicm 535: if (!exact) {
536: if (strcmp(window, "!") == 0) {
537: fs->wl = TAILQ_FIRST(&fs->s->lastw);
538: if (fs->wl == NULL)
539: return (-1);
540: fs->idx = fs->wl->idx;
541: fs->w = fs->wl->window;
542: return (0);
543: } else if (strcmp(window, "^") == 0) {
544: fs->wl = RB_MIN(winlinks, &fs->s->windows);
545: if (fs->wl == NULL)
546: return (-1);
547: fs->idx = fs->wl->idx;
548: fs->w = fs->wl->window;
549: return (0);
550: } else if (strcmp(window, "$") == 0) {
551: fs->wl = RB_MAX(winlinks, &fs->s->windows);
552: if (fs->wl == NULL)
553: return (-1);
554: fs->idx = fs->wl->idx;
555: fs->w = fs->wl->window;
556: return (0);
557: }
1.1 nicm 558: }
559:
560: /* First see if this is a valid window index in this session. */
1.9 nicm 561: if (window[0] != '+' && window[0] != '-') {
562: idx = strtonum(window, 0, INT_MAX, &errstr);
563: if (errstr == NULL) {
564: if (fs->flags & CMD_FIND_WINDOW_INDEX) {
565: fs->idx = idx;
566: return (0);
567: }
568: fs->wl = winlink_find_by_index(&fs->s->windows, idx);
569: if (fs->wl != NULL) {
570: fs->w = fs->wl->window;
571: return (0);
572: }
1.1 nicm 573: }
574: }
575:
576: /* Look for exact matches, error if more than one. */
577: fs->wl = NULL;
578: RB_FOREACH(wl, winlinks, &fs->s->windows) {
579: if (strcmp(window, wl->window->name) == 0) {
580: if (fs->wl != NULL)
581: return (-1);
582: fs->wl = wl;
583: }
584: }
585: if (fs->wl != NULL) {
586: fs->idx = fs->wl->idx;
587: fs->w = fs->wl->window;
588: return (0);
589: }
1.9 nicm 590:
591: /* Stop now if exact only. */
592: if (exact)
593: return (-1);
594:
1.1 nicm 595: /* Try as the start of a window name, error if multiple. */
596: fs->wl = NULL;
597: RB_FOREACH(wl, winlinks, &fs->s->windows) {
598: if (strncmp(window, wl->window->name, strlen(window)) == 0) {
599: if (fs->wl != NULL)
600: return (-1);
601: fs->wl = wl;
602: }
603: }
604: if (fs->wl != NULL) {
605: fs->idx = fs->wl->idx;
606: fs->w = fs->wl->window;
607: return (0);
608: }
609:
610: /* Now look for pattern matches, again error if multiple. */
611: fs->wl = NULL;
612: RB_FOREACH(wl, winlinks, &fs->s->windows) {
613: if (fnmatch(window, wl->window->name, 0) == 0) {
614: if (fs->wl != NULL)
615: return (-1);
616: fs->wl = wl;
617: }
618: }
619: if (fs->wl != NULL) {
620: fs->idx = fs->wl->idx;
621: fs->w = fs->wl->window;
622: return (0);
623: }
624:
625: return (-1);
626: }
627:
628: /* Find window from given pane. Needs wp, fills in s and wl and w. */
629: int
630: cmd_find_get_window_with_pane(struct cmd_find_state *fs)
631: {
632: log_debug("%s", __func__);
633:
634: fs->w = fs->wp->window;
635: return (cmd_find_best_session_with_window(fs));
636: }
637:
638: /* Find pane from string. Fills in s, wl, w, wp. */
639: int
640: cmd_find_get_pane(struct cmd_find_state *fs, const char *pane)
641: {
642: log_debug("%s: %s", __func__, pane);
643:
644: /* Check for pane ids starting with %. */
645: if (*pane == '%') {
646: fs->wp = window_pane_find_by_id_str(pane);
647: if (fs->wp == NULL)
648: return (-1);
649: fs->w = fs->wp->window;
650: return (cmd_find_best_session_with_window(fs));
651: }
652:
1.4 nicm 653: /* Not a pane id, so try the current session and window. */
1.1 nicm 654: fs->s = fs->current->s;
655: fs->wl = fs->current->wl;
656: fs->idx = fs->current->idx;
657: fs->w = fs->current->w;
658:
659: /* We now only need to find the pane in this window. */
1.4 nicm 660: if (cmd_find_get_pane_with_window(fs, pane) == 0)
661: return (0);
662:
663: /* Otherwise try as a window itself (this will also try as session). */
664: if (cmd_find_get_window(fs, pane) == 0) {
665: fs->wp = fs->w->active;
666: return (0);
667: }
668:
669: return (-1);
1.1 nicm 670: }
671:
672: /*
673: * Find pane from string, assuming it is in given session. Needs s, fills in wl
674: * and w and wp.
675: */
676: int
677: cmd_find_get_pane_with_session(struct cmd_find_state *fs, const char *pane)
678: {
679: log_debug("%s: %s", __func__, pane);
680:
681: /* Check for pane ids starting with %. */
682: if (*pane == '%') {
683: fs->wp = window_pane_find_by_id_str(pane);
684: if (fs->wp == NULL)
685: return (-1);
686: fs->w = fs->wp->window;
687: return (cmd_find_best_winlink_with_window(fs));
688: }
689:
690: /* Otherwise use the current window. */
691: fs->wl = fs->s->curw;
692: fs->idx = fs->wl->idx;
693: fs->w = fs->wl->window;
694:
695: /* Now we just need to look up the pane. */
696: return (cmd_find_get_pane_with_window(fs, pane));
697: }
698:
699: /*
700: * Find pane from string, assuming it is in the given window. Needs w, fills in
701: * wp.
702: */
703: int
704: cmd_find_get_pane_with_window(struct cmd_find_state *fs, const char *pane)
705: {
706: const char *errstr;
707: int idx;
708: struct window_pane *wp;
709: u_int n;
710:
711: log_debug("%s: %s", __func__, pane);
712:
713: /* Check for pane ids starting with %. */
714: if (*pane == '%') {
715: fs->wp = window_pane_find_by_id_str(pane);
716: if (fs->wp == NULL || fs->wp->window != fs->w)
717: return (-1);
718: return (0);
719: }
720:
721: /* Try special characters. */
722: if (strcmp(pane, "!") == 0) {
723: if (fs->w->last == NULL)
724: return (-1);
725: fs->wp = fs->w->last;
726: return (0);
1.12 nicm 727: } else if (strcmp(pane, "{up-of}") == 0) {
1.1 nicm 728: fs->wp = window_pane_find_up(fs->w->active);
729: if (fs->wp == NULL)
730: return (-1);
731: return (0);
1.12 nicm 732: } else if (strcmp(pane, "{down-of}") == 0) {
1.1 nicm 733: fs->wp = window_pane_find_down(fs->w->active);
734: if (fs->wp == NULL)
735: return (-1);
736: return (0);
1.12 nicm 737: } else if (strcmp(pane, "{left-of}") == 0) {
1.1 nicm 738: fs->wp = window_pane_find_left(fs->w->active);
739: if (fs->wp == NULL)
740: return (-1);
741: return (0);
1.12 nicm 742: } else if (strcmp(pane, "{right-of}") == 0) {
1.1 nicm 743: fs->wp = window_pane_find_right(fs->w->active);
744: if (fs->wp == NULL)
745: return (-1);
746: return (0);
747: }
748:
749: /* Try as an offset. */
750: if (pane[0] == '+' || pane[0] == '-') {
751: if (pane[1] != '\0')
752: n = strtonum(pane + 1, 1, INT_MAX, NULL);
753: else
754: n = 1;
755: wp = fs->w->active;
756: if (pane[0] == '+')
757: fs->wp = window_pane_next_by_number(fs->w, wp, n);
758: else
759: fs->wp = window_pane_previous_by_number(fs->w, wp, n);
760: if (fs->wp != NULL)
761: return (0);
762: }
763:
764: /* Get pane by index. */
765: idx = strtonum(pane, 0, INT_MAX, &errstr);
766: if (errstr == NULL) {
767: fs->wp = window_pane_at_index(fs->w, idx);
768: if (fs->wp != NULL)
769: return (0);
770: }
771:
772: /* Try as a description. */
773: fs->wp = window_find_string(fs->w, pane);
774: if (fs->wp != NULL)
775: return (0);
776:
777: return (-1);
778: }
779:
780: /* Clear state. */
781: void
782: cmd_find_clear_state(struct cmd_find_state *fs, struct cmd_q *cmdq, int flags)
783: {
1.7 nicm 784: memset(fs, 0, sizeof *fs);
1.1 nicm 785:
786: fs->cmdq = cmdq;
787: fs->flags = flags;
788:
789: fs->idx = -1;
790: }
791:
792: /* Split target into pieces and resolve for the given type. */
793: struct cmd_find_state *
794: cmd_find_target(struct cmd_q *cmdq, const char *target, enum cmd_find_type type,
795: int flags)
796: {
797: static struct cmd_find_state fs, current;
798: struct mouse_event *m;
799: char *colon, *period, *copy = NULL;
800: const char *session, *window, *pane;
801:
1.18 nicm 802: /* Log the arguments. */
803: if (target == NULL)
804: log_debug("%s: target none, type %d", __func__, type);
805: else
806: log_debug("%s: target %s, type %d", __func__, target, type);
807: log_debug("%s: cmdq %p, flags %#x", __func__, cmdq, flags);
808:
1.1 nicm 809: /* Find current state. */
810: cmd_find_clear_state(¤t, cmdq, flags);
1.8 nicm 811: if (server_check_marked() && (flags & CMD_FIND_DEFAULT_MARKED)) {
812: current.s = marked_session;
813: current.wl = marked_winlink;
814: current.idx = current.wl->idx;
815: current.w = current.wl->window;
816: current.wp = marked_window_pane;
817: }
818: if (current.s == NULL && cmd_find_current_session(¤t) != 0) {
1.1 nicm 819: if (~flags & CMD_FIND_QUIET)
820: cmdq_error(cmdq, "no current session");
821: goto error;
822: }
823:
824: /* Clear new state. */
825: cmd_find_clear_state(&fs, cmdq, flags);
826: fs.current = ¤t;
827:
828: /* An empty or NULL target is the current. */
829: if (target == NULL || *target == '\0')
830: goto current;
831:
832: /* Mouse target is a plain = or {mouse}. */
833: if (strcmp(target, "=") == 0 || strcmp(target, "{mouse}") == 0) {
834: m = &cmdq->item->mouse;
835: switch (type) {
836: case CMD_FIND_PANE:
837: fs.wp = cmd_mouse_pane(m, &fs.s, &fs.wl);
838: if (fs.wp != NULL)
839: fs.w = fs.wl->window;
840: break;
841: case CMD_FIND_WINDOW:
842: case CMD_FIND_SESSION:
843: fs.wl = cmd_mouse_window(m, &fs.s);
844: if (fs.wl != NULL) {
845: fs.w = fs.wl->window;
846: fs.wp = fs.w->active;
847: }
848: break;
849: }
850: if (fs.wp == NULL) {
851: if (~flags & CMD_FIND_QUIET)
852: cmdq_error(cmdq, "no mouse target");
853: goto error;
854: }
1.18 nicm 855: goto found;
1.1 nicm 856: }
1.8 nicm 857:
858: /* Marked target is a plain ~ or {marked}. */
859: if (strcmp(target, "~") == 0 || strcmp(target, "{marked}") == 0) {
860: if (!server_check_marked()) {
861: if (~flags & CMD_FIND_QUIET)
862: cmdq_error(cmdq, "no marked target");
863: goto error;
864: }
865: fs.s = marked_session;
866: fs.wl = marked_winlink;
867: fs.idx = fs.wl->idx;
868: fs.w = fs.wl->window;
869: fs.wp = marked_window_pane;
1.18 nicm 870: goto found;
1.8 nicm 871: }
1.1 nicm 872:
873: /* Find separators if they exist. */
1.8 nicm 874: copy = xstrdup(target);
1.1 nicm 875: colon = strchr(copy, ':');
876: if (colon != NULL)
877: *colon++ = '\0';
878: if (colon == NULL)
879: period = strchr(copy, '.');
880: else
881: period = strchr(colon, '.');
882: if (period != NULL)
883: *period++ = '\0';
884:
885: /* Set session, window and pane parts. */
886: session = window = pane = NULL;
887: if (colon != NULL && period != NULL) {
888: session = copy;
889: window = colon;
890: pane = period;
891: } else if (colon != NULL && period == NULL) {
892: session = copy;
893: window = colon;
894: } else if (colon == NULL && period != NULL) {
895: window = copy;
896: pane = period;
897: } else {
898: if (*copy == '$')
899: session = copy;
900: else if (*copy == '@')
901: window = copy;
902: else if (*copy == '%')
903: pane = copy;
904: else {
905: switch (type) {
906: case CMD_FIND_SESSION:
907: session = copy;
908: break;
909: case CMD_FIND_WINDOW:
910: window = copy;
911: break;
912: case CMD_FIND_PANE:
913: pane = copy;
914: break;
915: }
916: }
1.9 nicm 917: }
918:
919: /* Set exact match flags. */
920: if (session != NULL && *session == '=') {
921: session++;
922: fs.flags |= CMD_FIND_EXACT_SESSION;
923: }
924: if (window != NULL && *window == '=') {
925: window++;
926: fs.flags |= CMD_FIND_EXACT_WINDOW;
1.1 nicm 927: }
928:
929: /* Empty is the same as NULL. */
930: if (session != NULL && *session == '\0')
931: session = NULL;
932: if (window != NULL && *window == '\0')
933: window = NULL;
934: if (pane != NULL && *pane == '\0')
935: pane = NULL;
936:
937: /* Map though conversion table. */
938: if (session != NULL)
939: session = cmd_find_map_table(cmd_find_session_table, session);
940: if (window != NULL)
941: window = cmd_find_map_table(cmd_find_window_table, window);
942: if (pane != NULL)
943: pane = cmd_find_map_table(cmd_find_pane_table, pane);
944:
945: log_debug("target %s (flags %#x): session=%s, window=%s, pane=%s",
946: target, flags, session == NULL ? "none" : session,
947: window == NULL ? "none" : window, pane == NULL ? "none" : pane);
948:
949: /* No pane is allowed if want an index. */
950: if (pane != NULL && (flags & CMD_FIND_WINDOW_INDEX)) {
951: if (~flags & CMD_FIND_QUIET)
952: cmdq_error(cmdq, "can't specify pane here");
953: goto error;
954: }
955:
956: /* If the session isn't NULL, look it up. */
957: if (session != NULL) {
958: /* This will fill in session. */
959: if (cmd_find_get_session(&fs, session) != 0)
960: goto no_session;
961:
962: /* If window and pane are NULL, use that session's current. */
963: if (window == NULL && pane == NULL) {
964: fs.wl = fs.s->curw;
965: fs.idx = -1;
966: fs.w = fs.wl->window;
967: fs.wp = fs.w->active;
968: goto found;
969: }
970:
971: /* If window is present but pane not, find window in session. */
972: if (window != NULL && pane == NULL) {
973: /* This will fill in winlink and window. */
974: if (cmd_find_get_window_with_session(&fs, window) != 0)
975: goto no_window;
976: if (~flags & CMD_FIND_WINDOW_INDEX)
977: fs.wp = fs.wl->window->active;
978: goto found;
979: }
980:
981: /* If pane is present but window not, find pane. */
982: if (window == NULL && pane != NULL) {
983: /* This will fill in winlink and window and pane. */
984: if (cmd_find_get_pane_with_session(&fs, pane) != 0)
985: goto no_pane;
986: goto found;
987: }
988:
989: /*
990: * If window and pane are present, find both in session. This
991: * will fill in winlink and window.
992: */
993: if (cmd_find_get_window_with_session(&fs, window) != 0)
994: goto no_window;
995: /* This will fill in pane. */
996: if (cmd_find_get_pane_with_window(&fs, pane) != 0)
997: goto no_pane;
998: goto found;
999: }
1000:
1001: /* No session. If window and pane, try them. */
1002: if (window != NULL && pane != NULL) {
1003: /* This will fill in session, winlink and window. */
1004: if (cmd_find_get_window(&fs, window) != 0)
1005: goto no_window;
1006: /* This will fill in pane. */
1007: if (cmd_find_get_pane_with_window(&fs, pane) != 0)
1008: goto no_pane;
1009: goto found;
1010: }
1011:
1012: /* If just window is present, try it. */
1013: if (window != NULL && pane == NULL) {
1014: /* This will fill in session, winlink and window. */
1015: if (cmd_find_get_window(&fs, window) != 0)
1016: goto no_window;
1017: if (~flags & CMD_FIND_WINDOW_INDEX)
1018: fs.wp = fs.wl->window->active;
1019: goto found;
1020: }
1021:
1022: /* If just pane is present, try it. */
1023: if (window == NULL && pane != NULL) {
1024: /* This will fill in session, winlink, window and pane. */
1025: if (cmd_find_get_pane(&fs, pane) != 0)
1026: goto no_pane;
1027: goto found;
1028: }
1029:
1030: current:
1031: /* None is the current session. */
1032: free(copy);
1033: if (flags & CMD_FIND_WINDOW_INDEX)
1034: current.idx = -1;
1.18 nicm 1035: cmd_find_log_state(__func__, ¤t);
1.1 nicm 1036: return (¤t);
1037:
1038: error:
1039: free(copy);
1.18 nicm 1040: log_debug(" error");
1.1 nicm 1041: return (NULL);
1042:
1043: found:
1.18 nicm 1044: cmd_find_log_state(__func__, &fs);
1.1 nicm 1045: free(copy);
1046: return (&fs);
1047:
1048: no_session:
1049: if (~flags & CMD_FIND_QUIET)
1050: cmdq_error(cmdq, "can't find session %s", session);
1051: goto error;
1052:
1053: no_window:
1054: if (~flags & CMD_FIND_QUIET)
1055: cmdq_error(cmdq, "can't find window %s", window);
1056: goto error;
1057:
1058: no_pane:
1059: if (~flags & CMD_FIND_QUIET)
1060: cmdq_error(cmdq, "can't find pane %s", pane);
1061: goto error;
1062: }
1063:
1064: /* Log the result. */
1065: void
1.18 nicm 1066: cmd_find_log_state(const char *prefix, struct cmd_find_state *fs)
1.1 nicm 1067: {
1068: if (fs->s != NULL)
1.18 nicm 1069: log_debug("%s: s=$%u", prefix, fs->s->id);
1.1 nicm 1070: else
1.18 nicm 1071: log_debug("%s: s=none", prefix);
1.1 nicm 1072: if (fs->wl != NULL) {
1.18 nicm 1073: log_debug("%s: wl=%u %d w=@%u %s", prefix, fs->wl->idx,
1.1 nicm 1074: fs->wl->window == fs->w, fs->w->id, fs->w->name);
1075: } else
1.18 nicm 1076: log_debug("%s: wl=none", prefix);
1.1 nicm 1077: if (fs->wp != NULL)
1.18 nicm 1078: log_debug("%s: wp=%%%u", prefix, fs->wp->id);
1.1 nicm 1079: else
1.18 nicm 1080: log_debug("%s: wp=none", prefix);
1.1 nicm 1081: if (fs->idx != -1)
1.18 nicm 1082: log_debug("%s: idx=%d", prefix, fs->idx);
1.1 nicm 1083: else
1.18 nicm 1084: log_debug("%s: idx=none", prefix);
1.1 nicm 1085: }
1086:
1087: /* Find the target client or report an error and return NULL. */
1088: struct client *
1089: cmd_find_client(struct cmd_q *cmdq, const char *target, int quiet)
1090: {
1091: struct client *c;
1092: char *copy;
1093: size_t size;
1094: const char *path;
1095:
1096: /* A NULL argument means the current client. */
1097: if (target == NULL) {
1098: c = cmd_find_current_client(cmdq);
1099: if (c == NULL && !quiet)
1100: cmdq_error(cmdq, "no current client");
1.16 nicm 1101: log_debug("%s: no target, return %p", __func__, c);
1.1 nicm 1102: return (c);
1103: }
1104: copy = xstrdup(target);
1105:
1106: /* Trim a single trailing colon if any. */
1107: size = strlen(copy);
1108: if (size != 0 && copy[size - 1] == ':')
1109: copy[size - 1] = '\0';
1110:
1111: /* Check path of each client. */
1112: TAILQ_FOREACH(c, &clients, entry) {
1113: if (c->session == NULL || c->tty.path == NULL)
1114: continue;
1115: path = c->tty.path;
1116:
1117: /* Try for exact match. */
1118: if (strcmp(copy, path) == 0)
1119: break;
1120:
1121: /* Try without leading /dev. */
1122: if (strncmp(path, _PATH_DEV, (sizeof _PATH_DEV) - 1) != 0)
1123: continue;
1124: if (strcmp(copy, path + (sizeof _PATH_DEV) - 1) == 0)
1125: break;
1126: }
1127:
1128: /* If no client found, report an error. */
1129: if (c == NULL && !quiet)
1130: cmdq_error(cmdq, "can't find client %s", copy);
1131:
1132: free(copy);
1.16 nicm 1133: log_debug("%s: target %s, return %p", __func__, target, c);
1.1 nicm 1134: return (c);
1135: }