xref: /openbsd-src/usr.bin/tmux/cmd-bind-key.c (revision f16b02c6671a9f86e94d692cf0cf55f13a5e7924)
1 /* $OpenBSD: cmd-bind-key.c,v 1.43 2021/08/23 11:48:21 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.
28  */
29 
30 static enum cmd_retval	cmd_bind_key_exec(struct cmd *, struct cmdq_item *);
31 
32 const struct cmd_entry cmd_bind_key_entry = {
33 	.name = "bind-key",
34 	.alias = "bind",
35 
36 	.args = { "nrN:T:", 1, -1, NULL },
37 	.usage = "[-nr] [-T key-table] [-N note] key "
38 	         "[command [arguments]]",
39 
40 	.flags = CMD_AFTERHOOK,
41 	.exec = cmd_bind_key_exec
42 };
43 
44 static enum cmd_retval
45 cmd_bind_key_exec(struct cmd *self, struct cmdq_item *item)
46 {
47 	struct args		 *args = cmd_get_args(self);
48 	key_code		  key;
49 	const char		 *tablename, *note = args_get(args, 'N');
50 	struct cmd_parse_result	 *pr;
51 	char			**argv;
52 	int			  argc, repeat;
53 	struct args_value	 *value;
54 	u_int			  count = args_count(args);
55 
56 	key = key_string_lookup_string(args_string(args, 0));
57 	if (key == KEYC_NONE || key == KEYC_UNKNOWN) {
58 		cmdq_error(item, "unknown key: %s", args_string(args, 0));
59 		return (CMD_RETURN_ERROR);
60 	}
61 
62 	if (args_has(args, 'T'))
63 		tablename = args_get(args, 'T');
64 	else if (args_has(args, 'n'))
65 		tablename = "root";
66 	else
67 		tablename = "prefix";
68 	repeat = args_has(args, 'r');
69 
70 	if (count == 1) {
71 		key_bindings_add(tablename, key, note, repeat, NULL);
72 		return (CMD_RETURN_NORMAL);
73 	}
74 
75 	value = args_value(args, 1);
76 	if (count == 2 && value->type == ARGS_COMMANDS) {
77 		key_bindings_add(tablename, key, note, repeat, value->cmdlist);
78 		value->cmdlist->references++;
79 		return (CMD_RETURN_NORMAL);
80 	}
81 
82 	if (count == 2)
83 		pr = cmd_parse_from_string(args_string(args, 1), NULL);
84 	else {
85 		args_vector(args, &argc, &argv);
86 		pr = cmd_parse_from_arguments(argc - 1, argv + 1, NULL);
87 		cmd_free_argv(argc, argv);
88 	}
89 	switch (pr->status) {
90 	case CMD_PARSE_ERROR:
91 		cmdq_error(item, "%s", pr->error);
92 		free(pr->error);
93 		return (CMD_RETURN_ERROR);
94 	case CMD_PARSE_SUCCESS:
95 		break;
96 	}
97 	key_bindings_add(tablename, key, note, repeat, pr->cmdlist);
98 	return (CMD_RETURN_NORMAL);
99 }
100