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

Line Branch Exec Source
1
/* $OpenBSD: cmd-bind-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
#include <string.h>
23
24
#include "tmux.h"
25
26
/*
27
 * Bind a key to a command, this recurses through cmd_*.
28
 */
29
30
enum cmd_retval	 cmd_bind_key_exec(struct cmd *, struct cmd_q *);
31
32
enum cmd_retval	 cmd_bind_key_mode_table(struct cmd *, struct cmd_q *,
33
		     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 command "
41
		 "[arguments]",
42
43
	.flags = 0,
44
	.exec = cmd_bind_key_exec
45
};
46
47
enum cmd_retval
48
cmd_bind_key_exec(struct cmd *self, struct cmd_q *cmdq)
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(cmdq, "not enough arguments");
59
			return (CMD_RETURN_ERROR);
60
		}
61
	} else {
62
		if (args->argc < 2) {
63
			cmdq_error(cmdq, "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(cmdq, "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, cmdq, 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(cmdq, "%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
enum cmd_retval
97
cmd_bind_key_mode_table(struct cmd *self, struct cmd_q *cmdq, 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
	const char			*arg;
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
	cmd = mode_key_fromstring(mtab->cmdstr, args->argv[1]);
113
	if (cmd == MODEKEY_NONE) {
114
		cmdq_error(cmdq, "unknown command: %s", args->argv[1]);
115
		return (CMD_RETURN_ERROR);
116
	}
117
118
	switch (cmd) {
119
	case MODEKEYCOPY_APPENDSELECTION:
120
	case MODEKEYCOPY_COPYSELECTION:
121
	case MODEKEYCOPY_STARTNAMEDBUFFER:
122
		if (args->argc == 2)
123
			arg = NULL;
124
		else {
125
			arg = args->argv[2];
126
			if (strcmp(arg, "-x") != 0) {
127
				cmdq_error(cmdq, "unknown argument");
128
				return (CMD_RETURN_ERROR);
129
			}
130
		}
131
		break;
132
	case MODEKEYCOPY_COPYPIPE:
133
		if (args->argc != 3) {
134
			cmdq_error(cmdq, "no argument given");
135
			return (CMD_RETURN_ERROR);
136
		}
137
		arg = args->argv[2];
138
		break;
139
	default:
140
		if (args->argc != 2) {
141
			cmdq_error(cmdq, "no argument allowed");
142
			return (CMD_RETURN_ERROR);
143
		}
144
		arg = NULL;
145
		break;
146
	}
147
148
	mtmp.key = key;
149
	mtmp.mode = !!args_has(args, 'c');
150
	if ((mbind = RB_FIND(mode_key_tree, mtab->tree, &mtmp)) == NULL) {
151
		mbind = xmalloc(sizeof *mbind);
152
		mbind->key = mtmp.key;
153
		mbind->mode = mtmp.mode;
154
		RB_INSERT(mode_key_tree, mtab->tree, mbind);
155
	}
156
	mbind->cmd = cmd;
157
	mbind->arg = arg != NULL ? xstrdup(arg) : NULL;
158
	return (CMD_RETURN_NORMAL);
159
}