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

Annotation of src/usr.bin/tmux/cmd-pipe-pane.c, Revision 1.3

1.3     ! nicm        1: /* $OpenBSD: cmd-pipe-pane.c,v 1.2 2009/10/21 18:12:31 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 <errno.h>
                     22: #include <fcntl.h>
                     23: #include <paths.h>
                     24: #include <string.h>
                     25: #include <unistd.h>
                     26:
                     27: #include "tmux.h"
                     28:
                     29: /*
                     30:  * Open pipe to redirect pane output. If already open, close first.
                     31:  */
                     32:
                     33: int    cmd_pipe_pane_exec(struct cmd *, struct cmd_ctx *);
                     34:
                     35: const struct cmd_entry cmd_pipe_pane_entry = {
                     36:        "pipe-pane", "pipep",
                     37:        CMD_TARGET_PANE_USAGE "[-o] [command]",
                     38:        CMD_ARG01, CMD_CHFLAG('o'),
                     39:        cmd_target_init,
                     40:        cmd_target_parse,
                     41:        cmd_pipe_pane_exec,
                     42:        cmd_target_free,
                     43:        cmd_target_print
                     44: };
                     45:
                     46: int
                     47: cmd_pipe_pane_exec(struct cmd *self, struct cmd_ctx *ctx)
                     48: {
                     49:        struct cmd_target_data  *data = self->data;
                     50:        struct window_pane      *wp;
                     51:        int                      old_fd, pipe_fd[2], null_fd, mode;
                     52:
1.2       nicm       53:        if (cmd_find_pane(ctx, data->target, NULL, &wp) == NULL)
1.1       nicm       54:                return (-1);
                     55:
                     56:        /* Destroy the old pipe. */
                     57:        old_fd = wp->pipe_fd;
                     58:        if (wp->pipe_fd != -1) {
                     59:                buffer_destroy(wp->pipe_buf);
                     60:                close(wp->pipe_fd);
                     61:                wp->pipe_fd = -1;
                     62:        }
                     63:
                     64:        /* If no pipe command, that is enough. */
                     65:        if (data->arg == NULL || *data->arg == '\0')
                     66:                return (0);
                     67:
                     68:        /*
                     69:         * With -o, only open the new pipe if there was no previous one. This
                     70:         * allows a pipe to be toggled with a single key, for example:
                     71:         *
                     72:         *      bind ^p pipep -o 'cat >>~/output'
                     73:         */
                     74:        if (data->chflags & CMD_CHFLAG('o') && old_fd != -1)
                     75:                return (0);
                     76:
                     77:        /* Open the new pipe. */
                     78:        if (pipe(pipe_fd) != 0) {
                     79:                ctx->error(ctx, "pipe error: %s", strerror(errno));
                     80:                return (-1);
                     81:        }
                     82:
                     83:        /* Fork the child. */
                     84:        switch (fork()) {
                     85:        case -1:
                     86:                ctx->error(ctx, "fork error: %s", strerror(errno));
                     87:                return (-1);
                     88:        case 0:
                     89:                /* Child process. */
                     90:                close(pipe_fd[0]);
1.3     ! nicm       91:                server_signal_clear();
1.1       nicm       92:
                     93:                if (dup2(pipe_fd[1], STDIN_FILENO) == -1)
                     94:                        _exit(1);
                     95:                if (pipe_fd[1] != STDIN_FILENO)
                     96:                        close(pipe_fd[1]);
                     97:
                     98:                null_fd = open(_PATH_DEVNULL, O_WRONLY, 0);
                     99:                if (dup2(null_fd, STDOUT_FILENO) == -1)
                    100:                        _exit(1);
                    101:                if (dup2(null_fd, STDERR_FILENO) == -1)
                    102:                        _exit(1);
                    103:                if (null_fd != STDOUT_FILENO && null_fd != STDERR_FILENO)
                    104:                        close(null_fd);
                    105:
                    106:                execl(_PATH_BSHELL, "sh", "-c", data->arg, (char *) NULL);
                    107:                _exit(1);
                    108:        default:
                    109:                /* Parent process. */
                    110:                close(pipe_fd[1]);
                    111:
                    112:                wp->pipe_fd = pipe_fd[0];
                    113:                wp->pipe_buf = buffer_create(BUFSIZ);
                    114:                wp->pipe_off = BUFFER_USED(wp->in);
                    115:
                    116:                if ((mode = fcntl(wp->pipe_fd, F_GETFL)) == -1)
                    117:                        fatal("fcntl failed");
                    118:                if (fcntl(wp->pipe_fd, F_SETFL, mode|O_NONBLOCK) == -1)
                    119:                        fatal("fcntl failed");
                    120:                if (fcntl(wp->pipe_fd, F_SETFD, FD_CLOEXEC) == -1)
                    121:                        fatal("fcntl failed");
                    122:                return (0);
                    123:        }
                    124:
                    125:        return (0);
                    126: }