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

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