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

Annotation of src/usr.bin/tmux/cmd-new-window.c, Revision 1.44

1.44    ! nicm        1: /* $OpenBSD: cmd-new-window.c,v 1.43 2014/10/20 23:35:28 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:
1.35      nicm       21: #include <errno.h>
                     22: #include <fcntl.h>
1.1       nicm       23: #include <stdlib.h>
1.35      nicm       24: #include <string.h>
                     25: #include <unistd.h>
1.1       nicm       26:
                     27: #include "tmux.h"
                     28:
                     29: /*
                     30:  * Create a new window.
                     31:  */
1.43      nicm       32:
                     33: #define NEW_WINDOW_TEMPLATE "#{session_name}:#{window_index}.#{pane_index}"
1.1       nicm       34:
1.31      nicm       35: enum cmd_retval        cmd_new_window_exec(struct cmd *, struct cmd_q *);
1.1       nicm       36:
                     37: const struct cmd_entry cmd_new_window_entry = {
                     38:        "new-window", "neww",
1.40      nicm       39:        "ac:dF:kn:Pt:", 0, -1,
1.21      nicm       40:        "[-adkP] [-c start-directory] [-F format] [-n window-name] "
1.29      nicm       41:        CMD_TARGET_WINDOW_USAGE " [command]",
1.17      nicm       42:        0,
                     43:        cmd_new_window_exec
1.1       nicm       44: };
                     45:
