1 /* $OpenBSD: cmd-unbind-key.c,v 1.7 2010/01/23 17:50:56 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 "tmux.h" 22 23 /* 24 * Unbind key from command. 25 */ 26 27 int cmd_unbind_key_parse(struct cmd *, int, char **, char **); 28 int cmd_unbind_key_exec(struct cmd *, struct cmd_ctx *); 29 void cmd_unbind_key_free(struct cmd *); 30 31 int cmd_unbind_key_table(struct cmd *, struct cmd_ctx *); 32 33 struct cmd_unbind_key_data { 34 int key; 35 36 int command_key; 37 char *tablename; 38 }; 39 40 const struct cmd_entry cmd_unbind_key_entry = { 41 "unbind-key", "unbind", 42 "[-cn] [-t key-table] key", 43 0, "", 44 NULL, 45 cmd_unbind_key_parse, 46 cmd_unbind_key_exec, 47 cmd_unbind_key_free, 48 NULL 49 }; 50 51 int 52 cmd_unbind_key_parse(struct cmd *self, int argc, char **argv, char **cause) 53 { 54 struct cmd_unbind_key_data *data; 55 int opt, no_prefix = 0; 56 57 self->data = data = xmalloc(sizeof *data); 58 data->command_key = 0; 59 data->tablename = NULL; 60 61 while ((opt = getopt(argc, argv, "cnt:")) != -1) { 62 switch (opt) { 63 case 'c': 64 data->command_key = 1; 65 break; 66 case 'n': 67 no_prefix = 1; 68 break; 69 case 't': 70 if (data->tablename == NULL) 71 data->tablename = xstrdup(optarg); 72 break; 73 default: 74 goto usage; 75 } 76 } 77 argc -= optind; 78 argv += optind; 79 if (argc != 1) 80 goto usage; 81 82 if ((data->key = key_string_lookup_string(argv[0])) == KEYC_NONE) { 83 xasprintf(cause, "unknown key: %s", argv[0]); 84 goto error; 85 } 86 if (!no_prefix) 87 data->key |= KEYC_PREFIX; 88 89 return (0); 90 91 usage: 92 xasprintf(cause, "usage: %s %s", self->entry->name, self->entry->usage); 93 94 error: 95 xfree(data); 96 return (-1); 97 } 98 99 int 100 cmd_unbind_key_exec(struct cmd *self, unused struct cmd_ctx *ctx) 101 { 102 struct cmd_unbind_key_data *data = self->data; 103 104 if (data == NULL) 105 return (0); 106 if (data->tablename != NULL) 107 return (cmd_unbind_key_table(self, ctx)); 108 109 key_bindings_remove(data->key); 110 111 return (0); 112 } 113 114 int 115 cmd_unbind_key_table(struct cmd *self, struct cmd_ctx *ctx) 116 { 117 struct cmd_unbind_key_data *data = self->data; 118 const struct mode_key_table *mtab; 119 struct mode_key_binding *mbind, mtmp; 120 121 if ((mtab = mode_key_findtable(data->tablename)) == NULL) { 122 ctx->error(ctx, "unknown key table: %s", data->tablename); 123 return (-1); 124 } 125 126 mtmp.key = data->key & ~KEYC_PREFIX; 127 mtmp.mode = data->command_key ? 1 : 0; 128 if ((mbind = SPLAY_FIND(mode_key_tree, mtab->tree, &mtmp)) != NULL) { 129 SPLAY_REMOVE(mode_key_tree, mtab->tree, mbind); 130 xfree(mbind); 131 } 132 return (0); 133 } 134 135 void 136 cmd_unbind_key_free(struct cmd *self) 137 { 138 struct cmd_unbind_key_data *data = self->data; 139 140 if (data->tablename != NULL) 141 xfree(data->tablename); 142 xfree(data); 143 } 144