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

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