xref: /openbsd-src/usr.bin/tmux/cmd-send-keys.c (revision 8e0c768258d4632c51876b4397034bc3152bf8db)
1 /* $OpenBSD: cmd-send-keys.c,v 1.50 2019/07/10 14:33:24 nicm Exp $ */
2 
3 /*
4  * Copyright (c) 2008 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  * Send keys to client.
28  */
29 
30 static enum cmd_retval	cmd_send_keys_exec(struct cmd *, struct cmdq_item *);
31 
32 const struct cmd_entry cmd_send_keys_entry = {
33 	.name = "send-keys",
34 	.alias = "send",
35 
36 	.args = { "HlXRMN:t:", 0, -1 },
37 	.usage = "[-HlXRM] [-N repeat-count] " CMD_TARGET_PANE_USAGE " key ...",
38 
39 	.target = { 't', CMD_FIND_PANE, 0 },
40 
41 	.flags = CMD_AFTERHOOK,
42 	.exec = cmd_send_keys_exec
43 };
44 
45 const struct cmd_entry cmd_send_prefix_entry = {
46 	.name = "send-prefix",
47 	.alias = NULL,
48 
49 	.args = { "2t:", 0, 0 },
50 	.usage = "[-2] " CMD_TARGET_PANE_USAGE,
51 
52 	.target = { 't', CMD_FIND_PANE, 0 },
53 
54 	.flags = CMD_AFTERHOOK,
55 	.exec = cmd_send_keys_exec
56 };
57 
58 static struct cmdq_item *
59 cmd_send_keys_inject_key(struct client *c, struct cmd_find_state *fs,
60     struct cmdq_item *item, key_code key)
61 {
62 	struct window_mode_entry	*wme;
63 	struct key_table		*table;
64 	struct key_binding		*bd;
65 
66 	wme = TAILQ_FIRST(&fs->wp->modes);
67 	if (wme == NULL || wme->mode->key_table == NULL) {
68 		if (options_get_number(fs->wp->window->options, "xterm-keys"))
69 			key |= KEYC_XTERM;
70 		window_pane_key(fs->wp, item->client, fs->s, fs->wl, key, NULL);
71 		return (item);
72 	}
73 	table = key_bindings_get_table(wme->mode->key_table(wme), 1);
74 
75 	bd = key_bindings_get(table, key & ~KEYC_XTERM);
76 	if (bd != NULL) {
77 		table->references++;
78 		item = key_bindings_dispatch(bd, item, c, NULL, &item->target);
79 		key_bindings_unref_table(table);
80 	}
81 	return (item);
82 }
83 
84 static struct cmdq_item *
85 cmd_send_keys_inject_string(struct client *c, struct cmd_find_state *fs,
86     struct cmdq_item *item, struct args *args, int i)
87 {
88 	const char		*s = args->argv[i];
89 	struct utf8_data	*ud, *uc;
90 	wchar_t			 wc;
91 	key_code		 key;
92 	char			*endptr;
93 	long			 n;
94 	int			 literal;
95 
96 	if (args_has(args, 'H')) {
97 		n = strtol(s, &endptr, 16);
98 		if (*s =='\0' || n < 0 || n > 0xff || *endptr != '\0')
99 			return (item);
100 		return (cmd_send_keys_inject_key(c, fs, item, KEYC_LITERAL|n));
101 	}
102 
103 	literal = args_has(args, 'l');
104 	if (!literal) {
105 		key = key_string_lookup_string(s);
106 		if (key != KEYC_NONE && key != KEYC_UNKNOWN)
107 			return (cmd_send_keys_inject_key(c, fs, item, key));
108 		literal = 1;
109 	}
110 	if (literal) {
111 		ud = utf8_fromcstr(s);
112 		for (uc = ud; uc->size != 0; uc++) {
113 			if (utf8_combine(uc, &wc) != UTF8_DONE)
114 				continue;
115 			item = cmd_send_keys_inject_key(c, fs, item, wc);
116 		}
117 		free(ud);
118 	}
119 	return (item);
120 }
121 
122 static enum cmd_retval
123 cmd_send_keys_exec(struct cmd *self, struct cmdq_item *item)
124 {
125 	struct args			*args = self->args;
126 	struct client			*c = cmd_find_client(item, NULL, 1);
127 	struct cmd_find_state		*fs = &item->target;
128 	struct window_pane		*wp = item->target.wp;
129 	struct session			*s = item->target.s;
130 	struct winlink			*wl = item->target.wl;
131 	struct mouse_event		*m = &item->shared->mouse;
132 	struct window_mode_entry	*wme = TAILQ_FIRST(&wp->modes);
133 	int				 i;
134 	key_code			 key;
135 	u_int				 np = 1;
136 	char				*cause = NULL;
137 
138 	if (args_has(args, 'N')) {
139 		np = args_strtonum(args, 'N', 1, UINT_MAX, &cause);
140 		if (cause != NULL) {
141 			cmdq_error(item, "repeat count %s", cause);
142 			free(cause);
143 			return (CMD_RETURN_ERROR);
144 		}
145 		if (wme != NULL && (args_has(args, 'X') || args->argc == 0)) {
146 			if (wme == NULL || wme->mode->command == NULL) {
147 				cmdq_error(item, "not in a mode");
148 				return (CMD_RETURN_ERROR);
149 			}
150 			wme->prefix = np;
151 		}
152 	}
153 
154 	if (args_has(args, 'X')) {
155 		if (wme == NULL || wme->mode->command == NULL) {
156 			cmdq_error(item, "not in a mode");
157 			return (CMD_RETURN_ERROR);
158 		}
159 		if (!m->valid)
160 			m = NULL;
161 		wme->mode->command(wme, c, s, wl, args, m);
162 		return (CMD_RETURN_NORMAL);
163 	}
164 
165 	if (args_has(args, 'M')) {
166 		wp = cmd_mouse_pane(m, &s, NULL);
167 		if (wp == NULL) {
168 			cmdq_error(item, "no mouse target");
169 			return (CMD_RETURN_ERROR);
170 		}
171 		window_pane_key(wp, item->client, s, wl, m->key, m);
172 		return (CMD_RETURN_NORMAL);
173 	}
174 
175 	if (self->entry == &cmd_send_prefix_entry) {
176 		if (args_has(args, '2'))
177 			key = options_get_number(s->options, "prefix2");
178 		else
179 			key = options_get_number(s->options, "prefix");
180 		cmd_send_keys_inject_key(c, fs, item, key);
181 		return (CMD_RETURN_NORMAL);
182 	}
183 
184 	if (args_has(args, 'R')) {
185 		window_pane_reset_palette(wp);
186 		input_reset(wp, 1);
187 	}
188 
189 	for (; np != 0; np--) {
190 		for (i = 0; i < args->argc; i++)
191 			item = cmd_send_keys_inject_string(c, fs, item, args, i);
192 	}
193 
194 	return (CMD_RETURN_NORMAL);
195 }
196