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

1.161   ! nicm        1: /* $OpenBSD: cmd.c,v 1.160 2020/04/13 16:19:37 nicm Exp $ */
1.1       nicm        2:
                      3: /*
1.120     nicm        4:  * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
1.1       nicm        5:  *
                      6:  * Permission to use, copy, modify, and distribute this software for any
                      7:  * purpose with or without fee is hereby granted, provided that the above
                      8:  * copyright notice and this permission notice appear in all copies.
                      9:  *
                     10:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     11:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     12:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     13:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     14:  * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
                     15:  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
                     16:  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
                     17:  */
                     18:
                     19: #include <sys/types.h>
                     20: #include <sys/time.h>
                     21:
1.5       nicm       22: #include <fnmatch.h>
                     23: #include <paths.h>
1.61      nicm       24: #include <pwd.h>
1.1       nicm       25: #include <stdlib.h>
                     26: #include <string.h>
                     27: #include <unistd.h>
                     28:
                     29: #include "tmux.h"
1.105     nicm       30:
                     31: extern const struct cmd_entry cmd_attach_session_entry;
                     32: extern const struct cmd_entry cmd_bind_key_entry;
                     33: extern const struct cmd_entry cmd_break_pane_entry;
                     34: extern const struct cmd_entry cmd_capture_pane_entry;
                     35: extern const struct cmd_entry cmd_choose_buffer_entry;
                     36: extern const struct cmd_entry cmd_choose_client_entry;
                     37: extern const struct cmd_entry cmd_choose_tree_entry;
                     38: extern const struct cmd_entry cmd_clear_history_entry;
                     39: extern const struct cmd_entry cmd_clock_mode_entry;
                     40: extern const struct cmd_entry cmd_command_prompt_entry;
                     41: extern const struct cmd_entry cmd_confirm_before_entry;
                     42: extern const struct cmd_entry cmd_copy_mode_entry;
1.161   ! nicm       43: extern const struct cmd_entry cmd_customize_mode_entry;
1.105     nicm       44: extern const struct cmd_entry cmd_delete_buffer_entry;
                     45: extern const struct cmd_entry cmd_detach_client_entry;
1.148     nicm       46: extern const struct cmd_entry cmd_display_menu_entry;
1.105     nicm       47: extern const struct cmd_entry cmd_display_message_entry;
1.157     nicm       48: extern const struct cmd_entry cmd_display_popup_entry;
1.105     nicm       49: extern const struct cmd_entry cmd_display_panes_entry;
                     50: extern const struct cmd_entry cmd_down_pane_entry;
                     51: extern const struct cmd_entry cmd_find_window_entry;
                     52: extern const struct cmd_entry cmd_has_session_entry;
                     53: extern const struct cmd_entry cmd_if_shell_entry;
                     54: extern const struct cmd_entry cmd_join_pane_entry;
                     55: extern const struct cmd_entry cmd_kill_pane_entry;
                     56: extern const struct cmd_entry cmd_kill_server_entry;
                     57: extern const struct cmd_entry cmd_kill_session_entry;
                     58: extern const struct cmd_entry cmd_kill_window_entry;
                     59: extern const struct cmd_entry cmd_last_pane_entry;
                     60: extern const struct cmd_entry cmd_last_window_entry;
                     61: extern const struct cmd_entry cmd_link_window_entry;
                     62: extern const struct cmd_entry cmd_list_buffers_entry;
                     63: extern const struct cmd_entry cmd_list_clients_entry;
                     64: extern const struct cmd_entry cmd_list_commands_entry;
                     65: extern const struct cmd_entry cmd_list_keys_entry;
                     66: extern const struct cmd_entry cmd_list_panes_entry;
                     67: extern const struct cmd_entry cmd_list_sessions_entry;
                     68: extern const struct cmd_entry cmd_list_windows_entry;
                     69: extern const struct cmd_entry cmd_load_buffer_entry;
                     70: extern const struct cmd_entry cmd_lock_client_entry;
                     71: extern const struct cmd_entry cmd_lock_server_entry;
                     72: extern const struct cmd_entry cmd_lock_session_entry;
                     73: extern const struct cmd_entry cmd_move_pane_entry;
                     74: extern const struct cmd_entry cmd_move_window_entry;
                     75: extern const struct cmd_entry cmd_new_session_entry;
                     76: extern const struct cmd_entry cmd_new_window_entry;
                     77: extern const struct cmd_entry cmd_next_layout_entry;
                     78: extern const struct cmd_entry cmd_next_window_entry;
                     79: extern const struct cmd_entry cmd_paste_buffer_entry;
                     80: extern const struct cmd_entry cmd_pipe_pane_entry;
                     81: extern const struct cmd_entry cmd_previous_layout_entry;
                     82: extern const struct cmd_entry cmd_previous_window_entry;
                     83: extern const struct cmd_entry cmd_refresh_client_entry;
                     84: extern const struct cmd_entry cmd_rename_session_entry;
                     85: extern const struct cmd_entry cmd_rename_window_entry;
                     86: extern const struct cmd_entry cmd_resize_pane_entry;
