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

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