xref: /openbsd-src/usr.bin/tmux/cmd-bind-key.c (revision f27c0343fcb44e6c4449f1410c781ac0a416a3e1)
1 /* $OpenBSD: cmd-bind-key.c,v 1.20 2015/04/10 16:00:08 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 <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 *, int);
33 
34 const struct cmd_entry cmd_bind_key_entry = {
35 	"bind-key", "bind",
36 	"cnrt:", 1, -1,
37 	"[-cnr] [-t mode-table] key command [arguments]",
38 	0,
39 	cmd_bind_key_exec
40 };
41 
42 enum cmd_retval
43 cmd_bind_key_exec(struct cmd *self, struct cmd_q *cmdq)
44 {
45 	struct args	*args = self->args;
46 	char		*cause;
47 	struct cmd_list	*cmdlist;
48 	int		 key;
49 
50 	if (args_has(args, 't')) {
51 		if (args->argc != 2 && args->argc != 3) {
52 			cmdq_error(cmdq, "not enough arguments");
53 			return (CMD_RETURN_ERROR);
54 		}
55 	} else {
56 		if (args->argc < 2) {
57 			cmdq_error(cmdq, "not enough arguments");
58 			return (CMD_RETURN_ERROR);
59 		}
60 	}
61 
62 	key = key_string_lookup_string(args->argv[0]);
63 	if (key == KEYC_NONE) {
64 		cmdq_error(cmdq, "unknown key: %s", args->argv[0]);
65 		return (CMD_RETURN_ERROR);
66 	}
67 
68 	if (args_has(args, 't'))
69 		return (cmd_bind_key_mode_table(self, cmdq, key));
70 
71 	cmdlist = cmd_list_parse(args->argc - 1, args->argv + 1, NULL, 0,
72 	    &cause);
73 	if (cmdlist == NULL) {
74 		cmdq_error(cmdq, "%s", cause);
75 		free(cause);
76 		return (CMD_RETURN_ERROR);
77 	}
78 
79 	if (!args_has(args, 'n'))
80 	    key |= KEYC_PREFIX;
81 	key_bindings_add(key, args_has(args, 'r'), cmdlist);
82 	return (CMD_RETURN_NORMAL);
83 }
84 
85 enum cmd_retval
86 cmd_bind_key_mode_table(struct cmd *self, struct cmd_q *cmdq, int key)
87 {
88 	struct args			*args = self->args;
89 	const char			*tablename;
90 	const struct mode_key_table	*mtab;
91 	struct mode_key_binding		*mbind, mtmp;
92 	enum mode_key_cmd		 cmd;
93 	const char			*arg;
94 
95 	tablename = args_get(args, 't');
96 	if ((mtab = mode_key_findtable(tablename)) == NULL) {
97 		cmdq_error(cmdq, "unknown key table: %s", tablename);
98 		return (CMD_RETURN_ERROR);
99 	}
100 
101 	cmd = mode_key_fromstring(mtab->cmdstr, args->argv[1]);
102 	if (cmd == MODEKEY_NONE) {
103 		cmdq_error(cmdq, "unknown command: %s", args->argv[1]);
104 		return (CMD_RETURN_ERROR);
105 	}
106 
107 	switch (cmd) {
108 	case MODEKEYCOPY_APPENDSELECTION:
109 	case MODEKEYCOPY_COPYSELECTION:
110 	case MODEKEYCOPY_STARTNAMEDBUFFER:
111 		if (args->argc == 2)
112 			arg = NULL;
113 		else {
114 			arg = args->argv[2];
115 			if (strcmp(arg, "-x") != 0) {
116 				cmdq_error(cmdq, "unknown argument");
117 				return (CMD_RETURN_ERROR);
118 			}
119 		}
120 		break;
121 	case MODEKEYCOPY_COPYPIPE:
122 		if (args->argc != 3) {
123 			cmdq_error(cmdq, "no argument given");
124 			return (CMD_RETURN_ERROR);
125 		}
126 		arg = args->argv[2];
127 		break;
128 	default:
129 		if (args->argc != 2) {
130 			cmdq_error(cmdq, "no argument allowed");
131 			return (CMD_RETURN_ERROR);
132 		}
133 		arg = NULL;
134 		break;
135 	}
136 
137 	mtmp.key = key;
138 	mtmp.mode = !!args_has(args, 'c');
139 	if ((mbind = RB_FIND(mode_key_tree, mtab->tree, &mtmp)) == NULL) {
140 		mbind = xmalloc(sizeof *mbind);
141 		mbind->key = mtmp.key;
142 		mbind->mode = mtmp.mode;
143 		RB_INSERT(mode_key_tree, mtab->tree, mbind);
144 	}
145 	mbind->cmd = cmd;
146 	mbind->arg = arg != NULL ? xstrdup(arg) : NULL;
147 	return (CMD_RETURN_NORMAL);
148 }
149