Coverage Report

Created: 2026-10-28 06:08

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/tmux/cmd-detach-client.c
Line
Count
Source
1
/* $OpenBSD: cmd-detach-client.c,v 1.40 2026/10/09 13:12:14 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
  .description = "Detach one or more clients.",
35
  .alias = "detach",
36
37
  .args = { "aE:s:t:P", 0, 0, NULL },
38
  .usage = "[-aP] [-E shell-command] "
39
           "[-s target-session] " CMD_TARGET_CLIENT_USAGE,
40
41
  .source = { 's', CMD_FIND_SESSION, CMD_FIND_CANFAIL },
42
43
  .flags = CMD_READONLY|CMD_CLIENT_TFLAG,
44
  .exec = cmd_detach_client_exec
45
};
46
47
const struct cmd_entry cmd_suspend_client_entry = {
48
  .name = "suspend-client",
49
  .description = "Suspend a client.",
50
  .alias = "suspendc",
51
52
  .args = { "t:", 0, 0, NULL },
53
  .usage = CMD_TARGET_CLIENT_USAGE,
54
55
  .flags = CMD_CLIENT_TFLAG,
56
  .exec = cmd_detach_client_exec
57
};
58
59
static enum cmd_retval
60
cmd_detach_client_exec(struct cmd *self, struct cmdq_item *item)
61
0
{
62
0
  struct args   *args = cmd_get_args(self);
63
0
  struct cmd_find_state *source = cmdq_get_source(item);
64
0
  struct client   *c = cmdq_get_client(item);
65
0
  struct client   *tc = cmdq_get_target_client(item), *loop;
66
0
  struct session    *s;
67
0
  enum msgtype     msgtype;
68
0
  const char    *cmd = args_get(args, 'E');
69
70
0
  if (cmd_get_entry(self) == &cmd_suspend_client_entry) {
71
0
    server_client_suspend(tc);
72
0
    return (CMD_RETURN_NORMAL);
73
0
  }
74
75
0
  if (c->flags & CLIENT_READONLY) {
76
0
    if (args_has(args, 's') || args_has(args, 'a') || c != tc) {
77
0
      cmdq_error(item, "client is read-only");
78
0
      return (CMD_RETURN_ERROR);
79
0
    }
80
0
  }
81
82
0
  if (args_has(args, 'P'))
83
0
    msgtype = MSG_DETACHKILL;
84
0
  else
85
0
    msgtype = MSG_DETACH;
86
87
0
  if (args_has(args, 's')) {
88
0
    s = source->s;
89
0
    if (s == NULL)
90
0
      return (CMD_RETURN_NORMAL);
91
0
    TAILQ_FOREACH(loop, &clients, entry) {
92
0
      if (loop->session == s) {
93
0
        if (cmd != NULL)
94
0
          server_client_exec(loop, cmd);
95
0
        else
96
0
          server_client_detach(loop, msgtype);
97
0
      }
98
0
    }
99
0
    return (CMD_RETURN_STOP);
100
0
  }
101
102
0
  if (args_has(args, 'a')) {
103
0
    TAILQ_FOREACH(loop, &clients, entry) {
104
0
      if (loop->session != NULL && loop != tc) {
105
0
        if (cmd != NULL)
106
0
          server_client_exec(loop, cmd);
107
0
        else
108
0
          server_client_detach(loop, msgtype);
109
0
      }
110
0
    }
111
0
    return (CMD_RETURN_NORMAL);
112
0
  }
113
114
0
  if (cmd != NULL)
115
0
    server_client_exec(tc, cmd);
116
0
  else
117
0
    server_client_detach(tc, msgtype);
118
0
  return (CMD_RETURN_STOP);
119
0
}