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

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