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

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