xref: /openbsd/usr.bin/tmux/cmd-switch-client.c (revision 404b540a)
1 /* $OpenBSD: cmd-switch-client.c,v 1.3 2009/07/26 12:58:44 nicm 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 int	cmd_switch_client_parse(struct cmd *, int, char **, char **);
31 int	cmd_switch_client_exec(struct cmd *, struct cmd_ctx *);
32 void	cmd_switch_client_free(struct cmd *);
33 size_t	cmd_switch_client_print(struct cmd *, char *, size_t);
34 
35 struct cmd_switch_client_data {
36 	char	*name;
37 	char	*target;
38 };
39 
40 const struct cmd_entry cmd_switch_client_entry = {
41 	"switch-client", "switchc",
42 	"[-c target-client] [-t target-session]",
43 	0, 0,
44 	NULL,
45 	cmd_switch_client_parse,
46 	cmd_switch_client_exec,
47 	cmd_switch_client_free,
48 	cmd_switch_client_print
49 };
50 
51 int
52 cmd_switch_client_parse(struct cmd *self, int argc, char **argv, char **cause)
53 {
54 	struct cmd_switch_client_data	*data;
55 	int				 opt;
56 
57 	self->data = data = xmalloc(sizeof *data);
58 	data->name = NULL;
59 	data->target = NULL;
60 
61 	while ((opt = getopt(argc, argv, "c:t:")) != -1) {
62 		switch (opt) {
63 		case 'c':
64 			data->name = xstrdup(optarg);
65 			break;
66 		case 't':
67 			data->target = xstrdup(optarg);
68 			break;
69 		default:
70 			goto usage;
71 		}
72 	}
73 	argc -= optind;
74 	argv += optind;
75 	if (argc != 0)
76 		goto usage;
77 
78 	return (0);
79 
80 usage:
81 	xasprintf(cause, "usage: %s %s", self->entry->name, self->entry->usage);
82 
83 	self->entry->free(self);
84 	return (-1);
85 }
86 
87 int
88 cmd_switch_client_exec(struct cmd *self, struct cmd_ctx *ctx)
89 {
90 	struct cmd_switch_client_data	*data = self->data;
91 	struct client			*c;
92 	struct session			*s;
93 
94 	if (data == NULL)
95 		return (0);
96 
97 	if ((c = cmd_find_client(ctx, data->name)) == NULL)
98 		return (-1);
99 	if ((s = cmd_find_session(ctx, data->target)) == NULL)
100 		return (-1);
101 
102 	c->session = s;
103 
104 	recalculate_sizes();
105 	server_redraw_client(c);
106 
107 	return (0);
108 }
109 
110 void
111 cmd_switch_client_free(struct cmd *self)
112 {
113 	struct cmd_switch_client_data	*data = self->data;
114 
115 	if (data->name != NULL)
116 		xfree(data->name);
117 	if (data->target != NULL)
118 		xfree(data->target);
119 	xfree(data);
120 }
121 
122 size_t
123 cmd_switch_client_print(struct cmd *self, char *buf, size_t len)
124 {
125 	struct cmd_switch_client_data	*data = self->data;
126 	size_t				 off = 0;
127 
128 	off += xsnprintf(buf, len, "%s", self->entry->name);
129 	if (data == NULL)
130 		return (off);
131 	if (off < len && data->name != NULL)
132 		off += cmd_prarg(buf + off, len - off, " -c ", data->name);
133 	if (off < len && data->target != NULL)
134 		off += cmd_prarg(buf + off, len - off, " -t ", data->target);
135 	return (off);
136 }
137