1 /* $OpenBSD: cmd-bind-key.c,v 1.31 2016/10/16 19:04:05 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, this recurses through cmd_*. 28 */ 29 30 static enum cmd_retval cmd_bind_key_exec(struct cmd *, struct cmdq_item *); 31 32 static enum cmd_retval cmd_bind_key_mode_table(struct cmd *, 33 struct cmdq_item *, key_code); 34 35 const struct cmd_entry cmd_bind_key_entry = { 36 .name = "bind-key", 37 .alias = "bind", 38 39 .args = { "cnrt:T:", 1, -1 }, 40 .usage = "[-cnr] [-t mode-table] [-T key-table] key " 41 "command [arguments]", 42 43 .flags = CMD_AFTERHOOK, 44 .exec = cmd_bind_key_exec 45 }; 46 47 static enum cmd_retval 48 cmd_bind_key_exec(struct cmd *self, struct cmdq_item *item) 49 { 50 struct args *args = self->args; 51 char *cause; 52 struct cmd_list *cmdlist; 53 key_code key; 54 const char *tablename; 55 56 if (args_has(args, 't')) { 57 if (args->argc != 2 && args->argc != 3) { 58 cmdq_error(item, "not enough arguments"); 59 return (CMD_RETURN_ERROR); 60 } 61 } else { 62 if (args->argc < 2) { 63 cmdq_error(item, "not enough arguments"); 64 return (CMD_RETURN_ERROR); 65 } 66 } 67 68 key = key_string_lookup_string(args->argv[0]); 69 if (key == KEYC_NONE || key == KEYC_UNKNOWN) { 70 cmdq_error(item, "unknown key: %s", args->argv[0]); 71 return (CMD_RETURN_ERROR); 72 } 73 74 if (args_has(args, 't')) 75 return (cmd_bind_key_mode_table(self, item, key)); 76 77 if (args_has(args, 'T')) 78 tablename = args_get(args, 'T'); 79 else if (args_has(args, 'n')) 80 tablename = "root"; 81 else 82 tablename = "prefix"; 83 84 cmdlist = cmd_list_parse(args->argc - 1, args->argv + 1, NULL, 0, 85 &cause); 86 if (cmdlist == NULL) { 87 cmdq_error(item, "%s", cause); 88 free(cause); 89 return (CMD_RETURN_ERROR); 90 } 91 92 key_bindings_add(tablename, key, args_has(args, 'r'), cmdlist); 93 return (CMD_RETURN_NORMAL); 94 } 95 96 static enum cmd_retval 97 cmd_bind_key_mode_table(struct cmd *self, struct cmdq_item *item, key_code key) 98 { 99 struct args *args = self->args; 100 const char *tablename; 101 const struct mode_key_table *mtab; 102 struct mode_key_binding *mbind, mtmp; 103 enum mode_key_cmd cmd; 104 105 tablename = args_get(args, 't'); 106 if ((mtab = mode_key_findtable(tablename)) == NULL) { 107 cmdq_error(item, "unknown key table: %s", tablename); 108 return (CMD_RETURN_ERROR); 109 } 110 111 cmd = mode_key_fromstring(mtab->cmdstr, args->argv[1]); 112 if (cmd == MODEKEY_NONE) { 113 cmdq_error(item, "unknown command: %s", args->argv[1]); 114 return (CMD_RETURN_ERROR); 115 } 116 117 if (args->argc != 2) { 118 cmdq_error(item, "no argument allowed"); 119 return (CMD_RETURN_ERROR); 120 } 121 122 mtmp.key = key; 123 if ((mbind = RB_FIND(mode_key_tree, mtab->tree, &mtmp)) == NULL) { 124 mbind = xmalloc(sizeof *mbind); 125 mbind->key = mtmp.key; 126 RB_INSERT(mode_key_tree, mtab->tree, mbind); 127 } 128 mbind->cmd = cmd; 129 return (CMD_RETURN_NORMAL); 130 } 131