Coverage Report

Created: 2026-10-17 06:18

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