GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.bin/tmux/cmd-unbind-key.c Lines: 0 53 0.0 %
Date: 2016-12-06 Branches: 0 30 0.0 %

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