Coverage Report

Created: 2026-07-16 06:53

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/tmux/cmd-send-keys.c
Line
Count
Source
1
/* $OpenBSD: cmd-send-keys.c,v 1.81 2026/06/11 19:13:34 nicm Exp $ */
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
0
  struct cmdq_item    *new_after = after;
74
75
0
  if (args_has(args, 'K')) {
76
0
    if (tc == NULL)
77
0
      return (item);
78
0
    event = xcalloc(1, sizeof *event);
79
0
    event->key = key|KEYC_SENT;
80
0
    memset(&event->m, 0, sizeof event->m);
81
0
    if (after == NULL) {
82
0
      if (server_client_handle_key(tc, event) != 0)
83
0
        return (item);
84
0
    } else {
85
0
      if (server_client_handle_key_after(tc, event, after,
86
0
          &new_after) != 0)
87
0
        return (new_after);
88
0
    }
89
0
    free(event->buf);
90
0
    free(event);
91
0
    return (item);
92
0
  }
93
94
0
  wme = TAILQ_FIRST(&wp->modes);
95
0
  if (wme == NULL || wme->mode->key_table == NULL) {
96
0
    if (window_pane_key(wp, tc, s, wl, key, NULL) != 0)
97
0
      return (NULL);
98
0
    return (item);
99
0
  }
100
0
  table = key_bindings_get_table(wme->mode->key_table(wme), 1);
101
102
0
  bd = key_bindings_get(table, key & ~KEYC_MASK_FLAGS);
103
0
  if (bd != NULL) {
104
0
    table->references++;
105
0
    after = key_bindings_dispatch(bd, after, tc, NULL, target);
106
0
    key_bindings_unref_table(table);
107
0
  }
108
0
  return (after);
109
0
}
110
111
static struct cmdq_item *
112
cmd_send_keys_inject_string(struct cmdq_item *item, struct cmdq_item *after,
113
    struct args *args, int i)
