Update man page for update-environment.
[tmux-openbsd.git] / cmd-switch-client.c
blobbce42c2ad066f36019ed99bf7d6e4f0780d58eed
1 /* $OpenBSD$ */
3 /*
4 * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
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.
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.
19 #include <sys/types.h>
21 #include <stdlib.h>
22 #include <string.h>
24 #include "tmux.h"
27 * Switch client to a different session.
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);
36 struct cmd_switch_client_data {
37 char *name;
38 char *target;
39 int flag_next;
40 int flag_previous;
43 const struct cmd_entry cmd_switch_client_entry = {
44 "switch-client", "switchc",
45 "[-np] [-c target-client] [-t target-session]",
46 0, "",
47 cmd_switch_client_init,
48 cmd_switch_client_parse,
49 cmd_switch_client_exec,
50 cmd_switch_client_free,
51 cmd_switch_client_print
54 void
55 cmd_switch_client_init(struct cmd *self, int key)
57 struct cmd_switch_client_data *data;
59 self->data = data = xmalloc(sizeof *data);
60 data->name = NULL;
61 data->target = NULL;
62 data->flag_next = 0;
63 data->flag_previous = 0;
65 switch (key) {
66 case '(':
67 data->flag_previous = 1;
68 break;
69 case ')':
70 data->flag_next = 1;
71 break;
75 int
76 cmd_switch_client_parse(struct cmd *self, int argc, char **argv, char **cause)
78 struct cmd_switch_client_data *data;
79 int opt;
81 self->entry->init(self, KEYC_NONE);
82 data = self->data;
84 while ((opt = getopt(argc, argv, "c:t:")) != -1) {
85 switch (opt) {
86 case 'c':
87 if (data->name == NULL)
88 data->name = xstrdup(optarg);
89 break;
90 case 't':
91 if (data->flag_next || data->flag_previous)
92 goto usage;
93 if (data->target == NULL)
94 data->target = xstrdup(optarg);
95 break;
96 case 'n':
97 if (data->flag_previous || data->target != NULL)
98 goto usage;
99 data->flag_next = 1;
100 break;
101 case 'p':
102 if (data->flag_next || data->target != NULL)
103 goto usage;
104 data->flag_next = 1;
105 break;
106 default:
107 goto usage;
110 argc -= optind;
111 argv += optind;
112 if (argc != 0)
113 goto usage;
115 return (0);
117 usage:
118 xasprintf(cause, "usage: %s %s", self->entry->name, self->entry->usage);
120 self->entry->free(self);
121 return (-1);
125 cmd_switch_client_exec(struct cmd *self, struct cmd_ctx *ctx)
127 struct cmd_switch_client_data *data = self->data;
128 struct client *c;
129 struct session *s;
131 if (data == NULL)
132 return (0);
134 if ((c = cmd_find_client(ctx, data->name)) == NULL)
135 return (-1);
137 if (data->flag_next) {
138 if ((s = session_next_session(c->session)) == NULL) {
139 ctx->error(ctx, "can't find next session");
140 return (-1);
142 } else if (data->flag_previous) {
143 if ((s = session_previous_session(c->session)) == NULL) {
144 ctx->error(ctx, "can't find previous session");
145 return (-1);
147 } else
148 s = cmd_find_session(ctx, data->target);
150 if (s == NULL)
151 return (-1);
152 c->session = s;
154 recalculate_sizes();
155 server_check_unattached();
156 server_redraw_client(c);
158 return (0);
161 void
162 cmd_switch_client_free(struct cmd *self)
164 struct cmd_switch_client_data *data = self->data;
166 if (data->name != NULL)
167 xfree(data->name);
168 if (data->target != NULL)
169 xfree(data->target);
170 xfree(data);
173 size_t
174 cmd_switch_client_print(struct cmd *self, char *buf, size_t len)
176 struct cmd_switch_client_data *data = self->data;
177 size_t off = 0;
179 off += xsnprintf(buf, len, "%s", self->entry->name);
180 if (data == NULL)
181 return (off);
182 if (off < len && data->flag_next)
183 off += xsnprintf(buf + off, len - off, "%s", " -n");
184 if (off < len && data->flag_previous)
185 off += xsnprintf(buf + off, len - off, "%s", " -p");
186 if (off < len && data->name != NULL)
187 off += cmd_prarg(buf + off, len - off, " -c ", data->name);
188 if (off < len && data->target != NULL)
189 off += cmd_prarg(buf + off, len - off, " -t ", data->target);
190 return (off);