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

1.145   ! nicm        1: /* $OpenBSD: cmd.c,v 1.144 2019/04/25 19:36:59 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:                cp = strchr(ov->string, '=');
                    339:                if (cp != NULL &&
                    340:                    (size_t)(cp - ov->string) == wanted &&
                    341:                    strncmp(old_argv[0], ov->string, wanted) == 0)
                    342:                        break;
1.142     nicm      343:                a = options_array_next(a);
1.135     nicm      344:        }
1.142     nicm      345:        if (a == NULL)
1.135     nicm      346:                return (-1);
                    347:
                    348:        if (cmd_string_split(cp + 1, &new_argc, &new_argv) != 0)
                    349:                return (-1);
                    350:
                    351:        *argc = new_argc + old_argc - 1;
                    352:        *argv = xcalloc((*argc) + 1, sizeof **argv);
                    353:
                    354:        for (i = 0; i < new_argc; i++)
                    355:                (*argv)[i] = xstrdup(new_argv[i]);
                    356:        for (i = 1; i < old_argc; i++)
                    357:                (*argv)[new_argc + i - 1] = xstrdup(old_argv[i]);
                    358:
                    359:        log_debug("alias: %s=%s", old_argv[0], cp + 1);
                    360:        for (i = 0; i < *argc; i++)
                    361:                log_debug("alias: argv[%d] = %s", i, (*argv)[i]);
                    362:
                    363:        cmd_free_argv(new_argc, new_argv);
                    364:        return (0);
                    365: }
                    366:
1.1       nicm      367: struct cmd *
1.80      nicm      368: cmd_parse(int argc, char **argv, const char *file, u_int line, char **cause)
1.1       nicm      369: {
1.135     nicm      370:        const char              *name;
1.1       nicm      371:        const struct cmd_entry **entryp, *entry;
1.27      deraadt   372:        struct cmd              *cmd;
1.49      nicm      373:        struct args             *args;
1.1       nicm      374:        char                     s[BUFSIZ];
1.135     nicm      375:        int                      ambiguous, allocated = 0;
1.1       nicm      376:
                    377:        *cause = NULL;
1.2       nicm      378:        if (argc == 0) {
                    379:                xasprintf(cause, "no command");
1.1       nicm      380:                return (NULL);
1.2       nicm      381:        }
1.135     nicm      382:        name = argv[0];
1.1       nicm      383:
1.135     nicm      384: retry:
                    385:        ambiguous = 0;
1.1       nicm      386:        entry = NULL;
                    387:        for (entryp = cmd_table; *entryp != NULL; entryp++) {
                    388:                if ((*entryp)->alias != NULL &&
                    389:                    strcmp((*entryp)->alias, argv[0]) == 0) {
1.3       nicm      390:                        ambiguous = 0;
1.1       nicm      391:                        entry = *entryp;
                    392:                        break;
                    393:                }
                    394:
                    395:                if (strncmp((*entryp)->name, argv[0], strlen(argv[0])) != 0)
                    396:                        continue;
                    397:                if (entry != NULL)
1.3       nicm      398:                        ambiguous = 1;
1.1       nicm      399:                entry = *entryp;
                    400:
                    401:                /* Bail now if an exact match. */
                    402:                if (strcmp(entry->name, argv[0]) == 0)
                    403:                        break;
                    404:        }
1.135     nicm      405:        if ((ambiguous || entry == NULL) &&
                    406:            server_proc != NULL &&
                    407:            !allocated &&
                    408:            cmd_try_alias(&argc, &argv) == 0) {
                    409:                allocated = 1;
                    410:                goto retry;
                    411:        }
1.3       nicm      412:        if (ambiguous)
                    413:                goto ambiguous;
1.1       nicm      414:        if (entry == NULL) {
1.135     nicm      415:                xasprintf(cause, "unknown command: %s", name);
1.1       nicm      416:                return (NULL);
                    417:        }
