GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.bin/tmux/cmd-switch-client.c Lines: 0 61 0.0 %
Date: 2017-11-07 Branches: 0 44 0.0 %

Line Branch Exec Source
1
/* $OpenBSD: cmd-switch-client.c,v 1.53 2017/08/30 10:33:57 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 <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 = { "lc:Enpt:rT:", 0, 0 },
38
	.usage = "[-Elnpr] [-c target-client] [-t target-session] "
39
		 "[-T key-table]",
40
41
	/* -t is special */
42
43
	.flags = CMD_READONLY,
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
{
50
	struct args		*args = self->args;
51
	const char		*tflag = args_get(args, 't');
52
	enum cmd_find_type	 type;
53
	int			 flags;
54
	struct client		*c;
55
	struct session		*s;
56
	struct winlink		*wl;
57
	struct window_pane	*wp;
58
	const char		*tablename;
59
	struct key_table	*table;
60
61
	if ((c = cmd_find_client(item, args_get(args, 'c'), 0)) == NULL)
62
		return (CMD_RETURN_ERROR);
63
64
	if (tflag != NULL && tflag[strcspn(tflag, ":.")] != '\0') {
65
		type = CMD_FIND_PANE;
66
		flags = 0;
67
	} else {
68
		type = CMD_FIND_SESSION;
69
		flags = CMD_FIND_PREFER_UNATTACHED;
70
	}
71
	if (cmd_find_target(&item->target, item, tflag, type, flags) != 0)
72
		return (CMD_RETURN_ERROR);
73
	s = item->target.s;
74
	wl = item->target.wl;
75
	wp = item->target.wp;
76
77
	if (args_has(args, 'r'))
78
		c->flags ^= CLIENT_READONLY;
79
80
	tablename = args_get(args, 'T');
81
	if (tablename != NULL) {
82
		table = key_bindings_get_table(tablename, 0);
83
		if (table == NULL) {
84
			cmdq_error(item, "table %s doesn't exist", tablename);
85
			return (CMD_RETURN_ERROR);
86
		}
87
		table->references++;
88
		key_bindings_unref_table(c->keytable);
89
		c->keytable = table;
90
		return (CMD_RETURN_NORMAL);
91
	}
92
93
	if (args_has(args, 'n')) {
94
		if ((s = session_next_session(c->session)) == NULL) {
95
			cmdq_error(item, "can't find next session");
96
			return (CMD_RETURN_ERROR);
97
		}
98
	} else if (args_has(args, 'p')) {
99
		if ((s = session_previous_session(c->session)) == NULL) {
100
			cmdq_error(item, "can't find previous session");
101
			return (CMD_RETURN_ERROR);
102
		}
103
	} else if (args_has(args, 'l')) {
104
		if (c->last_session != NULL && session_alive(c->last_session))
105
			s = c->last_session;
106
		else
107
			s = NULL;
108
		if (s == NULL) {
109
			cmdq_error(item, "can't find last session");
110
			return (CMD_RETURN_ERROR);
111
		}
112
	} else {
113
		if (item->client == NULL)
114
			return (CMD_RETURN_NORMAL);
115
		if (wl != NULL) {
116
			if (wp != NULL)
117
				window_set_active_pane(wp->window, wp);
118
			session_set_current(s, wl);
119
			cmd_find_from_session(&item->shared->current, s, 0);
120
		}
121
	}
122
123
	if (!args_has(args, 'E'))
124
		environ_update(s->options, c->environ, s->environ);
125
126
	if (c->session != NULL && c->session != s)
127
		c->last_session = c->session;
128
	c->session = s;
129
	if (~item->shared->flags & CMDQ_SHARED_REPEAT)
130
		server_client_set_key_table(c, NULL);
131
	status_timer_start(c);
132
	notify_client("client-session-changed", c);
133
	session_update_activity(s, NULL);
134
	gettimeofday(&s->last_attached_time, NULL);
135
136
	recalculate_sizes();
137
	server_check_unattached();
138
	server_redraw_client(c);
139
	s->curw->flags &= ~WINLINK_ALERTFLAGS;
140
	alerts_check_session(s);
141
142
	return (CMD_RETURN_NORMAL);
143
}