MSG_EXIT can now have a return code in the message, so check for that
[tmux-openbsd.git] / cmd-switch-client.c
blob03cb7c8dd7f0098792239d05553f780363599a89
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 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);
35 struct cmd_switch_client_data {
36 char *name;
37 char *target;
40 const struct cmd_entry cmd_switch_client_entry = {
41 "switch-client", "switchc",
42 "[-c target-client] [-t target-session]",
43 0, "",
44 NULL,
45 cmd_switch_client_parse,
46 cmd_switch_client_exec,
47 cmd_switch_client_free,
48 cmd_switch_client_print
51 int
52 cmd_switch_client_parse(struct cmd *self, int argc, char **argv, char **cause)
54 struct cmd_switch_client_data *data;
55 int opt;
57 self->data = data = xmalloc(sizeof *data);
58 data->name = NULL;
59 data->target = NULL;
61 while ((opt = getopt(argc, argv, "c:t:")) != -1) {
62 switch (opt) {
63 case 'c':
64 if (data->name == NULL)
65 data->name = xstrdup(optarg);
66 break;
67 case 't':
68 if (data->target == NULL)
69 data->target = xstrdup(optarg);
70 break;
71 default:
72 goto usage;
75 argc -= optind;
76 argv += optind;
77 if (argc != 0)
78 goto usage;
80 return (0);
82 usage:
83 xasprintf(cause, "usage: %s %s", self->entry->name, self->entry->usage);
85 self->entry->free(self);
86 return (-1);
89 int
90 cmd_switch_client_exec(struct cmd *self, struct cmd_ctx *ctx)
92 struct cmd_switch_client_data *data = self->data;
93 struct client *c;
94 struct session *s;
96 if (data == NULL)
97 return (0);
99 if ((c = cmd_find_client(ctx, data->name)) == NULL)
100 return (-1);
101 if ((s = cmd_find_session(ctx, data->target)) == NULL)
102 return (-1);
104 c->session = s;
106 recalculate_sizes();
107 server_redraw_client(c);
109 return (0);
112 void
113 cmd_switch_client_free(struct cmd *self)
115 struct cmd_switch_client_data *data = self->data;
117 if (data->name != NULL)
118 xfree(data->name);
119 if (data->target != NULL)
120 xfree(data->target);
121 xfree(data);
124 size_t
125 cmd_switch_client_print(struct cmd *self, char *buf, size_t len)
127 struct cmd_switch_client_data *data = self->data;
128 size_t off = 0;
130 off += xsnprintf(buf, len, "%s", self->entry->name);
131 if (data == NULL)
132 return (off);
133 if (off < len && data->name != NULL)
134 off += cmd_prarg(buf + off, len - off, " -c ", data->name);
135 if (off < len && data->target != NULL)
136 off += cmd_prarg(buf + off, len - off, " -t ", data->target);
137 return (off);