1.141     nicm       87: extern const struct cmd_entry cmd_resize_window_entry;
1.105     nicm       88: extern const struct cmd_entry cmd_respawn_pane_entry;
                     89: extern const struct cmd_entry cmd_respawn_window_entry;
                     90: extern const struct cmd_entry cmd_rotate_window_entry;
                     91: extern const struct cmd_entry cmd_run_shell_entry;
                     92: extern const struct cmd_entry cmd_save_buffer_entry;
                     93: extern const struct cmd_entry cmd_select_layout_entry;
                     94: extern const struct cmd_entry cmd_select_pane_entry;
                     95: extern const struct cmd_entry cmd_select_window_entry;
                     96: extern const struct cmd_entry cmd_send_keys_entry;
                     97: extern const struct cmd_entry cmd_send_prefix_entry;
                     98: extern const struct cmd_entry cmd_set_buffer_entry;
                     99: extern const struct cmd_entry cmd_set_environment_entry;
1.107     nicm      100: extern const struct cmd_entry cmd_set_hook_entry;
1.105     nicm      101: extern const struct cmd_entry cmd_set_option_entry;
                    102: extern const struct cmd_entry cmd_set_window_option_entry;
                    103: extern const struct cmd_entry cmd_show_buffer_entry;
                    104: extern const struct cmd_entry cmd_show_environment_entry;
1.107     nicm      105: extern const struct cmd_entry cmd_show_hooks_entry;
1.105     nicm      106: extern const struct cmd_entry cmd_show_messages_entry;
                    107: extern const struct cmd_entry cmd_show_options_entry;
                    108: extern const struct cmd_entry cmd_show_window_options_entry;
                    109: extern const struct cmd_entry cmd_source_file_entry;
                    110: extern const struct cmd_entry cmd_split_window_entry;
                    111: extern const struct cmd_entry cmd_start_server_entry;
                    112: extern const struct cmd_entry cmd_suspend_client_entry;
                    113: extern const struct cmd_entry cmd_swap_pane_entry;
                    114: extern const struct cmd_entry cmd_swap_window_entry;
                    115: extern const struct cmd_entry cmd_switch_client_entry;
                    116: extern const struct cmd_entry cmd_unbind_key_entry;
                    117: extern const struct cmd_entry cmd_unlink_window_entry;
                    118: extern const struct cmd_entry cmd_up_pane_entry;
                    119: extern const struct cmd_entry cmd_wait_for_entry;
