xref: /netbsd-src/external/bsd/tmux/dist/cmd-switch-client.c (revision 1b9578b8c2c1f848eeb16dabbfd7d1f0d9fdefbd)
1 /* $Id: cmd-switch-client.c,v 1.1.1.1 2011/03/10 09:15:37 jmmv Exp $ */
2 
3 /*
4  * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
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 void	cmd_switch_client_init(struct cmd *, int);
31 int	cmd_switch_client_parse(struct cmd *, int, char **, char **);
32 int	cmd_switch_client_exec(struct cmd *, struct cmd_ctx *);
33 void	cmd_switch_client_free(struct cmd *);
34 size_t	cmd_switch_client_print(struct cmd *, char *, size_t);
35 
36 struct cmd_switch_client_data {
37 	char	*name;
38 	char	*target;
39 	int      flag_last;
40 	int	 flag_next;
41 	int	 flag_previous;
42 };
43 
44 const struct cmd_entry cmd_switch_client_entry = {
45 	"switch-client", "switchc",
46 	"[-lnp] [-c target-client] [-t target-session]",
47 	0, "",
48 	cmd_switch_client_init,
49 	cmd_switch_client_parse,
50 	cmd_switch_client_exec,
51 	cmd_switch_client_free,
52 	cmd_switch_client_print
53 };
54 
55 void
56 cmd_switch_client_init(struct cmd *self, int key)
57 {
58 	struct cmd_switch_client_data	*data;
59 
60 	self->data = data = xmalloc(sizeof *data);
61 	data->name = NULL;
62 	data->target = NULL;
63 	data->flag_last = 0;
64 	data->flag_next = 0;
65 	data->flag_previous = 0;
66 
67 	switch (key) {
68 	case '(':
69 		data->flag_previous = 1;
70 		break;
71 	case ')':
72 		data->flag_next = 1;
73 		break;
74 	case 'L':
75 		data->flag_last = 1;
76 		break;
77 	}
78 }
79 
80 int
81 cmd_switch_client_parse(struct cmd *self, int argc, char **argv, char **cause)
82 {
83 	struct cmd_switch_client_data	*data;
84 	int				 opt;
85 
86 	self->entry->init(self, KEYC_NONE);
87 	data = self->data;
88 
89 	while ((opt = getopt(argc, argv, "c:lnpt:")) != -1) {
90 		switch (opt) {
91 		case 'c':
92 			if (data->name == NULL)
93 				data->name = xstrdup(optarg);
94 			break;
95 		case 'l':
96 			if (data->flag_next || data->flag_previous ||
97 			    data->target != NULL)
98 				goto usage;
99 			data->flag_last = 1;
100 			break;
101 		case 'n':
102 			if (data->flag_previous || data->flag_last ||
103 			    data->target != NULL)
104 				goto usage;
105 			data->flag_next = 1;
106 			break;
107 		case 'p':
108 			if (data->flag_next || data->flag_last ||
109 			    data->target != NULL)
110 				goto usage;
111 			data->flag_next = 1;
112 			break;
113 		case 't':
114 			if (data->flag_next || data->flag_previous)
115 				goto usage;
116 			if (data->target == NULL)
117 				data->target = xstrdup(optarg);
118 			break;
119 		default:
120 			goto usage;
121 		}
122 	}
123 	argc -= optind;
124 	argv += optind;
125 	if (argc != 0)
126 		goto usage;
127 
128 	return (0);
129 
130 usage:
131 	xasprintf(cause, "usage: %s %s", self->entry->name, self->entry->usage);
132 
133 	self->entry->free(self);
134 	return (-1);
135 }
136 
137 int
138 cmd_switch_client_exec(struct cmd *self, struct cmd_ctx *ctx)
139 {
140 	struct cmd_switch_client_data	*data = self->data;
141 	struct client			*c;
142 	struct session			*s;
143 
144 	if (data == NULL)
145 		return (0);
146 
147 	if ((c = cmd_find_client(ctx, data->name)) == NULL)
148 		return (-1);
149 
150 	s = NULL;
151 	if (data->flag_next) {
152 		if ((s = session_next_session(c->session)) == NULL) {
153 			ctx->error(ctx, "can't find next session");
154 			return (-1);
155 		}
156 	} else if (data->flag_previous) {
157 		if ((s = session_previous_session(c->session)) == NULL) {
158 			ctx->error(ctx, "can't find previous session");
159 			return (-1);
160 		}
161 	} else if (data->flag_last) {
162 		if (c->last_session != NULL && session_alive(c->last_session))
163 			s = c->last_session;
164 		if (s == NULL) {
165 			ctx->error(ctx, "can't find last session");
166 			return (-1);
167 		}
168 	} else
169 		s = cmd_find_session(ctx, data->target);
170 	if (s == NULL)
171 		return (-1);
172 
173 	if (c->session != NULL)
174 		c->last_session = c->session;
175 	c->session = s;
176 
177 	recalculate_sizes();
178 	server_check_unattached();
179 	server_redraw_client(c);
180 
181 	return (0);
182 }
183 
184 void
185 cmd_switch_client_free(struct cmd *self)
186 {
187 	struct cmd_switch_client_data	*data = self->data;
188 
189 	if (data->name != NULL)
190 		xfree(data->name);
191 	if (data->target != NULL)
192 		xfree(data->target);
193 	xfree(data);
194 }
195 
196 size_t
197 cmd_switch_client_print(struct cmd *self, char *buf, size_t len)
198 {
199 	struct cmd_switch_client_data	*data = self->data;
200 	size_t				 off = 0;
201 
202 	off += xsnprintf(buf, len, "%s", self->entry->name);
203 	if (data == NULL)
204 		return (off);
205 	if (off < len && data->flag_last)
206 		off += xsnprintf(buf + off, len - off, "%s", " -l");
207 	if (off < len && data->flag_next)
208 		off += xsnprintf(buf + off, len - off, "%s", " -n");
209 	if (off < len && data->flag_previous)
210 		off += xsnprintf(buf + off, len - off, "%s", " -p");
211 	if (off < len && data->name != NULL)
212 		off += cmd_prarg(buf + off, len - off, " -c ", data->name);
213 	if (off < len && data->target != NULL)
214 		off += cmd_prarg(buf + off, len - off, " -t ", data->target);
215 	return (off);
216 }
217