[BACK]Return to cmd-bind-key.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / tmux

Annotation of src/usr.bin/tmux/cmd-bind-key.c, Revision 1.35

1.35    ! nicm        1: /* $OpenBSD: cmd-bind-key.c,v 1.34 2019/05/27 12:16:27 nicm Exp $ */
1.1       nicm        2:
                      3: /*
1.25      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:
1.13      nicm       21: #include <stdlib.h>
1.5       nicm       22: #include <string.h>
                     23:
1.1       nicm       24: #include "tmux.h"
                     25:
                     26: /*
1.32      nicm       27:  * Bind a key to a command.
1.1       nicm       28:  */
                     29:
1.31      nicm       30: static enum cmd_retval cmd_bind_key_exec(struct cmd *, struct cmdq_item *);
1.1       nicm       31:
                     32: const struct cmd_entry cmd_bind_key_entry = {
1.24      nicm       33:        .name = "bind-key",
                     34:        .alias = "bind",
                     35:
1.35    ! nicm       36:        .args = { "cnrN:T:", 2, -1 },
        !            37:        .usage = "[-cnr] [-T key-table] [-N note] key "
1.26      nicm       38:                 "command [arguments]",
1.24      nicm       39:
1.30      nicm       40:        .flags = CMD_AFTERHOOK,
1.24      nicm       41:        .exec = cmd_bind_key_exec
1.1       nicm       42: };
                     43:
1.27      nicm       44: static enum cmd_retval
1.31      nicm       45: cmd_bind_key_exec(struct cmd *self, struct cmdq_item *item)
1.1       nicm       46: {
1.34      nicm       47:        struct args              *args = self->args;
                     48:        key_code                  key;
1.35    ! nicm       49:        const char               *tablename, *note;
1.34      nicm       50:        struct cmd_parse_result  *pr;
                     51:        char                    **argv = args->argv;
1.35    ! nicm       52:        int                       argc = args->argc, repeat;
1.17      nicm       53:
1.34      nicm       54:        key = key_string_lookup_string(argv[0]);
1.23      nicm       55:        if (key == KEYC_NONE || key == KEYC_UNKNOWN) {
1.34      nicm       56:                cmdq_error(item, "unknown key: %s", argv[0]);
1.14      nicm       57:                return (CMD_RETURN_ERROR);
1.10      nicm       58:        }
1.1       nicm       59:
1.21      nicm       60:        if (args_has(args, 'T'))
                     61:                tablename = args_get(args, 'T');
                     62:        else if (args_has(args, 'n'))
                     63:                tablename = "root";
                     64:        else
                     65:                tablename = "prefix";
1.35    ! nicm       66:        repeat = args_has(args, 'r');
1.21      nicm       67:
1.34      nicm       68:        if (argc == 2)
                     69:                pr = cmd_parse_from_string(argv[1], NULL);
                     70:        else
                     71:                pr = cmd_parse_from_arguments(argc - 1, argv + 1, NULL);
1.33      nicm       72:        switch (pr->status) {
                     73:        case CMD_PARSE_EMPTY:
                     74:                cmdq_error(item, "empty command");
1.14      nicm       75:                return (CMD_RETURN_ERROR);
1.33      nicm       76:        case CMD_PARSE_ERROR:
                     77:                cmdq_error(item, "%s", pr->error);
                     78:                free(pr->error);
                     79:                return (CMD_RETURN_ERROR);
                     80:        case CMD_PARSE_SUCCESS:
                     81:                break;
1.10      nicm       82:        }
1.35    ! nicm       83:        note = args_get(args, 'N');
        !            84:        key_bindings_add(tablename, key, note, repeat, pr->cmdlist);
1.14      nicm       85:        return (CMD_RETURN_NORMAL);
1.1       nicm       86: }