1.1       nicm      120:
                    121: const struct cmd_entry *cmd_table[] = {
                    122:        &cmd_attach_session_entry,
                    123:        &cmd_bind_key_entry,
                    124:        &cmd_break_pane_entry,
1.34      nicm      125:        &cmd_capture_pane_entry,
1.42      nicm      126:        &cmd_choose_buffer_entry,
1.15      nicm      127:        &cmd_choose_client_entry,
1.67      nicm      128:        &cmd_choose_tree_entry,
1.1       nicm      129:        &cmd_clear_history_entry,
                    130:        &cmd_clock_mode_entry,
                    131:        &cmd_command_prompt_entry,
                    132:        &cmd_confirm_before_entry,
                    133:        &cmd_copy_mode_entry,
1.161   ! nicm      134:        &cmd_customize_mode_entry,
1.1       nicm      135:        &cmd_delete_buffer_entry,
                    136:        &cmd_detach_client_entry,
1.148     nicm      137:        &cmd_display_menu_entry,
1.7       nicm      138:        &cmd_display_message_entry,
1.157     nicm      139:        &cmd_display_popup_entry,
1.16      nicm      140:        &cmd_display_panes_entry,
1.1       nicm      141:        &cmd_find_window_entry,
                    142:        &cmd_has_session_entry,
1.4       nicm      143:        &cmd_if_shell_entry,
1.37      nicm      144:        &cmd_join_pane_entry,
1.1       nicm      145:        &cmd_kill_pane_entry,
                    146:        &cmd_kill_server_entry,
                    147:        &cmd_kill_session_entry,
                    148:        &cmd_kill_window_entry,
1.45      nicm      149:        &cmd_last_pane_entry,
1.1       nicm      150:        &cmd_last_window_entry,
                    151:        &cmd_link_window_entry,
                    152:        &cmd_list_buffers_entry,
                    153:        &cmd_list_clients_entry,
                    154:        &cmd_list_commands_entry,
                    155:        &cmd_list_keys_entry,
1.23      nicm      156:        &cmd_list_panes_entry,
1.1       nicm      157:        &cmd_list_sessions_entry,
                    158:        &cmd_list_windows_entry,
                    159:        &cmd_load_buffer_entry,
1.19      nicm      160:        &cmd_lock_client_entry,
1.1       nicm      161:        &cmd_lock_server_entry,
1.19      nicm      162:        &cmd_lock_session_entry,
1.63      nicm      163:        &cmd_move_pane_entry,
1.1       nicm      164:        &cmd_move_window_entry,
                    165:        &cmd_new_session_entry,
                    166:        &cmd_new_window_entry,
                    167:        &cmd_next_layout_entry,
                    168:        &cmd_next_window_entry,
                    169:        &cmd_paste_buffer_entry,
1.24      nicm      170:        &cmd_pipe_pane_entry,
1.1       nicm      171:        &cmd_previous_layout_entry,
                    172:        &cmd_previous_window_entry,
                    173:        &cmd_refresh_client_entry,
                    174:        &cmd_rename_session_entry,
                    175:        &cmd_rename_window_entry,
                    176:        &cmd_resize_pane_entry,
1.141     nicm      177:        &cmd_resize_window_entry,
1.56      nicm      178:        &cmd_respawn_pane_entry,
1.1       nicm      179:        &cmd_respawn_window_entry,
                    180:        &cmd_rotate_window_entry,
1.17      nicm      181:        &cmd_run_shell_entry,
1.1       nicm      182:        &cmd_save_buffer_entry,
                    183:        &cmd_select_layout_entry,
                    184:        &cmd_select_pane_entry,
                    185:        &cmd_select_window_entry,
                    186:        &cmd_send_keys_entry,
                    187:        &cmd_send_prefix_entry,
                    188:        &cmd_set_buffer_entry,
1.13      nicm      189:        &cmd_set_environment_entry,
1.107     nicm      190:        &cmd_set_hook_entry,
1.1       nicm      191:        &cmd_set_option_entry,
                    192:        &cmd_set_window_option_entry,
                    193:        &cmd_show_buffer_entry,
1.13      nicm      194:        &cmd_show_environment_entry,
1.107     nicm      195:        &cmd_show_hooks_entry,
1.32      nicm      196:        &cmd_show_messages_entry,
1.1       nicm      197:        &cmd_show_options_entry,
                    198:        &cmd_show_window_options_entry,
                    199:        &cmd_source_file_entry,
                    200:        &cmd_split_window_entry,
                    201:        &cmd_start_server_entry,
                    202:        &cmd_suspend_client_entry,
                    203:        &cmd_swap_pane_entry,
                    204:        &cmd_swap_window_entry,
                    205:        &cmd_switch_client_entry,
                    206:        &cmd_unbind_key_entry,
                    207:        &cmd_unlink_window_entry,
1.82      nicm      208:        &cmd_wait_for_entry,
1.1       nicm      209:        NULL
                    210: };
                    211:
1.158     nicm      212: /* Instance of a command. */
                    213: struct cmd {
                    214:        const struct cmd_entry   *entry;
                    215:        struct args              *args;
                    216:        u_int                     group;
                    217:
                    218:        char                     *file;
                    219:        u_int                     line;
                    220:
                    221:        char                     *alias;
                    222:        int                       argc;
                    223:        char                    **argv;
                    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
                    251: cmd_prepend_argv(int *argc, char ***argv, char *arg)
                    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
                    268: cmd_append_argv(int *argc, char ***argv, char *arg)
                    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: {
                    360:        char    *buf;
                    361:        int      i;
                    362:        size_t   len;
                    363:
                    364:        if (argc == 0)
                    365:                return (xstrdup(""));
                    366:
                    367:        len = 0;
                    368:        buf = NULL;
                    369:
                    370:        for (i = 0; i < argc; i++) {
                    371:                len += strlen(argv[i]) + 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);
                    378:                strlcat(buf, argv[i], len);
                    379:        }
                    380:        return (buf);
1.10      nicm      381: }
                    382:
1.158     nicm      383: /* Get entry for command. */
                    384: const struct cmd_entry *
                    385: cmd_get_entry(struct cmd *cmd)
                    386: {
                    387:        return (cmd->entry);
                    388: }
                    389:
                    390: /* Get arguments for command. */
                    391: struct args *
                    392: cmd_get_args(struct cmd *cmd)
                    393: {
                    394:        return (cmd->args);
                    395: }
                    396:
1.159     nicm      397: /* Get group for command. */
                    398: u_int
                    399: cmd_get_group(struct cmd *cmd)
                    400: {
                    401:        return (cmd->group);
                    402: }
                    403:
1.158     nicm      404: /* Get file and line for command. */
                    405: void
                    406: cmd_get_source(struct cmd *cmd, const char **file, u_int *line)
                    407: {
                    408:        if (file != NULL)
                    409:                *file = cmd->file;
                    410:        if (line != NULL)
                    411:                *line = cmd->line;
                    412: }
                    413:
                    414: /* Look for an alias for a command. */
1.149     nicm      415: char *
                    416: cmd_get_alias(const char *name)
1.135     nicm      417: {
1.149     nicm      418:        struct options_entry            *o;
                    419:        struct options_array_item       *a;
                    420:        union options_value             *ov;
                    421:        size_t                           wanted, n;
                    422:        const char                      *equals;
1.135     nicm      423:
                    424:        o = options_get_only(global_options, "command-alias");
1.142     nicm      425:        if (o == NULL)
1.149     nicm      426:                return (NULL);
                    427:        wanted = strlen(name);
1.135     nicm      428:
1.142     nicm      429:        a = options_array_first(o);
                    430:        while (a != NULL) {
1.143     nicm      431:                ov = options_array_item_value(a);
1.149     nicm      432:
                    433:                equals = strchr(ov->string, '=');
                    434:                if (equals != NULL) {
                    435:                        n = equals - ov->string;
                    436:                        if (n == wanted && strncmp(name, ov->string, n) == 0)
                    437:                                return (xstrdup(equals + 1));
                    438:                }
                    439:
1.142     nicm      440:                a = options_array_next(a);
1.135     nicm      441:        }
1.149     nicm      442:        return (NULL);
                    443: }
