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

1.42    ! nicm        1: /* $OpenBSD: cmd.c,v 1.41 2010/06/21 01:46:36 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,
1.34      nicm       34:        &cmd_capture_pane_entry,
1.42    ! nicm       35:        &cmd_choose_buffer_entry,
1.15      nicm       36:        &cmd_choose_client_entry,
1.1       nicm       37:        &cmd_choose_session_entry,
                     38:        &cmd_choose_window_entry,
                     39:        &cmd_clear_history_entry,
                     40:        &cmd_clock_mode_entry,
                     41:        &cmd_command_prompt_entry,
                     42:        &cmd_confirm_before_entry,
                     43:        &cmd_copy_buffer_entry,
                     44:        &cmd_copy_mode_entry,
                     45:        &cmd_delete_buffer_entry,
                     46:        &cmd_detach_client_entry,
1.7       nicm       47:        &cmd_display_message_entry,
1.16      nicm       48:        &cmd_display_panes_entry,
1.1       nicm       49:        &cmd_find_window_entry,
                     50:        &cmd_has_session_entry,
1.4       nicm       51:        &cmd_if_shell_entry,
1.37      nicm       52:        &cmd_join_pane_entry,
1.1       nicm       53:        &cmd_kill_pane_entry,
                     54:        &cmd_kill_server_entry,
                     55:        &cmd_kill_session_entry,
                     56:        &cmd_kill_window_entry,
                     57:        &cmd_last_window_entry,
                     58:        &cmd_link_window_entry,
                     59:        &cmd_list_buffers_entry,
                     60:        &cmd_list_clients_entry,
                     61:        &cmd_list_commands_entry,
                     62:        &cmd_list_keys_entry,
1.23      nicm       63:        &cmd_list_panes_entry,
1.1       nicm       64:        &cmd_list_sessions_entry,
                     65:        &cmd_list_windows_entry,
                     66:        &cmd_load_buffer_entry,
1.19      nicm       67:        &cmd_lock_client_entry,
1.1       nicm       68:        &cmd_lock_server_entry,
1.19      nicm       69:        &cmd_lock_session_entry,
1.1       nicm       70:        &cmd_move_window_entry,
                     71:        &cmd_new_session_entry,
                     72:        &cmd_new_window_entry,
                     73:        &cmd_next_layout_entry,
                     74:        &cmd_next_window_entry,
                     75:        &cmd_paste_buffer_entry,
1.24      nicm       76:        &cmd_pipe_pane_entry,
1.1       nicm       77:        &cmd_previous_layout_entry,
                     78:        &cmd_previous_window_entry,
                     79:        &cmd_refresh_client_entry,
                     80:        &cmd_rename_session_entry,
                     81:        &cmd_rename_window_entry,
                     82:        &cmd_resize_pane_entry,
                     83:        &cmd_respawn_window_entry,
                     84:        &cmd_rotate_window_entry,
1.17      nicm       85:        &cmd_run_shell_entry,
1.1       nicm       86:        &cmd_save_buffer_entry,
                     87:        &cmd_select_layout_entry,
                     88:        &cmd_select_pane_entry,
                     89:        &cmd_select_window_entry,
                     90:        &cmd_send_keys_entry,
                     91:        &cmd_send_prefix_entry,
                     92:        &cmd_server_info_entry,
                     93:        &cmd_set_buffer_entry,
1.13      nicm       94:        &cmd_set_environment_entry,
1.1       nicm       95:        &cmd_set_option_entry,
                     96:        &cmd_set_window_option_entry,
                     97:        &cmd_show_buffer_entry,
1.13      nicm       98:        &cmd_show_environment_entry,
1.32      nicm       99:        &cmd_show_messages_entry,
1.1       nicm      100:        &cmd_show_options_entry,
                    101:        &cmd_show_window_options_entry,
                    102:        &cmd_source_file_entry,
                    103:        &cmd_split_window_entry,
                    104:        &cmd_start_server_entry,
                    105:        &cmd_suspend_client_entry,
                    106:        &cmd_swap_pane_entry,
                    107:        &cmd_swap_window_entry,
                    108:        &cmd_switch_client_entry,
                    109:        &cmd_unbind_key_entry,
                    110:        &cmd_unlink_window_entry,
                    111:        NULL
                    112: };
                    113:
1.31      nicm      114: struct session *cmd_choose_session(struct sessions *);
                    115: struct client  *cmd_choose_client(struct clients *);
1.5       nicm      116: struct client  *cmd_lookup_client(const char *);
                    117: struct session *cmd_lookup_session(const char *, int *);
                    118: struct winlink *cmd_lookup_window(struct session *, const char *, int *);
1.8       nicm      119: int             cmd_lookup_index(struct session *, const char *, int *);
1.5       nicm      120:
1.10      nicm      121: int
                    122: cmd_pack_argv(int argc, char **argv, char *buf, size_t len)
                    123: {
                    124:        size_t  arglen;
                    125:        int     i;
                    126:
                    127:        *buf = '\0';
                    128:        for (i = 0; i < argc; i++) {
                    129:                if (strlcpy(buf, argv[i], len) >= len)
                    130:                        return (-1);
                    131:                arglen = strlen(argv[i]) + 1;
                    132:                buf += arglen;
                    133:                len -= arglen;
                    134:        }
                    135:
                    136:        return (0);
                    137: }
                    138:
                    139: int
                    140: cmd_unpack_argv(char *buf, size_t len, int argc, char ***argv)
                    141: {
                    142:        int     i;
                    143:        size_t  arglen;
                    144:
                    145:        if (argc == 0)
                    146:                return (0);
                    147:        *argv = xcalloc(argc, sizeof **argv);
                    148:
                    149:        buf[len - 1] = '\0';
                    150:        for (i = 0; i < argc; i++) {
                    151:                if (len == 0) {
                    152:                        cmd_free_argv(argc, *argv);
                    153:                        return (-1);
                    154:                }
                    155:
                    156:                arglen = strlen(buf) + 1;
                    157:                (*argv)[i] = xstrdup(buf);
                    158:                buf += arglen;
                    159:                len -= arglen;
                    160:        }
                    161:
                    162:        return (0);
                    163: }
                    164:
                    165: void
                    166: cmd_free_argv(int argc, char **argv)
                    167: {
                    168:        int     i;
                    169:
                    170:        if (argc == 0)
1.35      nicm      171:                return;
1.10      nicm      172:        for (i = 0; i < argc; i++) {
                    173:                if (argv[i] != NULL)
                    174:                        xfree(argv[i]);
                    175:        }
                    176:        xfree(argv);
                    177: }
                    178:
1.1       nicm      179: struct cmd *
                    180: cmd_parse(int argc, char **argv, char **cause)
                    181: {
                    182:        const struct cmd_entry **entryp, *entry;
1.27      deraadt   183:        struct cmd              *cmd;
1.1       nicm      184:        char                     s[BUFSIZ];
1.3       nicm      185:        int                      opt, ambiguous = 0;
1.1       nicm      186:
                    187:        *cause = NULL;
1.2       nicm      188:        if (argc == 0) {
                    189:                xasprintf(cause, "no command");
1.1       nicm      190:                return (NULL);
1.2       nicm      191:        }
1.1       nicm      192:
                    193:        entry = NULL;
                    194:        for (entryp = cmd_table; *entryp != NULL; entryp++) {
                    195:                if ((*entryp)->alias != NULL &&
                    196:                    strcmp((*entryp)->alias, argv[0]) == 0) {
1.3       nicm      197:                        ambiguous = 0;
1.1       nicm      198:                        entry = *entryp;
                    199:                        break;
                    200:                }
                    201:
                    202:                if (strncmp((*entryp)->name, argv[0], strlen(argv[0])) != 0)
                    203:                        continue;
                    204:                if (entry != NULL)
1.3       nicm      205:                        ambiguous = 1;
1.1       nicm      206:                entry = *entryp;
                    207:
                    208:                /* Bail now if an exact match. */
                    209:                if (strcmp(entry->name, argv[0]) == 0)
                    210:                        break;
                    211:        }
