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

1.175   ! nicm        1: /* $OpenBSD: cmd.c,v 1.174 2022/05/30 12:48:57 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;
1.164     nicm       39: extern const struct cmd_entry cmd_clear_prompt_history_entry;
1.105     nicm       40: extern const struct cmd_entry cmd_clock_mode_entry;
                     41: extern const struct cmd_entry cmd_command_prompt_entry;
                     42: extern const struct cmd_entry cmd_confirm_before_entry;
                     43: extern const struct cmd_entry cmd_copy_mode_entry;
1.161     nicm       44: extern const struct cmd_entry cmd_customize_mode_entry;
1.105     nicm       45: extern const struct cmd_entry cmd_delete_buffer_entry;
                     46: extern const struct cmd_entry cmd_detach_client_entry;
1.148     nicm       47: extern const struct cmd_entry cmd_display_menu_entry;
1.105     nicm       48: extern const struct cmd_entry cmd_display_message_entry;
1.157     nicm       49: extern const struct cmd_entry cmd_display_popup_entry;
1.105     nicm       50: extern const struct cmd_entry cmd_display_panes_entry;
                     51: extern const struct cmd_entry cmd_down_pane_entry;
                     52: extern const struct cmd_entry cmd_find_window_entry;
                     53: extern const struct cmd_entry cmd_has_session_entry;
                     54: extern const struct cmd_entry cmd_if_shell_entry;
                     55: extern const struct cmd_entry cmd_join_pane_entry;
                     56: extern const struct cmd_entry cmd_kill_pane_entry;
                     57: extern const struct cmd_entry cmd_kill_server_entry;
                     58: extern const struct cmd_entry cmd_kill_session_entry;
                     59: extern const struct cmd_entry cmd_kill_window_entry;
                     60: extern const struct cmd_entry cmd_last_pane_entry;
                     61: extern const struct cmd_entry cmd_last_window_entry;
                     62: extern const struct cmd_entry cmd_link_window_entry;
                     63: extern const struct cmd_entry cmd_list_buffers_entry;
                     64: extern const struct cmd_entry cmd_list_clients_entry;
                     65: extern const struct cmd_entry cmd_list_commands_entry;
                     66: extern const struct cmd_entry cmd_list_keys_entry;
                     67: extern const struct cmd_entry cmd_list_panes_entry;
                     68: extern const struct cmd_entry cmd_list_sessions_entry;
                     69: extern const struct cmd_entry cmd_list_windows_entry;
                     70: extern const struct cmd_entry cmd_load_buffer_entry;
                     71: extern const struct cmd_entry cmd_lock_client_entry;
                     72: extern const struct cmd_entry cmd_lock_server_entry;
                     73: extern const struct cmd_entry cmd_lock_session_entry;
                     74: extern const struct cmd_entry cmd_move_pane_entry;
                     75: extern const struct cmd_entry cmd_move_window_entry;
                     76: extern const struct cmd_entry cmd_new_session_entry;
                     77: extern const struct cmd_entry cmd_new_window_entry;
                     78: extern const struct cmd_entry cmd_next_layout_entry;
                     79: extern const struct cmd_entry cmd_next_window_entry;
                     80: extern const struct cmd_entry cmd_paste_buffer_entry;
                     81: extern const struct cmd_entry cmd_pipe_pane_entry;
                     82: extern const struct cmd_entry cmd_previous_layout_entry;
                     83: extern const struct cmd_entry cmd_previous_window_entry;
                     84: extern const struct cmd_entry cmd_refresh_client_entry;
                     85: extern const struct cmd_entry cmd_rename_session_entry;
                     86: extern const struct cmd_entry cmd_rename_window_entry;
                     87: extern const struct cmd_entry cmd_resize_pane_entry;
1.141     nicm       88: extern const struct cmd_entry cmd_resize_window_entry;
1.105     nicm       89: extern const struct cmd_entry cmd_respawn_pane_entry;
                     90: extern const struct cmd_entry cmd_respawn_window_entry;
                     91: extern const struct cmd_entry cmd_rotate_window_entry;
                     92: extern const struct cmd_entry cmd_run_shell_entry;
                     93: extern const struct cmd_entry cmd_save_buffer_entry;
                     94: extern const struct cmd_entry cmd_select_layout_entry;
                     95: extern const struct cmd_entry cmd_select_pane_entry;
                     96: extern const struct cmd_entry cmd_select_window_entry;
                     97: extern const struct cmd_entry cmd_send_keys_entry;
                     98: extern const struct cmd_entry cmd_send_prefix_entry;
1.174     nicm       99: extern const struct cmd_entry cmd_server_access_entry;
1.105     nicm      100: extern const struct cmd_entry cmd_set_buffer_entry;
                    101: extern const struct cmd_entry cmd_set_environment_entry;
1.107     nicm      102: extern const struct cmd_entry cmd_set_hook_entry;
1.105     nicm      103: extern const struct cmd_entry cmd_set_option_entry;
                    104: extern const struct cmd_entry cmd_set_window_option_entry;
                    105: extern const struct cmd_entry cmd_show_buffer_entry;
                    106: extern const struct cmd_entry cmd_show_environment_entry;
1.107     nicm      107: extern const struct cmd_entry cmd_show_hooks_entry;
1.105     nicm      108: extern const struct cmd_entry cmd_show_messages_entry;
                    109: extern const struct cmd_entry cmd_show_options_entry;
1.164     nicm      110: extern const struct cmd_entry cmd_show_prompt_history_entry;
1.105     nicm      111: extern const struct cmd_entry cmd_show_window_options_entry;
                    112: extern const struct cmd_entry cmd_source_file_entry;
                    113: extern const struct cmd_entry cmd_split_window_entry;
                    114: extern const struct cmd_entry cmd_start_server_entry;
                    115: extern const struct cmd_entry cmd_suspend_client_entry;
                    116: extern const struct cmd_entry cmd_swap_pane_entry;
                    117: extern const struct cmd_entry cmd_swap_window_entry;
                    118: extern const struct cmd_entry cmd_switch_client_entry;
                    119: extern const struct cmd_entry cmd_unbind_key_entry;
                    120: extern const struct cmd_entry cmd_unlink_window_entry;
                    121: extern const struct cmd_entry cmd_up_pane_entry;
                    122: extern const struct cmd_entry cmd_wait_for_entry;
1.1       nicm      123:
                    124: const struct cmd_entry *cmd_table[] = {
                    125:        &cmd_attach_session_entry,
                    126:        &cmd_bind_key_entry,
                    127:        &cmd_break_pane_entry,
1.34      nicm      128:        &cmd_capture_pane_entry,
1.42      nicm      129:        &cmd_choose_buffer_entry,
1.15      nicm      130:        &cmd_choose_client_entry,
1.67      nicm      131:        &cmd_choose_tree_entry,
1.1       nicm      132:        &cmd_clear_history_entry,
1.164     nicm      133:        &cmd_clear_prompt_history_entry,
1.1       nicm      134:        &cmd_clock_mode_entry,
                    135:        &cmd_command_prompt_entry,
                    136:        &cmd_confirm_before_entry,
                    137:        &cmd_copy_mode_entry,
1.161     nicm      138:        &cmd_customize_mode_entry,
1.1       nicm      139:        &cmd_delete_buffer_entry,
                    140:        &cmd_detach_client_entry,
1.148     nicm      141:        &cmd_display_menu_entry,
1.7       nicm      142:        &cmd_display_message_entry,
1.157     nicm      143:        &cmd_display_popup_entry,
1.16      nicm      144:        &cmd_display_panes_entry,
1.1       nicm      145:        &cmd_find_window_entry,
                    146:        &cmd_has_session_entry,
1.4       nicm      147:        &cmd_if_shell_entry,
1.37      nicm      148:        &cmd_join_pane_entry,
1.1       nicm      149:        &cmd_kill_pane_entry,
                    150:        &cmd_kill_server_entry,
                    151:        &cmd_kill_session_entry,
                    152:        &cmd_kill_window_entry,
1.45      nicm      153:        &cmd_last_pane_entry,
1.1       nicm      154:        &cmd_last_window_entry,
                    155:        &cmd_link_window_entry,
                    156:        &cmd_list_buffers_entry,
                    157:        &cmd_list_clients_entry,
                    158:        &cmd_list_commands_entry,
                    159:        &cmd_list_keys_entry,
1.23      nicm      160:        &cmd_list_panes_entry,
1.1       nicm      161:        &cmd_list_sessions_entry,
                    162:        &cmd_list_windows_entry,
                    163:        &cmd_load_buffer_entry,
1.19      nicm      164:        &cmd_lock_client_entry,
1.1       nicm      165:        &cmd_lock_server_entry,
1.19      nicm      166:        &cmd_lock_session_entry,
1.63      nicm      167:        &cmd_move_pane_entry,
1.1       nicm      168:        &cmd_move_window_entry,
                    169:        &cmd_new_session_entry,
                    170:        &cmd_new_window_entry,
                    171:        &cmd_next_layout_entry,
                    172:        &cmd_next_window_entry,
                    173:        &cmd_paste_buffer_entry,
1.24      nicm      174:        &cmd_pipe_pane_entry,
1.1       nicm      175:        &cmd_previous_layout_entry,
                    176:        &cmd_previous_window_entry,
                    177:        &cmd_refresh_client_entry,
                    178:        &cmd_rename_session_entry,
                    179:        &cmd_rename_window_entry,
                    180:        &cmd_resize_pane_entry,
1.141     nicm      181:        &cmd_resize_window_entry,
1.56      nicm      182:        &cmd_respawn_pane_entry,
1.1       nicm      183:        &cmd_respawn_window_entry,
                    184:        &cmd_rotate_window_entry,
1.17      nicm      185:        &cmd_run_shell_entry,
1.1       nicm      186:        &cmd_save_buffer_entry,
                    187:        &cmd_select_layout_entry,
                    188:        &cmd_select_pane_entry,
                    189:        &cmd_select_window_entry,
                    190:        &cmd_send_keys_entry,
                    191:        &cmd_send_prefix_entry,
1.174     nicm      192:        &cmd_server_access_entry,
1.1       nicm      193:        &cmd_set_buffer_entry,
1.13      nicm      194:        &cmd_set_environment_entry,
1.107     nicm      195:        &cmd_set_hook_entry,
1.1       nicm      196:        &cmd_set_option_entry,
                    197:        &cmd_set_window_option_entry,
                    198:        &cmd_show_buffer_entry,
1.13      nicm      199:        &cmd_show_environment_entry,
1.107     nicm      200:        &cmd_show_hooks_entry,
1.32      nicm      201:        &cmd_show_messages_entry,
1.1       nicm      202:        &cmd_show_options_entry,
1.164     nicm      203:        &cmd_show_prompt_history_entry,
1.1       nicm      204:        &cmd_show_window_options_entry,
                    205:        &cmd_source_file_entry,
                    206:        &cmd_split_window_entry,
                    207:        &cmd_start_server_entry,
                    208:        &cmd_suspend_client_entry,
                    209:        &cmd_swap_pane_entry,
                    210:        &cmd_swap_window_entry,
                    211:        &cmd_switch_client_entry,
                    212:        &cmd_unbind_key_entry,
                    213:        &cmd_unlink_window_entry,
1.82      nicm      214:        &cmd_wait_for_entry,
1.1       nicm      215:        NULL
                    216: };
                    217:
1.158     nicm      218: /* Instance of a command. */
                    219: struct cmd {
                    220:        const struct cmd_entry   *entry;
                    221:        struct args              *args;
                    222:        u_int                     group;
                    223:
                    224:        char                     *file;
                    225:        u_int                     line;
                    226:
                    227:        TAILQ_ENTRY(cmd)          qentry;
                    228: };
                    229: TAILQ_HEAD(cmds, cmd);
                    230:
                    231: /* Next group number for new command list. */
