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

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