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