Annotation of src/usr.bin/tmux/cmd-bind-key.c, Revision 1.46
1.46 ! nicm 1: /* $OpenBSD: cmd-bind-key.c,v 1.45 2021/08/25 09:18:08 nicm Exp $ */
1.1 nicm 2:
3: /*
1.25 nicm 4: * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
1.1 nicm 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:
1.13 nicm 21: #include <stdlib.h>
1.5 nicm 22: #include <string.h>
23:
1.1 nicm 24: #include "tmux.h"
25:
26: /*
1.32 nicm 27: * Bind a key to a command.
1.1 nicm 28: */
29:
1.44 nicm 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 *);
1.1 nicm 34:
35: const struct cmd_entry cmd_bind_key_entry = {
1.24 nicm 36: .name = "bind-key",
37: .alias = "bind",
38:
1.44 nicm 39: .args = { "nrN:T:", 1, -1, cmd_bind_key_args_parse },
1.36 nicm 40: .usage = "[-nr] [-T key-table] [-N note] key "
1.38 nicm 41: "[command [arguments]]",
1.24 nicm 42:
1.30 nicm 43: .flags = CMD_AFTERHOOK,
1.24 nicm 44: .exec = cmd_bind_key_exec
1.1 nicm 45: };
1.44 nicm 46:
47: static enum args_parse_type
1.45 nicm 48: cmd_bind_key_args_parse(__unused struct args *args, __unused u_int idx,
1.44 nicm 49: __unused char **cause)
50: {
1.45 nicm 51: return (ARGS_PARSE_COMMANDS_OR_STRING);
1.44 nicm 52: }
1.1 nicm 53:
1.27 nicm 54: static enum cmd_retval
1.31 nicm 55: cmd_bind_key_exec(struct cmd *self, struct cmdq_item *item)
1.1 nicm 56: {
1.37 nicm 57: struct args *args = cmd_get_args(self);
1.34 nicm 58: key_code key;
1.38 nicm 59: const char *tablename, *note = args_get(args, 'N');
1.34 nicm 60: struct cmd_parse_result *pr;
1.46 ! nicm 61: int repeat;
1.42 nicm 62: struct args_value *value;
1.39 nicm 63: u_int count = args_count(args);
1.17 nicm 64:
1.39 nicm 65: key = key_string_lookup_string(args_string(args, 0));
1.23 nicm 66: if (key == KEYC_NONE || key == KEYC_UNKNOWN) {
1.39 nicm 67: cmdq_error(item, "unknown key: %s", args_string(args, 0));
1.14 nicm 68: return (CMD_RETURN_ERROR);
1.10 nicm 69: }
1.1 nicm 70:
1.21 nicm 71: if (args_has(args, 'T'))
72: tablename = args_get(args, 'T');
73: else if (args_has(args, 'n'))
74: tablename = "root";
75: else
76: tablename = "prefix";
1.35 nicm 77: repeat = args_has(args, 'r');
1.21 nicm 78:
1.42 nicm 79: if (count == 1) {
1.38 nicm 80: key_bindings_add(tablename, key, note, repeat, NULL);
1.42 nicm 81: return (CMD_RETURN_NORMAL);
82: }
83:
84: value = args_value(args, 1);
85: if (count == 2 && value->type == ARGS_COMMANDS) {
86: key_bindings_add(tablename, key, note, repeat, value->cmdlist);
1.43 nicm 87: value->cmdlist->references++;
1.42 nicm 88: return (CMD_RETURN_NORMAL);
89: }
90:
91: if (count == 2)
92: pr = cmd_parse_from_string(args_string(args, 1), NULL);
93: else {
1.46 ! nicm 94: pr = cmd_parse_from_arguments(args_values(args) + 1, count - 1,
! 95: NULL);
1.42 nicm 96: }
97: switch (pr->status) {
98: case CMD_PARSE_ERROR:
99: cmdq_error(item, "%s", pr->error);
100: free(pr->error);
101: return (CMD_RETURN_ERROR);
102: case CMD_PARSE_SUCCESS:
103: break;
104: }
105: key_bindings_add(tablename, key, note, repeat, pr->cmdlist);
1.14 nicm 106: return (CMD_RETURN_NORMAL);
1.1 nicm 107: }