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