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

1.67    ! nicm        1: /* $OpenBSD: cmd.c,v 1.66 2012/06/18 09:20:19 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.61      nicm       24: #include <pwd.h>
1.1       nicm       25: #include <stdlib.h>
                     26: #include <string.h>
                     27: #include <unistd.h>
                     28:
                     29: #include "tmux.h"
                     30:
                     31: const struct cmd_entry *cmd_table[] = {
                     32:        &cmd_attach_session_entry,
                     33:        &cmd_bind_key_entry,
                     34:        &cmd_break_pane_entry,
1.34      nicm       35:        &cmd_capture_pane_entry,
1.42      nicm       36:        &cmd_choose_buffer_entry,
1.15      nicm       37:        &cmd_choose_client_entry,
1.1       nicm       38:        &cmd_choose_session_entry,
1.67    ! nicm       39:        &cmd_choose_tree_entry,
1.1       nicm       40:        &cmd_choose_window_entry,
                     41:        &cmd_clear_history_entry,
                     42:        &cmd_clock_mode_entry,
                     43:        &cmd_command_prompt_entry,
                     44:        &cmd_confirm_before_entry,
                     45:        &cmd_copy_mode_entry,
                     46:        &cmd_delete_buffer_entry,
                     47:        &cmd_detach_client_entry,
1.7       nicm       48:        &cmd_display_message_entry,
1.16      nicm       49:        &cmd_display_panes_entry,
1.1       nicm       50:        &cmd_find_window_entry,
                     51:        &cmd_has_session_entry,
1.4       nicm       52:        &cmd_if_shell_entry,
1.37      nicm       53:        &cmd_join_pane_entry,
1.1       nicm       54:        &cmd_kill_pane_entry,
                     55:        &cmd_kill_server_entry,
                     56:        &cmd_kill_session_entry,
                     57:        &cmd_kill_window_entry,
1.45      nicm       58:        &cmd_last_pane_entry,
1.1       nicm       59:        &cmd_last_window_entry,
                     60:        &cmd_link_window_entry,
                     61:        &cmd_list_buffers_entry,
                     62:        &cmd_list_clients_entry,
                     63:        &cmd_list_commands_entry,
                     64:        &cmd_list_keys_entry,
1.23      nicm       65:        &cmd_list_panes_entry,
1.1       nicm       66:        &cmd_list_sessions_entry,
                     67:        &cmd_list_windows_entry,
                     68:        &cmd_load_buffer_entry,
1.19      nicm       69:        &cmd_lock_client_entry,
1.1       nicm       70:        &cmd_lock_server_entry,
1.19      nicm       71:        &cmd_lock_session_entry,
1.63      nicm       72:        &cmd_move_pane_entry,
1.1       nicm       73:        &cmd_move_window_entry,
                     74:        &cmd_new_session_entry,
                     75:        &cmd_new_window_entry,
                     76:        &cmd_next_layout_entry,
                     77:        &cmd_next_window_entry,
                     78:        &cmd_paste_buffer_entry,
1.24      nicm       79:        &cmd_pipe_pane_entry,
1.1       nicm       80:        &cmd_previous_layout_entry,
                     81:        &cmd_previous_window_entry,
                     82:        &cmd_refresh_client_entry,
                     83:        &cmd_rename_session_entry,
                     84:        &cmd_rename_window_entry,
                     85:        &cmd_resize_pane_entry,
1.56      nicm       86:        &cmd_respawn_pane_entry,
1.1       nicm       87:        &cmd_respawn_window_entry,
                     88:        &cmd_rotate_window_entry,
1.17      nicm       89:        &cmd_run_shell_entry,
1.1       nicm       90:        &cmd_save_buffer_entry,
                     91:        &cmd_select_layout_entry,
                     92:        &cmd_select_pane_entry,
                     93:        &cmd_select_window_entry,
                     94:        &cmd_send_keys_entry,
                     95:        &cmd_send_prefix_entry,
                     96:        &cmd_server_info_entry,
                     97:        &cmd_set_buffer_entry,
1.13      nicm       98:        &cmd_set_environment_entry,
1.1       nicm       99:        &cmd_set_option_entry,
                    100:        &cmd_set_window_option_entry,
                    101:        &cmd_show_buffer_entry,
1.13      nicm      102:        &cmd_show_environment_entry,
1.32      nicm      103:        &cmd_show_messages_entry,
1.1       nicm      104:        &cmd_show_options_entry,
                    105:        &cmd_show_window_options_entry,
                    106:        &cmd_source_file_entry,
                    107:        &cmd_split_window_entry,
                    108:        &cmd_start_server_entry,
                    109:        &cmd_suspend_client_entry,
                    110:        &cmd_swap_pane_entry,
                    111:        &cmd_swap_window_entry,
                    112:        &cmd_switch_client_entry,
                    113:        &cmd_unbind_key_entry,
                    114:        &cmd_unlink_window_entry,
                    115:        NULL
                    116: };
                    117:
1.47      nicm      118: struct session *cmd_choose_session_list(struct sessionslist *);
1.52      nicm      119: struct session *cmd_choose_session(int);
1.31      nicm      120: struct client  *cmd_choose_client(struct clients *);
1.5       nicm      121: struct client  *cmd_lookup_client(const char *);
                    122: struct session *cmd_lookup_session(const char *, int *);
                    123: struct winlink *cmd_lookup_window(struct session *, const char *, int *);
1.8       nicm      124: int             cmd_lookup_index(struct session *, const char *, int *);
1.51      nicm      125: struct window_pane *cmd_lookup_paneid(const char *);
1.60      nicm      126: struct winlink *cmd_lookup_winlink_windowid(struct session *, const char *);
                    127: struct window  *cmd_lookup_windowid(const char *);
                    128: struct session *cmd_window_session(struct cmd_ctx *,
                    129:                    struct window *, struct winlink **);
1.43      nicm      130: struct winlink *cmd_find_window_offset(const char *, struct session *, int *);
                    131: int             cmd_find_index_offset(const char *, struct session *, int *);
1.51      nicm      132: struct window_pane *cmd_find_pane_offset(const char *, struct winlink *);
1.5       nicm      133:
1.10      nicm      134: int
                    135: cmd_pack_argv(int argc, char **argv, char *buf, size_t len)
                    136: {
                    137:        size_t  arglen;
                    138:        int     i;
                    139:
                    140:        *buf = '\0';
                    141:        for (i = 0; i < argc; i++) {
                    142:                if (strlcpy(buf, argv[i], len) >= len)
                    143:                        return (-1);
                    144:                arglen = strlen(argv[i]) + 1;
                    145:                buf += arglen;
                    146:                len -= arglen;
                    147:        }
                    148:
                    149:        return (0);
                    150: }
                    151:
                    152: int
                    153: cmd_unpack_argv(char *buf, size_t len, int argc, char ***argv)
                    154: {
                    155:        int     i;
                    156:        size_t  arglen;
                    157:
                    158:        if (argc == 0)
                    159:                return (0);
                    160:        *argv = xcalloc(argc, sizeof **argv);
                    161:
                    162:        buf[len - 1] = '\0';
                    163:        for (i = 0; i < argc; i++) {
                    164:                if (len == 0) {
                    165:                        cmd_free_argv(argc, *argv);
                    166:                        return (-1);
                    167:                }
                    168:
                    169:                arglen = strlen(buf) + 1;
                    170:                (*argv)[i] = xstrdup(buf);
                    171:                buf += arglen;
                    172:                len -= arglen;
                    173:        }
                    174:
                    175:        return (0);
1.46      nicm      176: }
                    177:
                    178: char **
1.49      nicm      179: cmd_copy_argv(int argc, char *const *argv)
1.46      nicm      180: {
                    181:        char    **new_argv;
                    182:        int       i;
                    183:
                    184:        if (argc == 0)
                    185:                return (NULL);
                    186:        new_argv = xcalloc(argc, sizeof *new_argv);
                    187:        for (i = 0; i < argc; i++) {
                    188:                if (argv[i] != NULL)
                    189:                        new_argv[i] = xstrdup(argv[i]);
                    190:        }
                    191:        return (new_argv);
1.10      nicm      192: }
                    193:
                    194: void
                    195: cmd_free_argv(int argc, char **argv)
                    196: {
                    197:        int     i;
                    198:
                    199:        if (argc == 0)
1.35      nicm      200:                return;
1.10      nicm      201:        for (i = 0; i < argc; i++) {
                    202:                if (argv[i] != NULL)
                    203:                        xfree(argv[i]);
                    204:        }
                    205:        xfree(argv);
                    206: }
                    207:
1.1       nicm      208: struct cmd *
                    209: cmd_parse(int argc, char **argv, char **cause)
                    210: {
                    211:        const struct cmd_entry **entryp, *entry;
1.27      deraadt   212:        struct cmd              *cmd;
1.49      nicm      213:        struct args             *args;
1.1       nicm      214:        char                     s[BUFSIZ];
1.49      nicm      215:        int                      ambiguous = 0;
1.1       nicm      216:
                    217:        *cause = NULL;
1.2       nicm      218:        if (argc == 0) {
                    219:                xasprintf(cause, "no command");
1.1       nicm      220:                return (NULL);
1.2       nicm      221:        }
1.1       nicm      222:
                    223:        entry = NULL;
                    224:        for (entryp = cmd_table; *entryp != NULL; entryp++) {
                    225:                if ((*entryp)->alias != NULL &&
                    226:                    strcmp((*entryp)->alias, argv[0]) == 0) {
1.3       nicm      227:                        ambiguous = 0;
1.1       nicm      228:                        entry = *entryp;
                    229:                        break;
                    230:                }
                    231:
                    232:                if (strncmp((*entryp)->name, argv[0], strlen(argv[0])) != 0)
                    233:                        continue;
                    234:                if (entry != NULL)
1.3       nicm      235:                        ambiguous = 1;
1.1       nicm      236:                entry = *entryp;
                    237:
                    238:                /* Bail now if an exact match. */
                    239:                if (strcmp(entry->name, argv[0]) == 0)
                    240:                        break;
                    241:        }
