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

1.39    ! nicm        1: /* $OpenBSD: cmd-pipe-pane.c,v 1.38 2016/10/14 22:14:22 nicm Exp $ */
1.1       nicm        2:
                      3: /*
1.36      nicm        4:  * Copyright (c) 2009 Nicholas Marriott <nicholas.marriott@gmail.com>
1.1       nicm        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.39    ! nicm       36: static enum cmd_retval cmd_pipe_pane_exec(struct cmd *, struct cmdq_item *);
1.1       nicm       37:
1.37      nicm       38: static void cmd_pipe_pane_error_callback(struct bufferevent *, short, void *);
1.4       nicm       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:
1.35      nicm       47:        .tflag = CMD_PANE,
                     48:
1.38      nicm       49:        .flags = CMD_AFTERHOOK,
1.34      nicm       50:        .exec = cmd_pipe_pane_exec
1.1       nicm       51: };
                     52:
1.37      nicm       53: static enum cmd_retval
1.39    ! nicm       54: cmd_pipe_pane_exec(struct cmd *self, struct cmdq_item *item)
1.1       nicm       55: {
1.17      nicm       56:        struct args             *args = self->args;
1.39    ! nicm       57:        struct client           *c = item->state.c;
        !            58:        struct window_pane      *wp = item->state.tflag.wp;
        !            59:        struct session          *s = item->state.tflag.s;
        !            60:        struct winlink          *wl = item->state.tflag.wl;
1.29      nicm       61:        char                    *cmd;
1.18      nicm       62:        int                      old_fd, pipe_fd[2], null_fd;
1.29      nicm       63:        struct format_tree      *ft;
1.1       nicm       64:
                     65:        /* Destroy the old pipe. */
                     66:        old_fd = wp->pipe_fd;
                     67:        if (wp->pipe_fd != -1) {
1.4       nicm       68:                bufferevent_free(wp->pipe_event);
1.1       nicm       69:                close(wp->pipe_fd);
                     70:                wp->pipe_fd = -1;
                     71:        }
                     72:
                     73:        /* If no pipe command, that is enough. */
1.17      nicm       74:        if (args->argc == 0 || *args->argv[0] == '\0')
1.22      nicm       75:                return (CMD_RETURN_NORMAL);
1.1       nicm       76:
                     77:        /*
                     78:         * With -o, only open the new pipe if there was no previous one. This
                     79:         * allows a pipe to be toggled with a single key, for example:
                     80:         *
                     81:         *      bind ^p pipep -o 'cat >>~/output'
                     82:         */
1.17      nicm       83:        if (args_has(self->args, 'o') && old_fd != -1)
1.22      nicm       84:                return (CMD_RETURN_NORMAL);
1.1       nicm       85:
                     86:        /* Open the new pipe. */
1.4       nicm       87:        if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe_fd) != 0) {
1.39    ! nicm       88:                cmdq_error(item, "socketpair error: %s", strerror(errno));
1.22      nicm       89:                return (CMD_RETURN_ERROR);
1.1       nicm       90:        }
                     91:
1.29      nicm       92:        /* Expand the command. */
1.39    ! nicm       93:        ft = format_create(item, 0);
1.29      nicm       94:        format_defaults(ft, c, s, wl, wp);
                     95:        cmd = format_expand_time(ft, args->argv[0], time(NULL));
                     96:        format_free(ft);
                     97:
1.1       nicm       98:        /* Fork the child. */
                     99:        switch (fork()) {
                    100:        case -1:
1.39    ! nicm      101:                cmdq_error(item, "fork error: %s", strerror(errno));
1.29      nicm      102:
                    103:                free(cmd);
1.22      nicm      104:                return (CMD_RETURN_ERROR);
1.1       nicm      105:        case 0:
                    106:                /* Child process. */
                    107:                close(pipe_fd[0]);
1.15      nicm      108:                clear_signals(1);
1.1       nicm      109:
                    110:                if (dup2(pipe_fd[1], STDIN_FILENO) == -1)
                    111:                        _exit(1);
                    112:                if (pipe_fd[1] != STDIN_FILENO)
                    113:                        close(pipe_fd[1]);
                    114:
                    115:                null_fd = open(_PATH_DEVNULL, O_WRONLY, 0);
                    116:                if (dup2(null_fd, STDOUT_FILENO) == -1)
                    117:                        _exit(1);
                    118:                if (dup2(null_fd, STDERR_FILENO) == -1)
                    119:                        _exit(1);
                    120:                if (null_fd != STDOUT_FILENO && null_fd != STDERR_FILENO)
                    121:                        close(null_fd);
                    122:
1.16      nicm      123:                closefrom(STDERR_FILENO + 1);
                    124:
1.29      nicm      125:                execl(_PATH_BSHELL, "sh", "-c", cmd, (char *) NULL);
1.1       nicm      126:                _exit(1);
                    127:        default:
                    128:                /* Parent process. */
                    129:                close(pipe_fd[1]);
                    130:
                    131:                wp->pipe_fd = pipe_fd[0];
1.5       nicm      132:                wp->pipe_off = EVBUFFER_LENGTH(wp->event->input);
1.9       nicm      133:
1.4       nicm      134:                wp->pipe_event = bufferevent_new(wp->pipe_fd,
                    135:                    NULL, NULL, cmd_pipe_pane_error_callback, wp);
                    136:                bufferevent_enable(wp->pipe_event, EV_WRITE);
1.9       nicm      137:
1.18      nicm      138:                setblocking(wp->pipe_fd, 0);
1.29      nicm      139:
                    140:                free(cmd);
1.22      nicm      141:                return (CMD_RETURN_NORMAL);
1.1       nicm      142:        }
1.4       nicm      143: }
                    144:
1.37      nicm      145: static void
1.30      nicm      146: cmd_pipe_pane_error_callback(__unused struct bufferevent *bufev,
                    147:     __unused short what, void *data)
1.4       nicm      148: {
                    149:        struct window_pane      *wp = data;
                    150:
                    151:        bufferevent_free(wp->pipe_event);
                    152:        close(wp->pipe_fd);
                    153:        wp->pipe_fd = -1;
1.1       nicm      154: }