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

1.20    ! nicm        1: /* $OpenBSD: cmd.c,v 1.19 2009/09/24 14:17:09 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.20    ! nicm      113: struct client  *cmd_newest_client(void);
1.5       nicm      114: struct client  *cmd_lookup_client(const char *);
                    115: struct session *cmd_lookup_session(const char *, int *);
                    116: struct winlink *cmd_lookup_window(struct session *, const char *, int *);
1.8       nicm      117: int             cmd_lookup_index(struct session *, const char *, int *);
1.5       nicm      118:
1.10      nicm      119: int
                    120: cmd_pack_argv(int argc, char **argv, char *buf, size_t len)
                    121: {
                    122:        size_t  arglen;
                    123:        int     i;
                    124:
                    125:        *buf = '\0';
                    126:        for (i = 0; i < argc; i++) {
                    127:                if (strlcpy(buf, argv[i], len) >= len)
                    128:                        return (-1);
                    129:                arglen = strlen(argv[i]) + 1;
                    130:                buf += arglen;
                    131:                len -= arglen;
                    132:        }
                    133:
                    134:        return (0);
                    135: }
                    136:
                    137: int
                    138: cmd_unpack_argv(char *buf, size_t len, int argc, char ***argv)
                    139: {
                    140:        int     i;
                    141:        size_t  arglen;
                    142:
                    143:        if (argc == 0)
                    144:                return (0);
                    145:        *argv = xcalloc(argc, sizeof **argv);
                    146:
                    147:        buf[len - 1] = '\0';
                    148:        for (i = 0; i < argc; i++) {
                    149:                if (len == 0) {
                    150:                        cmd_free_argv(argc, *argv);
                    151:                        return (-1);
                    152:                }
                    153:
                    154:                arglen = strlen(buf) + 1;
                    155:                (*argv)[i] = xstrdup(buf);
                    156:                buf += arglen;
                    157:                len -= arglen;
                    158:        }
                    159:
                    160:        return (0);
                    161: }
                    162:
                    163: void
                    164: cmd_free_argv(int argc, char **argv)
                    165: {
                    166:        int     i;
                    167:
                    168:        if (argc == 0)
                    169:                return;
                    170:        for (i = 0; i < argc; i++) {
                    171:                if (argv[i] != NULL)
                    172:                        xfree(argv[i]);
                    173:        }
                    174:        xfree(argv);
                    175: }
                    176:
1.1       nicm      177: struct cmd *
                    178: cmd_parse(int argc, char **argv, char **cause)
                    179: {
                    180:        const struct cmd_entry **entryp, *entry;
                    181:        struct cmd              *cmd;
                    182:        char                     s[BUFSIZ];
1.3       nicm      183:        int                      opt, ambiguous = 0;
1.1       nicm      184:
                    185:        *cause = NULL;
1.2       nicm      186:        if (argc == 0) {
                    187:                xasprintf(cause, "no command");
1.1       nicm      188:                return (NULL);
1.2       nicm      189:        }
1.1       nicm      190:
                    191:        entry = NULL;
                    192:        for (entryp = cmd_table; *entryp != NULL; entryp++) {
                    193:                if ((*entryp)->alias != NULL &&
                    194:                    strcmp((*entryp)->alias, argv[0]) == 0) {
1.3       nicm      195:                        ambiguous = 0;
1.1       nicm      196:                        entry = *entryp;
                    197:                        break;
                    198:                }
                    199:
                    200:                if (strncmp((*entryp)->name, argv[0], strlen(argv[0])) != 0)
                    201:                        continue;
                    202:                if (entry != NULL)
1.3       nicm      203:                        ambiguous = 1;
1.1       nicm      204:                entry = *entryp;
                    205:
                    206:                /* Bail now if an exact match. */
                    207:                if (strcmp(entry->name, argv[0]) == 0)
                    208:                        break;
                    209:        }
1.3       nicm      210:        if (ambiguous)
                    211:                goto ambiguous;
