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

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