xref: /openbsd-src/usr.bin/tmux/cmd-detach-client.c (revision 0b7734b3d77bb9b21afec6f4621cae6c805dbd45)
1 /* $OpenBSD: cmd-detach-client.c,v 1.27 2016/01/19 15:59:12 nicm Exp $ */
2 
3 /*
4  * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
5  *
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.
9  *
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.
17  */
18 
19 #include <sys/types.h>
20 
21 #include <string.h>
22 
23 #include "tmux.h"
24 
25 /*
26  * Detach a client.
27  */
28 
29 enum cmd_retval	 cmd_detach_client_exec(struct cmd *, struct cmd_q *);
30 
31 const struct cmd_entry cmd_detach_client_entry = {
32 	.name = "detach-client",
33 	.alias = "detach",
34 
35 	.args = { "as:t:P", 0, 0 },
36 	.usage = "[-P] [-a] [-s target-session] " CMD_TARGET_CLIENT_USAGE,
37 
38 	.sflag = CMD_SESSION,
39 	.tflag = CMD_CLIENT,
40 
41 	.flags = CMD_READONLY,
42 	.exec = cmd_detach_client_exec
43 };
44 
45 const struct cmd_entry cmd_suspend_client_entry = {
46 	.name = "suspend-client",
47 	.alias = "suspendc",
48 
49 	.args = { "t:", 0, 0 },
50 	.usage = CMD_TARGET_CLIENT_USAGE,
51 
52 	.tflag = CMD_CLIENT,
53 
54 	.flags = 0,
55 	.exec = cmd_detach_client_exec
56 };
57 
58 enum cmd_retval
59 cmd_detach_client_exec(struct cmd *self, struct cmd_q *cmdq)
60 {
61 	struct args	*args = self->args;
62 	struct client	*c = cmdq->state.c, *cloop;
63 	struct session	*s;
64 	enum msgtype	 msgtype;
65 
66 	if (self->entry == &cmd_suspend_client_entry) {
67 		tty_stop_tty(&c->tty);
68 		c->flags |= CLIENT_SUSPENDED;
69 		proc_send(c->peer, MSG_SUSPEND, -1, NULL, 0);
70 		return (CMD_RETURN_NORMAL);
71 	}
72 
73 	if (args_has(args, 'P'))
74 		msgtype = MSG_DETACHKILL;
75 	else
76 		msgtype = MSG_DETACH;
77 
78 	if (args_has(args, 's')) {
79 		s = cmdq->state.sflag.s;
80 		TAILQ_FOREACH(cloop, &clients, entry) {
81 			if (cloop->session == s)
82 				server_client_detach(cloop, msgtype);
83 		}
84 		return (CMD_RETURN_STOP);
85 	}
86 
87 	if (args_has(args, 'a')) {
88 		TAILQ_FOREACH(cloop, &clients, entry) {
89 			if (cloop->session != NULL && cloop != c)
90 				server_client_detach(cloop, msgtype);
91 		}
92 		return (CMD_RETURN_NORMAL);
93 	}
94 
95 	server_client_detach(c, msgtype);
96 	return (CMD_RETURN_STOP);
97 }
98