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

Annotation of src/usr.bin/tmux/cmd-split-window.c, Revision 1.10

1.10    ! nicm        1: /* $OpenBSD: cmd-split-window.c,v 1.9 2009/08/13 19:04:00 nicm Exp $ */
1.1       nicm        2:
                      3: /*
                      4:  * Copyright (c) 2009 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 <unistd.h>
                     23:
                     24: #include "tmux.h"
                     25:
                     26: /*
                     27:  * Split a window (add a new pane).
                     28:  */
                     29:
                     30: int    cmd_split_window_parse(struct cmd *, int, char **, char **);
                     31: int    cmd_split_window_exec(struct cmd *, struct cmd_ctx *);
                     32: void   cmd_split_window_free(struct cmd *);
                     33: void   cmd_split_window_init(struct cmd *, int);
                     34: size_t cmd_split_window_print(struct cmd *, char *, size_t);
                     35:
                     36: struct cmd_split_window_data {
                     37:        char    *target;
                     38:        char    *cmd;
                     39:        int      flag_detached;
1.5       nicm       40:        int      flag_horizontal;
1.1       nicm       41:        int      percentage;
1.5       nicm       42:        int      size;
1.1       nicm       43: };
                     44:
                     45: const struct cmd_entry cmd_split_window_entry = {
                     46:        "split-window", "splitw",
1.5       nicm       47:        "[-dhv] [-p percentage|-l size] [-t target-window] [command]",
1.4       nicm       48:        0, 0,
1.1       nicm       49:        cmd_split_window_init,
                     50:        cmd_split_window_parse,
                     51:        cmd_split_window_exec,
                     52:        cmd_split_window_free,
                     53:        cmd_split_window_print
                     54: };
                     55:
                     56: void
