[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.33

1.33    ! nicm        1: /* $OpenBSD: cmd-find.c,v 1.32 2016/03/03 14:14:46 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 *);
        !            41: static struct client *cmd_find_current_client(struct cmd_q *);
        !            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.27      nicm      195:        if (fs->cmdq != NULL && fs->cmdq->client != NULL) {
1.15      nicm      196:                fs->s = cmd_find_try_TMUX(fs->cmdq->client, fs->w);
                    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;
                    231:        if (fs->s->curw->window == fs->w)
                    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.27      nicm      259:        if (fs->cmdq != NULL && fs->cmdq->client->tty.path != NULL) {
1.5       nicm      260:                RB_FOREACH(wp, window_pane_tree, &all_window_panes) {
                    261:                        if (strcmp(wp->tty, fs->cmdq->client->tty.path) == 0)
                    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;
                    295:        if (fs->cmdq != NULL)
                    296:                fs->s = cmd_find_try_TMUX(fs->cmdq->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.27      nicm      317:        if (fs->cmdq != NULL && fs->cmdq->client != NULL) {
1.16      nicm      318:                log_debug("%s: have client %p%s", __func__, fs->cmdq->client,
                    319:                    fs->cmdq->client->session == NULL ? "" : " (with session)");
1.1       nicm      320:                if (fs->cmdq->client->session == NULL)
                    321:                        return (cmd_find_current_session_with_client(fs));
                    322:                fs->s = fs->cmdq->client->session;
                    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.1       nicm      344: cmd_find_current_client(struct cmd_q *cmdq)
                    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.16      nicm      352:        if (cmdq->client != NULL && cmdq->client->session != NULL) {
                    353:                log_debug("%s: using cmdq %p client %p", __func__, cmdq,
                    354:                    cmdq->client);
1.1       nicm      355:                return (cmdq->client);
1.16      nicm      356:        }
1.1       nicm      357:
                    358:        /* Otherwise find the current session. */
                    359:        cmd_find_clear_state(&current, cmdq, 0);
                    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:
                    486:        /* Otherwise try as a session itself. */
                    487:        if (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);
                    661:                if (fs->wp == NULL)
                    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:
                    677:        /* Otherwise try as a window itself (this will also try as session). */
                    678:        if (cmd_find_get_window(fs, pane) == 0) {
                    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);
                    698:                if (fs->wp == NULL)
                    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);
                    730:                if (fs->wp == NULL || fs->wp->window != fs->w)
                    731:                        return (-1);
                    732:                return (0);
                    733:        }
                    734:
                    735:        /* Try special characters. */
                    736:        if (strcmp(pane, "!") == 0) {
                    737:                if (fs->w->last == NULL)
                    738:                        return (-1);
                    739:                fs->wp = fs->w->last;
                    740:                return (0);
1.12      nicm      741:        } else if (strcmp(pane, "{up-of}") == 0) {
1.1       nicm      742:                fs->wp = window_pane_find_up(fs->w->active);
                    743:                if (fs->wp == NULL)
                    744:                        return (-1);
                    745:                return (0);
1.12      nicm      746:        } else if (strcmp(pane, "{down-of}") == 0) {
1.1       nicm      747:                fs->wp = window_pane_find_down(fs->w->active);
                    748:                if (fs->wp == NULL)
                    749:                        return (-1);
                    750:                return (0);
1.12      nicm      751:        } else if (strcmp(pane, "{left-of}") == 0) {
1.1       nicm      752:                fs->wp = window_pane_find_left(fs->w->active);
                    753:                if (fs->wp == NULL)
                    754:                        return (-1);
                    755:                return (0);
1.12      nicm      756:        } else if (strcmp(pane, "{right-of}") == 0) {
1.1       nicm      757:                fs->wp = window_pane_find_right(fs->w->active);
                    758:                if (fs->wp == NULL)
                    759:                        return (-1);
                    760:                return (0);
                    761:        }
                    762:
                    763:        /* Try as an offset. */
                    764:        if (pane[0] == '+' || pane[0] == '-') {
                    765:                if (pane[1] != '\0')
                    766:                        n = strtonum(pane + 1, 1, INT_MAX, NULL);
                    767:                else
                    768:                        n = 1;
                    769:                wp = fs->w->active;
                    770:                if (pane[0] == '+')
                    771:                        fs->wp = window_pane_next_by_number(fs->w, wp, n);
                    772:                else
                    773:                        fs->wp = window_pane_previous_by_number(fs->w, wp, n);
                    774:                if (fs->wp != NULL)
                    775:                        return (0);
                    776:        }
                    777:
                    778:        /* Get pane by index. */
                    779:        idx = strtonum(pane, 0, INT_MAX, &errstr);
                    780:        if (errstr == NULL) {
                    781:                fs->wp = window_pane_at_index(fs->w, idx);
                    782:                if (fs->wp != NULL)
                    783:                        return (0);
                    784:        }
                    785:
                    786:        /* Try as a description. */
                    787:        fs->wp = window_find_string(fs->w, pane);
                    788:        if (fs->wp != NULL)
                    789:                return (0);
                    790:
                    791:        return (-1);
                    792: }
                    793:
                    794: /* Clear state. */
                    795: void
                    796: cmd_find_clear_state(struct cmd_find_state *fs, struct cmd_q *cmdq, int flags)
                    797: {
1.7       nicm      798:        memset(fs, 0, sizeof *fs);
1.1       nicm      799:
                    800:        fs->cmdq = cmdq;
                    801:        fs->flags = flags;
                    802:
                    803:        fs->idx = -1;
                    804: }
                    805:
