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

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