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

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