1.140     nicm      418:        cmd_log_argv(argc, argv, entry->name);
1.1       nicm      419:
1.117     nicm      420:        args = args_parse(entry->args.template, argc, argv);
1.49      nicm      421:        if (args == NULL)
                    422:                goto usage;
1.117     nicm      423:        if (entry->args.lower != -1 && args->argc < entry->args.lower)
1.49      nicm      424:                goto usage;
1.117     nicm      425:        if (entry->args.upper != -1 && args->argc > entry->args.upper)
1.49      nicm      426:                goto usage;
1.1       nicm      427:
1.80      nicm      428:        cmd = xcalloc(1, sizeof *cmd);
1.1       nicm      429:        cmd->entry = entry;
1.49      nicm      430:        cmd->args = args;
1.80      nicm      431:
                    432:        if (file != NULL)
                    433:                cmd->file = xstrdup(file);
                    434:        cmd->line = line;
                    435:
1.135     nicm      436:        if (allocated)
                    437:                cmd_free_argv(argc, argv);
1.1       nicm      438:        return (cmd);
                    439:
                    440: ambiguous:
                    441:        *s = '\0';
                    442:        for (entryp = cmd_table; *entryp != NULL; entryp++) {
                    443:                if (strncmp((*entryp)->name, argv[0], strlen(argv[0])) != 0)
                    444:                        continue;
                    445:                if (strlcat(s, (*entryp)->name, sizeof s) >= sizeof s)
                    446:                        break;
                    447:                if (strlcat(s, ", ", sizeof s) >= sizeof s)
                    448:                        break;
                    449:        }
                    450:        s[strlen(s) - 2] = '\0';
1.135     nicm      451:        xasprintf(cause, "ambiguous command: %s, could be: %s", name, s);
1.1       nicm      452:        return (NULL);
                    453:
                    454: usage:
1.49      nicm      455:        if (args != NULL)
                    456:                args_free(args);
1.1       nicm      457:        xasprintf(cause, "usage: %s %s", entry->name, entry->usage);
                    458:        return (NULL);
                    459: }
                    460:
1.106     nicm      461: char *
                    462: cmd_print(struct cmd *cmd)
1.1       nicm      463: {
1.106     nicm      464:        char    *out, *s;
                    465:
                    466:        s = args_print(cmd->args);
                    467:        if (*s != '\0')
                    468:                xasprintf(&out, "%s %s", cmd->entry->name, s);
                    469:        else
                    470:                out = xstrdup(cmd->entry->name);
                    471:        free(s);
1.49      nicm      472:
1.106     nicm      473:        return (out);
1.1       nicm      474: }
                    475:
1.99      nicm      476: /* Adjust current mouse position for a pane. */
                    477: int
                    478: cmd_mouse_at(struct window_pane *wp, struct mouse_event *m, u_int *xp,
                    479:     u_int *yp, int last)
                    480: {
                    481:        u_int   x, y;
                    482:
                    483:        if (last) {
1.145   ! nicm      484:                x = m->lx + m->ox;
        !           485:                y = m->ly + m->oy;
1.99      nicm      486:        } else {
                    487:                x = m->x;
                    488:                y = m->y;
                    489:        }
1.145   ! nicm      490:        log_debug("%s: x=%u, y=%u%s", __func__, x, y, last ? " (last)" : "");
1.99      nicm      491:
                    492:        if (m->statusat == 0 && y > 0)
                    493:                y--;
                    494:
                    495:        if (x < wp->xoff || x >= wp->xoff + wp->sx)
                    496:                return (-1);
                    497:        if (y < wp->yoff || y >= wp->yoff + wp->sy)
                    498:                return (-1);
                    499:
1.123     nicm      500:        if (xp != NULL)
                    501:                *xp = x - wp->xoff;
                    502:        if (yp != NULL)
                    503:                *yp = y - wp->yoff;
1.99      nicm      504:        return (0);
                    505: }
                    506:
                    507: /* Get current mouse window if any. */
                    508: struct winlink *
                    509: cmd_mouse_window(struct mouse_event *m, struct session **sp)
                    510: {
                    511:        struct session  *s;
                    512:        struct window   *w;
                    513:
                    514:        if (!m->valid || m->s == -1 || m->w == -1)
                    515:                return (NULL);
                    516:        if ((s = session_find_by_id(m->s)) == NULL)
                    517:                return (NULL);
                    518:        if ((w = window_find_by_id(m->w)) == NULL)
                    519:                return (NULL);
                    520:
                    521:        if (sp != NULL)
                    522:                *sp = s;
                    523:        return (winlink_find_by_window(&s->windows, w));
                    524: }
                    525:
                    526: /* Get current mouse pane if any. */
                    527: struct window_pane *
