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

1.5     ! nicm        1: /* $OpenBSD: cmd.c,v 1.4 2009/07/09 15:47:49 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,
                     34:        &cmd_choose_session_entry,
                     35:        &cmd_choose_window_entry,
                     36:        &cmd_clear_history_entry,
                     37:        &cmd_clock_mode_entry,
                     38:        &cmd_command_prompt_entry,
                     39:        &cmd_confirm_before_entry,
                     40:        &cmd_copy_buffer_entry,
                     41:        &cmd_copy_mode_entry,
                     42:        &cmd_delete_buffer_entry,
                     43:        &cmd_detach_client_entry,
                     44:        &cmd_down_pane_entry,
                     45:        &cmd_find_window_entry,
                     46:        &cmd_has_session_entry,
1.4       nicm       47:        &cmd_if_shell_entry,
1.1       nicm       48:        &cmd_kill_pane_entry,
                     49:        &cmd_kill_server_entry,
                     50:        &cmd_kill_session_entry,
                     51:        &cmd_kill_window_entry,
                     52:        &cmd_last_window_entry,
                     53:        &cmd_link_window_entry,
                     54:        &cmd_list_buffers_entry,
                     55:        &cmd_list_clients_entry,
                     56:        &cmd_list_commands_entry,
                     57:        &cmd_list_keys_entry,
                     58:        &cmd_list_sessions_entry,
                     59:        &cmd_list_windows_entry,
                     60:        &cmd_load_buffer_entry,
                     61:        &cmd_lock_server_entry,
                     62:        &cmd_move_window_entry,
                     63:        &cmd_new_session_entry,
                     64:        &cmd_new_window_entry,
                     65:        &cmd_next_layout_entry,
                     66:        &cmd_next_window_entry,
                     67:        &cmd_paste_buffer_entry,
                     68:        &cmd_previous_layout_entry,
                     69:        &cmd_previous_window_entry,
                     70:        &cmd_refresh_client_entry,
                     71:        &cmd_rename_session_entry,
                     72:        &cmd_rename_window_entry,
                     73:        &cmd_resize_pane_entry,
                     74:        &cmd_respawn_window_entry,
                     75:        &cmd_rotate_window_entry,
                     76:        &cmd_save_buffer_entry,
                     77:        &cmd_scroll_mode_entry,
                     78:        &cmd_select_layout_entry,
                     79:        &cmd_select_pane_entry,
                     80:        &cmd_select_prompt_entry,
                     81:        &cmd_select_window_entry,
                     82:        &cmd_send_keys_entry,
                     83:        &cmd_send_prefix_entry,
                     84:        &cmd_server_info_entry,
                     85:        &cmd_set_buffer_entry,
                     86:        &cmd_set_option_entry,
                     87:        &cmd_set_password_entry,
                     88:        &cmd_set_window_option_entry,
                     89:        &cmd_show_buffer_entry,
                     90:        &cmd_show_options_entry,
                     91:        &cmd_show_window_options_entry,
                     92:        &cmd_source_file_entry,
                     93:        &cmd_split_window_entry,
                     94:        &cmd_start_server_entry,
                     95:        &cmd_suspend_client_entry,
                     96:        &cmd_swap_pane_entry,
                     97:        &cmd_swap_window_entry,
                     98:        &cmd_switch_client_entry,
                     99:        &cmd_unbind_key_entry,
                    100:        &cmd_unlink_window_entry,
                    101:        &cmd_up_pane_entry,
                    102:        NULL
                    103: };
                    104:
1.5     ! nicm      105: struct session *cmd_newest_session(void);
        !           106: struct client  *cmd_lookup_client(const char *);
        !           107: struct session *cmd_lookup_session(const char *, int *);
        !           108: struct winlink *cmd_lookup_window(struct session *, const char *, int *);
        !           109:
1.1       nicm      110: struct cmd *
                    111: cmd_parse(int argc, char **argv, char **cause)
                    112: {
                    113:        const struct cmd_entry **entryp, *entry;
                    114:        struct cmd              *cmd;
                    115:        char                     s[BUFSIZ];
1.3       nicm      116:        int                      opt, ambiguous = 0;
1.1       nicm      117:
                    118:        *cause = NULL;
1.2       nicm      119:        if (argc == 0) {
                    120:                xasprintf(cause, "no command");
1.1       nicm      121:                return (NULL);
1.2       nicm      122:        }
1.1       nicm      123:
                    124:        entry = NULL;
                    125:        for (entryp = cmd_table; *entryp != NULL; entryp++) {
                    126:                if ((*entryp)->alias != NULL &&
                    127:                    strcmp((*entryp)->alias, argv[0]) == 0) {
1.3       nicm      128:                        ambiguous = 0;
1.1       nicm      129:                        entry = *entryp;
                    130:                        break;
                    131:                }
                    132:
                    133:                if (strncmp((*entryp)->name, argv[0], strlen(argv[0])) != 0)
                    134:                        continue;
                    135:                if (entry != NULL)
1.3       nicm      136:                        ambiguous = 1;
1.1       nicm      137:                entry = *entryp;
                    138:
                    139:                /* Bail now if an exact match. */
                    140:                if (strcmp(entry->name, argv[0]) == 0)
                    141:                        break;
                    142:        }