1.3       nicm      212:        if (ambiguous)
                    213:                goto ambiguous;
1.1       nicm      214:        if (entry == NULL) {
                    215:                xasprintf(cause, "unknown command: %s", argv[0]);
                    216:                return (NULL);
                    217:        }
                    218:
                    219:        optreset = 1;
                    220:        optind = 1;
                    221:        if (entry->parse == NULL) {
                    222:                while ((opt = getopt(argc, argv, "")) != -1) {
                    223:                        switch (opt) {
                    224:                        default:
                    225:                                goto usage;
                    226:                        }
                    227:                }
                    228:                argc -= optind;
                    229:                argv += optind;
                    230:                if (argc != 0)
                    231:                        goto usage;
                    232:        }
                    233:
                    234:        cmd = xmalloc(sizeof *cmd);
                    235:        cmd->entry = entry;
                    236:        cmd->data = NULL;
                    237:        if (entry->parse != NULL) {
                    238:                if (entry->parse(cmd, argc, argv, cause) != 0) {
                    239:                        xfree(cmd);
                    240:                        return (NULL);
                    241:                }
                    242:        }
                    243:        return (cmd);
                    244:
                    245: ambiguous:
                    246:        *s = '\0';
                    247:        for (entryp = cmd_table; *entryp != NULL; entryp++) {
                    248:                if (strncmp((*entryp)->name, argv[0], strlen(argv[0])) != 0)
                    249:                        continue;
                    250:                if (strlcat(s, (*entryp)->name, sizeof s) >= sizeof s)
                    251:                        break;
                    252:                if (strlcat(s, ", ", sizeof s) >= sizeof s)
                    253:                        break;
                    254:        }
                    255:        s[strlen(s) - 2] = '\0';
                    256:        xasprintf(cause, "ambiguous command: %s, could be: %s", argv[0], s);
                    257:        return (NULL);
                    258:
                    259: usage:
                    260:        xasprintf(cause, "usage: %s %s", entry->name, entry->usage);
                    261:        return (NULL);
                    262: }
                    263:
                    264: int
                    265: cmd_exec(struct cmd *cmd, struct cmd_ctx *ctx)
                    266: {
                    267:        return (cmd->entry->exec(cmd, ctx));
                    268: }
                    269:
                    270: void
                    271: cmd_free(struct cmd *cmd)
                    272: {
                    273:        if (cmd->data != NULL && cmd->entry->free != NULL)
                    274:                cmd->entry->free(cmd);
                    275:        xfree(cmd);
                    276: }
                    277:
                    278: size_t
                    279: cmd_print(struct cmd *cmd, char *buf, size_t len)
                    280: {
1.33      nicm      281:        if (cmd->entry->print == NULL)
1.1       nicm      282:                return (xsnprintf(buf, len, "%s", cmd->entry->name));
                    283:        return (cmd->entry->print(cmd, buf, len));
                    284: }
                    285:
1.5       nicm      286: /*
                    287:  * Figure out the current session. Use: 1) the current session, if the command
1.31      nicm      288:  * context has one; 2) the most recently used session containing the pty of the
                    289:  * calling client, if any; 3) the session specified in the TMUX variable from
                    290:  * the environment (as passed from the client); 4) the most recently used
                    291:  * session from all sessions.
1.5       nicm      292:  */
1.1       nicm      293: struct session *
                    294: cmd_current_session(struct cmd_ctx *ctx)
                    295: {
                    296:        struct msg_command_data *data = ctx->msgdata;
1.11      nicm      297:        struct client           *c = ctx->cmdclient;
1.5       nicm      298:        struct session          *s;
1.11      nicm      299:        struct sessions          ss;
                    300:        struct winlink          *wl;
                    301:        struct window_pane      *wp;
                    302:        u_int                    i;
                    303:        int                      found;
1.1       nicm      304:
1.14      nicm      305:        if (ctx->curclient != NULL && ctx->curclient->session != NULL)
                    306:                return (ctx->curclient->session);
1.1       nicm      307:
1.11      nicm      308:        /*
                    309:         * If the name of the calling client's pty is know, build a list of the
                    310:         * sessions that contain it and if any choose either the first or the
                    311:         * newest.
                    312:         */
                    313:        if (c != NULL && c->tty.path != NULL) {
                    314:                ARRAY_INIT(&ss);
                    315:                for (i = 0; i < ARRAY_LENGTH(&sessions); i++) {
                    316:                        if ((s = ARRAY_ITEM(&sessions, i)) == NULL)
                    317:                                continue;
                    318:                        found = 0;
                    319:                        RB_FOREACH(wl, winlinks, &s->windows) {
                    320:                                TAILQ_FOREACH(wp, &wl->window->panes, entry) {
                    321:                                        if (strcmp(wp->tty, c->tty.path) == 0) {
                    322:                                                found = 1;
                    323:                                                break;
                    324:                                        }
                    325:                                }
                    326:                                if (found)
                    327:                                        break;
                    328:                        }
                    329:                        if (found)
                    330:                                ARRAY_ADD(&ss, s);
                    331:                }
                    332:
1.31      nicm      333:                s = cmd_choose_session(&ss);
1.11      nicm      334:                ARRAY_FREE(&ss);
                    335:                if (s != NULL)
                    336:                        return (s);
                    337:        }
                    338:
                    339:        /* Use the session from the TMUX environment variable. */
1.1       nicm      340:        if (data != NULL && data->pid != -1) {
1.5       nicm      341:                if (data->pid != getpid())
1.1       nicm      342:                        return (NULL);
1.5       nicm      343:                if (data->idx > ARRAY_LENGTH(&sessions))
1.1       nicm      344:                        return (NULL);
1.5       nicm      345:                if ((s = ARRAY_ITEM(&sessions, data->idx)) == NULL)
1.1       nicm      346:                        return (NULL);
                    347:                return (s);
                    348:        }
                    349:
1.31      nicm      350:        return (cmd_choose_session(&sessions));
1.5       nicm      351: }
                    352:
1.31      nicm      353: /* Find the most recently used session from a list. */
1.5       nicm      354: struct session *
1.31      nicm      355: cmd_choose_session(struct sessions *ss)
1.5       nicm      356: {
1.31      nicm      357:        struct session  *s, *sbest;
1.5       nicm      358:        struct timeval  *tv = NULL;
                    359:        u_int            i;
                    360:
1.31      nicm      361:        sbest = NULL;
1.11      nicm      362:        for (i = 0; i < ARRAY_LENGTH(ss); i++) {
                    363:                if ((s = ARRAY_ITEM(ss, i)) == NULL)
1.5       nicm      364:                        continue;
                    365:
1.31      nicm      366:                if (tv == NULL || timercmp(&s->activity_time, tv, >)) {
                    367:                        sbest = s;
                    368:                        tv = &s->activity_time;
1.1       nicm      369:                }
                    370:        }
1.5       nicm      371:
1.31      nicm      372:        return (sbest);
1.1       nicm      373: }
                    374:
1.30      nicm      375: /*
                    376:  * Find the current client. First try the current client if set, then pick the
1.31      nicm      377:  * most recently used of the clients attached to the current session if any,
                    378:  * then of all clients.
1.30      nicm      379:  */
                    380: struct client *
                    381: cmd_current_client(struct cmd_ctx *ctx)
                    382: {
                    383:        struct session          *s;
                    384:        struct client           *c;
                    385:        struct clients           cc;
                    386:        u_int                    i;
                    387:
                    388:        if (ctx->curclient != NULL)
                    389:                return (ctx->curclient);
                    390:
                    391:        /*
                    392:         * No current client set. Find the current session and return the
                    393:         * newest of its clients.
                    394:         */
                    395:        s = cmd_current_session(ctx);
                    396:        if (s != NULL && !(s->flags & SESSION_UNATTACHED)) {
                    397:                ARRAY_INIT(&cc);
                    398:                for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
                    399:                        if ((c = ARRAY_ITEM(&clients, i)) == NULL)
                    400:                                continue;
                    401:                        if (s == c->session)
                    402:                                ARRAY_ADD(&cc, c);
                    403:                }
                    404:
1.31      nicm      405:                c = cmd_choose_client(&cc);
1.30      nicm      406:                ARRAY_FREE(&cc);
                    407:                if (c != NULL)
                    408:                        return (c);
                    409:        }
                    410:
1.31      nicm      411:        return (cmd_choose_client(&clients));
1.30      nicm      412: }
                    413:
1.31      nicm      414: /* Choose the most recently used client from a list. */
1.20      nicm      415: struct client *
1.31      nicm      416: cmd_choose_client(struct clients *cc)
1.20      nicm      417: {
1.31      nicm      418:        struct client   *c, *cbest;
1.20      nicm      419:        struct timeval  *tv = NULL;
                    420:        u_int            i;
                    421:
1.31      nicm      422:        cbest = NULL;
1.30      nicm      423:        for (i = 0; i < ARRAY_LENGTH(cc); i++) {
                    424:                if ((c = ARRAY_ITEM(cc, i)) == NULL)
1.20      nicm      425:                        continue;
                    426:                if (c->session == NULL)
                    427:                        continue;
                    428:
1.31      nicm      429:                if (tv == NULL || timercmp(&c->activity_time, tv, >)) {
                    430:                        cbest = c;
                    431:                        tv = &c->activity_time;
1.20      nicm      432:                }
                    433:        }
                    434:
1.31      nicm      435:        return (cbest);
1.20      nicm      436: }
                    437:
1.5       nicm      438: /* Find the target client or report an error and return NULL. */
1.1       nicm      439: struct client *
                    440: cmd_find_client(struct cmd_ctx *ctx, const char *arg)
                    441: {
1.30      nicm      442:        struct client   *c;
1.5       nicm      443:        char            *tmparg;
                    444:        size_t           arglen;
1.1       nicm      445:
1.5       nicm      446:        /* A NULL argument means the current client. */
1.30      nicm      447:        if (arg == NULL)
                    448:                return (cmd_current_client(ctx));
1.5       nicm      449:        tmparg = xstrdup(arg);
                    450:
                    451:        /* Trim a single trailing colon if any. */
                    452:        arglen = strlen(tmparg);
                    453:        if (arglen != 0 && tmparg[arglen - 1] == ':')
                    454:                tmparg[arglen - 1] = '\0';
                    455:
                    456:        /* Find the client, if any. */
                    457:        c = cmd_lookup_client(tmparg);
                    458:
                    459:        /* If no client found, report an error. */
                    460:        if (c == NULL)
                    461:                ctx->error(ctx, "client not found: %s", tmparg);
                    462:
                    463:        xfree(tmparg);
                    464:        return (c);
                    465: }
                    466:
                    467: /*
                    468:  * Lookup a client by device path. Either of a full match and a match without a
                    469:  * leading _PATH_DEV ("/dev/") is accepted.
                    470:  */
                    471: struct client *
                    472: cmd_lookup_client(const char *name)
                    473: {
                    474:        struct client   *c;
                    475:        const char      *path;
                    476:        u_int            i;
                    477:
                    478:        for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
1.25      nicm      479:                c = ARRAY_ITEM(&clients, i);
                    480:                if (c == NULL || c->session == NULL)
1.5       nicm      481:                        continue;
                    482:                path = c->tty.path;
                    483:
                    484:                /* Check for exact matches. */
                    485:                if (strcmp(name, path) == 0)
                    486:                        return (c);
                    487:
                    488:                /* Check without leading /dev if present. */
                    489:                if (strncmp(path, _PATH_DEV, (sizeof _PATH_DEV) - 1) != 0)
                    490:                        continue;
                    491:                if (strcmp(name, path + (sizeof _PATH_DEV) - 1) == 0)
                    492:                        return (c);
                    493:        }
                    494:
                    495:        return (NULL);
                    496: }
                    497:
                    498: /* Lookup a session by name. If no session is found, NULL is returned. */
                    499: struct session *
                    500: cmd_lookup_session(const char *name, int *ambiguous)
                    501: {
                    502:        struct session  *s, *sfound;
                    503:        u_int            i;
                    504:
                    505:        *ambiguous = 0;
                    506:
                    507:        /*
1.28      nicm      508:         * Look for matches. First look for exact matches - session names must
                    509:         * be unique so an exact match can't be ambigious and can just be
                    510:         * returned.
1.5       nicm      511:         */
1.35      nicm      512:        for (i = 0; i < ARRAY_LENGTH(&sessions); i++) {
1.5       nicm      513:                if ((s = ARRAY_ITEM(&sessions, i)) == NULL)
                    514:                        continue;
                    515:                if (strcmp(name, s->name) == 0)
                    516:                        return (s);
1.28      nicm      517:        }
                    518:
                    519:        /*
                    520:         * Otherwise look for partial matches, returning early if it is found to
                    521:         * be ambiguous.
                    522:         */
                    523:        sfound = NULL;
1.35      nicm      524:        for (i = 0; i < ARRAY_LENGTH(&sessions); i++) {
1.28      nicm      525:                if ((s = ARRAY_ITEM(&sessions, i)) == NULL)
                    526:                        continue;
1.5       nicm      527:                if (strncmp(name, s->name, strlen(name)) == 0 ||
                    528:                    fnmatch(name, s->name, 0) == 0) {
                    529:                        if (sfound != NULL) {
                    530:                                *ambiguous = 1;
                    531:                                return (NULL);
                    532:                        }
                    533:                        sfound = s;
                    534:                }
                    535:        }
1.35      nicm      536:        return (sfound);
1.5       nicm      537: }
                    538:
                    539: /*
1.8       nicm      540:  * Lookup a window or return -1 if not found or ambigious. First try as an
                    541:  * index and if invalid, use fnmatch or leading prefix. Return NULL but fill in
1.41      nicm      542:  * idx if the window index is a valid number but there is no window with that
1.8       nicm      543:  * index.
1.5       nicm      544:  */
                    545: struct winlink *
                    546: cmd_lookup_window(struct session *s, const char *name, int *ambiguous)
                    547: {
                    548:        struct winlink  *wl, *wlfound;
                    549:        const char      *errstr;
                    550:        u_int            idx;
                    551:
                    552:        *ambiguous = 0;
                    553:
                    554:        /* First see if this is a valid window index in this session. */
                    555:        idx = strtonum(name, 0, INT_MAX, &errstr);
                    556:        if (errstr == NULL) {
                    557:                if ((wl = winlink_find_by_index(&s->windows, idx)) != NULL)
                    558:                        return (wl);
                    559:        }
1.35      nicm      560:
1.5       nicm      561:        /* Look for exact matches, error if more than one. */
                    562:        wlfound = NULL;
                    563:        RB_FOREACH(wl, winlinks, &s->windows) {
1.8       nicm      564:                if (strcmp(name, wl->window->name) == 0) {
1.5       nicm      565:                        if (wlfound != NULL) {
                    566:                                *ambiguous = 1;
                    567:                                return (NULL);
                    568:                        }
                    569:                        wlfound = wl;
1.1       nicm      570:                }
                    571:        }
1.5       nicm      572:        if (wlfound != NULL)
                    573:                return (wlfound);
                    574:
                    575:        /* Now look for pattern matches, again error if multiple. */
                    576:        wlfound = NULL;
                    577:        RB_FOREACH(wl, winlinks, &s->windows) {
1.8       nicm      578:                if (strncmp(name, wl->window->name, strlen(name)) == 0 ||
                    579:                    fnmatch(name, wl->window->name, 0) == 0) {
1.5       nicm      580:                        if (wlfound != NULL) {
                    581:                                *ambiguous = 1;
                    582:                                return (NULL);
                    583:                        }
                    584:                        wlfound = wl;
                    585:                }
1.35      nicm      586:        }
1.5       nicm      587:        if (wlfound != NULL)
                    588:                return (wlfound);
                    589:
                    590:        return (NULL);
1.1       nicm      591: }
                    592:
