[BACK]Return to cmd-bind-key.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / tmux

Annotation of src/usr.bin/tmux/cmd-bind-key.c, Revision 1.1

1.1     ! nicm        1: /* $OpenBSD$ */
        !             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:        "[-r] key command [arguments]",
        !            43:        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;
        !            58:
        !            59:        self->data = data = xmalloc(sizeof *data);
        !            60:        data->can_repeat = 0;
        !            61:        data->cmdlist = NULL;
        !            62:
        !            63:        while ((opt = getopt(argc, argv, "r")) != -1) {
        !            64:                switch (opt) {
        !            65:                case 'r':
        !            66:                        data->can_repeat = 1;
        !            67:                        break;
        !            68:                default:
        !            69:                        goto usage;
        !            70:                }
        !            71:        }
        !            72:        argc -= optind;
        !            73:        argv += optind;
        !            74:        if (argc < 1)
        !            75:                goto usage;
        !            76:
        !            77:        if ((data->key = key_string_lookup_string(argv[0])) == KEYC_NONE) {
        !            78:                xasprintf(cause, "unknown key: %s", argv[0]);
        !            79:                goto error;
        !            80:        }
        !            81:
        !            82:        argc--;
        !            83:        argv++;
        !            84:        if ((data->cmdlist = cmd_list_parse(argc, argv, cause)) == NULL)
        !            85:                goto error;
        !            86:
        !            87:        return (0);
        !            88:
        !            89: usage:
        !            90:        xasprintf(cause, "usage: %s %s", self->entry->name, self->entry->usage);
        !            91:
        !            92: error:
        !            93:        self->entry->free(self);
        !            94:        return (-1);
        !            95: }
        !            96:
        !            97: int
        !            98: cmd_bind_key_exec(struct cmd *self, unused struct cmd_ctx *ctx)
        !            99: {
        !           100:        struct cmd_bind_key_data        *data = self->data;
        !           101:
        !           102:        if (data == NULL)
        !           103:                return (0);
        !           104:
        !           105:        key_bindings_add(data->key, data->can_repeat, data->cmdlist);
        !           106:        data->cmdlist = NULL;   /* avoid free */
        !           107:
        !           108:        return (0);
        !           109: }
        !           110:
        !           111: void
        !           112: cmd_bind_key_send(struct cmd *self, struct buffer *b)
        !           113: {
        !           114:        struct cmd_bind_key_data        *data = self->data;
        !           115:
        !           116:        buffer_write(b, data, sizeof *data);
        !           117:        cmd_list_send(data->cmdlist, b);
        !           118: }
        !           119:
        !           120: void
        !           121: cmd_bind_key_recv(struct cmd *self, struct buffer *b)
        !           122: {
        !           123:        struct cmd_bind_key_data        *data;
        !           124:
        !           125:        self->data = data = xmalloc(sizeof *data);
        !           126:        buffer_read(b, data, sizeof *data);
        !           127:        data->cmdlist = cmd_list_recv(b);
        !           128: }
        !           129:
        !           130: void
        !           131: cmd_bind_key_free(struct cmd *self)
        !           132: {
        !           133:        struct cmd_bind_key_data        *data = self->data;
        !           134:
        !           135:        if (data->cmdlist != NULL)
        !           136:                cmd_list_free(data->cmdlist);
        !           137:        xfree(data);
        !           138: }
        !           139:
        !           140: size_t
        !           141: cmd_bind_key_print(struct cmd *self, char *buf, size_t len)
        !           142: {
        !           143:        struct cmd_bind_key_data        *data = self->data;
        !           144:        size_t                           off = 0;
        !           145:        const char                      *skey;
        !           146:
        !           147:        off += xsnprintf(buf, len, "%s", self->entry->name);
        !           148:        if (data == NULL)
        !           149:                return (off);
        !           150:        if (off < len) {
        !           151:                skey = key_string_lookup_key(data->key);
        !           152:                off += xsnprintf(buf + off, len - off, " %s ", skey);
        !           153:        }
        !           154:        if (off < len)
        !           155:                off += cmd_list_print(data->cmdlist, buf + off, len - off);
        !           156:        return (off);
        !           157: }