1.3       nicm      143:        if (ambiguous)
                    144:                goto ambiguous;
1.1       nicm      145:        if (entry == NULL) {
                    146:                xasprintf(cause, "unknown command: %s", argv[0]);
                    147:                return (NULL);
                    148:        }
                    149:
                    150:        optreset = 1;
                    151:        optind = 1;
                    152:        if (entry->parse == NULL) {
                    153:                while ((opt = getopt(argc, argv, "")) != -1) {
                    154:                        switch (opt) {
                    155:                        default:
                    156:                                goto usage;
                    157:                        }
                    158:                }
                    159:                argc -= optind;
                    160:                argv += optind;
                    161:                if (argc != 0)
                    162:                        goto usage;
                    163:        }
                    164:
                    165:        cmd = xmalloc(sizeof *cmd);
                    166:        cmd->entry = entry;
                    167:        cmd->data = NULL;
                    168:        if (entry->parse != NULL) {
                    169:                if (entry->parse(cmd, argc, argv, cause) != 0) {
                    170:                        xfree(cmd);
                    171:                        return (NULL);
                    172:                }
                    173:        }
                    174:        return (cmd);
                    175:
                    176: ambiguous:
                    177:        *s = '\0';
                    178:        for (entryp = cmd_table; *entryp != NULL; entryp++) {
                    179:                if (strncmp((*entryp)->name, argv[0], strlen(argv[0])) != 0)
                    180:                        continue;
                    181:                if (strlcat(s, (*entryp)->name, sizeof s) >= sizeof s)
                    182:                        break;
                    183:                if (strlcat(s, ", ", sizeof s) >= sizeof s)
                    184:                        break;
                    185:        }
                    186:        s[strlen(s) - 2] = '\0';
                    187:        xasprintf(cause, "ambiguous command: %s, could be: %s", argv[0], s);
                    188:        return (NULL);
                    189:
                    190: usage:
                    191:        xasprintf(cause, "usage: %s %s", entry->name, entry->usage);
                    192:        return (NULL);
                    193: }
                    194:
                    195: int
                    196: cmd_exec(struct cmd *cmd, struct cmd_ctx *ctx)
                    197: {
                    198:        if (server_locked) {
                    199:                ctx->error(ctx, "server is locked");
                    200:                return (-1);
                    201:        }
                    202:        return (cmd->entry->exec(cmd, ctx));
                    203: }
                    204:
                    205: void
                    206: cmd_send(struct cmd *cmd, struct buffer *b)
                    207: {
                    208:        const struct cmd_entry **entryp;
                    209:        u_int                    n;
                    210:
                    211:        n = 0;
                    212:        for (entryp = cmd_table; *entryp != NULL; entryp++) {
                    213:                if (*entryp == cmd->entry)
                    214:                        break;
                    215:                n++;
                    216:        }
                    217:        if (*entryp == NULL)
                    218:                fatalx("command not found");
                    219:
                    220:        buffer_write(b, &n, sizeof n);
                    221:
                    222:        if (cmd->entry->send != NULL)
                    223:                cmd->entry->send(cmd, b);
                    224: }
                    225:
                    226: struct cmd *
                    227: cmd_recv(struct buffer *b)
                    228: {
                    229:        const struct cmd_entry **entryp;
                    230:        struct cmd              *cmd;
                    231:        u_int                    m, n;
                    232:
                    233:        buffer_read(b, &m, sizeof m);
                    234:
                    235:        n = 0;
                    236:        for (entryp = cmd_table; *entryp != NULL; entryp++) {
                    237:                if (n == m)
                    238:                        break;
                    239:                n++;
                    240:        }
                    241:        if (*entryp == NULL)
                    242:                fatalx("command not found");
                    243:
                    244:        cmd = xmalloc(sizeof *cmd);
                    245:        cmd->entry = *entryp;
                    246:
                    247:        if (cmd->entry->recv != NULL)
                    248:                cmd->entry->recv(cmd, b);
                    249:        return (cmd);
                    250: }
                    251:
                    252: void
                    253: cmd_free(struct cmd *cmd)
                    254: {
                    255:        if (cmd->data != NULL && cmd->entry->free != NULL)
                    256:                cmd->entry->free(cmd);
                    257:        xfree(cmd);
                    258: }
                    259:
                    260: size_t
                    261: cmd_print(struct cmd *cmd, char *buf, size_t len)
                    262: {
                    263:        if (cmd->entry->print == NULL) {
                    264:                return (xsnprintf(buf, len, "%s", cmd->entry->name));
                    265:        }
                    266:        return (cmd->entry->print(cmd, buf, len));
                    267: }
                    268:
                    269: void
                    270: cmd_send_string(struct buffer *b, const char *s)
                    271: {
                    272:        size_t  n;
                    273:
                    274:        if (s == NULL) {
                    275:                n = 0;
                    276:                buffer_write(b, &n, sizeof n);
                    277:                return;
                    278:        }
                    279:
                    280:        n = strlen(s) + 1;
                    281:        buffer_write(b, &n, sizeof n);
                    282:
                    283:        buffer_write(b, s, n);
                    284: }
                    285:
                    286: char *
                    287: cmd_recv_string(struct buffer *b)
                    288: {
                    289:        char   *s;
                    290:        size_t  n;
                    291:
                    292:        buffer_read(b, &n, sizeof n);
                    293:
                    294:        if (n == 0)
                    295:                return (NULL);
                    296:
                    297:        s = xmalloc(n);
                    298:        buffer_read(b, s, n);
                    299:        s[n - 1] = '\0';
                    300:
                    301:        return (s);
                    302: }
                    303:
1.5     ! nicm      304: /*
        !           305:  * Figure out the current session. Use: 1) the current session, if the command
        !           306:  * context has one; 2) the session specified in the TMUX variable from the
        !           307:  * environment (as passed from the client); 3) the newest session.
        !           308:  */
1.1       nicm      309: struct session *
                    310: cmd_current_session(struct cmd_ctx *ctx)
                    311: {
                    312:        struct msg_command_data *data = ctx->msgdata;
1.5     ! nicm      313:        struct session          *s;
1.1       nicm      314:
                    315:        if (ctx->cursession != NULL)
                    316:                return (ctx->cursession);
                    317:
                    318:        if (data != NULL && data->pid != -1) {
1.5     ! nicm      319:                if (data->pid != getpid())
1.1       nicm      320:                        return (NULL);
1.5     ! nicm      321:                if (data->idx > ARRAY_LENGTH(&sessions))
1.1       nicm      322:                        return (NULL);
1.5     ! nicm      323:                if ((s = ARRAY_ITEM(&sessions, data->idx)) == NULL)
1.1       nicm      324:                        return (NULL);
                    325:                return (s);
                    326:        }
                    327:
1.5     ! nicm      328:        return (cmd_newest_session());
        !           329: }
        !           330:
        !           331: /* Find the newest session. */
        !           332: struct session *
        !           333: cmd_newest_session(void)
        !           334: {
        !           335:        struct session  *s, *snewest;
        !           336:        struct timeval  *tv = NULL;
        !           337:        u_int            i;
        !           338:
        !           339:        snewest = NULL;
1.1       nicm      340:        for (i = 0; i < ARRAY_LENGTH(&sessions); i++) {
1.5     ! nicm      341:                if ((s = ARRAY_ITEM(&sessions, i)) == NULL)
        !           342:                        continue;
        !           343:
        !           344:                if (tv == NULL || timercmp(&s->tv, tv, >)) {
        !           345:                        snewest = s;
1.1       nicm      346:                        tv = &s->tv;
                    347:                }
                    348:        }
1.5     ! nicm      349:
        !           350:        return (snewest);
1.1       nicm      351: }
                    352:
