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

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