2011-07-09 11:42:33 +02:00
|
|
|
/* $Id$ */
|
2009-10-12 02:35:08 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2009 Nicholas Marriott <nicm@users.sourceforge.net>
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
|
|
|
|
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
2009-11-08 23:59:53 +01:00
|
|
|
#include <sys/socket.h>
|
2009-10-12 02:35:08 +02:00
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <string.h>
|
2010-06-06 02:28:00 +02:00
|
|
|
#include <time.h>
|
2009-10-12 02:35:08 +02:00
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "tmux.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Open pipe to redirect pane output. If already open, close first.
|
|
|
|
*/
|
|
|
|
|
|
|
|
int cmd_pipe_pane_exec(struct cmd *, struct cmd_ctx *);
|
|
|
|
|
2009-11-08 23:59:53 +01:00
|
|
|
void cmd_pipe_pane_error_callback(struct bufferevent *, short, void *);
|
|
|
|
|
2009-10-12 02:35:08 +02:00
|
|
|
const struct cmd_entry cmd_pipe_pane_entry = {
|
|
|
|
"pipe-pane", "pipep",
|
2011-01-07 15:45:34 +01:00
|
|
|
"ot:", 0, 1,
|
2011-10-31 14:55:43 +01:00
|
|
|
"[-o] " CMD_TARGET_PANE_USAGE " [command]",
|
2011-01-07 15:45:34 +01:00
|
|
|
0,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
cmd_pipe_pane_exec
|
2009-10-12 02:35:08 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
int
|
|
|
|
cmd_pipe_pane_exec(struct cmd *self, struct cmd_ctx *ctx)
|
|
|
|
{
|
2011-01-07 15:45:34 +01:00
|
|
|
struct args *args = self->args;
|
2010-06-06 02:28:00 +02:00
|
|
|
struct client *c;
|
2009-10-12 02:35:08 +02:00
|
|
|
struct window_pane *wp;
|
2010-06-06 02:28:00 +02:00
|
|
|
char *command;
|
2011-01-22 00:44:13 +01:00
|
|
|
int old_fd, pipe_fd[2], null_fd;
|
2009-10-12 02:35:08 +02:00
|
|
|
|
2011-01-07 15:45:34 +01:00
|
|
|
if (cmd_find_pane(ctx, args_get(args, 't'), NULL, &wp) == NULL)
|
2009-10-12 02:35:08 +02:00
|
|
|
return (-1);
|
2011-08-04 19:04:08 +02:00
|
|
|
c = cmd_find_client(ctx, NULL);
|
2009-10-12 02:35:08 +02:00
|
|
|
|
|
|
|
/* Destroy the old pipe. */
|
|
|
|
old_fd = wp->pipe_fd;
|
|
|
|
if (wp->pipe_fd != -1) {
|
2009-11-08 23:59:53 +01:00
|
|
|
bufferevent_free(wp->pipe_event);
|
2009-10-12 02:35:08 +02:00
|
|
|
close(wp->pipe_fd);
|
|
|
|
wp->pipe_fd = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If no pipe command, that is enough. */
|
2011-01-07 15:45:34 +01:00
|
|
|
if (args->argc == 0 || *args->argv[0] == '\0')
|
2009-10-12 02:35:08 +02:00
|
|
|
return (0);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* With -o, only open the new pipe if there was no previous one. This
|
|
|
|
* allows a pipe to be toggled with a single key, for example:
|
|
|
|
*
|
|
|
|
* bind ^p pipep -o 'cat >>~/output'
|
|
|
|
*/
|
2011-01-07 15:45:34 +01:00
|
|
|
if (args_has(self->args, 'o') && old_fd != -1)
|
2009-10-12 02:35:08 +02:00
|
|
|
return (0);
|
|
|
|
|
|
|
|
/* Open the new pipe. */
|
2009-11-08 23:59:53 +01:00
|
|
|
if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe_fd) != 0) {
|
|
|
|
ctx->error(ctx, "socketpair error: %s", strerror(errno));
|
2009-10-12 02:35:08 +02:00
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Fork the child. */
|
|
|
|
switch (fork()) {
|
|
|
|
case -1:
|
|
|
|
ctx->error(ctx, "fork error: %s", strerror(errno));
|
2009-12-04 23:14:47 +01:00
|
|
|
return (-1);
|
2009-10-12 02:35:08 +02:00
|
|
|
case 0:
|
|
|
|
/* Child process. */
|
|
|
|
close(pipe_fd[0]);
|
2010-08-29 16:42:11 +02:00
|
|
|
clear_signals(1);
|
2009-10-12 02:35:08 +02:00
|
|
|
|
|
|
|
if (dup2(pipe_fd[1], STDIN_FILENO) == -1)
|
|
|
|
_exit(1);
|
|
|
|
if (pipe_fd[1] != STDIN_FILENO)
|
|
|
|
close(pipe_fd[1]);
|
|
|
|
|
|
|
|
null_fd = open(_PATH_DEVNULL, O_WRONLY, 0);
|
|
|
|
if (dup2(null_fd, STDOUT_FILENO) == -1)
|
|
|
|
_exit(1);
|
|
|
|
if (dup2(null_fd, STDERR_FILENO) == -1)
|
|
|
|
_exit(1);
|
|
|
|
if (null_fd != STDOUT_FILENO && null_fd != STDERR_FILENO)
|
|
|
|
close(null_fd);
|
|
|
|
|
2010-10-24 02:45:57 +02:00
|
|
|
closefrom(STDERR_FILENO + 1);
|
|
|
|
|
2011-04-07 00:21:02 +02:00
|
|
|
command = status_replace(
|
|
|
|
c, NULL, NULL, NULL, args->argv[0], time(NULL), 0);
|
2010-06-06 02:28:00 +02:00
|
|
|
execl(_PATH_BSHELL, "sh", "-c", command, (char *) NULL);
|
2009-10-12 02:35:08 +02:00
|
|
|
_exit(1);
|
|
|
|
default:
|
|
|
|
/* Parent process. */
|
|
|
|
close(pipe_fd[1]);
|
|
|
|
|
|
|
|
wp->pipe_fd = pipe_fd[0];
|
2009-11-09 00:02:56 +01:00
|
|
|
wp->pipe_off = EVBUFFER_LENGTH(wp->event->input);
|
2009-12-04 23:14:47 +01:00
|
|
|
|
2009-11-08 23:59:53 +01:00
|
|
|
wp->pipe_event = bufferevent_new(wp->pipe_fd,
|
|
|
|
NULL, NULL, cmd_pipe_pane_error_callback, wp);
|
|
|
|
bufferevent_enable(wp->pipe_event, EV_WRITE);
|
2009-12-04 23:14:47 +01:00
|
|
|
|
2011-01-22 00:44:13 +01:00
|
|
|
setblocking(wp->pipe_fd, 0);
|
2009-10-12 02:35:08 +02:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
}
|
2009-11-08 23:59:53 +01:00
|
|
|
|
2009-11-28 15:50:37 +01:00
|
|
|
/* ARGSUSED */
|
2009-11-08 23:59:53 +01:00
|
|
|
void
|
|
|
|
cmd_pipe_pane_error_callback(
|
|
|
|
unused struct bufferevent *bufev, unused short what, void *data)
|
|
|
|
{
|
|
|
|
struct window_pane *wp = data;
|
|
|
|
|
|
|
|
bufferevent_free(wp->pipe_event);
|
|
|
|
close(wp->pipe_fd);
|
|
|
|
wp->pipe_fd = -1;
|
|
|
|
}
|