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

1.138   ! nicm        1: /* $OpenBSD: cmd.c,v 1.137 2017/04/21 20:26:34 nicm Exp $ */
1.1       nicm        2:
                      3: /*
1.120     nicm        4:  * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
1.1       nicm        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"
1.105     nicm       30:
                     31: extern const struct cmd_entry cmd_attach_session_entry;
                     32: extern const struct cmd_entry cmd_bind_key_entry;
                     33: extern const struct cmd_entry cmd_break_pane_entry;
                     34: extern const struct cmd_entry cmd_capture_pane_entry;
                     35: extern const struct cmd_entry cmd_choose_buffer_entry;
                     36: extern const struct cmd_entry cmd_choose_client_entry;
                     37: extern const struct cmd_entry cmd_choose_session_entry;
                     38: extern const struct cmd_entry cmd_choose_tree_entry;
                     39: extern const struct cmd_entry cmd_choose_window_entry;
                     40: extern const struct cmd_entry cmd_clear_history_entry;
                     41: extern const struct cmd_entry cmd_clock_mode_entry;
                     42: extern const struct cmd_entry cmd_command_prompt_entry;
                     43: extern const struct cmd_entry cmd_confirm_before_entry;
                     44: extern const struct cmd_entry cmd_copy_mode_entry;
                     45: extern const struct cmd_entry cmd_delete_buffer_entry;
                     46: extern const struct cmd_entry cmd_detach_client_entry;
                     47: extern const struct cmd_entry cmd_display_message_entry;
                     48: extern const struct cmd_entry cmd_display_panes_entry;
                     49: extern const struct cmd_entry cmd_down_pane_entry;
                     50: extern const struct cmd_entry cmd_find_window_entry;
                     51: extern const struct cmd_entry cmd_has_session_entry;
                     52: extern const struct cmd_entry cmd_if_shell_entry;
                     53: extern const struct cmd_entry cmd_join_pane_entry;
                     54: extern const struct cmd_entry cmd_kill_pane_entry;
                     55: extern const struct cmd_entry cmd_kill_server_entry;
                     56: extern const struct cmd_entry cmd_kill_session_entry;
                     57: extern const struct cmd_entry cmd_kill_window_entry;
                     58: extern const struct cmd_entry cmd_last_pane_entry;
                     59: extern const struct cmd_entry cmd_last_window_entry;
                     60: extern const struct cmd_entry cmd_link_window_entry;
                     61: extern const struct cmd_entry cmd_list_buffers_entry;
                     62: extern const struct cmd_entry cmd_list_clients_entry;
                     63: extern const struct cmd_entry cmd_list_commands_entry;
                     64: extern const struct cmd_entry cmd_list_keys_entry;
                     65: extern const struct cmd_entry cmd_list_panes_entry;
                     66: extern const struct cmd_entry cmd_list_sessions_entry;
                     67: extern const struct cmd_entry cmd_list_windows_entry;
                     68: extern const struct cmd_entry cmd_load_buffer_entry;
                     69: extern const struct cmd_entry cmd_lock_client_entry;
                     70: extern const struct cmd_entry cmd_lock_server_entry;
                     71: extern const struct cmd_entry cmd_lock_session_entry;
                     72: extern const struct cmd_entry cmd_move_pane_entry;
                     73: extern const struct cmd_entry cmd_move_window_entry;
                     74: extern const struct cmd_entry cmd_new_session_entry;
                     75: extern const struct cmd_entry cmd_new_window_entry;
                     76: extern const struct cmd_entry cmd_next_layout_entry;
                     77: extern const struct cmd_entry cmd_next_window_entry;
                     78: extern const struct cmd_entry cmd_paste_buffer_entry;
                     79: extern const struct cmd_entry cmd_pipe_pane_entry;
                     80: extern const struct cmd_entry cmd_previous_layout_entry;
                     81: extern const struct cmd_entry cmd_previous_window_entry;
                     82: extern const struct cmd_entry cmd_refresh_client_entry;
                     83: extern const struct cmd_entry cmd_rename_session_entry;
                     84: extern const struct cmd_entry cmd_rename_window_entry;
                     85: extern const struct cmd_entry cmd_resize_pane_entry;
                     86: extern const struct cmd_entry cmd_respawn_pane_entry;
                     87: extern const struct cmd_entry cmd_respawn_window_entry;
                     88: extern const struct cmd_entry cmd_rotate_window_entry;
                     89: extern const struct cmd_entry cmd_run_shell_entry;
                     90: extern const struct cmd_entry cmd_save_buffer_entry;
                     91: extern const struct cmd_entry cmd_select_layout_entry;
                     92: extern const struct cmd_entry cmd_select_pane_entry;
                     93: extern const struct cmd_entry cmd_select_window_entry;
                     94: extern const struct cmd_entry cmd_send_keys_entry;
                     95: extern const struct cmd_entry cmd_send_prefix_entry;
                     96: extern const struct cmd_entry cmd_set_buffer_entry;
                     97: extern const struct cmd_entry cmd_set_environment_entry;
1.107     nicm       98: extern const struct cmd_entry cmd_set_hook_entry;
1.105     nicm       99: extern const struct cmd_entry cmd_set_option_entry;
                    100: extern const struct cmd_entry cmd_set_window_option_entry;
                    101: extern const struct cmd_entry cmd_show_buffer_entry;
                    102: extern const struct cmd_entry cmd_show_environment_entry;
1.107     nicm      103: extern const struct cmd_entry cmd_show_hooks_entry;
1.105     nicm      104: extern const struct cmd_entry cmd_show_messages_entry;
                    105: extern const struct cmd_entry cmd_show_options_entry;
                    106: extern const struct cmd_entry cmd_show_window_options_entry;
                    107: extern const struct cmd_entry cmd_source_file_entry;
                    108: extern const struct cmd_entry cmd_split_window_entry;
                    109: extern const struct cmd_entry cmd_start_server_entry;
                    110: extern const struct cmd_entry cmd_suspend_client_entry;
                    111: extern const struct cmd_entry cmd_swap_pane_entry;
                    112: extern const struct cmd_entry cmd_swap_window_entry;
                    113: extern const struct cmd_entry cmd_switch_client_entry;
                    114: extern const struct cmd_entry cmd_unbind_key_entry;
                    115: extern const struct cmd_entry cmd_unlink_window_entry;
                    116: extern const struct cmd_entry cmd_up_pane_entry;
                    117: extern const struct cmd_entry cmd_wait_for_entry;
1.1       nicm      118:
                    119: const struct cmd_entry *cmd_table[] = {
                    120:        &cmd_attach_session_entry,
                    121:        &cmd_bind_key_entry,
                    122:        &cmd_break_pane_entry,
1.34      nicm      123:        &cmd_capture_pane_entry,
1.42      nicm      124:        &cmd_choose_buffer_entry,
1.15      nicm      125:        &cmd_choose_client_entry,
1.1       nicm      126:        &cmd_choose_session_entry,
1.67      nicm      127:        &cmd_choose_tree_entry,
1.1       nicm      128:        &cmd_choose_window_entry,
                    129:        &cmd_clear_history_entry,
                    130:        &cmd_clock_mode_entry,
                    131:        &cmd_command_prompt_entry,
                    132:        &cmd_confirm_before_entry,
                    133:        &cmd_copy_mode_entry,
                    134:        &cmd_delete_buffer_entry,
                    135:        &cmd_detach_client_entry,
1.7       nicm      136:        &cmd_display_message_entry,
1.16      nicm      137:        &cmd_display_panes_entry,
1.1       nicm      138:        &cmd_find_window_entry,
                    139:        &cmd_has_session_entry,
1.4       nicm      140:        &cmd_if_shell_entry,
1.37      nicm      141:        &cmd_join_pane_entry,
1.1       nicm      142:        &cmd_kill_pane_entry,
                    143:        &cmd_kill_server_entry,
                    144:        &cmd_kill_session_entry,
                    145:        &cmd_kill_window_entry,
1.45      nicm      146:        &cmd_last_pane_entry,
1.1       nicm      147:        &cmd_last_window_entry,
                    148:        &cmd_link_window_entry,
                    149:        &cmd_list_buffers_entry,
                    150:        &cmd_list_clients_entry,
                    151:        &cmd_list_commands_entry,
                    152:        &cmd_list_keys_entry,
1.23      nicm      153:        &cmd_list_panes_entry,
1.1       nicm      154:        &cmd_list_sessions_entry,
                    155:        &cmd_list_windows_entry,
                    156:        &cmd_load_buffer_entry,
1.19      nicm      157:        &cmd_lock_client_entry,
1.1       nicm      158:        &cmd_lock_server_entry,
1.19      nicm      159:        &cmd_lock_session_entry,
1.63      nicm      160:        &cmd_move_pane_entry,
1.1       nicm      161:        &cmd_move_window_entry,
                    162:        &cmd_new_session_entry,
                    163:        &cmd_new_window_entry,
                    164:        &cmd_next_layout_entry,
                    165:        &cmd_next_window_entry,
                    166:        &cmd_paste_buffer_entry,
1.24      nicm      167:        &cmd_pipe_pane_entry,
1.1       nicm      168:        &cmd_previous_layout_entry,
                    169:        &cmd_previous_window_entry,
                    170:        &cmd_refresh_client_entry,
                    171:        &cmd_rename_session_entry,
                    172:        &cmd_rename_window_entry,
                    173:        &cmd_resize_pane_entry,
1.56      nicm      174:        &cmd_respawn_pane_entry,
1.1       nicm      175:        &cmd_respawn_window_entry,
                    176:        &cmd_rotate_window_entry,
1.17      nicm      177:        &cmd_run_shell_entry,
1.1       nicm      178:        &cmd_save_buffer_entry,
                    179:        &cmd_select_layout_entry,
                    180:        &cmd_select_pane_entry,
                    181:        &cmd_select_window_entry,
                    182:        &cmd_send_keys_entry,
                    183:        &cmd_send_prefix_entry,
                    184:        &cmd_set_buffer_entry,
1.13      nicm      185:        &cmd_set_environment_entry,
1.107     nicm      186:        &cmd_set_hook_entry,
1.1       nicm      187:        &cmd_set_option_entry,
                    188:        &cmd_set_window_option_entry,
                    189:        &cmd_show_buffer_entry,
1.13      nicm      190:        &cmd_show_environment_entry,
1.107     nicm      191:        &cmd_show_hooks_entry,
1.32      nicm      192:        &cmd_show_messages_entry,
1.1       nicm      193:        &cmd_show_options_entry,
                    194:        &cmd_show_window_options_entry,
                    195:        &cmd_source_file_entry,
                    196:        &cmd_split_window_entry,
                    197:        &cmd_start_server_entry,
                    198:        &cmd_suspend_client_entry,
                    199:        &cmd_swap_pane_entry,
                    200:        &cmd_swap_window_entry,
                    201:        &cmd_switch_client_entry,
                    202:        &cmd_unbind_key_entry,
                    203:        &cmd_unlink_window_entry,
1.82      nicm      204:        &cmd_wait_for_entry,
1.1       nicm      205:        NULL
                    206: };
                    207:
1.10      nicm      208: int
                    209: cmd_pack_argv(int argc, char **argv, char *buf, size_t len)
                    210: {
                    211:        size_t  arglen;
                    212:        int     i;
1.91      nicm      213:
                    214:        if (argc == 0)
                    215:                return (0);
1.10      nicm      216:
                    217:        *buf = '\0';
                    218:        for (i = 0; i < argc; i++) {
                    219:                if (strlcpy(buf, argv[i], len) >= len)
                    220:                        return (-1);
                    221:                arglen = strlen(argv[i]) + 1;
                    222:                buf += arglen;
                    223:                len -= arglen;
                    224:        }
                    225:
                    226:        return (0);
                    227: }
                    228:
                    229: int
                    230: cmd_unpack_argv(char *buf, size_t len, int argc, char ***argv)
                    231: {
                    232:        int     i;
                    233:        size_t  arglen;
                    234:
                    235:        if (argc == 0)
                    236:                return (0);
                    237:        *argv = xcalloc(argc, sizeof **argv);
                    238:
                    239:        buf[len - 1] = '\0';
                    240:        for (i = 0; i < argc; i++) {
                    241:                if (len == 0) {
                    242:                        cmd_free_argv(argc, *argv);
                    243:                        return (-1);
                    244:                }
                    245:
                    246:                arglen = strlen(buf) + 1;
                    247:                (*argv)[i] = xstrdup(buf);
                    248:                buf += arglen;
                    249:                len -= arglen;
                    250:        }
                    251:
                    252:        return (0);
1.46      nicm      253: }
                    254:
                    255: char **
1.93      nicm      256: cmd_copy_argv(int argc, char **argv)
1.46      nicm      257: {
                    258:        char    **new_argv;
                    259:        int       i;
                    260:
                    261:        if (argc == 0)
                    262:                return (NULL);
1.94      nicm      263:        new_argv = xcalloc(argc + 1, sizeof *new_argv);
1.46      nicm      264:        for (i = 0; i < argc; i++) {
                    265:                if (argv[i] != NULL)
                    266:                        new_argv[i] = xstrdup(argv[i]);
                    267:        }
                    268:        return (new_argv);
1.10      nicm      269: }
                    270:
                    271: void
                    272: cmd_free_argv(int argc, char **argv)
                    273: {
                    274:        int     i;
                    275:
                    276:        if (argc == 0)
1.35      nicm      277:                return;
1.68      nicm      278:        for (i = 0; i < argc; i++)
                    279:                free(argv[i]);
                    280:        free(argv);
1.94      nicm      281: }
                    282:
                    283: char *
                    284: cmd_stringify_argv(int argc, char **argv)
                    285: {
                    286:        char    *buf;
                    287:        int      i;
                    288:        size_t   len;
                    289:
                    290:        if (argc == 0)
                    291:                return (xstrdup(""));
                    292:
                    293:        len = 0;
                    294:        buf = NULL;
                    295:
                    296:        for (i = 0; i < argc; i++) {
                    297:                len += strlen(argv[i]) + 1;
1.98      nicm      298:                buf = xrealloc(buf, len);
1.94      nicm      299:
                    300:                if (i == 0)
                    301:                        *buf = '\0';
                    302:                else
                    303:                        strlcat(buf, " ", len);
                    304:                strlcat(buf, argv[i], len);
                    305:        }
                    306:        return (buf);
1.10      nicm      307: }
                    308:
1.135     nicm      309: static int
                    310: cmd_try_alias(int *argc, char ***argv)
                    311: {
                    312:        struct options_entry     *o;
                    313:        int                       old_argc = *argc, new_argc;
                    314:        char                    **old_argv = *argv, **new_argv;
                    315:        u_int                     size, idx;
                    316:        int                       i;
                    317:        size_t                    wanted;
                    318:        const char               *s, *cp = NULL;
                    319:
                    320:        o = options_get_only(global_options, "command-alias");
                    321:        if (o == NULL || options_array_size(o, &size) == -1 || size == 0)
                    322:                return (-1);
                    323:
                    324:        wanted = strlen(old_argv[0]);
                    325:        for (idx = 0; idx < size; idx++) {
                    326:                s = options_array_get(o, idx);
                    327:                if (s == NULL)
                    328:                        continue;
                    329:
                    330:                cp = strchr(s, '=');
                    331:                if (cp == NULL || (size_t)(cp - s) != wanted)
                    332:                        continue;
                    333:                if (strncmp(old_argv[0], s, wanted) == 0)
                    334:                        break;
                    335:        }
                    336:        if (idx == size)
                    337:                return (-1);
                    338:
                    339:        if (cmd_string_split(cp + 1, &new_argc, &new_argv) != 0)
                    340:                return (-1);
                    341:
                    342:        *argc = new_argc + old_argc - 1;
                    343:        *argv = xcalloc((*argc) + 1, sizeof **argv);
                    344:
                    345:        for (i = 0; i < new_argc; i++)
                    346:                (*argv)[i] = xstrdup(new_argv[i]);
                    347:        for (i = 1; i < old_argc; i++)
                    348:                (*argv)[new_argc + i - 1] = xstrdup(old_argv[i]);
                    349:
                    350:        log_debug("alias: %s=%s", old_argv[0], cp + 1);
                    351:        for (i = 0; i < *argc; i++)
                    352:                log_debug("alias: argv[%d] = %s", i, (*argv)[i]);
                    353:
                    354:        cmd_free_argv(new_argc, new_argv);
                    355:        return (0);
                    356: }
                    357:
1.1       nicm      358: struct cmd *
1.80      nicm      359: cmd_parse(int argc, char **argv, const char *file, u_int line, char **cause)
1.1       nicm      360: {
1.135     nicm      361:        const char              *name;
1.1       nicm      362:        const struct cmd_entry **entryp, *entry;
1.27      deraadt   363:        struct cmd              *cmd;
1.49      nicm      364:        struct args             *args;
1.1       nicm      365:        char                     s[BUFSIZ];
1.135     nicm      366:        int                      ambiguous, allocated = 0;
1.1       nicm      367:
                    368:        *cause = NULL;
1.2       nicm      369:        if (argc == 0) {
                    370:                xasprintf(cause, "no command");
1.1       nicm      371:                return (NULL);
1.2       nicm      372:        }
1.135     nicm      373:        name = argv[0];
1.1       nicm      374:
1.135     nicm      375: retry:
                    376:        ambiguous = 0;
1.1       nicm      377:        entry = NULL;
                    378:        for (entryp = cmd_table; *entryp != NULL; entryp++) {
                    379:                if ((*entryp)->alias != NULL &&
                    380:                    strcmp((*entryp)->alias, argv[0]) == 0) {
1.3       nicm      381:                        ambiguous = 0;
1.1       nicm      382:                        entry = *entryp;
                    383:                        break;
                    384:                }
                    385:
                    386:                if (strncmp((*entryp)->name, argv[0], strlen(argv[0])) != 0)
                    387:                        continue;
                    388:                if (entry != NULL)
1.3       nicm      389:                        ambiguous = 1;
1.1       nicm      390:                entry = *entryp;
                    391:
                    392:                /* Bail now if an exact match. */
                    393:                if (strcmp(entry->name, argv[0]) == 0)
                    394:                        break;
                    395:        }