1.8       nicm      593: /*
                    594:  * Find a window index - if the window doesn't exist, check if it is a
                    595:  * potential index and return it anyway.
                    596:  */
                    597: int
                    598: cmd_lookup_index(struct session *s, const char *name, int *ambiguous)
                    599: {
                    600:        struct winlink  *wl;
                    601:        const char      *errstr;
                    602:        u_int            idx;
                    603:
                    604:        if ((wl = cmd_lookup_window(s, name, ambiguous)) != NULL)
                    605:                return (wl->idx);
                    606:        if (*ambiguous)
                    607:                return (-1);
                    608:
                    609:        idx = strtonum(name, 0, INT_MAX, &errstr);
                    610:        if (errstr == NULL)
                    611:                return (idx);
                    612:
                    613:        return (-1);
                    614: }
                    615:
1.5       nicm      616: /* Find the target session or report an error and return NULL. */
1.1       nicm      617: struct session *
                    618: cmd_find_session(struct cmd_ctx *ctx, const char *arg)
                    619: {
                    620:        struct session  *s;
1.5       nicm      621:        struct client   *c;
                    622:        char            *tmparg;
                    623:        size_t           arglen;
                    624:        int              ambiguous;
1.1       nicm      625:
1.5       nicm      626:        /* A NULL argument means the current session. */
1.1       nicm      627:        if (arg == NULL)
1.5       nicm      628:                return (cmd_current_session(ctx));
                    629:        tmparg = xstrdup(arg);
                    630:
                    631:        /* Trim a single trailing colon if any. */
                    632:        arglen = strlen(tmparg);
                    633:        if (arglen != 0 && tmparg[arglen - 1] == ':')
                    634:                tmparg[arglen - 1] = '\0';
                    635:
                    636:        /* Find the session, if any. */
                    637:        s = cmd_lookup_session(tmparg, &ambiguous);
                    638:
                    639:        /* If it doesn't, try to match it as a client. */
                    640:        if (s == NULL && (c = cmd_lookup_client(tmparg)) != NULL)
                    641:                s = c->session;
                    642:
                    643:        /* If no session found, report an error. */
                    644:        if (s == NULL) {
                    645:                if (ambiguous)
                    646:                        ctx->error(ctx, "more than one session: %s", tmparg);
                    647:                else
                    648:                        ctx->error(ctx, "session not found: %s", tmparg);
1.1       nicm      649:        }
1.5       nicm      650:
                    651:        xfree(tmparg);
1.1       nicm      652:        return (s);
                    653: }
                    654:
1.5       nicm      655: /* Find the target session and window or report an error and return NULL. */
1.1       nicm      656: struct winlink *
                    657: cmd_find_window(struct cmd_ctx *ctx, const char *arg, struct session **sp)
                    658: {
                    659:        struct session  *s;
                    660:        struct winlink  *wl;
1.5       nicm      661:        const char      *winptr;
                    662:        char            *sessptr = NULL;
                    663:        int              ambiguous = 0;
1.41      nicm      664:        int              n = 1;
1.5       nicm      665:
                    666:        /*
                    667:         * Find the current session. There must always be a current session, if
                    668:         * it can't be found, report an error.
                    669:         */
                    670:        if ((s = cmd_current_session(ctx)) == NULL) {
                    671:                ctx->error(ctx, "can't establish current session");
                    672:                return (NULL);
                    673:        }
                    674:
                    675:        /* A NULL argument means the current session and window. */
                    676:        if (arg == NULL) {
                    677:                if (sp != NULL)
                    678:                        *sp = s;
                    679:                return (s->curw);
                    680:        }
                    681:
                    682:        /* Time to look at the argument. If it is empty, that is an error. */
                    683:        if (*arg == '\0')
                    684:                goto not_found;
                    685:
1.8       nicm      686:        /* Find the separating colon and split into window and session. */
1.5       nicm      687:        winptr = strchr(arg, ':');
                    688:        if (winptr == NULL)
1.8       nicm      689:                goto no_colon;
                    690:        winptr++;       /* skip : */
                    691:        sessptr = xstrdup(arg);
                    692:        *strchr(sessptr, ':') = '\0';
1.5       nicm      693:
                    694:        /* Try to lookup the session if present. */
1.8       nicm      695:        if (*sessptr != '\0') {
                    696:                if ((s = cmd_lookup_session(sessptr, &ambiguous)) == NULL)
1.5       nicm      697:                        goto no_session;
                    698:        }
                    699:        if (sp != NULL)
                    700:                *sp = s;
                    701:
                    702:        /*
                    703:         * Then work out the window. An empty string is the current window,
1.38      nicm      704:         * otherwise try special cases then to look it up in the session.
1.5       nicm      705:         */
1.8       nicm      706:        if (*winptr == '\0')
1.5       nicm      707:                wl = s->curw;
1.38      nicm      708:        else if (winptr[0] == '!' && winptr[1] == '\0')
                    709:                wl = TAILQ_FIRST(&s->lastw);
1.41      nicm      710:        else if (winptr[0] == '+' || winptr[0] == '-') {
                    711:                if (winptr[1] != '\0')
                    712:                        n = strtonum(winptr + 1, 1, INT_MAX, NULL);
                    713:                if (n == 0)
                    714:                        wl = cmd_lookup_window(s, winptr, &ambiguous);
                    715:                else {
                    716:                        if (winptr[0] == '+')
                    717:                                wl = winlink_next_by_number(s->curw, n);
                    718:                        else
                    719:                                wl = winlink_previous_by_number(s->curw, n);
                    720:                        /* Search by name before giving up. */
                    721:                        if (wl == NULL)
                    722:                                wl = cmd_lookup_window(s, winptr, &ambiguous);
                    723:                }
                    724:        } else
1.38      nicm      725:                wl = cmd_lookup_window(s, winptr, &ambiguous);
                    726:        if (wl == NULL)
1.5       nicm      727:                goto not_found;
1.35      nicm      728:
1.5       nicm      729:        if (sessptr != NULL)
                    730:                xfree(sessptr);
                    731:        return (wl);
                    732:
1.8       nicm      733: no_colon:
1.38      nicm      734:        /*
                    735:         * No colon in the string, first try special cases, then as a window
                    736:         * and lastly as a session.
                    737:         */
                    738:        if (arg[0] == '!' && arg[1] == '\0') {
                    739:                if ((wl = TAILQ_FIRST(&s->lastw)) == NULL)
                    740:                        goto not_found;
1.41      nicm      741:        } else if (arg[0] == '+' || arg[0] == '-') {
                    742:                if (arg[1] != '\0')
                    743:                        n = strtonum(arg + 1, 1, INT_MAX, NULL);
                    744:                if (n == 0)
                    745:                        wl = cmd_lookup_window(s, arg, &ambiguous);
                    746:                else {
                    747:                        if (arg[0] == '+')
                    748:                                wl = winlink_next_by_number(s->curw, n);
                    749:                        else
                    750:                                wl = winlink_previous_by_number(s->curw, n);
                    751:                        /* Search by name before giving up. */
                    752:                        if (wl == NULL)
                    753:                                wl = cmd_lookup_window(s, arg, &ambiguous);
                    754:                }
                    755:                if (wl == NULL)
                    756:                        goto lookup_session;
                    757:        } else if ((wl = cmd_lookup_window(s, arg, &ambiguous)) == NULL)
                    758:                goto lookup_session;
1.8       nicm      759:
                    760:        if (sp != NULL)
                    761:                *sp = s;
                    762:
                    763:        return (wl);
                    764:
1.41      nicm      765: lookup_session:
                    766:        if (ambiguous)
                    767:                goto not_found;
                    768:        if ((s = cmd_lookup_session(arg, &ambiguous)) == NULL)
                    769:                goto no_session;
                    770:
                    771:        if (sp != NULL)
                    772:                *sp = s;
                    773:
                    774:        return (s->curw);
                    775:
1.5       nicm      776: no_session:
                    777:        if (ambiguous)
1.8       nicm      778:                ctx->error(ctx, "multiple sessions: %s", arg);
1.5       nicm      779:        else
1.8       nicm      780:                ctx->error(ctx, "session not found: %s", arg);
1.5       nicm      781:        if (sessptr != NULL)
                    782:                xfree(sessptr);
                    783:        return (NULL);
                    784:
                    785: not_found:
                    786:        if (ambiguous)
                    787:                ctx->error(ctx, "multiple windows: %s", arg);
                    788:        else
                    789:                ctx->error(ctx, "window not found: %s", arg);
                    790:        if (sessptr != NULL)
                    791:                xfree(sessptr);
                    792:        return (NULL);
                    793: }
