Annotation of src/usr.bin/tmux/cmd-pipe-pane.c, Revision 1.19
1.19 ! nicm 1: /* $OpenBSD: cmd-pipe-pane.c,v 1.18 2011/01/08 01:52:36 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>
25: #include <string.h>
1.13 nicm 26: #include <time.h>
1.1 nicm 27: #include <unistd.h>
28:
29: #include "tmux.h"
30:
31: /*
32: * Open pipe to redirect pane output. If already open, close first.
33: */
34:
35: int cmd_pipe_pane_exec(struct cmd *, struct cmd_ctx *);
36:
1.4 nicm 37: void cmd_pipe_pane_error_callback(struct bufferevent *, short, void *);
38:
1.1 nicm 39: const struct cmd_entry cmd_pipe_pane_entry = {
40: "pipe-pane", "pipep",
1.17 nicm 41: "ot:", 0, 1,
1.1 nicm 42: CMD_TARGET_PANE_USAGE "[-o] [command]",
1.17 nicm 43: 0,
44: NULL,
45: NULL,
46: cmd_pipe_pane_exec
1.1 nicm 47: };
48:
49: int
50: cmd_pipe_pane_exec(struct cmd *self, struct cmd_ctx *ctx)
51: {
1.17 nicm 52: struct args *args = self->args;
1.13 nicm 53: struct client *c;
1.1 nicm 54: struct window_pane *wp;
1.13 nicm 55: char *command;
1.18 nicm 56: int old_fd, pipe_fd[2], null_fd;
1.1 nicm 57:
1.14 nicm 58: if ((c = cmd_find_client(ctx, NULL)) == NULL)
1.13 nicm 59: return (-1);
60:
1.17 nicm 61: if (cmd_find_pane(ctx, args_get(args, 't'), NULL, &wp) == NULL)
1.1 nicm 62: return (-1);
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.1 nicm 74: return (0);
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.1 nicm 83: return (0);
84:
85: /* Open the new pipe. */
1.4 nicm 86: if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe_fd) != 0) {
87: ctx->error(ctx, "socketpair error: %s", strerror(errno));
1.1 nicm 88: return (-1);
89: }
90:
91: /* Fork the child. */
92: switch (fork()) {
93: case -1:
94: ctx->error(ctx, "fork error: %s", strerror(errno));
1.9 nicm 95: return (-1);
1.1 nicm 96: case 0:
97: /* Child process. */
98: close(pipe_fd[0]);
1.15 nicm 99: clear_signals(1);
1.1 nicm 100:
101: if (dup2(pipe_fd[1], STDIN_FILENO) == -1)
102: _exit(1);
103: if (pipe_fd[1] != STDIN_FILENO)
104: close(pipe_fd[1]);
105:
106: null_fd = open(_PATH_DEVNULL, O_WRONLY, 0);
107: if (dup2(null_fd, STDOUT_FILENO) == -1)
108: _exit(1);
109: if (dup2(null_fd, STDERR_FILENO) == -1)
110: _exit(1);
111: if (null_fd != STDOUT_FILENO && null_fd != STDERR_FILENO)
112: close(null_fd);
113:
1.16 nicm 114: closefrom(STDERR_FILENO + 1);
115:
1.19 ! nicm 116: command = status_replace(
! 117: c, NULL, NULL, NULL, args->argv[0], time(NULL), 0);
1.13 nicm 118: execl(_PATH_BSHELL, "sh", "-c", command, (char *) NULL);
1.1 nicm 119: _exit(1);
120: default:
121: /* Parent process. */
122: close(pipe_fd[1]);
123:
124: wp->pipe_fd = pipe_fd[0];
1.5 nicm 125: wp->pipe_off = EVBUFFER_LENGTH(wp->event->input);
1.9 nicm 126:
1.4 nicm 127: wp->pipe_event = bufferevent_new(wp->pipe_fd,
128: NULL, NULL, cmd_pipe_pane_error_callback, wp);
129: bufferevent_enable(wp->pipe_event, EV_WRITE);
1.9 nicm 130:
1.18 nicm 131: setblocking(wp->pipe_fd, 0);
1.1 nicm 132: return (0);
133: }
1.4 nicm 134: }
135:
1.8 nicm 136: /* ARGSUSED */
1.4 nicm 137: void
138: cmd_pipe_pane_error_callback(
139: unused struct bufferevent *bufev, unused short what, void *data)
140: {
141: struct window_pane *wp = data;
142:
143: bufferevent_free(wp->pipe_event);
144: close(wp->pipe_fd);
145: wp->pipe_fd = -1;
1.1 nicm 146: }