1.1       nicm      212:        if (entry == NULL) {
                    213:                xasprintf(cause, "unknown command: %s", argv[0]);
                    214:                return (NULL);
                    215:        }
                    216:
                    217:        optreset = 1;
                    218:        optind = 1;
                    219:        if (entry->parse == NULL) {
                    220:                while ((opt = getopt(argc, argv, "")) != -1) {
                    221:                        switch (opt) {
                    222:                        default:
                    223:                                goto usage;
                    224:                        }
                    225:                }
                    226:                argc -= optind;
                    227:                argv += optind;
                    228:                if (argc != 0)
                    229:                        goto usage;
                    230:        }
                    231:
                    232:        cmd = xmalloc(sizeof *cmd);
                    233:        cmd->entry = entry;
                    234:        cmd->data = NULL;
                    235:        if (entry->parse != NULL) {
                    236:                if (entry->parse(cmd, argc, argv, cause) != 0) {
                    237:                        xfree(cmd);
                    238:                        return (NULL);
                    239:                }
                    240:        }
                    241:        return (cmd);
                    242:
                    243: ambiguous:
                    244:        *s = '\0';
                    245:        for (entryp = cmd_table; *entryp != NULL; entryp++) {
                    246:                if (strncmp((*entryp)->name, argv[0], strlen(argv[0])) != 0)
                    247:                        continue;
                    248:                if (strlcat(s, (*entryp)->name, sizeof s) >= sizeof s)
                    249:                        break;
                    250:                if (strlcat(s, ", ", sizeof s) >= sizeof s)
                    251:                        break;
                    252:        }
                    253:        s[strlen(s) - 2] = '\0';
                    254:        xasprintf(cause, "ambiguous command: %s, could be: %s", argv[0], s);
                    255:        return (NULL);
                    256:
                    257: usage:
                    258:        xasprintf(cause, "usage: %s %s", entry->name, entry->usage);
                    259:        return (NULL);
                    260: }
                    261:
                    262: int
                    263: cmd_exec(struct cmd *cmd, struct cmd_ctx *ctx)
                    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.20    ! nicm      373: /* Find the newest client. */
        !           374: struct client *
        !           375: cmd_newest_client(void)
        !           376: {
        !           377:        struct client   *c, *cnewest;
        !           378:        struct timeval  *tv = NULL;
        !           379:        u_int            i;
        !           380:
        !           381:        cnewest = NULL;
        !           382:        for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
        !           383:                if ((c = ARRAY_ITEM(&clients, i)) == NULL)
        !           384:                        continue;
        !           385:                if (c->session == NULL)
        !           386:                        continue;
        !           387:
        !           388:                if (tv == NULL || timercmp(&c->tv, tv, >)) {
        !           389:                        cnewest = c;
        !           390:                        tv = &c->tv;
        !           391:                }
        !           392:        }
        !           393:
        !           394:        return (cnewest);
        !           395: }
        !           396:
1.5       nicm      397: /* Find the target client or report an error and return NULL. */
1.1       nicm      398: struct client *
                    399: cmd_find_client(struct cmd_ctx *ctx, const char *arg)
                    400: {
                    401:        struct client   *c;
1.20    ! nicm      402:        struct session  *s;
1.5       nicm      403:        char            *tmparg;
                    404:        size_t           arglen;
1.20    ! nicm      405:        u_int            i;
1.1       nicm      406:
1.5       nicm      407:        /* A NULL argument means the current client. */
1.20    ! nicm      408:        if (arg == NULL) {
        !           409:                if (ctx->curclient != NULL)
        !           410:                        return (ctx->curclient);
        !           411:                /*
        !           412:                 * No current client set. Find the current session and see if
        !           413:                 * it has only has one client.
        !           414:                 */
        !           415:                s = cmd_current_session(ctx);
        !           416:                if (s != NULL) {
        !           417:                        c = NULL;
        !           418:                        for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
        !           419:                                if (ARRAY_ITEM(&clients, i)->session == s) {
        !           420:                                        if (c != NULL)
        !           421:                                                break;
        !           422:                                        c = ARRAY_ITEM(&clients, i);
        !           423:                                }
        !           424:                        }
        !           425:                        if (i == ARRAY_LENGTH(&clients) && c != NULL)
        !           426:                                return (c);
        !           427:                }
        !           428:                return (cmd_newest_client());
        !           429:        }