1.135     nicm      396:        if ((ambiguous || entry == NULL) &&
                    397:            server_proc != NULL &&
                    398:            !allocated &&
                    399:            cmd_try_alias(&argc, &argv) == 0) {
                    400:                allocated = 1;
                    401:                goto retry;
                    402:        }
1.3       nicm      403:        if (ambiguous)
                    404:                goto ambiguous;
1.1       nicm      405:        if (entry == NULL) {
1.135     nicm      406:                xasprintf(cause, "unknown command: %s", name);
1.1       nicm      407:                return (NULL);
                    408:        }
                    409:
1.117     nicm      410:        args = args_parse(entry->args.template, argc, argv);
1.49      nicm      411:        if (args == NULL)
                    412:                goto usage;
1.117     nicm      413:        if (entry->args.lower != -1 && args->argc < entry->args.lower)
1.49      nicm      414:                goto usage;
1.117     nicm      415:        if (entry->args.upper != -1 && args->argc > entry->args.upper)
1.49      nicm      416:                goto usage;
1.1       nicm      417:
1.80      nicm      418:        cmd = xcalloc(1, sizeof *cmd);
1.1       nicm      419:        cmd->entry = entry;
1.49      nicm      420:        cmd->args = args;
1.80      nicm      421:
                    422:        if (file != NULL)
                    423:                cmd->file = xstrdup(file);
                    424:        cmd->line = line;
                    425:
1.135     nicm      426:        if (allocated)
                    427:                cmd_free_argv(argc, argv);
1.1       nicm      428:        return (cmd);
                    429:
                    430: ambiguous:
                    431:        *s = '\0';
                    432:        for (entryp = cmd_table; *entryp != NULL; entryp++) {
                    433:                if (strncmp((*entryp)->name, argv[0], strlen(argv[0])) != 0)
                    434:                        continue;
                    435:                if (strlcat(s, (*entryp)->name, sizeof s) >= sizeof s)
                    436:                        break;
                    437:                if (strlcat(s, ", ", sizeof s) >= sizeof s)
                    438:                        break;
                    439:        }
                    440:        s[strlen(s) - 2] = '\0';
1.135     nicm      441:        xasprintf(cause, "ambiguous command: %s, could be: %s", name, s);
1.1       nicm      442:        return (NULL);
                    443:
                    444: usage:
1.49      nicm      445:        if (args != NULL)
                    446:                args_free(args);
1.1       nicm      447:        xasprintf(cause, "usage: %s %s", entry->name, entry->usage);
                    448:        return (NULL);
                    449: }
                    450:
1.106     nicm      451: char *
                    452: cmd_print(struct cmd *cmd)
1.1       nicm      453: {
1.106     nicm      454:        char    *out, *s;
                    455:
                    456:        s = args_print(cmd->args);
                    457:        if (*s != '\0')
                    458:                xasprintf(&out, "%s %s", cmd->entry->name, s);
                    459:        else
                    460:                out = xstrdup(cmd->entry->name);
                    461:        free(s);
1.49      nicm      462:
1.106     nicm      463:        return (out);
1.1       nicm      464: }
                    465:
1.99      nicm      466: /* Adjust current mouse position for a pane. */
                    467: int
                    468: cmd_mouse_at(struct window_pane *wp, struct mouse_event *m, u_int *xp,
                    469:     u_int *yp, int last)
                    470: {
                    471:        u_int   x, y;
                    472:
                    473:        if (last) {
                    474:                x = m->lx;
                    475:                y = m->ly;
                    476:        } else {
                    477:                x = m->x;
                    478:                y = m->y;
                    479:        }
                    480:
                    481:        if (m->statusat == 0 && y > 0)
                    482:                y--;
                    483:        else if (m->statusat > 0 && y >= (u_int)m->statusat)
                    484:                y = m->statusat - 1;
                    485:
                    486:        if (x < wp->xoff || x >= wp->xoff + wp->sx)
                    487:                return (-1);
                    488:        if (y < wp->yoff || y >= wp->yoff + wp->sy)
                    489:                return (-1);
                    490:
1.123     nicm      491:        if (xp != NULL)
                    492:                *xp = x - wp->xoff;
                    493:        if (yp != NULL)
                    494:                *yp = y - wp->yoff;
1.99      nicm      495:        return (0);
                    496: }
                    497:
                    498: /* Get current mouse window if any. */
                    499: struct winlink *
                    500: cmd_mouse_window(struct mouse_event *m, struct session **sp)
                    501: {
                    502:        struct session  *s;
                    503:        struct window   *w;
                    504:
                    505:        if (!m->valid || m->s == -1 || m->w == -1)
                    506:                return (NULL);
                    507:        if ((s = session_find_by_id(m->s)) == NULL)
                    508:                return (NULL);
                    509:        if ((w = window_find_by_id(m->w)) == NULL)
                    510:                return (NULL);
                    511:
                    512:        if (sp != NULL)
                    513:                *sp = s;
                    514:        return (winlink_find_by_window(&s->windows, w));
                    515: }
                    516:
                    517: /* Get current mouse pane if any. */
                    518: struct window_pane *