1.23      nicm      806: /* Check if a state if valid. */
                    807: int
                    808: cmd_find_valid_state(struct cmd_find_state *fs)
                    809: {
                    810:        struct winlink  *wl;
                    811:
                    812:        if (fs->s == NULL || fs->wl == NULL || fs->w == NULL || fs->wp == NULL)
                    813:                return (0);
                    814:
                    815:        if (!session_alive(fs->s))
                    816:                return (0);
                    817:
                    818:        RB_FOREACH(wl, winlinks, &fs->s->windows) {
                    819:                if (wl->window == fs->w && wl == fs->wl)
                    820:                        break;
                    821:        }
                    822:        if (wl == NULL)
                    823:                return (0);
                    824:
                    825:        if (fs->w != fs->wl->window)
                    826:                return (0);
                    827:
                    828:        if (!window_has_pane(fs->w, fs->wp))
                    829:                return (0);
                    830:        return (window_pane_visible(fs->wp));
                    831: }
                    832:
                    833: /* Copy a state. */
                    834: void
                    835: cmd_find_copy_state(struct cmd_find_state *dst, struct cmd_find_state *src)
                    836: {
                    837:        dst->s = src->s;
                    838:        dst->wl = src->wl;
1.26      nicm      839:        dst->idx = src->idx;
                    840:        dst->w = src->w;
1.23      nicm      841:        dst->wp = src->wp;
                    842: }
                    843:
                    844: /* Log the result. */
                    845: void
                    846: cmd_find_log_state(const char *prefix, struct cmd_find_state *fs)
                    847: {
                    848:        if (fs->s != NULL)
                    849:                log_debug("%s: s=$%u", prefix, fs->s->id);
                    850:        else
                    851:                log_debug("%s: s=none", prefix);
                    852:        if (fs->wl != NULL) {
                    853:                log_debug("%s: wl=%u %d w=@%u %s", prefix, fs->wl->idx,
                    854:                    fs->wl->window == fs->w, fs->w->id, fs->w->name);
                    855:        } else
                    856:                log_debug("%s: wl=none", prefix);
                    857:        if (fs->wp != NULL)
                    858:                log_debug("%s: wp=%%%u", prefix, fs->wp->id);
                    859:        else
                    860:                log_debug("%s: wp=none", prefix);
                    861:        if (fs->idx != -1)
                    862:                log_debug("%s: idx=%d", prefix, fs->idx);
                    863:        else
                    864:                log_debug("%s: idx=none", prefix);
1.27      nicm      865: }
                    866:
                    867: /* Find state from a session. */
                    868: int
                    869: cmd_find_from_session(struct cmd_find_state *fs, struct session *s)
                    870: {
                    871:        cmd_find_clear_state(fs, NULL, 0);
                    872:
                    873:        fs->s = s;
                    874:        fs->wl = fs->s->curw;
                    875:        fs->w = fs->wl->window;
                    876:        fs->wp = fs->w->active;
1.29      nicm      877:
                    878:        cmd_find_log_state(__func__, fs);
                    879:        return (0);
                    880: }
                    881:
                    882: /* Find state from a winlink. */
                    883: int
                    884: cmd_find_from_winlink(struct cmd_find_state *fs, struct session *s,
                    885:     struct winlink *wl)
                    886: {
                    887:        cmd_find_clear_state(fs, NULL, 0);
                    888:
                    889:        fs->s = s;
                    890:        fs->wl = wl;
                    891:        fs->w = wl->window;
                    892:        fs->wp = wl->window->active;
1.27      nicm      893:
                    894:        cmd_find_log_state(__func__, fs);
                    895:        return (0);
                    896: }
                    897:
                    898: /* Find state from a window. */
                    899: int
                    900: cmd_find_from_window(struct cmd_find_state *fs, struct window *w)
                    901: {
                    902:        cmd_find_clear_state(fs, NULL, 0);
                    903:
                    904:        fs->w = w;
                    905:        if (cmd_find_best_session_with_window(fs) != 0)
                    906:                return (-1);
                    907:        if (cmd_find_best_winlink_with_window(fs) != 0)
                    908:                return (-1);
                    909:
                    910:        cmd_find_log_state(__func__, fs);
                    911:        return (0);
                    912: }
                    913:
                    914: /* Find state from a pane. */
                    915: int
                    916: cmd_find_from_pane(struct cmd_find_state *fs, struct window_pane *wp)
                    917: {
                    918:        if (cmd_find_from_window(fs, wp->window) != 0)
                    919:                return (-1);
                    920:        fs->wp = wp;
                    921:
                    922:        cmd_find_log_state(__func__, fs);
                    923:        return (0);
1.23      nicm      924: }
                    925:
