1 /* $OpenBSD: cmd-send-keys.c,v 1.35 2016/12/08 22:15:37 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->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 } 81 82 if (args_has(args, 'X')) { 83 if (wp->mode == NULL || wp->mode->command == NULL) { 84 cmdq_error(item, "not in a mode"); 85 return (CMD_RETURN_ERROR); 86 } 87 wp->modeprefix = np; 88 if (!m->valid) 89 wp->mode->command(wp, c, s, args, NULL); 90 else 91 wp->mode->command(wp, c, s, args, m); 92 return (CMD_RETURN_NORMAL); 93 } 94 95 if (args_has(args, 'M')) { 96 wp = cmd_mouse_pane(m, &s, NULL); 97 if (wp == NULL) { 98 cmdq_error(item, "no mouse target"); 99 return (CMD_RETURN_ERROR); 100 } 101 window_pane_key(wp, NULL, s, m->key, m); 102 return (CMD_RETURN_NORMAL); 103 } 104 105 if (self->entry == &cmd_send_prefix_entry) { 106 if (args_has(args, '2')) 107 key = options_get_number(s->options, "prefix2"); 108 else 109 key = options_get_number(s->options, "prefix"); 110 window_pane_key(wp, NULL, s, key, NULL); 111 return (CMD_RETURN_NORMAL); 112 } 113 114 if (args_has(args, 'R')) 115 input_reset(wp, 1); 116 117 for (; np != 0; np--) { 118 for (i = 0; i < args->argc; i++) { 119 literal = args_has(args, 'l'); 120 if (!literal) { 121 key = key_string_lookup_string(args->argv[i]); 122 if (key != KEYC_NONE && key != KEYC_UNKNOWN) 123 window_pane_key(wp, NULL, s, key, NULL); 124 else 125 literal = 1; 126 } 127 if (literal) { 128 ud = utf8_fromcstr(args->argv[i]); 129 for (uc = ud; uc->size != 0; uc++) { 130 if (utf8_combine(uc, &wc) == UTF8_DONE) 131 window_pane_key(wp, NULL, s, wc, NULL); 132 } 133 free(ud); 134 } 135 } 136 137 } 138 139 return (CMD_RETURN_NORMAL); 140 } 141