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

1.38    ! nicm        1: /* $OpenBSD: cmd.c,v 1.37 2010/01/07 20:52:18 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,
1.38    ! nicm      705:         * otherwise try special cases then to look it up in the session.
1.5       nicm      706:         */
1.8       nicm      707:        if (*winptr == '\0')
1.5       nicm      708:                wl = s->curw;
1.38    ! nicm      709:        else if (winptr[0] == '!' && winptr[1] == '\0')
        !           710:                wl = TAILQ_FIRST(&s->lastw);
        !           711:        else if (winptr[0] == '+' && winptr[1] == '\0')
        !           712:                wl = winlink_next(s->curw);
        !           713:        else if (winptr[0] == '-' && winptr[1] == '\0')
        !           714:                wl = winlink_previous(s->curw);
        !           715:        else
        !           716:                wl = cmd_lookup_window(s, winptr, &ambiguous);
        !           717:        if (wl == NULL)
1.5       nicm      718:                goto not_found;
1.35      nicm      719:
1.5       nicm      720:        if (sessptr != NULL)
                    721:                xfree(sessptr);
                    722:        return (wl);
                    723:
1.8       nicm      724: no_colon:
1.38    ! nicm      725:        /*
        !           726:         * No colon in the string, first try special cases, then as a window
        !           727:         * and lastly as a session.
        !           728:         */
        !           729:        if (arg[0] == '!' && arg[1] == '\0') {
        !           730:                if ((wl = TAILQ_FIRST(&s->lastw)) == NULL)
        !           731:                        goto not_found;
        !           732:        } else if (arg[0] == '+' && arg[1] == '\0') {
        !           733:                if ((wl = winlink_next(s->curw)) == NULL)
        !           734:                        goto not_found;
        !           735:        } else if (arg[0] == '-' && arg[1] == '\0') {
        !           736:                if ((wl = winlink_previous(s->curw)) == NULL)
        !           737:                        goto not_found;
        !           738:        } else if ((wl = cmd_lookup_window(s, arg, &ambiguous)) == NULL) {
1.8       nicm      739:                if (ambiguous)
                    740:                        goto not_found;
                    741:                if ((s = cmd_lookup_session(arg, &ambiguous)) == NULL)
                    742:                        goto no_session;
                    743:                wl = s->curw;
                    744:        }
                    745:
                    746:        if (sp != NULL)
                    747:                *sp = s;
                    748:
                    749:        return (wl);
                    750:
1.5       nicm      751: no_session:
                    752:        if (ambiguous)
1.8       nicm      753:                ctx->error(ctx, "multiple sessions: %s", arg);
1.5       nicm      754:        else
1.8       nicm      755:                ctx->error(ctx, "session not found: %s", arg);
1.5       nicm      756:        if (sessptr != NULL)
                    757:                xfree(sessptr);
                    758:        return (NULL);
                    759:
                    760: not_found:
                    761:        if (ambiguous)
                    762:                ctx->error(ctx, "multiple windows: %s", arg);
                    763:        else
                    764:                ctx->error(ctx, "window not found: %s", arg);
                    765:        if (sessptr != NULL)
                    766:                xfree(sessptr);
                    767:        return (NULL);
                    768: }