1.104     nicm      519: cmd_mouse_pane(struct mouse_event *m, struct session **sp,
                    520:     struct winlink **wlp)
1.99      nicm      521: {
                    522:        struct winlink          *wl;
                    523:        struct window_pane      *wp;
                    524:
                    525:        if ((wl = cmd_mouse_window(m, sp)) == NULL)
                    526:                return (NULL);
                    527:        if ((wp = window_pane_find_by_id(m->wp)) == NULL)
                    528:                return (NULL);
                    529:        if (!window_has_pane(wl->window, wp))
                    530:                return (NULL);
                    531:
                    532:        if (wlp != NULL)
                    533:                *wlp = wl;
1.43      nicm      534:        return (wp);
1.15      nicm      535: }
                    536:
                    537: /* Replace the first %% or %idx in template by s. */
                    538: char *
1.75      nicm      539: cmd_template_replace(const char *template, const char *s, int idx)
1.15      nicm      540: {
1.75      nicm      541:        char             ch, *buf;
1.131     nicm      542:        const char      *ptr, *cp, quote[] = "\"\\$";
1.127     nicm      543:        int              replaced, quoted;
1.84      nicm      544:        size_t           len;
1.15      nicm      545:
1.76      nicm      546:        if (strchr(template, '%') == NULL)
1.15      nicm      547:                return (xstrdup(template));
                    548:
                    549:        buf = xmalloc(1);
                    550:        *buf = '\0';
                    551:        len = 0;
                    552:        replaced = 0;
                    553:
                    554:        ptr = template;
                    555:        while (*ptr != '\0') {
                    556:                switch (ch = *ptr++) {
                    557:                case '%':
                    558:                        if (*ptr < '1' || *ptr > '9' || *ptr - '0' != idx) {
                    559:                                if (*ptr != '%' || replaced)
                    560:                                        break;
                    561:                                replaced = 1;
                    562:                        }
                    563:                        ptr++;
                    564:
1.127     nicm      565:                        quoted = (*ptr == '%');
                    566:                        if (quoted)
                    567:                                ptr++;
                    568:
1.132     nicm      569:                        buf = xrealloc(buf, len + (strlen(s) * 3) + 1);
1.127     nicm      570:                        for (cp = s; *cp != '\0'; cp++) {
1.131     nicm      571:                                if (quoted && strchr(quote, *cp) != NULL)
1.127     nicm      572:                                        buf[len++] = '\\';
1.132     nicm      573:                                if (quoted && *cp == ';') {
                    574:                                        buf[len++] = '\\';
                    575:                                        buf[len++] = '\\';
                    576:                                }
1.127     nicm      577:                                buf[len++] = *cp;
                    578:                        }
                    579:                        buf[len] = '\0';
1.15      nicm      580:                        continue;
                    581:                }
1.98      nicm      582:                buf = xrealloc(buf, len + 2);
1.15      nicm      583:                buf[len++] = ch;
                    584:                buf[len] = '\0';
                    585:        }
                    586:
                    587:        return (buf);
1.1       nicm      588: }