xref: /openbsd-src/usr.bin/tmux/cmd-send-keys.c (revision 4b70baf6e17fc8b27fc1f7fa7929335753fa94c3)
1 /* $OpenBSD: cmd-send-keys.c,v 1.46 2019/03/12 11:16:50 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 = { "lXRMN:t:", 0, -1 },
37 	.usage = "[-lXRM] [-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 void
59 cmd_send_keys_inject(struct client *c, struct cmdq_item *item, key_code key)
60 {
61 	struct window_pane		*wp = item->target.wp;
62 	struct session			*s = item->target.s;
63 	struct winlink			*wl = item->target.wl;
64 	struct window_mode_entry	*wme;
65 	struct key_table		*table;
66 	struct key_binding		*bd;
67 
68 	wme = TAILQ_FIRST(&wp->modes);
69 	if (wme == NULL || wme->mode->key_table == NULL) {
70 		if (options_get_number(wp->window->options, "xterm-keys"))
71 			key |= KEYC_XTERM;
72 		window_pane_key(wp, NULL, s, wl, key, NULL);
73 		return;
74 	}
75 	table = key_bindings_get_table(wme->mode->key_table(wme), 1);
76 
77 	bd = key_bindings_get(table, key & ~KEYC_XTERM);
78 	if (bd != NULL) {
79 		table->references++;
80 		key_bindings_dispatch(bd, item, c, NULL, &item->target);
81 		key_bindings_unref_table(table);
82 	}
83 }
84 
85 static enum cmd_retval
86 cmd_send_keys_exec(struct cmd *self, struct cmdq_item *item)
87 {
88 	struct args			*args = self->args;
89 	struct client			*c = cmd_find_client(item, NULL, 1);
90 	struct window_pane		*wp = item->target.wp;
91 	struct session			*s = item->target.s;
92 	struct winlink			*wl = item->target.wl;
93 	struct mouse_event		*m = &item->shared->mouse;
94 	struct window_mode_entry	*wme = TAILQ_FIRST(&wp->modes);
95 	struct utf8_data		*ud, *uc;
96 	wchar_t				 wc;
97 	int				 i, literal;
98 	key_code			 key;
99 	u_int				 np = 1;
100 	char				*cause = NULL;
101 
102 	if (args_has(args, 'N')) {
103 		np = args_strtonum(args, 'N', 1, UINT_MAX, &cause);
104 		if (cause != NULL) {
105 			cmdq_error(item, "repeat count %s", cause);
106 			free(cause);
107 			return (CMD_RETURN_ERROR);
108 		}
109 		if (wme != NULL && (args_has(args, 'X') || args->argc == 0)) {
110 			if (wme == NULL || wme->mode->command == NULL) {
111 				cmdq_error(item, "not in a mode");
112 				return (CMD_RETURN_ERROR);
113 			}
114 			wme->prefix = np;
115 		}
116 	}
117 
118 	if (args_has(args, 'X')) {
119 		if (wme == NULL || wme->mode->command == NULL) {
120 			cmdq_error(item, "not in a mode");
121 			return (CMD_RETURN_ERROR);
122 		}
123 		if (!m->valid)
124 			m = NULL;
125 		wme->mode->command(wme, c, s, wl, args, m);
126 		return (CMD_RETURN_NORMAL);
127 	}
128 
129 	if (args_has(args, 'M')) {
130 		wp = cmd_mouse_pane(m, &s, NULL);
131 		if (wp == NULL) {
132 			cmdq_error(item, "no mouse target");
133 			return (CMD_RETURN_ERROR);
134 		}
135 		window_pane_key(wp, NULL, s, wl, m->key, m);
136 		return (CMD_RETURN_NORMAL);
137 	}
138 
139 	if (self->entry == &cmd_send_prefix_entry) {
140 		if (args_has(args, '2'))
141 			key = options_get_number(s->options, "prefix2");
142 		else
143 			key = options_get_number(s->options, "prefix");
144 		cmd_send_keys_inject(c, item, key);
145 		return (CMD_RETURN_NORMAL);
146 	}
147 
148 	if (args_has(args, 'R')) {
149 		window_pane_reset_palette(wp);
150 		input_reset(wp, 1);
151 	}
152 
153 	for (; np != 0; np--) {
154 		for (i = 0; i < args->argc; i++) {
155 			literal = args_has(args, 'l');
156 			if (!literal) {
157 				key = key_string_lookup_string(args->argv[i]);
158 				if (key != KEYC_NONE && key != KEYC_UNKNOWN)
159 					cmd_send_keys_inject(c, item, key);
160 				else
161 					literal = 1;
162 			}
163 			if (literal) {
164 				ud = utf8_fromcstr(args->argv[i]);
165 				for (uc = ud; uc->size != 0; uc++) {
166 					if (utf8_combine(uc, &wc) != UTF8_DONE)
167 						continue;
168 					cmd_send_keys_inject(c, item, wc);
169 				}
170 				free(ud);
171 			}
172 		}
173 
174 	}
175 
176 	return (CMD_RETURN_NORMAL);
177 }
178