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

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