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

Annotation of src/usr.bin/tmux/cmd-send-keys.c, Revision 1.49

1.49    ! nicm        1: /* $OpenBSD: cmd-send-keys.c,v 1.48 2019/05/12 08:58:09 nicm Exp $ */
1.1       nicm        2:
                      3: /*
1.28      nicm        4:  * Copyright (c) 2008 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:
                     21: #include <stdlib.h>
1.10      nicm       22: #include <string.h>
1.1       nicm       23:
                     24: #include "tmux.h"
                     25:
                     26: /*
                     27:  * Send keys to client.
                     28:  */
                     29:
1.33      nicm       30: static enum cmd_retval cmd_send_keys_exec(struct cmd *, struct cmdq_item *);
1.1       nicm       31:
                     32: const struct cmd_entry cmd_send_keys_entry = {
1.25      nicm       33:        .name = "send-keys",
                     34:        .alias = "send",
                     35:
1.49    ! nicm       36:        .args = { "HlXRMN:t:", 0, -1 },
        !            37:        .usage = "[-HlXRM] [-N repeat-count] " CMD_TARGET_PANE_USAGE " key ...",
1.25      nicm       38:
1.39      nicm       39:        .target = { 't', CMD_FIND_PANE, 0 },
1.26      nicm       40:
1.31      nicm       41:        .flags = CMD_AFTERHOOK,
1.25      nicm       42:        .exec = cmd_send_keys_exec
1.1       nicm       43: };
                     44:
1.14      nicm       45: const struct cmd_entry cmd_send_prefix_entry = {
1.25      nicm       46:        .name = "send-prefix",
                     47:        .alias = NULL,
                     48:
                     49:        .args = { "2t:", 0, 0 },
                     50:        .usage = "[-2] " CMD_TARGET_PANE_USAGE,
                     51:
1.39      nicm       52:        .target = { 't', CMD_FIND_PANE, 0 },
1.26      nicm       53:
1.31      nicm       54:        .flags = CMD_AFTERHOOK,
1.25      nicm       55:        .exec = cmd_send_keys_exec
1.14      nicm       56: };
                     57:
1.47      nicm       58: static struct cmdq_item *
1.49    ! nicm       59: cmd_send_keys_inject_key(struct client *c, struct cmdq_item *item, key_code key)
1.40      nicm       60: {
1.49    ! nicm       61:        struct cmd_find_state           *fs = &item->target;
1.46      nicm       62:        struct window_mode_entry        *wme;
1.45      nicm       63:        struct key_table                *table;
                     64:        struct key_binding              *bd;
1.40      nicm       65:
1.47      nicm       66:        wme = TAILQ_FIRST(&fs->wp->modes);
1.45      nicm       67:        if (wme == NULL || wme->mode->key_table == NULL) {
1.47      nicm       68:                if (options_get_number(fs->wp->window->options, "xterm-keys"))
1.42      nicm       69:                        key |= KEYC_XTERM;
1.48      nicm       70:                window_pane_key(fs->wp, item->client, fs->s, fs->wl, key, NULL);
1.47      nicm       71:                return (item);
1.40      nicm       72:        }
1.45      nicm       73:        table = key_bindings_get_table(wme->mode->key_table(wme), 1);
1.40      nicm       74:
1.43      nicm       75:        bd = key_bindings_get(table, key & ~KEYC_XTERM);
1.40      nicm       76:        if (bd != NULL) {
                     77:                table->references++;
1.47      nicm       78:                item = key_bindings_dispatch(bd, item, c, NULL, &item->target);
1.40      nicm       79:                key_bindings_unref_table(table);
                     80:        }
1.47      nicm       81:        return (item);
1.40      nicm       82: }
                     83:
1.49    ! nicm       84: static struct cmdq_item *
        !            85: cmd_send_keys_inject_string(struct client *c, struct cmdq_item *item,
        !            86:     struct args *args, int i)
        !            87: {
        !            88:        const char              *s = args->argv[i];
        !            89:        struct utf8_data        *ud, *uc;
        !            90:        wchar_t                  wc;
        !            91:        key_code                 key;
        !            92:        char                    *endptr;
        !            93:        long                     n;
        !            94:        int                      literal;
        !            95:
        !            96:        if (args_has(args, 'H')) {
        !            97:                n = strtol(s, &endptr, 16);
        !            98:                if (*s =='\0' || n < 0 || n > 0xff || *endptr != '\0')
        !            99:                        return (item);
        !           100:                return (cmd_send_keys_inject_key(c, item, KEYC_LITERAL|n));
        !           101:        }
        !           102:
        !           103:        literal = args_has(args, 'l');
        !           104:        if (!literal) {
        !           105:                key = key_string_lookup_string(s);
        !           106:                if (key != KEYC_NONE && key != KEYC_UNKNOWN)
        !           107:                        return (cmd_send_keys_inject_key(c, item, key));
        !           108:                literal = 1;
        !           109:        }
        !           110:        if (literal) {
        !           111:                ud = utf8_fromcstr(s);
        !           112:                for (uc = ud; uc->size != 0; uc++) {
        !           113:                        if (utf8_combine(uc, &wc) != UTF8_DONE)
        !           114:                                continue;
        !           115:                        item = cmd_send_keys_inject_key(c, item, wc);
        !           116:                        }
        !           117:                free(ud);
        !           118:        }
        !           119:        return (item);
        !           120: }
        !           121:
1.29      nicm      122: static enum cmd_retval
1.33      nicm      123: cmd_send_keys_exec(struct cmd *self, struct cmdq_item *item)
1.1       nicm      124: {
1.45      nicm      125:        struct args                     *args = self->args;
                    126:        struct client                   *c = cmd_find_client(item, NULL, 1);
                    127:        struct window_pane              *wp = item->target.wp;
                    128:        struct session                  *s = item->target.s;
                    129:        struct winlink                  *wl = item->target.wl;
                    130:        struct mouse_event              *m = &item->shared->mouse;
1.46      nicm      131:        struct window_mode_entry        *wme = TAILQ_FIRST(&wp->modes);
1.49    ! nicm      132:        int                              i;
1.45      nicm      133:        key_code                         key;
                    134:        u_int                            np = 1;
                    135:        char                            *cause = NULL;
1.30      nicm      136:
                    137:        if (args_has(args, 'N')) {
                    138:                np = args_strtonum(args, 'N', 1, UINT_MAX, &cause);
                    139:                if (cause != NULL) {
1.34      nicm      140:                        cmdq_error(item, "repeat count %s", cause);
1.30      nicm      141:                        free(cause);
                    142:                        return (CMD_RETURN_ERROR);
                    143:                }
1.46      nicm      144:                if (wme != NULL && (args_has(args, 'X') || args->argc == 0)) {
                    145:                        if (wme == NULL || wme->mode->command == NULL) {
                    146:                                cmdq_error(item, "not in a mode");
                    147:                                return (CMD_RETURN_ERROR);
                    148:                        }
1.45      nicm      149:                        wme->prefix = np;
1.46      nicm      150:                }
1.30      nicm      151:        }
                    152:
                    153:        if (args_has(args, 'X')) {
1.45      nicm      154:                if (wme == NULL || wme->mode->command == NULL) {
1.33      nicm      155:                        cmdq_error(item, "not in a mode");
1.30      nicm      156:                        return (CMD_RETURN_ERROR);
                    157:                }
                    158:                if (!m->valid)
1.45      nicm      159:                        m = NULL;
                    160:                wme->mode->command(wme, c, s, wl, args, m);
1.30      nicm      161:                return (CMD_RETURN_NORMAL);
                    162:        }
                    163:
1.19      nicm      164:        if (args_has(args, 'M')) {
                    165:                wp = cmd_mouse_pane(m, &s, NULL);
                    166:                if (wp == NULL) {
1.33      nicm      167:                        cmdq_error(item, "no mouse target");
1.19      nicm      168:                        return (CMD_RETURN_ERROR);
                    169:                }
1.48      nicm      170:                window_pane_key(wp, item->client, s, wl, m->key, m);
1.19      nicm      171:                return (CMD_RETURN_NORMAL);
                    172:        }
1.14      nicm      173:
                    174:        if (self->entry == &cmd_send_prefix_entry) {
                    175:                if (args_has(args, '2'))
1.21      nicm      176:                        key = options_get_number(s->options, "prefix2");
1.14      nicm      177:                else
1.21      nicm      178:                        key = options_get_number(s->options, "prefix");
1.49    ! nicm      179:                cmd_send_keys_inject_key(c, item, key);
1.14      nicm      180:                return (CMD_RETURN_NORMAL);
                    181:        }
1.10      nicm      182:
1.37      nicm      183:        if (args_has(args, 'R')) {
                    184:                window_pane_reset_palette(wp);
1.27      nicm      185:                input_reset(wp, 1);
1.37      nicm      186:        }
1.1       nicm      187:
1.34      nicm      188:        for (; np != 0; np--) {
1.49    ! nicm      189:                for (i = 0; i < args->argc; i++)
        !           190:                        item = cmd_send_keys_inject_string(c, item, args, i);
1.9       nicm      191:        }
1.1       nicm      192:
1.12      nicm      193:        return (CMD_RETURN_NORMAL);
1.1       nicm      194: }