1 /* $OpenBSD: cmd-send-keys.c,v 1.34 2016/11/29 12:54:46 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 const u_char *keystr; 67 int i, literal; 68 key_code key; 69 u_int np = 1; 70 char *cause = NULL; 71 72 if (args_has(args, 'N')) { 73 np = args_strtonum(args, 'N', 1, UINT_MAX, &cause); 74 if (cause != NULL) { 75 cmdq_error(item, "repeat count %s", cause); 76 free(cause); 77 return (CMD_RETURN_ERROR); 78 } 79 } 80 81 if (args_has(args, 'X')) { 82 if (wp->mode == NULL || wp->mode->command == NULL) { 83 cmdq_error(item, "not in a mode"); 84 return (CMD_RETURN_ERROR); 85 } 86 wp->modeprefix = np; 87 if (!m->valid) 88 wp->mode->command(wp, c, s, args, NULL); 89 else 90 wp->mode->command(wp, c, s, args, m); 91 return (CMD_RETURN_NORMAL); 92 } 93 94 if (args_has(args, 'M')) { 95 wp = cmd_mouse_pane(m, &s, NULL); 96 if (wp == NULL) { 97 cmdq_error(item, "no mouse target"); 98 return (CMD_RETURN_ERROR); 99 } 100 window_pane_key(wp, NULL, s, m->key, m); 101 return (CMD_RETURN_NORMAL); 102 } 103 104 if (self->entry == &cmd_send_prefix_entry) { 105 if (args_has(args, '2')) 106 key = options_get_number(s->options, "prefix2"); 107 else 108 key = options_get_number(s->options, "prefix"); 109 window_pane_key(wp, NULL, s, key, NULL); 110 return (CMD_RETURN_NORMAL); 111 } 112 113 if (args_has(args, 'R')) 114 input_reset(wp, 1); 115 116 for (; np != 0; np--) { 117 for (i = 0; i < args->argc; i++) { 118 literal = args_has(args, 'l'); 119 if (!literal) { 120 key = key_string_lookup_string(args->argv[i]); 121 if (key != KEYC_NONE && key != KEYC_UNKNOWN) 122 window_pane_key(wp, NULL, s, key, NULL); 123 else 124 literal = 1; 125 } 126 if (literal) { 127 for (keystr = args->argv[i]; *keystr != '\0'; keystr++) 128 window_pane_key(wp, NULL, s, *keystr, NULL); 129 } 130 } 131 132 } 133 134 return (CMD_RETURN_NORMAL); 135 } 136