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
{
44 const struct cmd_entry cmd_switch_client_entry
= {
45 "switch-client", "switchc",
46 "[-lnp] [-c target-client] [-t target-session]",
48 cmd_switch_client_init
,
49 cmd_switch_client_parse
,
50 cmd_switch_client_exec
,
51 cmd_switch_client_free
,
52 cmd_switch_client_print
56 cmd_switch_client_init(struct cmd
*self
, int key
)
58 struct cmd_switch_client_data
*data
;
60 self
->data
= data
= xmalloc(sizeof *data
);
65 data
->flag_previous
= 0;
69 data
->flag_previous
= 1;
81 cmd_switch_client_parse(struct cmd
*self
, int argc
, char **argv
, char **cause
)
83 struct cmd_switch_client_data
*data
;
86 self
->entry
->init(self
, KEYC_NONE
);
89 while ((opt
= getopt(argc
, argv
, "c:lnpt:")) != -1) {
92 if (data
->name
== NULL
)
93 data
->name
= xstrdup(optarg
);
96 if (data
->flag_next
|| data
->flag_previous
||
102 if (data
->flag_previous
|| data
->flag_last
||
103 data
->target
!= NULL
)
108 if (data
->flag_next
|| data
->flag_last
||
109 data
->target
!= NULL
)
114 if (data
->flag_next
|| data
->flag_previous
)
116 if (data
->target
== NULL
)
117 data
->target
= xstrdup(optarg
);
131 xasprintf(cause
, "usage: %s %s", self
->entry
->name
, self
->entry
->usage
);
133 self
->entry
->free(self
);
138 cmd_switch_client_exec(struct cmd
*self
, struct cmd_ctx
*ctx
)
140 struct cmd_switch_client_data
*data
= self
->data
;
147 if ((c
= cmd_find_client(ctx
, data
->name
)) == NULL
)
151 if (data
->flag_next
) {
152 if ((s
= session_next_session(c
->session
)) == NULL
) {
153 ctx
->error(ctx
, "can't find next session");
156 } else if (data
->flag_previous
) {
157 if ((s
= session_previous_session(c
->session
)) == NULL
) {
158 ctx
->error(ctx
, "can't find previous session");
161 } else if (data
->flag_last
) {
162 if (c
->last_session
!= NULL
&& session_alive(c
->last_session
))
165 ctx
->error(ctx
, "can't find last session");
169 s
= cmd_find_session(ctx
, data
->target
);
173 if (c
->session
!= NULL
)
174 c
->last_session
= c
->session
;
178 server_check_unattached();
179 server_redraw_client(c
);
185 cmd_switch_client_free(struct cmd
*self
)
187 struct cmd_switch_client_data
*data
= self
->data
;
189 if (data
->name
!= NULL
)
191 if (data
->target
!= NULL
)
197 cmd_switch_client_print(struct cmd
*self
, char *buf
, size_t len
)
199 struct cmd_switch_client_data
*data
= self
->data
;
202 off
+= xsnprintf(buf
, len
, "%s", self
->entry
->name
);
205 if (off
< len
&& data
->flag_last
)
206 off
+= xsnprintf(buf
+ off
, len
- off
, "%s", " -l");
207 if (off
< len
&& data
->flag_next
)
208 off
+= xsnprintf(buf
+ off
, len
- off
, "%s", " -n");
209 if (off
< len
&& data
->flag_previous
)
210 off
+= xsnprintf(buf
+ off
, len
- off
, "%s", " -p");
211 if (off
< len
&& data
->name
!= NULL
)
212 off
+= cmd_prarg(buf
+ off
, len
- off
, " -c ", data
->name
);
213 if (off
< len
&& data
->target
!= NULL
)
214 off
+= cmd_prarg(buf
+ off
, len
- off
, " -t ", data
->target
);