xref: /openbsd-src/usr.bin/tmux/cmd-detach-client.c (revision 99fd087599a8791921855f21bd7e36130f39aadc)
1 /* $OpenBSD: cmd-detach-client.c,v 1.32 2017/04/22 10:22:39 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 static enum cmd_retval	cmd_detach_client_exec(struct cmd *,
30 			    struct cmdq_item *);
31 
32 const struct cmd_entry cmd_detach_client_entry = {
33 	.name = "detach-client",
34 	.alias = "detach",
35 
36 	.args = { "aE:s:t:P", 0, 0 },
37 	.usage = "[-aP] [-E shell-command] "
38 	         "[-s target-session] " CMD_TARGET_CLIENT_USAGE,
39 
40 	.source = { 's', CMD_FIND_SESSION, CMD_FIND_CANFAIL },
41 
42 	.flags = CMD_READONLY,
43 	.exec = cmd_detach_client_exec
44 };
45 
46 const struct cmd_entry cmd_suspend_client_entry = {
47 	.name = "suspend-client",
48 	.alias = "suspendc",
49 
50 	.args = { "t:", 0, 0 },
51 	.usage = CMD_TARGET_CLIENT_USAGE,
52 
53 	.flags = 0,
54 	.exec = cmd_detach_client_exec
55 };
56 
57 static enum cmd_retval
58 cmd_detach_client_exec(struct cmd *self, struct cmdq_item *item)
59 {
60 	struct args	*args = self->args;
61 	struct client	*c, *cloop;
62 	struct session	*s;
63 	enum msgtype	 msgtype;
64 	const char	*cmd = args_get(args, 'E');
65 
66 	if ((c = cmd_find_client(item, args_get(args, 't'), 0)) == NULL)
67 		return (CMD_RETURN_ERROR);
68 
69 	if (self->entry == &cmd_suspend_client_entry) {
70 		server_client_suspend(c);
71 		return (CMD_RETURN_NORMAL);
72 	}
73 
74 	if (args_has(args, 'P'))
75 		msgtype = MSG_DETACHKILL;
76 	else
77 		msgtype = MSG_DETACH;
78 
79 	if (args_has(args, 's')) {
80 		s = item->source.s;
81 		if (s == NULL)
82 			return (CMD_RETURN_NORMAL);
83 		TAILQ_FOREACH(cloop, &clients, entry) {
84 			if (cloop->session == s) {
85 				if (cmd != NULL)
86 					server_client_exec(cloop, cmd);
87 				else
88 					server_client_detach(cloop, msgtype);
89 			}
90 		}
91 		return (CMD_RETURN_STOP);
92 	}
93 
94 	if (args_has(args, 'a')) {
95 		TAILQ_FOREACH(cloop, &clients, entry) {
96 			if (cloop->session != NULL && cloop != c) {
97 				if (cmd != NULL)
98 					server_client_exec(cloop, cmd);
99 				else
100 					server_client_detach(cloop, msgtype);
101 			}
102 		}
103 		return (CMD_RETURN_NORMAL);
104 	}
105 
106 	if (cmd != NULL)
107 		server_client_exec(c, cmd);
108 	else
109 		server_client_detach(c, msgtype);
110 	return (CMD_RETURN_STOP);
111 }
112