1.3       nicm      242:        if (ambiguous)
                    243:                goto ambiguous;
1.1       nicm      244:        if (entry == NULL) {
                    245:                xasprintf(cause, "unknown command: %s", argv[0]);
                    246:                return (NULL);
                    247:        }
                    248:
1.49      nicm      249:        args = args_parse(entry->args_template, argc, argv);
                    250:        if (args == NULL)
                    251:                goto usage;
                    252:        if (entry->args_lower != -1 && args->argc < entry->args_lower)
                    253:                goto usage;
                    254:        if (entry->args_upper != -1 && args->argc > entry->args_upper)
                    255:                goto usage;
                    256:        if (entry->check != NULL && entry->check(args) != 0)
                    257:                goto usage;
1.1       nicm      258:
                    259:        cmd = xmalloc(sizeof *cmd);
                    260:        cmd->entry = entry;
1.49      nicm      261:        cmd->args = args;
1.1       nicm      262:        return (cmd);
                    263:
                    264: ambiguous:
                    265:        *s = '\0';
                    266:        for (entryp = cmd_table; *entryp != NULL; entryp++) {
                    267:                if (strncmp((*entryp)->name, argv[0], strlen(argv[0])) != 0)
                    268:                        continue;
                    269:                if (strlcat(s, (*entryp)->name, sizeof s) >= sizeof s)
                    270:                        break;
                    271:                if (strlcat(s, ", ", sizeof s) >= sizeof s)
                    272:                        break;
                    273:        }
                    274:        s[strlen(s) - 2] = '\0';
                    275:        xasprintf(cause, "ambiguous command: %s, could be: %s", argv[0], s);
                    276:        return (NULL);
                    277:
                    278: usage:
1.49      nicm      279:        if (args != NULL)
                    280:                args_free(args);
1.1       nicm      281:        xasprintf(cause, "usage: %s %s", entry->name, entry->usage);
                    282:        return (NULL);
                    283: }
                    284:
                    285: int
                    286: cmd_exec(struct cmd *cmd, struct cmd_ctx *ctx)
                    287: {
                    288:        return (cmd->entry->exec(cmd, ctx));
                    289: }
                    290:
                    291: void
                    292: cmd_free(struct cmd *cmd)
                    293: {
1.49      nicm      294:        if (cmd->args != NULL)
                    295:                args_free(cmd->args);
1.1       nicm      296:        xfree(cmd);
                    297: }
                    298:
                    299: size_t
                    300: cmd_print(struct cmd *cmd, char *buf, size_t len)
                    301: {
1.49      nicm      302:        size_t  off, used;
                    303:
                    304:        off = xsnprintf(buf, len, "%s ", cmd->entry->name);
                    305:        if (off < len) {
                    306:                used = args_print(cmd->args, buf + off, len - off);
                    307:                if (used == 0)
1.64      nicm      308:                        off--;
                    309:                else
1.49      nicm      310:                        off += used;
1.64      nicm      311:                buf[off] = '\0';
1.49      nicm      312:        }
                    313:        return (off);
1.1       nicm      314: }
                    315:
1.5       nicm      316: /*
                    317:  * Figure out the current session. Use: 1) the current session, if the command
1.31      nicm      318:  * context has one; 2) the most recently used session containing the pty of the
                    319:  * calling client, if any; 3) the session specified in the TMUX variable from
                    320:  * the environment (as passed from the client); 4) the most recently used
                    321:  * session from all sessions.
1.5       nicm      322:  */
1.1       nicm      323: struct session *
1.52      nicm      324: cmd_current_session(struct cmd_ctx *ctx, int prefer_unattached)
1.1       nicm      325: {
                    326:        struct msg_command_data *data = ctx->msgdata;
1.11      nicm      327:        struct client           *c = ctx->cmdclient;
1.5       nicm      328:        struct session          *s;
1.47      nicm      329:        struct sessionslist      ss;
1.11      nicm      330:        struct winlink          *wl;
                    331:        struct window_pane      *wp;
                    332:        int                      found;
1.1       nicm      333:
1.14      nicm      334:        if (ctx->curclient != NULL && ctx->curclient->session != NULL)
                    335:                return (ctx->curclient->session);
1.1       nicm      336:
1.11      nicm      337:        /*
                    338:         * If the name of the calling client's pty is know, build a list of the
                    339:         * sessions that contain it and if any choose either the first or the
                    340:         * newest.
                    341:         */
                    342:        if (c != NULL && c->tty.path != NULL) {
                    343:                ARRAY_INIT(&ss);
1.47      nicm      344:                RB_FOREACH(s, sessions, &sessions) {
1.11      nicm      345:                        found = 0;
                    346:                        RB_FOREACH(wl, winlinks, &s->windows) {
                    347:                                TAILQ_FOREACH(wp, &wl->window->panes, entry) {
                    348:                                        if (strcmp(wp->tty, c->tty.path) == 0) {
                    349:                                                found = 1;
                    350:                                                break;
                    351:                                        }
                    352:                                }
                    353:                                if (found)
                    354:                                        break;
                    355:                        }
                    356:                        if (found)
                    357:                                ARRAY_ADD(&ss, s);
                    358:                }
                    359:
1.47      nicm      360:                s = cmd_choose_session_list(&ss);
1.11      nicm      361:                ARRAY_FREE(&ss);
                    362:                if (s != NULL)
                    363:                        return (s);
                    364:        }
                    365:
                    366:        /* Use the session from the TMUX environment variable. */
1.50      nicm      367:        if (data != NULL && data->pid == getpid() && data->idx != -1) {
1.47      nicm      368:                s = session_find_by_index(data->idx);
                    369:                if (s != NULL)
                    370:                        return (s);
                    371:        }
                    372:
1.52      nicm      373:        return (cmd_choose_session(prefer_unattached));
1.47      nicm      374: }
                    375:
1.52      nicm      376: /*
                    377:  * Find the most recently used session, preferring unattached if the flag is
                    378:  * set.
                    379:  */
1.47      nicm      380: struct session *
1.52      nicm      381: cmd_choose_session(int prefer_unattached)
1.47      nicm      382: {
                    383:        struct session  *s, *sbest;
                    384:        struct timeval  *tv = NULL;
                    385:
                    386:        sbest = NULL;
                    387:        RB_FOREACH(s, sessions, &sessions) {
1.52      nicm      388:                if (tv == NULL || timercmp(&s->activity_time, tv, >) ||
                    389:                    (prefer_unattached &&
                    390:                    !(sbest->flags & SESSION_UNATTACHED) &&
                    391:                    (s->flags & SESSION_UNATTACHED))) {
1.47      nicm      392:                        sbest = s;
                    393:                        tv = &s->activity_time;
                    394:                }
                    395:        }
1.1       nicm      396:
1.47      nicm      397:        return (sbest);
1.5       nicm      398: }
                    399:
1.31      nicm      400: /* Find the most recently used session from a list. */
1.5       nicm      401: struct session *
1.47      nicm      402: cmd_choose_session_list(struct sessionslist *ss)
1.5       nicm      403: {
1.31      nicm      404:        struct session  *s, *sbest;
1.5       nicm      405:        struct timeval  *tv = NULL;
                    406:        u_int            i;
                    407:
1.31      nicm      408:        sbest = NULL;
1.11      nicm      409:        for (i = 0; i < ARRAY_LENGTH(ss); i++) {
                    410:                if ((s = ARRAY_ITEM(ss, i)) == NULL)
1.5       nicm      411:                        continue;
                    412:
1.31      nicm      413:                if (tv == NULL || timercmp(&s->activity_time, tv, >)) {
                    414:                        sbest = s;
                    415:                        tv = &s->activity_time;
1.1       nicm      416:                }
                    417:        }
1.5       nicm      418:
1.31      nicm      419:        return (sbest);
1.1       nicm      420: }
                    421:
1.30      nicm      422: /*
                    423:  * Find the current client. First try the current client if set, then pick the
1.31      nicm      424:  * most recently used of the clients attached to the current session if any,
                    425:  * then of all clients.
1.30      nicm      426:  */
                    427: struct client *
                    428: cmd_current_client(struct cmd_ctx *ctx)
                    429: {
                    430:        struct session          *s;
                    431:        struct client           *c;
                    432:        struct clients           cc;
                    433:        u_int                    i;
                    434:
                    435:        if (ctx->curclient != NULL)
                    436:                return (ctx->curclient);
                    437:
                    438:        /*
                    439:         * No current client set. Find the current session and return the
                    440:         * newest of its clients.
                    441:         */
1.52      nicm      442:        s = cmd_current_session(ctx, 0);
1.30      nicm      443:        if (s != NULL && !(s->flags & SESSION_UNATTACHED)) {
                    444:                ARRAY_INIT(&cc);
                    445:                for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
                    446:                        if ((c = ARRAY_ITEM(&clients, i)) == NULL)
                    447:                                continue;
                    448:                        if (s == c->session)
                    449:                                ARRAY_ADD(&cc, c);
                    450:                }
                    451:
1.31      nicm      452:                c = cmd_choose_client(&cc);
1.30      nicm      453:                ARRAY_FREE(&cc);
                    454:                if (c != NULL)
                    455:                        return (c);
                    456:        }
                    457:
1.31      nicm      458:        return (cmd_choose_client(&clients));
1.30      nicm      459: }
                    460:
1.31      nicm      461: /* Choose the most recently used client from a list. */
1.20      nicm      462: struct client *
1.31      nicm      463: cmd_choose_client(struct clients *cc)
1.20      nicm      464: {
1.31      nicm      465:        struct client   *c, *cbest;
1.20      nicm      466:        struct timeval  *tv = NULL;
                    467:        u_int            i;
                    468:
1.31      nicm      469:        cbest = NULL;
1.30      nicm      470:        for (i = 0; i < ARRAY_LENGTH(cc); i++) {
                    471:                if ((c = ARRAY_ITEM(cc, i)) == NULL)
1.20      nicm      472:                        continue;
                    473:                if (c->session == NULL)
                    474:                        continue;
                    475:
1.31      nicm      476:                if (tv == NULL || timercmp(&c->activity_time, tv, >)) {
                    477:                        cbest = c;
                    478:                        tv = &c->activity_time;
1.20      nicm      479:                }
                    480:        }
                    481:
1.31      nicm      482:        return (cbest);
1.20      nicm      483: }
                    484:
1.5       nicm      485: /* Find the target client or report an error and return NULL. */
1.1       nicm      486: struct client *
                    487: cmd_find_client(struct cmd_ctx *ctx, const char *arg)
                    488: {
1.30      nicm      489:        struct client   *c;
1.5       nicm      490:        char            *tmparg;
                    491:        size_t           arglen;
1.1       nicm      492:
1.5       nicm      493:        /* A NULL argument means the current client. */
1.30      nicm      494:        if (arg == NULL)
                    495:                return (cmd_current_client(ctx));
1.5       nicm      496:        tmparg = xstrdup(arg);
                    497:
                    498:        /* Trim a single trailing colon if any. */
                    499:        arglen = strlen(tmparg);
                    500:        if (arglen != 0 && tmparg[arglen - 1] == ':')
                    501:                tmparg[arglen - 1] = '\0';
                    502:
                    503:        /* Find the client, if any. */
                    504:        c = cmd_lookup_client(tmparg);
                    505:
                    506:        /* If no client found, report an error. */
                    507:        if (c == NULL)
                    508:                ctx->error(ctx, "client not found: %s", tmparg);
                    509:
                    510:        xfree(tmparg);
                    511:        return (c);
                    512: }
                    513:
                    514: /*
                    515:  * Lookup a client by device path. Either of a full match and a match without a
                    516:  * leading _PATH_DEV ("/dev/") is accepted.
                    517:  */
                    518: struct client *
                    519: cmd_lookup_client(const char *name)
                    520: {
                    521:        struct client   *c;
                    522:        const char      *path;
                    523:        u_int            i;
                    524:
                    525:        for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
1.25      nicm      526:                c = ARRAY_ITEM(&clients, i);
                    527:                if (c == NULL || c->session == NULL)
1.5       nicm      528:                        continue;
                    529:                path = c->tty.path;
                    530:
                    531:                /* Check for exact matches. */
                    532:                if (strcmp(name, path) == 0)
                    533:                        return (c);
                    534:
                    535:                /* Check without leading /dev if present. */
                    536:                if (strncmp(path, _PATH_DEV, (sizeof _PATH_DEV) - 1) != 0)
                    537:                        continue;
                    538:                if (strcmp(name, path + (sizeof _PATH_DEV) - 1) == 0)
                    539:                        return (c);
                    540:        }
                    541:
                    542:        return (NULL);
                    543: }
                    544:
                    545: /* Lookup a session by name. If no session is found, NULL is returned. */
                    546: struct session *
                    547: cmd_lookup_session(const char *name, int *ambiguous)
                    548: {
                    549:        struct session  *s, *sfound;
                    550:
                    551:        *ambiguous = 0;
                    552:
                    553:        /*
1.28      nicm      554:         * Look for matches. First look for exact matches - session names must
                    555:         * be unique so an exact match can't be ambigious and can just be
                    556:         * returned.
1.5       nicm      557:         */
1.47      nicm      558:        if ((s = session_find(name)) != NULL)
                    559:                return (s);
1.28      nicm      560:
                    561:        /*
                    562:         * Otherwise look for partial matches, returning early if it is found to
                    563:         * be ambiguous.
                    564:         */
                    565:        sfound = NULL;
1.47      nicm      566:        RB_FOREACH(s, sessions, &sessions) {
1.5       nicm      567:                if (strncmp(name, s->name, strlen(name)) == 0 ||
                    568:                    fnmatch(name, s->name, 0) == 0) {
                    569:                        if (sfound != NULL) {
                    570:                                *ambiguous = 1;
                    571:                                return (NULL);
                    572:                        }
                    573:                        sfound = s;
                    574:                }
                    575:        }
1.35      nicm      576:        return (sfound);
1.5       nicm      577: }
                    578:
                    579: /*
1.8       nicm      580:  * Lookup a window or return -1 if not found or ambigious. First try as an
                    581:  * index and if invalid, use fnmatch or leading prefix. Return NULL but fill in
1.41      nicm      582:  * idx if the window index is a valid number but there is no window with that
1.8       nicm      583:  * index.
1.5       nicm      584:  */
                    585: struct winlink *
                    586: cmd_lookup_window(struct session *s, const char *name, int *ambiguous)
                    587: {
                    588:        struct winlink  *wl, *wlfound;
                    589:        const char      *errstr;
                    590:        u_int            idx;
                    591:
                    592:        *ambiguous = 0;
                    593:
1.60      nicm      594:        /* Try as a window id. */
                    595:        if ((wl = cmd_lookup_winlink_windowid(s, name)) != NULL)
                    596:            return (wl);
                    597:
1.5       nicm      598:        /* First see if this is a valid window index in this session. */
                    599:        idx = strtonum(name, 0, INT_MAX, &errstr);
                    600:        if (errstr == NULL) {
                    601:                if ((wl = winlink_find_by_index(&s->windows, idx)) != NULL)
                    602:                        return (wl);
                    603:        }
1.35      nicm      604:
1.5       nicm      605:        /* Look for exact matches, error if more than one. */
                    606:        wlfound = NULL;
                    607:        RB_FOREACH(wl, winlinks, &s->windows) {
1.8       nicm      608:                if (strcmp(name, wl->window->name) == 0) {
1.5       nicm      609:                        if (wlfound != NULL) {
                    610:                                *ambiguous = 1;
                    611:                                return (NULL);
                    612:                        }
                    613:                        wlfound = wl;
1.1       nicm      614:                }
                    615:        }
1.5       nicm      616:        if (wlfound != NULL)
                    617:                return (wlfound);
                    618:
                    619:        /* Now look for pattern matches, again error if multiple. */
                    620:        wlfound = NULL;
                    621:        RB_FOREACH(wl, winlinks, &s->windows) {
1.8       nicm      622:                if (strncmp(name, wl->window->name, strlen(name)) == 0 ||
                    623:                    fnmatch(name, wl->window->name, 0) == 0) {
1.5       nicm      624:                        if (wlfound != NULL) {
                    625:                                *ambiguous = 1;
                    626:                                return (NULL);
                    627:                        }
                    628:                        wlfound = wl;
                    629:                }
1.35      nicm      630:        }
1.5       nicm      631:        if (wlfound != NULL)
                    632:                return (wlfound);
                    633:
                    634:        return (NULL);
1.1       nicm      635: }
                    636:
1.8       nicm      637: /*
                    638:  * Find a window index - if the window doesn't exist, check if it is a
                    639:  * potential index and return it anyway.
                    640:  */
                    641: int
                    642: cmd_lookup_index(struct session *s, const char *name, int *ambiguous)
                    643: {
                    644:        struct winlink  *wl;
                    645:        const char      *errstr;
                    646:        u_int            idx;
                    647:
                    648:        if ((wl = cmd_lookup_window(s, name, ambiguous)) != NULL)
                    649:                return (wl->idx);
                    650:        if (*ambiguous)
                    651:                return (-1);
                    652:
                    653:        idx = strtonum(name, 0, INT_MAX, &errstr);
                    654:        if (errstr == NULL)
                    655:                return (idx);
                    656:
                    657:        return (-1);
                    658: }
                    659:
1.60      nicm      660: /* Lookup pane id. An initial % means a pane id. */
1.51      nicm      661: struct window_pane *
                    662: cmd_lookup_paneid(const char *arg)
                    663: {
                    664:        const char      *errstr;
                    665:        u_int            paneid;
                    666:
                    667:        if (*arg != '%')
                    668:                return (NULL);
                    669:
                    670:        paneid = strtonum(arg + 1, 0, UINT_MAX, &errstr);
                    671:        if (errstr != NULL)
                    672:                return (NULL);
                    673:        return (window_pane_find_by_id(paneid));
                    674: }
                    675:
1.60      nicm      676: /* Lookup window id in a session. An initial @ means a window id. */
                    677: struct winlink *
                    678: cmd_lookup_winlink_windowid(struct session *s, const char *arg)
                    679: {
                    680:        const char      *errstr;
                    681:        u_int            windowid;
                    682:
                    683:        if (*arg != '@')
                    684:                return (NULL);
                    685:
                    686:        windowid = strtonum(arg + 1, 0, UINT_MAX, &errstr);
                    687:        if (errstr != NULL)
                    688:                return (NULL);
                    689:        return (winlink_find_by_window_id(&s->windows, windowid));
                    690: }
                    691:
                    692: /* Lookup window id. An initial @ means a window id. */
                    693: struct window *
                    694: cmd_lookup_windowid(const char *arg)
                    695: {
                    696:        const char      *errstr;
                    697:        u_int            windowid;
                    698:
                    699:        if (*arg != '@')
                    700:                return (NULL);
                    701:
                    702:        windowid = strtonum(arg + 1, 0, UINT_MAX, &errstr);
                    703:        if (errstr != NULL)
                    704:                return (NULL);
                    705:        return (window_find_by_id(windowid));
                    706: }
                    707:
                    708: /* Find session and winlink for window. */
