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

1.13    ! nicm        1: /* $OpenBSD: cmd-find.c,v 1.12 2015/08/13 15:02:23 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.12      nicm      105:        { "{up-of}", "{up-of}" },
                    106:        { "{down-of}", "{down-of}" },
                    107:        { "{left-of}", "{left-of}" },
                    108:        { "{right-of}", "{right-of}" },
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:        }
1.9       nicm      564:
                    565:        /* Stop now if exact only. */
                    566:        if (exact)
                    567:                return (-1);
                    568:
1.1       nicm      569:        /* Try as the start of a window name, error if multiple. */
                    570:        fs->wl = NULL;
                    571:        RB_FOREACH(wl, winlinks, &fs->s->windows) {
                    572:                if (strncmp(window, wl->window->name, strlen(window)) == 0) {
                    573:                        if (fs->wl != NULL)
                    574:                                return (-1);
                    575:                        fs->wl = wl;
                    576:                }
                    577:        }
                    578:        if (fs->wl != NULL) {
                    579:                fs->idx = fs->wl->idx;
                    580:                fs->w = fs->wl->window;
                    581:                return (0);
                    582:        }
                    583:
                    584:        /* Now look for pattern matches, again error if multiple. */
                    585:        fs->wl = NULL;
                    586:        RB_FOREACH(wl, winlinks, &fs->s->windows) {
                    587:                if (fnmatch(window, wl->window->name, 0) == 0) {
                    588:                        if (fs->wl != NULL)
                    589:                                return (-1);
                    590:                        fs->wl = wl;
                    591:                }
                    592:        }
                    593:        if (fs->wl != NULL) {
                    594:                fs->idx = fs->wl->idx;
                    595:                fs->w = fs->wl->window;
                    596:                return (0);
                    597:        }
                    598:
                    599:        return (-1);
                    600: }
                    601:
                    602: /* Find window from given pane. Needs wp, fills in s and wl and w. */
                    603: int
                    604: cmd_find_get_window_with_pane(struct cmd_find_state *fs)
                    605: {
                    606:        log_debug("%s", __func__);
                    607:
                    608:        fs->w = fs->wp->window;
                    609:        return (cmd_find_best_session_with_window(fs));
                    610: }
                    611:
                    612: /* Find pane from string. Fills in s, wl, w, wp. */
                    613: int
                    614: cmd_find_get_pane(struct cmd_find_state *fs, const char *pane)
                    615: {
                    616:        log_debug("%s: %s", __func__, pane);
                    617:
                    618:        /* Check for pane ids starting with %. */
                    619:        if (*pane == '%') {
                    620:                fs->wp = window_pane_find_by_id_str(pane);
                    621:                if (fs->wp == NULL)
                    622:                        return (-1);
                    623:                fs->w = fs->wp->window;
                    624:                return (cmd_find_best_session_with_window(fs));
                    625:        }
                    626:
1.4       nicm      627:        /* Not a pane id, so try the current session and window. */
1.1       nicm      628:        fs->s = fs->current->s;
                    629:        fs->wl = fs->current->wl;
                    630:        fs->idx = fs->current->idx;
                    631:        fs->w = fs->current->w;
                    632:
                    633:        /* We now only need to find the pane in this window. */
1.4       nicm      634:        if (cmd_find_get_pane_with_window(fs, pane) == 0)
                    635:                return (0);
                    636:
                    637:        /* Otherwise try as a window itself (this will also try as session). */
                    638:        if (cmd_find_get_window(fs, pane) == 0) {
                    639:                fs->wp = fs->w->active;
                    640:                return (0);
                    641:        }
                    642:
                    643:        return (-1);
1.1       nicm      644: }
                    645:
                    646: /*
                    647:  * Find pane from string, assuming it is in given session. Needs s, fills in wl
                    648:  * and w and wp.
                    649:  */
                    650: int
                    651: cmd_find_get_pane_with_session(struct cmd_find_state *fs, const char *pane)
                    652: {
                    653:        log_debug("%s: %s", __func__, pane);
                    654:
                    655:        /* Check for pane ids starting with %. */
                    656:        if (*pane == '%') {
                    657:                fs->wp = window_pane_find_by_id_str(pane);
                    658:                if (fs->wp == NULL)
                    659:                        return (-1);
                    660:                fs->w = fs->wp->window;
                    661:                return (cmd_find_best_winlink_with_window(fs));
                    662:        }
                    663:
                    664:        /* Otherwise use the current window. */
                    665:        fs->wl = fs->s->curw;
                    666:        fs->idx = fs->wl->idx;
                    667:        fs->w = fs->wl->window;
                    668:
                    669:        /* Now we just need to look up the pane. */
                    670:        return (cmd_find_get_pane_with_window(fs, pane));
                    671: }
                    672:
                    673: /*
                    674:  * Find pane from string, assuming it is in the given window. Needs w, fills in
                    675:  * wp.
                    676:  */
                    677: int
                    678: cmd_find_get_pane_with_window(struct cmd_find_state *fs, const char *pane)
                    679: {
                    680:        const char              *errstr;
                    681:        int                      idx;
                    682:        struct window_pane      *wp;
                    683:        u_int                    n;
                    684:
                    685:        log_debug("%s: %s", __func__, pane);
                    686:
                    687:        /* Check for pane ids starting with %. */
                    688:        if (*pane == '%') {
                    689:                fs->wp = window_pane_find_by_id_str(pane);
                    690:                if (fs->wp == NULL || fs->wp->window != fs->w)
                    691:                        return (-1);
                    692:                return (0);
                    693:        }
                    694:
                    695:        /* Try special characters. */
                    696:        if (strcmp(pane, "!") == 0) {
                    697:                if (fs->w->last == NULL)
                    698:                        return (-1);
                    699:                fs->wp = fs->w->last;
                    700:                return (0);
1.12      nicm      701:        } else if (strcmp(pane, "{up-of}") == 0) {
1.1       nicm      702:                fs->wp = window_pane_find_up(fs->w->active);
                    703:                if (fs->wp == NULL)
                    704:                        return (-1);
                    705:                return (0);
1.12      nicm      706:        } else if (strcmp(pane, "{down-of}") == 0) {
1.1       nicm      707:                fs->wp = window_pane_find_down(fs->w->active);
                    708:                if (fs->wp == NULL)
                    709:                        return (-1);
                    710:                return (0);
1.12      nicm      711:        } else if (strcmp(pane, "{left-of}") == 0) {
1.1       nicm      712:                fs->wp = window_pane_find_left(fs->w->active);
                    713:                if (fs->wp == NULL)
                    714:                        return (-1);
                    715:                return (0);
1.12      nicm      716:        } else if (strcmp(pane, "{right-of}") == 0) {
1.1       nicm      717:                fs->wp = window_pane_find_right(fs->w->active);
                    718:                if (fs->wp == NULL)
                    719:                        return (-1);
                    720:                return (0);
                    721:        }
                    722:
                    723:        /* Try as an offset. */
                    724:        if (pane[0] == '+' || pane[0] == '-') {
                    725:                if (pane[1] != '\0')
                    726:                        n = strtonum(pane + 1, 1, INT_MAX, NULL);
                    727:                else
                    728:                        n = 1;
                    729:                wp = fs->w->active;
                    730:                if (pane[0] == '+')
                    731:                        fs->wp = window_pane_next_by_number(fs->w, wp, n);
                    732:                else
                    733:                        fs->wp = window_pane_previous_by_number(fs->w, wp, n);
                    734:                if (fs->wp != NULL)
                    735:                        return (0);
                    736:        }
                    737:
                    738:        /* Get pane by index. */
                    739:        idx = strtonum(pane, 0, INT_MAX, &errstr);
                    740:        if (errstr == NULL) {
                    741:                fs->wp = window_pane_at_index(fs->w, idx);
                    742:                if (fs->wp != NULL)
                    743:                        return (0);
                    744:        }
                    745:
                    746:        /* Try as a description. */
                    747:        fs->wp = window_find_string(fs->w, pane);
                    748:        if (fs->wp != NULL)
                    749:                return (0);
                    750:
                    751:        return (-1);
                    752: }
                    753:
                    754: /* Clear state. */
                    755: void
                    756: cmd_find_clear_state(struct cmd_find_state *fs, struct cmd_q *cmdq, int flags)
                    757: {
1.7       nicm      758:        memset(fs, 0, sizeof *fs);
1.1       nicm      759:
                    760:        fs->cmdq = cmdq;
                    761:        fs->flags = flags;
                    762:
                    763:        fs->idx = -1;
                    764: }
                    765:
                    766: /* Split target into pieces and resolve for the given type. */
                    767: struct cmd_find_state *
                    768: cmd_find_target(struct cmd_q *cmdq, const char *target, enum cmd_find_type type,
                    769:     int flags)
                    770: {
                    771:        static struct cmd_find_state     fs, current;
                    772:        struct mouse_event              *m;
                    773:        char                            *colon, *period, *copy = NULL;
                    774:        const char                      *session, *window, *pane;
                    775:
                    776:        /* Find current state. */
                    777:        cmd_find_clear_state(&current, cmdq, flags);
1.8       nicm      778:        if (server_check_marked() && (flags & CMD_FIND_DEFAULT_MARKED)) {
                    779:                current.s = marked_session;
                    780:                current.wl = marked_winlink;
                    781:                current.idx = current.wl->idx;
                    782:                current.w = current.wl->window;
                    783:                current.wp = marked_window_pane;
                    784:        }
                    785:        if (current.s == NULL && cmd_find_current_session(&current) != 0) {
1.1       nicm      786:                if (~flags & CMD_FIND_QUIET)
                    787:                        cmdq_error(cmdq, "no current session");
                    788:                goto error;
                    789:        }
                    790:
                    791:        /* Clear new state. */
                    792:        cmd_find_clear_state(&fs, cmdq, flags);
                    793:        fs.current = &current;
                    794:
                    795:        /* An empty or NULL target is the current. */
                    796:        if (target == NULL || *target == '\0')
                    797:                goto current;
                    798:
                    799:        /* Mouse target is a plain = or {mouse}. */
                    800:        if (strcmp(target, "=") == 0 || strcmp(target, "{mouse}") == 0) {
                    801:                m = &cmdq->item->mouse;
                    802:                switch (type) {
                    803:                case CMD_FIND_PANE:
                    804:                        fs.wp = cmd_mouse_pane(m, &fs.s, &fs.wl);
                    805:                        if (fs.wp != NULL)
                    806:                                fs.w = fs.wl->window;
                    807:                        break;
                    808:                case CMD_FIND_WINDOW:
                    809:                case CMD_FIND_SESSION:
                    810:                        fs.wl = cmd_mouse_window(m, &fs.s);
                    811:                        if (fs.wl != NULL) {
                    812:                                fs.w = fs.wl->window;
                    813:                                fs.wp = fs.w->active;
                    814:                        }
                    815:                        break;
                    816:                }
                    817:                if (fs.wp == NULL) {
                    818:                        if (~flags & CMD_FIND_QUIET)
                    819:                                cmdq_error(cmdq, "no mouse target");
                    820:                        goto error;
                    821:                }
                    822:                return (&fs);
                    823:        }
1.8       nicm      824:
                    825:        /* Marked target is a plain ~ or {marked}. */
                    826:        if (strcmp(target, "~") == 0 || strcmp(target, "{marked}") == 0) {
                    827:                if (!server_check_marked()) {
                    828:                        if (~flags & CMD_FIND_QUIET)
                    829:                                cmdq_error(cmdq, "no marked target");
                    830:                        goto error;
                    831:                }
                    832:                fs.s = marked_session;
                    833:                fs.wl = marked_winlink;
                    834:                fs.idx = fs.wl->idx;
                    835:                fs.w = fs.wl->window;
                    836:                fs.wp = marked_window_pane;
                    837:                return (&fs);
                    838:        }
1.1       nicm      839:
                    840:        /* Find separators if they exist. */
1.8       nicm      841:        copy = xstrdup(target);
1.1       nicm      842:        colon = strchr(copy, ':');
                    843:        if (colon != NULL)
                    844:                *colon++ = '\0';
                    845:        if (colon == NULL)
                    846:                period = strchr(copy, '.');
                    847:        else
                    848:                period = strchr(colon, '.');
                    849:        if (period != NULL)
                    850:                *period++ = '\0';
                    851:
                    852:        /* Set session, window and pane parts. */
                    853:        session = window = pane = NULL;
                    854:        if (colon != NULL && period != NULL) {
                    855:                session = copy;
                    856:                window = colon;
                    857:                pane = period;
                    858:        } else if (colon != NULL && period == NULL) {
                    859:                session = copy;
                    860:                window = colon;
                    861:        } else if (colon == NULL && period != NULL) {
                    862:                window = copy;
                    863:                pane = period;
                    864:        } else {
                    865:                if (*copy == '$')
                    866:                        session = copy;
                    867:                else if (*copy == '@')
                    868:                        window = copy;
                    869:                else if (*copy == '%')
                    870:                        pane = copy;
                    871:                else {
                    872:                        switch (type) {
                    873:                        case CMD_FIND_SESSION:
                    874:                                session = copy;
                    875:                                break;
                    876:                        case CMD_FIND_WINDOW:
                    877:                                window = copy;
                    878:                                break;
                    879:                        case CMD_FIND_PANE:
                    880:                                pane = copy;
                    881:                                break;
                    882:                        }
                    883:                }
1.9       nicm      884:        }
                    885:
                    886:        /* Set exact match flags. */
                    887:        if (session != NULL && *session == '=') {
                    888:                session++;
                    889:                fs.flags |= CMD_FIND_EXACT_SESSION;
                    890:        }
                    891:        if (window != NULL && *window == '=') {
                    892:                window++;
                    893:                fs.flags |= CMD_FIND_EXACT_WINDOW;
1.1       nicm      894:        }
                    895:
                    896:        /* Empty is the same as NULL. */
                    897:        if (session != NULL && *session == '\0')
                    898:                session = NULL;
                    899:        if (window != NULL && *window == '\0')
                    900:                window = NULL;
                    901:        if (pane != NULL && *pane == '\0')
                    902:                pane = NULL;
                    903:
                    904:        /* Map though conversion table. */
                    905:        if (session != NULL)
                    906:                session = cmd_find_map_table(cmd_find_session_table, session);
                    907:        if (window != NULL)
                    908:                window = cmd_find_map_table(cmd_find_window_table, window);
                    909:        if (pane != NULL)
                    910:                pane = cmd_find_map_table(cmd_find_pane_table, pane);
                    911:
                    912:        log_debug("target %s (flags %#x): session=%s, window=%s, pane=%s",
                    913:            target, flags, session == NULL ? "none" : session,
                    914:            window == NULL ? "none" : window, pane == NULL ? "none" : pane);
                    915:
                    916:        /* No pane is allowed if want an index. */
                    917:        if (pane != NULL && (flags & CMD_FIND_WINDOW_INDEX)) {
                    918:                if (~flags & CMD_FIND_QUIET)
                    919:                        cmdq_error(cmdq, "can't specify pane here");
                    920:                goto error;
                    921:        }
                    922:
                    923:        /* If the session isn't NULL, look it up. */
                    924:        if (session != NULL) {
                    925:                /* This will fill in session. */
                    926:                if (cmd_find_get_session(&fs, session) != 0)
                    927:                        goto no_session;
                    928:
                    929:                /* If window and pane are NULL, use that session's current. */
                    930:                if (window == NULL && pane == NULL) {
                    931:                        fs.wl = fs.s->curw;
                    932:                        fs.idx = -1;
                    933:                        fs.w = fs.wl->window;
                    934:                        fs.wp = fs.w->active;
                    935:                        goto found;
                    936:                }
                    937:
                    938:                /* If window is present but pane not, find window in session. */
                    939:                if (window != NULL && pane == NULL) {
                    940:                        /* This will fill in winlink and window. */
                    941:                        if (cmd_find_get_window_with_session(&fs, window) != 0)
                    942:                                goto no_window;
                    943:                        if (~flags & CMD_FIND_WINDOW_INDEX)
                    944:                                fs.wp = fs.wl->window->active;
                    945:                        goto found;
                    946:                }
                    947:
                    948:                /* If pane is present but window not, find pane. */
                    949:                if (window == NULL && pane != NULL) {
                    950:                        /* This will fill in winlink and window and pane. */
                    951:                        if (cmd_find_get_pane_with_session(&fs, pane) != 0)
                    952:                                goto no_pane;
                    953:                        goto found;
                    954:                }
                    955:
                    956:                /*
                    957:                 * If window and pane are present, find both in session. This
                    958:                 * will fill in winlink and window.
                    959:                 */
                    960:                if (cmd_find_get_window_with_session(&fs, window) != 0)
                    961:                        goto no_window;
                    962:                /* This will fill in pane. */
                    963:                if (cmd_find_get_pane_with_window(&fs, pane) != 0)
                    964:                        goto no_pane;
                    965:                goto found;
                    966:        }
                    967:
                    968:        /* No session. If window and pane, try them. */
                    969:        if (window != NULL && pane != NULL) {
                    970:                /* This will fill in session, winlink and window. */
                    971:                if (cmd_find_get_window(&fs, window) != 0)
                    972:                        goto no_window;
                    973:                /* This will fill in pane. */
                    974:                if (cmd_find_get_pane_with_window(&fs, pane) != 0)
                    975:                        goto no_pane;
                    976:                goto found;
                    977:        }
                    978:
                    979:        /* If just window is present, try it. */
                    980:        if (window != NULL && pane == NULL) {
                    981:                /* This will fill in session, winlink and window. */
                    982:                if (cmd_find_get_window(&fs, window) != 0)
                    983:                        goto no_window;
                    984:                if (~flags & CMD_FIND_WINDOW_INDEX)
                    985:                        fs.wp = fs.wl->window->active;
                    986:                goto found;
                    987:        }
                    988:
                    989:        /* If just pane is present, try it. */
                    990:        if (window == NULL && pane != NULL) {
                    991:                /* This will fill in session, winlink, window and pane. */
                    992:                if (cmd_find_get_pane(&fs, pane) != 0)
                    993:                        goto no_pane;
                    994:                goto found;
                    995:        }
                    996:
                    997: current:
                    998:        /* None is the current session. */
                    999:        free(copy);
                   1000:        if (flags & CMD_FIND_WINDOW_INDEX)
                   1001:                current.idx = -1;
                   1002:        return (&current);
                   1003:
                   1004: error:
                   1005:        free(copy);
                   1006:        return (NULL);
                   1007:
                   1008: found:
                   1009:        free(copy);
                   1010:        return (&fs);
                   1011:
                   1012: no_session:
                   1013:        if (~flags & CMD_FIND_QUIET)
                   1014:                cmdq_error(cmdq, "can't find session %s", session);
                   1015:        goto error;
                   1016:
                   1017: no_window:
                   1018:        if (~flags & CMD_FIND_QUIET)
                   1019:                cmdq_error(cmdq, "can't find window %s", window);
                   1020:        goto error;
                   1021:
                   1022: no_pane:
                   1023:        if (~flags & CMD_FIND_QUIET)
                   1024:                cmdq_error(cmdq, "can't find pane %s", pane);
                   1025:        goto error;
                   1026: }
                   1027:
                   1028: /* Log the result. */
                   1029: void
                   1030: cmd_find_log_state(const char *f, const char *target, struct cmd_find_state *fs)
                   1031: {
                   1032:        log_debug("%s: target %s%s", f, target == NULL ? "none" : target,
                   1033:            fs != NULL ? "" : " (failed)");
                   1034:        if (fs == NULL)
                   1035:                return;
                   1036:        if (fs->s != NULL)
                   1037:                log_debug("\ts=$%u", fs->s->id);
                   1038:        else
                   1039:                log_debug("\ts=none");
                   1040:        if (fs->wl != NULL) {
                   1041:                log_debug("\twl=%u %d w=@%u %s", fs->wl->idx,
                   1042:                    fs->wl->window == fs->w, fs->w->id, fs->w->name);
                   1043:        } else
                   1044:                log_debug("\twl=none");
                   1045:        if (fs->wp != NULL)
                   1046:                log_debug("\twp=%%%u", fs->wp->id);
                   1047:        else
                   1048:                log_debug("\twp=none");
                   1049:        if (fs->idx != -1)
                   1050:                log_debug("\tidx=%d", fs->idx);
                   1051:        else
                   1052:                log_debug("\tidx=none");
                   1053: }
                   1054:
                   1055: /* Find the current session. */
                   1056: struct session *
                   1057: cmd_find_current(struct cmd_q *cmdq)
                   1058: {
                   1059:        struct cmd_find_state   *fs;
                   1060:        int                      flags = CMD_FIND_QUIET;
                   1061:
                   1062:        fs = cmd_find_target(cmdq, NULL, CMD_FIND_SESSION, flags);
                   1063:        cmd_find_log_state(__func__, NULL, fs);
                   1064:        if (fs == NULL)
                   1065:                return (NULL);
                   1066:
                   1067:        return (fs->s);
                   1068: }
                   1069:
                   1070: /* Find the target session or report an error and return NULL. */
                   1071: struct session *
                   1072: cmd_find_session(struct cmd_q *cmdq, const char *target, int prefer_unattached)
                   1073: {
                   1074:        struct cmd_find_state   *fs;
                   1075:        int                      flags = 0;
                   1076:
                   1077:        if (prefer_unattached)
                   1078:                flags |= CMD_FIND_PREFER_UNATTACHED;
                   1079:
                   1080:        fs = cmd_find_target(cmdq, target, CMD_FIND_SESSION, flags);
                   1081:        cmd_find_log_state(__func__, target, fs);
                   1082:        if (fs == NULL)
                   1083:                return (NULL);
                   1084:
                   1085:        return (fs->s);
                   1086: }
                   1087:
                   1088: /* Find the target window or report an error and return NULL. */
                   1089: struct winlink *
                   1090: cmd_find_window(struct cmd_q *cmdq, const char *target, struct session **sp)
                   1091: {
                   1092:        struct cmd_find_state   *fs;
                   1093:
                   1094:        fs = cmd_find_target(cmdq, target, CMD_FIND_WINDOW, 0);
                   1095:        cmd_find_log_state(__func__, target, fs);
                   1096:        if (fs == NULL)
                   1097:                return (NULL);
                   1098:
                   1099:        if (sp != NULL)
                   1100:                *sp = fs->s;
                   1101:        return (fs->wl);
                   1102: }
                   1103:
1.8       nicm     1104: /* Find the target window, defaulting to marked rather than current. */
                   1105: struct winlink *
                   1106: cmd_find_window_marked(struct cmd_q *cmdq, const char *target,
                   1107:     struct session **sp)
                   1108: {
                   1109:        struct cmd_find_state   *fs;
                   1110:        int                      flags = CMD_FIND_DEFAULT_MARKED;
                   1111:
                   1112:        fs = cmd_find_target(cmdq, target, CMD_FIND_WINDOW, flags);
                   1113:        cmd_find_log_state(__func__, target, fs);
                   1114:        if (fs == NULL)
                   1115:                return (NULL);
                   1116:
                   1117:        if (sp != NULL)
                   1118:                *sp = fs->s;
                   1119:        return (fs->wl);
                   1120: }
                   1121:
1.1       nicm     1122: /* Find the target pane and report an error and return NULL. */
                   1123: struct winlink *
                   1124: cmd_find_pane(struct cmd_q *cmdq, const char *target, struct session **sp,
                   1125:     struct window_pane **wpp)
                   1126: {
                   1127:        struct cmd_find_state   *fs;
                   1128:
                   1129:        fs = cmd_find_target(cmdq, target, CMD_FIND_PANE, 0);
1.8       nicm     1130:        cmd_find_log_state(__func__, target, fs);
                   1131:        if (fs == NULL)
                   1132:                return (NULL);
                   1133:
                   1134:        if (sp != NULL)
                   1135:                *sp = fs->s;
                   1136:        if (wpp != NULL)
                   1137:                *wpp = fs->wp;
                   1138:        return (fs->wl);
                   1139: }
                   1140:
                   1141: /* Find the target pane, defaulting to marked rather than current. */
                   1142: struct winlink *
                   1143: cmd_find_pane_marked(struct cmd_q *cmdq, const char *target,
                   1144:     struct session **sp, struct window_pane **wpp)
                   1145: {
                   1146:        struct cmd_find_state   *fs;
                   1147:        int                      flags = CMD_FIND_DEFAULT_MARKED;
                   1148:
                   1149:        fs = cmd_find_target(cmdq, target, CMD_FIND_PANE, flags);
1.1       nicm     1150:        cmd_find_log_state(__func__, target, fs);
                   1151:        if (fs == NULL)
                   1152:                return (NULL);
                   1153:
                   1154:        if (sp != NULL)
                   1155:                *sp = fs->s;
                   1156:        if (wpp != NULL)
                   1157:                *wpp = fs->wp;
                   1158:        return (fs->wl);
                   1159: }
                   1160:
                   1161: /* Find the target client or report an error and return NULL. */
                   1162: struct client *
                   1163: cmd_find_client(struct cmd_q *cmdq, const char *target, int quiet)
                   1164: {
                   1165:        struct client   *c;
                   1166:        char            *copy;
                   1167:        size_t           size;
                   1168:        const char      *path;
                   1169:
                   1170:        /* A NULL argument means the current client. */
                   1171:        if (target == NULL) {
                   1172:                c = cmd_find_current_client(cmdq);
                   1173:                if (c == NULL && !quiet)
                   1174:                        cmdq_error(cmdq, "no current client");
                   1175:                return (c);
                   1176:        }
                   1177:        copy = xstrdup(target);
                   1178:
                   1179:        /* Trim a single trailing colon if any. */
                   1180:        size = strlen(copy);
                   1181:        if (size != 0 && copy[size - 1] == ':')
                   1182:                copy[size - 1] = '\0';
                   1183:
                   1184:        /* Check path of each client. */
                   1185:        TAILQ_FOREACH(c, &clients, entry) {
                   1186:                if (c->session == NULL || c->tty.path == NULL)
                   1187:                        continue;
                   1188:                path = c->tty.path;
                   1189:
                   1190:                /* Try for exact match. */
                   1191:                if (strcmp(copy, path) == 0)
                   1192:                        break;
                   1193:
                   1194:                /* Try without leading /dev. */
                   1195:                if (strncmp(path, _PATH_DEV, (sizeof _PATH_DEV) - 1) != 0)
                   1196:                        continue;
                   1197:                if (strcmp(copy, path + (sizeof _PATH_DEV) - 1) == 0)
                   1198:                        break;
                   1199:        }
                   1200:
                   1201:        /* If no client found, report an error. */
                   1202:        if (c == NULL && !quiet)
                   1203:                cmdq_error(cmdq, "can't find client %s", copy);
                   1204:
                   1205:        free(copy);
                   1206:        return (c);
                   1207: }
                   1208:
                   1209: /*
                   1210:  * Find the target session and window index, whether or not it exists in the
                   1211:  * session. Return -2 on error or -1 if no window index is specified. This is
                   1212:  * used when parsing an argument for a window target that may not exist (for
                   1213:  * example if it is going to be created).
                   1214:  */
                   1215: int
                   1216: cmd_find_index(struct cmd_q *cmdq, const char *target, struct session **sp)
                   1217: {
                   1218:        struct cmd_find_state   *fs;
                   1219:        int                      flags = CMD_FIND_WINDOW_INDEX;
                   1220:
                   1221:        fs = cmd_find_target(cmdq, target, CMD_FIND_WINDOW, flags);
                   1222:        cmd_find_log_state(__func__, target, fs);
                   1223:        if (fs == NULL)
                   1224:                return (-2);
                   1225:
                   1226:        if (sp != NULL)
                   1227:                *sp = fs->s;
                   1228:        return (fs->idx);
                   1229: }