1.151     nicm      232: static u_int cmd_list_next_group = 1;
                    233:
1.158     nicm      234: /* Log an argument vector. */
1.150     nicm      235: void printflike(3, 4)
                    236: cmd_log_argv(int argc, char **argv, const char *fmt, ...)
1.140     nicm      237: {
1.150     nicm      238:        char    *prefix;
                    239:        va_list  ap;
                    240:        int      i;
                    241:
                    242:        va_start(ap, fmt);
                    243:        xvasprintf(&prefix, fmt, ap);
                    244:        va_end(ap);
1.140     nicm      245:
                    246:        for (i = 0; i < argc; i++)
                    247:                log_debug("%s: argv[%d]=%s", prefix, i, argv[i]);
1.150     nicm      248:        free(prefix);
1.140     nicm      249: }
                    250:
1.158     nicm      251: /* Prepend to an argument vector. */
1.149     nicm      252: void
1.165     nicm      253: cmd_prepend_argv(int *argc, char ***argv, const char *arg)
1.149     nicm      254: {
                    255:        char    **new_argv;
                    256:        int       i;
                    257:
                    258:        new_argv = xreallocarray(NULL, (*argc) + 1, sizeof *new_argv);
                    259:        new_argv[0] = xstrdup(arg);
                    260:        for (i = 0; i < *argc; i++)
                    261:                new_argv[1 + i] = (*argv)[i];
                    262:
                    263:        free(*argv);
                    264:        *argv = new_argv;
                    265:        (*argc)++;
                    266: }
                    267:
