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

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