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>
32 * Open pipe to redirect pane output. If already open, close first.
35 int cmd_pipe_pane_exec(struct cmd
*, struct cmd_ctx
*);
37 void cmd_pipe_pane_error_callback(struct bufferevent
*, short, void *);
39 const struct cmd_entry cmd_pipe_pane_entry
= {
42 CMD_TARGET_PANE_USAGE
"[-o] [command]",
50 cmd_pipe_pane_exec(struct cmd
*self
, struct cmd_ctx
*ctx
)
52 struct args
*args
= self
->args
;
54 struct window_pane
*wp
;
56 int old_fd
, pipe_fd
[2], null_fd
;
58 if ((c
= cmd_find_client(ctx
, NULL
)) == NULL
)
61 if (cmd_find_pane(ctx
, args_get(args
, 't'), NULL
, &wp
) == NULL
)
64 /* Destroy the old pipe. */
66 if (wp
->pipe_fd
!= -1) {
67 bufferevent_free(wp
->pipe_event
);
72 /* If no pipe command, that is enough. */
73 if (args
->argc
== 0 || *args
->argv
[0] == '\0')
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:
80 * bind ^p pipep -o 'cat >>~/output'
82 if (args_has(self
->args
, 'o') && old_fd
!= -1)
85 /* Open the new pipe. */
86 if (socketpair(AF_UNIX
, SOCK_STREAM
, PF_UNSPEC
, pipe_fd
) != 0) {
87 ctx
->error(ctx
, "socketpair error: %s", strerror(errno
));
94 ctx
->error(ctx
, "fork error: %s", strerror(errno
));
101 if (dup2(pipe_fd
[1], STDIN_FILENO
) == -1)
103 if (pipe_fd
[1] != STDIN_FILENO
)
106 null_fd
= open(_PATH_DEVNULL
, O_WRONLY
, 0);
107 if (dup2(null_fd
, STDOUT_FILENO
) == -1)
109 if (dup2(null_fd
, STDERR_FILENO
) == -1)
111 if (null_fd
!= STDOUT_FILENO
&& null_fd
!= STDERR_FILENO
)
114 closefrom(STDERR_FILENO
+ 1);
116 command
= status_replace(c
, NULL
, args
->argv
[0], time(NULL
), 0);
117 execl(_PATH_BSHELL
, "sh", "-c", command
, (char *) NULL
);
120 /* Parent process. */
123 wp
->pipe_fd
= pipe_fd
[0];
124 wp
->pipe_off
= EVBUFFER_LENGTH(wp
->event
->input
);
126 wp
->pipe_event
= bufferevent_new(wp
->pipe_fd
,
127 NULL
, NULL
, cmd_pipe_pane_error_callback
, wp
);
128 bufferevent_enable(wp
->pipe_event
, EV_WRITE
);
130 setblocking(wp
->pipe_fd
, 0);
137 cmd_pipe_pane_error_callback(
138 unused
struct bufferevent
*bufev
, unused
short what
, void *data
)
140 struct window_pane
*wp
= data
;
142 bufferevent_free(wp
->pipe_event
);