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

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