xref: /openbsd-src/usr.bin/tmux/cmd-unbind-key.c (revision 43003dfe3ad45d1698bed8a37f2b0f5b14f20d4f)
1 /* $OpenBSD: cmd-unbind-key.c,v 1.5 2009/07/28 17:05:10 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, 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 			data->tablename = xstrdup(optarg);
71 			break;
72 		default:
73 			goto usage;
74 		}
75 	}
76 	argc -= optind;
77 	argv += optind;
78 	if (argc != 1)
79 		goto usage;
80 
81 	if ((data->key = key_string_lookup_string(argv[0])) == KEYC_NONE) {
82 		xasprintf(cause, "unknown key: %s", argv[0]);
83 		goto error;
84 	}
85 	if (!no_prefix)
86 		data->key |= KEYC_PREFIX;
87 
88 	return (0);
89 
90 usage:
91 	xasprintf(cause, "usage: %s %s", self->entry->name, self->entry->usage);
92 
93 error:
94 	xfree(data);
95 	return (-1);
96 }
97 
98 int
99 cmd_unbind_key_exec(struct cmd *self, unused struct cmd_ctx *ctx)
100 {
101 	struct cmd_unbind_key_data	*data = self->data;
102 
103 	if (data == NULL)
104 		return (0);
105 	if (data->tablename != NULL)
106 		return (cmd_unbind_key_table(self, ctx));
107 
108 	key_bindings_remove(data->key);
109 
110 	return (0);
111 }
112 
113 int
114 cmd_unbind_key_table(struct cmd *self, struct cmd_ctx *ctx)
115 {
116 	struct cmd_unbind_key_data	*data = self->data;
117 	const struct mode_key_table	*mtab;
118 	struct mode_key_binding		*mbind, mtmp;
119 
120 	if ((mtab = mode_key_findtable(data->tablename)) == NULL) {
121 		ctx->error(ctx, "unknown key table: %s", data->tablename);
122 		return (-1);
123 	}
124 
125 	mtmp.key = data->key & ~KEYC_PREFIX;
126 	mtmp.mode = data->command_key ? 1 : 0;
127 	if ((mbind = SPLAY_FIND(mode_key_tree, mtab->tree, &mtmp)) != NULL) {
128 		SPLAY_REMOVE(mode_key_tree, mtab->tree, mbind);
129 		xfree(mbind);
130 	}
131 	return (0);
132 }
133 
134 void
135 cmd_unbind_key_free(struct cmd *self)
136 {
137 	struct cmd_unbind_key_data	*data = self->data;
138 
139 	if (data->tablename != NULL)
140 		xfree(data->tablename);
141 	xfree(data);
142 }
143