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

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