1.5     ! nicm      353: /* Find the target client or report an error and return NULL. */
1.1       nicm      354: struct client *
                    355: cmd_find_client(struct cmd_ctx *ctx, const char *arg)
                    356: {
                    357:        struct client   *c;
1.5     ! nicm      358:        char            *tmparg;
        !           359:        size_t           arglen;
1.1       nicm      360:
1.5     ! nicm      361:        /* A NULL argument means the current client. */
1.1       nicm      362:        if (arg == NULL)
1.5     ! nicm      363:                return (ctx->curclient);
        !           364:        tmparg = xstrdup(arg);
        !           365:
        !           366:        /* Trim a single trailing colon if any. */
        !           367:        arglen = strlen(tmparg);
        !           368:        if (arglen != 0 && tmparg[arglen - 1] == ':')
        !           369:                tmparg[arglen - 1] = '\0';
        !           370:
        !           371:        /* Find the client, if any. */
        !           372:        c = cmd_lookup_client(tmparg);
        !           373:
        !           374:        /* If no client found, report an error. */
        !           375:        if (c == NULL)
        !           376:                ctx->error(ctx, "client not found: %s", tmparg);
        !           377:
        !           378:        xfree(tmparg);
        !           379:        return (c);
        !           380: }
        !           381:
        !           382: /*
        !           383:  * Lookup a client by device path. Either of a full match and a match without a
        !           384:  * leading _PATH_DEV ("/dev/") is accepted.
        !           385:  */
        !           386: struct client *
        !           387: cmd_lookup_client(const char *name)
        !           388: {
        !           389:        struct client   *c;
        !           390:        const char      *path;
        !           391:        u_int            i;
        !           392:
        !           393:        for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
        !           394:                if ((c = ARRAY_ITEM(&clients, i)) == NULL)
        !           395:                        continue;
        !           396:                path = c->tty.path;
        !           397:
        !           398:                /* Check for exact matches. */
        !           399:                if (strcmp(name, path) == 0)
        !           400:                        return (c);
        !           401:
        !           402:                /* Check without leading /dev if present. */
        !           403:                if (strncmp(path, _PATH_DEV, (sizeof _PATH_DEV) - 1) != 0)
        !           404:                        continue;
        !           405:                if (strcmp(name, path + (sizeof _PATH_DEV) - 1) == 0)
        !           406:                        return (c);
        !           407:        }
        !           408:
        !           409:        return (NULL);
        !           410: }
        !           411:
        !           412: /* Lookup a session by name. If no session is found, NULL is returned. */
        !           413: struct session *
        !           414: cmd_lookup_session(const char *name, int *ambiguous)
        !           415: {
        !           416:        struct session  *s, *sfound;
        !           417:        u_int            i;
        !           418:
        !           419:        *ambiguous = 0;
        !           420:
        !           421:        /*
        !           422:         * Look for matches. Session names must be unique so an exact match
        !           423:         * can't be ambigious and can just be returned.
        !           424:         */
        !           425:        sfound = NULL;
        !           426:        for (i = 0; i < ARRAY_LENGTH(&sessions); i++) {
        !           427:                if ((s = ARRAY_ITEM(&sessions, i)) == NULL)
        !           428:                        continue;
        !           429:
        !           430:                /* Check for an exact match and return it if found. */
        !           431:                if (strcmp(name, s->name) == 0)
        !           432:                        return (s);
        !           433:
        !           434:                /* Then check for pattern matches. */
        !           435:                if (strncmp(name, s->name, strlen(name)) == 0 ||
        !           436:                    fnmatch(name, s->name, 0) == 0) {
        !           437:                        if (sfound != NULL) {
        !           438:                                *ambiguous = 1;
        !           439:                                return (NULL);
        !           440:                        }
        !           441:                        sfound = s;
        !           442:                }
        !           443:        }
        !           444:
        !           445:        return (sfound);
        !           446: }
        !           447:
        !           448: /*
        !           449:  * Lookup a window or return -1 if not found or ambigious. First try as an index
        !           450:  * and if invalid, use fnmatch or leading prefix.
        !           451:  */
        !           452: struct winlink *
        !           453: cmd_lookup_window(struct session *s, const char *name, int *ambiguous)
        !           454: {
        !           455:        struct winlink  *wl, *wlfound;
        !           456:        struct window   *w;
        !           457:        const char      *errstr;
        !           458:        u_int            idx;
        !           459:
        !           460:        *ambiguous = 0;
        !           461:
        !           462:        /* First see if this is a valid window index in this session. */
        !           463:        idx = strtonum(name, 0, INT_MAX, &errstr);
        !           464:        if (errstr == NULL) {
        !           465:                if ((wl = winlink_find_by_index(&s->windows, idx)) != NULL)
        !           466:                        return (wl);
        !           467:        }
        !           468:
        !           469:        /* Look for exact matches, error if more than one. */
        !           470:        wlfound = NULL;
        !           471:        RB_FOREACH(wl, winlinks, &s->windows) {
        !           472:                w = wl->window;
        !           473:                if (strcmp(name, w->name) == 0) {
        !           474:                        if (wlfound != NULL) {
        !           475:                                *ambiguous = 1;
        !           476:                                return (NULL);
        !           477:                        }
        !           478:                        wlfound = wl;
1.1       nicm      479:                }
                    480:        }
1.5     ! nicm      481:        if (wlfound != NULL)
        !           482:                return (wlfound);
        !           483:
        !           484:        /* Now look for pattern matches, again error if multiple. */
        !           485:        wlfound = NULL;
        !           486:        RB_FOREACH(wl, winlinks, &s->windows) {
        !           487:                w = wl->window;
        !           488:                if (strncmp(name, w->name, strlen(name)) == 0 ||
        !           489:                    fnmatch(name, w->name, 0) == 0) {
        !           490:                        if (wlfound != NULL) {
        !           491:                                *ambiguous = 1;
        !           492:                                return (NULL);
        !           493:                        }
        !           494:                        wlfound = wl;
        !           495:                }
        !           496:        }
        !           497:        if (wlfound != NULL)
        !           498:                return (wlfound);
        !           499:
        !           500:        return (NULL);
1.1       nicm      501: }
                    502:
1.5     ! nicm      503: /* Find the target session or report an error and return NULL. */
1.1       nicm      504: struct session *
                    505: cmd_find_session(struct cmd_ctx *ctx, const char *arg)
                    506: {
                    507:        struct session  *s;
1.5     ! nicm      508:        struct client   *c;
        !           509:        char            *tmparg;
        !           510:        size_t           arglen;
        !           511:        int              ambiguous;
1.1       nicm      512:
1.5     ! nicm      513:        /* A NULL argument means the current session. */
1.1       nicm      514:        if (arg == NULL)
1.5     ! nicm      515:                return (cmd_current_session(ctx));
        !           516:        tmparg = xstrdup(arg);
        !           517:
        !           518:        /* Trim a single trailing colon if any. */
        !           519:        arglen = strlen(tmparg);
        !           520:        if (arglen != 0 && tmparg[arglen - 1] == ':')
        !           521:                tmparg[arglen - 1] = '\0';
        !           522:
        !           523:        /* Find the session, if any. */
        !           524:        s = cmd_lookup_session(tmparg, &ambiguous);
        !           525:
        !           526:        /* If it doesn't, try to match it as a client. */
        !           527:        if (s == NULL && (c = cmd_lookup_client(tmparg)) != NULL)
        !           528:                s = c->session;
        !           529:
        !           530:        /* If no session found, report an error. */
        !           531:        if (s == NULL) {
        !           532:                if (ambiguous)
        !           533:                        ctx->error(ctx, "more than one session: %s", tmparg);
        !           534:                else
        !           535:                        ctx->error(ctx, "session not found: %s", tmparg);
1.1       nicm      536:        }
1.5     ! nicm      537:
        !           538:        xfree(tmparg);
1.1       nicm      539:        return (s);
                    540: }
                    541:
1.5     ! nicm      542: /* Find the target session and window or report an error and return NULL. */
1.1       nicm      543: struct winlink *
                    544: cmd_find_window(struct cmd_ctx *ctx, const char *arg, struct session **sp)
                    545: {
                    546:        struct session  *s;
                    547:        struct winlink  *wl;
1.5     ! nicm      548:        const char      *winptr;
        !           549:        char            *sessptr = NULL;
        !           550:        int              ambiguous = 0;
        !           551:
        !           552:        /*
        !           553:         * Find the current session. There must always be a current session, if
        !           554:         * it can't be found, report an error.
        !           555:         */
        !           556:        if ((s = cmd_current_session(ctx)) == NULL) {
        !           557:                ctx->error(ctx, "can't establish current session");
        !           558:                return (NULL);
        !           559:        }
        !           560:
        !           561:        /* A NULL argument means the current session and window. */
        !           562:        if (arg == NULL) {
        !           563:                if (sp != NULL)
        !           564:                        *sp = s;
        !           565:                return (s->curw);
        !           566:        }
        !           567:
        !           568:        /* Time to look at the argument. If it is empty, that is an error. */
        !           569:        if (*arg == '\0')
        !           570:                goto not_found;
        !           571:
        !           572:        /* Find the separating colon. If none, assume the current session. */
        !           573:        winptr = strchr(arg, ':');
        !           574:        if (winptr == NULL)
        !           575:                winptr = xstrdup(arg);
        !           576:        else {
        !           577:                winptr++;       /* skip : */
        !           578:                sessptr = xstrdup(arg);
        !           579:                *strchr(sessptr, ':') = '\0';
        !           580:        }
        !           581:
        !           582:        log_debug("%s: winptr=%s, sessptr=%s", __func__, winptr, sessptr);
        !           583:
        !           584:        /* Try to lookup the session if present. */
        !           585:        if (sessptr != NULL && *sessptr != '\0') {
        !           586:                if  ((s = cmd_lookup_session(sessptr, &ambiguous)) == NULL)
        !           587:                        goto no_session;
        !           588:        }
        !           589:        if (sp != NULL)
        !           590:                *sp = s;
        !           591:
        !           592:        /*
        !           593:         * Then work out the window. An empty string is the current window,
        !           594:         * otherwise try to look it up in the session.
        !           595:         */
        !           596:        if (winptr == NULL || *winptr == '\0')
        !           597:                wl = s->curw;
        !           598:        else if ((wl = cmd_lookup_window(s, winptr, &ambiguous)) == NULL)
        !           599:                goto not_found;
        !           600:
        !           601:        if (sessptr != NULL)
        !           602:                xfree(sessptr);
        !           603:        return (wl);
        !           604:
        !           605: no_session:
        !           606:        if (ambiguous)
        !           607:                ctx->error(ctx, "multiple sessions: %s", sessptr);
        !           608:        else
        !           609:                ctx->error(ctx, "session not found: %s", sessptr);
        !           610:        if (sessptr != NULL)
        !           611:                xfree(sessptr);
        !           612:        return (NULL);
        !           613:
        !           614: not_found:
        !           615:        if (ambiguous)
        !           616:                ctx->error(ctx, "multiple windows: %s", arg);
        !           617:        else
        !           618:                ctx->error(ctx, "window not found: %s", arg);
        !           619:        if (sessptr != NULL)
        !           620:                xfree(sessptr);
        !           621:        return (NULL);
        !           622: }
