1 /* $OpenBSD: cmd-bind-key.c,v 1.3 2009/07/24 14:52:47 nicm Exp $ */ 2 3 /* 4 * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net> 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 "tmux.h" 22 23 /* 24 * Bind a key to a command, this recurses through cmd_*. 25 */ 26 27 int cmd_bind_key_parse(struct cmd *, int, char **, char **); 28 int cmd_bind_key_exec(struct cmd *, struct cmd_ctx *); 29 void cmd_bind_key_send(struct cmd *, struct buffer *); 30 void cmd_bind_key_recv(struct cmd *, struct buffer *); 31 void cmd_bind_key_free(struct cmd *); 32 size_t cmd_bind_key_print(struct cmd *, char *, size_t); 33 34 struct cmd_bind_key_data { 35 int key; 36 int can_repeat; 37 struct cmd_list *cmdlist; 38 }; 39 40 const struct cmd_entry cmd_bind_key_entry = { 41 "bind-key", "bind", 42 "[-nr] key command [arguments]", 43 0, 0, 44 NULL, 45 cmd_bind_key_parse, 46 cmd_bind_key_exec, 47 cmd_bind_key_send, 48 cmd_bind_key_recv, 49 cmd_bind_key_free, 50 cmd_bind_key_print 51 }; 52 53 int 54 cmd_bind_key_parse(struct cmd *self, int argc, char **argv, char **cause) 55 { 56 struct cmd_bind_key_data *data; 57 int opt, no_prefix = 0; 58 59 self->data = data = xmalloc(sizeof *data); 60 data->can_repeat = 0; 61 data->cmdlist = NULL; 62 63 while ((opt = getopt(argc, argv, "nr")) != -1) { 64 switch (opt) { 65 case 'n': 66 no_prefix = 1; 67 break; 68 case 'r': 69 data->can_repeat = 1; 70 break; 71 default: 72 goto usage; 73 } 74 } 75 argc -= optind; 76 argv += optind; 77 if (argc < 1) 78 goto usage; 79 80 if ((data->key = key_string_lookup_string(argv[0])) == KEYC_NONE) { 81 xasprintf(cause, "unknown key: %s", argv[0]); 82 goto error; 83 } 84 if (!no_prefix) 85 data->key |= KEYC_PREFIX; 86 87 argc--; 88 argv++; 89 if ((data->cmdlist = cmd_list_parse(argc, argv, cause)) == NULL) 90 goto error; 91 92 return (0); 93 94 usage: 95 xasprintf(cause, "usage: %s %s", self->entry->name, self->entry->usage); 96 97 error: 98 self->entry->free(self); 99 return (-1); 100 } 101 102 int 103 cmd_bind_key_exec(struct cmd *self, unused struct cmd_ctx *ctx) 104 { 105 struct cmd_bind_key_data *data = self->data; 106 107 if (data == NULL) 108 return (0); 109 110 key_bindings_add(data->key, data->can_repeat, data->cmdlist); 111 data->cmdlist = NULL; /* avoid free */ 112 113 return (0); 114 } 115 116 void 117 cmd_bind_key_send(struct cmd *self, struct buffer *b) 118 { 119 struct cmd_bind_key_data *data = self->data; 120 121 buffer_write(b, data, sizeof *data); 122 cmd_list_send(data->cmdlist, b); 123 } 124 125 void 126 cmd_bind_key_recv(struct cmd *self, struct buffer *b) 127 { 128 struct cmd_bind_key_data *data; 129 130 self->data = data = xmalloc(sizeof *data); 131 buffer_read(b, data, sizeof *data); 132 data->cmdlist = cmd_list_recv(b); 133 } 134 135 void 136 cmd_bind_key_free(struct cmd *self) 137 { 138 struct cmd_bind_key_data *data = self->data; 139 140 if (data->cmdlist != NULL) 141 cmd_list_free(data->cmdlist); 142 xfree(data); 143 } 144 145 size_t 146 cmd_bind_key_print(struct cmd *self, char *buf, size_t len) 147 { 148 struct cmd_bind_key_data *data = self->data; 149 size_t off = 0; 150 const char *skey; 151 152 off += xsnprintf(buf, len, "%s", self->entry->name); 153 if (data == NULL) 154 return (off); 155 if (off < len) { 156 skey = key_string_lookup_key(data->key); 157 off += xsnprintf(buf + off, len - off, " %s ", skey); 158 } 159 if (off < len) 160 off += cmd_list_print(data->cmdlist, buf + off, len - off); 161 return (off); 162 } 163