Coverage Report

Created: 2026-05-30 06:39

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/tmux/cmd-switch-client.c
Line
Count
Source
1
/* $OpenBSD$ */
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 <stdlib.h>
22
#include <string.h>
23
#include <unistd.h>
24
25
#include "tmux.h"
26
27
/*
28
 * Switch client to a different session.
29
 */
30
31
static enum cmd_retval  cmd_switch_client_exec(struct cmd *,
32
          struct cmdq_item *);
33
34
const struct cmd_entry cmd_switch_client_entry = {
35
  .name = "switch-client",
36
  .alias = "switchc",
37
38
  .args = { "c:EFlnO:pt:rT:Z", 0, 0, NULL },
39
  .usage = "[-ElnprZ] [-c target-client] [-t target-session] "
40
     "[-T key-table] [-O order]",
41
42
  /* -t is special */
43
44
  .flags = CMD_READONLY|CMD_CLIENT_CFLAG,
45
  .exec = cmd_switch_client_exec
46
};
47
48
static enum cmd_retval
49
cmd_switch_client_exec(struct cmd *self, struct cmdq_item *item)
50
0
{
51
0
  struct args   *args = cmd_get_args(self);
52
0
  struct cmd_find_state *current = cmdq_get_current(item);
53
0
  struct cmd_find_state  target;
54
0
  const char    *tflag = args_get(args, 't');
55
0
  enum cmd_find_type   type;
56
0
  int      flags;
57
0
  struct client   *c = cmdq_get_client(item);
58
0
  struct client   *tc = cmdq_get_target_client(item);
59
0
  struct session    *s;
60
0
  struct winlink    *wl;
61
0
  struct window   *w;
62
0
  struct window_pane  *wp;
63
0
  const char    *tablename;
64
0
  struct key_table  *table;
65
0
  struct sort_criteria   sort_crit;
66
0
  uid_t      uid;
67
68
0
  if (tflag != NULL &&
69
0
      (tflag[strcspn(tflag, ":.%")] != '\0' || strcmp(tflag, "=") == 0)) {
70
0
    type = CMD_FIND_PANE;
71
0
    flags = 0;
72
0
  } else {
73
0
    type = CMD_FIND_SESSION;
74
0
    flags = CMD_FIND_PREFER_UNATTACHED;
75
0
  }
76
0
  if (cmd_find_target(&target, item, tflag, type, flags) != 0)
77
0
    return (CMD_RETURN_ERROR);
78
0
  s = target.s;
79
0
  wl = target.wl;
80
0
  wp = target.wp;
81
82
0
  if (args_has(args, 'r')) {
83
0
    if (tc->flags & CLIENT_READONLY) {
84
0
      uid = proc_get_peer_uid(c->peer);
85
0
      if (uid != getuid()) {
86
0
        cmdq_error(item, "client is read-only");
87
0
        return (CMD_RETURN_ERROR);
88
0
      }
89
0
    }
90
0
    if (tc->flags & CLIENT_READONLY)
91
0
      tc->flags &= ~(CLIENT_READONLY|CLIENT_IGNORESIZE);
92
0
    else
93
0
      tc->flags |= (CLIENT_READONLY|CLIENT_IGNORESIZE);
94
0
  }
95
96
0
  tablename = args_get(args, 'T');
97
0
  if (tablename != NULL) {
98
0
    table = key_bindings_get_table(tablename, 0);
99
0
    if (table == NULL) {
100
0
      cmdq_error(item, "table %s doesn't exist", tablename);
101
0
      return (CMD_RETURN_ERROR);
102
0
    }
103
0
    table->references++;
104
0
    key_bindings_unref_table(tc->keytable);
105
0
    tc->keytable = table;
106
0
    return (CMD_RETURN_NORMAL);
107
0
  }
108
109
0
  sort_crit.order = sort_order_from_string(args_get(args, 'O'));
110
0
  if (sort_crit.order == SORT_END && args_has(args, 'O')) {
111
0
    cmdq_error(item, "invalid sort order");
112
0
    return (CMD_RETURN_ERROR);
113
0
  }
114
0
  sort_crit.reversed = args_has(args, 'r');
115
116
0
  if (args_has(args, 'n')) {
117
0
    s = session_next_session(tc->session, &sort_crit);
118
0
    if (s == NULL) {
119
0
      cmdq_error(item, "can't find next session");
120
0
      return (CMD_RETURN_ERROR);
121
0
    }
122
0
  } else if (args_has(args, 'p')) {
123
0
    s = session_previous_session(tc->session, &sort_crit);
124
0
    if (s == NULL) {
125
0
      cmdq_error(item, "can't find previous session");
126
0
      return (CMD_RETURN_ERROR);
127
0
    }
128
0
  } else if (args_has(args, 'l')) {
129
0
    if (tc->last_session != NULL && session_alive(tc->last_session))
130
0
      s = tc->last_session;
131
0
    else
132
0
      s = NULL;
133
0
    if (s == NULL) {
134
0
      cmdq_error(item, "can't find last session");
135
0
      return (CMD_RETURN_ERROR);
136
0
    }
137
0
  } else {
138
0
    if (cmdq_get_client(item) == NULL)
139
0
      return (CMD_RETURN_NORMAL);
140
0
    if (wl != NULL && wp != NULL && wp != wl->window->active) {
141
0
      w = wl->window;
142
0
      if (window_push_zoom(w, 0, args_has(args, 'Z')))
143
0
        server_redraw_window(w);
144
0
      window_redraw_active_switch(w, wp);
145
0
      window_set_active_pane(w, wp, 1);
146
0
      if (window_pop_zoom(w))
147
0
        server_redraw_window(w);
148
0
    }
149
0
    if (wl != NULL) {
150
0
      session_set_current(s, wl);
151
0
      cmd_find_from_session(current, s, 0);
152
0
    }
153
0
  }
154
155
0
  if (!args_has(args, 'E'))
156
0
    environ_update(s->options, tc->environ, s->environ);
157
158
0
  server_client_set_session(tc, s);
159
0
  if (~cmdq_get_flags(item) & CMDQ_STATE_REPEAT)
160
0
    server_client_set_key_table(tc, NULL);
161
162
0
  return (CMD_RETURN_NORMAL);
163
0
}