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

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