Annotation of src/usr.bin/tmux/cmd-switch-client.c, Revision 1.1
1.1 ! nicm 1: /* $OpenBSD$ */
! 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_send(struct cmd *, struct buffer *);
! 33: void cmd_switch_client_recv(struct cmd *, struct buffer *);
! 34: void cmd_switch_client_free(struct cmd *);
! 35: size_t cmd_switch_client_print(struct cmd *, char *, size_t);
! 36:
! 37: struct cmd_switch_client_data {
! 38: char *name;
! 39: char *target;
! 40: };
! 41:
! 42: const struct cmd_entry cmd_switch_client_entry = {
! 43: "switch-client", "switchc",
! 44: "[-c target-client] [-t target-session]",
! 45: 0,
! 46: NULL,
! 47: cmd_switch_client_parse,
! 48: cmd_switch_client_exec,
! 49: cmd_switch_client_send,
! 50: cmd_switch_client_recv,
! 51: cmd_switch_client_free,
! 52: cmd_switch_client_print
! 53: };
! 54:
! 55: int
! 56: cmd_switch_client_parse(struct cmd *self, int argc, char **argv, char **cause)
! 57: {
! 58: struct cmd_switch_client_data *data;
! 59: int opt;
! 60:
! 61: self->data = data = xmalloc(sizeof *data);
! 62: data->name = NULL;
! 63: data->target = NULL;
! 64:
! 65: while ((opt = getopt(argc, argv, "c:t:")) != -1) {
! 66: switch (opt) {
! 67: case 'c':
! 68: data->name = xstrdup(optarg);
! 69: break;
! 70: case 't':
! 71: data->target = xstrdup(optarg);
! 72: break;
! 73: default:
! 74: goto usage;
! 75: }
! 76: }
! 77: argc -= optind;
! 78: argv += optind;
! 79: if (argc != 0)
! 80: goto usage;
! 81:
! 82: return (0);
! 83:
! 84: usage:
! 85: xasprintf(cause, "usage: %s %s", self->entry->name, self->entry->usage);
! 86:
! 87: self->entry->free(self);
! 88: return (-1);
! 89: }
! 90:
! 91: int
! 92: cmd_switch_client_exec(struct cmd *self, struct cmd_ctx *ctx)
! 93: {
! 94: struct cmd_switch_client_data *data = self->data;
! 95: struct client *c;
! 96: struct session *s;
! 97:
! 98: if (data == NULL)
! 99: return (0);
! 100:
! 101: if ((c = cmd_find_client(ctx, data->name)) == NULL)
! 102: return (-1);
! 103: if ((s = cmd_find_session(ctx, data->target)) == NULL)
! 104: return (-1);
! 105:
! 106: c->session = s;
! 107:
! 108: recalculate_sizes();
! 109: server_redraw_client(c);
! 110:
! 111: return (0);
! 112: }
! 113:
! 114: void
! 115: cmd_switch_client_send(struct cmd *self, struct buffer *b)
! 116: {
! 117: struct cmd_switch_client_data *data = self->data;
! 118:
! 119: buffer_write(b, data, sizeof *data);
! 120: cmd_send_string(b, data->name);
! 121: cmd_send_string(b, data->target);
! 122: }
! 123:
! 124: void
! 125: cmd_switch_client_recv(struct cmd *self, struct buffer *b)
! 126: {
! 127: struct cmd_switch_client_data *data;
! 128:
! 129: self->data = data = xmalloc(sizeof *data);
! 130: buffer_read(b, data, sizeof *data);
! 131: data->name = cmd_recv_string(b);
! 132: data->target = cmd_recv_string(b);
! 133: }
! 134:
! 135: void
! 136: cmd_switch_client_free(struct cmd *self)
! 137: {
! 138: struct cmd_switch_client_data *data = self->data;
! 139:
! 140: if (data->name != NULL)
! 141: xfree(data->name);
! 142: if (data->target != NULL)
! 143: xfree(data->target);
! 144: xfree(data);
! 145: }
! 146:
! 147: size_t
! 148: cmd_switch_client_print(struct cmd *self, char *buf, size_t len)
! 149: {
! 150: struct cmd_switch_client_data *data = self->data;
! 151: size_t off = 0;
! 152:
! 153: off += xsnprintf(buf, len, "%s", self->entry->name);
! 154: if (data == NULL)
! 155: return (off);
! 156: if (off < len && data->name != NULL)
! 157: off += cmd_prarg(buf + off, len - off, " -c ", data->name);
! 158: if (off < len && data->target != NULL)
! 159: off += cmd_prarg(buf + off, len - off, " -t ", data->target);
! 160: return (off);
! 161: }