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

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