1  | 
     | 
     | 
    /* $OpenBSD: cmd-bind-key.c,v 1.32 2017/01/24 21:50:22 nicm Exp $ */  | 
    
    
    2  | 
     | 
     | 
     | 
    
    
    3  | 
     | 
     | 
    /*  | 
    
    
    4  | 
     | 
     | 
     * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>  | 
    
    
    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  | 
     | 
     | 
     | 
    
    
    21  | 
     | 
     | 
    #include <stdlib.h>  | 
    
    
    22  | 
     | 
     | 
    #include <string.h>  | 
    
    
    23  | 
     | 
     | 
     | 
    
    
    24  | 
     | 
     | 
    #include "tmux.h"  | 
    
    
    25  | 
     | 
     | 
     | 
    
    
    26  | 
     | 
     | 
    /*  | 
    
    
    27  | 
     | 
     | 
     * Bind a key to a command.  | 
    
    
    28  | 
     | 
     | 
     */  | 
    
    
    29  | 
     | 
     | 
     | 
    
    
    30  | 
     | 
     | 
    static enum cmd_retval	cmd_bind_key_exec(struct cmd *, struct cmdq_item *);  | 
    
    
    31  | 
     | 
     | 
     | 
    
    
    32  | 
     | 
     | 
    const struct cmd_entry cmd_bind_key_entry = { | 
    
    
    33  | 
     | 
     | 
    	.name = "bind-key",  | 
    
    
    34  | 
     | 
     | 
    	.alias = "bind",  | 
    
    
    35  | 
     | 
     | 
     | 
    
    
    36  | 
     | 
     | 
    	.args = { "cnrT:", 2, -1 }, | 
    
    
    37  | 
     | 
     | 
    	.usage = "[-cnr] [-T key-table] key "  | 
    
    
    38  | 
     | 
     | 
    	         "command [arguments]",  | 
    
    
    39  | 
     | 
     | 
     | 
    
    
    40  | 
     | 
     | 
    	.flags = CMD_AFTERHOOK,  | 
    
    
    41  | 
     | 
     | 
    	.exec = cmd_bind_key_exec  | 
    
    
    42  | 
     | 
     | 
    };  | 
    
    
    43  | 
     | 
     | 
     | 
    
    
    44  | 
     | 
     | 
    static enum cmd_retval  | 
    
    
    45  | 
     | 
     | 
    cmd_bind_key_exec(struct cmd *self, struct cmdq_item *item)  | 
    
    
    46  | 
     | 
     | 
    { | 
    
    
    47  | 
     | 
     | 
    	struct args	*args = self->args;  | 
    
    
    48  | 
     | 
     | 
    	char		*cause;  | 
    
    
    49  | 
     | 
     | 
    	struct cmd_list	*cmdlist;  | 
    
    
    50  | 
     | 
     | 
    	key_code	 key;  | 
    
    
    51  | 
     | 
     | 
    	const char	*tablename;  | 
    
    
    52  | 
     | 
     | 
     | 
    
    
    53  | 
     | 
     | 
    	key = key_string_lookup_string(args->argv[0]);  | 
    
    
    54  | 
     | 
     | 
    	if (key == KEYC_NONE || key == KEYC_UNKNOWN) { | 
    
    
    55  | 
     | 
     | 
    		cmdq_error(item, "unknown key: %s", args->argv[0]);  | 
    
    
    56  | 
     | 
     | 
    		return (CMD_RETURN_ERROR);  | 
    
    
    57  | 
     | 
     | 
    	}  | 
    
    
    58  | 
     | 
     | 
     | 
    
    
    59  | 
     | 
     | 
    	if (args_has(args, 'T'))  | 
    
    
    60  | 
     | 
     | 
    		tablename = args_get(args, 'T');  | 
    
    
    61  | 
     | 
     | 
    	else if (args_has(args, 'n'))  | 
    
    
    62  | 
     | 
     | 
    		tablename = "root";  | 
    
    
    63  | 
     | 
     | 
    	else  | 
    
    
    64  | 
     | 
     | 
    		tablename = "prefix";  | 
    
    
    65  | 
     | 
     | 
     | 
    
    
    66  | 
     | 
     | 
    	cmdlist = cmd_list_parse(args->argc - 1, args->argv + 1, NULL, 0,  | 
    
    
    67  | 
     | 
     | 
    	    &cause);  | 
    
    
    68  | 
     | 
     | 
    	if (cmdlist == NULL) { | 
    
    
    69  | 
     | 
     | 
    		cmdq_error(item, "%s", cause);  | 
    
    
    70  | 
     | 
     | 
    		free(cause);  | 
    
    
    71  | 
     | 
     | 
    		return (CMD_RETURN_ERROR);  | 
    
    
    72  | 
     | 
     | 
    	}  | 
    
    
    73  | 
     | 
     | 
     | 
    
    
    74  | 
     | 
     | 
    	key_bindings_add(tablename, key, args_has(args, 'r'), cmdlist);  | 
    
    
    75  | 
     | 
     | 
    	return (CMD_RETURN_NORMAL);  | 
    
    
    76  | 
     | 
     | 
    }  |