xref: /openbsd-src/usr.bin/tmux/cmd-send-keys.c (revision bebc73f12fb20d0fe5d2665112e006eb30001352)
1 /* $OpenBSD: cmd-send-keys.c,v 1.38 2017/04/21 14:01:19 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 	.tflag = CMD_PANE,
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 	.tflag = CMD_PANE,
53 
54 	.flags = CMD_AFTERHOOK,
55 	.exec = cmd_send_keys_exec
56 };
57 
58 static enum cmd_retval
59 cmd_send_keys_exec(struct cmd *self, struct cmdq_item *item)
60 {
61 	struct args		*args = self->args;
62 	struct client		*c = item->state.c;
63 	struct window_pane	*wp = item->state.tflag.wp;
64 	struct session		*s = item->state.tflag.s;
65 	struct mouse_event	*m = &item->shared->mouse;
66 	struct utf8_data	*ud, *uc;
67 	wchar_t			 wc;
68 	int			 i, literal;
69 	key_code		 key;
70 	u_int			 np = 1;
71 	char			*cause = NULL;
72 
73 	if (args_has(args, 'N')) {
74 		np = args_strtonum(args, 'N', 1, UINT_MAX, &cause);
75 		if (cause != NULL) {
76 			cmdq_error(item, "repeat count %s", cause);
77 			free(cause);
78 			return (CMD_RETURN_ERROR);
79 		}
80 		if (args_has(args, 'X') || args->argc == 0)
81 			wp->modeprefix = np;
82 	}
83 
84 	if (args_has(args, 'X')) {
85 		if (wp->mode == NULL || wp->mode->command == NULL) {
86 			cmdq_error(item, "not in a mode");
87 			return (CMD_RETURN_ERROR);
88 		}
89 		if (!m->valid)
90 			wp->mode->command(wp, c, s, args, NULL);
91 		else
92 			wp->mode->command(wp, c, s, args, m);
93 		return (CMD_RETURN_NORMAL);
94 	}
95 
96 	if (args_has(args, 'M')) {
97 		wp = cmd_mouse_pane(m, &s, NULL);
98 		if (wp == NULL) {
99 			cmdq_error(item, "no mouse target");
100 			return (CMD_RETURN_ERROR);
101 		}
102 		window_pane_key(wp, NULL, s, m->key, m);
103 		return (CMD_RETURN_NORMAL);
104 	}
105 
106 	if (self->entry == &cmd_send_prefix_entry) {
107 		if (args_has(args, '2'))
108 			key = options_get_number(s->options, "prefix2");
109 		else
110 			key = options_get_number(s->options, "prefix");
111 		window_pane_key(wp, NULL, s, key, NULL);
112 		return (CMD_RETURN_NORMAL);
113 	}
114 
115 	if (args_has(args, 'R')) {
116 		window_pane_reset_palette(wp);
117 		input_reset(wp, 1);
118 	}
119 
120 	for (; np != 0; np--) {
121 		for (i = 0; i < args->argc; i++) {
122 			literal = args_has(args, 'l');
123 			if (!literal) {
124 				key = key_string_lookup_string(args->argv[i]);
125 				if (key != KEYC_NONE && key != KEYC_UNKNOWN)
126 					window_pane_key(wp, NULL, s, key, NULL);
127 				else
128 					literal = 1;
129 			}
130 			if (literal) {
131 				ud = utf8_fromcstr(args->argv[i]);
132 				for (uc = ud; uc->size != 0; uc++) {
133 					if (utf8_combine(uc, &wc) != UTF8_DONE)
134 						continue;
135 					window_pane_key(wp, NULL, s, wc, NULL);
136 				}
137 				free(ud);
138 			}
139 		}
140 
141 	}
142 
143 	return (CMD_RETURN_NORMAL);
144 }
145