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

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