[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.19

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