1.1       nicm      794:
1.5       nicm      795: /*
                    796:  * Find the target session and window index, whether or not it exists in the
                    797:  * session. Return -2 on error or -1 if no window index is specified. This is
1.8       nicm      798:  * used when parsing an argument for a window target that may not exist (for
                    799:  * example if it is going to be created).
1.5       nicm      800:  */
                    801: int
                    802: cmd_find_index(struct cmd_ctx *ctx, const char *arg, struct session **sp)
                    803: {
                    804:        struct session  *s;
1.38      nicm      805:        struct winlink  *wl;
1.8       nicm      806:        const char      *winptr;
1.5       nicm      807:        char            *sessptr = NULL;
                    808:        int              idx, ambiguous = 0;
1.41      nicm      809:        int              n = 1;
1.5       nicm      810:
                    811:        /*
                    812:         * Find the current session. There must always be a current session, if
                    813:         * it can't be found, report an error.
                    814:         */
                    815:        if ((s = cmd_current_session(ctx)) == NULL) {
                    816:                ctx->error(ctx, "can't establish current session");
1.9       nicm      817:                return (-2);
1.1       nicm      818:        }
1.5       nicm      819:
                    820:        /* A NULL argument means the current session and "no window" (-1). */
                    821:        if (arg == NULL) {
                    822:                if (sp != NULL)
                    823:                        *sp = s;
                    824:                return (-1);
                    825:        }
                    826:
                    827:        /* Time to look at the argument. If it is empty, that is an error. */
                    828:        if (*arg == '\0')
                    829:                goto not_found;
                    830:
                    831:        /* Find the separating colon. If none, assume the current session. */
                    832:        winptr = strchr(arg, ':');
                    833:        if (winptr == NULL)
1.8       nicm      834:                goto no_colon;
                    835:        winptr++;       /* skip : */
                    836:        sessptr = xstrdup(arg);
                    837:        *strchr(sessptr, ':') = '\0';
1.5       nicm      838:
                    839:        /* Try to lookup the session if present. */
                    840:        if (sessptr != NULL && *sessptr != '\0') {
1.8       nicm      841:                if ((s = cmd_lookup_session(sessptr, &ambiguous)) == NULL)
1.5       nicm      842:                        goto no_session;
                    843:        }
1.1       nicm      844:        if (sp != NULL)
                    845:                *sp = s;
                    846:
1.5       nicm      847:        /*
1.8       nicm      848:         * Then work out the window. An empty string is a new window otherwise
                    849:         * try to look it up in the session.
1.5       nicm      850:         */
1.8       nicm      851:        if (*winptr == '\0')
1.38      nicm      852:                idx = -1;
                    853:        else if (winptr[0] == '!' && winptr[1] == '\0') {
                    854:                if ((wl = TAILQ_FIRST(&s->lastw)) == NULL)
                    855:                        goto not_found;
                    856:                idx = wl->idx;
1.41      nicm      857:        } else if (winptr[0] == '+' || winptr[0] == '-') {
                    858:                if (winptr[1] != '\0')
                    859:                        n = strtonum(winptr + 1, 1, INT_MAX, NULL);
                    860:                if (winptr[0] == '+' && s->curw->idx == INT_MAX)
                    861:                        idx = cmd_lookup_index(s, winptr, &ambiguous);
                    862:                else if (winptr[0] == '-' && s->curw->idx == 0)
                    863:                        idx = cmd_lookup_index(s, winptr, &ambiguous);
                    864:                else if (n == 0)
                    865:                        idx = cmd_lookup_index(s, winptr, &ambiguous);
                    866:                else if (winptr[0] == '+')
                    867:                        idx = s->curw->idx + n;
                    868:                else
                    869:                        idx = s->curw->idx - n;
                    870:                if (idx < 0)
                    871:                        goto invalid_index;
                    872:        } else if ((idx = cmd_lookup_index(s, winptr, &ambiguous)) == -1)
                    873:                goto invalid_index;
1.35      nicm      874:
1.5       nicm      875:        if (sessptr != NULL)
                    876:                xfree(sessptr);
                    877:        return (idx);
                    878:
1.8       nicm      879: no_colon:
1.38      nicm      880:        /*
                    881:         * No colon in the string, first try special cases, then as a window
                    882:         * and lastly as a session.
                    883:         */
                    884:        if (arg[0] == '!' && arg[1] == '\0') {
                    885:                if ((wl = TAILQ_FIRST(&s->lastw)) == NULL)
                    886:                        goto not_found;
                    887:                idx = wl->idx;
1.41      nicm      888:        } else if (arg[0] == '+' || arg[0] == '-') {
                    889:                if (arg[1] != '\0')
                    890:                        n = strtonum(arg + 1, 1, INT_MAX, NULL);
                    891:                if (arg[0] == '+' && s->curw->idx == INT_MAX)
                    892:                        idx = cmd_lookup_index(s, arg, &ambiguous);
                    893:                else if (arg[0] == '-' && s->curw->idx == 0)
                    894:                        idx = cmd_lookup_index(s, arg, &ambiguous);
                    895:                else if (n == 0)
                    896:                        idx = cmd_lookup_index(s, arg, &ambiguous);
                    897:                else if (arg[0] == '+')
                    898:                        idx = s->curw->idx + n;
                    899:                else
                    900:                        idx = s->curw->idx - n;
                    901:                if (idx < 0)
                    902:                        goto lookup_session;
                    903:        } else if ((idx = cmd_lookup_index(s, arg, &ambiguous)) == -1)
                    904:                goto lookup_session;
1.8       nicm      905:
                    906:        if (sp != NULL)
                    907:                *sp = s;
                    908:
                    909:        return (idx);
                    910:
1.41      nicm      911: lookup_session:
                    912:        if (ambiguous)
                    913:                goto not_found;
                    914:        if ((s = cmd_lookup_session(arg, &ambiguous)) == NULL)
                    915:                goto no_session;
                    916:
                    917:        if (sp != NULL)
                    918:                *sp = s;
                    919:
                    920:        return (-1);
                    921:
1.5       nicm      922: no_session:
                    923:        if (ambiguous)
1.35      nicm      924:                ctx->error(ctx, "multiple sessions: %s", arg);
1.5       nicm      925:        else
1.8       nicm      926:                ctx->error(ctx, "session not found: %s", arg);
1.5       nicm      927:        if (sessptr != NULL)
                    928:                xfree(sessptr);
                    929:        return (-2);
                    930:
1.41      nicm      931: invalid_index:
                    932:        if (ambiguous)
                    933:                goto not_found;
                    934:        ctx->error(ctx, "invalid index: %s", arg);
                    935:
                    936:        if (sessptr != NULL)
                    937:                xfree(sessptr);
                    938:        return (-2);
                    939:
1.5       nicm      940: not_found:
                    941:        if (ambiguous)
                    942:                ctx->error(ctx, "multiple windows: %s", arg);
1.1       nicm      943:        else
1.5       nicm      944:                ctx->error(ctx, "window not found: %s", arg);
                    945:        if (sessptr != NULL)
                    946:                xfree(sessptr);
                    947:        return (-2);
1.12      nicm      948: }
                    949:
                    950: /*
                    951:  * Find the target session, window and pane number or report an error and
                    952:  * return NULL. The pane number is separated from the session:window by a .,
                    953:  * such as mysession:mywindow.0.
                    954:  */
                    955: struct winlink *
                    956: cmd_find_pane(struct cmd_ctx *ctx,
                    957:     const char *arg, struct session **sp, struct window_pane **wpp)
                    958: {
1.36      nicm      959:        struct session          *s;
                    960:        struct winlink          *wl;
                    961:        struct layout_cell      *lc;
                    962:        const char              *period, *errstr;
                    963:        char                    *winptr, *paneptr;
1.41      nicm      964:        u_int                    idx, n = 1;
1.12      nicm      965:
                    966:        /* Get the current session. */
                    967:        if ((s = cmd_current_session(ctx)) == NULL) {
1.27      deraadt   968:                ctx->error(ctx, "can't establish current session");
1.12      nicm      969:                return (NULL);
                    970:        }
                    971:        if (sp != NULL)
                    972:                *sp = s;
                    973:
                    974:        /* A NULL argument means the current session, window and pane. */
                    975:        if (arg == NULL) {
                    976:                *wpp = s->curw->window->active;
                    977:                return (s->curw);
                    978:        }
                    979:
                    980:        /* Look for a separating period. */
                    981:        if ((period = strrchr(arg, '.')) == NULL)
                    982:                goto no_period;
                    983:
                    984:        /* Pull out the window part and parse it. */
                    985:        winptr = xstrdup(arg);
                    986:        winptr[period - arg] = '\0';
                    987:        if (*winptr == '\0')
                    988:                wl = s->curw;
                    989:        else if ((wl = cmd_find_window(ctx, winptr, sp)) == NULL)
                    990:                goto error;
                    991:
                    992:        /* Find the pane section and look it up. */
                    993:        paneptr = winptr + (period - arg) + 1;
                    994:        if (*paneptr == '\0')
                    995:                *wpp = wl->window->active;
1.41      nicm      996:        else if (paneptr[0] == '+' || paneptr[0] == '-') {
                    997:                if (paneptr[1] != '\0')
                    998:                        n = strtonum(paneptr + 1, 1, INT_MAX, NULL);
                    999:                idx = window_pane_index(wl->window, wl->window->active);
                   1000:                if (paneptr[0] == '+' && idx == INT_MAX)
                   1001:                        *wpp = TAILQ_FIRST(&wl->window->panes);
                   1002:                else if (paneptr[0] == '-' && idx == 0)
                   1003:                        *wpp = TAILQ_LAST(&wl->window->panes, window_panes);
                   1004:                else if (n == 0)
                   1005:                        *wpp = wl->window->active;
                   1006:                else if (paneptr[0] == '+')
                   1007:                        *wpp = window_pane_at_index(wl->window, idx + n);
                   1008:                else
                   1009:                        *wpp = window_pane_at_index(wl->window, idx - n);
                   1010:                if (paneptr[0] == '+' && *wpp == NULL)
                   1011:                        *wpp = TAILQ_FIRST(&wl->window->panes);
                   1012:                else if (paneptr[0] == '-' && *wpp == NULL)
                   1013:                        *wpp = TAILQ_LAST(&wl->window->panes, window_panes);
                   1014:                else if (*wpp == NULL)
                   1015:                        goto error;
                   1016:        } else {
1.12      nicm     1017:                idx = strtonum(paneptr, 0, INT_MAX, &errstr);
1.36      nicm     1018:                if (errstr != NULL)
                   1019:                        goto lookup_string;
1.12      nicm     1020:                *wpp = window_pane_at_index(wl->window, idx);
1.36      nicm     1021:                if (*wpp == NULL)
                   1022:                        goto lookup_string;
1.12      nicm     1023:        }
                   1024:
                   1025:        xfree(winptr);
                   1026:        return (wl);
                   1027:
1.36      nicm     1028: lookup_string:
                   1029:        /* Try pane string description. */
1.39      nicm     1030:        if ((lc = layout_find_string(wl->window, paneptr)) == NULL) {
1.36      nicm     1031:                ctx->error(ctx, "can't find pane: %s", paneptr);
                   1032:                goto error;
                   1033:        }
                   1034:        *wpp = lc->wp;
                   1035:
                   1036:        xfree(winptr);
1.39      nicm     1037:        return (wl);
1.36      nicm     1038:
1.12      nicm     1039: no_period:
                   1040:        /* Try as a pane number alone. */
                   1041:        idx = strtonum(arg, 0, INT_MAX, &errstr);
                   1042:        if (errstr != NULL)
                   1043:                goto lookup_window;
                   1044:
                   1045:        /* Try index in the current session and window. */
                   1046:        if ((*wpp = window_pane_at_index(s->curw->window, idx)) == NULL)
                   1047:                goto lookup_window;
1.35      nicm     1048:
1.12      nicm     1049:        return (s->curw);
                   1050:
                   1051: lookup_window:
1.36      nicm     1052:        /* Try pane string description. */
                   1053:        if ((lc = layout_find_string(s->curw->window, arg)) != NULL) {
                   1054:                *wpp = lc->wp;
                   1055:                return (s->curw);
                   1056:        }
                   1057:
1.12      nicm     1058:        /* Try as a window and use the active pane. */
                   1059:        if ((wl = cmd_find_window(ctx, arg, sp)) != NULL)
                   1060:                *wpp = wl->window->active;
                   1061:        return (wl);
1.35      nicm     1062:
1.12      nicm     1063: error:
                   1064:        xfree(winptr);
                   1065:        return (NULL);
1.15      nicm     1066: }
                   1067:
                   1068: /* Replace the first %% or %idx in template by s. */
                   1069: char *
                   1070: cmd_template_replace(char *template, const char *s, int idx)
                   1071: {
                   1072:        char     ch;
                   1073:        char    *buf, *ptr;
                   1074:        int      replaced;
                   1075:        size_t   len;
                   1076:
                   1077:        if (strstr(template, "%") == NULL)
                   1078:                return (xstrdup(template));
                   1079:
                   1080:        buf = xmalloc(1);
                   1081:        *buf = '\0';
                   1082:        len = 0;
                   1083:        replaced = 0;
                   1084:
                   1085:        ptr = template;
                   1086:        while (*ptr != '\0') {
                   1087:                switch (ch = *ptr++) {
                   1088:                case '%':
                   1089:                        if (*ptr < '1' || *ptr > '9' || *ptr - '0' != idx) {
                   1090:                                if (*ptr != '%' || replaced)
                   1091:                                        break;
                   1092:                                replaced = 1;
                   1093:                        }
                   1094:                        ptr++;
                   1095:
                   1096:                        len += strlen(s);
                   1097:                        buf = xrealloc(buf, 1, len + 1);
                   1098:                        strlcat(buf, s, len + 1);
                   1099:                        continue;
                   1100:                }
                   1101:                buf = xrealloc(buf, 1, len + 2);
                   1102:                buf[len++] = ch;
                   1103:                buf[len] = '\0';
                   1104:        }
                   1105:
                   1106:        return (buf);
1.1       nicm     1107: }