114
0
{
115
0
  const char    *s = args_string(args, i);
116
0
  struct utf8_data  *ud, *loop;
117
0
  utf8_char    uc;
118
0
  key_code     key;
119
0
  char      *endptr;
120
0
  long       n;
121
0
  int      literal;
122
123
0
  if (args_has(args, 'H')) {
124
0
    n = strtol(s, &endptr, 16);
125
0
    if (*s =='\0' || n < 0 || n > 0xff || *endptr != '\0')
126
0
      return (item);
127
0
    return (cmd_send_keys_inject_key(item, after, args,
128
0
        KEYC_LITERAL|n));
129
0
  }
130
131
0
  literal = args_has(args, 'l');
132
0
  if (!literal) {
133
0
    key = key_string_lookup_string(s);
134
0
    if (key != KEYC_NONE && key != KEYC_UNKNOWN) {
135
0
      after = cmd_send_keys_inject_key(item, after, args,
136
0
          key);
137
0
      if (after != NULL)
138
0
        return (after);
139
0
    }
140
0
    literal = 1;
141
0
  }
142
0
  if (literal) {
143
0
    ud = utf8_fromcstr(s);
144
0
    for (loop = ud; loop->size != 0; loop++) {
145
0
      if (loop->size == 1 && loop->data[0] <= 0x7f)
146
0
        key = loop->data[0];
147
0
      else {
148
0
        if (utf8_from_data(loop, &uc) != UTF8_DONE)
149
0
          continue;
150
0
        key = uc;
151
0
      }
152
0
      after = cmd_send_keys_inject_key(item, after, args,
153
0
          key);
154
0
    }
155
0
    free(ud);
156
0
  }
157
0
  return (after);
158
0
}
159
160
static enum cmd_retval
161
cmd_send_keys_exec(struct cmd *self, struct cmdq_item *item)
162
0
{
163
0
  struct args     *args = cmd_get_args(self);
164
0
  struct cmd_find_state   *target = cmdq_get_target(item);
165
0
  struct client     *tc = cmdq_get_target_client(item);
166
0
  struct session      *s = target->s;
167
0
  struct winlink      *wl = target->wl;
168
0
  struct window_pane    *wp = target->wp;
169
0
  struct key_event    *event = cmdq_get_event(item);
170
0
  struct mouse_event    *m = &event->m;
171
0
  struct window_mode_entry  *wme = TAILQ_FIRST(&wp->modes);
172
0
  struct cmdq_item    *after = item;
173
0
  key_code       key;
174
0
  u_int        i, np = 1;
175
0
  u_int        count = args_count(args);
176
0
  char        *cause = NULL;
177
178
0
  if (tc != NULL && tc->flags & CLIENT_READONLY && !args_has(args, 'X')) {
179
0
    cmdq_error(item, "client is read-only");
180
0
    return (CMD_RETURN_ERROR);
181
0
  }
182
183
0
  if (args_has(args, 'N')) {
184
0
    np = args_strtonum_and_expand(args, 'N', 1, UINT_MAX, item,
185
0
       &cause);
186
0
    if (cause != NULL) {
187
0
      cmdq_error(item, "repeat count %s", cause);
188
0
      free(cause);
189
0
      return (CMD_RETURN_ERROR);
190
0
    }
191
0
    if (wme != NULL && (args_has(args, 'X') || count == 0)) {
192
0
      if (wme->mode->command == NULL) {
193
0
        cmdq_error(item, "not in a mode");
194
0
        return (CMD_RETURN_ERROR);
195
0
      }
196
0
      wme->prefix = np;
197
0
    }
198
0
  }
199
200
0
  if (args_has(args, 'X')) {
201
0
    if (wme == NULL || wme->mode->command == NULL) {
202
0
      cmdq_error(item, "not in a mode");
203
0
      return (CMD_RETURN_ERROR);
204
0
    }
205
0
    if (!m->valid)
206
0
      m = NULL;
207
0
    wme->mode->command(wme, tc, s, wl, args, m);
208
0
    return (CMD_RETURN_NORMAL);
209
0
  }
210
211
0
  if (args_has(args, 'M')) {
212
0
    wp = cmd_mouse_pane(m, &s, NULL);
213
0
    if (wp == NULL) {
214
0
      cmdq_error(item, "no mouse target");
215
0
      return (CMD_RETURN_ERROR);
216
0
    }
217
0
    window_pane_key(wp, tc, s, wl, m->key, m);
218
0
    return (CMD_RETURN_NORMAL);
219
0
  }
220
221
0
  if (cmd_get_entry(self) == &cmd_send_prefix_entry) {
222
0
    if (args_has(args, '2'))
223
0
      key = options_get_number(s->options, "prefix2");
224
0
    else
225
0
      key = options_get_number(s->options, "prefix");
226
0
    cmd_send_keys_inject_key(item, item, args, key);
227
0
    return (CMD_RETURN_NORMAL);
228
0
  }
229
230
0
  if (args_has(args, 'R')) {
231
0
    colour_palette_clear(&wp->palette);
232
0
    input_reset(wp->ictx, 1);
233
0
    wp->flags |= (PANE_STYLECHANGED|PANE_THEMECHANGED|PANE_REDRAW);
234
0
  }
235
236
0
  if (count == 0) {
237
0
    if (args_has(args, 'N') || args_has(args, 'R'))
238
0
      return (CMD_RETURN_NORMAL);
239
0
    after = args_has(args, 'K') ? item : NULL;
240
0
    for (; np != 0; np--)
241
0
      after = cmd_send_keys_inject_key(item, after, args,
242
0
          event->key);
243
0
    return (CMD_RETURN_NORMAL);
244
0
  }
245
246
0
  for (; np != 0; np--) {
247
0
    for (i = 0; i < count; i++) {
248
0
      after = cmd_send_keys_inject_string(item, after, args,
249
0
          i);
250
0
    }
251
0
  }
252
253
0
  return (CMD_RETURN_NORMAL);
254
0
}