1.31      nicm      926: /* Find current state. */
                    927: int
                    928: cmd_find_current(struct cmd_find_state *fs, struct cmd_q *cmdq, int flags)
                    929: {
                    930:        cmd_find_clear_state(fs, cmdq, flags);
                    931:        if (cmd_find_current_session(fs) != 0) {
                    932:                if (~flags & CMD_FIND_QUIET)
                    933:                        cmdq_error(cmdq, "no current session");
                    934:                return (-1);
                    935:        }
                    936:        return (0);
                    937: }
                    938:
1.20      nicm      939: /*
                    940:  * Split target into pieces and resolve for the given type. Fills in the given
                    941:  * state. Returns 0 on success or -1 on error.
                    942:  */
                    943: int
1.31      nicm      944: cmd_find_target(struct cmd_find_state *fs, struct cmd_find_state *current,
                    945:     struct cmd_q *cmdq, const char *target, enum cmd_find_type type, int flags)
1.1       nicm      946: {
1.20      nicm      947:        struct mouse_event      *m;
                    948:        char                    *colon, *period, *copy = NULL;
                    949:        const char              *session, *window, *pane;
1.1       nicm      950:
1.18      nicm      951:        /* Log the arguments. */
                    952:        if (target == NULL)
                    953:                log_debug("%s: target none, type %d", __func__, type);
                    954:        else
                    955:                log_debug("%s: target %s, type %d", __func__, target, type);
                    956:        log_debug("%s: cmdq %p, flags %#x", __func__, cmdq, flags);
                    957:
1.24      nicm      958:        /* Clear new state. */
                    959:        cmd_find_clear_state(fs, cmdq, flags);
                    960:
1.1       nicm      961:        /* Find current state. */
1.23      nicm      962:        if (server_check_marked() && (flags & CMD_FIND_DEFAULT_MARKED))
1.24      nicm      963:                fs->current = &marked_pane;
1.28      nicm      964:        else if (cmd_find_valid_state(&cmdq->current))
                    965:                fs->current = &cmdq->current;
1.31      nicm      966:        else
                    967:                fs->current = current;
1.1       nicm      968:
                    969:        /* An empty or NULL target is the current. */
                    970:        if (target == NULL || *target == '\0')
                    971:                goto current;
                    972:
                    973:        /* Mouse target is a plain = or {mouse}. */
                    974:        if (strcmp(target, "=") == 0 || strcmp(target, "{mouse}") == 0) {
                    975:                m = &cmdq->item->mouse;
                    976:                switch (type) {
                    977:                case CMD_FIND_PANE:
1.20      nicm      978:                        fs->wp = cmd_mouse_pane(m, &fs->s, &fs->wl);
                    979:                        if (fs->wp != NULL)
                    980:                                fs->w = fs->wl->window;
1.1       nicm      981:                        break;
                    982:                case CMD_FIND_WINDOW:
                    983:                case CMD_FIND_SESSION:
1.20      nicm      984:                        fs->wl = cmd_mouse_window(m, &fs->s);
                    985:                        if (fs->wl != NULL) {
                    986:                                fs->w = fs->wl->window;
                    987:                                fs->wp = fs->w->active;
1.1       nicm      988:                        }
                    989:                        break;
                    990:                }
1.20      nicm      991:                if (fs->wp == NULL) {
1.1       nicm      992:                        if (~flags & CMD_FIND_QUIET)
                    993:                                cmdq_error(cmdq, "no mouse target");
                    994:                        goto error;
                    995:                }
1.18      nicm      996:                goto found;
1.1       nicm      997:        }
1.8       nicm      998:
                    999:        /* Marked target is a plain ~ or {marked}. */
                   1000:        if (strcmp(target, "~") == 0 || strcmp(target, "{marked}") == 0) {
                   1001:                if (!server_check_marked()) {
                   1002:                        if (~flags & CMD_FIND_QUIET)
                   1003:                                cmdq_error(cmdq, "no marked target");
                   1004:                        goto error;
                   1005:                }
1.23      nicm     1006:                cmd_find_copy_state(fs, &marked_pane);
1.18      nicm     1007:                goto found;
1.8       nicm     1008:        }
1.1       nicm     1009:
                   1010:        /* Find separators if they exist. */
1.8       nicm     1011:        copy = xstrdup(target);
1.1       nicm     1012:        colon = strchr(copy, ':');
                   1013:        if (colon != NULL)
                   1014:                *colon++ = '\0';
                   1015:        if (colon == NULL)
                   1016:                period = strchr(copy, '.');
                   1017:        else
                   1018:                period = strchr(colon, '.');
                   1019:        if (period != NULL)
                   1020:                *period++ = '\0';
                   1021:
                   1022:        /* Set session, window and pane parts. */
                   1023:        session = window = pane = NULL;
                   1024:        if (colon != NULL && period != NULL) {
                   1025:                session = copy;
                   1026:                window = colon;
                   1027:                pane = period;
                   1028:        } else if (colon != NULL && period == NULL) {
                   1029:                session = copy;
                   1030:                window = colon;
                   1031:        } else if (colon == NULL && period != NULL) {
                   1032:                window = copy;
                   1033:                pane = period;
                   1034:        } else {
                   1035:                if (*copy == '$')
                   1036:                        session = copy;
                   1037:                else if (*copy == '@')
                   1038:                        window = copy;
                   1039:                else if (*copy == '%')
                   1040:                        pane = copy;
                   1041:                else {
                   1042:                        switch (type) {
                   1043:                        case CMD_FIND_SESSION:
                   1044:                                session = copy;
                   1045:                                break;
                   1046:                        case CMD_FIND_WINDOW:
                   1047:                                window = copy;
                   1048:                                break;
                   1049:                        case CMD_FIND_PANE:
                   1050:                                pane = copy;
                   1051:                                break;
                   1052:                        }
                   1053:                }
1.9       nicm     1054:        }
                   1055:
                   1056:        /* Set exact match flags. */
                   1057:        if (session != NULL && *session == '=') {
                   1058:                session++;
1.20      nicm     1059:                fs->flags |= CMD_FIND_EXACT_SESSION;
1.9       nicm     1060:        }
                   1061:        if (window != NULL && *window == '=') {
                   1062:                window++;
1.20      nicm     1063:                fs->flags |= CMD_FIND_EXACT_WINDOW;
1.1       nicm     1064:        }
                   1065:
                   1066:        /* Empty is the same as NULL. */
                   1067:        if (session != NULL && *session == '\0')
                   1068:                session = NULL;
                   1069:        if (window != NULL && *window == '\0')
                   1070:                window = NULL;
                   1071:        if (pane != NULL && *pane == '\0')
                   1072:                pane = NULL;
                   1073:
                   1074:        /* Map though conversion table. */
                   1075:        if (session != NULL)
                   1076:                session = cmd_find_map_table(cmd_find_session_table, session);
                   1077:        if (window != NULL)
                   1078:                window = cmd_find_map_table(cmd_find_window_table, window);
                   1079:        if (pane != NULL)
                   1080:                pane = cmd_find_map_table(cmd_find_pane_table, pane);
                   1081:
                   1082:        log_debug("target %s (flags %#x): session=%s, window=%s, pane=%s",
                   1083:            target, flags, session == NULL ? "none" : session,
                   1084:            window == NULL ? "none" : window, pane == NULL ? "none" : pane);
                   1085:
                   1086:        /* No pane is allowed if want an index. */
                   1087:        if (pane != NULL && (flags & CMD_FIND_WINDOW_INDEX)) {
                   1088:                if (~flags & CMD_FIND_QUIET)
                   1089:                        cmdq_error(cmdq, "can't specify pane here");
                   1090:                goto error;
                   1091:        }
                   1092:
                   1093:        /* If the session isn't NULL, look it up. */
                   1094:        if (session != NULL) {
                   1095:                /* This will fill in session. */
1.20      nicm     1096:                if (cmd_find_get_session(fs, session) != 0)
1.1       nicm     1097:                        goto no_session;
                   1098:
                   1099:                /* If window and pane are NULL, use that session's current. */
                   1100:                if (window == NULL && pane == NULL) {
1.20      nicm     1101:                        fs->wl = fs->s->curw;
                   1102:                        fs->idx = -1;
                   1103:                        fs->w = fs->wl->window;
                   1104:                        fs->wp = fs->w->active;
1.1       nicm     1105:                        goto found;
                   1106:                }
                   1107:
                   1108:                /* If window is present but pane not, find window in session. */
                   1109:                if (window != NULL && pane == NULL) {
                   1110:                        /* This will fill in winlink and window. */
1.20      nicm     1111:                        if (cmd_find_get_window_with_session(fs, window) != 0)
1.1       nicm     1112:                                goto no_window;
1.21      nicm     1113:                        fs->wp = fs->wl->window->active;
1.1       nicm     1114:                        goto found;
                   1115:                }
                   1116:
                   1117:                /* If pane is present but window not, find pane. */
                   1118:                if (window == NULL && pane != NULL) {
                   1119:                        /* This will fill in winlink and window and pane. */
1.20      nicm     1120:                        if (cmd_find_get_pane_with_session(fs, pane) != 0)
1.1       nicm     1121:                                goto no_pane;
                   1122:                        goto found;
                   1123:                }
                   1124:
                   1125:                /*
                   1126:                 * If window and pane are present, find both in session. This
                   1127:                 * will fill in winlink and window.
                   1128:                 */
1.20      nicm     1129:                if (cmd_find_get_window_with_session(fs, window) != 0)
1.1       nicm     1130:                        goto no_window;
                   1131:                /* This will fill in pane. */
1.20      nicm     1132:                if (cmd_find_get_pane_with_window(fs, pane) != 0)
1.1       nicm     1133:                        goto no_pane;
                   1134:                goto found;
                   1135:        }
                   1136:
                   1137:        /* No session. If window and pane, try them. */
                   1138:        if (window != NULL && pane != NULL) {
                   1139:                /* This will fill in session, winlink and window. */
1.20      nicm     1140:                if (cmd_find_get_window(fs, window) != 0)
1.1       nicm     1141:                        goto no_window;
                   1142:                /* This will fill in pane. */
1.20      nicm     1143:                if (cmd_find_get_pane_with_window(fs, pane) != 0)
1.1       nicm     1144:                        goto no_pane;
                   1145:                goto found;
                   1146:        }
                   1147:
                   1148:        /* If just window is present, try it. */
                   1149:        if (window != NULL && pane == NULL) {
                   1150:                /* This will fill in session, winlink and window. */
1.20      nicm     1151:                if (cmd_find_get_window(fs, window) != 0)
1.1       nicm     1152:                        goto no_window;
1.21      nicm     1153:                fs->wp = fs->wl->window->active;
1.1       nicm     1154:                goto found;
                   1155:        }
                   1156:
                   1157:        /* If just pane is present, try it. */
                   1158:        if (window == NULL && pane != NULL) {
                   1159:                /* This will fill in session, winlink, window and pane. */
1.20      nicm     1160:                if (cmd_find_get_pane(fs, pane) != 0)
1.1       nicm     1161:                        goto no_pane;
                   1162:                goto found;
                   1163:        }
                   1164:
                   1165: current:
1.20      nicm     1166:        /* Use the current session. */
1.24      nicm     1167:        cmd_find_copy_state(fs, fs->current);
1.1       nicm     1168:        if (flags & CMD_FIND_WINDOW_INDEX)
1.24      nicm     1169:                fs->idx = -1;
1.20      nicm     1170:        goto found;
1.1       nicm     1171:
                   1172: error:
1.20      nicm     1173:        fs->current = NULL;
                   1174:        log_debug("    error");
                   1175:
1.1       nicm     1176:        free(copy);
1.20      nicm     1177:        return (-1);
1.1       nicm     1178:
                   1179: found:
1.20      nicm     1180:        fs->current = NULL;
                   1181:        cmd_find_log_state(__func__, fs);
                   1182:
1.1       nicm     1183:        free(copy);
1.20      nicm     1184:        return (0);
1.1       nicm     1185:
                   1186: no_session:
                   1187:        if (~flags & CMD_FIND_QUIET)
                   1188:                cmdq_error(cmdq, "can't find session %s", session);
                   1189:        goto error;
                   1190:
                   1191: no_window:
                   1192:        if (~flags & CMD_FIND_QUIET)
                   1193:                cmdq_error(cmdq, "can't find window %s", window);
                   1194:        goto error;
                   1195:
                   1196: no_pane:
                   1197:        if (~flags & CMD_FIND_QUIET)
                   1198:                cmdq_error(cmdq, "can't find pane %s", pane);
                   1199:        goto error;
                   1200: }
                   1201:
                   1202: /* Find the target client or report an error and return NULL. */
                   1203: struct client *
                   1204: cmd_find_client(struct cmd_q *cmdq, const char *target, int quiet)
                   1205: {
                   1206:        struct client   *c;
                   1207:        char            *copy;
                   1208:        size_t           size;
                   1209:        const char      *path;
                   1210:
                   1211:        /* A NULL argument means the current client. */
1.32      nicm     1212:        if (cmdq != NULL && target == NULL) {
1.1       nicm     1213:                c = cmd_find_current_client(cmdq);
                   1214:                if (c == NULL && !quiet)
                   1215:                        cmdq_error(cmdq, "no current client");
1.16      nicm     1216:                log_debug("%s: no target, return %p", __func__, c);
1.1       nicm     1217:                return (c);
                   1218:        }
                   1219:        copy = xstrdup(target);
                   1220:
                   1221:        /* Trim a single trailing colon if any. */
                   1222:        size = strlen(copy);
                   1223:        if (size != 0 && copy[size - 1] == ':')
                   1224:                copy[size - 1] = '\0';
                   1225:
                   1226:        /* Check path of each client. */
                   1227:        TAILQ_FOREACH(c, &clients, entry) {
                   1228:                if (c->session == NULL || c->tty.path == NULL)
                   1229:                        continue;
                   1230:                path = c->tty.path;
                   1231:
                   1232:                /* Try for exact match. */
                   1233:                if (strcmp(copy, path) == 0)
                   1234:                        break;
                   1235:
                   1236:                /* Try without leading /dev. */
                   1237:                if (strncmp(path, _PATH_DEV, (sizeof _PATH_DEV) - 1) != 0)
                   1238:                        continue;
                   1239:                if (strcmp(copy, path + (sizeof _PATH_DEV) - 1) == 0)
                   1240:                        break;
                   1241:        }
                   1242:
                   1243:        /* If no client found, report an error. */
                   1244:        if (c == NULL && !quiet)
                   1245:                cmdq_error(cmdq, "can't find client %s", copy);
                   1246:
                   1247:        free(copy);
1.16      nicm     1248:        log_debug("%s: target %s, return %p", __func__, target, c);
1.1       nicm     1249:        return (c);
                   1250: }