4 * Copyright (c) 2008 Nicholas Marriott <nicm@users.sourceforge.net>
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.
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.
19 #include <sys/types.h>
26 * Send keys to client.
29 int cmd_send_keys_parse(struct cmd
*, int, char **, char **);
30 int cmd_send_keys_exec(struct cmd
*, struct cmd_ctx
*);
31 void cmd_send_keys_free(struct cmd
*);
32 size_t cmd_send_keys_print(struct cmd
*, char *, size_t);
34 struct cmd_send_keys_data
{
40 const struct cmd_entry cmd_send_keys_entry
= {
42 "[-t target-pane] key ...",
52 cmd_send_keys_parse(struct cmd
*self
, int argc
, char **argv
, char **cause
)
54 struct cmd_send_keys_data
*data
;
58 self
->data
= data
= xmalloc(sizeof *data
);
63 while ((opt
= getopt(argc
, argv
, "t:")) != -1) {
66 if (data
->target
== NULL
)
67 data
->target
= xstrdup(optarg
);
79 if ((key
= key_string_lookup_string(*argv
)) != KEYC_NONE
) {
80 data
->keys
= xrealloc(
81 data
->keys
, data
->nkeys
+ 1, sizeof *data
->keys
);
82 data
->keys
[data
->nkeys
++] = key
;
84 for (s
= *argv
; *s
!= '\0'; s
++) {
85 data
->keys
= xrealloc(data
->keys
,
86 data
->nkeys
+ 1, sizeof *data
->keys
);
87 data
->keys
[data
->nkeys
++] = *s
;
97 xasprintf(cause
, "usage: %s %s", self
->entry
->name
, self
->entry
->usage
);
99 self
->entry
->free(self
);
104 cmd_send_keys_exec(struct cmd
*self
, struct cmd_ctx
*ctx
)
106 struct cmd_send_keys_data
*data
= self
->data
;
107 struct window_pane
*wp
;
114 if (cmd_find_pane(ctx
, data
->target
, &s
, &wp
) == NULL
)
117 for (i
= 0; i
< data
->nkeys
; i
++)
118 window_pane_key(wp
, s
, data
->keys
[i
]);
124 cmd_send_keys_free(struct cmd
*self
)
126 struct cmd_send_keys_data
*data
= self
->data
;
128 if (data
->target
!= NULL
)
134 cmd_send_keys_print(struct cmd
*self
, char *buf
, size_t len
)
136 struct cmd_send_keys_data
*data
= self
->data
;
140 off
+= xsnprintf(buf
, len
, "%s", self
->entry
->name
);
143 if (off
< len
&& data
->target
!= NULL
)
144 off
+= cmd_prarg(buf
+ off
, len
- off
, " -t ", data
->target
);
146 for (i
= 0; i
< data
->nkeys
; i
++) {
149 off
+= xsnprintf(buf
+ off
,
150 len
- off
, " %s", key_string_lookup_key(data
->keys
[i
]));