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

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