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

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