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

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