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

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