1 /* $OpenBSD: cmd-bind-key.c,v 1.44 2021/08/25 08:51:55 nicm Exp $ */ 2 3 /* 4 * Copyright (c) 2007 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 * Bind a key to a command. 28 */ 29 30 static enum args_parse_type cmd_bind_key_args_parse(struct args *, u_int, 31 char **); 32 static enum cmd_retval cmd_bind_key_exec(struct cmd *, 33 struct cmdq_item *); 34 35 const struct cmd_entry cmd_bind_key_entry = { 36 .name = "bind-key", 37 .alias = "bind", 38 39 .args = { "nrN:T:", 1, -1, cmd_bind_key_args_parse }, 40 .usage = "[-nr] [-T key-table] [-N note] key " 41 "[command [arguments]]", 42 43 .flags = CMD_AFTERHOOK, 44 .exec = cmd_bind_key_exec 45 }; 46 47 static enum args_parse_type 48 cmd_bind_key_args_parse(__unused struct args *args, u_int idx, 49 __unused char **cause) 50 { 51 if (idx == 1) 52 return (ARGS_PARSE_COMMANDS_OR_STRING); 53 return (ARGS_PARSE_STRING); 54 } 55 56 static enum cmd_retval 57 cmd_bind_key_exec(struct cmd *self, struct cmdq_item *item) 58 { 59 struct args *args = cmd_get_args(self); 60 key_code key; 61 const char *tablename, *note = args_get(args, 'N'); 62 struct cmd_parse_result *pr; 63 char **argv; 64 int argc, repeat; 65 struct args_value *value; 66 u_int count = args_count(args); 67 68 key = key_string_lookup_string(args_string(args, 0)); 69 if (key == KEYC_NONE || key == KEYC_UNKNOWN) { 70 cmdq_error(item, "unknown key: %s", args_string(args, 0)); 71 return (CMD_RETURN_ERROR); 72 } 73 74 if (args_has(args, 'T')) 75 tablename = args_get(args, 'T'); 76 else if (args_has(args, 'n')) 77 tablename = "root"; 78 else 79 tablename = "prefix"; 80 repeat = args_has(args, 'r'); 81 82 if (count == 1) { 83 key_bindings_add(tablename, key, note, repeat, NULL); 84 return (CMD_RETURN_NORMAL); 85 } 86 87 value = args_value(args, 1); 88 if (count == 2 && value->type == ARGS_COMMANDS) { 89 key_bindings_add(tablename, key, note, repeat, value->cmdlist); 90 value->cmdlist->references++; 91 return (CMD_RETURN_NORMAL); 92 } 93 94 if (count == 2) 95 pr = cmd_parse_from_string(args_string(args, 1), NULL); 96 else { 97 args_vector(args, &argc, &argv); 98 pr = cmd_parse_from_arguments(argc - 1, argv + 1, NULL); 99 cmd_free_argv(argc, argv); 100 } 101 switch (pr->status) { 102 case CMD_PARSE_ERROR: 103 cmdq_error(item, "%s", pr->error); 104 free(pr->error); 105 return (CMD_RETURN_ERROR); 106 case CMD_PARSE_SUCCESS: 107 break; 108 } 109 key_bindings_add(tablename, key, note, repeat, pr->cmdlist); 110 return (CMD_RETURN_NORMAL); 111 } 112