1.135     nicm      444:
1.158     nicm      445: /* Look up a command entry by name. */
1.149     nicm      446: static const struct cmd_entry *
                    447: cmd_find(const char *name, char **cause)
                    448: {
1.152     nicm      449:        const struct cmd_entry  **loop, *entry, *found = NULL;
                    450:        int                       ambiguous;
1.155     nicm      451:        char                      s[8192];
1.135     nicm      452:
1.149     nicm      453:        ambiguous = 0;
                    454:        for (loop = cmd_table; *loop != NULL; loop++) {
                    455:                entry = *loop;
                    456:                if (entry->alias != NULL && strcmp(entry->alias, name) == 0) {
                    457:                        ambiguous = 0;
                    458:                        found = entry;
                    459:                        break;
                    460:                }
1.135     nicm      461:
1.149     nicm      462:                if (strncmp(entry->name, name, strlen(name)) != 0)
                    463:                        continue;
                    464:                if (found != NULL)
                    465:                        ambiguous = 1;
                    466:                found = entry;
1.135     nicm      467:
1.149     nicm      468:                if (strcmp(entry->name, name) == 0)
                    469:                        break;
                    470:        }
                    471:        if (ambiguous)
                    472:                goto ambiguous;
                    473:        if (found == NULL) {
                    474:                xasprintf(cause, "unknown command: %s", name);
                    475:                return (NULL);
                    476:        }
                    477:        return (found);
1.135     nicm      478:
1.149     nicm      479: ambiguous:
                    480:        *s = '\0';
                    481:        for (loop = cmd_table; *loop != NULL; loop++) {
                    482:                entry = *loop;
                    483:                if (strncmp(entry->name, name, strlen(name)) != 0)
                    484:                        continue;
                    485:                if (strlcat(s, entry->name, sizeof s) >= sizeof s)
                    486:                        break;
                    487:                if (strlcat(s, ", ", sizeof s) >= sizeof s)
                    488:                        break;
                    489:        }
                    490:        s[strlen(s) - 2] = '\0';
                    491:        xasprintf(cause, "ambiguous command: %s, could be: %s", name, s);
                    492:        return (NULL);
1.135     nicm      493: }
                    494:
1.158     nicm      495: /* Parse a single command from an argument vector. */
1.1       nicm      496: struct cmd *
1.80      nicm      497: cmd_parse(int argc, char **argv, const char *file, u_int line, char **cause)
1.1       nicm      498: {
1.149     nicm      499:        const struct cmd_entry  *entry;
1.135     nicm      500:        const char              *name;
1.27      deraadt   501:        struct cmd              *cmd;
1.49      nicm      502:        struct args             *args;
1.1       nicm      503:
1.2       nicm      504:        if (argc == 0) {
                    505:                xasprintf(cause, "no command");
1.1       nicm      506:                return (NULL);
1.2       nicm      507:        }
1.135     nicm      508:        name = argv[0];
1.1       nicm      509:
1.149     nicm      510:        entry = cmd_find(name, cause);
                    511:        if (entry == NULL)
1.1       nicm      512:                return (NULL);
1.150     nicm      513:        cmd_log_argv(argc, argv, "%s: %s", __func__, entry->name);
1.1       nicm      514:
1.117     nicm      515:        args = args_parse(entry->args.template, argc, argv);
1.49      nicm      516:        if (args == NULL)
                    517:                goto usage;
1.117     nicm      518:        if (entry->args.lower != -1 && args->argc < entry->args.lower)
1.49      nicm      519:                goto usage;
1.117     nicm      520:        if (entry->args.upper != -1 && args->argc > entry->args.upper)
1.49      nicm      521:                goto usage;
1.1       nicm      522:
1.80      nicm      523:        cmd = xcalloc(1, sizeof *cmd);
1.1       nicm      524:        cmd->entry = entry;
1.49      nicm      525:        cmd->args = args;
1.80      nicm      526:
                    527:        if (file != NULL)
                    528:                cmd->file = xstrdup(file);
                    529:        cmd->line = line;
                    530:
1.149     nicm      531:        cmd->alias = NULL;
                    532:        cmd->argc = argc;
                    533:        cmd->argv = cmd_copy_argv(argc, argv);
                    534:
1.1       nicm      535:        return (cmd);
                    536:
                    537: usage:
1.49      nicm      538:        if (args != NULL)
                    539:                args_free(args);
1.1       nicm      540:        xasprintf(cause, "usage: %s %s", entry->name, entry->usage);
                    541:        return (NULL);
1.149     nicm      542: }
                    543:
