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

1.16    ! nicm        1: /* $OpenBSD: cmd-bind-key.c,v 1.15 2013/03/22 15:52:40 nicm Exp $ */
1.1       nicm        2:
                      3: /*
                      4:  * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
                      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: /*
                     27:  * Bind a key to a command, this recurses through cmd_*.
                     28:  */
                     29:
1.14      nicm       30: enum cmd_retval         cmd_bind_key_check(struct args *);
1.16    ! nicm       31: enum cmd_retval         cmd_bind_key_exec(struct cmd *, struct cmd_q *);
1.1       nicm       32:
1.16    ! nicm       33: enum cmd_retval         cmd_bind_key_table(struct cmd *, struct cmd_q *, int);
1.1       nicm       34:
                     35: const struct cmd_entry cmd_bind_key_entry = {
                     36:        "bind-key", "bind",
1.10      nicm       37:        "cnrt:", 1, -1,
1.5       nicm       38:        "[-cnr] [-t key-table] key command [arguments]",
1.10      nicm       39:        0,
1.1       nicm       40:        NULL,
1.10      nicm       41:        cmd_bind_key_check,
                     42:        cmd_bind_key_exec
1.1       nicm       43: };
                     44:
1.14      nicm       45: enum cmd_retval
1.10      nicm       46: cmd_bind_key_check(struct args *args)
1.1       nicm       47: {
1.10      nicm       48:        if (args_has(args, 't')) {
1.15      nicm       49:                if (args->argc != 2 && args->argc != 3)
1.14      nicm       50:                        return (CMD_RETURN_ERROR);
1.5       nicm       51:        } else {
1.10      nicm       52:                if (args->argc < 2)
1.14      nicm       53:                        return (CMD_RETURN_ERROR);
1.5       nicm       54:        }
1.14      nicm       55:        return (CMD_RETURN_NORMAL);
1.1       nicm       56: }
                     57:
1.14      nicm       58: enum cmd_retval
1.16    ! nicm       59: cmd_bind_key_exec(struct cmd *self, struct cmd_q *cmdq)
1.1       nicm       60: {
1.10      nicm       61:        struct args     *args = self->args;
                     62:        char            *cause;
                     63:        struct cmd_list *cmdlist;
                     64:        int              key;
                     65:
                     66:        key = key_string_lookup_string(args->argv[0]);
                     67:        if (key == KEYC_NONE) {
1.16    ! nicm       68:                cmdq_error(cmdq, "unknown key: %s", args->argv[0]);
1.14      nicm       69:                return (CMD_RETURN_ERROR);
1.10      nicm       70:        }
1.1       nicm       71:
1.10      nicm       72:        if (args_has(args, 't'))
1.16    ! nicm       73:                return (cmd_bind_key_table(self, cmdq, key));
1.1       nicm       74:
1.16    ! nicm       75:        cmdlist = cmd_list_parse(args->argc - 1, args->argv + 1, NULL, 0,
        !            76:            &cause);
1.10      nicm       77:        if (cmdlist == NULL) {
1.16    ! nicm       78:                cmdq_error(cmdq, "%s", cause);
1.13      nicm       79:                free(cause);
1.14      nicm       80:                return (CMD_RETURN_ERROR);
1.10      nicm       81:        }
1.1       nicm       82:
1.10      nicm       83:        if (!args_has(args, 'n'))
                     84:            key |= KEYC_PREFIX;
                     85:        key_bindings_add(key, args_has(args, 'r'), cmdlist);
1.14      nicm       86:        return (CMD_RETURN_NORMAL);
1.1       nicm       87: }
                     88:
1.14      nicm       89: enum cmd_retval
1.16    ! nicm       90: cmd_bind_key_table(struct cmd *self, struct cmd_q *cmdq, int key)
1.5       nicm       91: {
1.10      nicm       92:        struct args                     *args = self->args;
                     93:        const char                      *tablename;
1.5       nicm       94:        const struct mode_key_table     *mtab;
                     95:        struct mode_key_binding         *mbind, mtmp;
                     96:        enum mode_key_cmd                cmd;
1.15      nicm       97:        const char                      *arg;
1.5       nicm       98:
1.10      nicm       99:        tablename = args_get(args, 't');
                    100:        if ((mtab = mode_key_findtable(tablename)) == NULL) {
1.16    ! nicm      101:                cmdq_error(cmdq, "unknown key table: %s", tablename);
1.14      nicm      102:                return (CMD_RETURN_ERROR);
1.5       nicm      103:        }
                    104:
1.11      nicm      105:        cmd = mode_key_fromstring(mtab->cmdstr, args->argv[1]);
1.5       nicm      106:        if (cmd == MODEKEY_NONE) {
1.16    ! nicm      107:                cmdq_error(cmdq, "unknown command: %s", args->argv[1]);
1.14      nicm      108:                return (CMD_RETURN_ERROR);
1.5       nicm      109:        }
1.7       nicm      110:
1.15      nicm      111:        if (cmd != MODEKEYCOPY_COPYPIPE) {
                    112:                if (args->argc != 2) {
1.16    ! nicm      113:                        cmdq_error(cmdq, "no argument allowed");
1.15      nicm      114:                        return (CMD_RETURN_ERROR);
                    115:                }
                    116:                arg = NULL;
                    117:        } else {
                    118:                if (args->argc != 3) {
1.16    ! nicm      119:                        cmdq_error(cmdq, "no argument given");
1.15      nicm      120:                        return (CMD_RETURN_ERROR);
                    121:                }
                    122:                arg = args->argv[2];
                    123:        }
                    124:
1.10      nicm      125:        mtmp.key = key;
                    126:        mtmp.mode = !!args_has(args, 'c');
1.15      nicm      127:        if ((mbind = RB_FIND(mode_key_tree, mtab->tree, &mtmp)) == NULL) {
                    128:                mbind = xmalloc(sizeof *mbind);
                    129:                mbind->key = mtmp.key;
                    130:                mbind->mode = mtmp.mode;
                    131:                RB_INSERT(mode_key_tree, mtab->tree, mbind);
                    132:        }
1.5       nicm      133:        mbind->cmd = cmd;
1.15      nicm      134:        mbind->arg = arg != NULL ? xstrdup(arg) : NULL;
1.14      nicm      135:        return (CMD_RETURN_NORMAL);
1.1       nicm      136: }