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

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