4 * Copyright (c) 2007 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>
27 * Switch client to a different session.
30 int cmd_switch_client_parse(struct cmd
*, int, char **, char **);
31 int cmd_switch_client_exec(struct cmd
*, struct cmd_ctx
*);
32 void cmd_switch_client_free(struct cmd
*);
33 size_t cmd_switch_client_print(struct cmd
*, char *, size_t);
35 struct cmd_switch_client_data
{
40 const struct cmd_entry cmd_switch_client_entry
= {
41 "switch-client", "switchc",
42 "[-c target-client] [-t target-session]",
45 cmd_switch_client_parse
,
46 cmd_switch_client_exec
,
47 cmd_switch_client_free
,
48 cmd_switch_client_print
52 cmd_switch_client_parse(struct cmd
*self
, int argc
, char **argv
, char **cause
)
54 struct cmd_switch_client_data
*data
;
57 self
->data
= data
= xmalloc(sizeof *data
);
61 while ((opt
= getopt(argc
, argv
, "c:t:")) != -1) {
64 if (data
->name
== NULL
)
65 data
->name
= xstrdup(optarg
);
68 if (data
->target
== NULL
)
69 data
->target
= xstrdup(optarg
);
83 xasprintf(cause
, "usage: %s %s", self
->entry
->name
, self
->entry
->usage
);
85 self
->entry
->free(self
);
90 cmd_switch_client_exec(struct cmd
*self
, struct cmd_ctx
*ctx
)
92 struct cmd_switch_client_data
*data
= self
->data
;
99 if ((c
= cmd_find_client(ctx
, data
->name
)) == NULL
)
101 if ((s
= cmd_find_session(ctx
, data
->target
)) == NULL
)
107 server_redraw_client(c
);
113 cmd_switch_client_free(struct cmd
*self
)
115 struct cmd_switch_client_data
*data
= self
->data
;
117 if (data
->name
!= NULL
)
119 if (data
->target
!= NULL
)
125 cmd_switch_client_print(struct cmd
*self
, char *buf
, size_t len
)
127 struct cmd_switch_client_data
*data
= self
->data
;
130 off
+= xsnprintf(buf
, len
, "%s", self
->entry
->name
);
133 if (off
< len
&& data
->name
!= NULL
)
134 off
+= cmd_prarg(buf
+ off
, len
- off
, " -c ", data
->name
);
135 if (off
< len
&& data
->target
!= NULL
)
136 off
+= cmd_prarg(buf
+ off
, len
- off
, " -t ", data
->target
);