1.104     nicm      528: cmd_mouse_pane(struct mouse_event *m, struct session **sp,
                    529:     struct winlink **wlp)
1.99      nicm      530: {
                    531:        struct winlink          *wl;
                    532:        struct window_pane      *wp;
                    533:
                    534:        if ((wl = cmd_mouse_window(m, sp)) == NULL)
                    535:                return (NULL);
                    536:        if ((wp = window_pane_find_by_id(m->wp)) == NULL)
                    537:                return (NULL);
                    538:        if (!window_has_pane(wl->window, wp))
                    539:                return (NULL);
                    540:
                    541:        if (wlp != NULL)
                    542:                *wlp = wl;
1.43      nicm      543:        return (wp);
1.15      nicm      544: }
                    545:
                    546: /* Replace the first %% or %idx in template by s. */
                    547: char *
1.75      nicm      548: cmd_template_replace(const char *template, const char *s, int idx)
1.15      nicm      549: {
1.75      nicm      550:        char             ch, *buf;
1.131     nicm      551:        const char      *ptr, *cp, quote[] = "\"\\$";
1.127     nicm      552:        int              replaced, quoted;
1.84      nicm      553:        size_t           len;
1.15      nicm      554:
1.76      nicm      555:        if (strchr(template, '%') == NULL)
1.15      nicm      556:                return (xstrdup(template));
                    557:
                    558:        buf = xmalloc(1);
                    559:        *buf = '\0';
                    560:        len = 0;
                    561:        replaced = 0;
                    562:
                    563:        ptr = template;
                    564:        while (*ptr != '\0') {
                    565:                switch (ch = *ptr++) {
                    566:                case '%':
                    567:                        if (*ptr < '1' || *ptr > '9' || *ptr - '0' != idx) {
                    568:                                if (*ptr != '%' || replaced)
                    569:                                        break;
                    570:                                replaced = 1;
                    571:                        }
                    572:                        ptr++;
                    573:
1.127     nicm      574:                        quoted = (*ptr == '%');
                    575:                        if (quoted)
                    576:                                ptr++;
                    577:
1.132     nicm      578:                        buf = xrealloc(buf, len + (strlen(s) * 3) + 1);
1.127     nicm      579:                        for (cp = s; *cp != '\0'; cp++) {
1.131     nicm      580:                                if (quoted && strchr(quote, *cp) != NULL)
1.127     nicm      581:                                        buf[len++] = '\\';
1.132     nicm      582:                                if (quoted && *cp == ';') {
                    583:                                        buf[len++] = '\\';
                    584:                                        buf[len++] = '\\';
                    585:                                }
1.127     nicm      586:                                buf[len++] = *cp;
                    587:                        }
                    588:                        buf[len] = '\0';
1.15      nicm      589:                        continue;
                    590:                }
1.98      nicm      591:                buf = xrealloc(buf, len + 2);
1.15      nicm      592:                buf[len++] = ch;
                    593:                buf[len] = '\0';
                    594:        }
                    595:
                    596:        return (buf);
1.1       nicm      597: }