1.51      nicm      709: struct session *
1.60      nicm      710: cmd_window_session(struct cmd_ctx *ctx, struct window *w, struct winlink **wlp)
1.51      nicm      711: {
                    712:        struct session          *s;
                    713:        struct sessionslist      ss;
                    714:        struct winlink          *wl;
                    715:
1.60      nicm      716:        /* If this window is in the current session, return that winlink. */
1.52      nicm      717:        s = cmd_current_session(ctx, 0);
1.51      nicm      718:        if (s != NULL) {
1.60      nicm      719:                wl = winlink_find_by_window(&s->windows, w);
1.51      nicm      720:                if (wl != NULL) {
                    721:                        if (wlp != NULL)
                    722:                                *wlp = wl;
                    723:                        return (s);
                    724:                }
                    725:        }
                    726:
1.60      nicm      727:        /* Otherwise choose from all sessions with this window. */
1.51      nicm      728:        ARRAY_INIT(&ss);
                    729:        RB_FOREACH(s, sessions, &sessions) {
1.60      nicm      730:                if (winlink_find_by_window(&s->windows, w) != NULL)
1.51      nicm      731:                        ARRAY_ADD(&ss, s);
                    732:        }
                    733:        s = cmd_choose_session_list(&ss);
                    734:        ARRAY_FREE(&ss);
                    735:        if (wlp != NULL)
1.60      nicm      736:                *wlp = winlink_find_by_window(&s->windows, w);
1.51      nicm      737:        return (s);
                    738: }
                    739:
1.5       nicm      740: /* Find the target session or report an error and return NULL. */
1.1       nicm      741: struct session *
1.52      nicm      742: cmd_find_session(struct cmd_ctx *ctx, const char *arg, int prefer_unattached)
1.1       nicm      743: {
1.51      nicm      744:        struct session          *s;
                    745:        struct window_pane      *wp;
1.60      nicm      746:        struct window           *w;
1.51      nicm      747:        struct client           *c;
                    748:        char                    *tmparg;
                    749:        size_t                   arglen;
                    750:        int                      ambiguous;
1.1       nicm      751:
1.5       nicm      752:        /* A NULL argument means the current session. */
1.1       nicm      753:        if (arg == NULL)
1.52      nicm      754:                return (cmd_current_session(ctx, prefer_unattached));
1.5       nicm      755:
1.60      nicm      756:        /* Lookup as pane id or window id. */
1.51      nicm      757:        if ((wp = cmd_lookup_paneid(arg)) != NULL)
1.60      nicm      758:                return (cmd_window_session(ctx, wp->window, NULL));
                    759:        if ((w = cmd_lookup_windowid(arg)) != NULL)
                    760:                return (cmd_window_session(ctx, w, NULL));
1.51      nicm      761:
1.5       nicm      762:        /* Trim a single trailing colon if any. */
1.54      nicm      763:        tmparg = xstrdup(arg);
1.5       nicm      764:        arglen = strlen(tmparg);
                    765:        if (arglen != 0 && tmparg[arglen - 1] == ':')
                    766:                tmparg[arglen - 1] = '\0';
                    767:
1.53      nicm      768:        /* An empty session name is the current session. */
                    769:        if (*tmparg == '\0') {
1.54      nicm      770:                xfree(tmparg);
1.53      nicm      771:                return (cmd_current_session(ctx, prefer_unattached));
                    772:        }
                    773:
1.5       nicm      774:        /* Find the session, if any. */
                    775:        s = cmd_lookup_session(tmparg, &ambiguous);
                    776:
                    777:        /* If it doesn't, try to match it as a client. */
                    778:        if (s == NULL && (c = cmd_lookup_client(tmparg)) != NULL)
                    779:                s = c->session;
                    780:
                    781:        /* If no session found, report an error. */
                    782:        if (s == NULL) {
                    783:                if (ambiguous)
                    784:                        ctx->error(ctx, "more than one session: %s", tmparg);
                    785:                else
                    786:                        ctx->error(ctx, "session not found: %s", tmparg);
1.1       nicm      787:        }
1.5       nicm      788:
                    789:        xfree(tmparg);
1.1       nicm      790:        return (s);
                    791: }
                    792:
1.5       nicm      793: /* Find the target session and window or report an error and return NULL. */
1.1       nicm      794: struct winlink *
                    795: cmd_find_window(struct cmd_ctx *ctx, const char *arg, struct session **sp)
                    796: {
1.51      nicm      797:        struct session          *s;
                    798:        struct winlink          *wl;
                    799:        struct window_pane      *wp;
                    800:        const char              *winptr;
                    801:        char                    *sessptr = NULL;
                    802:        int                      ambiguous = 0;
1.5       nicm      803:
                    804:        /*
                    805:         * Find the current session. There must always be a current session, if
                    806:         * it can't be found, report an error.
                    807:         */
1.52      nicm      808:        if ((s = cmd_current_session(ctx, 0)) == NULL) {
1.5       nicm      809:                ctx->error(ctx, "can't establish current session");
                    810:                return (NULL);
                    811:        }
                    812:
                    813:        /* A NULL argument means the current session and window. */
                    814:        if (arg == NULL) {
                    815:                if (sp != NULL)
                    816:                        *sp = s;
                    817:                return (s->curw);
                    818:        }
                    819:
1.51      nicm      820:        /* Lookup as pane id. */
                    821:        if ((wp = cmd_lookup_paneid(arg)) != NULL) {
1.60      nicm      822:                s = cmd_window_session(ctx, wp->window, &wl);
1.51      nicm      823:                if (sp != NULL)
                    824:                        *sp = s;
                    825:                return (wl);
                    826:        }
                    827:
1.5       nicm      828:        /* Time to look at the argument. If it is empty, that is an error. */
                    829:        if (*arg == '\0')
                    830:                goto not_found;
                    831:
1.8       nicm      832:        /* Find the separating colon and split into window and session. */
1.5       nicm      833:        winptr = strchr(arg, ':');
                    834:        if (winptr == NULL)
1.8       nicm      835:                goto no_colon;
                    836:        winptr++;       /* skip : */
                    837:        sessptr = xstrdup(arg);
                    838:        *strchr(sessptr, ':') = '\0';
1.5       nicm      839:
                    840:        /* Try to lookup the session if present. */
1.8       nicm      841:        if (*sessptr != '\0') {
                    842:                if ((s = cmd_lookup_session(sessptr, &ambiguous)) == NULL)
1.5       nicm      843:                        goto no_session;
                    844:        }
                    845:        if (sp != NULL)
                    846:                *sp = s;
                    847:
                    848:        /*
                    849:         * Then work out the window. An empty string is the current window,
1.38      nicm      850:         * otherwise try special cases then to look it up in the session.
1.5       nicm      851:         */
1.8       nicm      852:        if (*winptr == '\0')
1.5       nicm      853:                wl = s->curw;
1.38      nicm      854:        else if (winptr[0] == '!' && winptr[1] == '\0')
                    855:                wl = TAILQ_FIRST(&s->lastw);
1.43      nicm      856:        else if (winptr[0] == '+' || winptr[0] == '-')
                    857:                wl = cmd_find_window_offset(winptr, s, &ambiguous);
                    858:        else
1.38      nicm      859:                wl = cmd_lookup_window(s, winptr, &ambiguous);
                    860:        if (wl == NULL)
1.5       nicm      861:                goto not_found;
1.35      nicm      862:
1.5       nicm      863:        if (sessptr != NULL)
                    864:                xfree(sessptr);
                    865:        return (wl);
                    866:
1.8       nicm      867: no_colon:
1.38      nicm      868:        /*
                    869:         * No colon in the string, first try special cases, then as a window
                    870:         * and lastly as a session.
                    871:         */
                    872:        if (arg[0] == '!' && arg[1] == '\0') {
                    873:                if ((wl = TAILQ_FIRST(&s->lastw)) == NULL)
                    874:                        goto not_found;
1.41      nicm      875:        } else if (arg[0] == '+' || arg[0] == '-') {
1.43      nicm      876:                if ((wl = cmd_find_window_offset(arg, s, &ambiguous)) == NULL)
1.41      nicm      877:                        goto lookup_session;
                    878:        } else if ((wl = cmd_lookup_window(s, arg, &ambiguous)) == NULL)
                    879:                goto lookup_session;
1.8       nicm      880:
                    881:        if (sp != NULL)
                    882:                *sp = s;
                    883:
                    884:        return (wl);
                    885:
1.41      nicm      886: lookup_session:
                    887:        if (ambiguous)
                    888:                goto not_found;
1.53      nicm      889:        if (*arg != '\0' && (s = cmd_lookup_session(arg, &ambiguous)) == NULL)
1.41      nicm      890:                goto no_session;
                    891:
                    892:        if (sp != NULL)
                    893:                *sp = s;
                    894:
                    895:        return (s->curw);
                    896:
1.5       nicm      897: no_session:
                    898:        if (ambiguous)
1.8       nicm      899:                ctx->error(ctx, "multiple sessions: %s", arg);
1.5       nicm      900:        else
1.8       nicm      901:                ctx->error(ctx, "session not found: %s", arg);
1.5       nicm      902:        if (sessptr != NULL)
                    903:                xfree(sessptr);
                    904:        return (NULL);
                    905:
                    906: not_found:
                    907:        if (ambiguous)
                    908:                ctx->error(ctx, "multiple windows: %s", arg);
                    909:        else
                    910:                ctx->error(ctx, "window not found: %s", arg);
                    911:        if (sessptr != NULL)
                    912:                xfree(sessptr);
                    913:        return (NULL);
                    914: }
