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

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