Coverage Report

Created: 2026-04-27 07:08

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