When removing a pane, don't change the active pane unless the active
[tmux-openbsd.git] / cmd-send-keys.c
blob3c680524fc0e64bacd519f5ff78425780c0ac658
1 /* $OpenBSD$ */
3 /*
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>
21 #include <stdlib.h>
23 #include "tmux.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 {
35 char *target;
36 u_int nkeys;
37 int *keys;
40 const struct cmd_entry cmd_send_keys_entry = {
41 "send-keys", "send",
42 "[-t target-pane] key ...",
43 0, "",
44 NULL,
45 cmd_send_keys_parse,
46 cmd_send_keys_exec,
47 cmd_send_keys_free,
48 cmd_send_keys_print
51 int
52 cmd_send_keys_parse(struct cmd *self, int argc, char **argv, char **cause)
54 struct cmd_send_keys_data *data;
55 int opt, key;
56 char *s;
58 self->data = data = xmalloc(sizeof *data);
59 data->target = NULL;
60 data->nkeys = 0;
61 data->keys = NULL;
63 while ((opt = getopt(argc, argv, "t:")) != -1) {
64 switch (opt) {
65 case 't':
66 if (data->target == NULL)
67 data->target = xstrdup(optarg);
68 break;
69 default:
70 goto usage;
73 argc -= optind;
74 argv += optind;
75 if (argc == 0)
76 goto usage;
78 while (argc-- != 0) {
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;
83 } else {
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;
91 argv++;
94 return (0);
96 usage:
97 xasprintf(cause, "usage: %s %s", self->entry->name, self->entry->usage);
99 self->entry->free(self);
100 return (-1);
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;
108 struct session *s;
109 u_int i;
111 if (data == NULL)
112 return (-1);
114 if (cmd_find_pane(ctx, data->target, &s, &wp) == NULL)
115 return (-1);
117 for (i = 0; i < data->nkeys; i++)
118 window_pane_key(wp, s, data->keys[i]);
120 return (0);
123 void
124 cmd_send_keys_free(struct cmd *self)
126 struct cmd_send_keys_data *data = self->data;
128 if (data->target != NULL)
129 xfree(data->target);
130 xfree(data);
133 size_t
134 cmd_send_keys_print(struct cmd *self, char *buf, size_t len)
136 struct cmd_send_keys_data *data = self->data;
137 size_t off = 0;
138 u_int i;
140 off += xsnprintf(buf, len, "%s", self->entry->name);
141 if (data == NULL)
142 return (off);
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++) {
147 if (off >= len)
148 break;
149 off += xsnprintf(buf + off,
150 len - off, " %s", key_string_lookup_key(data->keys[i]));
152 return (off);