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

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