[BACK]Return to cmd-switch-client.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / tmux

Annotation of src/usr.bin/tmux/cmd-switch-client.c, Revision 1.5

1.5     ! nicm        1: /* $OpenBSD: cmd-switch-client.c,v 1.4 2009/11/13 19:53:29 nicm Exp $ */
1.1       nicm        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]",
1.4       nicm       43:        0, "",
1.1       nicm       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':
1.5     ! nicm       64:                        if (data->name == NULL)
        !            65:                                data->name = xstrdup(optarg);
1.1       nicm       66:                        break;
                     67:                case 't':
1.5     ! nicm       68:                        if (data->target == NULL)
        !            69:                                data->target = xstrdup(optarg);
1.1       nicm       70:                        break;
                     71:                default:
                     72:                        goto usage;
                     73:                }
                     74:        }
                     75:        argc -= optind;
                     76:        argv += optind;
                     77:        if (argc != 0)
                     78:                goto usage;
                     79:
                     80:        return (0);
                     81:
                     82: usage:
                     83:        xasprintf(cause, "usage: %s %s", self->entry->name, self->entry->usage);
                     84:
                     85:        self->entry->free(self);
                     86:        return (-1);
                     87: }
                     88:
                     89: int
                     90: cmd_switch_client_exec(struct cmd *self, struct cmd_ctx *ctx)
                     91: {
                     92:        struct cmd_switch_client_data   *data = self->data;
                     93:        struct client                   *c;
                     94:        struct session                  *s;
                     95:
                     96:        if (data == NULL)
                     97:                return (0);
                     98:
                     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);
                    103:
                    104:        c->session = s;
                    105:
                    106:        recalculate_sizes();
                    107:        server_redraw_client(c);
                    108:
                    109:        return (0);
                    110: }
                    111:
                    112: void
                    113: cmd_switch_client_free(struct cmd *self)
                    114: {
                    115:        struct cmd_switch_client_data   *data = self->data;
                    116:
                    117:        if (data->name != NULL)
                    118:                xfree(data->name);
                    119:        if (data->target != NULL)
                    120:                xfree(data->target);
                    121:        xfree(data);
                    122: }
                    123:
                    124: size_t
                    125: cmd_switch_client_print(struct cmd *self, char *buf, size_t len)
                    126: {
                    127:        struct cmd_switch_client_data   *data = self->data;
                    128:        size_t                           off = 0;
                    129:
                    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);
                    138: }