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