cmd-detach-client.c revision 1.37
1/* $OpenBSD: cmd-detach-client.c,v 1.37 2024/03/21 11:26:28 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 29static enum cmd_retval cmd_detach_client_exec(struct cmd *, 30 struct cmdq_item *); 31 32const struct cmd_entry cmd_detach_client_entry = { 33 .name = "detach-client", 34 .alias = "detach", 35 36 .args = { "aE:s:t:P", 0, 0, NULL }, 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|CMD_CLIENT_TFLAG, 43 .exec = cmd_detach_client_exec 44}; 45 46const struct cmd_entry cmd_suspend_client_entry = { 47 .name = "suspend-client", 48 .alias = "suspendc", 49 50 .args = { "t:", 0, 0, NULL }, 51 .usage = CMD_TARGET_CLIENT_USAGE, 52 53 .flags = CMD_CLIENT_TFLAG, 54 .exec = cmd_detach_client_exec 55}; 56 57static enum cmd_retval 58cmd_detach_client_exec(struct cmd *self, struct cmdq_item *item) 59{ 60 struct args *args = cmd_get_args(self); 61 struct cmd_find_state *source = cmdq_get_source(item); 62 struct client *c = cmdq_get_client(item); 63 struct client *tc = cmdq_get_target_client(item), *loop; 64 struct session *s; 65 enum msgtype msgtype; 66 const char *cmd = args_get(args, 'E'); 67 68 if (cmd_get_entry(self) == &cmd_suspend_client_entry) { 69 server_client_suspend(tc); 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 = source->s; 80 if (s == NULL) 81 return (CMD_RETURN_NORMAL); 82 TAILQ_FOREACH(loop, &clients, entry) { 83 if (loop->session == s) { 84 if (cmd != NULL) 85 server_client_exec(loop, cmd); 86 else 87 server_client_detach(loop, msgtype); 88 } 89 } 90 return (CMD_RETURN_STOP); 91 } 92 93 if (args_has(args, 'a')) { 94 TAILQ_FOREACH(loop, &clients, entry) { 95 if (loop->session != NULL && loop != tc) { 96 if (cmd != NULL) 97 server_client_exec(loop, cmd); 98 else 99 server_client_detach(loop, msgtype); 100 } 101 } 102 return (CMD_RETURN_NORMAL); 103 } 104 105 if (cmd != NULL) { 106 if (c == NULL || c->session == NULL) { 107 cmdq_error(item, "must be attached for -E"); 108 return (CMD_RETURN_ERROR); 109 } 110 server_client_exec(tc, cmd); 111 } else 112 server_client_detach(tc, msgtype); 113 return (CMD_RETURN_STOP); 114} 115