1.5       nicm      430:        tmparg = xstrdup(arg);
                    431:
                    432:        /* Trim a single trailing colon if any. */
                    433:        arglen = strlen(tmparg);
                    434:        if (arglen != 0 && tmparg[arglen - 1] == ':')
                    435:                tmparg[arglen - 1] = '\0';
                    436:
                    437:        /* Find the client, if any. */
                    438:        c = cmd_lookup_client(tmparg);
                    439:
                    440:        /* If no client found, report an error. */
                    441:        if (c == NULL)
                    442:                ctx->error(ctx, "client not found: %s", tmparg);
                    443:
                    444:        xfree(tmparg);
                    445:        return (c);
                    446: }
                    447:
                    448: /*
                    449:  * Lookup a client by device path. Either of a full match and a match without a
                    450:  * leading _PATH_DEV ("/dev/") is accepted.
                    451:  */
                    452: struct client *
                    453: cmd_lookup_client(const char *name)
                    454: {
                    455:        struct client   *c;
                    456:        const char      *path;
                    457:        u_int            i;
                    458:
                    459:        for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
                    460:                if ((c = ARRAY_ITEM(&clients, i)) == NULL)
                    461:                        continue;
                    462:                path = c->tty.path;
                    463:
                    464:                /* Check for exact matches. */
                    465:                if (strcmp(name, path) == 0)
                    466:                        return (c);
                    467:
                    468:                /* Check without leading /dev if present. */
                    469:                if (strncmp(path, _PATH_DEV, (sizeof _PATH_DEV) - 1) != 0)
                    470:                        continue;
                    471:                if (strcmp(name, path + (sizeof _PATH_DEV) - 1) == 0)
                    472:                        return (c);
                    473:        }
                    474:
                    475:        return (NULL);
                    476: }
                    477:
                    478: /* Lookup a session by name. If no session is found, NULL is returned. */
                    479: struct session *
                    480: cmd_lookup_session(const char *name, int *ambiguous)
                    481: {
                    482:        struct session  *s, *sfound;
                    483:        u_int            i;
                    484:
                    485:        *ambiguous = 0;
                    486:
                    487:        /*
                    488:         * Look for matches. Session names must be unique so an exact match
                    489:         * can't be ambigious and can just be returned.
                    490:         */
                    491:        sfound = NULL;
                    492:        for (i = 0; i < ARRAY_LENGTH(&sessions); i++) {
                    493:                if ((s = ARRAY_ITEM(&sessions, i)) == NULL)
                    494:                        continue;
                    495:
                    496:                /* Check for an exact match and return it if found. */
                    497:                if (strcmp(name, s->name) == 0)
                    498:                        return (s);
                    499:
                    500:                /* Then check for pattern matches. */
                    501:                if (strncmp(name, s->name, strlen(name)) == 0 ||
                    502:                    fnmatch(name, s->name, 0) == 0) {
                    503:                        if (sfound != NULL) {
                    504:                                *ambiguous = 1;
                    505:                                return (NULL);
                    506:                        }
                    507:                        sfound = s;
                    508:                }
                    509:        }
                    510:
                    511:        return (sfound);
                    512: }
                    513:
                    514: /*
1.8       nicm      515:  * Lookup a window or return -1 if not found or ambigious. First try as an
                    516:  * index and if invalid, use fnmatch or leading prefix. Return NULL but fill in
                    517:  * idx if the window index is a valid number but there is now window with that
                    518:  * index.
1.5       nicm      519:  */
                    520: struct winlink *
                    521: cmd_lookup_window(struct session *s, const char *name, int *ambiguous)
                    522: {
                    523:        struct winlink  *wl, *wlfound;
                    524:        const char      *errstr;
                    525:        u_int            idx;
                    526:
                    527:        *ambiguous = 0;
                    528:
                    529:        /* First see if this is a valid window index in this session. */
                    530:        idx = strtonum(name, 0, INT_MAX, &errstr);
                    531:        if (errstr == NULL) {
                    532:                if ((wl = winlink_find_by_index(&s->windows, idx)) != NULL)
                    533:                        return (wl);
                    534:        }
                    535:
                    536:        /* Look for exact matches, error if more than one. */
                    537:        wlfound = NULL;
                    538:        RB_FOREACH(wl, winlinks, &s->windows) {
1.8       nicm      539:                if (strcmp(name, wl->window->name) == 0) {
1.5       nicm      540:                        if (wlfound != NULL) {
                    541:                                *ambiguous = 1;
                    542:                                return (NULL);
                    543:                        }
                    544:                        wlfound = wl;
1.1       nicm      545:                }
                    546:        }
1.5       nicm      547:        if (wlfound != NULL)
                    548:                return (wlfound);
                    549:
                    550:        /* Now look for pattern matches, again error if multiple. */
                    551:        wlfound = NULL;
                    552:        RB_FOREACH(wl, winlinks, &s->windows) {
1.8       nicm      553:                if (strncmp(name, wl->window->name, strlen(name)) == 0 ||
                    554:                    fnmatch(name, wl->window->name, 0) == 0) {
1.5       nicm      555:                        if (wlfound != NULL) {
                    556:                                *ambiguous = 1;
                    557:                                return (NULL);
                    558:                        }
                    559:                        wlfound = wl;
                    560:                }
                    561:        }
                    562:        if (wlfound != NULL)
                    563:                return (wlfound);
                    564:
                    565:        return (NULL);
1.1       nicm      566: }
                    567:
1.8       nicm      568: /*
                    569:  * Find a window index - if the window doesn't exist, check if it is a
                    570:  * potential index and return it anyway.
                    571:  */
                    572: int
                    573: cmd_lookup_index(struct session *s, const char *name, int *ambiguous)
                    574: {
                    575:        struct winlink  *wl;
                    576:        const char      *errstr;
                    577:        u_int            idx;
                    578:
                    579:        if ((wl = cmd_lookup_window(s, name, ambiguous)) != NULL)
                    580:                return (wl->idx);
                    581:        if (*ambiguous)
                    582:                return (-1);
                    583:
                    584:        idx = strtonum(name, 0, INT_MAX, &errstr);
                    585:        if (errstr == NULL)
                    586:                return (idx);
                    587:
                    588:        return (-1);
                    589: }
                    590:
1.5       nicm      591: /* Find the target session or report an error and return NULL. */
1.1       nicm      592: struct session *
                    593: cmd_find_session(struct cmd_ctx *ctx, const char *arg)
                    594: {
                    595:        struct session  *s;
1.5       nicm      596:        struct client   *c;
                    597:        char            *tmparg;
                    598:        size_t           arglen;
                    599:        int              ambiguous;
1.1       nicm      600:
1.5       nicm      601:        /* A NULL argument means the current session. */
1.1       nicm      602:        if (arg == NULL)
1.5       nicm      603:                return (cmd_current_session(ctx));
                    604:        tmparg = xstrdup(arg);
                    605:
                    606:        /* Trim a single trailing colon if any. */
                    607:        arglen = strlen(tmparg);
                    608:        if (arglen != 0 && tmparg[arglen - 1] == ':')
                    609:                tmparg[arglen - 1] = '\0';
                    610:
                    611:        /* Find the session, if any. */
                    612:        s = cmd_lookup_session(tmparg, &ambiguous);
                    613:
                    614:        /* If it doesn't, try to match it as a client. */
                    615:        if (s == NULL && (c = cmd_lookup_client(tmparg)) != NULL)
                    616:                s = c->session;
                    617:
                    618:        /* If no session found, report an error. */
                    619:        if (s == NULL) {
                    620:                if (ambiguous)
                    621:                        ctx->error(ctx, "more than one session: %s", tmparg);
                    622:                else
                    623:                        ctx->error(ctx, "session not found: %s", tmparg);
1.1       nicm      624:        }
1.5       nicm      625:
                    626:        xfree(tmparg);
1.1       nicm      627:        return (s);
                    628: }
                    629:
1.5       nicm      630: /* Find the target session and window or report an error and return NULL. */
1.1       nicm      631: struct winlink *
                    632: cmd_find_window(struct cmd_ctx *ctx, const char *arg, struct session **sp)
                    633: {
                    634:        struct session  *s;
                    635:        struct winlink  *wl;
1.5       nicm      636:        const char      *winptr;
                    637:        char            *sessptr = NULL;
                    638:        int              ambiguous = 0;
                    639:
                    640:        /*
                    641:         * Find the current session. There must always be a current session, if
                    642:         * it can't be found, report an error.
                    643:         */
                    644:        if ((s = cmd_current_session(ctx)) == NULL) {
                    645:                ctx->error(ctx, "can't establish current session");
                    646:                return (NULL);
                    647:        }
                    648:
                    649:        /* A NULL argument means the current session and window. */
                    650:        if (arg == NULL) {
                    651:                if (sp != NULL)
                    652:                        *sp = s;
                    653:                return (s->curw);
                    654:        }
                    655:
                    656:        /* Time to look at the argument. If it is empty, that is an error. */
                    657:        if (*arg == '\0')
                    658:                goto not_found;
                    659:
1.8       nicm      660:        /* Find the separating colon and split into window and session. */
1.5       nicm      661:        winptr = strchr(arg, ':');
                    662:        if (winptr == NULL)
1.8       nicm      663:                goto no_colon;
                    664:        winptr++;       /* skip : */
                    665:        sessptr = xstrdup(arg);
                    666:        *strchr(sessptr, ':') = '\0';
1.5       nicm      667:
                    668:        /* Try to lookup the session if present. */
1.8       nicm      669:        if (*sessptr != '\0') {
                    670:                if ((s = cmd_lookup_session(sessptr, &ambiguous)) == NULL)
1.5       nicm      671:                        goto no_session;
                    672:        }
                    673:        if (sp != NULL)
                    674:                *sp = s;
                    675:
                    676:        /*
                    677:         * Then work out the window. An empty string is the current window,
                    678:         * otherwise try to look it up in the session.
                    679:         */
1.8       nicm      680:        if (*winptr == '\0')
1.5       nicm      681:                wl = s->curw;
                    682:        else if ((wl = cmd_lookup_window(s, winptr, &ambiguous)) == NULL)
                    683:                goto not_found;
                    684:
                    685:        if (sessptr != NULL)
                    686:                xfree(sessptr);
                    687:        return (wl);
                    688:
1.8       nicm      689: no_colon:
                    690:        /* No colon in the string, first try as a window then as a session. */
                    691:        if ((wl = cmd_lookup_window(s, arg, &ambiguous)) == NULL) {
                    692:                if (ambiguous)
                    693:                        goto not_found;
                    694:                if ((s = cmd_lookup_session(arg, &ambiguous)) == NULL)
                    695:                        goto no_session;
                    696:                wl = s->curw;
                    697:        }
                    698:
                    699:        if (sp != NULL)
                    700:                *sp = s;
                    701:
                    702:        return (wl);
                    703:
1.5       nicm      704: no_session:
                    705:        if (ambiguous)
1.8       nicm      706:                ctx->error(ctx, "multiple sessions: %s", arg);
1.5       nicm      707:        else
1.8       nicm      708:                ctx->error(ctx, "session not found: %s", arg);
1.5       nicm      709:        if (sessptr != NULL)
                    710:                xfree(sessptr);
                    711:        return (NULL);
                    712:
                    713: not_found:
                    714:        if (ambiguous)
                    715:                ctx->error(ctx, "multiple windows: %s", arg);
                    716:        else
                    717:                ctx->error(ctx, "window not found: %s", arg);
                    718:        if (sessptr != NULL)
                    719:                xfree(sessptr);
                    720:        return (NULL);
                    721: }
