1 /* $OpenBSD: cmd-unbind-key.c,v 1.22 2015/11/12 11:05:34 nicm Exp $ */ 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 21 #include <stdlib.h> 22 23 #include "tmux.h" 24 25 /* 26 * Unbind key from command. 27 */ 28 29 enum cmd_retval cmd_unbind_key_exec(struct cmd *, struct cmd_q *); 30 enum cmd_retval cmd_unbind_key_mode_table(struct cmd *, struct cmd_q *, 31 key_code); 32 33 const struct cmd_entry cmd_unbind_key_entry = { 34 "unbind-key", "unbind", 35 "acnt:T:", 0, 1, 36 "[-acn] [-t mode-table] [-T key-table] key", 37 0, 38 cmd_unbind_key_exec 39 }; 40 41 enum cmd_retval 42 cmd_unbind_key_exec(struct cmd *self, struct cmd_q *cmdq) 43 { 44 struct args *args = self->args; 45 key_code key; 46 const char *tablename; 47 48 if (!args_has(args, 'a')) { 49 if (args->argc != 1) { 50 cmdq_error(cmdq, "missing key"); 51 return (CMD_RETURN_ERROR); 52 } 53 key = key_string_lookup_string(args->argv[0]); 54 if (key == KEYC_NONE) { 55 cmdq_error(cmdq, "unknown key: %s", args->argv[0]); 56 return (CMD_RETURN_ERROR); 57 } 58 } else { 59 if (args->argc != 0) { 60 cmdq_error(cmdq, "key given with -a"); 61 return (CMD_RETURN_ERROR); 62 } 63 key = KEYC_NONE; 64 } 65 66 if (args_has(args, 't')) 67 return (cmd_unbind_key_mode_table(self, cmdq, key)); 68 69 if (key == KEYC_NONE) { 70 tablename = args_get(args, 'T'); 71 if (tablename == NULL) { 72 key_bindings_remove_table("root"); 73 key_bindings_remove_table("prefix"); 74 return (CMD_RETURN_NORMAL); 75 } 76 if (key_bindings_get_table(tablename, 0) == NULL) { 77 cmdq_error(cmdq, "table %s doesn't exist", tablename); 78 return (CMD_RETURN_ERROR); 79 } 80 key_bindings_remove_table(tablename); 81 return (CMD_RETURN_NORMAL); 82 } 83 84 if (args_has(args, 'T')) { 85 tablename = args_get(args, 'T'); 86 if (key_bindings_get_table(tablename, 0) == NULL) { 87 cmdq_error(cmdq, "table %s doesn't exist", tablename); 88 return (CMD_RETURN_ERROR); 89 } 90 } else if (args_has(args, 'n')) 91 tablename = "root"; 92 else 93 tablename = "prefix"; 94 key_bindings_remove(tablename, key); 95 return (CMD_RETURN_NORMAL); 96 } 97 98 enum cmd_retval 99 cmd_unbind_key_mode_table(struct cmd *self, struct cmd_q *cmdq, key_code key) 100 { 101 struct args *args = self->args; 102 const char *tablename; 103 const struct mode_key_table *mtab; 104 struct mode_key_binding *mbind, mtmp; 105 106 tablename = args_get(args, 't'); 107 if ((mtab = mode_key_findtable(tablename)) == NULL) { 108 cmdq_error(cmdq, "unknown key table: %s", tablename); 109 return (CMD_RETURN_ERROR); 110 } 111 112 if (key == KEYC_NONE) { 113 while (!RB_EMPTY(mtab->tree)) { 114 mbind = RB_ROOT(mtab->tree); 115 RB_REMOVE(mode_key_tree, mtab->tree, mbind); 116 free(mbind); 117 } 118 return (CMD_RETURN_NORMAL); 119 } 120 121 mtmp.key = key; 122 mtmp.mode = !!args_has(args, 'c'); 123 if ((mbind = RB_FIND(mode_key_tree, mtab->tree, &mtmp)) != NULL) { 124 RB_REMOVE(mode_key_tree, mtab->tree, mbind); 125 free(mbind); 126 } 127 return (CMD_RETURN_NORMAL); 128 } 129