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 void cmd_switch_client_init(struct cmd
*, int);
31 int cmd_switch_client_parse(struct cmd
*, int, char **, char **);
32 int cmd_switch_client_exec(struct cmd
*, struct cmd_ctx
*);
33 void cmd_switch_client_free(struct cmd
*);
34 size_t cmd_switch_client_print(struct cmd
*, char *, size_t);
36 struct cmd_switch_client_data
{
43 const struct cmd_entry cmd_switch_client_entry
= {
44 "switch-client", "switchc",
45 "[-np] [-c target-client] [-t target-session]",
47 cmd_switch_client_init
,
48 cmd_switch_client_parse
,
49 cmd_switch_client_exec
,
50 cmd_switch_client_free
,
51 cmd_switch_client_print
55 cmd_switch_client_init(struct cmd
*self
, int key
)
57 struct cmd_switch_client_data
*data
;
59 self
->data
= data
= xmalloc(sizeof *data
);
63 data
->flag_previous
= 0;
67 data
->flag_previous
= 1;
76 cmd_switch_client_parse(struct cmd
*self
, int argc
, char **argv
, char **cause
)
78 struct cmd_switch_client_data
*data
;
81 self
->entry
->init(self
, KEYC_NONE
);
84 while ((opt
= getopt(argc
, argv
, "c:t:")) != -1) {
87 if (data
->name
== NULL
)
88 data
->name
= xstrdup(optarg
);
91 if (data
->flag_next
|| data
->flag_previous
)
93 if (data
->target
== NULL
)
94 data
->target
= xstrdup(optarg
);
97 if (data
->flag_previous
|| data
->target
!= NULL
)
102 if (data
->flag_next
|| data
->target
!= NULL
)
118 xasprintf(cause
, "usage: %s %s", self
->entry
->name
, self
->entry
->usage
);
120 self
->entry
->free(self
);
125 cmd_switch_client_exec(struct cmd
*self
, struct cmd_ctx
*ctx
)
127 struct cmd_switch_client_data
*data
= self
->data
;
134 if ((c
= cmd_find_client(ctx
, data
->name
)) == NULL
)
137 if (data
->flag_next
) {
138 if ((s
= session_next_session(c
->session
)) == NULL
) {
139 ctx
->error(ctx
, "can't find next session");
142 } else if (data
->flag_previous
) {
143 if ((s
= session_previous_session(c
->session
)) == NULL
) {
144 ctx
->error(ctx
, "can't find previous session");
148 s
= cmd_find_session(ctx
, data
->target
);
155 server_check_unattached();
156 server_redraw_client(c
);
162 cmd_switch_client_free(struct cmd
*self
)
164 struct cmd_switch_client_data
*data
= self
->data
;
166 if (data
->name
!= NULL
)
168 if (data
->target
!= NULL
)
174 cmd_switch_client_print(struct cmd
*self
, char *buf
, size_t len
)
176 struct cmd_switch_client_data
*data
= self
->data
;
179 off
+= xsnprintf(buf
, len
, "%s", self
->entry
->name
);
182 if (off
< len
&& data
->flag_next
)
183 off
+= xsnprintf(buf
+ off
, len
- off
, "%s", " -n");
184 if (off
< len
&& data
->flag_previous
)
185 off
+= xsnprintf(buf
+ off
, len
- off
, "%s", " -p");
186 if (off
< len
&& data
->name
!= NULL
)
187 off
+= cmd_prarg(buf
+ off
, len
- off
, " -c ", data
->name
);
188 if (off
< len
&& data
->target
!= NULL
)
189 off
+= cmd_prarg(buf
+ off
, len
- off
, " -t ", data
->target
);