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

1.34    ! nicm        1: /* $OpenBSD: cmd-pipe-pane.c,v 1.33 2015/12/13 14:32:38 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>
1.4       nicm       20: #include <sys/socket.h>
1.1       nicm       21:
                     22: #include <errno.h>
                     23: #include <fcntl.h>
                     24: #include <paths.h>
1.29      nicm       25: #include <stdlib.h>
1.1       nicm       26: #include <string.h>
1.13      nicm       27: #include <time.h>
1.1       nicm       28: #include <unistd.h>
                     29:
                     30: #include "tmux.h"
                     31:
                     32: /*
                     33:  * Open pipe to redirect pane output. If already open, close first.
                     34:  */
                     35:
1.25      nicm       36: enum cmd_retval         cmd_pipe_pane_exec(struct cmd *, struct cmd_q *);
1.1       nicm       37:
1.4       nicm       38: void   cmd_pipe_pane_error_callback(struct bufferevent *, short, void *);
                     39:
1.1       nicm       40: const struct cmd_entry cmd_pipe_pane_entry = {
1.34    ! nicm       41:        .name = "pipe-pane",
        !            42:        .alias = "pipep",
        !            43:
        !            44:        .args = { "ot:", 0, 1 },
        !            45:        .usage = "[-o] " CMD_TARGET_PANE_USAGE " [command]",
        !            46:
        !            47:        .flags = CMD_PANE_T,
        !            48:        .exec = cmd_pipe_pane_exec
1.1       nicm       49: };
                     50:
1.22      nicm       51: enum cmd_retval
1.25      nicm       52: cmd_pipe_pane_exec(struct cmd *self, struct cmd_q *cmdq)
1.1       nicm       53: {
1.17      nicm       54:        struct args             *args = self->args;
1.33      nicm       55:        struct client           *c = cmdq->state.c;
                     56:        struct window_pane      *wp = cmdq->state.tflag.wp;
                     57:        struct session          *s = cmdq->state.tflag.s;
                     58:        struct winlink          *wl = cmdq->state.tflag.wl;
1.29      nicm       59:        char                    *cmd;
1.18      nicm       60:        int                      old_fd, pipe_fd[2], null_fd;
1.29      nicm       61:        struct format_tree      *ft;
1.1       nicm       62:
                     63:        /* Destroy the old pipe. */
                     64:        old_fd = wp->pipe_fd;
                     65:        if (wp->pipe_fd != -1) {
1.4       nicm       66:                bufferevent_free(wp->pipe_event);
1.1       nicm       67:                close(wp->pipe_fd);
                     68:                wp->pipe_fd = -1;
                     69:        }
                     70:
                     71:        /* If no pipe command, that is enough. */
1.17      nicm       72:        if (args->argc == 0 || *args->argv[0] == '\0')
1.22      nicm       73:                return (CMD_RETURN_NORMAL);
1.1       nicm       74:
                     75:        /*
                     76:         * With -o, only open the new pipe if there was no previous one. This
                     77:         * allows a pipe to be toggled with a single key, for example:
                     78:         *
                     79:         *      bind ^p pipep -o 'cat >>~/output'
                     80:         */
1.17      nicm       81:        if (args_has(self->args, 'o') && old_fd != -1)
1.22      nicm       82:                return (CMD_RETURN_NORMAL);
1.1       nicm       83:
                     84:        /* Open the new pipe. */
1.4       nicm       85:        if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe_fd) != 0) {
1.25      nicm       86:                cmdq_error(cmdq, "socketpair error: %s", strerror(errno));
1.22      nicm       87:                return (CMD_RETURN_ERROR);
1.1       nicm       88:        }
                     89:
1.29      nicm       90:        /* Expand the command. */
1.32      nicm       91:        ft = format_create(cmdq, 0);
1.29      nicm       92:        format_defaults(ft, c, s, wl, wp);
                     93:        cmd = format_expand_time(ft, args->argv[0], time(NULL));
                     94:        format_free(ft);
                     95:
1.1       nicm       96:        /* Fork the child. */
                     97:        switch (fork()) {
                     98:        case -1:
1.25      nicm       99:                cmdq_error(cmdq, "fork error: %s", strerror(errno));
1.29      nicm      100:
                    101:                free(cmd);
1.22      nicm      102:                return (CMD_RETURN_ERROR);
1.1       nicm      103:        case 0:
                    104:                /* Child process. */
                    105:                close(pipe_fd[0]);
1.15      nicm      106:                clear_signals(1);
1.1       nicm      107:
                    108:                if (dup2(pipe_fd[1], STDIN_FILENO) == -1)
                    109:                        _exit(1);
                    110:                if (pipe_fd[1] != STDIN_FILENO)
                    111:                        close(pipe_fd[1]);
                    112:
                    113:                null_fd = open(_PATH_DEVNULL, O_WRONLY, 0);
                    114:                if (dup2(null_fd, STDOUT_FILENO) == -1)
                    115:                        _exit(1);
                    116:                if (dup2(null_fd, STDERR_FILENO) == -1)
                    117:                        _exit(1);
                    118:                if (null_fd != STDOUT_FILENO && null_fd != STDERR_FILENO)
                    119:                        close(null_fd);
                    120:
1.16      nicm      121:                closefrom(STDERR_FILENO + 1);
                    122:
1.29      nicm      123:                execl(_PATH_BSHELL, "sh", "-c", cmd, (char *) NULL);
1.1       nicm      124:                _exit(1);
                    125:        default:
                    126:                /* Parent process. */
                    127:                close(pipe_fd[1]);
                    128:
                    129:                wp->pipe_fd = pipe_fd[0];
1.5       nicm      130:                wp->pipe_off = EVBUFFER_LENGTH(wp->event->input);
1.9       nicm      131:
1.4       nicm      132:                wp->pipe_event = bufferevent_new(wp->pipe_fd,
                    133:                    NULL, NULL, cmd_pipe_pane_error_callback, wp);
                    134:                bufferevent_enable(wp->pipe_event, EV_WRITE);
1.9       nicm      135:
1.18      nicm      136:                setblocking(wp->pipe_fd, 0);
1.29      nicm      137:
                    138:                free(cmd);
1.22      nicm      139:                return (CMD_RETURN_NORMAL);
1.1       nicm      140:        }
1.4       nicm      141: }
                    142:
                    143: void
1.30      nicm      144: cmd_pipe_pane_error_callback(__unused struct bufferevent *bufev,
                    145:     __unused short what, void *data)
1.4       nicm      146: {
                    147:        struct window_pane      *wp = data;
                    148:
                    149:        bufferevent_free(wp->pipe_event);
                    150:        close(wp->pipe_fd);
                    151:        wp->pipe_fd = -1;
1.1       nicm      152: }