1.158     nicm      544: /* Free a command. */
1.149     nicm      545: void
                    546: cmd_free(struct cmd *cmd)
                    547: {
                    548:        free(cmd->alias);
                    549:        cmd_free_argv(cmd->argc, cmd->argv);
                    550:
                    551:        free(cmd->file);
                    552:
                    553:        args_free(cmd->args);
                    554:        free(cmd);
1.1       nicm      555: }
                    556:
1.158     nicm      557: /* Get a command as a string. */
1.106     nicm      558: char *
                    559: cmd_print(struct cmd *cmd)
1.1       nicm      560: {
1.106     nicm      561:        char    *out, *s;
                    562:
                    563:        s = args_print(cmd->args);
                    564:        if (*s != '\0')
                    565:                xasprintf(&out, "%s %s", cmd->entry->name, s);
                    566:        else
                    567:                out = xstrdup(cmd->entry->name);
                    568:        free(s);
1.49      nicm      569:
1.106     nicm      570:        return (out);
1.151     nicm      571: }
                    572:
1.158     nicm      573: /* Create a new command list. */
1.151     nicm      574: struct cmd_list *
                    575: cmd_list_new(void)
                    576: {
                    577:        struct cmd_list *cmdlist;
                    578:
                    579:        cmdlist = xcalloc(1, sizeof *cmdlist);
                    580:        cmdlist->references = 1;
                    581:        cmdlist->group = cmd_list_next_group++;
1.158     nicm      582:        cmdlist->list = xcalloc(1, sizeof *cmdlist->list);
                    583:        TAILQ_INIT(cmdlist->list);
1.151     nicm      584:        return (cmdlist);
                    585: }
                    586:
1.158     nicm      587: /* Append a command to a command list. */
1.151     nicm      588: void
                    589: cmd_list_append(struct cmd_list *cmdlist, struct cmd *cmd)
                    590: {
                    591:        cmd->group = cmdlist->group;
1.158     nicm      592:        TAILQ_INSERT_TAIL(cmdlist->list, cmd, qentry);
1.151     nicm      593: }
                    594:
1.158     nicm      595: /* Move all commands from one command list to another */
1.151     nicm      596: void
                    597: cmd_list_move(struct cmd_list *cmdlist, struct cmd_list *from)
                    598: {
                    599:        struct cmd      *cmd, *cmd1;
                    600:
1.158     nicm      601:        TAILQ_FOREACH_SAFE(cmd, from->list, qentry, cmd1) {
                    602:                TAILQ_REMOVE(from->list, cmd, qentry);
                    603:                TAILQ_INSERT_TAIL(cmdlist->list, cmd, qentry);
1.151     nicm      604:        }
                    605:        cmdlist->group = cmd_list_next_group++;
                    606: }
                    607:
1.158     nicm      608: /* Free a command list. */
1.151     nicm      609: void
                    610: cmd_list_free(struct cmd_list *cmdlist)
                    611: {
                    612:        struct cmd      *cmd, *cmd1;
                    613:
                    614:        if (--cmdlist->references != 0)
                    615:                return;
                    616:
1.158     nicm      617:        TAILQ_FOREACH_SAFE(cmd, cmdlist->list, qentry, cmd1) {
                    618:                TAILQ_REMOVE(cmdlist->list, cmd, qentry);
1.151     nicm      619:                cmd_free(cmd);
                    620:        }
1.158     nicm      621:        free(cmdlist->list);
1.151     nicm      622:        free(cmdlist);
                    623: }
                    624:
1.158     nicm      625: /* Get a command list as a string. */
1.151     nicm      626: char *
                    627: cmd_list_print(struct cmd_list *cmdlist, int escaped)
                    628: {
1.160     nicm      629:        struct cmd      *cmd, *next;
1.151     nicm      630:        char            *buf, *this;
                    631:        size_t           len;
                    632:
                    633:        len = 1;
                    634:        buf = xcalloc(1, len);
                    635:
1.158     nicm      636:        TAILQ_FOREACH(cmd, cmdlist->list, qentry) {
1.151     nicm      637:                this = cmd_print(cmd);
                    638:
1.160     nicm      639:                len += strlen(this) + 6;
1.151     nicm      640:                buf = xrealloc(buf, len);
                    641:
                    642:                strlcat(buf, this, len);
1.160     nicm      643:
                    644:                next = TAILQ_NEXT(cmd, qentry);
                    645:                if (next != NULL) {
                    646:                        if (cmd->group != next->group) {
                    647:                                if (escaped)
                    648:                                        strlcat(buf, " \\;\\; ", len);
                    649:                                else
                    650:                                        strlcat(buf, " ;; ", len);
                    651:                        } else {
                    652:                                if (escaped)
                    653:                                        strlcat(buf, " \\; ", len);
                    654:                                else
                    655:                                        strlcat(buf, " ; ", len);
                    656:                        }
1.151     nicm      657:                }
                    658:
                    659:                free(this);
                    660:        }
                    661:
                    662:        return (buf);
1.158     nicm      663: }
                    664:
                    665: /* Get first command in list. */
                    666: struct cmd *
1.159     nicm      667: cmd_list_first(struct cmd_list *cmdlist)
1.158     nicm      668: {
1.159     nicm      669:        return (TAILQ_FIRST(cmdlist->list));
1.158     nicm      670: }
                    671:
                    672: /* Get next command in list. */
                    673: struct cmd *
1.159     nicm      674: cmd_list_next(struct cmd *cmd)
1.158     nicm      675: {
1.159     nicm      676:        return (TAILQ_NEXT(cmd, qentry));
1.158     nicm      677: }
                    678:
                    679: /* Do all of the commands in this command list have this flag? */
                    680: int
                    681: cmd_list_all_have(struct cmd_list *cmdlist, int flag)
                    682: {
                    683:        struct cmd      *cmd;
                    684:
                    685:        TAILQ_FOREACH(cmd, cmdlist->list, qentry) {
                    686:                if (~cmd->entry->flags & flag)
                    687:                        return (0);
                    688:        }
                    689:        return (1);
                    690: }
                    691:
                    692: /* Do any of the commands in this command list have this flag? */
                    693: int
                    694: cmd_list_any_have(struct cmd_list *cmdlist, int flag)
                    695: {
                    696:        struct cmd      *cmd;
                    697:
                    698:        TAILQ_FOREACH(cmd, cmdlist->list, qentry) {
                    699:                if (cmd->entry->flags & flag)
                    700:                        return (1);
                    701:        }
                    702:        return (0);
1.1       nicm      703: }
                    704:
1.99      nicm      705: /* Adjust current mouse position for a pane. */
                    706: int
                    707: cmd_mouse_at(struct window_pane *wp, struct mouse_event *m, u_int *xp,
                    708:     u_int *yp, int last)
                    709: {
                    710:        u_int   x, y;
                    711:
                    712:        if (last) {
1.145     nicm      713:                x = m->lx + m->ox;
                    714:                y = m->ly + m->oy;
1.99      nicm      715:        } else {
1.146     nicm      716:                x = m->x + m->ox;
                    717:                y = m->y + m->oy;
1.99      nicm      718:        }
1.145     nicm      719:        log_debug("%s: x=%u, y=%u%s", __func__, x, y, last ? " (last)" : "");
1.99      nicm      720:
1.153     nicm      721:        if (m->statusat == 0 && y >= m->statuslines)
                    722:                y -= m->statuslines;
1.99      nicm      723:
                    724:        if (x < wp->xoff || x >= wp->xoff + wp->sx)
                    725:                return (-1);
                    726:        if (y < wp->yoff || y >= wp->yoff + wp->sy)
                    727:                return (-1);
                    728:
1.123     nicm      729:        if (xp != NULL)
                    730:                *xp = x - wp->xoff;
                    731:        if (yp != NULL)
                    732:                *yp = y - wp->yoff;
1.99      nicm      733:        return (0);
                    734: }
                    735:
                    736: /* Get current mouse window if any. */
                    737: struct winlink *
                    738: cmd_mouse_window(struct mouse_event *m, struct session **sp)
                    739: {
                    740:        struct session  *s;
                    741:        struct window   *w;
1.147     nicm      742:        struct winlink  *wl;
1.99      nicm      743:
1.147     nicm      744:        if (!m->valid)
1.99      nicm      745:                return (NULL);
1.147     nicm      746:        if (m->s == -1 || (s = session_find_by_id(m->s)) == NULL)
1.99      nicm      747:                return (NULL);
1.147     nicm      748:        if (m->w == -1)
                    749:                wl = s->curw;
                    750:        else {
                    751:                if ((w = window_find_by_id(m->w)) == NULL)
                    752:                        return (NULL);
                    753:                wl = winlink_find_by_window(&s->windows, w);
                    754:        }
1.99      nicm      755:        if (sp != NULL)
                    756:                *sp = s;
1.147     nicm      757:        return (wl);
1.99      nicm      758: }
                    759:
                    760: /* Get current mouse pane if any. */
                    761: struct window_pane *
