Coverage Report

Created: 2026-04-27 07:08

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/tmux/cmd-send-keys.c
Line
Count
Source
1
/* $OpenBSD$ */
2
3
/*
4
 * Copyright (c) 2008 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
 * Send keys to client.
28
 */
29
30
static enum cmd_retval  cmd_send_keys_exec(struct cmd *, struct cmdq_item *);
31
32
const struct cmd_entry cmd_send_keys_entry = {
33
  .name = "send-keys",
34
  .alias = "send",
35
36
  .args = { "c:FHKlMN:Rt:X", 0, -1, NULL },
37
  .usage = "[-FHKlMRX] [-c target-client] [-N repeat-count] "
38
           CMD_TARGET_PANE_USAGE " [key ...]",
39
40
  .target = { 't', CMD_FIND_PANE, 0 },
41
42
  .flags = CMD_AFTERHOOK|CMD_CLIENT_CFLAG|CMD_CLIENT_CANFAIL|
43
     CMD_READONLY,
44
  .exec = cmd_send_keys_exec
45
};
46
47
const struct cmd_entry cmd_send_prefix_entry = {
48
  .name = "send-prefix",
49
  .alias = NULL,
50
51
  .args = { "2t:", 0, 0, NULL },
52
  .usage = "[-2] " CMD_TARGET_PANE_USAGE,
53
54
  .target = { 't', CMD_FIND_PANE, 0 },
55
56
  .flags = CMD_AFTERHOOK,
57
  .exec = cmd_send_keys_exec
58
};
59
60
static struct cmdq_item *
61
cmd_send_keys_inject_key(struct cmdq_item *item, struct cmdq_item *after,
62
    struct args *args, key_code key)
63
0
{
64
0
  struct cmd_find_state   *target = cmdq_get_target(item);
65
0
  struct client     *tc = cmdq_get_target_client(item);
66
0
  struct session      *s = target->s;
67
0
  struct winlink      *wl = target->wl;
68
0
  struct window_pane    *wp = target->wp;
69
0
  struct window_mode_entry  *wme;
70
0
  struct key_table    *table = NULL;
71
0
  struct key_binding    *bd;
72
0
  struct key_event    *event;
73
74
0
  if (args_has(args, 'K')) {
75
0
    if (tc == NULL)
76
0
      return (item);
77
0
    event = xcalloc(1, sizeof *event);
78
0
    event->key = key|KEYC_SENT;
79
0
    memset(&event->m, 0, sizeof event->m);
80
0
    if (server_client_handle_key(tc, event) == 0) {
81
0
      free(event->buf);
82
0
      free(event);
83
0
    }
84
0
    return (item);
85
0
  }
86
87
0
  wme = TAILQ_FIRST(&wp->modes);
88
0
  if (wme == NULL || wme->mode->key_table == NULL) {
89
0
    if (window_pane_key(wp, tc, s, wl, key, NULL) != 0)
90
0
      return (NULL);
91
0
    return (item);
92
0
  }
93
0
  table = key_bindings_get_table(wme->mode->key_table(wme), 1);
94
95
0
  bd = key_bindings_get(table, key & ~KEYC_MASK_FLAGS);
96
0
  if (bd != NULL) {
97
0
    table->references++;
98
0
    after = key_bindings_dispatch(bd, after, tc, NULL, target);
99
0
    key_bindings_unref_table(table);
100
0
  }
101
0
  return (after);
102
0
}
103
104
static struct cmdq_item *
105
cmd_send_keys_inject_string(struct cmdq_item *item, struct cmdq_item *after,
106
    struct args *args, int i)