1.158     nicm      268: /* Append to an argument vector. */
1.149     nicm      269: void
1.165     nicm      270: cmd_append_argv(int *argc, char ***argv, const char *arg)
1.149     nicm      271: {
                    272:        *argv = xreallocarray(*argv, (*argc) + 1, sizeof **argv);
                    273:        (*argv)[(*argc)++] = xstrdup(arg);
                    274: }
                    275:
1.158     nicm      276: /* Pack an argument vector up into a buffer. */
1.10      nicm      277: int
                    278: cmd_pack_argv(int argc, char **argv, char *buf, size_t len)
                    279: {
                    280:        size_t  arglen;
                    281:        int     i;
1.91      nicm      282:
                    283:        if (argc == 0)
                    284:                return (0);
1.150     nicm      285:        cmd_log_argv(argc, argv, "%s", __func__);
1.10      nicm      286:
                    287:        *buf = '\0';
                    288:        for (i = 0; i < argc; i++) {
                    289:                if (strlcpy(buf, argv[i], len) >= len)
                    290:                        return (-1);
                    291:                arglen = strlen(argv[i]) + 1;
                    292:                buf += arglen;
                    293:                len -= arglen;
                    294:        }
                    295:
                    296:        return (0);
                    297: }
                    298:
1.158     nicm      299: /* Unpack an argument vector from a packed buffer. */
1.10      nicm      300: int
                    301: cmd_unpack_argv(char *buf, size_t len, int argc, char ***argv)
                    302: {
                    303:        int     i;
                    304:        size_t  arglen;
                    305:
                    306:        if (argc == 0)
                    307:                return (0);
                    308:        *argv = xcalloc(argc, sizeof **argv);
                    309:
                    310:        buf[len - 1] = '\0';
                    311:        for (i = 0; i < argc; i++) {
                    312:                if (len == 0) {
                    313:                        cmd_free_argv(argc, *argv);
                    314:                        return (-1);
                    315:                }
                    316:
                    317:                arglen = strlen(buf) + 1;
                    318:                (*argv)[i] = xstrdup(buf);
1.140     nicm      319:
1.10      nicm      320:                buf += arglen;
                    321:                len -= arglen;
                    322:        }
1.150     nicm      323:        cmd_log_argv(argc, *argv, "%s", __func__);
1.10      nicm      324:
                    325:        return (0);
1.46      nicm      326: }
                    327:
1.158     nicm      328: /* Copy an argument vector, ensuring it is terminated by NULL. */
1.46      nicm      329: char **
1.93      nicm      330: cmd_copy_argv(int argc, char **argv)
1.46      nicm      331: {
                    332:        char    **new_argv;
                    333:        int       i;
                    334:
                    335:        if (argc == 0)
                    336:                return (NULL);
1.94      nicm      337:        new_argv = xcalloc(argc + 1, sizeof *new_argv);
1.46      nicm      338:        for (i = 0; i < argc; i++) {
                    339:                if (argv[i] != NULL)
                    340:                        new_argv[i] = xstrdup(argv[i]);
                    341:        }
                    342:        return (new_argv);
1.10      nicm      343: }
                    344:
1.158     nicm      345: /* Free an argument vector. */
1.10      nicm      346: void
                    347: cmd_free_argv(int argc, char **argv)
                    348: {
                    349:        int     i;
                    350:
                    351:        if (argc == 0)
1.35      nicm      352:                return;
1.68      nicm      353:        for (i = 0; i < argc; i++)
                    354:                free(argv[i]);
                    355:        free(argv);
1.94      nicm      356: }
                    357:
1.158     nicm      358: /* Convert argument vector to a string. */
1.94      nicm      359: char *
                    360: cmd_stringify_argv(int argc, char **argv)
                    361: {
1.162     nicm      362:        char    *buf = NULL, *s;
                    363:        size_t   len = 0;
1.94      nicm      364:        int      i;
                    365:
                    366:        if (argc == 0)
                    367:                return (xstrdup(""));
                    368:
1.162     nicm      369:        for (i = 0; i < argc; i++) {
                    370:                s = args_escape(argv[i]);
                    371:                log_debug("%s: %u %s = %s", __func__, i, argv[i], s);
1.94      nicm      372:
1.162     nicm      373:                len += strlen(s) + 1;
1.98      nicm      374:                buf = xrealloc(buf, len);
1.94      nicm      375:
                    376:                if (i == 0)
                    377:                        *buf = '\0';
                    378:                else
                    379:                        strlcat(buf, " ", len);
1.162     nicm      380:                strlcat(buf, s, len);
                    381:
                    382:                free(s);
1.94      nicm      383:        }
                    384:        return (buf);
1.10      nicm      385: }
                    386:
1.158     nicm      387: /* Get entry for command. */
                    388: const struct cmd_entry *
                    389: cmd_get_entry(struct cmd *cmd)
                    390: {
                    391:        return (cmd->entry);
                    392: }
                    393:
                    394: /* Get arguments for command. */
                    395: struct args *
                    396: cmd_get_args(struct cmd *cmd)
                    397: {
                    398:        return (cmd->args);
                    399: }
                    400:
1.159     nicm      401: /* Get group for command. */
                    402: u_int
                    403: cmd_get_group(struct cmd *cmd)
                    404: {
                    405:        return (cmd->group);
                    406: }
                    407:
1.158     nicm      408: /* Get file and line for command. */
                    409: void
                    410: cmd_get_source(struct cmd *cmd, const char **file, u_int *line)
                    411: {
                    412:        if (file != NULL)
                    413:                *file = cmd->file;
                    414:        if (line != NULL)
                    415:                *line = cmd->line;
                    416: }
                    417:
                    418: /* Look for an alias for a command. */
1.149     nicm      419: char *
                    420: cmd_get_alias(const char *name)
