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

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