1.104     nicm      762: cmd_mouse_pane(struct mouse_event *m, struct session **sp,
                    763:     struct winlink **wlp)
1.99      nicm      764: {
                    765:        struct winlink          *wl;
                    766:        struct window_pane      *wp;
                    767:
                    768:        if ((wl = cmd_mouse_window(m, sp)) == NULL)
                    769:                return (NULL);
                    770:        if ((wp = window_pane_find_by_id(m->wp)) == NULL)
                    771:                return (NULL);
                    772:        if (!window_has_pane(wl->window, wp))
                    773:                return (NULL);
                    774:
                    775:        if (wlp != NULL)
                    776:                *wlp = wl;
1.43      nicm      777:        return (wp);
1.15      nicm      778: }
                    779:
                    780: /* Replace the first %% or %idx in template by s. */
                    781: char *
1.75      nicm      782: cmd_template_replace(const char *template, const char *s, int idx)
1.15      nicm      783: {
1.75      nicm      784:        char             ch, *buf;
1.156     nicm      785:        const char      *ptr, *cp, quote[] = "\"\\$;~";
1.127     nicm      786:        int              replaced, quoted;
1.84      nicm      787:        size_t           len;
1.15      nicm      788:
1.76      nicm      789:        if (strchr(template, '%') == NULL)
1.15      nicm      790:                return (xstrdup(template));
                    791:
                    792:        buf = xmalloc(1);
                    793:        *buf = '\0';
                    794:        len = 0;
                    795:        replaced = 0;
                    796:
                    797:        ptr = template;
                    798:        while (*ptr != '\0') {
                    799:                switch (ch = *ptr++) {
                    800:                case '%':
                    801:                        if (*ptr < '1' || *ptr > '9' || *ptr - '0' != idx) {
                    802:                                if (*ptr != '%' || replaced)
                    803:                                        break;
                    804:                                replaced = 1;
                    805:                        }
                    806:                        ptr++;
                    807:
1.127     nicm      808:                        quoted = (*ptr == '%');
                    809:                        if (quoted)
                    810:                                ptr++;
                    811:
1.132     nicm      812:                        buf = xrealloc(buf, len + (strlen(s) * 3) + 1);
1.127     nicm      813:                        for (cp = s; *cp != '\0'; cp++) {
1.131     nicm      814:                                if (quoted && strchr(quote, *cp) != NULL)
1.127     nicm      815:                                        buf[len++] = '\\';
                    816:                                buf[len++] = *cp;
                    817:                        }
                    818:                        buf[len] = '\0';
1.15      nicm      819:                        continue;
                    820:                }
1.98      nicm      821:                buf = xrealloc(buf, len + 2);
1.15      nicm      822:                buf[len++] = ch;
                    823:                buf[len] = '\0';
                    824:        }
                    825:
                    826:        return (buf);
1.1       nicm      827: }