[BACK]Return to cmd-find.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / tmux

Annotation of src/usr.bin/tmux/cmd-find.c, Revision 1.41

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