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

1.49    ! nicm        1: /* $OpenBSD: cmd-split-window.c,v 1.48 2013/11/22 20:58:36 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.46      nicm       21: #include <errno.h>
                     22: #include <fcntl.h>
1.11      nicm       23: #include <paths.h>
1.1       nicm       24: #include <stdlib.h>
1.46      nicm       25: #include <string.h>
1.1       nicm       26: #include <unistd.h>
                     27:
                     28: #include "tmux.h"
                     29:
                     30: /*
                     31:  * Split a window (add a new pane).
                     32:  */
                     33:
1.35      nicm       34: void            cmd_split_window_key_binding(struct cmd *, int);
1.40      nicm       35: enum cmd_retval         cmd_split_window_exec(struct cmd *, struct cmd_q *);
1.1       nicm       36:
                     37: const struct cmd_entry cmd_split_window_entry = {
                     38:        "split-window", "splitw",
1.30      nicm       39:        "c:dF:l:hp:Pt:v", 0, 1,
                     40:        "[-dhvP] [-c start-directory] [-F format] [-p percentage|-l size] "
1.37      nicm       41:        CMD_TARGET_PANE_USAGE " [command]",
1.22      nicm       42:        0,
1.44      nicm       43:        cmd_split_window_key_binding,
1.22      nicm       44:        cmd_split_window_exec
1.1       nicm       45: };
                     46:
                     47: void
1.22      nicm       48: cmd_split_window_key_binding(struct cmd *self, int key)
1.1       nicm       49: {
1.22      nicm       50:        self->args = args_create(0);
                     51:        if (key == '%')
                     52:                args_set(self->args, 'h', NULL);
1.1       nicm       53: }
                     54:
1.35      nicm       55: enum cmd_retval
1.40      nicm       56: cmd_split_window_exec(struct cmd *self, struct cmd_q *cmdq)
1.1       nicm       57: {
1.22      nicm       58:        struct args             *args = self->args;
                     59:        struct session          *s;
                     60:        struct winlink          *wl;
                     61:        struct window           *w;
                     62:        struct window_pane      *wp, *new_wp = NULL;
                     63:        struct environ           env;
1.49    ! nicm       64:        const char              *cmd, *path, *shell, *template;
1.46      nicm       65:        char                    *cause, *new_cause, *cp;
1.30      nicm       66:        u_int                    hlimit;
1.46      nicm       67:        int                      size, percentage, cwd, fd = -1;
1.22      nicm       68:        enum layout_type         type;
                     69:        struct layout_cell      *lc;
1.30      nicm       70:        struct client           *c;
                     71:        struct format_tree      *ft;
1.49    ! nicm       72:        struct environ_entry    *envent;
1.1       nicm       73:
1.40      nicm       74:        if ((wl = cmd_find_pane(cmdq, args_get(args, 't'), &s, &wp)) == NULL)
1.35      nicm       75:                return (CMD_RETURN_ERROR);
1.1       nicm       76:        w = wl->window;
1.41      nicm       77:        server_unzoom_window(w);
1.1       nicm       78:
1.8       nicm       79:        environ_init(&env);
                     80:        environ_copy(&global_environ, &env);
                     81:        environ_copy(&s->environ, &env);
                     82:        server_fill_environ(s, &env);
1.1       nicm       83:
1.22      nicm       84:        if (args->argc == 0)
1.1       nicm       85:                cmd = options_get_string(&s->options, "default-command");
1.22      nicm       86:        else
                     87:                cmd = args->argv[0];
1.46      nicm       88:
                     89:        if (args_has(args, 'c')) {
                     90:                ft = format_create();
                     91:                if ((c = cmd_find_client(cmdq, NULL, 1)) != NULL)
                     92:                        format_client(ft, c);
                     93:                format_session(ft, s);
                     94:                format_winlink(ft, s, s->curw);
                     95:                format_window_pane(ft, s->curw->window->active);
                     96:                cp = format_expand(ft, args_get(args, 'c'));
                     97:                format_free(ft);
                     98:
1.48      nicm       99:                if (cp != NULL && *cp != '\0') {
                    100:                        fd = open(cp, O_RDONLY|O_DIRECTORY);
                    101:                        free(cp);
                    102:                        if (fd == -1) {
                    103:                                cmdq_error(cmdq, "bad working directory: %s",
                    104:                                    strerror(errno));
                    105:                                return (CMD_RETURN_ERROR);
                    106:                        }
                    107:                } else if (cp != NULL)
                    108:                        free(cp);
1.46      nicm      109:                cwd = fd;
1.47      nicm      110:        } else if (cmdq->client != NULL && cmdq->client->session == NULL)
1.46      nicm      111:                cwd = cmdq->client->cwd;
                    112:        else
                    113:                cwd = s->cwd;
1.1       nicm      114:
1.15      nicm      115:        type = LAYOUT_TOPBOTTOM;
1.22      nicm      116:        if (args_has(args, 'h'))
1.15      nicm      117:                type = LAYOUT_LEFTRIGHT;
                    118:
1.5       nicm      119:        size = -1;
1.23      nicm      120:        if (args_has(args, 'l')) {
                    121:                size = args_strtonum(args, 'l', 0, INT_MAX, &cause);
1.22      nicm      122:                if (cause != NULL) {
1.24      nicm      123:                        xasprintf(&new_cause, "size %s", cause);
1.34      nicm      124:                        free(cause);
1.24      nicm      125:                        cause = new_cause;
                    126:                        goto error;
1.22      nicm      127:                }
                    128:        } else if (args_has(args, 'p')) {
                    129:                percentage = args_strtonum(args, 'p', 0, INT_MAX, &cause);
                    130:                if (cause != NULL) {
1.24      nicm      131:                        xasprintf(&new_cause, "percentage %s", cause);
1.34      nicm      132:                        free(cause);
1.24      nicm      133:                        cause = new_cause;
                    134:                        goto error;
1.22      nicm      135:                }
1.15      nicm      136:                if (type == LAYOUT_TOPBOTTOM)
1.22      nicm      137:                        size = (wp->sy * percentage) / 100;
1.15      nicm      138:                else
1.22      nicm      139:                        size = (wp->sx * percentage) / 100;
1.15      nicm      140:        }
1.5       nicm      141:        hlimit = options_get_number(&s->options, "history-limit");
                    142:
1.11      nicm      143:        shell = options_get_string(&s->options, "default-shell");
                    144:        if (*shell == '\0' || areshell(shell))
                    145:                shell = _PATH_BSHELL;
                    146:
1.29      nicm      147:        if ((lc = layout_split_pane(wp, type, size, 0)) == NULL) {
1.5       nicm      148:                cause = xstrdup("pane too small");
                    149:                goto error;
1.1       nicm      150:        }
1.19      nicm      151:        new_wp = window_add_pane(w, hlimit);
1.49    ! nicm      152:
        !           153:        path = NULL;
        !           154:        if (cmdq->client != NULL && cmdq->client->session == NULL)
        !           155:                envent = environ_find(&cmdq->client->environ, "PATH");
        !           156:        else
        !           157:                envent = environ_find(&s->environ, "PATH");
        !           158:        if (envent != NULL)
        !           159:                path = envent->value;
        !           160:
1.42      nicm      161:        if (window_pane_spawn(
1.49    ! nicm      162:            new_wp, cmd, path, shell, cwd, &env, s->tio, &cause) != 0)
1.19      nicm      163:                goto error;
                    164:        layout_assign_pane(lc, new_wp);
1.5       nicm      165:
1.1       nicm      166:        server_redraw_window(w);
                    167:
1.22      nicm      168:        if (!args_has(args, 'd')) {
1.17      nicm      169:                window_set_active_pane(w, new_wp);
1.1       nicm      170:                session_select(s, wl->idx);
                    171:                server_redraw_session(s);
                    172:        } else
                    173:                server_status_session(s);
                    174:
1.8       nicm      175:        environ_free(&env);
1.21      nicm      176:
1.22      nicm      177:        if (args_has(args, 'P')) {
1.33      nicm      178:                if ((template = args_get(args, 'F')) == NULL)
1.36      nicm      179:                        template = SPLIT_WINDOW_TEMPLATE;
1.30      nicm      180:
                    181:                ft = format_create();
1.40      nicm      182:                if ((c = cmd_find_client(cmdq, NULL, 1)) != NULL)
                    183:                        format_client(ft, c);
1.30      nicm      184:                format_session(ft, s);
                    185:                format_winlink(ft, s, wl);
                    186:                format_window_pane(ft, new_wp);
                    187:
                    188:                cp = format_expand(ft, template);
1.40      nicm      189:                cmdq_print(cmdq, "%s", cp);
1.34      nicm      190:                free(cp);
1.30      nicm      191:
                    192:                format_free(ft);
1.21      nicm      193:        }
1.32      nicm      194:        notify_window_layout_changed(w);
1.46      nicm      195:
                    196:        if (fd != -1)
                    197:                close(fd);
1.35      nicm      198:        return (CMD_RETURN_NORMAL);
1.5       nicm      199:
                    200: error:
1.8       nicm      201:        environ_free(&env);
1.17      nicm      202:        if (new_wp != NULL)
                    203:                window_remove_pane(w, new_wp);
1.40      nicm      204:        cmdq_error(cmdq, "create pane failed: %s", cause);
1.34      nicm      205:        free(cause);
1.46      nicm      206:        if (fd != -1)
                    207:                close(fd);
1.35      nicm      208:        return (CMD_RETURN_ERROR);
1.1       nicm      209: }