[BACK]Return to cmd.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / tmux

Annotation of src/usr.bin/tmux/cmd.c, Revision 1.47

1.47    ! nicm        1: /* $OpenBSD: cmd.c,v 1.46 2010/10/29 20:11:57 nicm Exp $ */
1.1       nicm        2:
                      3: /*
                      4:  * Copyright (c) 2007 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: #include <sys/time.h>
                     21:
1.5       nicm       22: #include <fnmatch.h>
                     23: #include <paths.h>
1.1       nicm       24: #include <stdlib.h>
                     25: #include <string.h>
                     26: #include <unistd.h>
                     27:
                     28: #include "tmux.h"
                     29:
                     30: const struct cmd_entry *cmd_table[] = {
                     31:        &cmd_attach_session_entry,
                     32:        &cmd_bind_key_entry,
                     33:        &cmd_break_pane_entry,
1.34      nicm       34:        &cmd_capture_pane_entry,
1.42      nicm       35:        &cmd_choose_buffer_entry,
1.15      nicm       36:        &cmd_choose_client_entry,
1.1       nicm       37:        &cmd_choose_session_entry,
                     38:        &cmd_choose_window_entry,
                     39:        &cmd_clear_history_entry,
                     40:        &cmd_clock_mode_entry,
                     41:        &cmd_command_prompt_entry,
                     42:        &cmd_confirm_before_entry,
                     43:        &cmd_copy_buffer_entry,
                     44:        &cmd_copy_mode_entry,
                     45:        &cmd_delete_buffer_entry,
                     46:        &cmd_detach_client_entry,
1.7       nicm       47:        &cmd_display_message_entry,
1.16      nicm       48:        &cmd_display_panes_entry,
1.1       nicm       49:        &cmd_find_window_entry,
                     50:        &cmd_has_session_entry,
1.4       nicm       51:        &cmd_if_shell_entry,
1.37      nicm       52:        &cmd_join_pane_entry,
1.1       nicm       53:        &cmd_kill_pane_entry,
                     54:        &cmd_kill_server_entry,
                     55:        &cmd_kill_session_entry,
                     56:        &cmd_kill_window_entry,
1.45      nicm       57:        &cmd_last_pane_entry,
1.1       nicm       58:        &cmd_last_window_entry,
                     59:        &cmd_link_window_entry,
                     60:        &cmd_list_buffers_entry,
                     61:        &cmd_list_clients_entry,
                     62:        &cmd_list_commands_entry,
                     63:        &cmd_list_keys_entry,
1.23      nicm       64:        &cmd_list_panes_entry,
1.1       nicm       65:        &cmd_list_sessions_entry,
                     66:        &cmd_list_windows_entry,
                     67:        &cmd_load_buffer_entry,
1.19      nicm       68:        &cmd_lock_client_entry,
1.1       nicm       69:        &cmd_lock_server_entry,
1.19      nicm       70:        &cmd_lock_session_entry,
1.1       nicm       71:        &cmd_move_window_entry,
                     72:        &cmd_new_session_entry,
                     73:        &cmd_new_window_entry,
                     74:        &cmd_next_layout_entry,
                     75:        &cmd_next_window_entry,
                     76:        &cmd_paste_buffer_entry,
1.24      nicm       77:        &cmd_pipe_pane_entry,
1.1       nicm       78:        &cmd_previous_layout_entry,
                     79:        &cmd_previous_window_entry,
                     80:        &cmd_refresh_client_entry,
                     81:        &cmd_rename_session_entry,
                     82:        &cmd_rename_window_entry,
                     83:        &cmd_resize_pane_entry,
                     84:        &cmd_respawn_window_entry,
                     85:        &cmd_rotate_window_entry,
1.17      nicm       86:        &cmd_run_shell_entry,
1.1       nicm       87:        &cmd_save_buffer_entry,
                     88:        &cmd_select_layout_entry,
                     89:        &cmd_select_pane_entry,
                     90:        &cmd_select_window_entry,
                     91:        &cmd_send_keys_entry,
                     92:        &cmd_send_prefix_entry,
                     93:        &cmd_server_info_entry,
                     94:        &cmd_set_buffer_entry,
1.13      nicm       95:        &cmd_set_environment_entry,
1.1       nicm       96:        &cmd_set_option_entry,
                     97:        &cmd_set_window_option_entry,
                     98:        &cmd_show_buffer_entry,
1.13      nicm       99:        &cmd_show_environment_entry,
1.32      nicm      100:        &cmd_show_messages_entry,
1.1       nicm      101:        &cmd_show_options_entry,
                    102:        &cmd_show_window_options_entry,
                    103:        &cmd_source_file_entry,
                    104:        &cmd_split_window_entry,
                    105:        &cmd_start_server_entry,
                    106:        &cmd_suspend_client_entry,
                    107:        &cmd_swap_pane_entry,
                    108:        &cmd_swap_window_entry,
                    109:        &cmd_switch_client_entry,
                    110:        &cmd_unbind_key_entry,
                    111:        &cmd_unlink_window_entry,
                    112:        NULL
                    113: };
                    114:
1.47    ! nicm      115: struct session *cmd_choose_session_list(struct sessionslist *);
        !           116: struct session *cmd_choose_session(void);
1.31      nicm      117: struct client  *cmd_choose_client(struct clients *);
1.5       nicm      118: struct client  *cmd_lookup_client(const char *);
                    119: struct session *cmd_lookup_session(const char *, int *);
                    120: struct winlink *cmd_lookup_window(struct session *, const char *, int *);
1.8       nicm      121: int             cmd_lookup_index(struct session *, const char *, int *);
1.43      nicm      122: struct winlink *cmd_find_window_offset(const char *, struct session *, int *);
                    123: int             cmd_find_index_offset(const char *, struct session *, int *);
                    124: struct window_pane     *cmd_find_pane_offset(const char *, struct winlink *);
1.5       nicm      125:
1.10      nicm      126: int
                    127: cmd_pack_argv(int argc, char **argv, char *buf, size_t len)
                    128: {
                    129:        size_t  arglen;
                    130:        int     i;
                    131:
                    132:        *buf = '\0';
                    133:        for (i = 0; i < argc; i++) {
                    134:                if (strlcpy(buf, argv[i], len) >= len)
                    135:                        return (-1);
                    136:                arglen = strlen(argv[i]) + 1;
                    137:                buf += arglen;
                    138:                len -= arglen;
                    139:        }
                    140:
                    141:        return (0);
                    142: }
                    143:
                    144: int
                    145: cmd_unpack_argv(char *buf, size_t len, int argc, char ***argv)
                    146: {
                    147:        int     i;
                    148:        size_t  arglen;
                    149:
                    150:        if (argc == 0)
                    151:                return (0);
                    152:        *argv = xcalloc(argc, sizeof **argv);
                    153:
                    154:        buf[len - 1] = '\0';
                    155:        for (i = 0; i < argc; i++) {
                    156:                if (len == 0) {
                    157:                        cmd_free_argv(argc, *argv);
                    158:                        return (-1);
                    159:                }
                    160:
                    161:                arglen = strlen(buf) + 1;
                    162:                (*argv)[i] = xstrdup(buf);
                    163:                buf += arglen;
                    164:                len -= arglen;
                    165:        }
                    166:
                    167:        return (0);
1.46      nicm      168: }
                    169:
                    170: char **
                    171: cmd_copy_argv(int argc, char **argv)
                    172: {
                    173:        char    **new_argv;
                    174:        int       i;
                    175:
                    176:        if (argc == 0)
                    177:                return (NULL);
                    178:        new_argv = xcalloc(argc, sizeof *new_argv);
                    179:        for (i = 0; i < argc; i++) {
                    180:                if (argv[i] != NULL)
                    181:                        new_argv[i] = xstrdup(argv[i]);
                    182:        }
                    183:        return (new_argv);
1.10      nicm      184: }
                    185:
                    186: void
                    187: cmd_free_argv(int argc, char **argv)
                    188: {
                    189:        int     i;
                    190:
                    191:        if (argc == 0)
1.35      nicm      192:                return;
1.10      nicm      193:        for (i = 0; i < argc; i++) {
                    194:                if (argv[i] != NULL)
                    195:                        xfree(argv[i]);
                    196:        }
                    197:        xfree(argv);
                    198: }
                    199:
1.1       nicm      200: struct cmd *
                    201: cmd_parse(int argc, char **argv, char **cause)
                    202: {
                    203:        const struct cmd_entry **entryp, *entry;
1.27      deraadt   204:        struct cmd              *cmd;
1.1       nicm      205:        char                     s[BUFSIZ];
1.3       nicm      206:        int                      opt, ambiguous = 0;
1.1       nicm      207:
                    208:        *cause = NULL;
1.2       nicm      209:        if (argc == 0) {
                    210:                xasprintf(cause, "no command");
1.1       nicm      211:                return (NULL);
1.2       nicm      212:        }
1.1       nicm      213:
                    214:        entry = NULL;
                    215:        for (entryp = cmd_table; *entryp != NULL; entryp++) {
                    216:                if ((*entryp)->alias != NULL &&
                    217:                    strcmp((*entryp)->alias, argv[0]) == 0) {
1.3       nicm      218:                        ambiguous = 0;
1.1       nicm      219:                        entry = *entryp;
                    220:                        break;
                    221:                }
                    222:
                    223:                if (strncmp((*entryp)->name, argv[0], strlen(argv[0])) != 0)
                    224:                        continue;
                    225:                if (entry != NULL)
1.3       nicm      226:                        ambiguous = 1;
1.1       nicm      227:                entry = *entryp;
                    228:
                    229:                /* Bail now if an exact match. */
                    230:                if (strcmp(entry->name, argv[0]) == 0)
                    231:                        break;
                    232:        }