1.1       nicm      769:
1.5       nicm      770: /*
                    771:  * Find the target session and window index, whether or not it exists in the
                    772:  * session. Return -2 on error or -1 if no window index is specified. This is
1.8       nicm      773:  * used when parsing an argument for a window target that may not exist (for
                    774:  * example if it is going to be created).
1.5       nicm      775:  */
                    776: int
                    777: cmd_find_index(struct cmd_ctx *ctx, const char *arg, struct session **sp)
                    778: {
                    779:        struct session  *s;
1.38    ! nicm      780:        struct winlink  *wl;
1.8       nicm      781:        const char      *winptr;
1.5       nicm      782:        char            *sessptr = NULL;
                    783:        int              idx, ambiguous = 0;
                    784:
                    785:        /*
                    786:         * Find the current session. There must always be a current session, if
                    787:         * it can't be found, report an error.
                    788:         */
                    789:        if ((s = cmd_current_session(ctx)) == NULL) {
                    790:                ctx->error(ctx, "can't establish current session");
1.9       nicm      791:                return (-2);
1.1       nicm      792:        }
1.5       nicm      793:
                    794:        /* A NULL argument means the current session and "no window" (-1). */
                    795:        if (arg == NULL) {
                    796:                if (sp != NULL)
                    797:                        *sp = s;
                    798:                return (-1);
                    799:        }
                    800:
                    801:        /* Time to look at the argument. If it is empty, that is an error. */
                    802:        if (*arg == '\0')
                    803:                goto not_found;
                    804:
                    805:        /* Find the separating colon. If none, assume the current session. */
                    806:        winptr = strchr(arg, ':');
                    807:        if (winptr == NULL)
1.8       nicm      808:                goto no_colon;
                    809:        winptr++;       /* skip : */
                    810:        sessptr = xstrdup(arg);
                    811:        *strchr(sessptr, ':') = '\0';
1.5       nicm      812:
                    813:        /* Try to lookup the session if present. */
                    814:        if (sessptr != NULL && *sessptr != '\0') {
1.8       nicm      815:                if ((s = cmd_lookup_session(sessptr, &ambiguous)) == NULL)
1.5       nicm      816:                        goto no_session;
                    817:        }
1.1       nicm      818:        if (sp != NULL)
                    819:                *sp = s;
                    820:
1.5       nicm      821:        /*
1.8       nicm      822:         * Then work out the window. An empty string is a new window otherwise
                    823:         * try to look it up in the session.
1.5       nicm      824:         */
1.8       nicm      825:        if (*winptr == '\0')
1.38    ! nicm      826:                idx = -1;
        !           827:        else if (winptr[0] == '!' && winptr[1] == '\0') {
        !           828:                if ((wl = TAILQ_FIRST(&s->lastw)) == NULL)
        !           829:                        goto not_found;
        !           830:                idx = wl->idx;
        !           831:        } else if (winptr[0] == '+' && winptr[1] == '\0') {
        !           832:                if (s->curw->idx == INT_MAX)
        !           833:                        goto not_found;
        !           834:                idx = s->curw->idx + 1;
        !           835:        } else if (winptr[0] == '-' && winptr[1] == '\0') {
        !           836:                if (s->curw->idx == 0)
        !           837:                        goto not_found;
        !           838:                idx = s->curw->idx - 1;
        !           839:        } else if ((idx = cmd_lookup_index(s, winptr, &ambiguous)) == -1) {
1.5       nicm      840:                if (ambiguous)
                    841:                        goto not_found;
1.8       nicm      842:                ctx->error(ctx, "invalid index: %s", arg);
                    843:                idx = -2;
                    844:        }
1.35      nicm      845:
1.5       nicm      846:        if (sessptr != NULL)
                    847:                xfree(sessptr);
                    848:        return (idx);
                    849:
1.8       nicm      850: no_colon:
1.38    ! nicm      851:        /*
        !           852:         * No colon in the string, first try special cases, then as a window
        !           853:         * and lastly as a session.
        !           854:         */
        !           855:        if (arg[0] == '!' && arg[1] == '\0') {
        !           856:                if ((wl = TAILQ_FIRST(&s->lastw)) == NULL)
        !           857:                        goto not_found;
        !           858:                idx = wl->idx;
        !           859:        } else if (arg[0] == '+' && arg[1] == '\0') {
        !           860:                if (s->curw->idx == INT_MAX)
        !           861:                        goto not_found;
        !           862:                idx = s->curw->idx + 1;
        !           863:        } else if (arg[0] == '-' && arg[1] == '\0') {
        !           864:                if (s->curw->idx == 0)
        !           865:                        goto not_found;
        !           866:                idx = s->curw->idx - 1;
        !           867:        } else if ((idx = cmd_lookup_index(s, arg, &ambiguous)) == -1) {
1.8       nicm      868:                if (ambiguous)
                    869:                        goto not_found;
                    870:                if ((s = cmd_lookup_session(arg, &ambiguous)) == NULL)
                    871:                        goto no_session;
                    872:                idx = -1;
                    873:        }
                    874:
                    875:        if (sp != NULL)
                    876:                *sp = s;
                    877:
                    878:        return (idx);
                    879:
1.5       nicm      880: no_session:
                    881:        if (ambiguous)
1.35      nicm      882:                ctx->error(ctx, "multiple sessions: %s", arg);
1.5       nicm      883:        else
1.8       nicm      884:                ctx->error(ctx, "session not found: %s", arg);
1.5       nicm      885:        if (sessptr != NULL)
                    886:                xfree(sessptr);
                    887:        return (-2);
                    888:
                    889: not_found:
                    890:        if (ambiguous)
                    891:                ctx->error(ctx, "multiple windows: %s", arg);
1.1       nicm      892:        else
1.5       nicm      893:                ctx->error(ctx, "window not found: %s", arg);
                    894:        if (sessptr != NULL)
                    895:                xfree(sessptr);
                    896:        return (-2);
1.12      nicm      897: }
                    898:
                    899: /*
                    900:  * Find the target session, window and pane number or report an error and
                    901:  * return NULL. The pane number is separated from the session:window by a .,
                    902:  * such as mysession:mywindow.0.
                    903:  */
                    904: struct winlink *
                    905: cmd_find_pane(struct cmd_ctx *ctx,
                    906:     const char *arg, struct session **sp, struct window_pane **wpp)
                    907: {
1.36      nicm      908:        struct session          *s;
                    909:        struct winlink          *wl;
                    910:        struct layout_cell      *lc;
                    911:        const char              *period, *errstr;
                    912:        char                    *winptr, *paneptr;
                    913:        u_int                    idx;
1.12      nicm      914:
                    915:        /* Get the current session. */
                    916:        if ((s = cmd_current_session(ctx)) == NULL) {
1.27      deraadt   917:                ctx->error(ctx, "can't establish current session");
1.12      nicm      918:                return (NULL);
                    919:        }
                    920:        if (sp != NULL)
                    921:                *sp = s;
                    922:
                    923:        /* A NULL argument means the current session, window and pane. */
                    924:        if (arg == NULL) {
                    925:                *wpp = s->curw->window->active;
                    926:                return (s->curw);
                    927:        }
                    928:
                    929:        /* Look for a separating period. */
                    930:        if ((period = strrchr(arg, '.')) == NULL)
                    931:                goto no_period;
                    932:
                    933:        /* Pull out the window part and parse it. */
                    934:        winptr = xstrdup(arg);
                    935:        winptr[period - arg] = '\0';
                    936:        if (*winptr == '\0')
                    937:                wl = s->curw;
                    938:        else if ((wl = cmd_find_window(ctx, winptr, sp)) == NULL)
                    939:                goto error;
                    940:
                    941:        /* Find the pane section and look it up. */
                    942:        paneptr = winptr + (period - arg) + 1;
                    943:        if (*paneptr == '\0')
                    944:                *wpp = wl->window->active;
                    945:        else {
                    946:                idx = strtonum(paneptr, 0, INT_MAX, &errstr);
1.36      nicm      947:                if (errstr != NULL)
                    948:                        goto lookup_string;
1.12      nicm      949:                *wpp = window_pane_at_index(wl->window, idx);
1.36      nicm      950:                if (*wpp == NULL)
                    951:                        goto lookup_string;
1.12      nicm      952:        }
                    953:
                    954:        xfree(winptr);
                    955:        return (wl);
                    956:
1.36      nicm      957: lookup_string:
                    958:        /* Try pane string description. */
                    959:        if ((lc = layout_find_string(s->curw->window, paneptr)) == NULL) {
                    960:                ctx->error(ctx, "can't find pane: %s", paneptr);
                    961:                goto error;
                    962:        }
                    963:        *wpp = lc->wp;
                    964:
                    965:        xfree(winptr);
                    966:        return (s->curw);
                    967:
1.12      nicm      968: no_period:
                    969:        /* Try as a pane number alone. */
                    970:        idx = strtonum(arg, 0, INT_MAX, &errstr);
                    971:        if (errstr != NULL)
                    972:                goto lookup_window;
                    973:
                    974:        /* Try index in the current session and window. */
                    975:        if ((*wpp = window_pane_at_index(s->curw->window, idx)) == NULL)
                    976:                goto lookup_window;
1.35      nicm      977:
1.12      nicm      978:        return (s->curw);
                    979:
                    980: lookup_window:
1.36      nicm      981:        /* Try pane string description. */
                    982:        if ((lc = layout_find_string(s->curw->window, arg)) != NULL) {
                    983:                *wpp = lc->wp;
                    984:                return (s->curw);
                    985:        }
                    986:
1.12      nicm      987:        /* Try as a window and use the active pane. */
                    988:        if ((wl = cmd_find_window(ctx, arg, sp)) != NULL)
                    989:                *wpp = wl->window->active;
                    990:        return (wl);
1.35      nicm      991:
1.12      nicm      992: error:
                    993:        xfree(winptr);
                    994:        return (NULL);
1.15      nicm      995: }
                    996:
                    997: /* Replace the first %% or %idx in template by s. */
                    998: char *
                    999: cmd_template_replace(char *template, const char *s, int idx)
                   1000: {
                   1001:        char     ch;
                   1002:        char    *buf, *ptr;
                   1003:        int      replaced;
                   1004:        size_t   len;
                   1005:
                   1006:        if (strstr(template, "%") == NULL)
                   1007:                return (xstrdup(template));
                   1008:
                   1009:        buf = xmalloc(1);
                   1010:        *buf = '\0';
                   1011:        len = 0;
                   1012:        replaced = 0;
                   1013:
                   1014:        ptr = template;
                   1015:        while (*ptr != '\0') {
                   1016:                switch (ch = *ptr++) {
                   1017:                case '%':
                   1018:                        if (*ptr < '1' || *ptr > '9' || *ptr - '0' != idx) {
                   1019:                                if (*ptr != '%' || replaced)
                   1020:                                        break;
                   1021:                                replaced = 1;
                   1022:                        }
                   1023:                        ptr++;
                   1024:
                   1025:                        len += strlen(s);
                   1026:                        buf = xrealloc(buf, 1, len + 1);
                   1027:                        strlcat(buf, s, len + 1);
                   1028:                        continue;
                   1029:                }
                   1030:                buf = xrealloc(buf, 1, len + 2);
                   1031:                buf[len++] = ch;
                   1032:                buf[len] = '\0';
                   1033:        }
                   1034:
                   1035:        return (buf);
1.1       nicm     1036: }