1.5       nicm       57: cmd_split_window_init(struct cmd *self, int key)
1.1       nicm       58: {
                     59:        struct cmd_split_window_data     *data;
                     60:
                     61:        self->data = data = xmalloc(sizeof *data);
                     62:        data->target = NULL;
                     63:        data->cmd = NULL;
                     64:        data->flag_detached = 0;
1.5       nicm       65:        data->flag_horizontal = 0;
1.1       nicm       66:        data->percentage = -1;
1.5       nicm       67:        data->size = -1;
                     68:
                     69:        switch (key) {
                     70:        case '%':
                     71:                data->flag_horizontal = 1;
                     72:                break;
                     73:        case '"':
                     74:                data->flag_horizontal = 0;
                     75:                break;
                     76:        }
1.1       nicm       77: }
                     78:
                     79: int
                     80: cmd_split_window_parse(struct cmd *self, int argc, char **argv, char **cause)
                     81: {
                     82:        struct cmd_split_window_data    *data;
1.5       nicm       83:        int                              opt;
1.1       nicm       84:        const char                      *errstr;
                     85:
                     86:        self->entry->init(self, 0);
                     87:        data = self->data;
                     88:
1.5       nicm       89:        while ((opt = getopt(argc, argv, "dhl:p:t:v")) != -1) {
1.1       nicm       90:                switch (opt) {
                     91:                case 'd':
                     92:                        data->flag_detached = 1;
                     93:                        break;
1.5       nicm       94:                case 'h':
                     95:                        data->flag_horizontal = 1;
                     96:                        break;
1.1       nicm       97:                case 't':
                     98:                        if (data->target == NULL)
                     99:                                data->target = xstrdup(optarg);
                    100:                        break;
                    101:                case 'l':
1.5       nicm      102:                        if (data->percentage != -1 || data->size != -1)
                    103:                                break;
                    104:                        data->size = strtonum(optarg, 1, INT_MAX, &errstr);
                    105:                        if (errstr != NULL) {
                    106:                                xasprintf(cause, "size %s", errstr);
                    107:                                goto error;
1.1       nicm      108:                        }
                    109:                        break;
                    110:                case 'p':
1.5       nicm      111:                        if (data->size != -1 || data->percentage != -1)
                    112:                                break;
                    113:                        data->percentage = strtonum(optarg, 1, 100, &errstr);
                    114:                        if (errstr != NULL) {
                    115:                                xasprintf(cause, "percentage %s", errstr);
                    116:                                goto error;
1.1       nicm      117:                        }
                    118:                        break;
1.5       nicm      119:                case 'v':
                    120:                        data->flag_horizontal = 0;
                    121:                        break;
1.1       nicm      122:                default:
                    123:                        goto usage;
                    124:                }
                    125:        }
                    126:        argc -= optind;
                    127:        argv += optind;
                    128:        if (argc != 0 && argc != 1)
                    129:                goto usage;
                    130:
                    131:        if (argc == 1)
                    132:                data->cmd = xstrdup(argv[0]);
                    133:
                    134:        return (0);
                    135:
                    136: usage:
                    137:        xasprintf(cause, "usage: %s %s", self->entry->name, self->entry->usage);
                    138:
                    139: error:
                    140:        self->entry->free(self);
                    141:        return (-1);
                    142: }
                    143:
                    144: int
                    145: cmd_split_window_exec(struct cmd *self, struct cmd_ctx *ctx)
                    146: {
                    147:        struct cmd_split_window_data    *data = self->data;
                    148:        struct session                  *s;
                    149:        struct winlink                  *wl;
                    150:        struct window                   *w;
                    151:        struct window_pane              *wp;
1.8       nicm      152:        struct environ                   env;
1.1       nicm      153:        char                            *cmd, *cwd, *cause;
1.2       nicm      154:        u_int                            hlimit;
1.5       nicm      155:        int                              size;
                    156:        enum layout_type                 type;
1.1       nicm      157:
                    158:        if ((wl = cmd_find_window(ctx, data->target, &s)) == NULL)
                    159:                return (-1);
                    160:        w = wl->window;
                    161:
1.8       nicm      162:        environ_init(&env);
                    163:        environ_copy(&global_environ, &env);
                    164:        environ_copy(&s->environ, &env);
                    165:        server_fill_environ(s, &env);
1.1       nicm      166:
                    167:        cmd = data->cmd;
                    168:        if (cmd == NULL)
                    169:                cmd = options_get_string(&s->options, "default-command");
                    170:        if (ctx->cmdclient == NULL || ctx->cmdclient->cwd == NULL)
1.3       nicm      171:                cwd = options_get_string(&s->options, "default-path");
1.1       nicm      172:        else
                    173:                cwd = ctx->cmdclient->cwd;
                    174:
1.5       nicm      175:        size = -1;
                    176:        if (data->size != -1)
                    177:                size = data->size;
1.1       nicm      178:        else if (data->percentage != -1)
1.5       nicm      179:                size = (w->active->sy * data->percentage) / 100;
                    180:        hlimit = options_get_number(&s->options, "history-limit");
1.1       nicm      181:
1.5       nicm      182:        type = LAYOUT_TOPBOTTOM;
                    183:        if (data->flag_horizontal)
                    184:                type = LAYOUT_LEFTRIGHT;
                    185:
1.6       nicm      186:        wp = window_add_pane(w, hlimit);
1.9       nicm      187:        if (window_pane_spawn(wp, cmd, cwd, &env, &s->tio, &cause) != 0)
1.5       nicm      188:                goto error;
                    189:        if (layout_split_pane(w->active, type, size, wp) != 0) {
                    190:                cause = xstrdup("pane too small");
                    191:                goto error;
1.1       nicm      192:        }
1.5       nicm      193:
1.1       nicm      194:        server_redraw_window(w);
                    195:
                    196:        if (!data->flag_detached) {
                    197:                window_set_active_pane(w, wp);
                    198:                session_select(s, wl->idx);
                    199:                server_redraw_session(s);
                    200:        } else
                    201:                server_status_session(s);
                    202:
1.8       nicm      203:        environ_free(&env);
1.1       nicm      204:        return (0);
1.5       nicm      205:
                    206: error:
1.8       nicm      207:        environ_free(&env);
1.5       nicm      208:        if (wp != NULL)
                    209:                window_remove_pane(w, wp);
                    210:        ctx->error(ctx, "create pane failed: %s", cause);
                    211:        xfree(cause);
                    212:        return (-1);
1.1       nicm      213: }
                    214:
                    215: void
                    216: cmd_split_window_free(struct cmd *self)
                    217: {
                    218:        struct cmd_split_window_data    *data = self->data;
                    219:
                    220:        if (data->target != NULL)
                    221:                xfree(data->target);
                    222:        if (data->cmd != NULL)
                    223:                xfree(data->cmd);
                    224:        xfree(data);
                    225: }
                    226:
                    227: size_t
                    228: cmd_split_window_print(struct cmd *self, char *buf, size_t len)
                    229: {
                    230:        struct cmd_split_window_data    *data = self->data;
                    231:        size_t                           off = 0;
                    232:
                    233:        off += xsnprintf(buf, len, "%s", self->entry->name);
                    234:        if (data == NULL)
                    235:                return (off);
                    236:        if (off < len && data->flag_detached)
                    237:                off += xsnprintf(buf + off, len - off, " -d");
1.5       nicm      238:        if (off < len && data->flag_horizontal)
                    239:                off += xsnprintf(buf + off, len - off, " -h");
1.10    ! nicm      240:        if (off < len && data->size > 0)
        !           241:                off += xsnprintf(buf + off, len - off, " -l %d", data->size);
        !           242:        if (off < len && data->percentage > 0) {
        !           243:                off += xsnprintf(
        !           244:                    buf + off, len - off, " -p %d", data->percentage);
        !           245:        }
1.1       nicm      246:        if (off < len && data->target != NULL)
                    247:                off += cmd_prarg(buf + off, len - off, " -t ", data->target);
                    248:        if (off < len && data->cmd != NULL)
                    249:                off += cmd_prarg(buf + off, len - off, " ", data->cmd);
                    250:        return (off);
                    251: }