1.1       nicm      623:
1.5     ! nicm      624: /*
        !           625:  * Find the target session and window index, whether or not it exists in the
        !           626:  * session. Return -2 on error or -1 if no window index is specified. This is
        !           627:  * used when parsing an argument for a window target that may not be exist (for
        !           628:  * example it is going to be created).
        !           629:  */
        !           630: int
        !           631: cmd_find_index(struct cmd_ctx *ctx, const char *arg, struct session **sp)
        !           632: {
        !           633:        struct session  *s;
        !           634:        struct winlink  *wl;
        !           635:        const char      *winptr, *errstr;
        !           636:        char            *sessptr = NULL;
        !           637:        int              idx, ambiguous = 0;
        !           638:
        !           639:        /*
        !           640:         * Find the current session. There must always be a current session, if
        !           641:         * it can't be found, report an error.
        !           642:         */
        !           643:        if ((s = cmd_current_session(ctx)) == NULL) {
        !           644:                ctx->error(ctx, "can't establish current session");
1.1       nicm      645:                return (NULL);
                    646:        }
1.5     ! nicm      647:
        !           648:        /* A NULL argument means the current session and "no window" (-1). */
        !           649:        if (arg == NULL) {
        !           650:                if (sp != NULL)
        !           651:                        *sp = s;
        !           652:                return (-1);
        !           653:        }
        !           654:
        !           655:        /* Time to look at the argument. If it is empty, that is an error. */
        !           656:        if (*arg == '\0')
        !           657:                goto not_found;
        !           658:
        !           659:        /* Find the separating colon. If none, assume the current session. */
        !           660:        winptr = strchr(arg, ':');
        !           661:        if (winptr == NULL)
        !           662:                winptr = xstrdup(arg);
        !           663:        else {
        !           664:                winptr++;       /* skip : */
        !           665:                sessptr = xstrdup(arg);
        !           666:                *strchr(sessptr, ':') = '\0';
        !           667:        }
        !           668:
        !           669:        log_debug("%s: winptr=%s, sessptr=%s", __func__, winptr, sessptr);
        !           670:
        !           671:        /* Try to lookup the session if present. */
        !           672:        if (sessptr != NULL && *sessptr != '\0') {
        !           673:                if  ((s = cmd_lookup_session(sessptr, &ambiguous)) == NULL)
        !           674:                        goto no_session;
        !           675:        }
1.1       nicm      676:        if (sp != NULL)
                    677:                *sp = s;
                    678:
1.5     ! nicm      679:        /*
        !           680:         * Then work out the window. No : means "no window" (-1), an empty
        !           681:         * string is the current window, otherwise try to look it up in the
        !           682:         * session.
        !           683:         */
        !           684:        if (winptr == NULL)
        !           685:                idx = -1;
        !           686:        else if (*winptr == '\0')
        !           687:                idx = s->curw->idx;
        !           688:        else if ((wl = cmd_lookup_window(s, winptr, &ambiguous)) == NULL) {
        !           689:                if (ambiguous)
        !           690:                        goto not_found;
        !           691:                /* Don't care it doesn't exist if this is a valid index. */
        !           692:                idx = strtonum(winptr, 0, INT_MAX, &errstr);
        !           693:                if (errstr != NULL)  {
        !           694:                        ctx->error(ctx, "index %s: %s", errstr, winptr);
        !           695:                        idx = -2;
        !           696:                }
        !           697:        } else
        !           698:                idx = wl->idx;
        !           699:
        !           700:        if (sessptr != NULL)
        !           701:                xfree(sessptr);
        !           702:        return (idx);
        !           703:
        !           704: no_session:
        !           705:        if (ambiguous)
        !           706:                ctx->error(ctx, "multiple sessions: %s", sessptr);
        !           707:        else
        !           708:                ctx->error(ctx, "session not found: %s", sessptr);
        !           709:        if (sessptr != NULL)
        !           710:                xfree(sessptr);
        !           711:        return (-2);
        !           712:
        !           713: not_found:
        !           714:        if (ambiguous)
        !           715:                ctx->error(ctx, "multiple windows: %s", arg);
1.1       nicm      716:        else
1.5     ! nicm      717:                ctx->error(ctx, "window not found: %s", arg);
        !           718:        if (sessptr != NULL)
        !           719:                xfree(sessptr);
        !           720:        return (-2);
1.1       nicm      721: }