1.135     nicm      421: {
1.149     nicm      422:        struct options_entry            *o;
                    423:        struct options_array_item       *a;
                    424:        union options_value             *ov;
                    425:        size_t                           wanted, n;
                    426:        const char                      *equals;
1.135     nicm      427:
                    428:        o = options_get_only(global_options, "command-alias");
1.142     nicm      429:        if (o == NULL)
1.149     nicm      430:                return (NULL);
                    431:        wanted = strlen(name);
1.135     nicm      432:
1.142     nicm      433:        a = options_array_first(o);
                    434:        while (a != NULL) {
1.143     nicm      435:                ov = options_array_item_value(a);
1.149     nicm      436:
                    437:                equals = strchr(ov->string, '=');
                    438:                if (equals != NULL) {
                    439:                        n = equals - ov->string;
                    440:                        if (n == wanted && strncmp(name, ov->string, n) == 0)
                    441:                                return (xstrdup(equals + 1));
                    442:                }
                    443:
1.142     nicm      444:                a = options_array_next(a);
1.135     nicm      445:        }
1.149     nicm      446:        return (NULL);
                    447: }
1.135     nicm      448:
1.158     nicm      449: /* Look up a command entry by name. */
1.149     nicm      450: static const struct cmd_entry *
                    451: cmd_find(const char *name, char **cause)
                    452: {
1.152     nicm      453:        const struct cmd_entry  **loop, *entry, *found = NULL;
                    454:        int                       ambiguous;
1.155     nicm      455:        char                      s[8192];
1.135     nicm      456:
1.149     nicm      457:        ambiguous = 0;
                    458:        for (loop = cmd_table; *loop != NULL; loop++) {
                    459:                entry = *loop;
                    460:                if (entry->alias != NULL && strcmp(entry->alias, name) == 0) {
                    461:                        ambiguous = 0;
                    462:                        found = entry;
                    463:                        break;
                    464:                }
1.135     nicm      465:
1.149     nicm      466:                if (strncmp(entry->name, name, strlen(name)) != 0)
                    467:                        continue;
                    468:                if (found != NULL)
                    469:                        ambiguous = 1;
                    470:                found = entry;
1.135     nicm      471:
1.149     nicm      472:                if (strcmp(entry->name, name) == 0)
                    473:                        break;
                    474:        }
                    475:        if (ambiguous)
                    476:                goto ambiguous;
                    477:        if (found == NULL) {
                    478:                xasprintf(cause, "unknown command: %s", name);
                    479:                return (NULL);
                    480:        }
                    481:        return (found);
1.135     nicm      482:
1.149     nicm      483: ambiguous:
                    484:        *s = '\0';
                    485:        for (loop = cmd_table; *loop != NULL; loop++) {
                    486:                entry = *loop;
                    487:                if (strncmp(entry->name, name, strlen(name)) != 0)
                    488:                        continue;
                    489:                if (strlcat(s, entry->name, sizeof s) >= sizeof s)
                    490:                        break;
                    491:                if (strlcat(s, ", ", sizeof s) >= sizeof s)
                    492:                        break;
                    493:        }
                    494:        s[strlen(s) - 2] = '\0';
                    495:        xasprintf(cause, "ambiguous command: %s, could be: %s", name, s);
                    496:        return (NULL);
1.135     nicm      497: }
                    498:
1.158     nicm      499: /* Parse a single command from an argument vector. */
1.1       nicm      500: struct cmd *
1.170     nicm      501: cmd_parse(struct args_value *values, u_int count, const char *file, u_int line,
                    502:     char **cause)
1.1       nicm      503: {
1.149     nicm      504:        const struct cmd_entry  *entry;
1.27      deraadt   505:        struct cmd              *cmd;
1.49      nicm      506:        struct args             *args;
1.173     nicm      507:        char                    *error = NULL;
1.1       nicm      508:
1.170     nicm      509:        if (count == 0 || values[0].type != ARGS_STRING) {
1.2       nicm      510:                xasprintf(cause, "no command");
1.1       nicm      511:                return (NULL);
1.2       nicm      512:        }
1.170     nicm      513:        entry = cmd_find(values[0].string, cause);
1.149     nicm      514:        if (entry == NULL)
1.1       nicm      515:                return (NULL);
                    516:
1.171     nicm      517:        args = args_parse(&entry->args, values, count, &error);
                    518:        if (args == NULL && error == NULL) {
                    519:                xasprintf(cause, "usage: %s %s", entry->name, entry->usage);
                    520:                return (NULL);
                    521:        }
1.170     nicm      522:        if (args == NULL) {
1.171     nicm      523:                xasprintf(cause, "command %s: %s", entry->name, error);
                    524:                free(error);
1.170     nicm      525:                return (NULL);
                    526:        }
1.1       nicm      527:
1.80      nicm      528:        cmd = xcalloc(1, sizeof *cmd);
1.1       nicm      529:        cmd->entry = entry;
1.49      nicm      530:        cmd->args = args;
1.80      nicm      531:
                    532:        if (file != NULL)
                    533:                cmd->file = xstrdup(file);
                    534:        cmd->line = line;
                    535:
1.1       nicm      536:        return (cmd);
1.149     nicm      537: }
                    538:
