This commit was manufactured by cvs2svn to create tag 'TMUX_1_2'.
[tmux.git] / cmd-pipe-pane.c
blob19611893b065e26d4864c4a7ec835927058191b5
1 /* $Id: cmd-pipe-pane.c,v 1.10 2009-12-04 22:14:47 tcunha Exp $ */
3 /*
4 * Copyright (c) 2009 Nicholas Marriott <nicm@users.sourceforge.net>
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.
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.
19 #include <sys/types.h>
20 #include <sys/socket.h>
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <string.h>
25 #include <unistd.h>
27 #include "tmux.h"
30 * Open pipe to redirect pane output. If already open, close first.
33 int cmd_pipe_pane_exec(struct cmd *, struct cmd_ctx *);
35 void cmd_pipe_pane_error_callback(struct bufferevent *, short, void *);
37 const struct cmd_entry cmd_pipe_pane_entry = {
38 "pipe-pane", "pipep",
39 CMD_TARGET_PANE_USAGE "[-o] [command]",
40 CMD_ARG01, "o",
41 cmd_target_init,
42 cmd_target_parse,
43 cmd_pipe_pane_exec,
44 cmd_target_free,
45 cmd_target_print
48 int
49 cmd_pipe_pane_exec(struct cmd *self, struct cmd_ctx *ctx)
51 struct cmd_target_data *data = self->data;
52 struct window_pane *wp;
53 int old_fd, pipe_fd[2], null_fd, mode;
55 if (cmd_find_pane(ctx, data->target, NULL, &wp) == NULL)
56 return (-1);
58 /* Destroy the old pipe. */
59 old_fd = wp->pipe_fd;
60 if (wp->pipe_fd != -1) {
61 bufferevent_free(wp->pipe_event);
62 close(wp->pipe_fd);
63 wp->pipe_fd = -1;
66 /* If no pipe command, that is enough. */
67 if (data->arg == NULL || *data->arg == '\0')
68 return (0);
71 * With -o, only open the new pipe if there was no previous one. This
72 * allows a pipe to be toggled with a single key, for example:
74 * bind ^p pipep -o 'cat >>~/output'
76 if (cmd_check_flag(data->chflags, 'o') && old_fd != -1)
77 return (0);
79 /* Open the new pipe. */
80 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe_fd) != 0) {
81 ctx->error(ctx, "socketpair error: %s", strerror(errno));
82 return (-1);
85 /* Fork the child. */
86 switch (fork()) {
87 case -1:
88 ctx->error(ctx, "fork error: %s", strerror(errno));
89 return (-1);
90 case 0:
91 /* Child process. */
92 close(pipe_fd[0]);
93 server_signal_clear();
95 if (dup2(pipe_fd[1], STDIN_FILENO) == -1)
96 _exit(1);
97 if (pipe_fd[1] != STDIN_FILENO)
98 close(pipe_fd[1]);
100 null_fd = open(_PATH_DEVNULL, O_WRONLY, 0);
101 if (dup2(null_fd, STDOUT_FILENO) == -1)
102 _exit(1);
103 if (dup2(null_fd, STDERR_FILENO) == -1)
104 _exit(1);
105 if (null_fd != STDOUT_FILENO && null_fd != STDERR_FILENO)
106 close(null_fd);
108 execl(_PATH_BSHELL, "sh", "-c", data->arg, (char *) NULL);
109 _exit(1);
110 default:
111 /* Parent process. */
112 close(pipe_fd[1]);
114 wp->pipe_fd = pipe_fd[0];
115 wp->pipe_off = EVBUFFER_LENGTH(wp->event->input);
117 wp->pipe_event = bufferevent_new(wp->pipe_fd,
118 NULL, NULL, cmd_pipe_pane_error_callback, wp);
119 bufferevent_enable(wp->pipe_event, EV_WRITE);
121 if ((mode = fcntl(wp->pipe_fd, F_GETFL)) == -1)
122 fatal("fcntl failed");
123 if (fcntl(wp->pipe_fd, F_SETFL, mode|O_NONBLOCK) == -1)
124 fatal("fcntl failed");
125 if (fcntl(wp->pipe_fd, F_SETFD, FD_CLOEXEC) == -1)
126 fatal("fcntl failed");
127 return (0);
131 /* ARGSUSED */
132 void
133 cmd_pipe_pane_error_callback(
134 unused struct bufferevent *bufev, unused short what, void *data)
136 struct window_pane *wp = data;
138 bufferevent_free(wp->pipe_event);
139 close(wp->pipe_fd);
140 wp->pipe_fd = -1;