1.26      nicm       46: enum cmd_retval
1.31      nicm       47: cmd_new_window_exec(struct cmd *self, struct cmd_q *cmdq)
1.1       nicm       48: {
1.21      nicm       49:        struct args             *args = self->args;
                     50:        struct session          *s;
                     51:        struct winlink          *wl;
                     52:        struct client           *c;
1.39      nicm       53:        const char              *cmd, *path, *template;
1.40      nicm       54:        char                   **argv, *cause, *cp;
                     55:        int                      argc, idx, last, detached, cwd, fd = -1;
1.21      nicm       56:        struct format_tree      *ft;
1.39      nicm       57:        struct environ_entry    *envent;
1.17      nicm       58:
                     59:        if (args_has(args, 'a')) {
1.31      nicm       60:                wl = cmd_find_window(cmdq, args_get(args, 't'), &s);
1.17      nicm       61:                if (wl == NULL)
1.26      nicm       62:                        return (CMD_RETURN_ERROR);
1.13      nicm       63:                idx = wl->idx + 1;
                     64:
                     65:                /* Find the next free index. */
                     66:                for (last = idx; last < INT_MAX; last++) {
                     67:                        if (winlink_find_by_index(&s->windows, last) == NULL)
                     68:                                break;
                     69:                }
                     70:                if (last == INT_MAX) {
1.31      nicm       71:                        cmdq_error(cmdq, "no free window indexes");
1.26      nicm       72:                        return (CMD_RETURN_ERROR);
1.13      nicm       73:                }
                     74:
                     75:                /* Move everything from last - 1 to idx up a bit. */
                     76:                for (; last > idx; last--) {
                     77:                        wl = winlink_find_by_index(&s->windows, last - 1);
                     78:                        server_link_window(s, wl, s, last, 0, 0, NULL);
                     79:                        server_unlink_window(s, wl);
                     80:                }
                     81:        } else {
1.39      nicm       82:                idx = cmd_find_index(cmdq, args_get(args, 't'), &s);
                     83:                if (idx == -2)
1.26      nicm       84:                        return (CMD_RETURN_ERROR);
1.13      nicm       85:        }
1.17      nicm       86:        detached = args_has(args, 'd');
1.1       nicm       87:
1.40      nicm       88:        if (args->argc == 0) {
1.1       nicm       89:                cmd = options_get_string(&s->options, "default-command");
1.40      nicm       90:                if (cmd != NULL && *cmd != '\0') {
                     91:                        argc = 1;
1.41      nicm       92:                        argv = (char **)&cmd;
1.40      nicm       93:                } else {
                     94:                        argc = 0;
                     95:                        argv = NULL;
                     96:                }
                     97:        } else {
                     98:                argc = args->argc;
                     99:                argv = args->argv;
                    100:        }
1.35      nicm      101:
1.39      nicm      102:        path = NULL;
                    103:        if (cmdq->client != NULL && cmdq->client->session == NULL)
                    104:                envent = environ_find(&cmdq->client->environ, "PATH");
                    105:        else
                    106:                envent = environ_find(&s->environ, "PATH");
                    107:        if (envent != NULL)
                    108:                path = envent->value;
                    109:
1.35      nicm      110:        if (args_has(args, 'c')) {
                    111:                ft = format_create();
1.44    ! nicm      112:                format_defaults(ft, cmd_find_client(cmdq, NULL, 1), s, NULL,
        !           113:                    NULL);
1.35      nicm      114:                cp = format_expand(ft, args_get(args, 'c'));
                    115:                format_free(ft);
                    116:
1.37      nicm      117:                if (cp != NULL && *cp != '\0') {
                    118:                        fd = open(cp, O_RDONLY|O_DIRECTORY);
                    119:                        free(cp);
                    120:                        if (fd == -1) {
                    121:                                cmdq_error(cmdq, "bad working directory: %s",
                    122:                                    strerror(errno));
                    123:                                return (CMD_RETURN_ERROR);
                    124:                        }
                    125:                } else if (cp != NULL)
                    126:                        free(cp);
1.35      nicm      127:                cwd = fd;
1.36      nicm      128:        } else if (cmdq->client != NULL && cmdq->client->session == NULL)
1.35      nicm      129:                cwd = cmdq->client->cwd;
                    130:        else
                    131:                cwd = s->cwd;
1.38      nicm      132:
                    133:        wl = NULL;
                    134:        if (idx != -1)
                    135:                wl = winlink_find_by_index(&s->windows, idx);
                    136:        if (wl != NULL && args_has(args, 'k')) {
                    137:                /*
                    138:                 * Can't use session_detach as it will destroy session if this
                    139:                 * makes it empty.
                    140:                 */
                    141:                notify_window_unlinked(s, wl->window);
                    142:                wl->flags &= ~WINLINK_ALERTFLAGS;
                    143:                winlink_stack_remove(&s->lastw, wl);
                    144:                winlink_remove(&s->windows, wl);
                    145:
                    146:                /* Force select/redraw if current. */
                    147:                if (wl == s->curw) {
                    148:                        detached = 0;
                    149:                        s->curw = NULL;
                    150:                }
                    151:        }
1.1       nicm      152:
1.6       nicm      153:        if (idx == -1)
                    154:                idx = -1 - options_get_number(&s->options, "base-index");
1.40      nicm      155:        wl = session_new(s, args_get(args, 'n'), argc, argv, path, cwd, idx,
                    156:                &cause);
1.1       nicm      157:        if (wl == NULL) {
1.31      nicm      158:                cmdq_error(cmdq, "create window failed: %s", cause);
1.25      nicm      159:                free(cause);
1.35      nicm      160:                goto error;
1.1       nicm      161:        }
1.17      nicm      162:        if (!detached) {
1.1       nicm      163:                session_select(s, wl->idx);
1.8       nicm      164:                server_redraw_session_group(s);
1.11      nicm      165:        } else
1.8       nicm      166:                server_status_session_group(s);
1.1       nicm      167:
1.21      nicm      168:        if (args_has(args, 'P')) {
1.24      nicm      169:                if ((template = args_get(args, 'F')) == NULL)
1.27      nicm      170:                        template = NEW_WINDOW_TEMPLATE;
1.21      nicm      171:
                    172:                ft = format_create();
1.44    ! nicm      173:                format_defaults(ft, cmd_find_client(cmdq, NULL, 1), s, wl,
        !           174:                    NULL);
1.21      nicm      175:
                    176:                cp = format_expand(ft, template);
1.31      nicm      177:                cmdq_print(cmdq, "%s", cp);
1.25      nicm      178:                free(cp);
1.21      nicm      179:
                    180:                format_free(ft);
                    181:        }
                    182:
1.35      nicm      183:        if (fd != -1)
                    184:                close(fd);
1.26      nicm      185:        return (CMD_RETURN_NORMAL);
1.35      nicm      186:
                    187: error:
                    188:        if (fd != -1)
                    189:                close(fd);
                    190:        return (CMD_RETURN_ERROR);
1.1       nicm      191: }