1.158     nicm      539: /* Free a command. */
1.149     nicm      540: void
                    541: cmd_free(struct cmd *cmd)
                    542: {
                    543:        free(cmd->file);
                    544:
                    545:        args_free(cmd->args);
                    546:        free(cmd);
1.1       nicm      547: }
                    548:
1.172     nicm      549: /* Copy a command. */
                    550: struct cmd *
                    551: cmd_copy(struct cmd *cmd, int argc, char **argv)
                    552: {
                    553:        struct cmd      *new_cmd;
                    554:
                    555:        new_cmd = xcalloc(1, sizeof *new_cmd);
                    556:        new_cmd->entry = cmd->entry;
                    557:        new_cmd->args = args_copy(cmd->args, argc, argv);
                    558:
                    559:        if (cmd->file != NULL)
                    560:                new_cmd->file = xstrdup(cmd->file);
                    561:        new_cmd->line = cmd->line;
                    562:
                    563:        return (new_cmd);
                    564: }
                    565:
1.158     nicm      566: /* Get a command as a string. */
1.106     nicm      567: char *
                    568: cmd_print(struct cmd *cmd)
1.1       nicm      569: {
1.106     nicm      570:        char    *out, *s;
                    571:
                    572:        s = args_print(cmd->args);
                    573:        if (*s != '\0')
                    574:                xasprintf(&out, "%s %s", cmd->entry->name, s);
                    575:        else
                    576:                out = xstrdup(cmd->entry->name);
                    577:        free(s);
1.49      nicm      578:
1.106     nicm      579:        return (out);
1.151     nicm      580: }
                    581:
1.158     nicm      582: /* Create a new command list. */
1.151     nicm      583: struct cmd_list *
                    584: cmd_list_new(void)
                    585: {
                    586:        struct cmd_list *cmdlist;
                    587:
                    588:        cmdlist = xcalloc(1, sizeof *cmdlist);
                    589:        cmdlist->references = 1;
                    590:        cmdlist->group = cmd_list_next_group++;
1.158     nicm      591:        cmdlist->list = xcalloc(1, sizeof *cmdlist->list);
                    592:        TAILQ_INIT(cmdlist->list);
1.151     nicm      593:        return (cmdlist);
                    594: }
                    595:
1.158     nicm      596: /* Append a command to a command list. */
1.151     nicm      597: void
                    598: cmd_list_append(struct cmd_list *cmdlist, struct cmd *cmd)
                    599: {
                    600:        cmd->group = cmdlist->group;
1.158     nicm      601:        TAILQ_INSERT_TAIL(cmdlist->list, cmd, qentry);
1.151     nicm      602: }
                    603:
1.168     nicm      604: /* Append all commands from one list to another.  */
                    605: void
                    606: cmd_list_append_all(struct cmd_list *cmdlist, struct cmd_list *from)
                    607: {
                    608:        struct cmd      *cmd;
                    609:
                    610:        TAILQ_FOREACH(cmd, from->list, qentry)
                    611:                cmd->group = cmdlist->group;
                    612:        TAILQ_CONCAT(cmdlist->list, from->list, qentry);
                    613: }
                    614:
                    615: /* Move all commands from one command list to another. */
1.151     nicm      616: void
                    617: cmd_list_move(struct cmd_list *cmdlist, struct cmd_list *from)
                    618: {
1.163     bket      619:        TAILQ_CONCAT(cmdlist->list, from->list, qentry);
1.151     nicm      620:        cmdlist->group = cmd_list_next_group++;
                    621: }
                    622:
1.158     nicm      623: /* Free a command list. */
1.151     nicm      624: void
                    625: cmd_list_free(struct cmd_list *cmdlist)
                    626: {
                    627:        struct cmd      *cmd, *cmd1;
                    628:
                    629:        if (--cmdlist->references != 0)
                    630:                return;
                    631:
1.158     nicm      632:        TAILQ_FOREACH_SAFE(cmd, cmdlist->list, qentry, cmd1) {
                    633:                TAILQ_REMOVE(cmdlist->list, cmd, qentry);
1.151     nicm      634:                cmd_free(cmd);
                    635:        }
1.158     nicm      636:        free(cmdlist->list);
1.151     nicm      637:        free(cmdlist);
1.172     nicm      638: }
                    639:
                    640: /* Copy a command list, expanding %s in arguments. */
                    641: struct cmd_list *
                    642: cmd_list_copy(struct cmd_list *cmdlist, int argc, char **argv)
                    643: {
                    644:        struct cmd      *cmd;
                    645:        struct cmd_list *new_cmdlist;
                    646:        struct cmd      *new_cmd;
                    647:        u_int            group = cmdlist->group;
                    648:        char            *s;
                    649:
                    650:        s = cmd_list_print(cmdlist, 0);
                    651:        log_debug("%s: %s", __func__, s);
                    652:        free(s);
                    653:
                    654:        new_cmdlist = cmd_list_new();
                    655:        TAILQ_FOREACH(cmd, cmdlist->list, qentry) {
                    656:                if (cmd->group != group) {
                    657:                        new_cmdlist->group = cmd_list_next_group++;
                    658:                        group = cmd->group;
                    659:                }
                    660:                new_cmd = cmd_copy(cmd, argc, argv);
                    661:                cmd_list_append(new_cmdlist, new_cmd);
                    662:        }
                    663:
                    664:        s = cmd_list_print(new_cmdlist, 0);
                    665:        log_debug("%s: %s", __func__, s);
                    666:        free(s);
                    667:
                    668:        return (new_cmdlist);
1.151     nicm      669: }
                    670:
1.158     nicm      671: /* Get a command list as a string. */
1.151     nicm      672: char *
                    673: cmd_list_print(struct cmd_list *cmdlist, int escaped)
                    674: {
1.160     nicm      675:        struct cmd      *cmd, *next;
1.151     nicm      676:        char            *buf, *this;
                    677:        size_t           len;
                    678:
                    679:        len = 1;
                    680:        buf = xcalloc(1, len);
                    681:
1.158     nicm      682:        TAILQ_FOREACH(cmd, cmdlist->list, qentry) {
1.151     nicm      683:                this = cmd_print(cmd);
                    684:
1.160     nicm      685:                len += strlen(this) + 6;
1.151     nicm      686:                buf = xrealloc(buf, len);
                    687:
                    688:                strlcat(buf, this, len);
1.160     nicm      689:
                    690:                next = TAILQ_NEXT(cmd, qentry);
                    691:                if (next != NULL) {
                    692:                        if (cmd->group != next->group) {
                    693:                                if (escaped)
                    694:                                        strlcat(buf, " \\;\\; ", len);
                    695:                                else
                    696:                                        strlcat(buf, " ;; ", len);
                    697:                        } else {
                    698:                                if (escaped)
                    699:                                        strlcat(buf, " \\; ", len);
                    700:                                else
                    701:                                        strlcat(buf, " ; ", len);
                    702:                        }
1.151     nicm      703:                }
                    704:
                    705:                free(this);
                    706:        }
                    707:
                    708:        return (buf);
1.158     nicm      709: }
                    710:
                    711: /* Get first command in list. */
                    712: struct cmd *
1.159     nicm      713: cmd_list_first(struct cmd_list *cmdlist)
1.158     nicm      714: {
1.159     nicm      715:        return (TAILQ_FIRST(cmdlist->list));
1.158     nicm      716: }
                    717:
                    718: /* Get next command in list. */
                    719: struct cmd *
1.159     nicm      720: cmd_list_next(struct cmd *cmd)
1.158     nicm      721: {
1.159     nicm      722:        return (TAILQ_NEXT(cmd, qentry));
1.158     nicm      723: }
                    724:
                    725: /* Do all of the commands in this command list have this flag? */
                    726: int
                    727: cmd_list_all_have(struct cmd_list *cmdlist, int flag)
                    728: {
                    729:        struct cmd      *cmd;
                    730:
                    731:        TAILQ_FOREACH(cmd, cmdlist->list, qentry) {
                    732:                if (~cmd->entry->flags & flag)
                    733:                        return (0);
                    734:        }
                    735:        return (1);
                    736: }
                    737:
                    738: /* Do any of the commands in this command list have this flag? */
                    739: int
                    740: cmd_list_any_have(struct cmd_list *cmdlist, int flag)
                    741: {
                    742:        struct cmd      *cmd;
                    743:
                    744:        TAILQ_FOREACH(cmd, cmdlist->list, qentry) {
                    745:                if (cmd->entry->flags & flag)
                    746:                        return (1);
                    747:        }
                    748:        return (0);
1.1       nicm      749: }
                    750:
1.99      nicm      751: /* Adjust current mouse position for a pane. */
                    752: int
                    753: cmd_mouse_at(struct window_pane *wp, struct mouse_event *m, u_int *xp,
                    754:     u_int *yp, int last)
                    755: {
                    756:        u_int   x, y;
                    757:
                    758:        if (last) {
1.145     nicm      759:                x = m->lx + m->ox;
                    760:                y = m->ly + m->oy;
1.99      nicm      761:        } else {
1.146     nicm      762:                x = m->x + m->ox;
                    763:                y = m->y + m->oy;
1.99      nicm      764:        }
1.145     nicm      765:        log_debug("%s: x=%u, y=%u%s", __func__, x, y, last ? " (last)" : "");
1.99      nicm      766:
1.153     nicm      767:        if (m->statusat == 0 && y >= m->statuslines)
                    768:                y -= m->statuslines;
1.99      nicm      769:
                    770:        if (x < wp->xoff || x >= wp->xoff + wp->sx)
                    771:                return (-1);
                    772:        if (y < wp->yoff || y >= wp->yoff + wp->sy)
                    773:                return (-1);
                    774:
1.123     nicm      775:        if (xp != NULL)
                    776:                *xp = x - wp->xoff;
                    777:        if (yp != NULL)
                    778:                *yp = y - wp->yoff;
1.99      nicm      779:        return (0);
                    780: }
                    781:
                    782: /* Get current mouse window if any. */
                    783: struct winlink *
                    784: cmd_mouse_window(struct mouse_event *m, struct session **sp)
                    785: {
                    786:        struct session  *s;
                    787:        struct window   *w;
1.147     nicm      788:        struct winlink  *wl;
1.99      nicm      789:
1.147     nicm      790:        if (!m->valid)
1.99      nicm      791:                return (NULL);
1.147     nicm      792:        if (m->s == -1 || (s = session_find_by_id(m->s)) == NULL)
1.99      nicm      793:                return (NULL);
1.147     nicm      794:        if (m->w == -1)
                    795:                wl = s->curw;
                    796:        else {
                    797:                if ((w = window_find_by_id(m->w)) == NULL)
                    798:                        return (NULL);
                    799:                wl = winlink_find_by_window(&s->windows, w);
                    800:        }
1.99      nicm      801:        if (sp != NULL)
                    802:                *sp = s;
1.147     nicm      803:        return (wl);
1.99      nicm      804: }
                    805:
                    806: /* Get current mouse pane if any. */
                    807: struct window_pane *
1.104     nicm      808: cmd_mouse_pane(struct mouse_event *m, struct session **sp,
                    809:     struct winlink **wlp)
1.99      nicm      810: {
                    811:        struct winlink          *wl;
                    812:        struct window_pane      *wp;
                    813:
                    814:        if ((wl = cmd_mouse_window(m, sp)) == NULL)
                    815:                return (NULL);
1.175   ! nicm      816:        if (m->wp == -1)
        !           817:                wp = wl->window->active;
        !           818:        else {
        !           819:                if ((wp = window_pane_find_by_id(m->wp)) == NULL)
        !           820:                        return (NULL);
        !           821:                if (!window_has_pane(wl->window, wp))
        !           822:                        return (NULL);
        !           823:        }
1.99      nicm      824:
                    825:        if (wlp != NULL)
                    826:                *wlp = wl;
1.43      nicm      827:        return (wp);
1.15      nicm      828: }
                    829:
                    830: /* Replace the first %% or %idx in template by s. */
                    831: char *
1.75      nicm      832: cmd_template_replace(const char *template, const char *s, int idx)
1.15      nicm      833: {
1.75      nicm      834:        char             ch, *buf;
1.156     nicm      835:        const char      *ptr, *cp, quote[] = "\"\\$;~";
1.127     nicm      836:        int              replaced, quoted;
1.84      nicm      837:        size_t           len;
1.15      nicm      838:
1.76      nicm      839:        if (strchr(template, '%') == NULL)
1.15      nicm      840:                return (xstrdup(template));
                    841:
                    842:        buf = xmalloc(1);
                    843:        *buf = '\0';
                    844:        len = 0;
                    845:        replaced = 0;
                    846:
                    847:        ptr = template;
                    848:        while (*ptr != '\0') {
                    849:                switch (ch = *ptr++) {
                    850:                case '%':
                    851:                        if (*ptr < '1' || *ptr > '9' || *ptr - '0' != idx) {
                    852:                                if (*ptr != '%' || replaced)
                    853:                                        break;
                    854:                                replaced = 1;
                    855:                        }
                    856:                        ptr++;
                    857:
1.127     nicm      858:                        quoted = (*ptr == '%');
                    859:                        if (quoted)
                    860:                                ptr++;
                    861:
1.132     nicm      862:                        buf = xrealloc(buf, len + (strlen(s) * 3) + 1);
1.127     nicm      863:                        for (cp = s; *cp != '\0'; cp++) {
1.131     nicm      864:                                if (quoted && strchr(quote, *cp) != NULL)
1.127     nicm      865:                                        buf[len++] = '\\';
                    866:                                buf[len++] = *cp;
                    867:                        }
                    868:                        buf[len] = '\0';
1.15      nicm      869:                        continue;
                    870:                }
1.98      nicm      871:                buf = xrealloc(buf, len + 2);
1.15      nicm      872:                buf[len++] = ch;
                    873:                buf[len] = '\0';
                    874:        }
                    875:
                    876:        return (buf);
1.1       nicm      877: }