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

1.11    ! nicm        1: /* $OpenBSD: cmd.c,v 1.10 2009/07/26 12:58:44 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.11    ! nicm      106: struct session *cmd_newest_session(struct sessions *);
1.5       nicm      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
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:
                    300:        if (ctx->cursession != NULL)
                    301:                return (ctx->cursession);
                    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.1       nicm      773: }