1.1       nicm      915:
1.43      nicm      916: struct winlink *
                    917: cmd_find_window_offset(const char *winptr, struct session *s, int *ambiguous)
                    918: {
                    919:        struct winlink  *wl;
                    920:        int              offset = 1;
                    921:
                    922:        if (winptr[1] != '\0')
                    923:                offset = strtonum(winptr + 1, 1, INT_MAX, NULL);
                    924:        if (offset == 0)
                    925:                wl = cmd_lookup_window(s, winptr, ambiguous);
                    926:        else {
                    927:                if (winptr[0] == '+')
                    928:                        wl = winlink_next_by_number(s->curw, s, offset);
                    929:                else
                    930:                        wl = winlink_previous_by_number(s->curw, s, offset);
                    931:        }
                    932:
                    933:        return (wl);
                    934: }
                    935:
1.5       nicm      936: /*
                    937:  * Find the target session and window index, whether or not it exists in the
                    938:  * session. Return -2 on error or -1 if no window index is specified. This is
1.8       nicm      939:  * used when parsing an argument for a window target that may not exist (for
                    940:  * example if it is going to be created).
1.5       nicm      941:  */
                    942: int
                    943: cmd_find_index(struct cmd_ctx *ctx, const char *arg, struct session **sp)
                    944: {
                    945:        struct session  *s;
1.38      nicm      946:        struct winlink  *wl;
1.8       nicm      947:        const char      *winptr;
1.5       nicm      948:        char            *sessptr = NULL;
                    949:        int              idx, ambiguous = 0;
                    950:
                    951:        /*
                    952:         * Find the current session. There must always be a current session, if
                    953:         * it can't be found, report an error.
                    954:         */
1.52      nicm      955:        if ((s = cmd_current_session(ctx, 0)) == NULL) {
1.5       nicm      956:                ctx->error(ctx, "can't establish current session");
1.9       nicm      957:                return (-2);
1.1       nicm      958:        }
1.5       nicm      959:
                    960:        /* A NULL argument means the current session and "no window" (-1). */
                    961:        if (arg == NULL) {
                    962:                if (sp != NULL)
                    963:                        *sp = s;
                    964:                return (-1);
                    965:        }
                    966:
                    967:        /* Time to look at the argument. If it is empty, that is an error. */
                    968:        if (*arg == '\0')
                    969:                goto not_found;
                    970:
                    971:        /* Find the separating colon. If none, assume the current session. */
                    972:        winptr = strchr(arg, ':');
                    973:        if (winptr == NULL)
1.8       nicm      974:                goto no_colon;
                    975:        winptr++;       /* skip : */
                    976:        sessptr = xstrdup(arg);
                    977:        *strchr(sessptr, ':') = '\0';
1.5       nicm      978:
                    979:        /* Try to lookup the session if present. */
                    980:        if (sessptr != NULL && *sessptr != '\0') {
1.8       nicm      981:                if ((s = cmd_lookup_session(sessptr, &ambiguous)) == NULL)
1.5       nicm      982:                        goto no_session;
                    983:        }
1.1       nicm      984:        if (sp != NULL)
                    985:                *sp = s;
                    986:
1.5       nicm      987:        /*
1.8       nicm      988:         * Then work out the window. An empty string is a new window otherwise
                    989:         * try to look it up in the session.
1.5       nicm      990:         */
1.8       nicm      991:        if (*winptr == '\0')
1.38      nicm      992:                idx = -1;
                    993:        else if (winptr[0] == '!' && winptr[1] == '\0') {
                    994:                if ((wl = TAILQ_FIRST(&s->lastw)) == NULL)
                    995:                        goto not_found;
                    996:                idx = wl->idx;
1.41      nicm      997:        } else if (winptr[0] == '+' || winptr[0] == '-') {
1.43      nicm      998:                if ((idx = cmd_find_index_offset(winptr, s, &ambiguous)) < 0)
1.41      nicm      999:                        goto invalid_index;
                   1000:        } else if ((idx = cmd_lookup_index(s, winptr, &ambiguous)) == -1)
                   1001:                goto invalid_index;
1.35      nicm     1002:
1.5       nicm     1003:        if (sessptr != NULL)
                   1004:                xfree(sessptr);
                   1005:        return (idx);
                   1006:
1.8       nicm     1007: no_colon:
1.38      nicm     1008:        /*
                   1009:         * No colon in the string, first try special cases, then as a window
                   1010:         * and lastly as a session.
                   1011:         */
                   1012:        if (arg[0] == '!' && arg[1] == '\0') {
                   1013:                if ((wl = TAILQ_FIRST(&s->lastw)) == NULL)
                   1014:                        goto not_found;
                   1015:                idx = wl->idx;
1.41      nicm     1016:        } else if (arg[0] == '+' || arg[0] == '-') {
1.43      nicm     1017:                if ((idx = cmd_find_index_offset(arg, s, &ambiguous)) < 0)
1.41      nicm     1018:                        goto lookup_session;
                   1019:        } else if ((idx = cmd_lookup_index(s, arg, &ambiguous)) == -1)
                   1020:                goto lookup_session;
1.8       nicm     1021:
                   1022:        if (sp != NULL)
                   1023:                *sp = s;
                   1024:
                   1025:        return (idx);
                   1026:
1.41      nicm     1027: lookup_session:
                   1028:        if (ambiguous)
                   1029:                goto not_found;
1.53      nicm     1030:        if (*arg != '\0' && (s = cmd_lookup_session(arg, &ambiguous)) == NULL)
1.41      nicm     1031:                goto no_session;
                   1032:
                   1033:        if (sp != NULL)
                   1034:                *sp = s;
                   1035:
                   1036:        return (-1);
                   1037:
1.5       nicm     1038: no_session:
                   1039:        if (ambiguous)
1.35      nicm     1040:                ctx->error(ctx, "multiple sessions: %s", arg);
1.5       nicm     1041:        else
1.8       nicm     1042:                ctx->error(ctx, "session not found: %s", arg);
1.5       nicm     1043:        if (sessptr != NULL)
                   1044:                xfree(sessptr);
                   1045:        return (-2);
                   1046:
1.41      nicm     1047: invalid_index:
                   1048:        if (ambiguous)
                   1049:                goto not_found;
                   1050:        ctx->error(ctx, "invalid index: %s", arg);
                   1051:
                   1052:        if (sessptr != NULL)
                   1053:                xfree(sessptr);
                   1054:        return (-2);
                   1055:
1.5       nicm     1056: not_found:
                   1057:        if (ambiguous)
                   1058:                ctx->error(ctx, "multiple windows: %s", arg);
1.1       nicm     1059:        else
1.5       nicm     1060:                ctx->error(ctx, "window not found: %s", arg);
                   1061:        if (sessptr != NULL)
                   1062:                xfree(sessptr);
                   1063:        return (-2);
1.12      nicm     1064: }
                   1065:
1.43      nicm     1066: int
                   1067: cmd_find_index_offset(const char *winptr, struct session *s, int *ambiguous)
                   1068: {
                   1069:        int     idx, offset = 1;
                   1070:
                   1071:        if (winptr[1] != '\0')
                   1072:                offset = strtonum(winptr + 1, 1, INT_MAX, NULL);
                   1073:        if (offset == 0)
                   1074:                idx = cmd_lookup_index(s, winptr, ambiguous);
                   1075:        else {
                   1076:                if (winptr[0] == '+') {
                   1077:                        if (s->curw->idx == INT_MAX)
                   1078:                                idx = cmd_lookup_index(s, winptr, ambiguous);
                   1079:                        else
                   1080:                                idx = s->curw->idx + offset;
                   1081:                } else {
                   1082:                        if (s->curw->idx == 0)
                   1083:                                idx = cmd_lookup_index(s, winptr, ambiguous);
                   1084:                        else
                   1085:                                idx = s->curw->idx - offset;
                   1086:                }
                   1087:        }
                   1088:
                   1089:        return (idx);
                   1090: }
                   1091:
1.12      nicm     1092: /*
                   1093:  * Find the target session, window and pane number or report an error and
                   1094:  * return NULL. The pane number is separated from the session:window by a .,
                   1095:  * such as mysession:mywindow.0.
                   1096:  */
                   1097: struct winlink *
                   1098: cmd_find_pane(struct cmd_ctx *ctx,
                   1099:     const char *arg, struct session **sp, struct window_pane **wpp)
                   1100: {
1.55      nicm     1101:        struct session  *s;
                   1102:        struct winlink  *wl;
                   1103:        const char      *period, *errstr;
                   1104:        char            *winptr, *paneptr;
                   1105:        u_int            idx;
1.12      nicm     1106:
                   1107:        /* Get the current session. */
1.52      nicm     1108:        if ((s = cmd_current_session(ctx, 0)) == NULL) {
1.27      deraadt  1109:                ctx->error(ctx, "can't establish current session");
1.12      nicm     1110:                return (NULL);
                   1111:        }
                   1112:        if (sp != NULL)
                   1113:                *sp = s;
                   1114:
                   1115:        /* A NULL argument means the current session, window and pane. */
                   1116:        if (arg == NULL) {
                   1117:                *wpp = s->curw->window->active;
                   1118:                return (s->curw);
1.51      nicm     1119:        }
                   1120:
                   1121:        /* Lookup as pane id. */
                   1122:        if ((*wpp = cmd_lookup_paneid(arg)) != NULL) {
1.60      nicm     1123:                s = cmd_window_session(ctx, (*wpp)->window, &wl);
1.51      nicm     1124:                if (sp != NULL)
                   1125:                        *sp = s;
                   1126:                return (wl);
1.12      nicm     1127:        }
                   1128:
                   1129:        /* Look for a separating period. */
                   1130:        if ((period = strrchr(arg, '.')) == NULL)
                   1131:                goto no_period;
                   1132:
                   1133:        /* Pull out the window part and parse it. */
                   1134:        winptr = xstrdup(arg);
                   1135:        winptr[period - arg] = '\0';
                   1136:        if (*winptr == '\0')
                   1137:                wl = s->curw;
                   1138:        else if ((wl = cmd_find_window(ctx, winptr, sp)) == NULL)
                   1139:                goto error;
                   1140:
                   1141:        /* Find the pane section and look it up. */
                   1142:        paneptr = winptr + (period - arg) + 1;
                   1143:        if (*paneptr == '\0')
                   1144:                *wpp = wl->window->active;
1.43      nicm     1145:        else if (paneptr[0] == '+' || paneptr[0] == '-')
                   1146:                *wpp = cmd_find_pane_offset(paneptr, wl);
                   1147:        else {
1.12      nicm     1148:                idx = strtonum(paneptr, 0, INT_MAX, &errstr);
1.36      nicm     1149:                if (errstr != NULL)
                   1150:                        goto lookup_string;
1.12      nicm     1151:                *wpp = window_pane_at_index(wl->window, idx);
1.36      nicm     1152:                if (*wpp == NULL)
                   1153:                        goto lookup_string;
1.12      nicm     1154:        }
                   1155:
                   1156:        xfree(winptr);
                   1157:        return (wl);
                   1158:
1.36      nicm     1159: lookup_string:
                   1160:        /* Try pane string description. */
1.55      nicm     1161:        if ((*wpp = window_find_string(wl->window, paneptr)) == NULL) {
1.36      nicm     1162:                ctx->error(ctx, "can't find pane: %s", paneptr);
                   1163:                goto error;
                   1164:        }
                   1165:
                   1166:        xfree(winptr);
1.39      nicm     1167:        return (wl);
1.36      nicm     1168:
1.12      nicm     1169: no_period:
                   1170:        /* Try as a pane number alone. */
                   1171:        idx = strtonum(arg, 0, INT_MAX, &errstr);
                   1172:        if (errstr != NULL)
                   1173:                goto lookup_window;
                   1174:
                   1175:        /* Try index in the current session and window. */
                   1176:        if ((*wpp = window_pane_at_index(s->curw->window, idx)) == NULL)
                   1177:                goto lookup_window;
1.35      nicm     1178:
1.12      nicm     1179:        return (s->curw);
                   1180:
                   1181: lookup_window:
1.36      nicm     1182:        /* Try pane string description. */
1.55      nicm     1183:        if ((*wpp = window_find_string(s->curw->window, arg)) != NULL)
1.36      nicm     1184:                return (s->curw);
                   1185:
1.12      nicm     1186:        /* Try as a window and use the active pane. */
                   1187:        if ((wl = cmd_find_window(ctx, arg, sp)) != NULL)
                   1188:                *wpp = wl->window->active;
                   1189:        return (wl);
1.35      nicm     1190:
1.12      nicm     1191: error:
                   1192:        xfree(winptr);
                   1193:        return (NULL);
1.43      nicm     1194: }
                   1195:
                   1196: struct window_pane *
                   1197: cmd_find_pane_offset(const char *paneptr, struct winlink *wl)
                   1198: {
                   1199:        struct window           *w = wl->window;
                   1200:        struct window_pane      *wp = w->active;
                   1201:        u_int                    offset = 1;
                   1202:
                   1203:        if (paneptr[1] != '\0')
                   1204:                offset = strtonum(paneptr + 1, 1, INT_MAX, NULL);
                   1205:        if (offset > 0) {
                   1206:                if (paneptr[0] == '+')
                   1207:                        wp = window_pane_next_by_number(w, wp, offset);
                   1208:                else
                   1209:                        wp = window_pane_previous_by_number(w, wp, offset);
                   1210:        }
                   1211:
                   1212:        return (wp);
1.15      nicm     1213: }
                   1214:
                   1215: /* Replace the first %% or %idx in template by s. */
                   1216: char *
                   1217: cmd_template_replace(char *template, const char *s, int idx)
                   1218: {
                   1219:        char     ch;
                   1220:        char    *buf, *ptr;
                   1221:        int      replaced;
                   1222:        size_t   len;
                   1223:
                   1224:        if (strstr(template, "%") == NULL)
                   1225:                return (xstrdup(template));
                   1226:
                   1227:        buf = xmalloc(1);
                   1228:        *buf = '\0';
                   1229:        len = 0;
                   1230:        replaced = 0;
                   1231:
                   1232:        ptr = template;
                   1233:        while (*ptr != '\0') {
                   1234:                switch (ch = *ptr++) {
                   1235:                case '%':
                   1236:                        if (*ptr < '1' || *ptr > '9' || *ptr - '0' != idx) {
                   1237:                                if (*ptr != '%' || replaced)
                   1238:                                        break;
                   1239:                                replaced = 1;
                   1240:                        }
                   1241:                        ptr++;
                   1242:
                   1243:                        len += strlen(s);
                   1244:                        buf = xrealloc(buf, 1, len + 1);
                   1245:                        strlcat(buf, s, len + 1);
                   1246:                        continue;
                   1247:                }
                   1248:                buf = xrealloc(buf, 1, len + 2);
                   1249:                buf[len++] = ch;
                   1250:                buf[len] = '\0';
                   1251:        }
                   1252:
                   1253:        return (buf);
1.57      nicm     1254: }
                   1255:
1.61      nicm     1256: /*
                   1257:  * Return the default path for a new pane, using the given path or the
                   1258:  * default-path option if it is NULL. Several special values are accepted: the
                   1259:  * empty string or relative path for the current pane's working directory, ~
                   1260:  * for the user's home, - for the session working directory, . for the tmux
                   1261:  * server's working directory. The default on failure is the session's working
                   1262:  * directory.
                   1263:  */
1.59      nicm     1264: const char *
1.61      nicm     1265: cmd_get_default_path(struct cmd_ctx *ctx, const char *cwd)
1.57      nicm     1266: {
                   1267:        struct session          *s;
1.58      stsp     1268:        struct environ_entry    *envent;
1.61      nicm     1269:        const char              *root;
                   1270:        char                     tmp[MAXPATHLEN];
                   1271:        struct passwd           *pw;
                   1272:        int                      n;
                   1273:        size_t                   skip;
                   1274:        static char              path[MAXPATHLEN];
1.57      nicm     1275:
                   1276:        if ((s = cmd_current_session(ctx, 0)) == NULL)
                   1277:                return (NULL);
                   1278:
1.61      nicm     1279:        if (cwd == NULL)
                   1280:                cwd = options_get_string(&s->options, "default-path");
                   1281:
                   1282:        skip = 1;
                   1283:        if (strcmp(cwd, "$HOME") == 0 || strncmp(cwd, "$HOME/", 6) == 0) {
                   1284:                /* User's home directory - $HOME. */
                   1285:                skip = 5;
                   1286:                goto find_home;
                   1287:        } else if (cwd[0] == '~' && (cwd[1] == '\0' || cwd[1] == '/')) {
                   1288:                /* User's home directory - ~. */
                   1289:                goto find_home;
                   1290:        } else if (cwd[0] == '-' && (cwd[1] == '\0' || cwd[1] == '/')) {
                   1291:                /* Session working directory. */
                   1292:                root = s->cwd;
                   1293:                goto complete_path;
1.66      nicm     1294:        } else if (cwd[0] == '.' && (cwd[1] == '\0' || cwd[1] == '/')) {
1.61      nicm     1295:                /* Server working directory. */
                   1296:                if (getcwd(tmp, sizeof tmp) != NULL) {
                   1297:                        root = tmp;
                   1298:                        goto complete_path;
1.57      nicm     1299:                }
                   1300:                return (s->cwd);
1.61      nicm     1301:        } else if (*cwd == '/') {
                   1302:                /* Absolute path. */
                   1303:                return (cwd);
                   1304:        } else {
                   1305:                /* Empty or relative path. */
                   1306:                if (ctx->cmdclient != NULL && ctx->cmdclient->cwd != NULL)
                   1307:                        root = ctx->cmdclient->cwd;
1.66      nicm     1308:                else if (ctx->curclient != NULL && s->curw != NULL)
1.61      nicm     1309:                        root = get_proc_cwd(s->curw->window->active->pid);
                   1310:                else
                   1311:                        return (s->cwd);
                   1312:                skip = 0;
1.62      nicm     1313:                if (root != NULL)
                   1314:                        goto complete_path;
1.57      nicm     1315:        }
1.61      nicm     1316:
                   1317:        return (s->cwd);
                   1318:
                   1319: find_home:
                   1320:        envent = environ_find(&global_environ, "HOME");
                   1321:        if (envent != NULL && *envent->value != '\0')
                   1322:                root = envent->value;
                   1323:        else if ((pw = getpwuid(getuid())) != NULL)
                   1324:                root = pw->pw_dir;
                   1325:        else
                   1326:                return (s->cwd);
                   1327:
                   1328: complete_path:
1.65      nicm     1329:        if (root[skip] == '\0') {
                   1330:                strlcpy(path, root, sizeof path);
                   1331:                return (path);
                   1332:        }
1.61      nicm     1333:        n = snprintf(path, sizeof path, "%s/%s", root, cwd + skip);
                   1334:        if (n > 0 && (size_t)n < sizeof path)
                   1335:                return (path);
                   1336:        return (s->cwd);
1.1       nicm     1337: }