Coverage Report

Created: 2025-07-12 06:33

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