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

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