107
0
{
108
0
  const char    *s = args_string(args, i);
109
0
  struct utf8_data  *ud, *loop;
110
0
  utf8_char    uc;
111
0
  key_code     key;
112
0
  char      *endptr;
113
0
  long       n;
114
0
  int      literal;
115
116
0
  if (args_has(args, 'H')) {
117
0
    n = strtol(s, &endptr, 16);
118
0
    if (*s =='\0' || n < 0 || n > 0xff || *endptr != '\0')
119
0
      return (item);
120
0
    return (cmd_send_keys_inject_key(item, after, args,
121
0
        KEYC_LITERAL|n));
122
0
  }
123
124
0
  literal = args_has(args, 'l');
125
0
  if (!literal) {
126
0
    key = key_string_lookup_string(s);
127
0
    if (key != KEYC_NONE && key != KEYC_UNKNOWN) {
128
0
      after = cmd_send_keys_inject_key(item, after, args,
129
0
          key);
130
0
      if (after != NULL)
131
0
        return (after);
132
0
    }
133
0
    literal = 1;
134
0
  }
135
0
  if (literal) {
136
0
    ud = utf8_fromcstr(s);
137
0
    for (loop = ud; loop->size != 0; loop++) {
138
0
      if (loop->size == 1 && loop->data[0] <= 0x7f)
139
0
        key = loop->data[0];
140
0
      else {
141
0
        if (utf8_from_data(loop, &uc) != UTF8_DONE)
142
0
          continue;
143
0
        key = uc;
144
0
      }
145
0
      after = cmd_send_keys_inject_key(item, after, args,
146
0
          key);
147
0
    }
148
0
    free(ud);
149
0
  }
150
0
  return (after);
151
0
}
152
153
static enum cmd_retval
154
cmd_send_keys_exec(struct cmd *self, struct cmdq_item *item)
155
0
{
156
0
  struct args     *args = cmd_get_args(self);
157
0
  struct cmd_find_state   *target = cmdq_get_target(item);
158
0
  struct client     *tc = cmdq_get_target_client(item);
159
0
  struct session      *s = target->s;
160
0
  struct winlink      *wl = target->wl;
161
0
  struct window_pane    *wp = target->wp;
162
0
  struct key_event    *event = cmdq_get_event(item);
163
0
  struct mouse_event    *m = &event->m;
164
0
  struct window_mode_entry  *wme = TAILQ_FIRST(&wp->modes);
165
0
  struct cmdq_item    *after = item;
166
0
  key_code       key;
167
0
  u_int        i, np = 1;
168
0
  u_int        count = args_count(args);
169
0
  char        *cause = NULL;
170
171
0
  if (tc != NULL && tc->flags & CLIENT_READONLY && !args_has(args, 'X')) {
172
0
    cmdq_error(item, "client is read-only");
173
0
    return (CMD_RETURN_ERROR);
174
0
  }
175
176
0
  if (args_has(args, 'N')) {
177
0
    np = args_strtonum_and_expand(args, 'N', 1, UINT_MAX, item,
178
0
       &cause);
179
0
    if (cause != NULL) {
180
0
      cmdq_error(item, "repeat count %s", cause);
181
0
      free(cause);
182
0
      return (CMD_RETURN_ERROR);
183
0
    }
184
0
    if (wme != NULL && (args_has(args, 'X') || count == 0)) {
185
0
      if (wme->mode->command == NULL) {
186
0
        cmdq_error(item, "not in a mode");
187
0
        return (CMD_RETURN_ERROR);
188
0
      }
189
0
      wme->prefix = np;
190
0
    }
191
0
  }
192
193
0
  if (args_has(args, 'X')) {
194
0
    if (wme == NULL || wme->mode->command == NULL) {
195
0
      cmdq_error(item, "not in a mode");
196
0
      return (CMD_RETURN_ERROR);
197
0
    }
198
0
    if (!m->valid)
199
0
      m = NULL;
200
0
    wme->mode->command(wme, tc, s, wl, args, m);
201
0
    return (CMD_RETURN_NORMAL);
202
0
  }
203
204
0
  if (args_has(args, 'M')) {
205
0
    wp = cmd_mouse_pane(m, &s, NULL);
206
0
    if (wp == NULL) {
207
0
      cmdq_error(item, "no mouse target");
208
0
      return (CMD_RETURN_ERROR);
209
0
    }
210
0
    window_pane_key(wp, tc, s, wl, m->key, m);
211
0
    return (CMD_RETURN_NORMAL);
212
0
  }
213
214
0
  if (cmd_get_entry(self) == &cmd_send_prefix_entry) {
215
0
    if (args_has(args, '2'))
216
0
      key = options_get_number(s->options, "prefix2");
217
0
    else
218
0
      key = options_get_number(s->options, "prefix");
219
0
    cmd_send_keys_inject_key(item, item, args, key);
220
0
    return (CMD_RETURN_NORMAL);
221
0
  }
222
223
0
  if (args_has(args, 'R')) {
224
0
    colour_palette_clear(&wp->palette);
225
0
    input_reset(wp->ictx, 1);
226
0
    wp->flags |= (PANE_STYLECHANGED|PANE_THEMECHANGED|PANE_REDRAW);
227
0
  }
228
229
0
  if (count == 0) {
230
0
    if (args_has(args, 'N') || args_has(args, 'R'))
231
0
      return (CMD_RETURN_NORMAL);
232
0
    for (; np != 0; np--)
233
0
      cmd_send_keys_inject_key(item, NULL, args, event->key);
234
0
    return (CMD_RETURN_NORMAL);
235
0
  }
236
237
0
  for (; np != 0; np--) {
238
0
    for (i = 0; i < count; i++) {
239
0
      after = cmd_send_keys_inject_string(item, after, args,
240
0
          i);
241
0
    }
242
0
  }
243
244
0
  return (CMD_RETURN_NORMAL);
245
0
}