1.3       nicm      233:        if (ambiguous)
                    234:                goto ambiguous;
1.1       nicm      235:        if (entry == NULL) {
                    236:                xasprintf(cause, "unknown command: %s", argv[0]);
                    237:                return (NULL);
                    238:        }
                    239:
                    240:        optreset = 1;
                    241:        optind = 1;
                    242:        if (entry->parse == NULL) {
                    243:                while ((opt = getopt(argc, argv, "")) != -1) {
                    244:                        switch (opt) {
                    245:                        default:
                    246:                                goto usage;
                    247:                        }
                    248:                }
                    249:                argc -= optind;
                    250:                argv += optind;
                    251:                if (argc != 0)
                    252:                        goto usage;
                    253:        }
                    254:
                    255:        cmd = xmalloc(sizeof *cmd);
                    256:        cmd->entry = entry;
                    257:        cmd->data = NULL;
                    258:        if (entry->parse != NULL) {
                    259:                if (entry->parse(cmd, argc, argv, cause) != 0) {
                    260:                        xfree(cmd);
                    261:                        return (NULL);
                    262:                }
                    263:        }
                    264:        return (cmd);
                    265:
                    266: ambiguous:
                    267:        *s = '\0';
                    268:        for (entryp = cmd_table; *entryp != NULL; entryp++) {
                    269:                if (strncmp((*entryp)->name, argv[0], strlen(argv[0])) != 0)
                    270:                        continue;
                    271:                if (strlcat(s, (*entryp)->name, sizeof s) >= sizeof s)
                    272:                        break;
                    273:                if (strlcat(s, ", ", sizeof s) >= sizeof s)
                    274:                        break;
                    275:        }
                    276:        s[strlen(s) - 2] = '\0';
                    277:        xasprintf(cause, "ambiguous command: %s, could be: %s", argv[0], s);
                    278:        return (NULL);
                    279:
                    280: usage:
                    281:        xasprintf(cause, "usage: %s %s", entry->name, entry->usage);
                    282:        return (NULL);
                    283: }
                    284:
                    285: int
                    286: cmd_exec(struct cmd *cmd, struct cmd_ctx *ctx)
                    287: {
                    288:        return (cmd->entry->exec(cmd, ctx));
                    289: }
                    290:
                    291: void
                    292: cmd_free(struct cmd *cmd)
                    293: {
                    294:        if (cmd->data != NULL && cmd->entry->free != NULL)
                    295:                cmd->entry->free(cmd);
                    296:        xfree(cmd);
                    297: }
                    298:
                    299: size_t
                    300: cmd_print(struct cmd *cmd, char *buf, size_t len)
                    301: {
1.33      nicm      302:        if (cmd->entry->print == NULL)
1.1       nicm      303:                return (xsnprintf(buf, len, "%s", cmd->entry->name));
                    304:        return (cmd->entry->print(cmd, buf, len));
                    305: }
                    306:
1.5       nicm      307: /*
                    308:  * Figure out the current session. Use: 1) the current session, if the command
1.31      nicm      309:  * context has one; 2) the most recently used session containing the pty of the
                    310:  * calling client, if any; 3) the session specified in the TMUX variable from
                    311:  * the environment (as passed from the client); 4) the most recently used
                    312:  * session from all sessions.
1.5       nicm      313:  */
1.1       nicm      314: struct session *
                    315: cmd_current_session(struct cmd_ctx *ctx)
                    316: {
                    317:        struct msg_command_data *data = ctx->msgdata;
1.11      nicm      318:        struct client           *c = ctx->cmdclient;
1.5       nicm      319:        struct session          *s;
1.47    ! nicm      320:        struct sessionslist      ss;
1.11      nicm      321:        struct winlink          *wl;
                    322:        struct window_pane      *wp;
                    323:        int                      found;
1.1       nicm      324:
1.14      nicm      325:        if (ctx->curclient != NULL && ctx->curclient->session != NULL)
                    326:                return (ctx->curclient->session);
1.1       nicm      327:
1.11      nicm      328:        /*
                    329:         * If the name of the calling client's pty is know, build a list of the
                    330:         * sessions that contain it and if any choose either the first or the
                    331:         * newest.
                    332:         */
                    333:        if (c != NULL && c->tty.path != NULL) {
                    334:                ARRAY_INIT(&ss);
1.47    ! nicm      335:                RB_FOREACH(s, sessions, &sessions) {
1.11      nicm      336:                        found = 0;
                    337:                        RB_FOREACH(wl, winlinks, &s->windows) {
                    338:                                TAILQ_FOREACH(wp, &wl->window->panes, entry) {
                    339:                                        if (strcmp(wp->tty, c->tty.path) == 0) {
                    340:                                                found = 1;
                    341:                                                break;
                    342:                                        }
                    343:                                }
                    344:                                if (found)
                    345:                                        break;
                    346:                        }
                    347:                        if (found)
                    348:                                ARRAY_ADD(&ss, s);
                    349:                }
                    350:
1.47    ! nicm      351:                s = cmd_choose_session_list(&ss);
1.11      nicm      352:                ARRAY_FREE(&ss);
                    353:                if (s != NULL)
                    354:                        return (s);
                    355:        }
                    356:
                    357:        /* Use the session from the TMUX environment variable. */
1.47    ! nicm      358:        if (data != NULL && data->pid == getpid()) {
        !           359:                s = session_find_by_index(data->idx);
        !           360:                if (s != NULL)
        !           361:                        return (s);
        !           362:        }
        !           363:
        !           364:        return (cmd_choose_session());
        !           365: }
        !           366:
        !           367: /* Find the most recently used session. */
        !           368: struct session *
        !           369: cmd_choose_session(void)
        !           370: {
        !           371:        struct session  *s, *sbest;
        !           372:        struct timeval  *tv = NULL;
        !           373:
        !           374:        sbest = NULL;
        !           375:        RB_FOREACH(s, sessions, &sessions) {
        !           376:                if (tv == NULL || timercmp(&s->activity_time, tv, >)) {
        !           377:                        sbest = s;
        !           378:                        tv = &s->activity_time;
        !           379:                }
        !           380:        }
1.1       nicm      381:
1.47    ! nicm      382:        return (sbest);
1.5       nicm      383: }
                    384:
1.31      nicm      385: /* Find the most recently used session from a list. */
1.5       nicm      386: struct session *
1.47    ! nicm      387: cmd_choose_session_list(struct sessionslist *ss)
1.5       nicm      388: {
1.31      nicm      389:        struct session  *s, *sbest;
1.5       nicm      390:        struct timeval  *tv = NULL;
                    391:        u_int            i;
                    392:
1.31      nicm      393:        sbest = NULL;
1.11      nicm      394:        for (i = 0; i < ARRAY_LENGTH(ss); i++) {
                    395:                if ((s = ARRAY_ITEM(ss, i)) == NULL)
1.5       nicm      396:                        continue;
                    397:
1.31      nicm      398:                if (tv == NULL || timercmp(&s->activity_time, tv, >)) {
                    399:                        sbest = s;
                    400:                        tv = &s->activity_time;
1.1       nicm      401:                }
                    402:        }
1.5       nicm      403:
1.31      nicm      404:        return (sbest);
1.1       nicm      405: }
                    406:
1.30      nicm      407: /*
                    408:  * Find the current client. First try the current client if set, then pick the
1.31      nicm      409:  * most recently used of the clients attached to the current session if any,
                    410:  * then of all clients.
1.30      nicm      411:  */
                    412: struct client *
                    413: cmd_current_client(struct cmd_ctx *ctx)
                    414: {
                    415:        struct session          *s;
                    416:        struct client           *c;
                    417:        struct clients           cc;
                    418:        u_int                    i;
                    419:
                    420:        if (ctx->curclient != NULL)
                    421:                return (ctx->curclient);
                    422:
                    423:        /*
                    424:         * No current client set. Find the current session and return the
                    425:         * newest of its clients.
                    426:         */
                    427:        s = cmd_current_session(ctx);
                    428:        if (s != NULL && !(s->flags & SESSION_UNATTACHED)) {
                    429:                ARRAY_INIT(&cc);
                    430:                for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
                    431:                        if ((c = ARRAY_ITEM(&clients, i)) == NULL)
                    432:                                continue;
                    433:                        if (s == c->session)
                    434:                                ARRAY_ADD(&cc, c);
                    435:                }
                    436:
1.31      nicm      437:                c = cmd_choose_client(&cc);
1.30      nicm      438:                ARRAY_FREE(&cc);
                    439:                if (c != NULL)
                    440:                        return (c);
                    441:        }
                    442:
1.31      nicm      443:        return (cmd_choose_client(&clients));
1.30      nicm      444: }
                    445:
1.31      nicm      446: /* Choose the most recently used client from a list. */
1.20      nicm      447: struct client *
1.31      nicm      448: cmd_choose_client(struct clients *cc)
1.20      nicm      449: {
1.31      nicm      450:        struct client   *c, *cbest;
1.20      nicm      451:        struct timeval  *tv = NULL;
                    452:        u_int            i;
                    453:
1.31      nicm      454:        cbest = NULL;
1.30      nicm      455:        for (i = 0; i < ARRAY_LENGTH(cc); i++) {
                    456:                if ((c = ARRAY_ITEM(cc, i)) == NULL)
1.20      nicm      457:                        continue;
                    458:                if (c->session == NULL)
                    459:                        continue;
                    460:
1.31      nicm      461:                if (tv == NULL || timercmp(&c->activity_time, tv, >)) {
                    462:                        cbest = c;
                    463:                        tv = &c->activity_time;
1.20      nicm      464:                }
                    465:        }
                    466:
1.31      nicm      467:        return (cbest);
1.20      nicm      468: }
                    469:
1.5       nicm      470: /* Find the target client or report an error and return NULL. */
1.1       nicm      471: struct client *
                    472: cmd_find_client(struct cmd_ctx *ctx, const char *arg)
                    473: {
1.30      nicm      474:        struct client   *c;
1.5       nicm      475:        char            *tmparg;
                    476:        size_t           arglen;
1.1       nicm      477:
1.5       nicm      478:        /* A NULL argument means the current client. */
1.30      nicm      479:        if (arg == NULL)
                    480:                return (cmd_current_client(ctx));
1.5       nicm      481:        tmparg = xstrdup(arg);
                    482:
                    483:        /* Trim a single trailing colon if any. */
                    484:        arglen = strlen(tmparg);
                    485:        if (arglen != 0 && tmparg[arglen - 1] == ':')
                    486:                tmparg[arglen - 1] = '\0';
                    487:
                    488:        /* Find the client, if any. */
                    489:        c = cmd_lookup_client(tmparg);
                    490:
                    491:        /* If no client found, report an error. */
                    492:        if (c == NULL)
                    493:                ctx->error(ctx, "client not found: %s", tmparg);
                    494:
                    495:        xfree(tmparg);
                    496:        return (c);
                    497: }
                    498:
                    499: /*
                    500:  * Lookup a client by device path. Either of a full match and a match without a
                    501:  * leading _PATH_DEV ("/dev/") is accepted.
                    502:  */
                    503: struct client *
                    504: cmd_lookup_client(const char *name)
                    505: {
                    506:        struct client   *c;
                    507:        const char      *path;
                    508:        u_int            i;
                    509:
                    510:        for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
1.25      nicm      511:                c = ARRAY_ITEM(&clients, i);
                    512:                if (c == NULL || c->session == NULL)
1.5       nicm      513:                        continue;
                    514:                path = c->tty.path;
                    515:
                    516:                /* Check for exact matches. */
                    517:                if (strcmp(name, path) == 0)
                    518:                        return (c);
                    519:
                    520:                /* Check without leading /dev if present. */
                    521:                if (strncmp(path, _PATH_DEV, (sizeof _PATH_DEV) - 1) != 0)
                    522:                        continue;
                    523:                if (strcmp(name, path + (sizeof _PATH_DEV) - 1) == 0)
                    524:                        return (c);
                    525:        }
                    526:
                    527:        return (NULL);
                    528: }
                    529:
                    530: /* Lookup a session by name. If no session is found, NULL is returned. */
                    531: struct session *
                    532: cmd_lookup_session(const char *name, int *ambiguous)
                    533: {
                    534:        struct session  *s, *sfound;
                    535:
                    536:        *ambiguous = 0;
                    537:
                    538:        /*
1.28      nicm      539:         * Look for matches. First look for exact matches - session names must
                    540:         * be unique so an exact match can't be ambigious and can just be
                    541:         * returned.
1.5       nicm      542:         */
1.47    ! nicm      543:        if ((s = session_find(name)) != NULL)
        !           544:                return (s);
1.28      nicm      545:
                    546:        /*
                    547:         * Otherwise look for partial matches, returning early if it is found to
                    548:         * be ambiguous.
                    549:         */
                    550:        sfound = NULL;
1.47    ! nicm      551:        RB_FOREACH(s, sessions, &sessions) {
1.5       nicm      552:                if (strncmp(name, s->name, strlen(name)) == 0 ||
                    553:                    fnmatch(name, s->name, 0) == 0) {
                    554:                        if (sfound != NULL) {
                    555:                                *ambiguous = 1;
                    556:                                return (NULL);
                    557:                        }
                    558:                        sfound = s;
                    559:                }
                    560:        }
1.35      nicm      561:        return (sfound);
1.5       nicm      562: }
                    563:
                    564: /*
1.8       nicm      565:  * Lookup a window or return -1 if not found or ambigious. First try as an
                    566:  * index and if invalid, use fnmatch or leading prefix. Return NULL but fill in
1.41      nicm      567:  * idx if the window index is a valid number but there is no window with that
1.8       nicm      568:  * index.
1.5       nicm      569:  */
                    570: struct winlink *
                    571: cmd_lookup_window(struct session *s, const char *name, int *ambiguous)
                    572: {
                    573:        struct winlink  *wl, *wlfound;
                    574:        const char      *errstr;
                    575:        u_int            idx;
                    576:
                    577:        *ambiguous = 0;
                    578:
                    579:        /* First see if this is a valid window index in this session. */
                    580:        idx = strtonum(name, 0, INT_MAX, &errstr);
                    581:        if (errstr == NULL) {
                    582:                if ((wl = winlink_find_by_index(&s->windows, idx)) != NULL)
                    583:                        return (wl);
                    584:        }
1.35      nicm      585:
1.5       nicm      586:        /* Look for exact matches, error if more than one. */
                    587:        wlfound = NULL;
                    588:        RB_FOREACH(wl, winlinks, &s->windows) {
1.8       nicm      589:                if (strcmp(name, wl->window->name) == 0) {
1.5       nicm      590:                        if (wlfound != NULL) {
                    591:                                *ambiguous = 1;
                    592:                                return (NULL);
                    593:                        }
                    594:                        wlfound = wl;
1.1       nicm      595:                }
                    596:        }
1.5       nicm      597:        if (wlfound != NULL)
                    598:                return (wlfound);
                    599:
                    600:        /* Now look for pattern matches, again error if multiple. */
                    601:        wlfound = NULL;
                    602:        RB_FOREACH(wl, winlinks, &s->windows) {
1.8       nicm      603:                if (strncmp(name, wl->window->name, strlen(name)) == 0 ||
                    604:                    fnmatch(name, wl->window->name, 0) == 0) {
1.5       nicm      605:                        if (wlfound != NULL) {
                    606:                                *ambiguous = 1;
                    607:                                return (NULL);
                    608:                        }
                    609:                        wlfound = wl;
                    610:                }
1.35      nicm      611:        }
1.5       nicm      612:        if (wlfound != NULL)
                    613:                return (wlfound);
                    614:
                    615:        return (NULL);
1.1       nicm      616: }
                    617:
1.8       nicm      618: /*
                    619:  * Find a window index - if the window doesn't exist, check if it is a
                    620:  * potential index and return it anyway.
                    621:  */
                    622: int
                    623: cmd_lookup_index(struct session *s, const char *name, int *ambiguous)
                    624: {
                    625:        struct winlink  *wl;
                    626:        const char      *errstr;
                    627:        u_int            idx;
                    628:
                    629:        if ((wl = cmd_lookup_window(s, name, ambiguous)) != NULL)
                    630:                return (wl->idx);
                    631:        if (*ambiguous)
                    632:                return (-1);
                    633:
                    634:        idx = strtonum(name, 0, INT_MAX, &errstr);
                    635:        if (errstr == NULL)
                    636:                return (idx);
                    637:
                    638:        return (-1);
                    639: }
                    640:
1.5       nicm      641: /* Find the target session or report an error and return NULL. */
1.1       nicm      642: struct session *
                    643: cmd_find_session(struct cmd_ctx *ctx, const char *arg)
                    644: {
                    645:        struct session  *s;
1.5       nicm      646:        struct client   *c;
                    647:        char            *tmparg;
                    648:        size_t           arglen;
                    649:        int              ambiguous;
1.1       nicm      650:
1.5       nicm      651:        /* A NULL argument means the current session. */
1.1       nicm      652:        if (arg == NULL)
1.5       nicm      653:                return (cmd_current_session(ctx));
                    654:        tmparg = xstrdup(arg);
                    655:
                    656:        /* Trim a single trailing colon if any. */
                    657:        arglen = strlen(tmparg);
                    658:        if (arglen != 0 && tmparg[arglen - 1] == ':')
                    659:                tmparg[arglen - 1] = '\0';
                    660:
                    661:        /* Find the session, if any. */
                    662:        s = cmd_lookup_session(tmparg, &ambiguous);
                    663:
                    664:        /* If it doesn't, try to match it as a client. */
                    665:        if (s == NULL && (c = cmd_lookup_client(tmparg)) != NULL)
                    666:                s = c->session;
                    667:
                    668:        /* If no session found, report an error. */
                    669:        if (s == NULL) {
                    670:                if (ambiguous)
                    671:                        ctx->error(ctx, "more than one session: %s", tmparg);
                    672:                else
                    673:                        ctx->error(ctx, "session not found: %s", tmparg);
1.1       nicm      674:        }
1.5       nicm      675:
                    676:        xfree(tmparg);
1.1       nicm      677:        return (s);
                    678: }
                    679:
1.5       nicm      680: /* Find the target session and window or report an error and return NULL. */
1.1       nicm      681: struct winlink *
                    682: cmd_find_window(struct cmd_ctx *ctx, const char *arg, struct session **sp)
                    683: {
                    684:        struct session  *s;
                    685:        struct winlink  *wl;
1.5       nicm      686:        const char      *winptr;
                    687:        char            *sessptr = NULL;
                    688:        int              ambiguous = 0;
                    689:
                    690:        /*
                    691:         * Find the current session. There must always be a current session, if
                    692:         * it can't be found, report an error.
                    693:         */
                    694:        if ((s = cmd_current_session(ctx)) == NULL) {
                    695:                ctx->error(ctx, "can't establish current session");
                    696:                return (NULL);
                    697:        }
                    698:
                    699:        /* A NULL argument means the current session and window. */
                    700:        if (arg == NULL) {
                    701:                if (sp != NULL)
                    702:                        *sp = s;
                    703:                return (s->curw);
                    704:        }
                    705:
                    706:        /* Time to look at the argument. If it is empty, that is an error. */
                    707:        if (*arg == '\0')
                    708:                goto not_found;
                    709:
1.8       nicm      710:        /* Find the separating colon and split into window and session. */
1.5       nicm      711:        winptr = strchr(arg, ':');
                    712:        if (winptr == NULL)
1.8       nicm      713:                goto no_colon;
                    714:        winptr++;       /* skip : */
                    715:        sessptr = xstrdup(arg);
                    716:        *strchr(sessptr, ':') = '\0';
1.5       nicm      717:
                    718:        /* Try to lookup the session if present. */
1.8       nicm      719:        if (*sessptr != '\0') {
                    720:                if ((s = cmd_lookup_session(sessptr, &ambiguous)) == NULL)
1.5       nicm      721:                        goto no_session;
                    722:        }
                    723:        if (sp != NULL)
                    724:                *sp = s;
                    725:
                    726:        /*
                    727:         * Then work out the window. An empty string is the current window,
1.38      nicm      728:         * otherwise try special cases then to look it up in the session.
1.5       nicm      729:         */
1.8       nicm      730:        if (*winptr == '\0')
1.5       nicm      731:                wl = s->curw;
1.38      nicm      732:        else if (winptr[0] == '!' && winptr[1] == '\0')
                    733:                wl = TAILQ_FIRST(&s->lastw);
1.43      nicm      734:        else if (winptr[0] == '+' || winptr[0] == '-')
                    735:                wl = cmd_find_window_offset(winptr, s, &ambiguous);
                    736:        else
1.38      nicm      737:                wl = cmd_lookup_window(s, winptr, &ambiguous);
                    738:        if (wl == NULL)
1.5       nicm      739:                goto not_found;
1.35      nicm      740:
1.5       nicm      741:        if (sessptr != NULL)
                    742:                xfree(sessptr);
                    743:        return (wl);
                    744:
1.8       nicm      745: no_colon:
1.38      nicm      746:        /*
                    747:         * No colon in the string, first try special cases, then as a window
                    748:         * and lastly as a session.
                    749:         */
                    750:        if (arg[0] == '!' && arg[1] == '\0') {
                    751:                if ((wl = TAILQ_FIRST(&s->lastw)) == NULL)
                    752:                        goto not_found;
1.41      nicm      753:        } else if (arg[0] == '+' || arg[0] == '-') {
1.43      nicm      754:                if ((wl = cmd_find_window_offset(arg, s, &ambiguous)) == NULL)
1.41      nicm      755:                        goto lookup_session;
                    756:        } else if ((wl = cmd_lookup_window(s, arg, &ambiguous)) == NULL)
                    757:                goto lookup_session;
1.8       nicm      758:
                    759:        if (sp != NULL)
                    760:                *sp = s;
                    761:
                    762:        return (wl);
                    763:
1.41      nicm      764: lookup_session:
                    765:        if (ambiguous)
                    766:                goto not_found;
                    767:        if ((s = cmd_lookup_session(arg, &ambiguous)) == NULL)
                    768:                goto no_session;
                    769:
                    770:        if (sp != NULL)
                    771:                *sp = s;
                    772:
                    773:        return (s->curw);
                    774:
1.5       nicm      775: no_session:
                    776:        if (ambiguous)
1.8       nicm      777:                ctx->error(ctx, "multiple sessions: %s", arg);
1.5       nicm      778:        else
1.8       nicm      779:                ctx->error(ctx, "session not found: %s", arg);
1.5       nicm      780:        if (sessptr != NULL)
                    781:                xfree(sessptr);
                    782:        return (NULL);
                    783:
                    784: not_found:
                    785:        if (ambiguous)
                    786:                ctx->error(ctx, "multiple windows: %s", arg);
                    787:        else
                    788:                ctx->error(ctx, "window not found: %s", arg);
                    789:        if (sessptr != NULL)
                    790:                xfree(sessptr);
                    791:        return (NULL);
                    792: }
1.1       nicm      793:
1.43      nicm      794: struct winlink *
                    795: cmd_find_window_offset(const char *winptr, struct session *s, int *ambiguous)
                    796: {
                    797:        struct winlink  *wl;
                    798:        int              offset = 1;
                    799:
                    800:        if (winptr[1] != '\0')
                    801:                offset = strtonum(winptr + 1, 1, INT_MAX, NULL);
                    802:        if (offset == 0)
                    803:                wl = cmd_lookup_window(s, winptr, ambiguous);
                    804:        else {
                    805:                if (winptr[0] == '+')
                    806:                        wl = winlink_next_by_number(s->curw, s, offset);
                    807:                else
                    808:                        wl = winlink_previous_by_number(s->curw, s, offset);
                    809:        }
                    810:
                    811:        return (wl);
                    812: }
                    813:
1.5       nicm      814: /*
                    815:  * Find the target session and window index, whether or not it exists in the
                    816:  * session. Return -2 on error or -1 if no window index is specified. This is
1.8       nicm      817:  * used when parsing an argument for a window target that may not exist (for
                    818:  * example if it is going to be created).
1.5       nicm      819:  */
                    820: int
                    821: cmd_find_index(struct cmd_ctx *ctx, const char *arg, struct session **sp)
                    822: {
                    823:        struct session  *s;
1.38      nicm      824:        struct winlink  *wl;
1.8       nicm      825:        const char      *winptr;
1.5       nicm      826:        char            *sessptr = NULL;
                    827:        int              idx, ambiguous = 0;
                    828:
                    829:        /*
                    830:         * Find the current session. There must always be a current session, if
                    831:         * it can't be found, report an error.
                    832:         */
                    833:        if ((s = cmd_current_session(ctx)) == NULL) {
                    834:                ctx->error(ctx, "can't establish current session");
1.9       nicm      835:                return (-2);
1.1       nicm      836:        }
1.5       nicm      837:
                    838:        /* A NULL argument means the current session and "no window" (-1). */
                    839:        if (arg == NULL) {
                    840:                if (sp != NULL)
                    841:                        *sp = s;
                    842:                return (-1);
                    843:        }
                    844:
                    845:        /* Time to look at the argument. If it is empty, that is an error. */
                    846:        if (*arg == '\0')
                    847:                goto not_found;
                    848:
                    849:        /* Find the separating colon. If none, assume the current session. */
                    850:        winptr = strchr(arg, ':');
                    851:        if (winptr == NULL)
1.8       nicm      852:                goto no_colon;
                    853:        winptr++;       /* skip : */
                    854:        sessptr = xstrdup(arg);
                    855:        *strchr(sessptr, ':') = '\0';
1.5       nicm      856:
                    857:        /* Try to lookup the session if present. */
                    858:        if (sessptr != NULL && *sessptr != '\0') {
1.8       nicm      859:                if ((s = cmd_lookup_session(sessptr, &ambiguous)) == NULL)
1.5       nicm      860:                        goto no_session;
                    861:        }
1.1       nicm      862:        if (sp != NULL)
                    863:                *sp = s;
                    864:
1.5       nicm      865:        /*
1.8       nicm      866:         * Then work out the window. An empty string is a new window otherwise
                    867:         * try to look it up in the session.
1.5       nicm      868:         */
1.8       nicm      869:        if (*winptr == '\0')
1.38      nicm      870:                idx = -1;
                    871:        else if (winptr[0] == '!' && winptr[1] == '\0') {
                    872:                if ((wl = TAILQ_FIRST(&s->lastw)) == NULL)
                    873:                        goto not_found;
                    874:                idx = wl->idx;
1.41      nicm      875:        } else if (winptr[0] == '+' || winptr[0] == '-') {
1.43      nicm      876:                if ((idx = cmd_find_index_offset(winptr, s, &ambiguous)) < 0)
1.41      nicm      877:                        goto invalid_index;
                    878:        } else if ((idx = cmd_lookup_index(s, winptr, &ambiguous)) == -1)
                    879:                goto invalid_index;
1.35      nicm      880:
1.5       nicm      881:        if (sessptr != NULL)
                    882:                xfree(sessptr);
                    883:        return (idx);
                    884:
1.8       nicm      885: no_colon:
1.38      nicm      886:        /*
                    887:         * No colon in the string, first try special cases, then as a window
                    888:         * and lastly as a session.
                    889:         */
                    890:        if (arg[0] == '!' && arg[1] == '\0') {
                    891:                if ((wl = TAILQ_FIRST(&s->lastw)) == NULL)
                    892:                        goto not_found;
                    893:                idx = wl->idx;
1.41      nicm      894:        } else if (arg[0] == '+' || arg[0] == '-') {
1.43      nicm      895:                if ((idx = cmd_find_index_offset(arg, s, &ambiguous)) < 0)
1.41      nicm      896:                        goto lookup_session;
                    897:        } else if ((idx = cmd_lookup_index(s, arg, &ambiguous)) == -1)
                    898:                goto lookup_session;
1.8       nicm      899:
                    900:        if (sp != NULL)
                    901:                *sp = s;
                    902:
                    903:        return (idx);
                    904:
1.41      nicm      905: lookup_session:
                    906:        if (ambiguous)
                    907:                goto not_found;
                    908:        if ((s = cmd_lookup_session(arg, &ambiguous)) == NULL)
                    909:                goto no_session;
                    910:
                    911:        if (sp != NULL)
                    912:                *sp = s;
                    913:
                    914:        return (-1);
                    915:
1.5       nicm      916: no_session:
                    917:        if (ambiguous)
1.35      nicm      918:                ctx->error(ctx, "multiple sessions: %s", arg);
1.5       nicm      919:        else
1.8       nicm      920:                ctx->error(ctx, "session not found: %s", arg);
1.5       nicm      921:        if (sessptr != NULL)
                    922:                xfree(sessptr);
                    923:        return (-2);
                    924:
1.41      nicm      925: invalid_index:
                    926:        if (ambiguous)
                    927:                goto not_found;
                    928:        ctx->error(ctx, "invalid index: %s", arg);
                    929:
                    930:        if (sessptr != NULL)
                    931:                xfree(sessptr);
                    932:        return (-2);
                    933:
1.5       nicm      934: not_found:
                    935:        if (ambiguous)
                    936:                ctx->error(ctx, "multiple windows: %s", arg);
1.1       nicm      937:        else
1.5       nicm      938:                ctx->error(ctx, "window not found: %s", arg);
                    939:        if (sessptr != NULL)
                    940:                xfree(sessptr);
                    941:        return (-2);
1.12      nicm      942: }
                    943:
1.43      nicm      944: int
                    945: cmd_find_index_offset(const char *winptr, struct session *s, int *ambiguous)
                    946: {
                    947:        int     idx, offset = 1;
                    948:
                    949:        if (winptr[1] != '\0')
                    950:                offset = strtonum(winptr + 1, 1, INT_MAX, NULL);
                    951:        if (offset == 0)
                    952:                idx = cmd_lookup_index(s, winptr, ambiguous);
                    953:        else {
                    954:                if (winptr[0] == '+') {
                    955:                        if (s->curw->idx == INT_MAX)
                    956:                                idx = cmd_lookup_index(s, winptr, ambiguous);
                    957:                        else
                    958:                                idx = s->curw->idx + offset;
                    959:                } else {
                    960:                        if (s->curw->idx == 0)
                    961:                                idx = cmd_lookup_index(s, winptr, ambiguous);
                    962:                        else
                    963:                                idx = s->curw->idx - offset;
                    964:                }
                    965:        }
                    966:
                    967:        return (idx);
                    968: }
                    969:
1.12      nicm      970: /*
                    971:  * Find the target session, window and pane number or report an error and
                    972:  * return NULL. The pane number is separated from the session:window by a .,
                    973:  * such as mysession:mywindow.0.
                    974:  */
                    975: struct winlink *
                    976: cmd_find_pane(struct cmd_ctx *ctx,
                    977:     const char *arg, struct session **sp, struct window_pane **wpp)
                    978: {
1.36      nicm      979:        struct session          *s;
                    980:        struct winlink          *wl;
                    981:        struct layout_cell      *lc;
                    982:        const char              *period, *errstr;
                    983:        char                    *winptr, *paneptr;
1.43      nicm      984:        u_int                    idx;
1.12      nicm      985:
                    986:        /* Get the current session. */
                    987:        if ((s = cmd_current_session(ctx)) == NULL) {
1.27      deraadt   988:                ctx->error(ctx, "can't establish current session");
1.12      nicm      989:                return (NULL);
                    990:        }
                    991:        if (sp != NULL)
                    992:                *sp = s;
                    993:
                    994:        /* A NULL argument means the current session, window and pane. */
                    995:        if (arg == NULL) {
                    996:                *wpp = s->curw->window->active;
                    997:                return (s->curw);
                    998:        }
                    999:
                   1000:        /* Look for a separating period. */
                   1001:        if ((period = strrchr(arg, '.')) == NULL)
                   1002:                goto no_period;
                   1003:
                   1004:        /* Pull out the window part and parse it. */
                   1005:        winptr = xstrdup(arg);
                   1006:        winptr[period - arg] = '\0';
                   1007:        if (*winptr == '\0')
                   1008:                wl = s->curw;
                   1009:        else if ((wl = cmd_find_window(ctx, winptr, sp)) == NULL)
                   1010:                goto error;
                   1011:
                   1012:        /* Find the pane section and look it up. */
                   1013:        paneptr = winptr + (period - arg) + 1;
                   1014:        if (*paneptr == '\0')
                   1015:                *wpp = wl->window->active;
1.43      nicm     1016:        else if (paneptr[0] == '+' || paneptr[0] == '-')
                   1017:                *wpp = cmd_find_pane_offset(paneptr, wl);
                   1018:        else {
1.12      nicm     1019:                idx = strtonum(paneptr, 0, INT_MAX, &errstr);
1.36      nicm     1020:                if (errstr != NULL)
                   1021:                        goto lookup_string;
1.12      nicm     1022:                *wpp = window_pane_at_index(wl->window, idx);
1.36      nicm     1023:                if (*wpp == NULL)
                   1024:                        goto lookup_string;
1.12      nicm     1025:        }
                   1026:
                   1027:        xfree(winptr);
                   1028:        return (wl);
                   1029:
1.36      nicm     1030: lookup_string:
                   1031:        /* Try pane string description. */
1.39      nicm     1032:        if ((lc = layout_find_string(wl->window, paneptr)) == NULL) {
1.36      nicm     1033:                ctx->error(ctx, "can't find pane: %s", paneptr);
                   1034:                goto error;
                   1035:        }
                   1036:        *wpp = lc->wp;
                   1037:
                   1038:        xfree(winptr);
1.39      nicm     1039:        return (wl);
1.36      nicm     1040:
1.12      nicm     1041: no_period:
                   1042:        /* Try as a pane number alone. */
                   1043:        idx = strtonum(arg, 0, INT_MAX, &errstr);
                   1044:        if (errstr != NULL)
                   1045:                goto lookup_window;
                   1046:
                   1047:        /* Try index in the current session and window. */
                   1048:        if ((*wpp = window_pane_at_index(s->curw->window, idx)) == NULL)
                   1049:                goto lookup_window;
1.35      nicm     1050:
1.12      nicm     1051:        return (s->curw);
                   1052:
                   1053: lookup_window:
1.36      nicm     1054:        /* Try pane string description. */
                   1055:        if ((lc = layout_find_string(s->curw->window, arg)) != NULL) {
                   1056:                *wpp = lc->wp;
                   1057:                return (s->curw);
                   1058:        }
                   1059:
1.12      nicm     1060:        /* Try as a window and use the active pane. */
                   1061:        if ((wl = cmd_find_window(ctx, arg, sp)) != NULL)
                   1062:                *wpp = wl->window->active;
                   1063:        return (wl);
1.35      nicm     1064:
1.12      nicm     1065: error:
                   1066:        xfree(winptr);
                   1067:        return (NULL);
1.43      nicm     1068: }
                   1069:
                   1070: struct window_pane *
                   1071: cmd_find_pane_offset(const char *paneptr, struct winlink *wl)
                   1072: {
                   1073:        struct window           *w = wl->window;
                   1074:        struct window_pane      *wp = w->active;
                   1075:        u_int                    offset = 1;
                   1076:
                   1077:        if (paneptr[1] != '\0')
                   1078:                offset = strtonum(paneptr + 1, 1, INT_MAX, NULL);
                   1079:        if (offset > 0) {
                   1080:                if (paneptr[0] == '+')
                   1081:                        wp = window_pane_next_by_number(w, wp, offset);
                   1082:                else
                   1083:                        wp = window_pane_previous_by_number(w, wp, offset);
                   1084:        }
                   1085:
                   1086:        return (wp);
1.15      nicm     1087: }
                   1088:
                   1089: /* Replace the first %% or %idx in template by s. */
                   1090: char *
                   1091: cmd_template_replace(char *template, const char *s, int idx)
                   1092: {
                   1093:        char     ch;
                   1094:        char    *buf, *ptr;
                   1095:        int      replaced;
                   1096:        size_t   len;
                   1097:
                   1098:        if (strstr(template, "%") == NULL)
                   1099:                return (xstrdup(template));
                   1100:
                   1101:        buf = xmalloc(1);
                   1102:        *buf = '\0';
                   1103:        len = 0;
                   1104:        replaced = 0;
                   1105:
                   1106:        ptr = template;
                   1107:        while (*ptr != '\0') {
                   1108:                switch (ch = *ptr++) {
                   1109:                case '%':
                   1110:                        if (*ptr < '1' || *ptr > '9' || *ptr - '0' != idx) {
                   1111:                                if (*ptr != '%' || replaced)
                   1112:                                        break;
                   1113:                                replaced = 1;
                   1114:                        }
                   1115:                        ptr++;
                   1116:
                   1117:                        len += strlen(s);
                   1118:                        buf = xrealloc(buf, 1, len + 1);
                   1119:                        strlcat(buf, s, len + 1);
                   1120:                        continue;
                   1121:                }
                   1122:                buf = xrealloc(buf, 1, len + 2);
                   1123:                buf[len++] = ch;
                   1124:                buf[len] = '\0';
                   1125:        }
                   1126:
                   1127:        return (buf);
1.1       nicm     1128: }