1.1       nicm      722:
1.5       nicm      723: /*
                    724:  * Find the target session and window index, whether or not it exists in the
                    725:  * session. Return -2 on error or -1 if no window index is specified. This is
1.8       nicm      726:  * used when parsing an argument for a window target that may not exist (for
                    727:  * example if it is going to be created).
1.5       nicm      728:  */
                    729: int
                    730: cmd_find_index(struct cmd_ctx *ctx, const char *arg, struct session **sp)
                    731: {
                    732:        struct session  *s;
1.8       nicm      733:        const char      *winptr;
1.5       nicm      734:        char            *sessptr = NULL;
                    735:        int              idx, ambiguous = 0;
                    736:
                    737:        /*
                    738:         * Find the current session. There must always be a current session, if
                    739:         * it can't be found, report an error.
                    740:         */
                    741:        if ((s = cmd_current_session(ctx)) == NULL) {
                    742:                ctx->error(ctx, "can't establish current session");
1.9       nicm      743:                return (-2);
1.1       nicm      744:        }
1.5       nicm      745:
                    746:        /* A NULL argument means the current session and "no window" (-1). */
                    747:        if (arg == NULL) {
                    748:                if (sp != NULL)
                    749:                        *sp = s;
                    750:                return (-1);
                    751:        }
                    752:
                    753:        /* Time to look at the argument. If it is empty, that is an error. */
                    754:        if (*arg == '\0')
                    755:                goto not_found;
                    756:
                    757:        /* Find the separating colon. If none, assume the current session. */
                    758:        winptr = strchr(arg, ':');
                    759:        if (winptr == NULL)
1.8       nicm      760:                goto no_colon;
                    761:        winptr++;       /* skip : */
                    762:        sessptr = xstrdup(arg);
                    763:        *strchr(sessptr, ':') = '\0';
1.5       nicm      764:
                    765:        /* Try to lookup the session if present. */
                    766:        if (sessptr != NULL && *sessptr != '\0') {
1.8       nicm      767:                if ((s = cmd_lookup_session(sessptr, &ambiguous)) == NULL)
1.5       nicm      768:                        goto no_session;
                    769:        }
1.1       nicm      770:        if (sp != NULL)
                    771:                *sp = s;
                    772:
1.5       nicm      773:        /*
1.8       nicm      774:         * Then work out the window. An empty string is a new window otherwise
                    775:         * try to look it up in the session.
1.5       nicm      776:         */
1.8       nicm      777:        if (*winptr == '\0')
                    778:                 idx = -1;
                    779:        else if ((idx = cmd_lookup_index(s, winptr, &ambiguous)) == -1) {
1.5       nicm      780:                if (ambiguous)
                    781:                        goto not_found;
1.8       nicm      782:                ctx->error(ctx, "invalid index: %s", arg);
                    783:                idx = -2;
                    784:        }
1.5       nicm      785:
                    786:        if (sessptr != NULL)
                    787:                xfree(sessptr);
                    788:        return (idx);
                    789:
1.8       nicm      790: no_colon:
                    791:        /* No colon in the string, first try as a window then as a session. */
                    792:        if ((idx = cmd_lookup_index(s, arg, &ambiguous)) == -1) {
                    793:                if (ambiguous)
                    794:                        goto not_found;
                    795:                if ((s = cmd_lookup_session(arg, &ambiguous)) == NULL)
                    796:                        goto no_session;
                    797:                idx = -1;
                    798:        }
                    799:
                    800:        if (sp != NULL)
                    801:                *sp = s;
                    802:
                    803:        return (idx);
                    804:
1.5       nicm      805: no_session:
                    806:        if (ambiguous)
1.8       nicm      807:                ctx->error(ctx, "multiple sessions: %s", arg);
1.5       nicm      808:        else
1.8       nicm      809:                ctx->error(ctx, "session not found: %s", arg);
1.5       nicm      810:        if (sessptr != NULL)
                    811:                xfree(sessptr);
                    812:        return (-2);
                    813:
                    814: not_found:
                    815:        if (ambiguous)
                    816:                ctx->error(ctx, "multiple windows: %s", arg);
1.1       nicm      817:        else
1.5       nicm      818:                ctx->error(ctx, "window not found: %s", arg);
                    819:        if (sessptr != NULL)
                    820:                xfree(sessptr);
                    821:        return (-2);
1.12      nicm      822: }
                    823:
                    824: /*
                    825:  * Find the target session, window and pane number or report an error and
                    826:  * return NULL. The pane number is separated from the session:window by a .,
                    827:  * such as mysession:mywindow.0.
                    828:  */
                    829: struct winlink *
                    830: cmd_find_pane(struct cmd_ctx *ctx,
                    831:     const char *arg, struct session **sp, struct window_pane **wpp)
                    832: {
                    833:        struct session  *s;
                    834:        struct winlink  *wl;
                    835:        const char      *period;
                    836:        char            *winptr, *paneptr;
                    837:        const char      *errstr;
                    838:        u_int            idx;
                    839:
                    840:        /* Get the current session. */
                    841:        if ((s = cmd_current_session(ctx)) == NULL) {
                    842:                        ctx->error(ctx, "can't establish current session");
                    843:                return (NULL);
                    844:        }
                    845:        if (sp != NULL)
                    846:                *sp = s;
                    847:
                    848:        /* A NULL argument means the current session, window and pane. */
                    849:        if (arg == NULL) {
                    850:                *wpp = s->curw->window->active;
                    851:                return (s->curw);
                    852:        }
                    853:
                    854:        /* Look for a separating period. */
                    855:        if ((period = strrchr(arg, '.')) == NULL)
                    856:                goto no_period;
                    857:
                    858:        /* Pull out the window part and parse it. */
                    859:        winptr = xstrdup(arg);
                    860:        winptr[period - arg] = '\0';
                    861:        if (*winptr == '\0')
                    862:                wl = s->curw;
                    863:        else if ((wl = cmd_find_window(ctx, winptr, sp)) == NULL)
                    864:                goto error;
                    865:
                    866:        /* Find the pane section and look it up. */
                    867:        paneptr = winptr + (period - arg) + 1;
                    868:        if (*paneptr == '\0')
                    869:                *wpp = wl->window->active;
                    870:        else {
                    871:                idx = strtonum(paneptr, 0, INT_MAX, &errstr);
                    872:                if (errstr != NULL) {
                    873:                        ctx->error(ctx, "pane %s: %s", errstr, paneptr);
                    874:                        goto error;
                    875:                }
                    876:                *wpp = window_pane_at_index(wl->window, idx);
                    877:                if (*wpp == NULL) {
                    878:                        ctx->error(ctx, "no such pane: %u", idx);
                    879:                        goto error;
                    880:                }
                    881:        }
                    882:
                    883:        xfree(winptr);
                    884:        return (wl);
                    885:
                    886: no_period:
                    887:        /* Try as a pane number alone. */
                    888:        idx = strtonum(arg, 0, INT_MAX, &errstr);
                    889:        if (errstr != NULL)
                    890:                goto lookup_window;
                    891:
                    892:        /* Try index in the current session and window. */
                    893:        if ((*wpp = window_pane_at_index(s->curw->window, idx)) == NULL)
                    894:                goto lookup_window;
                    895:
                    896:        return (s->curw);
                    897:
                    898: lookup_window:
                    899:        /* Try as a window and use the active pane. */
                    900:        if ((wl = cmd_find_window(ctx, arg, sp)) != NULL)
                    901:                *wpp = wl->window->active;
                    902:        return (wl);
                    903:
                    904: error:
                    905:        xfree(winptr);
                    906:        return (NULL);
1.15      nicm      907: }
                    908:
                    909: /* Replace the first %% or %idx in template by s. */
                    910: char *
                    911: cmd_template_replace(char *template, const char *s, int idx)
                    912: {
                    913:        char     ch;
                    914:        char    *buf, *ptr;
                    915:        int      replaced;
                    916:        size_t   len;
                    917:
                    918:        if (strstr(template, "%") == NULL)
                    919:                return (xstrdup(template));
                    920:
                    921:        buf = xmalloc(1);
                    922:        *buf = '\0';
                    923:        len = 0;
                    924:        replaced = 0;
                    925:
                    926:        ptr = template;
                    927:        while (*ptr != '\0') {
                    928:                switch (ch = *ptr++) {
                    929:                case '%':
                    930:                        if (*ptr < '1' || *ptr > '9' || *ptr - '0' != idx) {
                    931:                                if (*ptr != '%' || replaced)
                    932:                                        break;
                    933:                                replaced = 1;
                    934:                        }
                    935:                        ptr++;
                    936:
                    937:                        len += strlen(s);
                    938:                        buf = xrealloc(buf, 1, len + 1);
                    939:                        strlcat(buf, s, len + 1);
                    940:                        continue;
                    941:                }
                    942:                buf = xrealloc(buf, 1, len + 2);
                    943:                buf[len++] = ch;
                    944:                buf[len] = '\0';
                    945:        }
                    946:
                    947:        return (buf);
1.1       nicm      948: }